forked from WiseLord/ampcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
417 lines (348 loc) · 7.38 KB
/
input.c
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "input.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include "eeprom.h"
static volatile int8_t encCnt;
static volatile cmdID cmdBuf;
static int8_t encRes = 0;
static uint8_t silenceTime;
// Previous state
static volatile uint8_t encPrev = ENC_0;
static volatile uint8_t btnPrev = BTN_STATE_0;
static volatile uint16_t displayTime;
static volatile uint16_t sensTimer; // Timer of temperature measuring process
static volatile int16_t stbyTimer = STBY_TIMER_OFF; // Standby timer
static volatile int16_t initTimer = INIT_TIMER_OFF; // Init timer
static volatile uint16_t secTimer; // 1 second timer
static volatile uint8_t clockTimer;
static volatile int16_t silenceTimer; // Timer to check silence
static volatile uint16_t rcTimer;
static uint8_t rcType;
static uint8_t rcAddr;
static uint8_t rcCode[CMD_RC_END]; // Array with rc commands
void rcCodesInit(void)
{
rcType = eeprom_read_byte((uint8_t*)EEPROM_RC_TYPE);
rcAddr = eeprom_read_byte((uint8_t*)EEPROM_RC_ADDR);
eeprom_read_block(rcCode, (uint8_t*)EEPROM_RC_CMD, CMD_RC_END);
return;
}
void inputInit(void)
{
// Setup buttons and encoder as inputs with pull-up resistors
DDR(BUTTON_1) &= ~BUTTON_1_LINE;
DDR(BUTTON_2) &= ~BUTTON_2_LINE;
DDR(BUTTON_3) &= ~BUTTON_3_LINE;
DDR(BUTTON_4) &= ~BUTTON_4_LINE;
DDR(BUTTON_5) &= ~BUTTON_5_LINE;
DDR(ENCODER_A) &= ~ENCODER_A_LINE;
DDR(ENCODER_B) &= ~ENCODER_B_LINE;
PORT(BUTTON_1) |= BUTTON_1_LINE;
PORT(BUTTON_2) |= BUTTON_2_LINE;
PORT(BUTTON_3) |= BUTTON_3_LINE;
PORT(BUTTON_4) |= BUTTON_4_LINE;
PORT(BUTTON_5) |= BUTTON_5_LINE;
PORT(ENCODER_A) |= ENCODER_A_LINE;
PORT(ENCODER_B) |= ENCODER_B_LINE;
// Set timer prescaller to 128 (125 kHz) and reset on match
TCCR2 = ((1<<CS22) | (0<<CS21) | (1<<CS20) | (1<<WGM21));
OCR2 = 125; // 125000/125 => 1000 polls/sec
TCNT2 = 0; // Reset timer value
TIMSK |= (1<<OCIE2); // Enable timer compare match interrupt
rcCodesInit();
encRes = eeprom_read_byte((uint8_t*)EEPROM_ENC_RES);
silenceTime = eeprom_read_byte((uint8_t*)EEPROM_SILENCE_TIMER);
encCnt = 0;
cmdBuf = CMD_RC_END;
sensTimer = 0;
return;
}
static uint8_t rcCmdIndex(uint8_t cmd)
{
uint8_t i;
for (i = 0; i < CMD_RC_END; i++)
if (cmd == rcCode[i])
return i;
return CMD_RC_END;
}
ISR (TIMER2_COMP_vect)
{
static int16_t btnCnt = 0; // Buttons press duration value
// Current state
uint8_t encNow = ENC_0;
uint8_t btnNow = BTN_STATE_0;
if (encRes) {
if (~PIN(ENCODER_A) & ENCODER_A_LINE)
encNow |= ENC_A;
if (~PIN(ENCODER_B) & ENCODER_B_LINE)
encNow |= ENC_B;
}
if (~PIN(BUTTON_1) & BUTTON_1_LINE)
btnNow |= BTN_1;
if (~PIN(BUTTON_2) & BUTTON_2_LINE)
btnNow |= BTN_2;
if (~PIN(BUTTON_3) & BUTTON_3_LINE)
btnNow |= BTN_3;
if (~PIN(BUTTON_4) & BUTTON_4_LINE)
btnNow |= BTN_4;
if (~PIN(BUTTON_5) & BUTTON_5_LINE)
btnNow |= BTN_5;
// If encoder event has happened, inc/dec encoder counter
if (encRes) {
if ((encPrev == ENC_0 && encNow == ENC_A) ||
(encPrev == ENC_A && encNow == ENC_AB) ||
(encPrev == ENC_AB && encNow == ENC_B) ||
(encPrev == ENC_B && encNow == ENC_0))
encCnt++;
if ((encPrev == ENC_0 && encNow == ENC_B) ||
(encPrev == ENC_B && encNow == ENC_AB) ||
(encPrev == ENC_AB && encNow == ENC_A) ||
(encPrev == ENC_A && encNow == ENC_0))
encCnt--;
encPrev = encNow;
} else {
if (~PIN(ENCODER_A) & ENCODER_A_LINE)
btnNow |= BTN_A;
if (~PIN(ENCODER_B) & ENCODER_B_LINE)
btnNow |= BTN_B;
encPrev = btnNow & (BTN_A | BTN_B);
}
// If button event has happened, place it to command buffer
if (btnNow) {
if (btnNow == btnPrev) {
btnCnt++;
if (btnCnt == LONG_PRESS) {
switch (btnPrev) {
case BTN_1:
cmdBuf = CMD_BTN_1_LONG;
break;
case BTN_2:
cmdBuf = CMD_BTN_2_LONG;
break;
case BTN_3:
cmdBuf = CMD_BTN_3_LONG;
break;
case BTN_4:
cmdBuf = CMD_BTN_4_LONG;
break;
case BTN_5:
cmdBuf = CMD_BTN_5_LONG;
break;
case BTN_12:
cmdBuf = CMD_BTN_12_LONG;
break;
case BTN_13:
cmdBuf = CMD_BTN_13_LONG;
break;
}
} else if (!encRes) {
if (btnCnt == LONG_PRESS + AUTOREPEAT) {
switch (btnPrev) {
case BTN_A:
encCnt++;
break;
case BTN_B:
encCnt--;
break;
}
btnCnt = LONG_PRESS + 1;
}
}
} else {
btnPrev = btnNow;
}
} else {
if ((btnCnt > SHORT_PRESS) && (btnCnt < LONG_PRESS)) {
switch (btnPrev) {
case BTN_1:
cmdBuf = CMD_BTN_1;
break;
case BTN_2:
cmdBuf = CMD_BTN_2;
break;
case BTN_3:
cmdBuf = CMD_BTN_3;
break;
case BTN_4:
cmdBuf = CMD_BTN_4;
break;
case BTN_5:
cmdBuf = CMD_BTN_5;
break;
}
if (!encRes) {
switch (btnPrev) {
case BTN_A:
encCnt++;
break;
case BTN_B:
encCnt--;
break;
}
}
}
btnCnt = 0;
}
btnPrev = btnNow;
// Timer of current display mode
if (displayTime)
displayTime--;
// Time from last IR command
if (rcTimer < 1000)
rcTimer++;
if (secTimer) {
secTimer--;
} else {
secTimer = 1000;
// Timer of standby mode
if (stbyTimer > 0)
stbyTimer--;
// Silence timer
if (silenceTimer > 0)
silenceTimer--;
// Timer of temperature measurement
if (sensTimer)
sensTimer--;
}
// Timer clock update
if (clockTimer)
clockTimer--;
// Init timer
if (initTimer > 0)
initTimer--;
return;
};
int8_t getEncoder(void)
{
int8_t ret = 0;
if (encRes) {
if (encRes > 0) {
while (encCnt >= encRes) {
ret++;
encCnt -= encRes;
}
while (encCnt <= -encRes) {
ret--;
encCnt += encRes;
}
} else {
while (encCnt <= encRes) {
ret++;
encCnt -= encRes;
}
while (encCnt >= -encRes) {
ret--;
encCnt += encRes;
}
}
} else {
ret = encCnt;
encCnt = 0;
}
return ret;
}
cmdID getBtnCmd(void)
{
cmdID ret = cmdBuf;
cmdBuf = CMD_RC_END;
return ret;
}
cmdID getRcCmd(void)
{
// Place RC event to command buffer if enough RC timer ticks
IRData ir = takeIrData();
uint8_t rcCmdBuf = CMD_RC_END;
if (ir.ready && (ir.type == rcType) && (ir.address == rcAddr)) {
if (!ir.repeat || (rcTimer > 800)) {
rcTimer = 0;
rcCmdBuf = rcCmdIndex(ir.command);
}
if (ir.command == rcCode[CMD_RC_VOL_UP] || ir.command == rcCode[CMD_RC_VOL_DOWN]) {
if (rcTimer > 400) {
rcTimer = 360;
rcCmdBuf = rcCmdIndex(ir.command);
}
}
}
return rcCmdBuf;
}
uint16_t getBtnBuf(void)
{
return btnPrev;
}
uint16_t getEncBuf(void)
{
return encPrev;
}
void setDisplayTime(uint16_t value)
{
displayTime = value;
return;
}
uint16_t getDisplayTime(void)
{
return displayTime;
}
uint8_t getSensTimer(void)
{
return sensTimer;
}
void setSensTimer(uint8_t val)
{
sensTimer = val;
return;
}
int16_t getStbyTimer(void)
{
return stbyTimer;
}
void setStbyTimer(int16_t val)
{
stbyTimer = val;
return;
}
void setSecTimer(uint16_t val)
{
secTimer = val;
return;
}
int16_t getSecTimer(void)
{
return secTimer;
}
void setClockTimer(uint8_t value)
{
clockTimer = value;
return;
}
uint8_t getClockTimer(void)
{
return clockTimer;
}
void enableSilenceTimer(void)
{
if (silenceTime)
silenceTimer = 60 * silenceTime;
else
silenceTimer = STBY_TIMER_OFF;
return;
}
int16_t getSilenceTimer(void)
{
return silenceTimer;
}
void disableSilenceTimer(void)
{
silenceTimer = STBY_TIMER_OFF;
return;
}
void setInitTimer(int16_t value)
{
initTimer = value;
return;
}
int16_t getInitTimer(void)
{
return initTimer;
}