-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmating.c
391 lines (349 loc) · 13 KB
/
mating.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
/*
* mating.c
*
* Created on: Jul 13, 2016
* Author: George
*/
#include "qc13.h"
#include "mating.h"
#include "led_display.h"
#include "leg_anims.h"
#include "badge.h"
#include "metrics.h"
//typedef struct {
// uint8_t proto_version;
// uint8_t from_addr;
// uint8_t hat_award_id;
// uint8_t camo_id;
// uint16_t flags;
// uint16_t crc16;
//} matepayload;
matepayload mp_out = {
MATE_VERSION | MATE_PROTO_BADGE,
0, //my_conf.badge_id,
0xff, // hat_award_id
0, //my_conf.camo_id,
BIT6,
0x00
};
matepayload mp_in = {0};
const uint8_t mate_sync_bytes[MATE_NUM_SYNC_BYTES] = {0xa0, 0xff, 0x00, 0xaa};
uint8_t mate_payload_out[sizeof(matepayload) + MATE_NUM_SYNC_BYTES] = {0};
uint8_t mate_payload_in[sizeof(matepayload)] = {0};
uint8_t mate_state = 0;
volatile uint8_t uart_in_ignore = 0;
volatile uint8_t uart_in_synced = 0;
volatile uint8_t uart_in_index = 0;
volatile uint8_t uart_out_index = 0;
volatile uint8_t uart_sending = 0;
volatile uint8_t uart_out_len = sizeof(matepayload) + MATE_NUM_SYNC_BYTES;
void init_mating() {
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2, GPIO_PIN5, GPIO_SECONDARY_MODULE_FUNCTION); // TX
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P2, GPIO_PIN6, GPIO_SECONDARY_MODULE_FUNCTION); // RX
// N = clockfreq / baudrate
// ratio N = 16,000,000 / 9600 = 1666.67
EUSCI_A_UART_initParam ini = {};
ini.selectClockSource = EUSCI_A_UART_CLOCKSOURCE_SMCLK;
ini.clockPrescalar = 104; // N/16
ini.firstModReg = 2; // N - (104*16)
ini.secondModReg = 0xD6; // fractional part of N: 0.6667. Use lookup table.
ini.parity = EUSCI_A_UART_NO_PARITY;
ini.msborLsbFirst = EUSCI_A_UART_LSB_FIRST;
ini.numberofStopBits = EUSCI_A_UART_ONE_STOP_BIT;
ini.uartMode = EUSCI_A_UART_MODE;
ini.overSampling = 1;
EUSCI_A_UART_init(EUSCI_A1_BASE, &ini);
EUSCI_A_UART_enable(EUSCI_A1_BASE);
EUSCI_A_UART_selectDeglitchTime(EUSCI_A1_BASE, EUSCI_A_UART_DEGLITCH_TIME_200ns);
EUSCI_A_UART_enableInterrupt(EUSCI_A1_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT);
EUSCI_A_UART_enableInterrupt(EUSCI_A1_BASE, EUSCI_A_UART_TRANSMIT_INTERRUPT);
}
uint8_t mate_camo = LEG_ANIM_NONE;
uint8_t mate_id = BADGES_IN_SYSTEM;
uint8_t mate_on_duty = 0;
uint8_t super_ink_waits_on_me = 0;
uint16_t mate_ink_wait = 0;
uint8_t in_rst = 1;
unsigned short crc16(volatile unsigned char *sbuf,unsigned char len){
unsigned short crc=0xFFFF;
while(len){
crc=(unsigned char)(crc >> 8) | (crc << 8);
crc^=(unsigned char) *sbuf;
crc^=(unsigned char)(crc & 0xff) >> 4;
crc^=(crc << 8) << 4;
crc^=((crc & 0xff) << 4) << 1;
len--;
sbuf++;
}
return crc;
}//crc16()
void mate_over_cleanup() {
mate_camo = LEG_ANIM_NONE;
mate_state = MS_IDLE;
in_rst = 1;
}
void maybe_enter_ink_wait(uint8_t local) {
if (my_conf.camo_id != mate_camo) {
return; //nope. :-(
}
mate_state = MS_INK_WAIT;
mate_ink_wait = 0;
super_ink_waits_on_me = !local;
}
void enter_super_inking() {
mate_ink_wait = 0;
just_sent_superink = 1;
mate_state = MS_SUPER_INK;
if (my_conf.dink_count < UINT32_MAX) my_conf.dink_count++;
save_inks_and_check();
}
void ink_wait_timeout() {
// We were in INK_WAIT, meaning we're waiting
// on either a local or remote button. But it didn't happen in time.
mate_state = MS_PAIRED;
}
void super_ink_timeout() {
// Cooldown is over!
mate_state = MS_PAIRED;
}
void mate_deferred_rx_interrupt() {
// Here we handle the messages.
// Load 'er up:
memcpy(&mp_in, mate_payload_in, sizeof(matepayload));
uart_in_ignore = 0;
if (mp_in.crc16 != crc16((uint8_t *) &mp_in, sizeof(matepayload)-2)) {
return; // Ignore invalid message.
}
// Basic rules:
// If O_HAI isn't asserted, we stay in MS_IDLE.
// If we get a RST anywhere outside PLUG or HALF_PAIR we go back to PLUG.
if (mate_state > MS_HALF_PAIR && mp_in.flags & M_RST) {
mate_state = MS_PLUG;
}
mate_camo = mp_in.camo_id;
mate_id = mp_in.from_addr;
mate_on_duty = (mp_in.flags & M_HANDLER_ON_DUTY)? 1 : 0;
if ((mp_in.flags & M_INK) && (my_conf.camo_id == mate_camo)) {
// "received ink button" animation (and our camos match)
tentacle_send_meta_mating(1, 0);
}
switch(mate_state) {
case MS_IDLE:
mate_over_cleanup();
break;
case MS_HALF_PAIR:
// Here we expect a ~RST message. If we get it we go to PAIRED!
// Technically this is the same behavior as below, so we...
// fall through:
case MS_PLUG:
// Here we expect a RX from a badge or pipe.
// We send a ~RST regardless.
// But what we get is probably going to have RST set: to HALF_PAIR
// But if it doesn't, we can go to PAIRED!
in_rst = 0;
mate_send_basic(0, 0, 0);
if (mp_in.flags & M_RST) {
// fresh message, go to HALF_PAIR
mate_state = MS_HALF_PAIR;
} else {
// dank message, go to PAIRED
if ((mp_in.flags & M_PIPE) && (mate_id == DEDICATED_BASE_ID)) {
// pipe
mate_state = MS_PIPE_PAIRED;
if (!my_conf.freeze_minuteman) {
my_conf.freeze_minuteman = 1;
my_conf_write_crc();
}
} else if (!(mp_in.flags & M_PIPE) && mate_id != DEDICATED_BASE_ID){
// badge
mate_state = MS_PAIRED;
mate_start(mate_id, mate_on_duty);
}
}
break;
case MS_INK_WAIT:
// Here we might get an ink button from our partner.
if (!super_ink_waits_on_me && (mp_in.flags & M_INK)) {
// we were waiting on our partner, so this is what we wanted.
enter_super_inking();
break;
} // otherwise, ignore it.
// fall through:
case MS_PAIRED:
// Here we might get either:
// ink button from partner
// (go to INK_WAIT)
if (mp_in.flags & M_INK) {
maybe_enter_ink_wait(0); // waiting on me!
}
// award message
// (get award! yay!)
if ((is_uber(mp_in.from_addr) || (mp_in.from_addr == DEDICATED_BASE_ID)) && mp_in.flags & M_HAT_AWARD) {
if (award_push_hat(mp_in.hat_award_id)) {
mate_send_hat_response(1);
// yay, uber hat award!
tentacle_start_anim(LEG_ANIM_HUMAN_HAT, 1, legs_all_anim_sets[LEG_ANIM_HUMAN_HAT][LEG_ANIM_DOUBLEINK]->ink_loops, 0); // interrupt with a blinky.
} else {
mate_send_hat_response(0);
}
}
// gild message
if ((mp_in.flags & M_BESTOW_GILD) && !(my_conf.gilded & GILD_AVAIL)) {
my_conf.gilded = GILD_AVAIL | GILD_ON;
my_conf_write_crc();
eye_twinkle_on();
}
// or a regular old message that's just updating
// some of the state we have about our partner.
// That stuff is all handled above.
break;
case MS_SUPER_INK:
// this is just a place we hang out. Ignore messages here probably.
// We only care about the stuff that gets set above.
break;
case MS_PIPE_PAIRED:
// We might get a reply from the pipe. It may give us stuff!
// Or it'll just print and leave us hanging. That's OK too.
if ((mp_in.from_addr == DEDICATED_BASE_ID) && (mp_in.flags & M_HAT_CLAIM_FROM_PIPE)) {
// If the incoming hat ID matches my current hat, OR it gets awarded successfully,
// CLAIM IT.
if ((mp_in.hat_award_id == my_conf.hat_id) || award_push_hat(mp_in.hat_award_id)) {
claim_hat(mp_in.hat_award_id);
mate_send_hat_response(1);
} else {
mate_send_hat_response(0);
}
}
break;
case MS_PIPE_DONE:
// Yeah, we shouldn't get anything here.
break;
case MS_UBER_HAT_OFFER:
// we expect an ACK or a NACK.
if (mp_in.flags & M_HAT_AWARD_ACK) {
// our hat offer was accepted.
my_conf.uber_hat_given = 1;
my_conf_write_crc();
mate_state = MS_PAIRED;
} else if (mp_in.flags & M_HAT_AWARD_NACK) {
// nuttin.
mate_state = MS_PAIRED;
}
}
}
void mate_send_preserve_flags() {
if (uart_sending) {
__no_operation();
return; // don't.
}
mp_out.from_addr = my_conf.badge_id;
mp_out.camo_id = my_conf.camo_id;
mp_out.achievements = my_conf.achievements;
mp_out.odh_mate_count = my_conf.odh_mate_count;
mp_out.odh_seen_count = my_conf.odh_seen_count;
mp_out.uber_mate_count = my_conf.uber_mate_count;
mp_out.uber_seen_count = my_conf.uber_seen_count;
mp_out.mate_count = my_conf.mate_count;
mp_out.seen_count = my_conf.seen_count;
if (hat_state & HS_HANDLER && is_handler(my_conf.badge_id))
mp_out.flags |= M_HANDLER_ON_DUTY;
if (my_conf.hat_holder)
mp_out.flags |= M_HAT_HOLDER;
if (my_conf.hat_holder && !(mp_out.flags & M_HAT_AWARD))
mp_out.hat_award_id = my_conf.hat_id;
if (my_conf.hat_claimed)
mp_out.flags |= M_BADGE_HAS_CLAIMED_HAT;
mp_out.crc16 = crc16((uint8_t *) &mp_out, sizeof(matepayload) - 2);
// CRC it.
// CRC_setSeed(CRC_BASE, MATE_CRC_SEED);
// for (uint8_t i = 0; i < sizeof(matepayload) - 2; i++) {
// CRC_set8BitData(CRC_BASE, ((uint8_t *) &mp_out)[i]);
// }
// mp_out.crc16 = CRC_getResult(CRC_BASE);
// Fill'er up:
memcpy(mate_payload_out, mate_sync_bytes, MATE_NUM_SYNC_BYTES);
memcpy(mate_payload_out+MATE_NUM_SYNC_BYTES, &mp_out, sizeof(matepayload));
uart_out_index = 0;
uart_sending = 1;
uart_out_len = sizeof(matepayload) + MATE_NUM_SYNC_BYTES;
EUSCI_A_UART_transmitData(EUSCI_A1_BASE, mate_payload_out[0]);
}
void mate_send_flags(uint16_t flags) {
if (uart_sending) {
__no_operation();
return; // don't.
}
mp_out.flags = flags;
mate_send_preserve_flags();
}
void mate_send_uber_hat_bestow() {
mp_out.hat_award_id = my_conf.badge_id;
mate_state = MS_UBER_HAT_OFFER;
mate_send_flags(M_HAT_AWARD);
}
void mate_send_hat_response(uint8_t ack) {
if (ack) { // Yes!
mate_send_flags(M_HAT_AWARD_ACK);
} else { // NACK
mate_send_flags(M_HAT_AWARD_NACK);
}
}
void mate_send_basic(uint8_t click, uint8_t rst, uint8_t gild) {
uint16_t flags_out = 0;
if (click)
flags_out |= M_INK;
if (rst)
flags_out |= M_RST;
if (gild)
flags_out |= M_BESTOW_GILD;
mate_send_flags(flags_out);
}
volatile uint8_t uart_in_byte = 0;
// ISR for pairing:
#pragma vector=USCI_A1_VECTOR
__interrupt void EUSCI_A1_ISR(void)
{
switch (__even_in_range(UCA1IV, 4)) {
//Vector 2 - RXIFG
case 2:
uart_in_byte = EUSCI_A_UART_receiveData(EUSCI_A1_BASE);
if (uart_in_ignore)
return;
// If we're not synced, we need to be checking against sync bytes.
if (!uart_in_synced) {
if (uart_in_byte == mate_sync_bytes[uart_in_index]) { // matchy:
uart_in_index++;
// If we're incremented past the end of the sync bytes:
if (uart_in_index >= MATE_NUM_SYNC_BYTES) {
uart_in_synced = 1; // we're synced.
uart_in_index = 0;
}
} else { // no matchy:
uart_in_index = 0;
}
} else { // If we are, let's add it to the payload.
mate_payload_in[uart_in_index] = uart_in_byte;
uart_in_index++;
if (uart_in_index >= sizeof(matepayload)) {
// Payload received.
uart_in_ignore = 1; // Don't clobber good stuff with new stuff.
f_mate_interrupt = 1;
uart_in_synced = 0;
uart_in_index = 0;
__bic_SR_register_on_exit(SLEEP_BITS);
}
}
break; // End of RXIFG ///////////////////////////////////////////////////////
case 4: // Vector 4 - TXIFG : I just sent a byte.
if (uart_sending) { // just finished sending uart_out_index.
uart_out_index++;
if (uart_out_index >= uart_out_len) { // done
uart_sending = 0;
} else { // more to send
EUSCI_A_UART_transmitData(EUSCI_A1_BASE, mate_payload_out[uart_out_index]);
}
}
break; // End of TXIFG /////////////////////////////////////////////////////
default: break;
} // End of ISR flag switch ////////////////////////////////////////////////////
}