ESP_IOT v2.5
IOT ESP Coding
M5ButtonModule.cpp
Go to the documentation of this file.
1#include "../../defines.h"
2
3#ifdef M5BUTTON_MODULE
4
5//#define TRY_SENSOR_CLASS
6//! Defined in defines.h
7#ifdef KEY_UNIT_SENSOR_CLASS
8
9#include "../SensorClass/SensorClassType.h"
10#include "../SensorClass/KeyUnitSensorClass.h"
11
12//! instance of the class
14
15
16//a pointer to a callback function that takes (char*) and returns void
17void M5Callback(char *parameter, boolean flag)
18{
19 SerialDebug.printf("M5.sensorCallbackSignature(%s,%d)\n", parameter, flag);
20
21 sendMessageString_mainModule((char*)"M5.KEY Pressed -- call feed");
22
23 //This (in mainModule) will look if connected so sends over BLE, otherwise MQTT (if connected)
25}
26#endif
27
28
29//! Ultrasonic-I2C
30//! 2.23.24 after nice ski day, Da King beautiful weather (Laura along)
31#ifdef ULTRASONIC_I2C_SENSOR_CLASS
32#include "../SensorClass/SensorClassType.h"
33#include "../SensorClass/Sonic_I2CSensorClass.h"
34//! instance of the class
35Sonic_I2CSensorClass *_Sonic_I2CSensorClass;
36
37//a pointer to a callback function that takes (char*) and returns void
38void M5CallbackValue(char *parameter, int value)
39{
40 SerialDebug.printf("M5.sensorCallbackValueSignature(%s,%d)\n", parameter, value);
41
42 //sendMessageString_mainModule((char*)"M5.KEY Pressed -- call feed");
43
44 //This (in mainModule) will look if connected so sends over BLE, otherwise MQTT (if connected)
45 //main_dispatchAsyncCommand(ASYNC_SEND_MQTT_FEED_MESSAGE);
46}
47
48#endif
49
50/**
51 * \link ButtonModule
52 */
53/*
54 *******************************************************************************
55 // https://randomnerdtutorials.com/esp32-pinout-reference-gpios/
56
57 Enable (EN)
58 Enable (EN) is the 3.3V regulator␖â␖™s enable pin. It␖â␖™s pulled up, so connect to ground to disable the 3.3V regulator. This means that you can use this pin connected to a pushbutton to restart your ESP32, for example.
59
60 Scott Version: 1.1.2022
61 *******************************************************************************
62 */
63
64//@see https://github.com/m5stack/M5StickC-Plus
65//@see https://github.com/m5stack/M5StickC-Plus/blob/master/src/utility/Button.h
66
67//! 3.3.22 Using the new JSON library which is supposed to catch syntax errors without blowing up
68//https://arduinojson.org/?utm_source=meta&utm_medium=library.properties
69
70/**
71 class Button {
72 public:
73 Button(uint8_t pin, uint8_t invert, uint32_t dbTime);
74 uint8_t read();
75 */
76/*----------------------------------------------------------------------*
77 * isPressed() and isReleased() check the button state when it was last *
78 * read, and return false (0) or true (!=0) accordingly. *
79 * These functions do not cause the button to be read. *
80 *----------------------------------------------------------------------*/
81/*
82 uint8_t isPressed();
83 uint8_t isReleased();
84 */
85/*----------------------------------------------------------------------*
86 * wasPressed() and wasReleased() check the button state to see if it *
87 * changed between the last two reads and return false (0) or *
88 * true (!=0) accordingly. *
89 * These functions do not cause the button to be read. *
90 *----------------------------------------------------------------------*/
91/*
92 uint8_t wasPressed();
93 uint8_t wasReleased();
94 */
95/*----------------------------------------------------------------------*
96 * pressedFor(ms) and releasedFor(ms) check to see if the button is *
97 * pressed (or released), and has been in that state for the specified *
98 * time in milliseconds. Returns false (0) or true (1) accordingly. *
99 * These functions do not cause the button to be read. *
100 *----------------------------------------------------------------------*/
101
102/*
103 uint8_t pressedFor(uint32_t ms);
104 uint8_t releasedFor(uint32_t ms);
105 uint8_t wasReleasefor(uint32_t ms);
106 */
107/*----------------------------------------------------------------------*
108 * lastChange() returns the time the button last changed state, *
109 * in milliseconds. *
110 *----------------------------------------------------------------------*/
111/*
112 uint32_t lastChange();
113 */
114/*
115 class Button {
116 public:
117 Button(uint8_t pin, uint8_t invert, uint32_t dbTime);
118 uint8_t read();
119 uint8_t isPressed();
120 uint8_t isReleased();
121 uint8_t wasPressed();
122 uint8_t wasReleased();
123 uint8_t pressedFor(uint32_t ms);
124 uint8_t releasedFor(uint32_t ms);
125 uint8_t wasReleasefor(uint32_t ms);
126 uint32_t lastChange();
127 */
128#ifdef ESP_M5
129#ifdef M5STICKCPLUS2
130//#include <M5StickCPlus.h>
131#endif
132#endif
133
134
135//extension of ButtonModule - includes Sensors
136
137//! 1.11.24 work on the DM5_DLight sensor
138//! @Links [Unit DLight](https://docs.m5stack.com/en/unit/dlight)
139//! @Links [HAT DLight](https://docs.m5stack.com/en/hat/hat_dlight)
140//! This will use the PIR Semantic Marker logic (although it needs an OFF or the opposite of the ON)
141#include <M5_DLight.h>
142//! the sensor object for the DLight
143M5_DLight _M5DLightSensor;
144
145//! this will be set in the setup()
146boolean _M5DlightSensorALIVE = false;
147
148//! ALIVE set on setup() or status()
149boolean _PIRSensorALIVE = true;
150//! check sensor alive
151void checkPIRSensorSensorStatus()
152{
153 //! this one is so fast it doesn't matter
154 _PIRSensorALIVE = true;
155}
156
157//! classify the status
158void classifyM5DlightSensorValue(int lux)
159{
160 //! if > = 65535 then it's not plugged in..
161 if (lux > 65000)
162 _M5DlightSensorALIVE = false;
163 else
164 _M5DlightSensorALIVE = true;
165}
166
167//! check sensor alive
168void checkM5DlightSensorStatus()
169{
170 //! get the sensor value
171 uint16_t lux = _M5DLightSensor.getLUX();
172
173 //!classify which sets the _M5DLightSensorAlive flag
174 classifyM5DlightSensorValue(lux);
175 SerialDebug.printf("checkM5DlightSensorStatus alive=%d\n",_M5DlightSensorALIVE );
176
177}
178
179//! this status will be called and let the ALIVE re-evaluate
180void statusM5ButtonModule()
181{
182 checkM5DlightSensorStatus();
183 checkPIRSensorSensorStatus();
184}
185
186
187
188//#include <M5Display.h>
189
190#include "M5ButtonModule.h"
191
192//! called by the feed operation to say the device is still running.. and count it as a button click.
193void refreshDelayButtonTouched_M5ButtonModule()
194{
195 refreshDelayButtonTouched_ButtonProcessing();
196}
197
198// See https://forum.arduino.cc/t/arduino-push-button-double-click-function/409353
199//!moved to the KeyUnitSensorClass ....
200#ifdef NOT_HERE_MOVED_TO_UNIT_CLASS
201
202#define USE_LED
203#ifdef USE_LED
204#include <FastLED.h>
205uint8_t _ledColor = 0;
206#define DATA_PIN 32 // Define LED pin. 定义LED引脚.
207CRGB _leds[1]; // Define the array of leds. 定义LED阵列.
208void keyUnitLED(void *parameter);
209void changeLedColor();
210#endif
211
212#define KEY_UNIT_GROVE
213#ifdef KEY_UNIT_GROVE
214#define KEY_PIN 33 //Define Key Pin. 定义Key引脚
215//!setup the KeyUnit
216void setupKeyUnit()
217{
218 pinMode(KEY_PIN, INPUT_PULLUP); // Init Key pin. 初始化Key引脚.
219
220#ifdef USE_LED
221 FastLED.addLeds<SK6812, DATA_PIN, GRB>(_leds,
222 1); // Init FastLED. 初始化FastLED.
223
224 xTaskCreate(
225 keyUnitLED, "led", 1000, NULL, 0,
226 NULL); // Create a thread for breathing LED. 创建一个线程用于LED呼吸灯.
227#endif
228}
229
230//! 2.21.25 add a way to change the button color (if any)
231void changeButtonColor_M5ButtonModule()
232{
233#ifdef USE_LED
234 SerialDebug.println("changeButtonColor_M5ButtonModule");
235 changeLedColor(); // Change LED color. 更换LED呼吸灯颜色.
236#endif
237}
238
239//! loop the key unit (after other called M5.updfate)
240void loopKeyUnit()
241{
242 if (!digitalRead(KEY_PIN))
243 {
244 // If Key was pressed. 如果按键按下.
245 SerialDebug.println("M5ButtonModule.Key Pressed");
246#ifdef USE_LED
247 changeLedColor(); // Change LED color. 更换LED呼吸灯颜色.
248#endif
249
250 while (!digitalRead(KEY_PIN))
251 // Hold until the key released. 在松开按键前保持状态.
252 ;
253 SerialDebug.println("Button.Key Released");
254
255 sendMessageString_mainModule((char*)"KEY Pressed -- call feed");
256
257 //This (in mainModule) will look if connected so sends over BLE, otherwise MQTT (if connected)
259 }
260}
261#ifdef USE_LED
262void keyUnitLED(void *parameter) {
263 _leds[0] = CRGB::Red;
264 for (;;) {
265 for (int i = 0; i < 255;
266 i++) { // Set LED brightness from 0 to 255. 设置LED亮度从0到255.
267 FastLED.setBrightness(i);
268 FastLED.show();
269 delay(5);
270 }
271 for (int i = 255; i > 0;
272 i--) { // Set LED brightness from 255 to 0. 设置LED亮度从255到0.
273 FastLED.setBrightness(i);
274 FastLED.show();
275 delay(5);
276 }
277 }
278 vTaskDelete(NULL);
279}
280
281void changeLedColor() {
282 _ledColor++;
283 if (_ledColor > 2) _ledColor = 0;
284 switch (_ledColor)
285 { // Change LED colors between R,G,B. 在红绿蓝中切换LED颜色.
286 case 0:
287 _leds[0] = CRGB::Red;
288 break;
289 case 1:
290 _leds[0] = CRGB::Green;
291 break;
292 case 2:
293 _leds[0] = CRGB::Blue;
294 break;
295 default:
296 break;
297 }
298}
299#endif
300#endif
301
302#endif // TRY_SENSOR_CLASS
303
304
305//! set a previous LUX so can see if changed from light to dark
306#define LUX_DARK -1
307#define LUX_SAME 0
308#define LUX_LIGHT 1
309int _previousLUXKind = LUX_SAME;
310
311#define SENSORS_MOTION_PIR
312#ifdef SENSORS_MOTION_PIR
313#define PIR_PIN 36 //passive IR Hat
314const uint32_t SLEEP_DURATION = 1 * 1000000;
315
316unsigned long _PrevSampleTime = 0;
317unsigned long _PrevTriggerTime = 0;
318unsigned long _InactivityTimeOut = 0;
319
320#define Elapsed3secs 3000
321#define Elapsed4secs 4000
322
323#define Elapsed3mins 180000 // 3 minutes in milliseconds
324struct IMUVariables
325{
326 float accX = 0.0F;
327 float accY = 0.0F;
328 float accZ = 0.0F;
329 float diffX = 0.0F;
330 float diffY = 0.0F;
331 float diffZ = 0.0F;
332 float diffXYZ = 0.0F;
333 float prevX = 0.0F;
334 float prevY = 0.0F;
335 float prevZ = 0.0F;
336#ifdef GYRO
337 float gyroX = 0.0F;
338 float gyroY = 0.0F;
339 float gyroZ = 0.0F;
340#endif
341#ifdef PITCH_ROLL
342 float pitch = 0.0F;
343 float roll = 0.0F;
344 float yaw = 0.0F;
345#endif
346 float TILT_SENSITIVITY = 0.5;
347} _IMU;
348
349
350//!Return "true" on PIR (over sensitivity) and false otherwise
351//!This is just a sensor (if plugged in) - so any timing is on the caller of this
352bool checkPIR_ButtonProcessing()
353{
354 // if (!getPreferenceBoolean_mainModule(PREFERENCE_SENSOR_PIR_VALUE))
355 // return false;
356
357 boolean triggeredPIR = digitalRead(PIR_PIN);
358 if (triggeredPIR)
359 {
360 if ((millis()-_PrevTriggerTime)>Elapsed4secs)
361 {
362 triggeredPIR = true;
363 _PrevTriggerTime = millis();
364 SerialLots.println("triggeredPIR = true");
365 }
366 else
367 triggeredPIR = false;
368 }
369 return triggeredPIR;
370}
371
372//! only check the LUX every MAX loop times..
373int _luxButtonCount = 0;
374#define LUXBUTTOMAX 5
375//! 1.23.24 Seems the getLUX() is a VERY SLOW operation and breaks
376//! button timing, etc.
377//!1.11.24 work on the DHat light sensor
378//!Return "true" on PIR (over sensitivity) and false otherwise
379//!This is just a sensor (if plugged in) - so any timing is on the caller of this
380//!returns the return 0 == no change, -1 = dark, and 1 == light
381//!@see https://github.com/konacurrents/ESP_IOT/issues/287
382int checkDLight_ButtonProcessing()
383{
384#ifdef NONO
385 _luxButtonCount++;
386 if (_luxButtonCount < LUXBUTTOMAX)
387 {
388 return LUX_SAME;
389 }
390 else
391 {
392 _luxButtonCount = 0;
393 }
394#endif
395 //! determing what the LUX values mean..
396 int LUXKind = LUX_LIGHT;
397 //SerialTemp.println("start: checkDLight_ButtonProcessing");
398#ifdef ESP_M5
399
400 //! get the sensor value
401 uint16_t lux = _M5DLightSensor.getLUX();
402
403 //! if > = 65535 then it's not plugged in..
404 SerialLots.printf("mid: checkDLight_ButtonProcessing(lux=%d)\n", lux);
405 classifyM5DlightSensorValue(lux);
406
407 //! grab the temporary threshold
408 int luxThresholdDark = getLUXThreshold_mainModule(THRESHOLD_KIND_DARK);
409
410 //!TODO: set this threshold via a EPROM value
411 if (lux < luxThresholdDark)
412 {
413 //! dark
414 LUXKind = LUX_DARK;
415
416 }
417 else
418 {
419 //! low light different (Note: it might be nice to have variations ...)
420 LUXKind = LUX_LIGHT;
421 }
422
423 boolean changedLight = true;
424 //! has this transitioned from drak to light??
425 if (LUXKind == _previousLUXKind)
426 {
427 changedLight = false;
428 LUXKind = LUX_SAME;
429 }
430 if (changedLight)
431 {
432 _previousLUXKind = LUXKind;
433 SerialLots.printf("DLight LUX changed = lux=%d == kind=%d\n", lux, LUXKind);
434 }
435#endif
436 SerialLots.printf("end: checkDLight_ButtonProcessing kind=%d\n", LUXKind);
437
438 return LUXKind;
439}
440//!looks at M5.IMU sensor to see if changed since last time..
441//!Return "true" on motion (over sensitivity) and false otherwise
442bool checkMotion_ButtonProcessing()
443{
444 //! if not an M5 return
445#ifndef ESP_M5
446 return false;
447#endif
448 //! if the TILT isn't an option.. then never return true ..
450 {
451 return false;
452 }
453 else
454 {
455 // main_printModuleConfiguration();
456 // SerialTemp.println("**** SENSOR TILT IS ON");
457 // main_printModuleConfiguration();
458 }
459
460 // 100ms sample interval
461 if ( (millis() - _PrevSampleTime) >100)
462 {
463 //update for next sample point
464 _PrevSampleTime = millis();
465#ifdef ESP_M5
466 // M5.IMU.getGyroData(&_IMU.gyroX,&_IMU.gyroY,&_IMU.gyroZ);
467#ifndef M5STICKCPLUS2
468
469 M5.IMU.getAccelData(&_IMU.accX,&_IMU.accY,&_IMU.accZ);
470 // M5.IMU.getAhrsData(&_IMU.pitch,&_IMU.roll,&_IMU.yaw);
471
472 //debug print
473 //SerialTemp.printf("%5.2f %5.2f %5.2f \n\r", _IMU.accX, _IMU.accY, _IMU.accZ);
474 // SerialTemp.printf("%5.2f %5.2f %5.2f \n\r", _IMU.gyroX, _IMU.gyroY, _IMU.gyroZ);
475 // SerialTemp.printf("%5.2f %5.2f %5.2f \n\r", _IMU.pitch, _IMU.roll, _IMU.yaw);
476#endif
477#endif
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;
482
483 //save x,y,z from this cycle for next cycle
484
485 _IMU.prevX = _IMU.accX;
486 _IMU.prevY = _IMU.accY;
487 _IMU.prevZ = _IMU.accZ;
488 }
489 //if the movement is above threshold sensitivity then broadcast and start IGNORE time
490 if ( (_IMU.diffXYZ > _IMU.TILT_SENSITIVITY) && ((millis()-_PrevTriggerTime)>Elapsed4secs) )
491 {
492 _PrevTriggerTime = millis();
493 SerialLots.printf("diff: %.2f \r\n", _IMU.diffXYZ);
494 // TILT has exceed threshold
495 return true;
496 }
497 else
498 {
499 //movement does not exceed threshold
500 return false;
501 }
502} // end of CheckMotion
503#endif // SENSORS_MOTION_PIR
504
505
506//!the setup for buttonProcessing (extension of ButtonModule)
507//! 在 M5StickC Plus 启动或者复位后,即会开始执行setup()函数中的程序,该部分只会执行一次。
508void setup_M5ButtonModule()
509{
511
512#ifdef KEY_UNIT_GROVE
513 //!setup the KeyUnit
514 setupKeyUnit();
515#endif //KEY_UNIT_GROVE
516#ifdef KEY_UNIT_SENSOR_CLASS
517 _KeyUnitSensorClass = new KeyUnitSensorClass((char*)"KeyUnitInstanceM5Button");
518 //! specify the callback
520 //! call the setup
522#endif
523
524 //! Ultrasonic-I2C
525 //! 2.23.24 after nice ski day, Da King beautiful weather (Laura along)
526#ifdef ULTRASONIC_I2C_SENSOR_CLASS
527 _Sonic_I2CSensorClass = new Sonic_I2CSensorClass((char*)"Sonic_I2CSensorClass");
528 //! specify the callback
529 _Sonic_I2CSensorClass->registerCallbackValue(&M5CallbackValue);
530 //! call the setup
531 _Sonic_I2CSensorClass->setup();
532#endif
533
534#ifdef ESP_M5
535 //!setup the plugin PIR (if there..)
536 pinMode(PIR_PIN,INPUT_PULLUP);
537
538 //M5.begin(); already called..
539 //Init IMU. 初始化IMU
540
541#ifndef M5STICKCPLUS2
542
543 M5.Imu.Init();
544 M5.Axp.begin();
545#endif
546 _InactivityTimeOut = Elapsed3mins;
547
548 //!https://docs.m5stack.com/en/api/stickc/pwm
549 //!
550 // ledcSetup(_channelBuzzer, _freq, _resolution_bits);
551 // ledcAttachPin(_buzzerPin, _channelBuzzer);
552 // M5.Beep.begin();
553
554 //! M5DHAT hangs with 2.1.0 M5 board..
555//#if defined(M5STACK_VERSION) && ("2.0.4" == "2.0.4")
556#ifdef M5STICKCPLUS2
557#else
558#define M5DHAT
559#endif
560#ifdef M5DHAT
561 SerialDebug.println("_M5DLightSensor sensor begin.....");
562 Wire.begin(0,26);
563 _M5DLightSensor.begin(); // HAT DLight
564
565 // CONTINUOUSLY_H_RESOLUTION_MODE
566 // CONTINUOUSLY_H_RESOLUTION_MODE2
567 // CONTINUOUSLY_L_RESOLUTION_MODE
568 // ONE_TIME_H_RESOLUTION_MODE
569 // ONE_TIME_H_RESOLUTION_MODE2
570 // ONE_TIME_L_RESOLUTION_MODE
571 _M5DLightSensor.setMode(CONTINUOUSLY_H_RESOLUTION_MODE);
572
573 //! 1.23.24 check the sensor status
574 checkM5DlightSensorStatus();
575#endif
576
577#endif
578}
579//!the loop for buttonProcessing (extension of ButtonModule)
580void loopCode_M5ButtonProcessing();
581
582#ifdef ESP_32
583//state of the button..
584boolean _buttonPressed;
585#endif
586
587//!small button on right side of M5StickC Plus
588void checkButtonB_M5ButtonProcessing()
589{
590 // SerialTemp.println("checkButtonB_ButtonProcessing");
591 boolean buttonTouched = true;
592#ifdef ESP_M5
593
594#ifndef M5STICKCPLUS2
595 //was 1000
596 if (M5.BtnB.wasReleasefor(500))
597 {
598 SerialTemp.println("buttonB_LongPress");
599
600 buttonB_LongPress();
601 }
602 //side button.. cycles through choices..
603 else
604#endif // not Cplus2
605 if (M5.BtnB.wasReleased())
606 {
607 SerialTemp.println("buttonB_ShortPress");
608
609 buttonB_ShortPress();
610 }
611 else
612 {
613
614 buttonTouched = false;
615 }
616 //if a button was touched, update the delay since no touch..
617 if (buttonTouched)
618 {
619 refreshDelayButtonTouched_M5ButtonModule();
620 }
621#endif // ESP_M5
622
623}
624
625//!big button on front of M5StickC Plus
626void checkButtonA_M5ButtonProcessing()
627{
628 boolean buttonTouched = true;
629#ifdef ESP_M5
630#ifndef M5STICKCPLUS2
631 //was 1000
632 if (M5.BtnA.wasReleasefor(500))
633 {
634 SerialDebug.println("buttonA LONG touched");
635 buttonA_LongPress();
636 }
637 else
638#endif
639 if (M5.BtnA.wasReleased())
640 {
641 SerialDebug.println("buttonA SHORT touched");
642
643 buttonA_ShortPress();
644 }
645 else
646 {
647 buttonTouched = false;
648 }
649 //if a button was touched, update the delay since no touch..
650 if (buttonTouched)
651 {
652 refreshDelayButtonTouched_M5ButtonModule();
653 }
654
655
656#endif //ESP_M5
657}
658
659
660//if _buttonPressed (the LOW state == 0)
661// then wait for the HIGH state == 1
662// and that is a button click..
663
664void loop_M5ButtonModule()
665{
666 //TODO.. check if this is needed for the old ESP32 devices...
667#ifdef ESP_32
668 // put your main code here, to run repeatedly:
669 int _buttonState;
670
671 _buttonState = digitalRead(_buttonPin);
672 switch (_buttonState)
673 {
674 case LOW: //0
675 _buttonPressed = true;
676 break;
677 default:
678
679 case HIGH: // 1 (nothing touched)
680 if (_buttonPressed)
681 {
682 // then the button was unpressed..
683 SerialLots.println("BUTTON PRESSED and RELEASED");
684 _buttonPressed = false;
685
687
688 }
689 break;
690 }
691 //SerialLots.printf("_buttonState = %d\n", _buttonState);
692#endif
693 //!calls the extension of ButtonModule
694 loopCode_M5ButtonProcessing();
695}
696
697//!the loop for buttonProcessing (extension of ButtonModule)
698void loopCode_M5ButtonProcessing()
699{
700
701 M5.update(); //Read the press state of the key. ONLY call once per loop or the status of B button is lost
702#ifdef KEY_UNIT_GROVE
703 //!setup the KeyUnit
704 loopKeyUnit();
705#endif //KEY_UNIT_GROVE
706#ifdef KEY_UNIT_SENSOR_CLASS
708#endif
709
710 //! Ultrasonic-I2C
711 //! 2.23.24 after nice ski day, Da King beautiful weather (Laura along)
712#ifdef ULTRASONIC_I2C_SENSOR_CLASS
713 _Sonic_I2CSensorClass->loop();
714#endif
715
716 //! ALIVE set on setup() or status()
717 if (_PIRSensorALIVE)
718 {
719 // check for tilt etc..
720 if (checkMotion_ButtonProcessing())
721 {
722 SerialTemp.println("motion detected");
723 sendMessageString_mainModule((char*)"M5.TiltDetected");
724
725 //This (in mainModule) will look if connected so sends over BLE, otherwise MQTT (if connected)
727 }
728
729 // check for PIR etc..
730 if (checkPIR_ButtonProcessing())
731 {
732 SerialTemp.println("PIR detected");
733 sendMessageString_mainModule((char*)"PIRDetected");
734
735 //! 1.10.24 support optionally sending a Sematic Marker command (MQTT instead)
736 //! if on then call MQTT insteal.
738 {
739 //! 1.10.24 Flag on whether a Semantic Marker command is sent on PIR, and the Command to send
740 //! 1.10.24 The Semantic Marker command is sent on PIR, and the Command to send
742
743 SerialDebug.printf("SM_ON_PIR command = %s\n", smCommand);
744
745 //This (in mainModule) will look if connected so sends over BLE, otherwise MQTT (if connected)
746 //!send an async call with a string parameter. This will set store the value and then async call the command (passing the parameter)
747 //!These are the ASYNC_CALL_PARAMETERS_MAX
749 }
750 else
751 {
752 //This (in mainModule) will look if connected so sends over BLE, otherwise MQTT (if connected)
754 }
755 }
756 }
757#define TRY_WITHOUT
758#ifdef TRY_WITHOUT
759
760 //! ALIVE set on setup() or status()
761 if (_M5DlightSensorALIVE)
762 {
763
764 //! 1.11.24 check for Dlight LUX DARK, LIGHT or same
765 int DLightKind = checkDLight_ButtonProcessing();
766
767 //! the sensor is evaluated again .. so if unplugged then ALIVE is false
768 if (_M5DlightSensorALIVE)
769 {
770 //! now if it changed ..
771 if (DLightKind != LUX_SAME)
772 {
773 SerialTemp.printf("DLight detected: %d\n", DLightKind);
774 switch (DLightKind)
775 {
776 //! in this sensor, the "OFF" is when the LIGHT is ON
777 case LUX_LIGHT:
778 sendMessageString_mainModule((char*)"DLight LUX Light");
779
780 //! 1.10.24 support optionally sending a Sematic Marker command (MQTT instead)
781 //! if on then call MQTT insteal.
783 {
784 //! 1.10.24 Flag on whether a Semantic Marker command is sent on PIR, and the Command to send
785 //! 1.10.24 The Semantic Marker command is sent on PIR, and the Command to send
786 //char *smCommand = (char*) "{'set':'socket','val':'off'}"; //getPreference_mainModule(PREFERENCE_SM_COMMAND_PIR_SETTING);
788
789 SerialDebug.printf("SM_ON_PIR_OFF command = %s\n", smCommand);
790
791 //This (in mainModule) will look if connected so sends over BLE, otherwise MQTT (if connected)
792 //!send an async call with a string parameter. This will set store the value and then async call the command (passing the parameter)
793 //!These are the ASYNC_CALL_PARAMETERS_MAX
795 }
796 break;
797 case LUX_DARK:
798 //! use the LIGHT for the "ON"
799 sendMessageString_mainModule((char*)"DLight LUX Dark");
800
801 //! 1.10.24 support optionally sending a Sematic Marker command (MQTT instead)
802 //! if on then call MQTT insteal.
804 {
805 //! 1.10.24 Flag on whether a Semantic Marker command is sent on PIR, and the Command to send
806 //! 1.10.24 The Semantic Marker command is sent on PIR, and the Command to send
808
809 SerialDebug.printf("SM_ON_PIR command = %s\n", smCommand);
810
811 //This (in mainModule) will look if connected so sends over BLE, otherwise MQTT (if connected)
812 //!send an async call with a string parameter. This will set store the value and then async call the command (passing the parameter)
813 //!These are the ASYNC_CALL_PARAMETERS_MAX
815 }
816 break;
817 }
818
819 }
820 }
821 }
822
823#endif
824
825 //checkBothPressed_ButtonProcessing();
826 checkButtonA_M5ButtonProcessing();
827 checkButtonB_M5ButtonProcessing();
828}
829
830//!This is only thing exposed to others.. (Kinda which only 1 button module)
831//!short press on buttonA (top button)
832void buttonA_ShortPress_M5ButtonModule()
833{
834
835}
836//!long press on buttonA (top button)
837void buttonA_LongPress_M5ButtonModule()
838{
839
840}
841//!the long press of the side button
842void buttonB_LongPress_M5ButtonModule()
843{
844
845}
846//!the short press of the side button
847void buttonB_ShortPress_M5ButtonModule()
848{
849
850}
851
852#endif //USE_BUTTON_MODULE
#define KEY_PIN
port b
#define DATA_PIN
Definition: LED_DisPlay.cpp:5
KeyUnitSensorClass * _KeyUnitSensorClass
Definition: MainModule.cpp:65
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
Definition: MainModule.cpp:531
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
Definition: MainModule.cpp:421
#define SINGLE_CLICK_BM
Definition: MainModule.h:93
#define THRESHOLD_KIND_DARK
Definition: MainModule.h:74
#define ASYNC_SEND_MQTT_FEED_MESSAGE
sends a message (like FEED) on the users topic
Definition: MainModule.h:203
#define ASYNC_JSON_MQTT_MESSAGE_PARAMETER
these are the async with a string parameter
Definition: MainModule.h:237
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
unsigned long millis()
Definition: TinyGPS.cpp:35
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.