-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbox.c
369 lines (336 loc) · 9.72 KB
/
box.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
#include <avr/io.h>
#include "Timer.h"
#include "led.h"
#include "box.h"
#include "adc.h"
#include "WireConversions.h"
#include <avr/eeprom.h>
#define UNUSED(x) (void)x
//FIXME write a generic debouncer and replace all current debouncers with it
//TODO both boxes: don't care about errors after task completed
uint32_t status;
uint8_t tool_was_in_slot;
#define BOX_TYPE_EEPROM_ADDR ((uint8_t*)13)
#define BOX_KNOWN_TYPES 2
void
set_box_type(uint8_t x)
{
eeprom_update_byte(BOX_TYPE_EEPROM_ADDR, x);
}
uint8_t
get_box_type(void)
{
return eeprom_read_byte(BOX_TYPE_EEPROM_ADDR);
}
uint8_t
determine_box_type(void)
{
// read eeprom location for box type. If 0, autodetermine and set. If nonzero, be that. If it's a value above the known types, act as if it was zero.
//this was tested with a pokey and peggy and worked for both
uint8_t v = eeprom_read_byte(BOX_TYPE_EEPROM_ADDR);
if (v && v <= BOX_KNOWN_TYPES) return v;
uint16_t val = 0;
val += adc_values[DROP_ERROR_LINE];
uint16_t ref = 512*1;
if (val < ref) {
//all low: peggy
v = BOX_TYPE_PEGGY;
} else {
//all high: pokey
v = BOX_TYPE_POKEY;
}
eeprom_write_byte(BOX_TYPE_EEPROM_ADDR, v);
return v;
}
// explicitly initializing to 0 to indicate that it's unlimited by default
ms_time_t timeout = 0;
void
buzzer_init(void)
{
//set up timer and pwm
//page 164 and following
TCCR4A = (1<<PWM4B) | (1<<COM4B0);
TCCR4B = (1<<CS42)|(1<<CS41); /* set prescaler to 1/32 */
TCCR4D &= ~0x03;
}
inline void
buzzer_set_duty_cycle(uint8_t x)
{
//assign to pwm compare register
OCR4B = x;
}
void
buzzer_on(void) {buzzer_set_duty_cycle(0xf0);}
void
buzzer_off(void) {buzzer_set_duty_cycle(0);}
struct led buzzer_as_led = {.on = buzzer_on, .off = buzzer_off};
//FIXME fix this mess
#define AS_USE_LEDS(dir, name, led, port, ddr, num) led(dir, name, port, ddr, num)
#define AS_LED_ON_DECL(name, port, ddr, num) void name ## _on(void);
#define __notled__(dir, name, port, ddr, num)
#define __led__(dir, name, port, ddr, num) AS_LED_ON_DECL(name, port, ddr, num)
//make declarations
BOX_HARDWARE_TABLE(AS_USE_LEDS);
#undef __notled__
#undef __led__
#define AS_LED_OFF_DECL(name, port, ddr, num) void name ## _off(void);
#define __notled__(dir, name, port, ddr, num)
#define __led__(dir, name, port, ddr, num) AS_LED_OFF_DECL(name, port, ddr, num)
// make declarations
BOX_HARDWARE_TABLE(AS_USE_LEDS);
#undef __notled__
#undef __led__
#define AS_LED_ON_FUN(name, port, ddr, num) void name ## _on(void) \
{port |= (1<<num);}
#define __notled__(dir, name, port, ddr, num)
#define __led__(dir, name, port, ddr, num) AS_LED_ON_FUN(name, port, ddr, num)
// make declarations
BOX_HARDWARE_TABLE(AS_USE_LEDS);
#undef __notled__
#undef __led__
#define AS_LED_OFF_FUN(name, port, ddr, num) void name ## _off(void) \
{port &= ~(1<<num);}
#define __notled__(dir, name, port, ddr, num)
#define __led__(dir, name, port, ddr, num) AS_LED_OFF_FUN(name, port, ddr, num)
// make declarations
BOX_HARDWARE_TABLE(AS_USE_LEDS);
#undef __notled__
#undef __led__
//not needed, done all at once
//#define AS_ENABLE_OUTPUT(name, port, ddr, num) ddr |= (1<<num);
#define __notled__(dir, name, port, ddr, num)
#define __led__(dir, name, port, ddr, num) {.on = name ## _on, .off = name ## _off},
#define BOX_LED_NUM 2
struct led box_leds[BOX_LED_NUM] = {BOX_HARDWARE_TABLE(AS_USE_LEDS)};
struct led *error_led = &box_leds[0];
#define FOREACH_BOX_LED(key) \
for (struct led *key=box_leds;key<&box_leds[BOX_LED_NUM];key++)
void
box_flash_handler(void)
{
ms_time_t now = millis();
FOREACH_BOX_LED(l) do_flashing(l, now);
do_flashing(&buzzer_as_led, now);
}
void
box_test_leds(void)
{
FOREACH_BOX_LED(l) start_flashing(l,-1,250,500);
}
#define out(name, ddr, num) ddr |= (1<<num);
#define in(name, ddr, num) ddr &= ~(1<<num);
#define AS_DIRECTION_SETUP(dir, name, led, port, ddr, num) dir(name,ddr,num);
void
box_init(void)
{
BOX_HARDWARE_TABLE(AS_DIRECTION_SETUP);
tool_was_in_slot = tool_in_slot();
/*
in, tool_connected?, PORTB, PB5
out, buzzer, PORTB, PB6
out, panel_led, PORTB, PB7
in, tool_holder, PORTD, PD4
in, wall_error, PORTD, PD6
*/
}
uint16_t wall_error_timeout = 350;
int error;
uint8_t error_end_recorded;
ms_time_t last_err_time, err_early_end_time;
TIME_t host_err_time;
void
handle_wall_errors(void)
{
// TODO this should work in a different way, so that it waits for the current error to end, then waits the delay time. If no new error happens in that time, send the old one, otherwise add the time to this one
// TODO currently there is some fancy buzzer management code but it can be avoided with start_flashing(&buzzer_as_led, 1, BUZZER_LENGTH, 0);
// if it is short and no other one happens, provide the short duration. This means that this can't use the debouncing code
if (cur_tool == WALL_ERROR_OK) {
if (!error) {
error = 1;
last_err_time = millis();
host_err_time = host_millis();
buzzer_on();
error_led->on();
}
error_end_recorded = 0;
}
if (error && cur_tool != WALL_ERROR_OK && !error_end_recorded) {
err_early_end_time = millis();
error_end_recorded = 1;
}
if (error
&& ((millis() - last_err_time) > MIN_BUZZER_LENGTH)
&& cur_tool != WALL_ERROR_OK) {
if (!error_end_recorded) //I don't think this will ever execute
err_early_end_time = millis();
buzzer_off();
error_led->off();
ms_time_t elapsed = err_early_end_time - last_err_time;
error = 0;
error_end_recorded = 0;
//store error report in buffer
if (elapsed >= wall_error_timeout)
new_wall_error(&werrbuf, host_err_time, elapsed);
}
}
void
reset_wall_errors(void)
{
error = 0;
}
int
tool_in_slot(void)
{
//return if the tool is in
return (cur_tool == NO_TOOL) ? 2 : adc_values[TOOL_HOLDER_LINE] > 512;
}
tool_state
classify_tool(int tool_error, int tool_jack)
{
tool_error = tool_error < 512;
tool_jack = tool_jack > 512;
tool_state err;
if (tool_error) {
if (tool_jack) {
err = WALL_ERROR_OK;
} else {
err = WALL_ERROR_WRONG; // hardware manufacturing error
//notify home base, happens in box_tick
}
} else {
if (tool_jack) {
err = TOOL_IN_AND_OK;
} else {
err = NO_TOOL; // user error
}
}
return err;
}
#define TOOL_DELAY 200
#define TOOL_STATE_FOOTPRINT 3
void
box_tick(void)
{
cur_tool = classify_tool(
adc_values[TOOL_ERROR_LINE], adc_values[TOOL_CONNECTED_LINE]
);
if (cur_tool == WALL_ERROR_WRONG) {
//set part of status, thereby notifying lms
status |= BAD_TOOL_STATUS_F;
} else {
status &= ~BAD_TOOL_STATUS_F;
}
//TODO debounce properly
static uint8_t msg;
static uint8_t pending;
static TIME_t host_started;
static ms_time_t started;
static uint8_t last_msg_sent = -1;
uint8_t tool_in = tool_in_slot();
if (tool_in != tool_was_in_slot && tool_in != last_msg_sent) {
pending = 1;
msg = tool_was_in_slot = tool_in;
started = millis();
host_started = host_millis();
}
if (pending && (millis()-started) > TOOL_DELAY) {
pending = 0;
status &= ~((uint32_t)TOOL_STATE_FOOTPRINT << 1);
status |= msg<<1;
new_tool(&toolbuf, host_started, last_msg_sent = msg);
}
//new_tool(&toolbuf, host_millis(), tool_was_in_slot=tool_in);
}
void new_wall_error(struct wall_error_buffer *b, uint64_t stamp, ms_time_t dur)
{
if (b->occupancy >= WALL_ERROR_BUFFER_SIZE) return;
b->stamps[b->first_empty] = stamp;
b->durs[b->first_empty] = dur;
b->occupancy++;
b->first_empty++;
b->first_empty %= WALL_ERROR_BUFFER_SIZE;
}
void new_drop_error(struct drop_error_buffer *b, uint64_t stamp)
{
if (b->occupancy >= DROP_ERROR_BUFFER_SIZE) return;
b->stamps[b->first_empty] = stamp;
b->occupancy++;
b->first_empty++;
b->first_empty %= DROP_ERROR_BUFFER_SIZE;
}
void new_poke(struct poke_buffer *b, uint64_t stamp, uint8_t loc)
{
if (b->occupancy >= POKE_BUFFER_SIZE) return;
b->stamps[b->first_empty] = stamp;
b->locs[b->first_empty] = loc;
b->occupancy++;
b->first_empty++;
b->first_empty %= POKE_BUFFER_SIZE;
}
void new_tool(struct tool_buffer *b, uint64_t stamp, uint8_t newst)
{
if (b->occupancy >= TOOL_BUFFER_SIZE) return;
b->stamps[b->first_empty] = stamp;
b->newsts[b->first_empty] = newst;
b->occupancy++;
b->first_empty++;
b->first_empty %= TOOL_BUFFER_SIZE;
}
void new_event(struct event_buffer *eb, uint64_t stamp, uint8_t typ)
{
if (eb->occupancy >= EVENT_BUFFER_SIZE)
eb->stamps[eb->first_empty] = stamp;
eb->typs[eb->first_empty] = typ;
eb->occupancy++;
eb->first_empty++;
eb->first_empty %= EVENT_BUFFER_SIZE;
}
int extract_wall_error(struct wall_error_buffer *eb, uint8_t *buf, int buflen)
{
if (buflen < (8 + 4) || !eb->occupancy) return -1;
time_to_wire(eb->stamps[eb->first_real], buf);
uint32_to_wire(eb->durs[eb->first_real], &buf[8]);
eb->occupancy--;
eb->first_real++;
eb->first_real %= WALL_ERROR_BUFFER_SIZE;
return 0;
}
int extract_drop_error(struct drop_error_buffer *eb, uint8_t *buf, int buflen)
{
if (buflen < 8 || !eb->occupancy) return -1;
time_to_wire(eb->stamps[eb->first_real], buf);
eb->occupancy--;
eb->first_real++;
eb->first_real %= DROP_ERROR_BUFFER_SIZE;
return 0;
}
int extract_poke(struct poke_buffer *eb, uint8_t *buf, int buflen)
{
if (buflen < (8 + 1) || !eb->occupancy) return -1;
time_to_wire(eb->stamps[eb->first_real], buf);
buf[8] = eb->locs[eb->first_real];
eb->occupancy--;
eb->first_real++;
eb->first_real %= POKE_BUFFER_SIZE;
return 0;
}
int extract_tool(struct tool_buffer *eb, uint8_t *buf, int buflen)
{
if (buflen < (8 + 1) || !eb->occupancy) return -1;
time_to_wire(eb->stamps[eb->first_real], buf);
buf[8] = eb->newsts[eb->first_real];
eb->occupancy--;
eb->first_real++;
eb->first_real %= TOOL_BUFFER_SIZE;
return 0;
}
int extract_event(struct event_buffer *eb, unsigned char *buf, int buflen)
{
if (buflen < (8 + 1) || !eb->occupancy) return -1;
time_to_wire(eb->stamps[eb->first_real], buf);
buf[8] = eb->typs[eb->first_real];
eb->occupancy--;
eb->first_real++;
eb->first_real %= EVENT_BUFFER_SIZE;
return 0;
}