2 * main_basic.c - a simplified main procedure that relies upon a ersver to do
3 * anything smart. Just be a dumb interface to a display, keypad, coin mech
7 #define VERSION_STRING "R 20040622"
9 #include "display_basic.h"
25 /* cur_motor[0] is 0 for nothing, or 1..10, or 0xff to say redraw.
26 * cur_motor[1] is 0..9 and only meaningful is cur_motor[0] != 0. */
29 bool check_standalone() {
30 if (is_standalone()) {
31 send_string("011 In standalone mode. Function disabled." CRLF);
37 void unknown_command() {
38 send_string("012 Unknown command. Type HELP for help." CRLF);
41 void motor_reply(u8 code) {
42 /* returns a message of the form MXYY - X is return code, YY is motor */
45 send_string("100 Vend successful." CRLF);
48 send_string("151 No motor there." CRLF);
50 case MOTOR_CURRENT_FAIL:
51 send_string("152 Over current." CRLF);
54 send_string("153 Home sensors failing." CRLF);
57 send_string("159 Unknown motor error." CRLF);
61 void dispense_something() {
62 /* process a message VXX in sci_rx_buf where XX is motor number */
65 if (check_standalone()) return;
66 if (must_verify() && !mic_verify((void*)sci_rx_buf)) {
67 send_string("019 Message verification failed." CRLF);
71 if (my_strncmp("ALL", (char*)sci_rx_buf+1, 3)) {
74 send_string("102 Vend all motors starting." CRLF);
75 for (motor[0] = '0'; motor[0] <= '9'; motor[0]++) {
76 for (motor[1] = '0'; motor[1] <= '9'; motor[1]++) {
77 send_string("101 Vending ");
80 motor_reply(dispense_motor(motor[0]*10+motor[1]));
83 send_string("102 Vend all motors complete." CRLF);
87 if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') ||
88 (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9')) {
89 sci_rx_buf[1] = sci_rx_buf[2] = '0';
90 motor_reply(MOTOR_NOSLOT);
94 slot = (sci_rx_buf[1] - '0') * 10;
95 slot += sci_rx_buf[2] - '0';
97 motor_reply(dispense_motor(slot));
100 void write_to_display() {
101 /* process a message in the form DXXXXXXXXXX to send to display */
105 if (check_standalone()) return;
107 for (i = 0; i < 10; i++)
109 buf[i] = sci_rx_buf[i+1];
113 for (; i < 10; i++) /* pad the rest out with spaces */
118 send_string("300 Written." CRLF);
121 void send_balance() {
123 sci_tx_buf[1] = have_change?'0':'1';
124 sci_tx_buf[2] = (coin_value/10000)%10;
125 sci_tx_buf[3] = (coin_value/1000)%10;
126 sci_tx_buf[4] = (coin_value/100)%10;
127 sci_tx_buf[5] = (coin_value/10)%10;
128 sci_tx_buf[6] = coin_value%10;
136 if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') ||
137 (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9') ||
138 (sci_rx_buf[3] < '0') || (sci_rx_buf[3] > '9') ||
139 (sci_rx_buf[4] < '0') || (sci_rx_buf[4] > '9') ||
140 (sci_rx_buf[5] < '0') || (sci_rx_buf[5] > '9')) {
143 cost = sci_rx_buf[1] - '0';
144 cost *= 10; cost = sci_rx_buf[2] - '0';
145 cost *= 10; cost = sci_rx_buf[3] - '0';
146 cost *= 10; cost = sci_rx_buf[4] - '0';
147 cost *= 10; cost = sci_rx_buf[5] - '0';
153 void send_keypress(u8 key) {
154 /* send a packet of the form KX with X being the key, or R for reset */
155 if (is_standalone()) return;
158 if (key == KEY_RESET) {
163 sci_tx_buf[2] = (key%10)+'0';
165 sci_tx_buf[3] = '\0';
167 send_string(" key." CRLF);
170 void send_door_msg(bool open) {
171 if (is_standalone()) return;
174 sci_tx_buf[2] = open?'1':'0';
177 send_string(" door open." CRLF);
179 send_string(" door closed." CRLF);
183 if (check_standalone()) return;
184 if (sci_rx_buf[1] == '\0')
186 else if (sci_rx_buf[1] == 'S') { /* synchronous beep */
187 if (sci_rx_buf[2] == '\0')
189 else if (sci_rx_buf[3] != '\0' && sci_rx_buf[4] == '\0')
190 chime_for(hex2u8(sci_rx_buf[2], sci_rx_buf[3]));
192 send_string("510 Unknown chime duration." CRLF);
195 while (chime_count); /* spin */
196 send_string("500 Chimed." CRLF);
198 } else if (sci_rx_buf[2] != '\0' && sci_rx_buf[3] == '\0')
199 chime_for(hex2u8(sci_rx_buf[1], sci_rx_buf[2]));
201 send_string("510 Unknown chime duration." CRLF);
204 send_string("500 Chime started." CRLF);
209 if (check_standalone()) return;
210 if (sci_rx_buf[1] == '\0')
212 else if (sci_rx_buf[1] == 'S') { /* synchronous beep */
213 if (sci_rx_buf[2] == '\0')
215 else if (sci_rx_buf[3] != '\0' && sci_rx_buf[4] == '\0')
216 unchime_for(hex2u8(sci_rx_buf[2], sci_rx_buf[3]));
218 send_string("511 Unknown silence duration." CRLF);
221 while (unchime_count); /* spin */
222 send_string("501 Silenced." CRLF);
224 } else if (sci_rx_buf[2] != '\0' && sci_rx_buf[3] == '\0')
225 unchime_for(hex2u8(sci_rx_buf[1], sci_rx_buf[2]));
227 send_string("511 Unknown silence duration." CRLF);
230 send_string("500 Silence started." CRLF);
234 void print_switches(u8 prompted) {
239 send_string(u82hex(misc_input));
241 send_string(u82hex(switch_input));
246 /* make sure it's really a ping */
247 if (!my_strncmp("ING", (char*)sci_rx_buf+1, 3)) {
251 /* respond with ack & pong */
252 send_string("000 PONG!" CRLF);
257 send_string(u82hex(mic_challenge >> 8));
258 send_string(u82hex(mic_challenge & 0xff));
260 send_string(is_standalone()?"% ":"# ");
264 if (!my_strncmp("BOUT", (char*)sci_rx_buf+1, 4)) {
269 "-----------------------------------------------------------------" CRLF
271 "-----------------------------------------------------------------" CRLF
272 " Revision " VERSION_STRING CRLF
274 " This snack machine was brought to you by " CRLF
275 " Bernard Blackham" CRLF
277 " Harry McNally" CRLF
278 " Michal Gornisiewicz" CRLF
281 " Another UCC project in action. http://www.ucc.asn.au/" CRLF
286 if (my_strncmp("CHO ON", (char*)sci_rx_buf+1, 6))
288 else if (my_strncmp("CHO OFF", (char*)sci_rx_buf+1, 7))
295 if (!my_strncmp("OO", (char*)sci_rx_buf+1, 2)) {
304 " /___________/___/|" CRLF
306 " | ==\\ /== | |" CRLF
307 " | O O | \\ \\ |" CRLF
310 " / | \\_____/ | / /" CRLF
312 "/||\\| | /||\\/" CRLF
313 " -------------| " CRLF
317 " ... Where's the cheese?" CRLF
323 "Valid commands are:" CRLF
324 " ABOUT ROM information" CRLF
326 " ECHO {ON|OFF} turn echo on or off" CRLF
327 " Vnn vend an item" CRLF
328 " VALL vend all items" CRLF
329 " DXXXXXXXXXX show a message on the display" CRLF
330 " B[nn] beep for a duration nn (optional)" CRLF
331 " S[...] query all internal switch states" CRLF
332 " H[...] this help screen" CRLF
333 " GETROM download the ROM source code using xmodem" CRLF
334 "Comments start with a #" CRLF
338 extern const char _rom_src_data[];
339 extern const u16 _rom_src_len;
341 if (!my_strncmp("ETROM", (char*)sci_rx_buf+1, 5)) {
346 send_string("Writing to serial port (maybe). Size is 0x");
347 send_string(u82hex(_rom_src_len >> 8));
348 send_string(u82hex(_rom_src_len & 0xff));
350 send_string(u82hex((u16)(&_rom_src_data) >> 8));
351 send_string(u82hex((u16)(&_rom_src_data) & 0xff));
352 send_string(" with signature ");
353 s[0] = _rom_src_data[0];
354 s[1] = _rom_src_data[1];
355 s[2] = _rom_src_data[2];
358 send_string(CRLF " Type YES to download rom.tar.bz2 via XMODEM: ");
360 while (!sci_have_packet); /* spin */
361 if (!my_strncmp("YES", (char*)sci_rx_buf, 3)) {
362 send_string(CRLF "Transfer cancelled." CRLF);
367 sci_doing_xmodem = 1;
368 if (!xmodem_init_xfer()) {
369 sci_doing_xmodem = 0;
370 send_string("XMODEM init failed. Nobody's listening :(" CRLF);
373 char *p = (char*)_rom_src_data;
374 char *end = (char*)_rom_src_data+_rom_src_len;
378 /* send partial packet */
379 if (!xmodem_send_packet((char*)p, end-p)) aborted = 1;
381 } if ((u16)p == 0xb600) {
382 /* we have an eeprom here. skip it. */
385 } else if (!xmodem_send_packet((char*)p, 128)) {
392 xmodem_finish_xfer();
393 sci_doing_xmodem = 0;
395 send_string(CRLF "Transfer aborted." CRLF);
397 send_string(CRLF "Transfer complete." CRLF);
401 if (my_strncmp("UIT", (char*)sci_rx_buf+1, 3))
402 send_string("013 You can't quit you doofus." CRLF);
408 //u8 sha1_digest[SHA1_SIGNATURE_SIZE];
412 for (i = 0; i < 11; i++)
413 display_buf[i] = ' ';
414 display_buf[10] = '\0';
416 changer_output = 0x7f;
417 _io_ports[M6811_PORTA] = 0xc0; /* display on. talking to serial port */
418 _io_ports[M6811_DDRA] = 0xfc;
419 _io_ports[M6811_DDRD] = 0x3e;
420 _io_ports[M6811_SPCR] = M6811_MSTR | M6811_SPR1;
421 set_misc_output(0x00);
427 unlock(); /* enable interrupts */
444 send_string("5N4X0RZ R US" CRLF);
447 mic_set_secret("ABCDEFGH12345678");
449 last_standalone = is_standalone();
456 last_switch_input = switch_input;
457 last_misc_input = misc_input;
460 if (cur_motor[0] == 0xff) { /* signal to say redraw screen */
461 set_msg("*5N4X0RZ* ");
465 if (door_open() != last_door_open) {
466 last_door_open = door_open();
467 send_door_msg(last_door_open);
470 set_msg(last_door_open?"DOOR OPEN ":"DOOR CLOSE");
473 if (last_standalone != is_standalone()) {
474 /* somebody flicked the standalone switch */
478 last_standalone = is_standalone();
481 if (last_misc_input != misc_input) {
483 last_misc_input = misc_input;
486 if (last_switch_input != switch_input) {
488 last_switch_input = switch_input;
491 if (sci_have_packet) {
492 switch (sci_rx_buf[0]) {
504 dispense_something();
543 if (keypad_pressed()) {
544 if (is_standalone()) {
545 if (last_key == KEY_RESET) {
550 cur_motor[1] = last_key%10;
551 display_buf[1] = cur_motor[1]+'0';
552 set_msg(display_buf);
554 motor_num = cur_motor[0]%10;
556 motor_num += cur_motor[1];
557 switch (dispense_motor(motor_num)) {
558 case MOTOR_HOME_FAIL:
559 set_msg(" HOME FAIL ");
561 case MOTOR_CURRENT_FAIL:
562 set_msg(" OVER CRNT ");
565 set_msg("THANK YOU");
568 set_msg(" NO MOTOR ");
571 set_msg("ERRRRRRRR?");
575 display_buf[0] = ' ';
576 display_buf[1] = ' ';
580 cur_motor[0] = last_key;
581 display_buf[0] = (last_key%10)+'0';
582 set_msg(display_buf);
586 send_keypress(last_key);
590 if (coin_value != last_coin_value) {
592 last_coin_value = coin_value;