-
Notifications
You must be signed in to change notification settings - Fork 0
/
CambiaCanales.ino
158 lines (131 loc) · 3.07 KB
/
CambiaCanales.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
#include <TinyOzOLED.h>
#include <TinyWireM.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <Bounce2.h>
#include <EEPROM.h>
#define BUTTON_PIN_1 3
#define BUTTON_PIN_2 A2
#define DL_ADDR 0x00
#define CHANNEL_UP 0x986730CF
#define CHANNEL_DOWN 0x98678877
IRsend irsend;
int selectedOption = -1;
int delayTime;
unsigned long interval = 1000;
unsigned long previousMillis = 0;
bool scanUp = false;
bool scanDown = false;
bool delayMenu = false;
long lastDebounceTime = 0;
long debounceDelay = 100;
Bounce btnEnter = Bounce(BUTTON_PIN_1, 5);
void setup() {
OSCCAL = 0x4E;
pinMode(BUTTON_PIN_1,INPUT);
pinMode(BUTTON_PIN_2,INPUT);
delayTime = EEPROM.read(DL_ADDR);
delay(10);
if (delayTime > 60) {
OzOled.printNumber(delayTime, 13, 7);
saveDelayTime(1);
}
OzOled.init();
OzOled.clearDisplay();
OzOled.setNormalDisplay();
OzOled.sendCommand(0xA1);
OzOled.sendCommand(0xC8);
OzOled.printString("CAMBIACANALES", 2, 1);
OzOled.printString("Scan arriba", 3, 3);
OzOled.printString("Scan abajo", 3, 4);
OzOled.printString("Detener scan", 3, 5);
OzOled.printString("Delay", 3, 7);
OzOled.printString("s", 15, 7);
showDelayTime();
changeOption(true);
}
void loop() {
unsigned long currentMillis = millis();
btnEnter.update();
int btnRead = analogRead(BUTTON_PIN_2);
if ((millis() - lastDebounceTime) > debounceDelay) {
if (btnRead < 600 && btnRead > 100) {
if (delayMenu)
changeDelayValue(true);
else
changeOption(true);
}
else if (btnRead > 1000) {
if (delayMenu)
changeDelayValue(false);
else
changeOption(false);
}
lastDebounceTime = millis();
}
if (btnEnter.fell()) {
optionSelect();
}
if (scanUp || scanDown) {
if ((unsigned long)(currentMillis - previousMillis) >= interval * delayTime) {
if (scanUp) irsend.sendNEC(CHANNEL_UP,32);
if (scanDown) irsend.sendNEC(CHANNEL_DOWN,32);
previousMillis = millis();
}
}
}
void changeOption(bool optUp) {
int options[4] = {3, 4, 5, 7};
OzOled.printString(" ", 1, options[selectedOption]);
if (optUp) {
selectedOption++;
}
else {
selectedOption--;
}
if (selectedOption == 4)
selectedOption = 0;
if (selectedOption == -1)
selectedOption = 3;
OzOled.printString("x", 1, options[selectedOption]);
}
void changeDelayValue(bool addSeconds) {
if (addSeconds) {
if (delayTime < 60) delayTime++;
}
else {
if (delayTime > 0) delayTime--;
}
showDelayTime();
}
void optionSelect() {
switch (selectedOption) {
case 0:
scanUp = true;
scanDown = false;
break;
case 1:
scanUp = false;
scanDown = true;
break;
case 2:
scanUp = false;
scanDown = false;
break;
case 3:
if (delayMenu) saveDelayTime(delayTime);
delayMenu = !delayMenu;
break;
}
}
void saveDelayTime(int seconds) {
EEPROM.write(DL_ADDR, seconds);
delay(10);
}
void showDelayTime() {
OzOled.setCursorXY(13, 7);
if (delayTime < 10) {
OzOled.printString(" ");
}
OzOled.printNumber(delayTime);
}