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 _leds = new CRGB[1] ; // Define the array of leds. 定义LED阵列.
28
29}
30
32{
33 printf("KeyUnitSensorClass destroy\n");
34}
35
36
37#define USE_LED
38#ifdef USE_LED
39#ifdef USE_FAST_LED
40#include <FastLED.h>
41#endif
42
43//! 8.20.24
44//! The PIN map stuff is tricky, and I don't know how to do this except via configuration messages
45//! (or Compile defines).
46//! When using the M5Atom, the PIN Port (next to the power is: G19, G21,G22,G23,G25, G33)
47//! But if using the the PORT B or the IC-2, the pins are DATA_PIN 23, KEY_PIN 33
48//! But the port out the back in DATA_PIN 26, KEY_PIN 32
49//! HOW TO KNOW THIS ??? With the QRCode scanner, it's the port out the back
50//! If the
51//! @see https://shop.m5stack.com/products/mechanical-key-button-unit
52//! 8.21.24 .. quick test, will use factoryClockwise to change these for a test
53//! if factoryClockwise then it's plugged into the M5 (DATA_PIN 26, KEY_PIN 32)
54//! if not factoryClockwise then it's the extension .. (DATA_PIN 23, KEY_PIN 33)
55
56#ifdef ATOM_QRCODE_MODULE
57//! port b
58#define DATA_PIN 26
60{
61 int pin;
62 //! 7.12.25
63 pin = DATA_PIN;
64 // SerialDebug.printf("getDATA_PIN = %d\n", pin);
65 return pin;
66}
67#else
68#define DATA_PIN 32 // Define LED pin. 定义LED引脚.
69int getDATA_PIN() { return DATA_PIN; };
70
71#endif
72#endif
73
74//#define USE_LED_BREATH //not working, (working for M5button and Core2)
75//! for ATOM (maybe change the priority and see what happens)
76
77
78//!wrapper static method
79void KeyUnitSensorClass::startTaskImpl(void* _this)
80{
81 SerialDebug.printf(" startTaskImpl == %p\n", _this);
82
83#ifdef USE_LED_BREATH
84 ((KeyUnitSensorClass*)_this)->keyUnitLED();
85#endif
86}
87#define KEY_UNIT_GROVE
88#ifdef KEY_UNIT_GROVE
89#ifdef ATOM_QRCODE_MODULE
90//! port b
91#define KEY_PIN 32
93{
94 int pin;
95 //! 7.12.25
96 pin = KEY_PIN;
97 // SerialDebug.printf("getKEY_PIN = %d\n", pin);
98 return pin;
99
100
101}
102#else
103#define KEY_PIN 33 //Define Key Pin. 定义Key引脚
104int getKEY_PIN() { return KEY_PIN; };
105#endif //ATOM_QRCODE_MODULE
106 //!setup the KeyUnit
107void KeyUnitSensorClass::setupKeyUnit()
108{
109 SerialDebug.printf("KeyUnitSensorClass.setupKeyUnit == %p\n", this);
110
111 pinMode(getKEY_PIN(), INPUT_PULLUP); // Init Key pin. 初始化Key引脚.
112
113 //! 5.3.25 register our PIN use
114 registerPinUse_mainModule(getKEY_PIN(), "KEY_PIN", "KeyUnitSensorClass", false);
115 registerPinUse_mainModule(getDATA_PIN(), "DATA_PIN", "KeyUnitSensorClass", false);
116
117#ifdef USE_LED
118 getDATA_PIN();
119 getKEY_PIN();
121 {
122 FastLED.addLeds<SK6812, 26, GRB>(this->_leds, 1); // Init FastLED. 初始化FastLED.
123 //! 5.3.25 register our PIN use
124 registerPinUse_mainModule(26, "FastLED", "KeyUnitSensorClass", false);
125
126 }
127 else
128 {
129 FastLED.addLeds<SK6812, 23, GRB>(this->_leds, 1); // Init FastLED. 初始化FastLED.
130 //! 5.3.25 register our PIN use
131 registerPinUse_mainModule(23, "FastLED", "KeyUnitSensorClass", false);
132 }
133#endif
134#ifdef USE_LED_BREATH //not working,
135 //!@see https://stackoverflow.com/questions/45831114/c-freertos-task-invalid-use-of-non-static-member-function
136 //!@see https://www.freertos.org/FreeRTOS_Support_Forum_Archive/July_2010/freertos_Is_it_possible_create_freertos_task_in_c_3778071.html
137 //!@see https://stackoverflow.com/questions/77931188/c-freertos-task-this-instance-being-passed-as-null?noredirect=1#comment137393059_77931188
138 //!4th parameter cannot be NULL but instead is 'this'
139 //!NOTE: 2024 stack size needed .. otherwise stack overflow 2.5.24 (my birthday)
140 xTaskCreate(
141 &this->startTaskImpl, "led", 2024, this, 0,
142 NULL); // Create a thread for breathing LED. 创建一个线程用于LED呼吸灯.
143#endif
144}
145
146
147//! loop the key unit (after other called M5.updfate)
148void KeyUnitSensorClass::loopKeyUnit()
149{
150 if (!digitalRead(getKEY_PIN()))
151 {
152 // If Key was pressed. 如果按键按下.
153 SerialDebug.println("KeyUnitSensorClass.Key Pressed");
154#ifdef USE_LED
155 changeLedColor(); // Change LED color. 更换LED呼吸灯颜色.
156#endif
157 //! THERE should be a time limit on the button .. say 10 or so..
158 while (!digitalRead(getKEY_PIN()))
159 // Hold until the key released. 在松开按键前保持状态.
160 ;
161 SerialDebug.println("KU.Key Released");
162
163 //!call the callback
164 callCallback((char*)"keyPressed",true);
165
166 }
167}
168
169
170
171#ifdef USE_LED_BREATH
172void KeyUnitSensorClass::keyUnitLED() {
173
174 SerialDebug.printf(" setupKeyUnit %p\n", this);
175 //! default to RED
176 _leds[0] = CRGB::Red;
177 for (;;) {
178 for (int i = 0; i < 255; i++)
179 {
180 //! Set LED brightness from 0 to 255. 设置LED亮度从0到255.
181 FastLED.setBrightness(i);
182 FastLED.show();
183 delay(5);
184 }
185 for (int i = 255; i > 0; i--)
186 {
187 //! Set LED brightness from 255 to 0. 设置LED亮度从255到0.
188 FastLED.setBrightness(i);
189 FastLED.show();
190 delay(5);
191 }
192 }
193 vTaskDelete(NULL);
194}
195#endif
196
198 SerialDebug.println("changeLedColor");
199 _ledColor++;
200 if (_ledColor > 2) _ledColor = 0;
201 switch (_ledColor)
202 { // Change LED colors between R,G,B. 在红绿蓝中切换LED颜色.
203 case 0:
204 _leds[0] = CRGB::Red;
205 break;
206 case 1:
207 _leds[0] = CRGB::Green;
208 break;
209 case 2:
210 _leds[0] = CRGB::Blue;
211 break;
212 default:
213 break;
214 }
215}
216#endif //key unit grove
217
218
219
220//! error: https://stackoverflow.com/questions/3065154/undefined-reference-to-vtable
221//!@see https://www.w3schools.com/cpp/cpp_class_methods.asp
223{
224 //!setup the KeyUnit
225 loopKeyUnit();
226}
227
229{
230 printf("KeyUnitSensorClass::setup()\n");
231
232 setupKeyUnit();
233}
234
235#ifdef NOT_DEFINED
236
237//! adding the messages as well
238//! 5.14.25 (Laura/Paul flying). 5.14.74 great Dead
239//! 8.28.23 Adding a way for others to get informed on messages that arrive
240//! for the set,val
241//! 12.27.23 support setName == "socket"
242//! 1.10.24 if deviceNameSpecified then this matches this device, otherwise for all.
243//! It's up to the receiver to decide if it has to be specified
244void KeyUnitSensorClass::messageSetVal_SensorClassType(char *setName, char* valValue, boolean deviceNameSpecified)
245{
246
247}
248
249//! 12.28.23, 8.28.23 Adding a way for others to get informed on messages that arrive
250//! for the send -
252{
253
254}
255
256//! 12.28.23, 8.28.23 Adding a way for others to get informed on messages that arrive
257//! for the cmd
259{
260
261}
262
263
264//! 5.15.25 try a special command local to this class
266{
267
268}
269#endif
270#endif //ESP_M5
int getDATA_PIN()
int getKEY_PIN()
#define KEY_PIN
port b
#define DATA_PIN
port b
void registerPinUse_mainModule(long pin, String pinName, String moduleName, boolean isI2C)
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...
#define PREFERENCE_STEPPER_FACTORY_CLOCKWISE_MOTOR_DIRECTION_SETTING
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