1#include "../../defines.h"
7#ifdef KEY_UNIT_SENSOR_CLASS
9#include "../SensorClass/SensorClassType.h"
10#include "../SensorClass/KeyUnitSensorClass.h"
17void M5Callback(
char *parameter,
boolean flag)
19 SerialDebug.printf(
"M5.sensorCallbackSignature(%s,%d)\n", parameter, flag);
31#ifdef ULTRASONIC_I2C_SENSOR_CLASS
32#include "../SensorClass/SensorClassType.h"
33#include "../SensorClass/Sonic_I2CSensorClass.h"
38void M5CallbackValue(
char *parameter,
int value)
40 SerialDebug.printf(
"M5.sensorCallbackValueSignature(%s,%d)\n", parameter, value);
141#include <M5_DLight.h>
143M5_DLight _M5DLightSensor;
146boolean _M5DlightSensorALIVE =
false;
149boolean _PIRSensorALIVE =
true;
151void checkPIRSensorSensorStatus()
154 _PIRSensorALIVE =
true;
158void classifyM5DlightSensorValue(
int lux)
162 _M5DlightSensorALIVE =
false;
164 _M5DlightSensorALIVE =
true;
168void checkM5DlightSensorStatus()
171 uint16_t lux = _M5DLightSensor.getLUX();
174 classifyM5DlightSensorValue(lux);
175 SerialDebug.printf(
"checkM5DlightSensorStatus alive=%d\n",_M5DlightSensorALIVE );
180void statusM5ButtonModule()
182 checkM5DlightSensorStatus();
183 checkPIRSensorSensorStatus();
193void refreshDelayButtonTouched_M5ButtonModule()
195 refreshDelayButtonTouched_ButtonProcessing();
200#ifdef NOT_HERE_MOVED_TO_UNIT_CLASS
205uint8_t _ledColor = 0;
208void keyUnitLED(
void *parameter);
209void changeLedColor();
212#define KEY_UNIT_GROVE
218 pinMode(
KEY_PIN, INPUT_PULLUP);
221 FastLED.addLeds<SK6812,
DATA_PIN, GRB>(_leds,
225 keyUnitLED,
"led", 1000, NULL, 0,
231void changeButtonColor_M5ButtonModule()
234 SerialDebug.println(
"changeButtonColor_M5ButtonModule");
245 SerialDebug.println(
"M5ButtonModule.Key Pressed");
253 SerialDebug.println(
"Button.Key Released");
262void keyUnitLED(
void *parameter) {
263 _leds[0] = CRGB::Red;
265 for (
int i = 0; i < 255;
267 FastLED.setBrightness(i);
271 for (
int i = 255; i > 0;
273 FastLED.setBrightness(i);
281void changeLedColor() {
283 if (_ledColor > 2) _ledColor = 0;
287 _leds[0] = CRGB::Red;
290 _leds[0] = CRGB::Green;
293 _leds[0] = CRGB::Blue;
309int _previousLUXKind = LUX_SAME;
311#define SENSORS_MOTION_PIR
312#ifdef SENSORS_MOTION_PIR
314const uint32_t SLEEP_DURATION = 1 * 1000000;
316unsigned long _PrevSampleTime = 0;
317unsigned long _PrevTriggerTime = 0;
318unsigned long _InactivityTimeOut = 0;
320#define Elapsed3secs 3000
321#define Elapsed4secs 4000
323#define Elapsed3mins 180000
332 float diffXYZ = 0.0F;
346 float TILT_SENSITIVITY = 0.5;
352bool checkPIR_ButtonProcessing()
357 boolean triggeredPIR = digitalRead(PIR_PIN);
360 if ((
millis()-_PrevTriggerTime)>Elapsed4secs)
363 _PrevTriggerTime =
millis();
364 SerialLots.println(
"triggeredPIR = true");
367 triggeredPIR =
false;
373int _luxButtonCount = 0;
382int checkDLight_ButtonProcessing()
386 if (_luxButtonCount < LUXBUTTOMAX)
396 int LUXKind = LUX_LIGHT;
401 uint16_t lux = _M5DLightSensor.getLUX();
404 SerialLots.printf(
"mid: checkDLight_ButtonProcessing(lux=%d)\n", lux);
405 classifyM5DlightSensorValue(lux);
411 if (lux < luxThresholdDark)
423 boolean changedLight =
true;
425 if (LUXKind == _previousLUXKind)
427 changedLight =
false;
432 _previousLUXKind = LUXKind;
433 SerialLots.printf(
"DLight LUX changed = lux=%d == kind=%d\n", lux, LUXKind);
436 SerialLots.printf(
"end: checkDLight_ButtonProcessing kind=%d\n", LUXKind);
442bool checkMotion_ButtonProcessing()
461 if ( (
millis() - _PrevSampleTime) >100)
464 _PrevSampleTime =
millis();
469 M5.IMU.getAccelData(&_IMU.accX,&_IMU.accY,&_IMU.accZ);
478 _IMU.diffX = abs(_IMU.prevX - _IMU.accX);
479 _IMU.diffY = abs(_IMU.prevY - _IMU.accY);
480 _IMU.diffZ = abs(_IMU.prevZ - _IMU.accZ);
481 _IMU.diffXYZ = _IMU.diffX + _IMU.diffY + _IMU.diffZ;
485 _IMU.prevX = _IMU.accX;
486 _IMU.prevY = _IMU.accY;
487 _IMU.prevZ = _IMU.accZ;
490 if ( (_IMU.diffXYZ > _IMU.TILT_SENSITIVITY) && ((
millis()-_PrevTriggerTime)>Elapsed4secs) )
492 _PrevTriggerTime =
millis();
493 SerialLots.printf(
"diff: %.2f \r\n", _IMU.diffXYZ);
508void setup_M5ButtonModule()
516#ifdef KEY_UNIT_SENSOR_CLASS
526#ifdef ULTRASONIC_I2C_SENSOR_CLASS
531 _Sonic_I2CSensorClass->
setup();
536 pinMode(PIR_PIN,INPUT_PULLUP);
546 _InactivityTimeOut = Elapsed3mins;
561 SerialDebug.println(
"_M5DLightSensor sensor begin.....");
563 _M5DLightSensor.begin();
571 _M5DLightSensor.setMode(CONTINUOUSLY_H_RESOLUTION_MODE);
574 checkM5DlightSensorStatus();
580void loopCode_M5ButtonProcessing();
584boolean _buttonPressed;
588void checkButtonB_M5ButtonProcessing()
591 boolean buttonTouched =
true;
596 if (M5.BtnB.wasReleasefor(500))
598 SerialTemp.println(
"buttonB_LongPress");
605 if (M5.BtnB.wasReleased())
607 SerialTemp.println(
"buttonB_ShortPress");
609 buttonB_ShortPress();
614 buttonTouched =
false;
619 refreshDelayButtonTouched_M5ButtonModule();
626void checkButtonA_M5ButtonProcessing()
628 boolean buttonTouched =
true;
632 if (M5.BtnA.wasReleasefor(500))
634 SerialDebug.println(
"buttonA LONG touched");
639 if (M5.BtnA.wasReleased())
641 SerialDebug.println(
"buttonA SHORT touched");
643 buttonA_ShortPress();
647 buttonTouched =
false;
652 refreshDelayButtonTouched_M5ButtonModule();
664void loop_M5ButtonModule()
671 _buttonState = digitalRead(_buttonPin);
672 switch (_buttonState)
675 _buttonPressed =
true;
683 SerialLots.println(
"BUTTON PRESSED and RELEASED");
684 _buttonPressed =
false;
694 loopCode_M5ButtonProcessing();
698void loopCode_M5ButtonProcessing()
706#ifdef KEY_UNIT_SENSOR_CLASS
712#ifdef ULTRASONIC_I2C_SENSOR_CLASS
713 _Sonic_I2CSensorClass->
loop();
720 if (checkMotion_ButtonProcessing())
722 SerialTemp.println(
"motion detected");
730 if (checkPIR_ButtonProcessing())
732 SerialTemp.println(
"PIR detected");
743 SerialDebug.printf(
"SM_ON_PIR command = %s\n", smCommand);
761 if (_M5DlightSensorALIVE)
765 int DLightKind = checkDLight_ButtonProcessing();
768 if (_M5DlightSensorALIVE)
771 if (DLightKind != LUX_SAME)
773 SerialTemp.printf(
"DLight detected: %d\n", DLightKind);
789 SerialDebug.printf(
"SM_ON_PIR_OFF command = %s\n", smCommand);
809 SerialDebug.printf(
"SM_ON_PIR command = %s\n", smCommand);
826 checkButtonA_M5ButtonProcessing();
827 checkButtonB_M5ButtonProcessing();
832void buttonA_ShortPress_M5ButtonModule()
837void buttonA_LongPress_M5ButtonModule()
842void buttonB_LongPress_M5ButtonModule()
847void buttonB_ShortPress_M5ButtonModule()
KeyUnitSensorClass * _KeyUnitSensorClass
int getLUXThreshold_mainModule(int thresholdKind)
get the threshold val
void main_dispatchAsyncCommandWithString(int asyncCallCommand, char *parameter)
void callCallbackMain(int callbacksModuleId, int callbackType, char *message)
performs the indirect callback based on the callbackType
void sendMessageString_mainModule(char *messageString)
adding a synchronous call to send a message over the network (assuming MQTT but not specified),...
void setCurrentSMMode_mainModule(int whichSMMode)
sets the current screen mode .. which can be used by Button and Display processing
void main_dispatchAsyncCommand(int asyncCallCommand)
checks if any async commands are in 'dispatch' mode, and if so, invokes them, and sets their flag to ...
#define CALLBACKS_BUTTON_MODULE
#define THRESHOLD_KIND_DARK
#define ASYNC_SEND_MQTT_FEED_MESSAGE
sends a message (like FEED) on the users topic
#define ASYNC_JSON_MQTT_MESSAGE_PARAMETER
these are the async with a string parameter
boolean getPreferenceBoolean_mainModule(int preferenceID)
called to set a preference (which will be an identifier and a string, which can be converted to a num...
char * getPreference_mainModule(int preferenceID)
#define PREFERENCE_SENSOR_TILT_VALUE
Sensor preferences.
#define PREFERENCE_SM_COMMAND_PIR_OFF_SETTING
1.11.24 The Semantic Marker command is sent on PIR, and the Command to send on OFF (or opposite)
#define PREFERENCE_SM_ON_PIR_SETTING
1.10.24 Flag on whether a Semantic Marker command is sent on PIR, and the Command to send
#define PREFERENCE_SM_COMMAND_PIR_SETTING
1.10.24 The Semantic Marker command is sent on PIR, and the Command to send
void setup()
Pure Virtual Function.
void loop()
Pure Virtual Function.
void registerCallback(sensorCallbackSignature *callback)
void registerCallbackValue(sensorCallbackValueSignature *callback)
void loop()
Pure Virtual Function.
void setup()
Pure Virtual Function.