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"
23 /* cur_motor[0] is 0 for nothing, or 1..10, or 0xff to say redraw.
24 * cur_motor[1] is 0..9 and only meaningful is cur_motor[0] != 0. */
27 bool check_standalone() {
28 if (is_standalone()) {
29 send_string("011 In standalone mode. Function disabled." CRLF);
35 void unknown_command() {
36 send_string("012 Unknown command. Type HELP for help." CRLF);
39 void motor_reply(u8 code) {
40 /* returns a message of the form MXYY - X is return code, YY is motor */
43 send_string("100 Vend successful." CRLF);
46 send_string("151 No motor there." CRLF);
48 case MOTOR_CURRENT_FAIL:
49 send_string("152 Over current." CRLF);
52 send_string("153 Home sensors failing." CRLF);
55 send_string("159 Unknown motor error." CRLF);
59 void dispense_something() {
60 /* process a message VXX in sci_rx_buf where XX is motor number */
63 if (check_standalone()) return;
65 if (my_strncmp("ALL", (char*)sci_rx_buf+1, 3)) {
68 send_string("102 Vend all motors starting." CRLF);
69 for (motor[0] = '0'; motor[0] <= '9'; motor[0]++) {
70 for (motor[1] = '0'; motor[1] <= '9'; motor[1]++) {
71 send_string("101 Vending ");
74 motor_reply(dispense_motor(motor[0]*10+motor[1]));
77 send_string("102 Vend all motors complete." CRLF);
81 if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') ||
82 (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9')) {
83 sci_rx_buf[1] = sci_rx_buf[2] = '0';
84 motor_reply(MOTOR_NOSLOT);
88 slot = (sci_rx_buf[1] - '0') * 10;
89 slot += sci_rx_buf[2] - '0';
91 motor_reply(dispense_motor(slot));
94 void write_to_display() {
95 /* process a message in the form DXXXXXXXXXX to send to display */
99 if (check_standalone()) return;
101 for (i = 0; i < 10; i++)
103 buf[i] = sci_rx_buf[i+1];
107 for (; i < 10; i++) /* pad the rest out with spaces */
112 send_string("300 Written." CRLF);
115 void send_balance() {
117 sci_tx_buf[1] = have_change?'0':'1';
118 sci_tx_buf[2] = (coin_value/10000)%10;
119 sci_tx_buf[3] = (coin_value/1000)%10;
120 sci_tx_buf[4] = (coin_value/100)%10;
121 sci_tx_buf[5] = (coin_value/10)%10;
122 sci_tx_buf[6] = coin_value%10;
130 if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') ||
131 (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9') ||
132 (sci_rx_buf[3] < '0') || (sci_rx_buf[3] > '9') ||
133 (sci_rx_buf[4] < '0') || (sci_rx_buf[4] > '9') ||
134 (sci_rx_buf[5] < '0') || (sci_rx_buf[5] > '9')) {
137 cost = sci_rx_buf[1] - '0';
138 cost *= 10; cost = sci_rx_buf[2] - '0';
139 cost *= 10; cost = sci_rx_buf[3] - '0';
140 cost *= 10; cost = sci_rx_buf[4] - '0';
141 cost *= 10; cost = sci_rx_buf[5] - '0';
147 void send_keypress(u8 key) {
148 /* send a packet of the form KX with X being the key, or R for reset */
149 if (is_standalone()) return;
152 if (key == KEY_RESET) {
157 sci_tx_buf[2] = (key%10)+'0';
159 sci_tx_buf[3] = '\0';
161 send_string(" key." CRLF);
164 void send_door_msg(bool open) {
165 if (is_standalone()) return;
168 sci_tx_buf[2] = open?'1':'0';
171 send_string(" door open." CRLF);
173 send_string(" door closed." CRLF);
176 u8 hexchar2u8(char b) {
177 if (b >= '0' && b <= '9') return b-'0';
178 if (b >= 'a' && b <= 'f') return b-'a'+0x0a;
179 if (b >= 'A' && b <= 'F') return b-'A'+0x0a;
183 char nibble2hexchar(u8 b) {
184 if (b <= 9) return b+'0';
185 if (b >= 10 && b <= 15) return b+'A';
189 u8 hex2u8(char msb, char lsb) {
190 return (hexchar2u8(msb) << 4) + hexchar2u8(lsb);
193 static char hexconv_buf[3];
195 hexconv_buf[0] = nibble2hexchar((a&0xf0) >> 4);
196 hexconv_buf[1] = nibble2hexchar(a&0x0f);
197 hexconv_buf[2] = '\0';
202 if (check_standalone()) return;
203 if (sci_rx_buf[1] == '\0')
205 else if (sci_rx_buf[2] != '\0' && sci_rx_buf[3] == '\0')
206 chime_start(hex2u8(sci_rx_buf[1], sci_rx_buf[2]));
208 send_string("510 Unknown chime duration." CRLF);
211 send_string("500 Chimed." CRLF);
214 void print_switches(u8 prompted) {
219 send_string(u82hex(misc_input));
221 send_string(u82hex(switch_input));
226 /* make sure it's really a ping */
227 if (!my_strncmp("ING", (char*)sci_rx_buf+1, 3)) {
231 /* respond with ack & pong */
232 send_string("000 PONG!" CRLF);
236 send_string(is_standalone()?"$ ":"# ");
240 if (!my_strncmp("BOUT", (char*)sci_rx_buf+1, 4)) {
245 "-----------------------------------------------------------------" CRLF
247 "-----------------------------------------------------------------" CRLF
248 " Revision " VERSION_STRING CRLF
250 " This snack machine was brought to you by " CRLF
251 " Bernard Blackham" CRLF
253 " Harry McNally" CRLF
254 " Michal Gornisiewicz" CRLF
257 " Another UCC project in action. http://www.ucc.asn.au/" CRLF
262 if (my_strncmp("CHO ON", (char*)sci_rx_buf+1, 6))
264 else if (my_strncmp("CHO OFF", (char*)sci_rx_buf+1, 7))
271 if (!my_strncmp("OO", (char*)sci_rx_buf+1, 2)) {
280 " /___________/___/|" CRLF
282 " | ==\\ /== | |" CRLF
283 " | O O | \\ \\ |" CRLF
286 " / | \\_____/ | / /" CRLF
288 "/||\\| | /||\\/" CRLF
289 " -------------| " CRLF
297 "Valid commands are:" CRLF
298 " ABOUT ROM information" CRLF
300 " ECHO {ON|OFF} turn echo on or off" CRLF
301 " Vnn vend an item" CRLF
302 " VALL vend all items" CRLF
303 " DXXXXXXXXXX show a message on the display" CRLF
304 " B[nn] beep for a duration nn (optional)" CRLF
305 " S[...] query all internal switch states" CRLF
306 " H[...] this help screen" CRLF
307 "Comments start with a #" CRLF
312 if (my_strncmp("UIT", (char*)sci_rx_buf+1, 3))
313 send_string("013 You can't quit you doofus." CRLF);
320 for (i = 0; i < 11; i++)
321 display_buf[i] = ' ';
322 display_buf[10] = '\0';
324 changer_output = 0x7f;
325 _io_ports[M6811_PORTA] = 0xc0; /* display on. talking to serial port */
326 _io_ports[M6811_DDRA] = 0xfc;
327 _io_ports[M6811_DDRD] = 0x3e;
328 _io_ports[M6811_SPCR] = M6811_MSTR | M6811_SPR1;
329 set_misc_output(0x00);
335 unlock(); /* enable interrupts */
353 send_string("5N4X0RZ R US" CRLF);
355 last_standalone = is_standalone();
362 last_switch_input = switch_input;
363 last_misc_input = misc_input;
366 if (cur_motor[0] == 0xff) { /* signal to say redraw screen */
367 set_msg("*5N4X0RZ* ");
371 if (door_open() != last_door_open) {
372 last_door_open = door_open();
373 send_door_msg(last_door_open);
375 set_msg(last_door_open?"DOOR OPEN ":"DOOR CLOSE");
378 if (last_standalone != is_standalone()) {
379 /* somebody flicked the standalone switch */
383 last_standalone = is_standalone();
386 if (last_misc_input != misc_input) {
388 last_misc_input = misc_input;
391 if (last_switch_input != switch_input) {
393 last_switch_input = switch_input;
396 if (sci_have_packet) {
397 switch (sci_rx_buf[0]) {
409 dispense_something();
442 if (keypad_pressed()) {
443 if (is_standalone()) {
444 if (last_key == KEY_RESET) {
449 cur_motor[1] = last_key%10;
450 display_buf[1] = cur_motor[1]+'0';
451 set_msg(display_buf);
453 motor_num = cur_motor[0]%10;
455 motor_num += cur_motor[1];
456 switch (dispense_motor(motor_num)) {
457 case MOTOR_HOME_FAIL:
458 set_msg(" HOME FAIL ");
460 case MOTOR_CURRENT_FAIL:
461 set_msg(" OVER CRNT ");
464 set_msg("THANK YOU");
467 set_msg(" NO MOTOR ");
470 set_msg("ERRRRRRRR?");
474 display_buf[0] = ' ';
475 display_buf[1] = ' ';
479 cur_motor[0] = last_key;
480 display_buf[0] = (last_key%10)+'0';
481 set_msg(display_buf);
485 send_keypress(last_key);
489 if (coin_value != last_coin_value) {
491 last_coin_value = coin_value;