-
Notifications
You must be signed in to change notification settings - Fork 0
/
recv.cpp
135 lines (111 loc) · 2.44 KB
/
recv.cpp
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
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdlib.h>
#include "serial.h"
/*
* Sync bit: I_SHORT, I_SYNC
* _
* | |_______________________________
* "0" bit: I_SHORT, I_LONG
* _
* | |___
* "1" bit: I_LONG , I_SHORT
* ___
* | |_
*/
int main(void)
{
cli();
TCCR0B |= (1<<CS02) | (1<<CS00); //prescale timer
TIMSK0 |= (1<<TOIE0); //enable timer overflow interrupt
// PB3 is the input
DDRB &= ~(1 << PB3);
// Set timer on PB3
PCMSK |= (1<<PCINT3); // tell pin change mask to listen to D3
GIMSK |= (1<<PCIE ); // enable PCINT interrupt in the general interrupt mask
sei();
while(1);
}
#define DIFF(x,y) ((x<y)?(y-x):(x-y))
#define I_SHORT 3
#define I_LONG 10
#define I_SYNC 105
#define CLEAR_COMMAND { \
length = 0;\
command = 0; \
}
uint32_t command;
uint8_t length;
#define REED_OPEN 0x1014
#define REED_CLOSE 0x1015
#define REMOTE_BUTTON1 0x154115
#define REMOTE_BUTTON2 0x154114
void handleCommand()
{
#ifdef DEBUG
TxByte('S');
SEND_32(command);
TxByte(length);
#endif
switch(command)
{
case REED_OPEN : TxByte('O'); break;
case REED_CLOSE : TxByte('C'); break;
case REMOTE_BUTTON1: TxByte('L'); break;
case REMOTE_BUTTON2: TxByte('M'); break;
}
}
ISR(TIM0_OVF_vect)
{
CLEAR_COMMAND;
}
ISR(PCINT0_vect)
{
cli();
if (PINB & (1 << PB3)) // Rising
{
if (DIFF(TCNT0, I_SHORT) < 2) // 1
{
#ifdef DEBUG
TxByte('1');
#endif
command <<= 1;
command |= 1;
length++;
}
else if (DIFF(TCNT0, I_LONG) < 2)
{
#ifdef DEBUG
TxByte('0');
#endif
command <<= 1;
length++;
}
else if (DIFF(TCNT0, I_SYNC) < 15)
{
#ifdef DEBUG
TxByte('S');
#endif
handleCommand();
CLEAR_COMMAND;
}
else
{
#ifdef DEBUG
TxByte('?');
TxByte(TCNT0);
#endif
CLEAR_COMMAND;
}
if ( (command & 0b10000000000000000000000000000000)||(length == 32) )
{
#ifdef DEBUG
TxByte('R');
#endif
CLEAR_COMMAND;
}
}
TCNT0 = 0;
sei();
}