+bool check_badpoke() {
+ if (cant_poke()) {
+ send_string("099 Can't poke without flipping DIP SW 3." CRLF);
+ return 1;
+ }
+ return 0;
+}
+
+void unknown_command() {
+ send_string("012 Unknown command. Type HELP for help." CRLF);
+}
+
+void motor_reply(u8 code) {
+ /* returns a message of the form MXYY - X is return code, YY is motor */
+ switch (code) {
+ case MOTOR_SUCCESS:
+ send_string("100 Vend successful." CRLF);
+ break;
+ case MOTOR_NOSLOT:
+ send_string("151 No motor there." CRLF);
+ break;
+ case MOTOR_CURRENT_FAIL:
+ send_string("152 Over current." CRLF);
+ break;
+ case MOTOR_HOME_FAIL:
+ send_string("153 Home sensors failing." CRLF);
+ break;
+ default:
+ send_string("159 Unknown motor error." CRLF);
+ }
+}
+
+void dispense_something() {
+ /* process a message VXX in sci_rx_buf where XX is motor number */
+ u8 slot;
+
+ if (check_standalone()) return;
+ if (must_verify() && !mic_verify((void*)sci_rx_buf)) {
+ send_string("019 Message verification failed." CRLF);
+ return;
+ }
+
+ if (my_strncmp("ALL", (char*)sci_rx_buf+1, 3)) {
+ char motor[3];
+ motor[2] = '\0';
+ send_string("102 Vend all motors starting." CRLF);
+ for (motor[0] = '0'; motor[0] <= '9'; motor[0]++) {
+ for (motor[1] = '0'; motor[1] <= '9'; motor[1]++) {
+ if (motor[1] == '5') continue; /* there is now row 5 */
+ send_string("101 Vending ");
+ send_string(motor);
+ send_string(CRLF);
+ motor_reply(dispense_motor((motor[0]-'0')*10+(motor[1]-'0')));
+ }
+ }
+ send_string("102 Vend all motors complete." CRLF);
+ return;
+ }
+
+ if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') ||
+ (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9')) {
+ sci_rx_buf[1] = sci_rx_buf[2] = '0';
+ motor_reply(MOTOR_NOSLOT);
+ return;
+ }
+
+ slot = (sci_rx_buf[1] - '0') * 10;
+ slot += sci_rx_buf[2] - '0';
+
+ motor_reply(dispense_motor(slot));
+}
+
+void write_to_display() {
+ /* process a message in the form DXXXXXXXXXX to send to display */
+ u8 i;
+ char buf[11];
+
+ if (check_standalone()) return;
+
+ for (i = 0; i < 10; i++)
+ if (sci_rx_buf[i+1])
+ buf[i] = sci_rx_buf[i+1];
+ else
+ break;
+
+ for (; i < 10; i++) /* pad the rest out with spaces */
+ buf[i] = ' ';
+ buf[i] = '\0';
+
+ set_msg(buf);
+ send_string("300 Written." CRLF);
+}
+
+void send_balance() {
+ sci_tx_buf[0] = 'C';
+ sci_tx_buf[1] = have_change?'0':'1';
+ sci_tx_buf[2] = (coin_value/10000)%10;
+ sci_tx_buf[3] = (coin_value/1000)%10;
+ sci_tx_buf[4] = (coin_value/100)%10;
+ sci_tx_buf[5] = (coin_value/10)%10;
+ sci_tx_buf[6] = coin_value%10;
+ sci_tx_buf[8] = 0;
+ send_buffer(1);
+}
+
+void give_change() {
+ u16 cost;
+
+ if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') ||
+ (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9') ||
+ (sci_rx_buf[3] < '0') || (sci_rx_buf[3] > '9') ||
+ (sci_rx_buf[4] < '0') || (sci_rx_buf[4] > '9') ||
+ (sci_rx_buf[5] < '0') || (sci_rx_buf[5] > '9')) {
+ //send_nack();
+ }
+ cost = sci_rx_buf[1] - '0';
+ cost *= 10; cost = sci_rx_buf[2] - '0';
+ cost *= 10; cost = sci_rx_buf[3] - '0';
+ cost *= 10; cost = sci_rx_buf[4] - '0';
+ cost *= 10; cost = sci_rx_buf[5] - '0';
+
+ coin_cost(cost);
+ //send_ack();
+}
+
+void send_keypress(u8 key) {
+ /* send a packet of the form KX with X being the key, or R for reset */
+ if (is_standalone()) return;
+
+ sci_tx_buf[0] = '2';
+ if (key == KEY_RESET) {
+ sci_tx_buf[1] = '1';
+ sci_tx_buf[2] = '1';
+ } else {
+ sci_tx_buf[1] = '0';
+ sci_tx_buf[2] = (key%10)+'0';
+ }
+ sci_tx_buf[3] = '\0';
+ send_buffer(0);
+ send_string(" key." CRLF);
+}
+
+void send_timer() {
+ /* send a packet of the form KX with X being the key, or R for reset */
+ unsigned int t=get_timer_counter();
+
+ send_string("013 ");
+ sci_tx_buf[0] = '0'+(t/10000)%10;
+ sci_tx_buf[1] = '0'+(t/1000)%10;
+ sci_tx_buf[2] = '0'+(t/100)%10;
+ sci_tx_buf[3] = '0'+(t/10)%10;
+ sci_tx_buf[4] = '0'+t%10;
+ sci_tx_buf[5] = 0;
+ send_buffer(1);
+}
+
+void send_door_msg(bool open) {
+ if (is_standalone()) return;
+ sci_tx_buf[0] = '4';
+ sci_tx_buf[1] = '0';
+ sci_tx_buf[2] = open?'1':'0';
+ send_buffer(0);
+ if (open)
+ send_string(" door open." CRLF);
+ else
+ send_string(" door closed." CRLF);
+}
+
+void do_chime() {
+ if (check_standalone()) return;
+ if (sci_rx_buf[1] == '\0')
+ chime_start();
+ else if (sci_rx_buf[1] == 'S') { /* synchronous beep */
+ if (sci_rx_buf[2] == '\0')
+ chime_start();
+ else if (sci_rx_buf[3] != '\0' && sci_rx_buf[4] == '\0')
+ chime_for(hex2u8(sci_rx_buf[2], sci_rx_buf[3]));
+ else {
+ send_string("510 Unknown chime duration." CRLF);
+ return;
+ }
+ while (chime_count); /* spin */
+ send_string("500 Chimed." CRLF);
+ return;
+ } else if (sci_rx_buf[2] != '\0' && sci_rx_buf[3] == '\0')
+ chime_for(hex2u8(sci_rx_buf[1], sci_rx_buf[2]));
+ else {
+ send_string("510 Unknown chime duration." CRLF);
+ return;
+ }
+ send_string("500 Chime started." CRLF);
+ return;
+}
+
+void do_silence() {
+ if (check_standalone()) return;
+ if (sci_rx_buf[1] == '\0')
+ unchime_start();
+ else if (sci_rx_buf[1] == 'S') { /* synchronous beep */
+ if (sci_rx_buf[2] == '\0')
+ unchime_start();
+ else if (sci_rx_buf[3] != '\0' && sci_rx_buf[4] == '\0')
+ unchime_for(hex2u8(sci_rx_buf[2], sci_rx_buf[3]));
+ else {
+ send_string("511 Unknown silence duration." CRLF);
+ return;
+ }
+ while (unchime_count); /* spin */
+ send_string("501 Silenced." CRLF);
+ return;
+ } else if (sci_rx_buf[2] != '\0' && sci_rx_buf[3] == '\0')
+ unchime_for(hex2u8(sci_rx_buf[1], sci_rx_buf[2]));
+ else {
+ send_string("511 Unknown silence duration." CRLF);
+ return;
+ }
+ send_string("501 Silence started." CRLF);
+ return;
+}
+
+void print_switches(u8 prompted) {
+ if (prompted)
+ send_string("600 ");
+ else
+ send_string("610 ");
+ send_string(u82hex(misc_input));
+ send_string(" ");
+ send_string(u82hex(switch_input));
+ send_string(CRLF);
+}
+
+void ping_pong() {
+ /* make sure it's really a ping */
+ if (!my_strncmp("ING", (char*)sci_rx_buf+1, 3)) {
+ unknown_command();
+ return;
+ }
+ /* respond with ack & pong */
+ send_string("000 PONG!" CRLF);
+}
+
+u16 hex2addr(char* addrptr) {
+ u16 v;
+ v = hex2u8(addrptr[0], addrptr[1]) << 8;
+ v |= hex2u8(addrptr[2], addrptr[3]);
+ return v;
+}
+
+void peek() {
+ if (!my_strncmp("EEK", (char*)sci_rx_buf+1, 3)) {
+ unknown_command();
+ return;
+ }
+ if (check_badpoke()) return;
+ if (ishex(sci_rx_buf[4]) && ishex(sci_rx_buf[5]) && ishex(sci_rx_buf[6]) &&
+ ishex(sci_rx_buf[7]) && sci_rx_buf[8] == '\0') {
+ u16 v = hex2addr((char*)(sci_rx_buf+4));
+ v = *((u8*)v);
+ send_string("090 ");
+ send_string(u82hex(v));
+ send_string(CRLF);
+ return;
+ }
+ send_string("091 Invalid location given." CRLF);
+}
+
+void poke() {
+ if (!my_strncmp("OKE", (char*)sci_rx_buf+1, 3)) {
+ unknown_command();
+ return;