Skip to content

Commit

Permalink
Revert dynamic delay and fix bug in readCount
Browse files Browse the repository at this point in the history
  • Loading branch information
noranraskin committed Jan 2, 2024
1 parent 5182668 commit 21d6f8b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=MT6701
version=1.0.1
version=1.0.2
author=Noran Raskin
maintainer=Noran Raskin <dev@noranraskin.com>
sentence=Use the MT6701 magnet rotary encoder with ESP.
Expand Down
29 changes: 15 additions & 14 deletions src/MT6701.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ int MT6701::getCount()
void MT6701::updateCount()
{
int newCount = readCount();
// give it four tries
for (int i = 0; i < 3 && newCount < 0; i++, newCount = readCount())
;
if (newCount < 0)
{
return;
Expand Down Expand Up @@ -171,31 +174,29 @@ int MT6701::readCount()
Wire.endTransmission(false); // End transmission, but keep the I2C bus active
Wire.requestFrom((int)address, 2); // Request two bytes
unsigned long startTime = millis();
while (Wire.available() < 2)
if (Wire.available() < 2)
{
if (millis() - startTime > 100)
{
return -1;
}
return -1;
}

int angle_h = data[0];
int angle_l = data[1] >> 2;
int angle_h = Wire.read();
int angle_l = Wire.read();

return (angle_h << 6) | angle_l; // returns value from 0 to 16383
}

void MT6701::updateTask(void *pvParameters)
{
MT6701 *mt6701 = static_cast<MT6701 *>(pvParameters);
TickType_t delay = pdMS_TO_TICKS(mt6701->updateIntervalMillis);
while (true)
{
mt6701->updateCount();
unsigned int delayMS = mt6701->lastUpdateTime + mt6701->updateIntervalMillis - millis();
if (delayMS > 0)
{
TickType_t delay = pdMS_TO_TICKS(delayMS);
vTaskDelay(delay);
}
vTaskDelay(delay);
// unsigned int delayMS = mt6701->lastUpdateTime + mt6701->updateIntervalMillis - millis();
// if (delayMS > 0)
// {
// TickType_t delay = pdMS_TO_TICKS(delayMS);
// vTaskDelay(delay);
// }
}
}

0 comments on commit 21d6f8b

Please sign in to comment.