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"
24 /* cur_motor[0] is 0 for nothing, or 1..10, or 0xff to say redraw.
25 * cur_motor[1] is 0..9 and only meaningful is cur_motor[0] != 0. */
28 bool check_standalone() {
29 if (is_standalone()) {
30 send_string("011 In standalone mode. Function disabled." CRLF);
36 void unknown_command() {
37 send_string("012 Unknown command. Type HELP for help." CRLF);
40 void motor_reply(u8 code) {
41 /* returns a message of the form MXYY - X is return code, YY is motor */
44 send_string("100 Vend successful." CRLF);
47 send_string("151 No motor there." CRLF);
49 case MOTOR_CURRENT_FAIL:
50 send_string("152 Over current." CRLF);
53 send_string("153 Home sensors failing." CRLF);
56 send_string("159 Unknown motor error." CRLF);
60 void dispense_something() {
61 /* process a message VXX in sci_rx_buf where XX is motor number */
64 if (check_standalone()) return;
66 if (my_strncmp("ALL", (char*)sci_rx_buf+1, 3)) {
69 send_string("102 Vend all motors starting." CRLF);
70 for (motor[0] = '0'; motor[0] <= '9'; motor[0]++) {
71 for (motor[1] = '0'; motor[1] <= '9'; motor[1]++) {
72 send_string("101 Vending ");
75 motor_reply(dispense_motor(motor[0]*10+motor[1]));
78 send_string("102 Vend all motors complete." CRLF);
82 if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') ||
83 (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9')) {
84 sci_rx_buf[1] = sci_rx_buf[2] = '0';
85 motor_reply(MOTOR_NOSLOT);
89 slot = (sci_rx_buf[1] - '0') * 10;
90 slot += sci_rx_buf[2] - '0';
92 motor_reply(dispense_motor(slot));
95 void write_to_display() {
96 /* process a message in the form DXXXXXXXXXX to send to display */
100 if (check_standalone()) return;
102 for (i = 0; i < 10; i++)
104 buf[i] = sci_rx_buf[i+1];
108 for (; i < 10; i++) /* pad the rest out with spaces */
113 send_string("300 Written." CRLF);
116 void send_balance() {
118 sci_tx_buf[1] = have_change?'0':'1';
119 sci_tx_buf[2] = (coin_value/10000)%10;
120 sci_tx_buf[3] = (coin_value/1000)%10;
121 sci_tx_buf[4] = (coin_value/100)%10;
122 sci_tx_buf[5] = (coin_value/10)%10;
123 sci_tx_buf[6] = coin_value%10;
131 if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') ||
132 (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9') ||
133 (sci_rx_buf[3] < '0') || (sci_rx_buf[3] > '9') ||
134 (sci_rx_buf[4] < '0') || (sci_rx_buf[4] > '9') ||
135 (sci_rx_buf[5] < '0') || (sci_rx_buf[5] > '9')) {
138 cost = sci_rx_buf[1] - '0';
139 cost *= 10; cost = sci_rx_buf[2] - '0';
140 cost *= 10; cost = sci_rx_buf[3] - '0';
141 cost *= 10; cost = sci_rx_buf[4] - '0';
142 cost *= 10; cost = sci_rx_buf[5] - '0';
148 void send_keypress(u8 key) {
149 /* send a packet of the form KX with X being the key, or R for reset */
150 if (is_standalone()) return;
153 if (key == KEY_RESET) {
158 sci_tx_buf[2] = (key%10)+'0';
160 sci_tx_buf[3] = '\0';
162 send_string(" key." CRLF);
165 void send_door_msg(bool open) {
166 if (is_standalone()) return;
169 sci_tx_buf[2] = open?'1':'0';
172 send_string(" door open." CRLF);
174 send_string(" door closed." CRLF);
177 u8 hexchar2u8(char b) {
178 if (b >= '0' && b <= '9') return b-'0';
179 if (b >= 'a' && b <= 'f') return b-'a'+0x0a;
180 if (b >= 'A' && b <= 'F') return b-'A'+0x0a;
184 char nibble2hexchar(u8 b) {
185 if (b <= 9) return b+'0';
186 if (b >= 10 && b <= 15) return b+'A'-10;
190 u8 hex2u8(char msb, char lsb) {
191 return (hexchar2u8(msb) << 4) + hexchar2u8(lsb);
194 static char hexconv_buf[3];
196 hexconv_buf[0] = nibble2hexchar((a&0xf0) >> 4);
197 hexconv_buf[1] = nibble2hexchar(a&0x0f);
198 hexconv_buf[2] = '\0';
203 if (check_standalone()) return;
204 if (sci_rx_buf[1] == '\0')
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 Chimed." CRLF);
215 void print_switches(u8 prompted) {
220 send_string(u82hex(misc_input));
222 send_string(u82hex(switch_input));
227 /* make sure it's really a ping */
228 if (!my_strncmp("ING", (char*)sci_rx_buf+1, 3)) {
232 /* respond with ack & pong */
233 send_string("000 PONG!" CRLF);
237 send_string(is_standalone()?"$ ":"# ");
241 if (!my_strncmp("BOUT", (char*)sci_rx_buf+1, 4)) {
246 "-----------------------------------------------------------------" CRLF
248 "-----------------------------------------------------------------" CRLF
249 " Revision " VERSION_STRING CRLF
251 " This snack machine was brought to you by " CRLF
252 " Bernard Blackham" CRLF
254 " Harry McNally" CRLF
255 " Michal Gornisiewicz" CRLF
258 " Another UCC project in action. http://www.ucc.asn.au/" CRLF
263 if (my_strncmp("CHO ON", (char*)sci_rx_buf+1, 6))
265 else if (my_strncmp("CHO OFF", (char*)sci_rx_buf+1, 7))
272 if (!my_strncmp("OO", (char*)sci_rx_buf+1, 2)) {
281 " /___________/___/|" CRLF
283 " | ==\\ /== | |" CRLF
284 " | O O | \\ \\ |" CRLF
287 " / | \\_____/ | / /" CRLF
289 "/||\\| | /||\\/" CRLF
290 " -------------| " CRLF
294 " ... Where's the cheese?" CRLF
300 "Valid commands are:" CRLF
301 " ABOUT ROM information" CRLF
303 " ECHO {ON|OFF} turn echo on or off" CRLF
304 " Vnn vend an item" CRLF
305 " VALL vend all items" CRLF
306 " DXXXXXXXXXX show a message on the display" CRLF
307 " B[nn] beep for a duration nn (optional)" CRLF
308 " S[...] query all internal switch states" CRLF
309 " H[...] this help screen" CRLF
310 " GETROM download the ROM source code using xmodem" CRLF
311 "Comments start with a #" CRLF
315 extern const char _rom_src_data[];
316 extern const u16 _rom_src_len;
318 if (!my_strncmp("ETROM", (char*)sci_rx_buf+1, 5)) {
323 send_string("Writing to serial port (maybe). Size is 0x");
324 send_string(u82hex(_rom_src_len >> 8));
325 send_string(u82hex(_rom_src_len & 0xff));
326 send_string(" with signature ");
327 s[0] = _rom_src_data[0];
328 s[1] = _rom_src_data[1];
329 s[2] = _rom_src_data[2];
332 send_string(CRLF " Type YES to download via XMODEM: ");
334 while (!sci_have_packet); /* spin */
335 if (!my_strncmp("YES", (char*)sci_rx_buf, 3)) return;
338 sci_doing_xmodem = 1;
339 if (!xmodem_init_xfer()) {
340 sci_doing_xmodem = 0;
341 send_string("XMODEM init failed. Nobody's listening :(" CRLF);
344 char *p = (char*)_rom_src_data;
345 char *end = (char*)_rom_src_data+_rom_src_len;
348 if (!xmodem_send_packet((char*)p, 128)) {
354 /* send partial packet */
355 if (!xmodem_send_packet((char*)p, end-p)) aborted = 1;
360 xmodem_finish_xfer();
361 sci_doing_xmodem = 0;
363 send_string(CRLF "Transfer aborted." CRLF);
365 send_string(CRLF "Transfer complete." CRLF);
369 if (my_strncmp("UIT", (char*)sci_rx_buf+1, 3))
370 send_string("013 You can't quit you doofus." CRLF);
377 for (i = 0; i < 11; i++)
378 display_buf[i] = ' ';
379 display_buf[10] = '\0';
381 changer_output = 0x7f;
382 _io_ports[M6811_PORTA] = 0xc0; /* display on. talking to serial port */
383 _io_ports[M6811_DDRA] = 0xfc;
384 _io_ports[M6811_DDRD] = 0x3e;
385 _io_ports[M6811_SPCR] = M6811_MSTR | M6811_SPR1;
386 set_misc_output(0x00);
392 unlock(); /* enable interrupts */
410 send_string("5N4X0RZ R US" CRLF);
412 last_standalone = is_standalone();
419 last_switch_input = switch_input;
420 last_misc_input = misc_input;
423 if (cur_motor[0] == 0xff) { /* signal to say redraw screen */
424 set_msg("*5N4X0RZ* ");
428 if (door_open() != last_door_open) {
429 last_door_open = door_open();
430 send_door_msg(last_door_open);
432 set_msg(last_door_open?"DOOR OPEN ":"DOOR CLOSE");
435 if (last_standalone != is_standalone()) {
436 /* somebody flicked the standalone switch */
440 last_standalone = is_standalone();
443 if (last_misc_input != misc_input) {
445 last_misc_input = misc_input;
448 if (last_switch_input != switch_input) {
450 last_switch_input = switch_input;
453 if (sci_have_packet) {
454 switch (sci_rx_buf[0]) {
466 dispense_something();
502 if (keypad_pressed()) {
503 if (is_standalone()) {
504 if (last_key == KEY_RESET) {
509 cur_motor[1] = last_key%10;
510 display_buf[1] = cur_motor[1]+'0';
511 set_msg(display_buf);
513 motor_num = cur_motor[0]%10;
515 motor_num += cur_motor[1];
516 switch (dispense_motor(motor_num)) {
517 case MOTOR_HOME_FAIL:
518 set_msg(" HOME FAIL ");
520 case MOTOR_CURRENT_FAIL:
521 set_msg(" OVER CRNT ");
524 set_msg("THANK YOU");
527 set_msg(" NO MOTOR ");
530 set_msg("ERRRRRRRR?");
534 display_buf[0] = ' ';
535 display_buf[1] = ' ';
539 cur_motor[0] = last_key;
540 display_buf[0] = (last_key%10)+'0';
541 set_msg(display_buf);
545 send_keypress(last_key);
549 if (coin_value != last_coin_value) {
551 last_coin_value = coin_value;