ESP_IOT v2.5
IOT ESP Coding
KeyUnitSensorClass.cpp
Go to the documentation of this file.
1
3
4#include "../../defines.h"
5#if defined(ESP_M5) && !defined(ESP_M5_MINIMAL_SENSORS)
6
7//#include <iostream>
8#include <string.h>
9#include <stdio.h>
10
11#ifdef ESP_M5
12#ifdef M5CORE2_MODULE
13//#include <M5Display.h>
14#else
15#ifdef M5STICKCPLUS2
16#include <M5StickCPlus.h>
17#endif
18
19#endif
20#endif
21
23{
24
25 printf("KeyUnitSensorClass init %s\n", config);
26 _ledColor = 0;
27#ifdef USE_FAST_LED
28 _leds = new CRGB[1] ; // Define the array of leds. 定义LED阵列.
29#endif
30}
31
33{
34 printf("KeyUnitSensorClass destroy\n");
35}
36
37
38#define USE_LED
39#ifdef USE_LED
40#ifdef USE_FAST_LED
41//#include <FastLED.h>
42#endif
43
44//! 8.20.24
45//! The PIN map stuff is tricky, and I don't know how to do this except via configuration messages
46//! (or Compile defines).
47//! When using the M5Atom, the PIN Port (next to the power is: G19, G21,G22,G23,G25, G33)
48//! But if using the the PORT B or the IC-2, the pins are DATA_PIN 23, KEY_PIN 33
49//! But the port out the back in DATA_PIN 26, KEY_PIN 32
50//! HOW TO KNOW THIS ??? With the QRCode scanner, it's the port out the back
51//! If the
52//! @see https://shop.m5stack.com/products/mechanical-key-button-unit
53//! 8.21.24 .. quick test, will use factoryClockwise to change these for a test
54//! if factoryClockwise then it's plugged into the M5 (DATA_PIN 26, KEY_PIN 32)
55//! if not factoryClockwise then it's the extension .. (DATA_PIN 23, KEY_PIN 33)
56
57#ifdef ATOM_QRCODE_MODULE
58#ifdef ESP_M5_ATOM_S3
59//! port b
60#define DATA_PIN 35
61//!@see https://github.com/FastLED/FastLED/blob/356a3794d9d2b027685a374715e32d0ee2ad3e9b/examples/SpecialDrivers/ESP/DriverTest/PlatformConfig.h#L37
62//!@see https://github.com/esphome/issues/issues/4501
63//!the RGB pin 35
64#else
65//! port b
66#define DATA_PIN 26
67#endif
69{
70 int pin;
71 //! 7.12.25
72 pin = DATA_PIN;
73 // SerialDebug.printf("getDATA_PIN = %d\n", pin);
74 return pin;
75}
76#else
77#define DATA_PIN 32 // Define LED pin. 定义LED引脚.
78int getDATA_PIN() { return DATA_PIN; };
79
80#endif
81#endif
82
83//#define USE_LED_BREATH //not working, (working for M5button and Core2)
84//! for ATOM (maybe change the priority and see what happens)
85
86
87//!wrapper static method
88void KeyUnitSensorClass::startTaskImpl(void* _this)
89{
90 SerialDebug.printf(" startTaskImpl == %p\n", _this);
91
92#ifdef USE_LED_BREATH
93 ((KeyUnitSensorClass*)_this)->keyUnitLED();
94#endif
95}
96#define KEY_UNIT_GROVE
97#ifdef KEY_UNIT_GROVE
98#ifdef ATOM_QRCODE_MODULE
99//! port b
100#ifdef ESP_M5_ATOM_S3
101//! 7.12.26 lots of room
102//! G32 -> G1
103#define KEY_PIN 1
104#else
105#define KEY_PIN 32
106#endif
108{
109 int pin;
110 //! 7.12.25
111 pin = KEY_PIN;
112 // SerialDebug.printf("getKEY_PIN = %d\n", pin);
113 return pin;
114
115
116}
117#else
118#define KEY_PIN 33 //Define Key Pin. 定义Key引脚
119int getKEY_PIN() { return KEY_PIN; };
120#endif //ATOM_QRCODE_MODULE
121 //!setup the KeyUnit
122void KeyUnitSensorClass::setupKeyUnit()
123{
124 SerialDebug.printf("KeyUnitSensorClass.setupKeyUnit == %p\n", this);
125 SerialDebug.printf("KeyUnitSensorClass. **** NOT pinMODE ... *******\n");
126
127 //DEBUG 7.10.26 FIX???
128 pinMode(getKEY_PIN(), INPUT_PULLUP); // Init Key pin. 初始化Key引脚.
129
130 //! 5.3.25 register our PIN use
131 registerPinUse_mainModule(getKEY_PIN(), "KEY_PIN", "KeyUnitSensorClass", false);
132 //26 -- fastLED .. registerPinUse_mainModule(getDATA_PIN(), "DATA_PIN", "KeyUnitSensorClass", false);
133
134#ifdef USE_FAST_LED
135
136#ifdef USE_LED
137 getDATA_PIN();
138 getKEY_PIN();
140 {
141 FastLED.addLeds<SK6812, 26, GRB>(this->_leds, 1); // Init FastLED. 初始化FastLED.
142 //! 5.3.25 register our PIN use
143 registerPinUse_mainModule(26, "FastLED", "KeyUnitSensorClass", false);
144
145 }
146 else
147 {
148 FastLED.addLeds<SK6812, 23, GRB>(this->_leds, 1); // Init FastLED. 初始化FastLED.
149 //! 5.3.25 register our PIN use
150 registerPinUse_mainModule(23, "FastLED", "KeyUnitSensorClass", false);
151 }
152#endif
153#endif
154
155#ifdef USE_LED_BREATH_x //not working,
156 //!@see https://stackoverflow.com/questions/45831114/c-freertos-task-invalid-use-of-non-static-member-function
157 //!@see https://www.freertos.org/FreeRTOS_Support_Forum_Archive/July_2010/freertos_Is_it_possible_create_freertos_task_in_c_3778071.html
158 //!@see https://stackoverflow.com/questions/77931188/c-freertos-task-this-instance-being-passed-as-null?noredirect=1#comment137393059_77931188
159 //!4th parameter cannot be NULL but instead is 'this'
160 //!NOTE: 2024 stack size needed .. otherwise stack overflow 2.5.24 (my birthday)
161 xTaskCreate(
162 &this->startTaskImpl, "led", 2024, this, 0,
163 NULL); // Create a thread for breathing LED. 创建一个线程用于LED呼吸灯.
164#endif
165
166}
167
168
169//! loop the key unit (after other called M5.updfate)
170void KeyUnitSensorClass::loopKeyUnit()
171{
172#pragma mark DEBUG_bad_maybeGoodNOW
173 //if (false)
174 if (!digitalRead(getKEY_PIN()))
175 {
176 // If Key was pressed. 如果按键按下.
177 SerialDebug.println("KeyUnitSensorClass.Key Pressed");
178#ifdef USE_LED
179 changeLedColor(); // Change LED color. 更换LED呼吸灯颜色.
180#endif
181 long startTime = micros();
182
183 //! THERE should be a time limit on the button .. say 10 or so..
184 while (!digitalRead(getKEY_PIN()))
185 // Hold until the key released. 在松开按键前保持状态.
186 ;
187 long endTime = micros();
188 double diffTime = endTime - startTime;
189 //microseconds
190 double seconds = diffTime / 1000000;
191 SerialDebug.printf("KU.Key Released: %f\n", seconds);
192
193 if (seconds > 2.0)
194 {
195 SerialDebug.printf("KU.Long Press: %f\n", seconds);
196
197#define USE_OPTION_1
198#ifdef USE_OPTION_1
199 //! 11.26.25
200 //! as per issue #412 option 1 is to store locally,
202 //! 8.16.25 BLE CLIENT //!optimized to not send the wrapper around the message
203 // need to send B or b .. depending on result
206 else
208#else
209 //! call their (on the other side of this BLE) toggle the buzzer...
211#endif
212 }
213 else
214 {
215 //!call the callback
216 callCallback((char*)"keyPressed",true);
217 }
218
219 }
220}
221
222
223
224#ifdef USE_LED_BREATH
225void KeyUnitSensorClass::keyUnitLED() {
226
227 SerialDebug.printf(" setupKeyUnit %p\n", this);
228 //! default to RED
229#ifdef USE_FAST_LED
230
231 _leds[0] = CRGB::Red;
232 for (;;) {
233 for (int i = 0; i < 255; i++)
234 {
235 //! Set LED brightness from 0 to 255. 设置LED亮度从0到255.
236 FastLED.setBrightness(i);
237 FastLED.show();
238 delay(5);
239 }
240 for (int i = 255; i > 0; i--)
241 {
242 //! Set LED brightness from 255 to 0. 设置LED亮度从255到0.
243 FastLED.setBrightness(i);
244 FastLED.show();
245 delay(5);
246 }
247 }
248 vTaskDelete(NULL);
249#endif
250}
251#endif
252
254#ifdef USE_FAST_LED
255 SerialDebug.println("changeLedColor");
256 _ledColor++;
257 if (_ledColor > 2) _ledColor = 0;
258 switch (_ledColor)
259 { // Change LED colors between R,G,B. 在红绿蓝中切换LED颜色.
260 case 0:
261 _leds[0] = CRGB::Red;
262 break;
263 case 1:
264 _leds[0] = CRGB::Green;
265 break;
266 case 2:
267 _leds[0] = CRGB::Blue;
268 break;
269 default:
270 break;
271 }
272#endif
273}
274#endif //key unit grove
275
276
277
278//! error: https://stackoverflow.com/questions/3065154/undefined-reference-to-vtable
279//!@see https://www.w3schools.com/cpp/cpp_class_methods.asp
281{
282 //!setup the KeyUnit
283 loopKeyUnit();
284}
285
287{
288 printf("KeyUnitSensorClass::setup()\n");
289
290 setupKeyUnit();
291}
292
293#ifdef NOT_DEFINED
294
295//! adding the messages as well
296//! 5.14.25 (Laura/Paul flying). 5.14.74 great Dead
297//! 8.28.23 Adding a way for others to get informed on messages that arrive
298//! for the set,val
299//! 12.27.23 support setName == "socket"
300//! 1.10.24 if deviceNameSpecified then this matches this device, otherwise for all.
301//! It's up to the receiver to decide if it has to be specified
302void KeyUnitSensorClass::messageSetVal_SensorClassType(char *setName, char* valValue, boolean deviceNameSpecified)
303{
304
305}
306
307//! 12.28.23, 8.28.23 Adding a way for others to get informed on messages that arrive
308//! for the send -
310{
311
312}
313
314//! 12.28.23, 8.28.23 Adding a way for others to get informed on messages that arrive
315//! for the cmd
317{
318
319}
320
321
322//! 5.15.25 try a special command local to this class
324{
325
326}
327#endif
328#endif //ESP_M5
void sendCommandBLEClient_13orLess(String cmdString)
send a string of 13 characters or less
int getDATA_PIN()
int getKEY_PIN()
#define KEY_PIN
port b
#define DATA_PIN
port b
boolean registerPinUse_mainModule(long pin, String pinName, String moduleName, boolean isI2C)
void processClientCommandChar_mainModule(char cmd)
single character version of processClientCommand (since many used that already)
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...
void togglePreferenceBoolean_mainModule(int preferenceID)
toggles a preference boolean
#define PREFERENCE_STEPPER_FACTORY_CLOCKWISE_MOTOR_DIRECTION_SETTING
#define PREFERENCE_STEPPER_BUZZER_VALUE
stepper preferences
KeyUnitSensorClass(char *config)
constructor
void setup()
Pure Virtual Function.
~KeyUnitSensorClass()
destructor
void loop()
Pure Virtual Function.
virtual void messageCmd_SensorClassType(char *cmdValue)
void callCallback(char *info, boolean flag)
call the callback
virtual void messageSend_SensorClassType(char *sendValue)
virtual void messageSetVal_SensorClassType(char *setName, char *valValue, boolean deviceNameSpecified)
virtual void messageLocal_SensorClassType(char *message)
5.15.25 try a special command local to this class