-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathButton_Triggered.ino
184 lines (157 loc) · 5.76 KB
/
Button_Triggered.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* @Description: Key trigger test
* @Author: LILYGO_L
* @Date: 2024-08-07 17:27:50
* @LastEditTime: 2024-11-25 17:25:33
* @License: GPL 3.0
*/
#include "Adafruit_EPD.h"
#include "pin_config.h"
struct Button_Triggered_Operator
{
using gesture = enum {
NOT_ACTIVE, // 未激活
SINGLE_CLICK, // 单击
DOUBLE_CLICK, // 双击
LONG_PRESS, // 长按
};
const uint32_t button_number = nRF52840_BOOT;
size_t cycletime_1 = 0;
size_t cycletime_2 = 0;
uint8_t current_state = gesture::NOT_ACTIVE;
bool trigger_start_flag = false;
bool trigger_flag = false;
bool timing_flag = false;
int8_t paragraph_triggered_level = -1;
uint8_t high_triggered_count = 0;
uint8_t low_triggered_count = 0;
uint8_t paragraph_triggered_count = 0;
};
SPIClass Custom_SPI(NRF_SPIM3, SCREEN_MISO, SCREEN_SCLK, SCREEN_MOSI);
Adafruit_SSD1681 display(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DC, SCREEN_RST,
SCREEN_CS, SCREEN_SRAM_CS, SCREEN_BUSY, &Custom_SPI, 32000000);
Button_Triggered_Operator Button_Triggered_OP;
bool Key_Scanning(void)
{
if (Button_Triggered_OP.trigger_start_flag == false)
{
if (digitalRead(Button_Triggered_OP.button_number) == LOW) // 低电平触发
{
Button_Triggered_OP.trigger_start_flag = true;
Serial.println("Press button to trigger start");
Button_Triggered_OP.high_triggered_count = 0;
Button_Triggered_OP.low_triggered_count = 0;
Button_Triggered_OP.paragraph_triggered_count = 0;
Button_Triggered_OP.paragraph_triggered_level = -1;
Button_Triggered_OP.trigger_flag = true;
Button_Triggered_OP.timing_flag = true;
}
}
if (Button_Triggered_OP.timing_flag == true)
{
Button_Triggered_OP.cycletime_2 = millis() + 1000; // 计时1000ms关闭
Button_Triggered_OP.timing_flag = false;
}
if (Button_Triggered_OP.trigger_flag == true)
{
if (millis() > Button_Triggered_OP.cycletime_1)
{
if (digitalRead(Button_Triggered_OP.button_number) == HIGH)
{
Button_Triggered_OP.high_triggered_count++;
if (Button_Triggered_OP.paragraph_triggered_level != HIGH)
{
Button_Triggered_OP.paragraph_triggered_count++;
Button_Triggered_OP.paragraph_triggered_level = HIGH;
}
}
else if (digitalRead(Button_Triggered_OP.button_number) == LOW)
{
Button_Triggered_OP.low_triggered_count++;
if (Button_Triggered_OP.paragraph_triggered_level != LOW)
{
Button_Triggered_OP.paragraph_triggered_count++;
Button_Triggered_OP.paragraph_triggered_level = LOW;
}
}
Button_Triggered_OP.cycletime_1 = millis() + 50;
}
if (Button_Triggered_OP.timing_flag == false)
{
if (millis() > Button_Triggered_OP.cycletime_2)
{
Serial.print("end\n");
Serial.printf("high_triggered_count: %d\n", Button_Triggered_OP.high_triggered_count);
Serial.printf("low_triggered_count: %d\n", Button_Triggered_OP.low_triggered_count);
Serial.printf("paragraph_triggered_count: %d\n", Button_Triggered_OP.paragraph_triggered_count);
Button_Triggered_OP.trigger_flag = false;
Button_Triggered_OP.trigger_start_flag = false;
if ((Button_Triggered_OP.paragraph_triggered_count == 2)) // 单击
{
Button_Triggered_OP.current_state = Button_Triggered_OP.gesture::SINGLE_CLICK;
return true;
}
else if ((Button_Triggered_OP.paragraph_triggered_count == 4)) // 双击
{
Button_Triggered_OP.current_state = Button_Triggered_OP.gesture::DOUBLE_CLICK;
return true;
}
else if ((Button_Triggered_OP.paragraph_triggered_count == 1)) // 长按
{
Button_Triggered_OP.current_state = Button_Triggered_OP.gesture::LONG_PRESS;
return true;
}
}
}
}
return false;
}
void setup()
{
Serial.begin(115200);
// while (!Serial)
// {
// delay(100);
// }
Serial.println("Ciallo");
pinMode(nRF52840_BOOT, INPUT_PULLUP);
display.begin();
display.setRotation(1);
display.fillScreen(EPD_WHITE);
display.setCursor(10, 60);
display.setTextColor(EPD_BLACK);
display.setTextSize(2);
display.print("Please press the button");
display.display(display.update_mode::FULL_REFRESH, true);
}
void loop()
{
if (Key_Scanning() == true)
{
display.fillScreen(EPD_WHITE);
display.setCursor(10, 60);
display.setTextColor(EPD_BLACK);
display.setTextSize(2);
switch (Button_Triggered_OP.current_state)
{
case Button_Triggered_OP.gesture::SINGLE_CLICK:
Serial.println("Key triggered: SINGLE_CLICK");
display.print("1.SINGLE_CLICK");
// delay(1000);
break;
case Button_Triggered_OP.gesture::DOUBLE_CLICK:
Serial.println("Key triggered: DOUBLE_CLICK");
display.print("2.DOUBLE_CLICK");
// delay(1000);
break;
case Button_Triggered_OP.gesture::LONG_PRESS:
Serial.println("Key triggered: LONG_PRESS");
display.print("3.LONG_PRESS");
// delay(1000);
break;
default:
break;
}
display.display(display.update_mode::FAST_REFRESH, true);
}
}