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;
67 if (my_strncmp("ALL", (char*)sci_rx_buf+1, 3)) {
70 send_string("102 Vend all motors starting." CRLF);
71 for (motor[0] = '0'; motor[0] <= '9'; motor[0]++) {
72 for (motor[1] = '0'; motor[1] <= '9'; motor[1]++) {
73 send_string("101 Vending ");
76 motor_reply(dispense_motor(motor[0]*10+motor[1]));
79 send_string("102 Vend all motors complete." CRLF);
83 if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') ||
84 (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9')) {
85 sci_rx_buf[1] = sci_rx_buf[2] = '0';
86 motor_reply(MOTOR_NOSLOT);
90 slot = (sci_rx_buf[1] - '0') * 10;
91 slot += sci_rx_buf[2] - '0';
93 motor_reply(dispense_motor(slot));
96 void write_to_display() {
97 /* process a message in the form DXXXXXXXXXX to send to display */
101 if (check_standalone()) return;
103 for (i = 0; i < 10; i++)
105 buf[i] = sci_rx_buf[i+1];
109 for (; i < 10; i++) /* pad the rest out with spaces */
114 send_string("300 Written." CRLF);
117 void send_balance() {
119 sci_tx_buf[1] = have_change?'0':'1';
120 sci_tx_buf[2] = (coin_value/10000)%10;
121 sci_tx_buf[3] = (coin_value/1000)%10;
122 sci_tx_buf[4] = (coin_value/100)%10;
123 sci_tx_buf[5] = (coin_value/10)%10;
124 sci_tx_buf[6] = coin_value%10;
132 if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') ||
133 (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9') ||
134 (sci_rx_buf[3] < '0') || (sci_rx_buf[3] > '9') ||
135 (sci_rx_buf[4] < '0') || (sci_rx_buf[4] > '9') ||
136 (sci_rx_buf[5] < '0') || (sci_rx_buf[5] > '9')) {
139 cost = sci_rx_buf[1] - '0';
140 cost *= 10; cost = sci_rx_buf[2] - '0';
141 cost *= 10; cost = sci_rx_buf[3] - '0';
142 cost *= 10; cost = sci_rx_buf[4] - '0';
143 cost *= 10; cost = sci_rx_buf[5] - '0';
149 void send_keypress(u8 key) {
150 /* send a packet of the form KX with X being the key, or R for reset */
151 if (is_standalone()) return;
154 if (key == KEY_RESET) {
159 sci_tx_buf[2] = (key%10)+'0';
161 sci_tx_buf[3] = '\0';
163 send_string(" key." CRLF);
166 void send_door_msg(bool open) {
167 if (is_standalone()) return;
170 sci_tx_buf[2] = open?'1':'0';
173 send_string(" door open." CRLF);
175 send_string(" door closed." CRLF);
178 u8 hexchar2u8(char b) {
179 if (b >= '0' && b <= '9') return b-'0';
180 if (b >= 'a' && b <= 'f') return b-'a'+0x0a;
181 if (b >= 'A' && b <= 'F') return b-'A'+0x0a;
185 char nibble2hexchar(u8 b) {
186 if (b <= 9) return b+'0';
187 if (b >= 10 && b <= 15) return b+'A'-10;
191 u8 hex2u8(char msb, char lsb) {
192 return (hexchar2u8(msb) << 4) + hexchar2u8(lsb);
195 static char hexconv_buf[3];
197 hexconv_buf[0] = nibble2hexchar((a&0xf0) >> 4);
198 hexconv_buf[1] = nibble2hexchar(a&0x0f);
199 hexconv_buf[2] = '\0';
204 if (check_standalone()) return;
205 if (sci_rx_buf[1] == '\0')
207 else if (sci_rx_buf[2] != '\0' && sci_rx_buf[3] == '\0')
208 chime_for(hex2u8(sci_rx_buf[1], sci_rx_buf[2]));
210 send_string("510 Unknown chime duration." CRLF);
213 send_string("500 Chimed." CRLF);
216 void print_switches(u8 prompted) {
221 send_string(u82hex(misc_input));
223 send_string(u82hex(switch_input));
228 /* make sure it's really a ping */
229 if (!my_strncmp("ING", (char*)sci_rx_buf+1, 3)) {
233 /* respond with ack & pong */
234 send_string("000 PONG!" CRLF);
238 send_string(is_standalone()?"$ ":"# ");
242 if (!my_strncmp("BOUT", (char*)sci_rx_buf+1, 4)) {
247 "-----------------------------------------------------------------" CRLF
249 "-----------------------------------------------------------------" CRLF
250 " Revision " VERSION_STRING CRLF
252 " This snack machine was brought to you by " CRLF
253 " Bernard Blackham" CRLF
255 " Harry McNally" CRLF
256 " Michal Gornisiewicz" CRLF
259 " Another UCC project in action. http://www.ucc.asn.au/" CRLF
264 if (my_strncmp("CHO ON", (char*)sci_rx_buf+1, 6))
266 else if (my_strncmp("CHO OFF", (char*)sci_rx_buf+1, 7))
273 if (!my_strncmp("OO", (char*)sci_rx_buf+1, 2)) {
282 " /___________/___/|" CRLF
284 " | ==\\ /== | |" CRLF
285 " | O O | \\ \\ |" CRLF
288 " / | \\_____/ | / /" CRLF
290 "/||\\| | /||\\/" CRLF
291 " -------------| " CRLF
295 " ... Where's the cheese?" CRLF
301 "Valid commands are:" CRLF
302 " ABOUT ROM information" CRLF
304 " ECHO {ON|OFF} turn echo on or off" CRLF
305 " Vnn vend an item" CRLF
306 " VALL vend all items" CRLF
307 " DXXXXXXXXXX show a message on the display" CRLF
308 " B[nn] beep for a duration nn (optional)" CRLF
309 " S[...] query all internal switch states" CRLF
310 " H[...] this help screen" CRLF
311 " GETROM download the ROM source code using xmodem" CRLF
312 "Comments start with a #" CRLF
316 extern const char _rom_src_data[];
317 extern const u16 _rom_src_len;
319 if (!my_strncmp("ETROM", (char*)sci_rx_buf+1, 5)) {
324 send_string("Writing to serial port (maybe). Size is 0x");
325 send_string(u82hex(_rom_src_len >> 8));
326 send_string(u82hex(_rom_src_len & 0xff));
328 send_string(u82hex((u16)(&_rom_src_data) >> 8));
329 send_string(u82hex((u16)(&_rom_src_data) & 0xff));
330 send_string(" with signature ");
331 s[0] = _rom_src_data[0];
332 s[1] = _rom_src_data[1];
333 s[2] = _rom_src_data[2];
336 send_string(CRLF " Type YES to download rom.tar.bz2 via XMODEM: ");
338 while (!sci_have_packet); /* spin */
339 if (!my_strncmp("YES", (char*)sci_rx_buf, 3)) {
340 send_string(CRLF "Transfer cancelled." CRLF);
345 sci_doing_xmodem = 1;
346 if (!xmodem_init_xfer()) {
347 sci_doing_xmodem = 0;
348 send_string("XMODEM init failed. Nobody's listening :(" CRLF);
351 char *p = (char*)_rom_src_data;
352 char *end = (char*)_rom_src_data+_rom_src_len;
356 /* send partial packet */
357 if (!xmodem_send_packet((char*)p, end-p)) aborted = 1;
359 } if ((u16)p == 0xb600) {
360 /* we have an eeprom here. skip it. */
363 } else if (!xmodem_send_packet((char*)p, 128)) {
370 xmodem_finish_xfer();
371 sci_doing_xmodem = 0;
373 send_string(CRLF "Transfer aborted." CRLF);
375 send_string(CRLF "Transfer complete." CRLF);
379 if (my_strncmp("UIT", (char*)sci_rx_buf+1, 3))
380 send_string("013 You can't quit you doofus." CRLF);
386 //u8 sha1_digest[SHA1_SIGNATURE_SIZE];
390 for (i = 0; i < 11; i++)
391 display_buf[i] = ' ';
392 display_buf[10] = '\0';
394 changer_output = 0x7f;
395 _io_ports[M6811_PORTA] = 0xc0; /* display on. talking to serial port */
396 _io_ports[M6811_DDRA] = 0xfc;
397 _io_ports[M6811_DDRD] = 0x3e;
398 _io_ports[M6811_SPCR] = M6811_MSTR | M6811_SPR1;
399 set_misc_output(0x00);
405 unlock(); /* enable interrupts */
422 send_string("5N4X0RZ R US" CRLF);
424 last_standalone = is_standalone();
431 last_switch_input = switch_input;
432 last_misc_input = misc_input;
435 if (cur_motor[0] == 0xff) { /* signal to say redraw screen */
436 set_msg("*5N4X0RZ* ");
440 if (door_open() != last_door_open) {
441 last_door_open = door_open();
442 send_door_msg(last_door_open);
445 set_msg(last_door_open?"DOOR OPEN ":"DOOR CLOSE");
448 if (last_standalone != is_standalone()) {
449 /* somebody flicked the standalone switch */
453 last_standalone = is_standalone();
456 if (last_misc_input != misc_input) {
458 last_misc_input = misc_input;
461 if (last_switch_input != switch_input) {
463 last_switch_input = switch_input;
466 if (sci_have_packet) {
469 //SHA1_Update(&ctx, sci_rx_buf, my_strlen(sci_rx_buf));
470 //SHA1_Final(sha1_digest, &ctx);
472 switch (sci_rx_buf[0]) {
484 dispense_something();
520 if (keypad_pressed()) {
521 if (is_standalone()) {
522 if (last_key == KEY_RESET) {
527 cur_motor[1] = last_key%10;
528 display_buf[1] = cur_motor[1]+'0';
529 set_msg(display_buf);
531 motor_num = cur_motor[0]%10;
533 motor_num += cur_motor[1];
534 switch (dispense_motor(motor_num)) {
535 case MOTOR_HOME_FAIL:
536 set_msg(" HOME FAIL ");
538 case MOTOR_CURRENT_FAIL:
539 set_msg(" OVER CRNT ");
542 set_msg("THANK YOU");
545 set_msg(" NO MOTOR ");
548 set_msg("ERRRRRRRR?");
552 display_buf[0] = ' ';
553 display_buf[1] = ' ';
557 cur_motor[0] = last_key;
558 display_buf[0] = (last_key%10)+'0';
559 set_msg(display_buf);
563 send_keypress(last_key);
567 if (coin_value != last_coin_value) {
569 last_coin_value = coin_value;