-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTinyTuningButton.ino
64 lines (50 loc) · 1 KB
/
TinyTuningButton.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
#include <stdint.h>
#include <EEPROM.h>
#define UART_RX_PIN 1
#include "./protocol.h"
static bool isTuned = false;
static bool isLocked = false;
static volatile uint8_t pressCount = 0;
static volatile uint32_t lastPress = 0;
void toggleTuning(bool save)
{
isTuned = !isTuned;
if(save)
EEPROM.write(0, isTuned ? (uint8_t)0xAA : (uint8_t)0x00);
if(isTuned)
SEND_REPEAT(setMaxSpeed(35));
else
SEND_REPEAT(setMaxSpeed(20));
SEND_REPEAT(setEcoMode(true));
delay(1000);
SEND_REPEAT(setEcoMode(false));
}
void buttonInterrupt()
{
uint32_t now = millis();
if(lastPress + 50 > now)
return;
lastPress = now;
pressCount++;
}
void setup()
{
pinMode(2, INPUT_PULLUP);
attachInterrupt(0, buttonInterrupt, CHANGE);
ScooterSerial.begin(115200);
ScooterSerial.stopListening();
delay(2000);
if(EEPROM.read(0) == 0xAA)
toggleTuning(false);
}
void loop()
{
if(lastPress + 500 < millis())
{
uint8_t oldCount = pressCount;
pressCount = 0;
if(oldCount >= 8)
toggleTuning(true);
}
delay(100);
}