forked from mikepea/buildbrighton-ir-badge
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbadge.h
More file actions
203 lines (164 loc) · 6.13 KB
/
Copy pathbadge.h
File metadata and controls
203 lines (164 loc) · 6.13 KB
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
//
// chip specifics
// tiny45
#define EEPROM_SIZE 256
// tiny25
//#define EEPROM_SIZE 128
#define EEPROM_SEND_DELAY 1000
// common code so we know def is a badge
#define OUR_COMMON_CODE 0xbb
#define MY_CODE_HEADER (long)(OUR_COMMON_CODE)<<24 | (long)(my_id) <<16
// how many badges do we keep an record of, for calculating "i've seen you recently"
#define BADGE_BUFFER_SIZE 20
// and how long before we say, 'ok, seen you again'
#define BADGE_LAST_SEEN_MAX 30
// debugging flags
//#define TURN_OFF_COLOUR_DISPLAY 1
//#define TURN_OFF_PWM_COLOUR 1
//#define DISABLE_IR_SENDING_CODE 1
//#define DISABLE_EEPROM_SENDING_CODE 1
#define ENABLE_FLASH_BYTE_CODE 1
// our data packet is OUR_COMMON_CODE << 24 | MY_ID << 16 | mode | data(=0b00{colour})
#define COMMON_CODE_MASK 0xff000000
#define ID_MASK 0x00ff0000
#define MODE_MASK 0x0000ff00
#define DATA_MASK 0x000000ff
// our 'game modes'
#define INIT_MODE 0x01
#define SENDING_COLOUR 0x02
#define AM_ZOMBIE 0x03
#define AM_INFECTED 0x04
#define SEND_ALL_EEPROM 0x05
#define REFLECT_COLOUR 0x06
#define CYCLE_COLOURS_SEEN 0x07
#define SEND_ME_YOUR_DATA 0x08
#define PROGRAM_BADGE_ID 0x09
// some debugging modes
#define DEBUG_TURN_OFF_DISPLAY 0b00000001
#define DEBUG_TURN_OFF_SENDNEC 0b00000001
// Zombie game
#define BITTEN_MAX 3
#define MAX_TIME_INFECTED 30
#define MAX_TIME_DEAD 10
// Apple codes (device id is first byte, mine is 5c)
#define APPLE_PLAY 0x000487ee
#define APPLE_VOLUME_UP 0x000b87ee
#define APPLE_VOLUME_DOWN 0x000d87ee
#define APPLE_NEXT_TRACK 0x000787ee
#define APPLE_PREV_TRACK 0x000887ee
#define APPLE_MENU 0x000287ee
// delay for flashing out the recd code
#define IR_DATA_PRINT_DELAY 50000
#define MAX_SAME_COLOUR_RECV 5
// for matt's design
#define bogusMask 0b00100000
#define redMask 0b00000100
#define grnMask 0b00000010
#define bluMask 0b00000001
#define rgbMask 0b00000111
#define irInMask 0b00001000
#define irOutMask 0b00010000
// 4 = PB3
#define irInPortBPin 4
#define JUST_RED_ON PORTB |= rgbMask; PORTB &= ~(redMask);
#define JUST_GREEN_ON PORTB |= rgbMask; PORTB &= ~(grnMask);
#define JUST_BLUE_ON PORTB |= rgbMask; PORTB &= ~(bluMask);
#define FLASH_BLUE PORTB ^= bluMask; delay_ten_us(10000); PORTB ^= bluMask;
#define FLASH_GREEN PORTB ^= grnMask; delay_ten_us(10000); PORTB ^= grnMask;
#define FLASH_RED PORTB ^= redMask; delay_ten_us(10000); PORTB ^= redMask;
#define RED 0
#define GREEN 1
#define BLUE 2
#define displayRedMask 0b00000011
#define displayGrnMask 0b00001100
#define displayBluMask 0b00110000
#define displayRGBMask 0b00111111
// how many times to send our IR code in each 1s loop.
#define NUM_SENDS 1
#define CLKFUDGE 5 // fudge factor for clock interrupt overhead
#define CLK 256 // max value for clock (timer 0)
#define PRESCALE 8 // timer0 clock prescale
#define SYSCLOCK 8000000 // main clock speed
#define CLKSPERUSEC (SYSCLOCK/PRESCALE/1000000) // timer clocks per microsecond
#define USECPERTICK 50 // microseconds per clock interrupt tick
#define INIT_TIMER_COUNT0 (CLK - USECPERTICK*CLKSPERUSEC + CLKFUDGE)
#define RESET_TIMER0 TCNT0 = INIT_TIMER_COUNT0
// IR detector output is active low
#define MARK 0
#define SPACE 1
#define NBITS 32 // bits in IR code
#define TOPBIT 0x80000000
#define TOLERANCE 25 // percent tolerance in measurements
#define LTOL (1.0 - TOLERANCE/100.)
#define UTOL (1.0 + TOLERANCE/100.)
#define STARTNOM 9000
#define SPACENOM 4500
#define BITMARKNOM 620
#define ONESPACENOM 1600
#define ZEROSPACENOM 480
#define RPTSPACENOM 2180
#define NEC_HDR_MARK 9000
#define NEC_HDR_SPACE 4500
#define NEC_BIT_MARK 560
#define NEC_ONE_SPACE 1600
#define NEC_ZERO_SPACE 560
#define NEC_RPT_SPACE 2250
#define TICKS_LOW(us) (int) (((us)*LTOL/USECPERTICK))
#define TICKS_HIGH(us) (int) (((us)*UTOL/USECPERTICK + 1))
// pulse parameters (tick counts)
#define STARTMIN (int)((STARTNOM/USECPERTICK)*LTOL) // start MARK
#define STARTMAX (int)((STARTNOM/USECPERTICK)*UTOL)
#define SPACEMIN (int)((SPACENOM/USECPERTICK)*LTOL)
#define SPACEMAX (int)((SPACENOM/USECPERTICK)*UTOL)
#define BITMARKMIN (int)((BITMARKNOM/USECPERTICK)*LTOL-2) // extra tolerance for low counts
#define BITMARKMAX (int)((BITMARKNOM/USECPERTICK)*UTOL+2)
#define ONESPACEMIN (int)((ONESPACENOM/USECPERTICK)*LTOL)
#define ONESPACEMAX (int)((ONESPACENOM/USECPERTICK)*UTOL)
#define ZEROSPACEMIN (int)((ZEROSPACENOM/USECPERTICK)*LTOL-2)
#define ZEROSPACEMAX (int)((ZEROSPACENOM/USECPERTICK)*UTOL+2)
#define RPTSPACEMIN (int)((RPTSPACENOM/USECPERTICK)*LTOL)
#define RPTSPACEMAX (int)((RPTSPACENOM/USECPERTICK)*UTOL)
// receiver states
#define IDLE 1
#define STARTH 2
#define STARTL 3
#define BIT 4
#define ONE 5
#define ZERO 6
#define STOP 7
#define BITMARK 8
#define RPTMARK 9
#define IRBUF_CUR irparams.irbuf[j]
// macros
#define nextstate(X) (irparams.rcvstate = X)
// repeat code for NEC
#define REPEAT 0xffffffff
// Values for decode_type
#define NONE 0
#define NEC 1
#define _GAP 5000 // Minimum gap between transmissions
#define GAP_TICKS (_GAP/USECPERTICK)
#define MAXBUF 4 // IR command code buffer length (circular buffer)
// for RGB PWM in interrupt
volatile unsigned char rgb_tick = 0;
volatile unsigned char ten_ms_ticks = 0;
volatile unsigned char hundr_ms_ticks = 0;
// state machine variables irparams
static volatile struct {
char rcvstate ; // IR receiver state
char bitcounter ; // bit counter
char irdata ; // MARK or SPACE read from IR input pin
char fptr ; // irbuf front pointer
char rptr ; // irbuf rear pointer
char irpin ; // pin for IR data from detector
unsigned int timer ; // state timer
unsigned long irmask ; // one-bit mask for constructing IR code
unsigned long ircode ; // IR code
unsigned long irbuf[MAXBUF] ; // circular buffer for IR codes
} irparams ;
// state machine variables irparams
typedef struct badge_record {
//char badge_id ;
uint8_t badge_id ;
int first_seen ;
} badge_record_t;