-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyaris.c
229 lines (208 loc) · 6.04 KB
/
yaris.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
#define ONE_TIME 2250UL
#define ZERO_TIME 1120UL
#define REPEAT_TIME 110000UL
#define INPUT_1 1
#define INPUT_A 2 // ADC1 aka PB2
#define ADC_A 1 // ADC1 aka PB2
#define OUTPUT_1 3
#define LED 4
// fake no-op definition to satisfy arduino IDE, that will be replaced by external macro:
#define _key_definition(x) _delay_ms(1000)
#include <avr/io.h>
#include <avr/interrupt.h> // for cli()
#include <util/delay.h>
typedef enum {
LOW,
HIGH
} DigitalState;
typedef enum {
K_NONE,
K_VOL_DOWN,
K_VOL_UP,
K_BACK,
K_FORWARD,
K_MODE,
K_PAUSE,
K_SOURCE,
N_KEYS
} Key;
static inline uint16_t analog_read(){
ADMUX = (1 << REFS0) | ADC_A; // Set ADC to read our defined ADC_A channel, comparing with the internal 1.1v reference for higher resolution.
ADCSRA |= (1 << ADSC) | (1 << ADEN); // Start conversion
while(!bit_is_set(ADCSRA,ADIF)); // Loop until conversion is complete
ADCSRA |= (1 << ADIF); // Clear ADIF by writing a 1 (this sets the value to 0)
return(ADC);
}
static inline DigitalState digital_read() {
return (PINB & (1<<INPUT_1)) ? HIGH : LOW;
}
static inline void on() { DDRB |= (1<<OUTPUT_1); }
static inline void off() { DDRB &= ~(1<<OUTPUT_1); }
static inline void ledOn() { PORTB |= (1<<LED); }
static inline void ledOff() { PORTB &= ~(1<<LED); }
void send_agc(){
on();
_delay_us(9000);
off();
_delay_us(4500);
}
static void send_1() {
on();
_delay_us(560);
off();
_delay_us(ONE_TIME - 560);
}
static void send_0() {
on();
_delay_us(560);
off();
_delay_us(ZERO_TIME - 560);
}
static void send_b9(){
send_1(); send_0(); send_0(); send_1(); send_1(); send_1(); send_0(); send_1(); send_0(); send_1(); send_1(); send_0(); send_0(); send_0(); send_1(); send_0();
}
static void send_key(Key key) {
if (key == K_MODE){ _key_definition(0xb916); }
if (key == K_PAUSE){ _key_definition(0xb90e); }
if (key == K_SOURCE){ _key_definition(0xb913); }
if (key == K_VOL_DOWN){ _key_definition(0xb915); }
if (key == K_VOL_UP){ _key_definition(0xb914); }
if (key == K_BACK){ _key_definition(0xb90a); }
if (key == K_FORWARD){ _key_definition(0xb90b); }
}
static void send_repeat() {
ledOn();
on();
_delay_us(9000);
off();
_delay_us(2250);
on();
_delay_us(560);
off();
ledOff();
_delay_us(REPEAT_TIME - (9000 + 2250 + 560));
}
static inline void setup() {
WDTCR = (1<<4); // Set WDCE to unset WDE (disable watchdog timer).
DDRB = (1<<LED); // LED pin is proper output; output pin starts in tri-state and DDRB will be toggled to drag it to ground.
PORTB = (1<<INPUT_A) | (1<<INPUT_1); // Activate pull-ups on both inputs.
}
static Key held_key_() {
Key found = K_NONE;
uint16_t analog = analog_read();
if (digital_read() == LOW){ found = found ? N_KEYS : K_MODE; }
if (analog > 160 && analog < 500){ found = found ? N_KEYS : K_VOL_DOWN; }
if (analog > 100 && analog < 150){ found = found ? N_KEYS : K_VOL_UP; }
if (analog > 55 && analog < 90){ found = found ? N_KEYS : K_FORWARD; }
if (analog > 25 && analog < 52){ found = found ? N_KEYS : K_BACK; }
return found >= N_KEYS ? K_NONE : found;
// If multiple key presses are detected, something is wrong, so do nothing.
}
static Key held_key() {
// Always read twice to check, as a cheap form of analog debouncing.
Key k = held_key_();
return (k == held_key_() ? k : K_NONE);
}
void handleSourceSwitch() {
while (held_key() == K_MODE){
ledOn();
_delay_ms(20);
ledOff();
_delay_ms(80);
ledOn();
send_agc();
send_key(K_SOURCE);
on();
_delay_us(560);
off();
ledOff();
while (held_key() == K_MODE) {
}
for (int toWait=25; toWait; --toWait) { // Timeout until exiting back to normal main loop.
_delay_ms(50);
Key k = held_key();
if (k == K_NONE){ continue; } // Nothing pressed; keep waiting until timeout.
else if (k == K_MODE) { break; } // MODE pressed again, so go round to switch source a second time.
else { return; } // A different key was pressed, so exit back to the normal main loop immediately.
}
}
}
void handleModeKey() {
ledOn();
send_agc();
send_key(K_MODE); // Mute on first press.
on();
_delay_us(560);
off();
ledOff();
int toHold = 10;
while (toHold && held_key() == K_MODE) {
_delay_ms(50);
--toHold;
}
if (!toHold) { // Key was held! We will switch source now instead.
ledOn();
send_agc();
send_key(K_MODE); // Undo initial mute first.
on();
_delay_us(560);
off();
ledOff();
_delay_ms(20);
handleSourceSwitch(); // Enter second loop where further single presses of MODE temporarily mean "switch source".
return;
}
else if (held_key() == K_NONE) { // Key was released -- wait for potential double tap...
ledOff();
for (int toWait=6; toWait; --toWait) {
_delay_ms(50);
Key k = held_key();
if (k == K_NONE){ continue; } // Nothing pressed; keep waiting until timeout.
else if (k == K_MODE) { // Double tap! We must send play/pause!
ledOn();
send_agc();
send_key(K_MODE); // Undo initial mute first.
on();
_delay_us(560);
off();
ledOff();
_delay_ms(20);
ledOn();
send_agc();
send_key(K_PAUSE);
on();
_delay_us(560);
off();
ledOff();
while (held_key() == K_MODE) {} // Wait for key to be released before exiting.
break;
}
else { break; } // A different key was pressed, so exit back to the normal main loop immediately.
}
}
}
int main() {
setup();
while(1){
Key key = held_key();
if (key == K_NONE){
continue; // Just keep waiting, if nothing was pressed.
}
if (key == K_MODE){
handleModeKey(); // This key is special and multifunctional, so has its own handler function.
continue;
}
// For all other keys, just behave like a normal remote, sending the code and repeating while held.
ledOn();
send_agc();
send_key(key);
on();
_delay_us(560);
off();
ledOff();
while (held_key() == key) {
send_repeat();
}
}
return 0;
}