-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.cpp
66 lines (60 loc) · 1.64 KB
/
button.cpp
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
#include <Arduino.h>
#include "button.h"
int prevState = 0; // states: -1 = floating, 0 = released, 1 = pressed
int isButtonPressed = 0; // states: -1 = floating, 0 = released, 1 = pressed
void button_isr()
{
isButtonPressed = digitalRead(PIN_BUTTON);
Serial.println("button changed: " + String(isButtonPressed));
}
void buttonSetup()
{
pinMode(PIN_BUTTON, INPUT_PULLUP); // Button
// attachInterrupt(PIN_BUTTON, button_isr, CHANGE);
// Serial.println("buttonSetup()");
}
void buttonLoop()
{
static unsigned long buttonTick = 0;
// non interupt
isButtonPressed = !digitalRead(PIN_BUTTON);
if (prevState != isButtonPressed)
{
prevState = isButtonPressed;
if (isButtonPressed == 0) // on released
{
// pushPinHi(PIN_BUZZER, 30); // beep 30ms
if (millis() - buttonTick >= 5000)
{ // long press 5 secs
// scanDtcError();
// resetECU();
Serial.println("button pressed >= 5s");
}
else if (millis() - buttonTick >= 3000)
{ // long press 3 secs
if (obd_select == 1)
obd_select = 2;
else
obd_select = 1;
saveConfig();
Serial.println("button long pressed >= 3s");
}
else if (millis() - buttonTick >= 5)
{ // short press 5 ms
pag_select++;
if (pag_select > 4)
{
pag_select = 1;
}
// saveConfig();
Serial.println("button short pressed <= 5ms");
}
buttonTick = 0; // reset timer
isButtonPressed = -1; // floating state
}
else if (isButtonPressed == 1) // on pressed
{
buttonTick = millis(); // start timer
}
}
}