forked from pyrodon/303Badge2012
/
main.c
423 lines (328 loc) · 10.7 KB
/
main.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
418
419
420
421
422
423
////////////////////////////////////////////////////////////////////////////////////////
// The 303 Defcon Badge Project for 2012
//
// This is a hardware and software platform that can be used to make a cool badge
// It includes lightshow, sound DAC, and chiptunes subsystem
// Alcohol sensor, and an RF communciation system.
// The software includes the ability to access functions locally, and remotely given
// sufficient badge privilege, it also has an STD simulator that probabalistically
// causes badges to infect other badges through the beacon subsystem.
//
// Native Compiler Used: SourceBoost BoostC (www.sourceboost.com)
//
////////////////////////////////////////////////////////////////////////////////////////
//
// Credits:
// Badge Concept: Don Kark (Th3D0n) and Mar Williams
// Hardware Design: Don Kark
// Software: Don Kark
// Case Concept and Design: Mar Williams and Trev Jackson
// Chiptunes Composer/Arranger: Steve Pordon
//
// Badge Production: Mar Williams, Fred Roybal, Sockwell, and numerous other 303
// and DenHac Supporters.
//
// Thank you to our primary sponsors who provided support to the project:
// Dark Tangent
// Kryus
//
// As well as other key sponsors and supporters:
// moose
// Peak Security
// y3t1
// Shaggy
// FAWCR
// Shapewerks
//
// Finally acknowledgement for code that was adapted for use in this project
// Microchip MRF49XA Reference Code
// "Hardware Chiptune Project" code release by Linus Akeson (Kry0). Used by permission.
// http://www.linusakesson.net/hardware/chiptune.php
//
// (c) 2012, Don Kark (Th3D0n)
//
//////////////////////////////////////////////////////////////////////////////////////
#include <system.h>
#include "BadgePicConfig.h"
#include "Badge.h"
#include "MRF49XA.h"
#include "ext_chiptunesong.h"
#include "sub_rfcmd.h"
//#define SPEEDTEST
#ifdef SPEEDTEST // Speeds up some of the user experience parameters for testing
#define TIME_ADDR 2
#define TIME_POV 10
#else
#define TIME_ADDR 4
#define TIME_POV 300
#endif
#define ELAPSED_SECS() (elapsed_msecs >> 10) // Not exact second but more efficient than /1000
unsigned long elapsed_msecs = 0; // Total elapsed time in seconds
unsigned char loop_msecs = 0; //
volatile unsigned char intr_intfreq; // 1-up counter every time through intr routine
volatile unsigned char intr_msecs;
unsigned char MyBadgeID;
volatile unsigned char MyMode;
unsigned char MyElev;
unsigned char rfrxbuf[PAYLOAD_MAX];
unsigned char rfrxlen;
unsigned char have_quorum;
#ifdef SPEEDTEST
#define BEACON_BASE (5*1000) // 5 second beacons
#define DENSITY_QUORUM_HI 3
#define DENSITY_QUORUM_LO 1
#define ELEVATE_BASE (5 * 60 * 1000)
#define ELEVATE_VAR 1
#define ELEVATE_DUR 30000
#else
#define BEACON_BASE (2*60*1000) // 2 minute beacons
#define DENSITY_QUORUM_HI 20 // 20 people in range
#define DENSITY_QUORUM_LO 12 // 12 people in range
#define ELEVATE_BASE (2 * 60 * 60 * 1000) // Privilege elevation about every 2 hours
#define ELEVATE_VAR 20000 // + up to 255 * 20 secs
#define ELEVATE_DUR (5*60*1000) // And will last 5 minutes
#endif
#define BEACON_RNDSCALE 8 // Random bias multiplier 8*255 = 2-3 secs (introduce some jitter)
// So beacons get out of each other's way if simultaneous
unsigned long last_beacon = 0;
#define RXBUFLEN PAYLOAD_MAX // Based on Microchip sample code.
unsigned char lastmode;
unsigned long elevatemsecs; // How long we've been elevated
unsigned long attelapsed; // How long we've been in "attention" mode.
#define DELAYMS 20
void main()
{
unsigned char i;
unsigned char savemode;
//
// Core Hardware Initialization
mcu_initialize(); // Initialize MCU resources and 8KHz interrupt...
nvreadbuf(); // Load the NV buffer from flash (get badge Addr and properties)
proc_btn1(); // Process Buttons
sound_config_polled(); // Configure the sound subsystem chip
etoh_init(); // Initialize the Alcohol Sensor Subsystem
MRF49XA_Init(); // Initialize the RF tranceiver
usb_ser_init(); // Initialize the serial port
light_init();
//
// Preliminary setup and interaction
// These setup a precurser items will occur prior to going into the main worker loop.
// This is mainly the one-time powerup and "boot" sequence
//
// Show the address of the badge on the LEDs (since there are only 7 LED's
// the binary number will show in green if the badge is < 128 and red if >
// 128 with the badge addresses 0 and 128 showing up as nothing (typically illegal
// numbers anyway
// Note, led_showbin does not interact with the interrupt driven light show well.
// So only use if you may not be in interrupt mode yet of if a show is not running
delay_s(1);
led_showbin(LED_SHOW_RED, nvget_badgetype() | 0x40); // Startup Indicator
delay_10us(100);
led_showbin(LED_SHOW_RED, 0);
delay_s(1);
led_showbin(LED_SHOW_RED, nvget_badgeperm() | 0x40); // Same
delay_10us(100);
led_showbin(LED_SHOW_RED, 0);
delay_s(1);
//
// Shows address as described above
//
MyBadgeID = nvget_badgeid();
led_showbin(MyBadgeID & 0x80 ? LED_SHOW_RED : LED_SHOW_GRN, MyBadgeID & 0x7f );
delay_s(TIME_ADDR);
led_showbin(LED_SHOW_NONE, 0);
init_rnd(nvget_badgetype()+1, MyBadgeID, 0x55); // Seed tha random number generator
//
// Perform POV credits (spin the badge on your wrist :)
//
led_pov(LED_SHOW_AUTO, TIME_POV);
// Enable Global Interrupts
// Use Interrupts. ISR is interrupt(), below
intcon.GIE=1;
//sample_play();
//delay_s(2);
MyMode = MODE_IDLE;
modelights(); // Sets lightshow based on a few state parameters
// Calculate initial elevation time
elevatemsecs = elapsed_msecs + ELEVATE_BASE + (unsigned long) rnd_randomize() * ELEVATE_VAR;
//
// Main Worker Loop
//
while(1) {
//
// Handle the elapsed time clocks
//
clear_bit(intcon, TMR0IE);
loop_msecs = intr_msecs; // Take copy to avoid race conditions
intr_msecs = 0;
set_bit(intcon, TMR0IE);
elapsed_msecs += loop_msecs; // Update elapsed time in msecs
savemode = MyMode; // Used to detact mode changes when command processers set new mode
switch(MyMode) {
case MODE_IDLE:
// If button pressed, handle it
if(btn_commandwork(loop_msecs) == BTN_WORKING) {
MyMode = MODE_GETCMD;
modelights();
playsong = 0;
//light_pause();
break;
}
// Look for incoming RF packet
if(MRF49XA_Receive_Packet(rfrxbuf,&rfrxlen) == PACKET_RECEIVED) {
MRF49XA_Reset_Radio();
//led_showbin(LED_SHOW_RED, 2);
//delay_10us(10);
//led_showbin(LED_SHOW_RED, 0);
rfcmd_execute(rfrxbuf, rfrxlen);
}
if(savemode != MyMode) { // Don't process remainder of idle switch if state change
break;
}
// If time to send a beacon, send it. Also process quorum sounds.
if(elapsed_msecs > (last_beacon + (unsigned long)BEACON_BASE + ((unsigned long)rnd_randomize()* BEACON_RNDSCALE))) { // time for beacon
rfcmd_3send(RFCMD_BEACON, MyBadgeID, nvget_socvec1());
last_beacon = elapsed_msecs;
rfcmd_clrcden();
if(have_quorum) {
if(rfcmd_getdensity() <= DENSITY_QUORUM_LO) {
have_quorum = 0;
if(!playsong) tune_startsong(SONG_CHIRP2);
}
}
else {
if(rfcmd_getdensity() >= DENSITY_QUORUM_HI) {
have_quorum = 1;
if (!playsong) tune_startsong(SONG_303);
}
}
modelights();
}
break;
case MODE_ETOH: // Run the alcohol sensor state machine
if(etoh_breathtest(ETOH_DOWORK, loop_msecs ) == ETOH_DONE) { // worker for ETOH
switch(etoh_getreward()) {
case REWARD_SOBER:
tune_startsong(SONG_BUZZER);
break;
case REWARD_TIPSY:
tune_startsong(SONG_PEWPEW);
break;
case REWARD_DRUNK:
tune_startsong(SONG_CACTUS);
break;
}
MyMode = MODE_IDLE;
light_show(LIGHTSHOW_SOCFLASH, 5);
break;
}
break;
case MODE_ATTEN:
// Puts all badges into visual attention mode to see who's in range
if(MyMode != lastmode) {
attelapsed = 0;
}
attelapsed += loop_msecs;
if(attelapsed > 120000) { // Stays there 2 min (should be in #def)
MyMode = MODE_IDLE;
}
if(MRF49XA_Receive_Packet(rfrxbuf,&rfrxlen) == PACKET_RECEIVED) {
MRF49XA_Reset_Radio();
rfcmd_execute(rfrxbuf, rfrxlen);
}
modelights();
break;
case MODE_GETCMD:
if (btn_commandwork( loop_msecs) != BTN_WORKING) {
MyMode = MODE_IDLE;
modelights();
}
break;
} // switch
tune_songwork(); // Worker thread for songs
light_animate(loop_msecs); // worker thread for lights
if(MyElev) { // Handle the privilege elevation timer
if(elapsed_msecs > (elevatemsecs + ELEVATE_DUR)) {
MyElev = 0;
elevatemsecs = elapsed_msecs + ELEVATE_BASE + (unsigned long) rnd_randomize() * ELEVATE_VAR;
modelights();
}
} else {
if(elapsed_msecs > elevatemsecs) {
MyElev = 1;
sample_play();
modelights();
}
}
lastmode = MyMode;
}
}
static unsigned char soundval = 0;
void interrupt( void )
{
if(intcon.TMR0IF) { // 8 KHz driven interrupt events
intcon.TMR0IE = 0;
tmr0l = TIMER_REGVAL;
intr_intfreq++;
if(intr_intfreq >= 8) { // Convert HZ rate to msecs
intr_intfreq = 0;
intr_msecs++; // Rolling msec counter (wraps every 65K seconds)
}
if (playsong) { // Call respective intr drivers. We don't have enough
// Time for all at 8KHz, so this enforces a priority
tune_play_intr();
} else if(playsample) {
sample_intr();
} else {
light_intr();
}
} else { // CALL FOR HELP -- UNKNOWN INTERRUPT (DEBUG STUFF)
portc.SIG_RC_DISP_GRN_O = 0;
portb.SIG_RB_DISPLED3_O = 1;
portb.SIG_RB_DISPLED4_O = 1;
portb.SIG_RB_DISPLED5_O = 1;
}
intcon.TMR0IF = 0;
intcon.TMR0IE = 1;
}
void modelights() // Call whenever MyMode, TmpPrivs changes, density
{
switch(MyMode) {
case MODE_IDLE:
switch(nvget_badgetype()) {
case NVBT303:
case NVBTHAC:
if(MyElev) {
light_show(LIGHTSHOW_DONGS, 2);
}
else {
if(have_quorum) {
light_show(LIGHTSHOW_SOCFLASH, 1);
}
else {
light_show(LIGHTSHOW_SOCFLASH, 3);
}
}
break;
case NVBTSKYGRUNT:
light_show(LIGHTSHOW_SKYGRUNT, 5);
break;
case NVBTSKYENFORCER:
light_show(LIGHTSHOW_SKYENFORCER, 3);
break;
case NVBTSKYSPEAKER:
light_show(LIGHTSHOW_SKYSPEAKER, 5);
break;
} // switch nvget_badgetype
break;
case MODE_ATTEN:
light_show(LIGHTSHOW_RAINBOW, 5);
break;
case MODE_ETOH:
light_show(LIGHTSHOW_OFF, 5);
break;
case MODE_GETCMD:
light_show(LIGHTSHOW_OFF, 5);
break;
} // Switch mymode
}