Skip to content

Commit 15ecc90

Browse files
committed
Update button behaviour. Still WIP
1 parent d7180ad commit 15ecc90

File tree

6 files changed

+31
-180
lines changed

6 files changed

+31
-180
lines changed

src/ButtonThread.cpp

+13-151
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ ButtonThread::ButtonThread() : OSThread("Button")
7373
userButton.setDebounceMs(1);
7474
userButton.attachDoubleClick(userButtonDoublePressed);
7575
userButton.attachMultiClick(userButtonMultiPressed, this); // Reference to instance: get click count from non-static OneButton
76-
#ifndef T_DECK // T-Deck immediately wakes up after shutdown, so disable this function
76+
#if !defined(T_DECK) && \
77+
!defined( \
78+
ELECROW_ThinkNode_M2) // T-Deck immediately wakes up after shutdown, Thinknode M2 has this on the smaller ALT button
7779
userButton.attachLongPressStart(userButtonPressedLongStart);
7880
userButton.attachLongPressStop(userButtonPressedLongStop);
7981
#endif
@@ -85,11 +87,10 @@ ButtonThread::ButtonThread() : OSThread("Button")
8587
// Some platforms (nrf52) have a SENSE variant which allows wake from sleep - override what OneButton did
8688
pinMode(BUTTON_PIN_ALT, INPUT_PULLUP_SENSE);
8789
#endif
88-
userButtonAlt.attachClick(userButtonPressed);
90+
userButtonAlt.attachClick(userButtonPressedScreen);
8991
userButtonAlt.setClickMs(BUTTON_CLICK_MS);
9092
userButtonAlt.setPressMs(BUTTON_LONGPRESS_MS);
9193
userButtonAlt.setDebounceMs(1);
92-
userButtonAlt.attachDoubleClick(userButtonDoublePressed);
9394
userButtonAlt.attachLongPressStart(userButtonPressedLongStart);
9495
userButtonAlt.attachLongPressStop(userButtonPressedLongStop);
9596
#endif
@@ -200,7 +201,12 @@ int32_t ButtonThread::runOnce()
200201
break;
201202
}
202203

