-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-ir.c
352 lines (297 loc) · 7.19 KB
/
main-ir.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
#define F_CPU 8000000L // clock frequency
#include <avr/io.h>
#include <avr/interrupt.h>
//#include <avr/pgmspace.h>
#include <avr/wdt.h>
#include <inttypes.h>
#include <string.h>
//#include "defines.h"
/******************* REMOTE CONFIG ********************************************/
#ifdef PANASONIC_VEQ1141
# include "panasonic_veq1141.h"
#else
# ifdef SANYO_PLCXT10A
# include "sanyo_plcxt10a.h"
# else // !SANYO_PLCXT10A
# error "No remote type defined!!!"
# endif // SANYO_PLCXT10A
#endif // PANASONIC_VEQ1141
#define ZERO_TEST
/******************* PINOUT CONFIG ********************************************/
#define LED_PIN PINB // LED PIN
#define LED_DDR DDRB // LED DDR
#define LED_PORT PORTB // LED PORT
#define LED_PWR 0 // zasilanie
#define LED_PWR_ON do { LED_PORT |= _BV(LED_PWR); } while(0)
#define LED_PWR_OFF do { LED_PORT &= ~_BV(LED_PWR); } while(0)
#define LED_IR 1 // odbior w podczerwieni
#define LED_IR_ON do { LED_PORT |= _BV(LED_IR); } while(0)
#define LED_IR_OFF do { LED_PORT &= ~_BV(LED_IR); } while(0)
#define LED_C 2 // kod przycisku pasuje do zdefiniowanych
#define LED_C_ON do { LED_PORT |= _BV(LED_C); } while(0)
#define LED_C_OFF do { LED_PORT &= ~_BV(LED_C); } while(0)
#define LED_MASK (_BV(LED_IR) | _BV(LED_PWR) | _BV(LED_C))
// jumper okreslania sposobu sprawdzania pakietow (zwarty -sprawdza tylko pierwszy pakiet)
#define MODE_SW_PIN PINB
#define MODE_SW_DDR DDRB
#define MODE_SW_PORT PORTB
#define MODE_SW 3
/******************* MISC CONFIG **********************************************/
// szybkosc kursora (w pikselach)
#define MOVE 0x04
#define TIMEOUT 3500
#define IR_MAX 96
#define SCALING 8
/******************************************************************************/
#define calcIRTime(val) ((val + (1 << (SCALING-3-1))) >> (SCALING-3))
static struct //
{
unsigned char length;
unsigned char count;
unsigned char data[IR_MAX];
} ir;
static unsigned char edges;
static unsigned int irdelta;
static uint32_t lastcode = 0;
static volatile unsigned char flags = 0;
static unsigned char reportBuffer[3] = {0};
#define IR_INTS_ON do{TIMSK |= (_BV(OCIE1A) | _BV(ICIE1));} while(0)
#define IR_INTS_OFF do{TIMSK &= ~(_BV(OCIE1A) | _BV(ICIE1));} while(0)
//#define IR_INTS_OFF do{TIMSK = 0;}while(0)
static char makeReport(void);
static void sendReport(void);
// zmiana zbocza na ICP
ISR(TIMER1_CAPT_vect)
{
static unsigned int prev;
unsigned int stamp;
unsigned char delta;
stamp = ICR1;
TCCR1B ^= _BV(ICES1);
LED_IR_ON;
delta = calcIRTime(stamp - prev);
asm volatile("" : : "r"(delta) );
prev = stamp;
// update timeout value
OCR1A = stamp + TIMEOUT * CYCLES_PER_US / 8;
if ( !(flags & (FLG_IGNORE_PACKET | FLG_MAKING_REPORT)) )
{
ir.length = 0;
if ( edges > 0 )
ir.data[edges - 1] = delta;
++edges;
}
}
// timeout odbioru pakietu w podczerwieni
ISR(TIMER1_COMPA_vect)
{
IR_INTS_OFF;
LED_IR_OFF;
if ( edges >= 40 &&
!(flags & (FLG_IGNORE_PACKET | FLG_MAKING_REPORT))
)
{
ir.count++;
ir.length = edges - 1; // nowy pakiet kompletny
flags |= FLG_IR_RECEIVED;
}
edges = 0;
flags &= ~FLG_IGNORE_PACKET;
TCCR1B &= ~_BV(ICES1);
TCNT0 = TCNT0V;
TIMSK |= _BV(TOIE0);
IR_INTS_ON;
}
ISR(TIMER0_OVF_vect)
{
if( !edges ) {
if( (ir.count > 0) &&
( (lastcode == CLMB) || (lastcode == CRMB) ) ) {
// raportuj uwolnienie przycisku LMB/RMB
reportBuffer[0] = 0;
reportBuffer[1] = 0;
reportBuffer[2] = 0;
flags |= FLG_SENDING_REPORT;
sendReport();
flags &= ~FLG_SENDING_REPORT;
}
ir.count = 0;
lastcode = 0;
}
TCNT0 = TCNT0V;
TIMSK &= ~_BV(TOIE0);
}
static char makeReport(void)
{
unsigned char i = 0;
unsigned char retval = 0;
unsigned char cc = ir.length-2;
uint32_t code = 0;
flags |= FLG_MAKING_REPORT;
if( !(MODE_SW_PIN & _BV(MODE_SW)) && (ir.count > 0) )
code = lastcode;
else {
for ( i = 0 ; i < 24 ; i++ ) {
if (edges)
flags |= FLG_IGNORE_PACKET;
#ifdef ZERO_TEST
if( ir.data[cc] > irdelta )
code &= ~_BV(i);
else
code |= _BV(i);
#else
if( ir.data[cc] > irdelta )
code |= _BV(i);
else
code &= ~_BV(i);
#endif
cc -= 2;
}
}
flags &= ~FLG_MAKING_REPORT;
//if ( !(flags & FLG_SENDING_REPORT) ) {
switch (code) {
case CUPH:
reportBuffer[0] = 1;
reportBuffer[1] = 0;
reportBuffer[2] = -MOVE;
lastcode = code;
retval = 1;
break;
case CDOWNH:
reportBuffer[0] = 1;
reportBuffer[1] = 0;
reportBuffer[2] = MOVE;
lastcode = code;
retval = 1;
break;
case CLEFTH:
reportBuffer[0] = 1;
reportBuffer[1] = -MOVE;
reportBuffer[2] = 0;
lastcode = code;
retval = 1;
break;
case CRIGHTH:
reportBuffer[0] = 1;
reportBuffer[1] = MOVE;
reportBuffer[2] = 0;
lastcode = code;
retval = 1;
break;
case CUP:
reportBuffer[0] = 0;
reportBuffer[1] = 0;
reportBuffer[2] = -MOVE;
lastcode = code;
retval = 1;
break;
case CDOWN:
reportBuffer[0] = 0;
reportBuffer[1] = 0;
reportBuffer[2] = MOVE;
lastcode = code;
retval = 1;
break;
case CLEFT:
reportBuffer[0] = 0;
reportBuffer[1] = -MOVE;
reportBuffer[2] = 0;
lastcode = code;
retval = 1;
break;
case CRIGHT:
reportBuffer[0] = 0;
reportBuffer[1] = MOVE;
reportBuffer[2] = 0;
lastcode = code;
retval = 1;
break;
case CLMB:
reportBuffer[0] = 1;
reportBuffer[1] = 0;
reportBuffer[2] = 0;
lastcode = code;
retval = 1;
break;
case CRMB:
reportBuffer[0] = 2;
reportBuffer[1] = 0;
reportBuffer[2] = 0;
lastcode = code;
retval = 1;
break;
case CRMBH:
reportBuffer[0] = 3;
reportBuffer[1] = 0;
reportBuffer[2] = 0;
lastcode = code;
retval = 1;
break;
default:
reportBuffer[0] = 0;
reportBuffer[1] = 0;
reportBuffer[2] = 0;
break;
}
//}
return retval;
}
static void sendReport(void)
{
unsigned char i;
for ( i=0 ; i< sizeof(reportBuffer) ; i++ ) {
while(!(UCSRA & _BV(UDRE))) ;
UDR = reportBuffer[i];
}
}
static void hardwareInit(void)
{
MODE_SW_PORT |= _BV(MODE_SW);
MODE_SW_DDR &= ~_BV(MODE_SW);
LED_DDR |= LED_MASK;
TCCR1B = _BV(ICNC1)
| _BV(CS11);
// ir interrupts (OCIE1A, ICIE1)
IR_INTS_ON;
/*** UART init ***/
// baud rate
UBRRH = (unsigned char)(((F_CPU)/((BAUD_RATE)*16l)-1)>>8);
UBRRL = (unsigned char) ((F_CPU)/((BAUD_RATE)*16l)-1);
// potrzebujemy tylko tx
UCSRB = _BV(TXEN);
// asynchronous mode, 8N1
UCSRC = _BV(UCSZ1) | _BV(UCSZ0);
// Timer0 prescaler na 1024
TCCR0B |= _BV(CS02) | _BV(CS00);
}
/* ------------------------------------------------------------------------- */
int main(void)
{
# ifdef ZERO_TEST
irdelta = calcIRTime(TZEROH);
# else
irdelta = calcIRTime(TONEH);
# endif
wdt_enable(WDTO_1S);
hardwareInit();
sei();
LED_PWR_ON;
LED_IR_OFF;
LED_C_OFF;
for(;;){
wdt_reset();
if ( (flags & FLG_IR_RECEIVED) ) {
if ( makeReport() ) {
LED_C_ON;
flags |= FLG_SENDING_REPORT;
sendReport();
LED_C_OFF;
ir.length = 0;
}
flags &= ~( FLG_SENDING_REPORT | FLG_IR_RECEIVED );
}
}
return 0;
}
/* ------------------------------------------------------------------------- */