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 "V 20040624"
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 bool check_badpoke() {
39 send_string("099 Can't poke without flipping DIP SW 3." CRLF);
45 void unknown_command() {
46 send_string("012 Unknown command. Type HELP for help." CRLF);
49 void motor_reply(u8 code) {
50 /* returns a message of the form MXYY - X is return code, YY is motor */
53 send_string("100 Vend successful." CRLF);
56 send_string("151 No motor there." CRLF);
58 case MOTOR_CURRENT_FAIL:
59 send_string("152 Over current." CRLF);
62 send_string("153 Home sensors failing." CRLF);
65 send_string("159 Unknown motor error." CRLF);
69 void dispense_something() {
70 /* process a message VXX in sci_rx_buf where XX is motor number */
73 if (check_standalone()) return;
74 if (must_verify() && !mic_verify((void*)sci_rx_buf)) {
75 send_string("019 Message verification failed." CRLF);
79 if (my_strncmp("ALL", (char*)sci_rx_buf+1, 3)) {
82 send_string("102 Vend all motors starting." CRLF);
83 for (motor[0] = '0'; motor[0] <= '9'; motor[0]++) {
84 for (motor[1] = '0'; motor[1] <= '9'; motor[1]++) {
85 send_string("101 Vending ");
88 motor_reply(dispense_motor((motor[0]-'0')*10+(motor[1]-'0')));
91 send_string("102 Vend all motors complete." CRLF);
95 if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') ||
96 (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9')) {
97 sci_rx_buf[1] = sci_rx_buf[2] = '0';
98 motor_reply(MOTOR_NOSLOT);
102 slot = (sci_rx_buf[1] - '0') * 10;
103 slot += sci_rx_buf[2] - '0';
105 motor_reply(dispense_motor(slot));
108 void write_to_display() {
109 /* process a message in the form DXXXXXXXXXX to send to display */
113 if (check_standalone()) return;
115 for (i = 0; i < 10; i++)
117 buf[i] = sci_rx_buf[i+1];
121 for (; i < 10; i++) /* pad the rest out with spaces */
126 send_string("300 Written." CRLF);
129 void send_balance() {
131 sci_tx_buf[1] = have_change?'0':'1';
132 sci_tx_buf[2] = (coin_value/10000)%10;
133 sci_tx_buf[3] = (coin_value/1000)%10;
134 sci_tx_buf[4] = (coin_value/100)%10;
135 sci_tx_buf[5] = (coin_value/10)%10;
136 sci_tx_buf[6] = coin_value%10;
144 if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') ||
145 (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9') ||
146 (sci_rx_buf[3] < '0') || (sci_rx_buf[3] > '9') ||
147 (sci_rx_buf[4] < '0') || (sci_rx_buf[4] > '9') ||
148 (sci_rx_buf[5] < '0') || (sci_rx_buf[5] > '9')) {
151 cost = sci_rx_buf[1] - '0';
152 cost *= 10; cost = sci_rx_buf[2] - '0';
153 cost *= 10; cost = sci_rx_buf[3] - '0';
154 cost *= 10; cost = sci_rx_buf[4] - '0';
155 cost *= 10; cost = sci_rx_buf[5] - '0';
161 void send_keypress(u8 key) {
162 /* send a packet of the form KX with X being the key, or R for reset */
163 if (is_standalone()) return;
166 if (key == KEY_RESET) {
171 sci_tx_buf[2] = (key%10)+'0';
173 sci_tx_buf[3] = '\0';
175 send_string(" key." CRLF);
178 void send_door_msg(bool open) {
179 if (is_standalone()) return;
182 sci_tx_buf[2] = open?'1':'0';
185 send_string(" door open." CRLF);
187 send_string(" door closed." CRLF);
191 if (check_standalone()) return;
192 if (sci_rx_buf[1] == '\0')
194 else if (sci_rx_buf[1] == 'S') { /* synchronous beep */
195 if (sci_rx_buf[2] == '\0')
197 else if (sci_rx_buf[3] != '\0' && sci_rx_buf[4] == '\0')
198 chime_for(hex2u8(sci_rx_buf[2], sci_rx_buf[3]));
200 send_string("510 Unknown chime duration." CRLF);
203 while (chime_count); /* spin */
204 send_string("500 Chimed." CRLF);
206 } else if (sci_rx_buf[2] != '\0' && sci_rx_buf[3] == '\0')
207 chime_for(hex2u8(sci_rx_buf[1], sci_rx_buf[2]));
209 send_string("510 Unknown chime duration." CRLF);
212 send_string("500 Chime started." CRLF);
217 if (check_standalone()) return;
218 if (sci_rx_buf[1] == '\0')
220 else if (sci_rx_buf[1] == 'S') { /* synchronous beep */
221 if (sci_rx_buf[2] == '\0')
223 else if (sci_rx_buf[3] != '\0' && sci_rx_buf[4] == '\0')
224 unchime_for(hex2u8(sci_rx_buf[2], sci_rx_buf[3]));
226 send_string("511 Unknown silence duration." CRLF);
229 while (unchime_count); /* spin */
230 send_string("501 Silenced." CRLF);
232 } else if (sci_rx_buf[2] != '\0' && sci_rx_buf[3] == '\0')
233 unchime_for(hex2u8(sci_rx_buf[1], sci_rx_buf[2]));
235 send_string("511 Unknown silence duration." CRLF);
238 send_string("500 Silence started." CRLF);
242 void print_switches(u8 prompted) {
247 send_string(u82hex(misc_input));
249 send_string(u82hex(switch_input));
254 /* make sure it's really a ping */
255 if (!my_strncmp("ING", (char*)sci_rx_buf+1, 3)) {
259 /* respond with ack & pong */
260 send_string("000 PONG!" CRLF);
263 u16 hex2addr(char* addrptr) {
265 v = hex2u8(addrptr[0], addrptr[1]) << 8;
266 v |= hex2u8(addrptr[2], addrptr[3]);
271 if (!my_strncmp("EEK", (char*)sci_rx_buf+1, 3)) {
275 if (check_badpoke()) return;
276 if (ishex(sci_rx_buf[4]) && ishex(sci_rx_buf[5]) && ishex(sci_rx_buf[6]) &&
277 ishex(sci_rx_buf[7]) && sci_rx_buf[8] == '\0') {
278 u16 v = hex2addr((char*)(sci_rx_buf+4));
281 send_string(u82hex(v));
285 send_string("091 Invalid location given." CRLF);
289 if (!my_strncmp("OKE", (char*)sci_rx_buf+1, 3)) {
293 if (check_badpoke()) return;
294 if (ishex(sci_rx_buf[4]) && ishex(sci_rx_buf[5]) && ishex(sci_rx_buf[6]) &&
295 ishex(sci_rx_buf[7]) && ishex(sci_rx_buf[8]) && ishex(sci_rx_buf[9])
296 && sci_rx_buf[10] == '\0') {
298 v = hex2addr((char*)(sci_rx_buf+4));
299 *(u8*)v = hex2u8(sci_rx_buf[8], sci_rx_buf[9]);
300 send_string("080 Written." CRLF);
303 send_string("081 Invalid location or byte given." CRLF);
307 if (!my_strncmp("UMP", (char*)sci_rx_buf+1, 3)) {
311 if (check_badpoke()) return;
312 if (ishex(sci_rx_buf[4]) && ishex(sci_rx_buf[5]) && ishex(sci_rx_buf[6]) &&
313 ishex(sci_rx_buf[7]) && sci_rx_buf[8] == '\0') {
314 u16 v = hex2addr((char*)(sci_rx_buf+4));
315 send_string("070 Jumping now." CRLF);
316 asm volatile ("jsr %0" : : "m"(*(u16*)v) : "d");
317 send_string("071 And back." CRLF);
320 send_string("079 Invalid location given." CRLF);
323 void do_set_password() {
326 if (check_badpoke()) return;
327 for (i=1; i < 17; i++) {
328 if (sci_rx_buf[i] == '\0') {
333 if (good && sci_rx_buf[17] != '\0') good = 0;
335 send_string("061 Password must be exactly 16 characters." CRLF);
339 mic_set_secret((char*)(sci_rx_buf+1));
340 send_string("060 Password has been set." CRLF);
345 send_string(u82hex(mic_challenge >> 8));
346 send_string(u82hex(mic_challenge & 0xff));
348 send_string(is_standalone()?"% ":"# ");
352 if (!my_strncmp("BOUT", (char*)sci_rx_buf+1, 4)) {
357 "-----------------------------------------------------------------" CRLF
359 "-----------------------------------------------------------------" CRLF
360 " Revision " VERSION_STRING CRLF
362 " This snack machine was brought to you by " CRLF
363 " Bernard Blackham" CRLF
365 " Harry McNally" CRLF
366 " Michal Gornisiewicz" CRLF
369 " Another UCC project in action. http://www.ucc.asn.au/" CRLF
374 if (my_strncmp("CHO ON", (char*)sci_rx_buf+1, 6))
376 else if (my_strncmp("CHO OFF", (char*)sci_rx_buf+1, 7))
383 if (!my_strncmp("OO", (char*)sci_rx_buf+1, 2)) {
392 " /___________/___/|" CRLF
394 " | ==\\ /== | |" CRLF
395 " | O O | \\ \\ |" CRLF
398 " / | \\_____/ | / /" CRLF
400 "/||\\| | /||\\/" CRLF
401 " -------------| " CRLF
405 " ... Where's the cheese?" CRLF
411 "Valid commands are:" CRLF
412 " ABOUT ROM information" CRLF
413 " B[S][nn] beep [synchronously] for a duration nn (optional)" CRLF
414 " C[S][nn] silence [synchronously] for a duration nn (optional)" CRLF
415 " Dxxxxxxxxxx show a message on the display" CRLF
416 " ECHO {ON|OFF} turn echo on or off" CRLF
417 " GETROM download the ROM source code using xmodem" CRLF
418 " H[...] this help screen" CRLF
419 "*JUMPxxxx jumps to a subroute at location xxxx" CRLF
420 "*PEEKxxxx returns the value of the byte at location xxxx" CRLF
421 "*POKExxxxyy sets the value of location xxxx to yy" CRLF
423 " S[...] query all internal switch states" CRLF
424 "+Vnn vend an item" CRLF
425 "+VALL vend all items" CRLF
426 "*Wxxxxxxxxxxxx set a new password for authenticated vends. xxx=16 chars" CRLF
427 " password will be converted to uppercase" CRLF
429 "Very few functions are available when the machine is in standalone " CRLF
430 "mode (DIP SW 1 is set)" CRLF
431 "+ denotes that this item requires authentication if DIP SW 2 is set" CRLF
432 "* denotes that DIP SW 3 must be set to use these" CRLF
433 "Commands starting with # are ignored (comments)" CRLF
437 extern const char _rom_src_data[];
438 extern const u16 _rom_src_len;
440 if (!my_strncmp("ETROM", (char*)sci_rx_buf+1, 5)) {
445 send_string("Writing to serial port (maybe). Size is 0x");
446 send_string(u82hex(_rom_src_len >> 8));
447 send_string(u82hex(_rom_src_len & 0xff));
449 send_string(u82hex((u16)(&_rom_src_data) >> 8));
450 send_string(u82hex((u16)(&_rom_src_data) & 0xff));
451 send_string(" with signature ");
452 s[0] = _rom_src_data[0];
453 s[1] = _rom_src_data[1];
454 s[2] = _rom_src_data[2];
457 send_string(CRLF " Type YES to download rom.tar.bz2 via XMODEM: ");
459 while (!sci_have_packet); /* spin */
460 if (!my_strncmp("YES", (char*)sci_rx_buf, 3)) {
461 send_string(CRLF "Transfer cancelled." CRLF);
466 sci_doing_xmodem = 1;
467 if (!xmodem_init_xfer()) {
468 sci_doing_xmodem = 0;
469 send_string("XMODEM init failed. Nobody's listening :(" CRLF);
472 char *p = (char*)_rom_src_data;
473 char *end = (char*)_rom_src_data+_rom_src_len;
477 /* send partial packet */
478 if (!xmodem_send_packet((char*)p, end-p)) aborted = 1;
480 } if ((u16)p == 0xb600) {
481 /* we have an eeprom here. skip it. */
484 } else if (!xmodem_send_packet((char*)p, 128)) {
491 xmodem_finish_xfer();
492 sci_doing_xmodem = 0;
494 send_string(CRLF "Transfer aborted." CRLF);
496 send_string(CRLF "Transfer complete." CRLF);
500 if (my_strncmp("UIT", (char*)sci_rx_buf+1, 3))
501 send_string("013 You can't quit you doofus." CRLF);
508 for (i = 0; i < 11; i++)
509 display_buf[i] = ' ';
510 display_buf[10] = '\0';
512 changer_output = 0x7f;
513 _io_ports[M6811_PORTA] = 0xc0; /* display on. talking to serial port */
514 _io_ports[M6811_DDRA] = 0xfc;
515 _io_ports[M6811_DDRD] = 0x3e;
516 _io_ports[M6811_SPCR] = M6811_MSTR | M6811_SPR1;
517 set_misc_output(0x00);
523 unlock(); /* enable interrupts */
540 send_string("5N4X0RZ R US" CRLF);
542 last_standalone = is_standalone();
549 last_switch_input = switch_input;
550 last_misc_input = misc_input;
553 if (cur_motor[0] == 0xff) { /* signal to say redraw screen */
554 set_msg("*5N4X0RZ* ");
558 if (door_open() != last_door_open) {
559 last_door_open = door_open();
560 send_door_msg(last_door_open);
563 set_msg(last_door_open?"DOOR OPEN ":"DOOR CLOSE");
566 if (last_standalone != is_standalone()) {
567 /* somebody flicked the standalone switch */
571 last_standalone = is_standalone();
574 if (last_misc_input != misc_input) {
576 last_misc_input = misc_input;
579 if (last_switch_input != switch_input) {
581 last_switch_input = switch_input;
584 if (sci_have_packet) {
586 switch (sci_rx_buf[0]) {
616 if (sci_rx_buf[1] == 'I')
618 else if (sci_rx_buf[1] == 'O')
620 else if (sci_rx_buf[1] == 'E')
633 dispense_something();
648 if (keypad_pressed()) {
649 if (is_standalone()) {
650 if (last_key == KEY_RESET) {
655 cur_motor[1] = last_key%10;
656 display_buf[1] = cur_motor[1]+'0';
657 set_msg(display_buf);
659 motor_num = cur_motor[0]%10;
661 motor_num += cur_motor[1];
662 switch (dispense_motor(motor_num)) {
663 case MOTOR_HOME_FAIL:
664 set_msg(" HOME FAIL ");
666 case MOTOR_CURRENT_FAIL:
667 set_msg(" OVER CRNT ");
670 set_msg("THANK YOU");
673 set_msg(" NO MOTOR ");
676 set_msg("ERRRRRRRR?");
680 display_buf[0] = ' ';
681 display_buf[1] = ' ';
685 cur_motor[0] = last_key;
686 display_buf[0] = (last_key%10)+'0';
687 set_msg(display_buf);
691 send_keypress(last_key);
695 if (coin_value != last_coin_value) {
697 last_coin_value = coin_value;