203-
case BUTTON_EVENT_PRESSED1: {
204+
case BUTTON_EVENT_PRESSED_SCREEN: {
205+
// turn screen on or off
206+
screen_flag = !screen_flag;
207+
if (screen)
208+
screen->setOn(screen_flag);
209+
break;
204210
}
205211

206212
case BUTTON_EVENT_DOUBLE_PRESSED: {
@@ -217,10 +223,6 @@ int32_t ButtonThread::runOnce()
217223
break;
218224
}
219225

220-
case BUTTON_EVENT_DOUBLE_PRESSED1: {
221-
break;
222-
}
223-
224226
case BUTTON_EVENT_MULTI_PRESSED: {
225227
LOG_BUTTON("Mulitipress! %hux", multipressClickCount);
226228
switch (multipressClickCount) {
@@ -236,20 +238,10 @@ int32_t ButtonThread::runOnce()
236238
#elif defined(ELECROW_ThinkNode_M2)
237239
case 3:
238240
LOG_INFO("3 clicks: toggle buzzer");
239-
pinMode(M2_buzzer, OUTPUT);
240-
241-
if (!buzzer_flag) {
242-
buzzer_test();
243-
BEEP_STATE = true;
244-
245-
analogWriteFrequency(4000);
246-
} else {
247-
currentState = OFF;
248-
isBuzzing = false;
249-
cont = 0;
250-
BEEP_STATE = false;
251-
}
252241
buzzer_flag = !buzzer_flag;
242+
if (buzzer_flag) {
243+
playBeep();
244+
}
253245
break;
254246
#endif
255247

@@ -301,22 +293,6 @@ int32_t ButtonThread::runOnce()
301293
break;
302294
}
303295

304-
case BUTTON_EVENT_LONG_PRESSED1: {
305-
currentState = OFF;
306-
isBuzzing = false;
307-
cont = 0;
308-
BEEP_STATE = false;
309-
pinMode(18, INPUT);
310-
screen->setOn(false);
311-
delay(1000);
312-
pinMode(1, OUTPUT);
313-
digitalWrite(1, LOW);
314-
pinMode(6, OUTPUT);
315-
digitalWrite(6, LOW);
316-
317-
break;
318-
}
319-
320296
#ifdef BUTTON_PIN_TOUCH
321297
case BUTTON_EVENT_TOUCH_LONG_PRESSED: {
322298
LOG_BUTTON("Touch press!");
@@ -341,113 +317,6 @@ int32_t ButtonThread::runOnce()
341317
return 50;
342318
}
343319

344-
void ButtonThread::buzzer_test()
345-
{
346-
currentState = SHORT_BEEP; // 初始化状态
347-
stateStartTime = millis(); // 获取当前时间
348-
}
349-
350-
void ButtonThread::buzzer_updata()
351-
{
352-
unsigned long currentTime = millis(); // 获取当前时间
353-
switch (currentState) {
354-
case SHORT_BEEP:
355-
if (isBuzzing) {
356-
// 如果正在响,检查时间
357-
if (currentTime - stateStartTime >= beepDuration) {
358-
// 停止蜂鸣器
359-
analogWriteFrequency(4000);
360-
analogWrite(M2_buzzer, 0);
361-
stateStartTime = currentTime; // 记录停的时间
362-
isBuzzing = false; // 更新状态
363-
}
364-
} else {
365-
// 如果未响,检查时间
366-
if (currentTime - stateStartTime >= 200) {
367-
// 启动蜂鸣器
368-
if (cont == 3) {
369-
currentState = LONG_BEEP;
370-
cont = 0;
371-
isBuzzing = false;
372-
} else {
373-
analogWriteFrequency(4000);
374-
analogWrite(M2_buzzer, 127);
375-
isBuzzing = true; // 更新状态
376-
cont = cont + 1;
377-
}
378-
stateStartTime = currentTime; // 记录响的时间
379-
}
380-
}
381-
break;
382-
case LONG_BEEP:
383-
if (isBuzzing) {
384-
// 如果正在响,检查时间
385-
if (currentTime - stateStartTime >= longBeepDuration) {
386-
// 停止蜂鸣器
387-
analogWriteFrequency(4000);
388-
analogWrite(M2_buzzer, 0);
389-
stateStartTime = currentTime; // 记录停的时间
390-
isBuzzing = false; // 更新状态
391-
}
392-
} else {
393-
// 如果未响,检查时间
394-
if (currentTime - stateStartTime >= 200) {
395-
// 启动蜂鸣器
396-
if (cont == 3) {
397-
currentState = SHORT_BEEP1;
398-
cont = 0;
399-
isBuzzing = false;
400-
} else {
401-
analogWriteFrequency(4000);
402-
analogWrite(M2_buzzer, 127);
403-
isBuzzing = true; // 更新状态
404-
cont = cont + 1;
405-
}
406-
stateStartTime = currentTime; // 记录响的时间
407-
}
408-
}
409-
break;
410-
case SHORT_BEEP1:
411-
if (isBuzzing) {
412-
// 如果正在响,检查时间
413-
if (currentTime - stateStartTime >= beepDuration) {
414-
// 停止蜂鸣器
415-
analogWriteFrequency(4000);
416-
analogWrite(M2_buzzer, 0);
417-
stateStartTime = currentTime; // 记录停的时间
418-
isBuzzing = false; // 更新状态
419-
}
420-
} else {
421-
// 如果未响,检查时间
422-
if (currentTime - stateStartTime >= 200) {
423-
// 启动蜂鸣器
424-
if (cont == 3) {
425-
currentState = WAIT;
426-
cont = 0;
427-
isBuzzing = false;
428-
} else {
429-
analogWriteFrequency(4000);
430-
analogWrite(M2_buzzer, 127);
431-
isBuzzing = true; // 更新状态
432-
cont = cont + 1;
433-
}
434-
stateStartTime = currentTime; // 记录响的时间
435-
}
436-
}
437-
break;
438-
case WAIT:
439-
if (currentTime - stateStartTime >= pauseDuration) {
440-
buzzer_test();
441-
}
442-
break;
443-
case OFF:
444-
break;
445-
default:
446-
// 其他状态处理
447-
break;
448-
}
449-
}
450-
451320
/*
452321
* Attach (or re-attach) hardware interrupts for buttons
453322
* Public method. Used outside class when waking from MCU sleep
@@ -573,13 +442,6 @@ void ButtonThread::userButtonPressedLongStart()
573442
}
574443
}
575444

576-
void ButtonThread::userButtonPressedLongStart1()
577-
{
578-
if (millis() > c_holdOffTime) {
579-
btnEvent = BUTTON_EVENT_LONG_PRESSED1;
580-
}
581-
}
582-
583445
void ButtonThread::userButtonPressedLongStop()
584446
{
585447
if (millis() > c_holdOffTime) {

src/ButtonThread.h

+5-22
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#define BUTTON_TOUCH_MS 400
1717
#endif
1818

19-
extern int screen_flag;
20-
2119
class ButtonThread : public concurrency::OSThread
2220
{
2321
public:
@@ -26,12 +24,10 @@ class ButtonThread : public concurrency::OSThread
2624
enum ButtonEventType {
2725
BUTTON_EVENT_NONE,
2826
BUTTON_EVENT_PRESSED,
29-
BUTTON_EVENT_PRESSED1,
27+
BUTTON_EVENT_PRESSED_SCREEN,
3028
BUTTON_EVENT_DOUBLE_PRESSED,
31-
BUTTON_EVENT_DOUBLE_PRESSED1,
3229
BUTTON_EVENT_MULTI_PRESSED,
3330
BUTTON_EVENT_LONG_PRESSED,
34-
BUTTON_EVENT_LONG_PRESSED1,
3531
BUTTON_EVENT_LONG_RELEASED,
3632
BUTTON_EVENT_TOUCH_LONG_PRESSED,
3733
};
@@ -42,19 +38,6 @@ class ButtonThread : public concurrency::OSThread
4238
void detachButtonInterrupts();
4339
void storeClickCount();
4440

45-
bool BEEP_STATE = false;
46-
void buzzer_updata();
47-
void buzzer_test();
48-
enum BuzzerState { OFF, SHORT_BEEP, LONG_BEEP, SHORT_BEEP1, WAIT };
49-
50-
bool buzzer_flag = false;
51-
BuzzerState currentState = OFF;
52-
const int beepDuration = 1000; // 发声持续时间
53-
const int pauseDuration = 1000; // 间隔时间
54-
const int longBeepDuration = 3000; // 长音持续时间
55-
unsigned long stateStartTime = 0;
56-
bool isBuzzing = false; // 蜂鸣器状态
57-
int cont = 0;
5841
// Disconnect and reconnect interrupts for light sleep
5942
#ifdef ARCH_ESP32
6043
int beforeLightSleep(void *unused);
@@ -81,6 +64,8 @@ class ButtonThread : public concurrency::OSThread
8164

8265
// set during IRQ
8366
static volatile ButtonEventType btnEvent;
67+
bool buzzer_flag = false;
68+
bool screen_flag = true;
8469

8570
// Store click count during callback, for later use
8671
volatile int multipressClickCount = 0;
@@ -89,17 +74,15 @@ class ButtonThread : public concurrency::OSThread
8974

9075
// IRQ callbacks
9176
static void userButtonPressed() { btnEvent = BUTTON_EVENT_PRESSED; }
92-
static void userButtonPressed1()
77+
static void userButtonPressedScreen()
9378
{
9479
if (millis() > c_holdOffTime) {
95-
btnEvent = BUTTON_EVENT_PRESSED1;
80+
btnEvent = BUTTON_EVENT_PRESSED_SCREEN;
9681
}
9782
}
9883
static void userButtonDoublePressed() { btnEvent = BUTTON_EVENT_DOUBLE_PRESSED; }
99-
static void userButtonDoublePressed1() { btnEvent = BUTTON_EVENT_DOUBLE_PRESSED1; }
10084
static void userButtonMultiPressed(void *callerThread); // Retrieve click count from non-static Onebutton while still valid
10185
static void userButtonPressedLongStart();
102-
static void userButtonPressedLongStart1();
10386
static void userButtonPressedLongStop();
10487
static void touchPressedLongStart() { btnEvent = BUTTON_EVENT_TOUCH_LONG_PRESSED; }
10588
};

src/buzz/buzz.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ struct ToneDuration {
3030
#define NOTE_B3 247
3131
#define NOTE_CS4 277
3232

33-
const int DURATION_1_8 = 125; // 1/8 note
34-
const int DURATION_1_4 = 250; // 1/4 note
33+
const int DURATION_1_8 = 125; // 1/8 note
34+
const int DURATION_1_4 = 250; // 1/4 note
35+
const int DURATION_1_2 = 500; // 1/2 note
36+
const int DURATION_3_4 = 750; // 1/4 note
37+
const int DURATION_1_1 = 1000; // 1/1 note
3538

3639
void playTones(const ToneDuration *tone_durations, int size)
3740
{
@@ -55,6 +58,12 @@ void playBeep()
5558
playTones(melody, sizeof(melody) / sizeof(ToneDuration));
5659
}
5760

61+
void playLongBeep()
62+
{
63+
ToneDuration melody[] = {{NOTE_B3, DURATION_1_1}};
64+
playTones(melody, sizeof(melody) / sizeof(ToneDuration));
65+
}
66+
5867
void playGPSEnableBeep()
5968
{
6069
ToneDuration melody[] = {{NOTE_C3, DURATION_1_8}, {NOTE_FS3, DURATION_1_4}, {NOTE_CS4, DURATION_1_4}};

src/buzz/buzz.h

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
void playBeep();
4+
void playLongBeep();
45
void playStartMelody();
56
void playShutdownMelody();
67
void playGPSEnableBeep();
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
[env:ThinkNode-M2]
22
extends = esp32s3_base
33
board = ESP32-S3-WROOM-1-N4
4-
board_level = extra
54
build_flags =
65
${esp32s3_base.build_flags}
76
-D ELECROW_ThinkNode_M2
87
-I variants/ELECROW-ThinkNode-M2
9-
10-
lib_deps = ${esp32s3_base.lib_deps}
11-
; lewisxhe/PCF8563_Library@^1.0.1

variants/ELECROW-ThinkNode-M2/variant.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#define POWER_CHRG 6
1111
#define POWER_FULL 42
1212

13-
#define M2_buzzer 5
13+
#define PIN_BUZZER 5
1414

1515
#define I2C_SCL 15
1616
#define I2C_SDA 16

0 commit comments

Comments
 (0)