forked from letscontrolit/ESPEasy
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request letscontrolit#5041 from tonhuisman/feature/P110-P1…
…13-Add-direction-option [P110]/[P113] Add Direction value, optimizations
- Loading branch information
Showing
14 changed files
with
167 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,16 @@ | |
// ###################################### [email protected] ########################################## | ||
// ####################################################################################################### | ||
|
||
// Changelog: | ||
// 2022-06-22, tonhuisman: Remove delay() call from begin(), handle delay via PLUGIN_FIFTY_PER_SECOND | ||
// Reformat source (uncrustify) | ||
// 2021-04-05, tonhuisman: Removed check for VL53L1X as that is not compatible with this driver (Got its own plugin P113) | ||
// 2021-02-06, tonhuisman: Refactored to use PluginStruct to enable multiple-instance use with an I2C Multiplexer | ||
// 2021-01-07, tonhuisman: Moved from PluginPlayground (P133) to main repo (P110), fixed some issues | ||
/** Changelog: | ||
* 2024-04-27 tonhuisman: Read sensor asynchronously to enable (the new default) trigger on changed value | ||
* 2024-04-26 tonhuisman: Migrate 'Send event when value unchanged' and 'Trigger delta' settings from P113 (at last...) | ||
* Add Direction value, -1 = closer, 0 = unchanged, 1 = further away | ||
* 2022-06-22 tonhuisman: Remove delay() call from begin(), handle delay via PLUGIN_FIFTY_PER_SECOND | ||
* Reformat source (uncrustify) | ||
* 2021-04-05 tonhuisman: Removed check for VL53L1X as that is not compatible with this driver (Got its own plugin P113) | ||
* 2021-02-06 tonhuisman: Refactored to use PluginStruct to enable multiple-instance use with an I2C Multiplexer | ||
* 2021-01-07 tonhuisman: Moved from PluginPlayground (P133) to main repo (P110), fixed some issues | ||
*/ | ||
|
||
// needs VL53L0X library from pololu https://github.com/pololu/vl53l0x-arduino | ||
|
||
|
@@ -21,6 +25,7 @@ | |
#define PLUGIN_ID_110 110 | ||
#define PLUGIN_NAME_110 "Distance - VL53L0X (200cm)" | ||
#define PLUGIN_VALUENAME1_110 "Distance" | ||
#define PLUGIN_VALUENAME2_110 "Direction" | ||
|
||
|
||
/////////////////////////// | ||
|
@@ -42,9 +47,10 @@ boolean Plugin_110(uint8_t function, struct EventStruct *event, String& string) | |
Device[deviceCount].PullUpOption = false; | ||
Device[deviceCount].InverseLogicOption = false; | ||
Device[deviceCount].FormulaOption = true; | ||
Device[deviceCount].ValueCount = 1; | ||
Device[deviceCount].ValueCount = 2; | ||
Device[deviceCount].SendDataOption = true; | ||
Device[deviceCount].TimerOption = true; | ||
Device[deviceCount].TimerOptional = true; | ||
Device[deviceCount].GlobalSyncOption = true; | ||
Device[deviceCount].PluginStats = true; | ||
break; | ||
|
@@ -59,6 +65,7 @@ boolean Plugin_110(uint8_t function, struct EventStruct *event, String& string) | |
case PLUGIN_GET_DEVICEVALUENAMES: | ||
{ | ||
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_110)); | ||
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_110)); | ||
break; | ||
} | ||
|
||
|
@@ -78,14 +85,14 @@ boolean Plugin_110(uint8_t function, struct EventStruct *event, String& string) | |
break; | ||
} | ||
|
||
# if FEATURE_I2C_GET_ADDRESS | ||
#if FEATURE_I2C_GET_ADDRESS | ||
case PLUGIN_I2C_GET_ADDRESS: | ||
{ | ||
event->Par1 = P110_I2C_ADDRESS; | ||
success = true; | ||
break; | ||
} | ||
# endif // if FEATURE_I2C_GET_ADDRESS | ||
#endif // if FEATURE_I2C_GET_ADDRESS | ||
|
||
case PLUGIN_WEBFORM_LOAD: | ||
{ | ||
|
@@ -105,6 +112,14 @@ boolean Plugin_110(uint8_t function, struct EventStruct *event, String& string) | |
const int optionValuesMode3[2] = { 0, 1 }; | ||
addFormSelector(F("Range"), F("prange"), 2, optionsMode3, optionValuesMode3, P110_RANGE); | ||
} | ||
addFormCheckBox(F("Send event when value unchanged"), F("notchanged"), P110_SEND_ALWAYS == 1); | ||
addFormNote(F("When checked, 'Trigger delta' setting is ignored!")); | ||
|
||
addFormNumericBox(F("Trigger delta"), F("delta"), P110_DELTA, 0, 100); | ||
addUnit(F("0-100mm")); | ||
#ifndef LIMIT_BUILD_SIZE | ||
addFormNote(F("Minimal change in Distance to trigger an event.")); | ||
#endif // ifndef LIMIT_BUILD_SIZE | ||
|
||
success = true; | ||
break; | ||
|
@@ -115,6 +130,8 @@ boolean Plugin_110(uint8_t function, struct EventStruct *event, String& string) | |
P110_I2C_ADDRESS = getFormItemInt(F("i2cAddr")); | ||
P110_TIMING = getFormItemInt(F("ptiming")); | ||
P110_RANGE = getFormItemInt(F("prange")); | ||
P110_SEND_ALWAYS = isFormItemChecked(F("notchanged")) ? 1 : 0; | ||
P110_DELTA = getFormItemInt(F("delta")); | ||
|
||
success = true; | ||
break; | ||
|
@@ -133,12 +150,37 @@ boolean Plugin_110(uint8_t function, struct EventStruct *event, String& string) | |
P110_data_struct *P110_data = static_cast<P110_data_struct *>(getPluginTaskData(event->TaskIndex)); | ||
|
||
if (nullptr != P110_data) { | ||
long dist = P110_data->readDistance(); | ||
const uint16_t dist = P110_data->getDistance(); | ||
const uint16_t p_dist = UserVar.getFloat(event->TaskIndex, 0); | ||
const int16_t direct = dist == p_dist ? 0 : (dist < p_dist ? -1 : 1); | ||
const bool triggered = (dist > (p_dist + P110_DELTA)) || (dist < (p_dist - P110_DELTA)); | ||
|
||
#ifdef P110_INFO_LOG | ||
|
||
success = P110_data->isReadSuccessful(); | ||
if (loglevelActiveFor(LOG_LEVEL_INFO)) { | ||
addLog(LOG_LEVEL_INFO, strformat(F("VL53L0x: Perform read: trig: %d, prev: %d, dist: %d"), triggered, p_dist, dist)); | ||
} | ||
#endif // ifdef P110_INFO_LOG | ||
|
||
if (success) { | ||
if (triggered || (P110_SEND_ALWAYS == 1)) { | ||
UserVar.setFloat(event->TaskIndex, 0, dist); // Value is classified as invalid when > 8190, so no conversion or 'split' needed | ||
UserVar.setFloat(event->TaskIndex, 1, direct); | ||
success = true; | ||
} | ||
} | ||
break; | ||
} | ||
|
||
case PLUGIN_TEN_PER_SECOND: // Handle sensor reading | ||
{ | ||
P110_data_struct *P110_data = static_cast<P110_data_struct *>(getPluginTaskData(event->TaskIndex)); | ||
|
||
if (nullptr != P110_data) { | ||
P110_data->readDistance(); | ||
|
||
if (P110_data->isReadSuccessful() && (Settings.TaskDeviceTimer[event->TaskIndex] == 0)) { // Trigger as soon as there's a valid | ||
// measurement and 0 interval is set | ||
Scheduler.schedule_task_device_timer(event->TaskIndex, millis() + 10); | ||
} | ||
} | ||
break; | ||
|
Oops, something went wrong.