+void give_change() {
+ u16 cost;
+
+ if ((msg_buf[1] < '0') || (msg_buf[1] > '9') ||
+ (msg_buf[2] < '0') || (msg_buf[2] > '9') ||
+ (msg_buf[3] < '0') || (msg_buf[3] > '9') ||
+ (msg_buf[4] < '0') || (msg_buf[4] > '9') ||
+ (msg_buf[5] < '0') || (msg_buf[5] > '9')) {
+ send_nack();
+ }
+ cost = msg_buf[1] - '0';
+ cost *= 10; cost = msg_buf[2] - '0';
+ cost *= 10; cost = msg_buf[3] - '0';
+ cost *= 10; cost = msg_buf[4] - '0';
+ cost *= 10; cost = msg_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 */
+ wait_for_tx_free();
+ tx_buffer[0] = 'K';
+ if (key == KEY_RESET)
+ tx_buffer[1] = 'R';
+ else
+ tx_buffer[1] = (key%10)+'0';
+ tx_buffer[2] = '\n';
+ tx_buffer[3] = 0;
+ send_packet();
+}
+
+void send_door_msg(bool open) {
+ wait_for_tx_free();
+ tx_buffer[0] = 'D';
+ tx_buffer[1] = open?'1':'0';
+ tx_buffer[2] = '\n';
+ tx_buffer[3] = 0;
+ send_packet();
+}
+
+void do_chime() {
+ chime_start();
+ send_ack();
+}
+
+void ping_pong() {
+ /* make sure it's really a ping */
+ if (msg_buf[1] != 'I' ||
+ msg_buf[2] != 'N' ||
+ msg_buf[3] != 'G') {
+ send_nack();
+ return;
+ }
+ /* respond with ack & pong */
+ wait_for_tx_free();
+ tx_buffer[0] = 'P';
+ tx_buffer[1] = 'O';
+ tx_buffer[2] = 'N';
+ tx_buffer[3] = 'G';
+ tx_buffer[4] = '\n';
+ tx_buffer[5] = 0;
+ send_packet();
+}
+
+u16 last_coin_value;
+bool last_door_open;
+char display_buf[11];
+/* cur_motor[0] is 0 for nothing, or 1..10, or 0xff to say redraw.
+ * cur_motor[1] is 0..9 and only meaningful is cur_motor[0] != 0. */
+u8 cur_motor[2];
+
+int main() {
+ u8 i;
+ for (i = 0; i < 11; i++)
+ display_buf[i] = ' ';
+ display_buf[10] = '\0';
+
+ changer_output = 0x7f;
+ _io_ports[M6811_PORTA] = 0xc0; /* display on. talking to serial port */
+ _io_ports[M6811_DDRA] = 0xfc;
+ _io_ports[M6811_DDRD] = 0x3e;
+ _io_ports[M6811_SPCR] = 0x12;
+ set_misc_output(0x00);
+
+ display_init();
+ set_msg(" HELLO ");
+
+ unlock(); /* enable interrupts */
+
+ comm_init();
+ coinmech_init();
+ keypad_init();
+ last_coin_value = 0;
+ last_door_open = 0;
+
+ delay(1000);
+
+ set_msg(" CRUEL ");
+
+ delay(1000);
+
+ set_msg(" WORLD ");
+ delay(1000);
+
+ chime_start();
+
+ cur_motor[0] = 0xff;
+ while(1) {
+ if (cur_motor[0] == 0xff) { /* signal to say redraw screen */
+ set_msg("*5N4X0RZ* ");
+ cur_motor[0] = 0;
+ }
+
+ if (door_open() != last_door_open) {
+ last_door_open = door_open();
+ send_door_msg(last_door_open);
+ chime_start();
+ if (last_door_open) {
+ set_msg("DOOR OPEND");
+ } else {
+ set_msg("DOOR CLOSE");
+ }
+ }
+
+ /*
+ if (rx_queue_state) {
+ switch (msg_buf[0]) {
+ case 'V':
+ dispense_something();
+ break;
+ case 'D':
+ write_to_display();
+ break;
+ case 'B':
+ do_chime();
+ break;
+ case 'U':
+ send_balance();
+ break;
+ case 'G':
+ give_change();
+ break;
+ case 'P':
+ ping_pong();
+ break;
+ default:
+ // shurg
+ send_nack();
+ break;
+ }
+ msg_clr();
+ }
+ */
+
+ keypad_read();
+ if (keypad_pressed()) {
+ if (last_key == KEY_RESET) {
+ cur_motor[0] = 0xff;
+ } else {
+ if (cur_motor[0]) {
+ u8 motor_num;
+ cur_motor[1] = last_key%10;
+ display_buf[1] = cur_motor[1]+'0';
+ set_msg(display_buf);
+
+ motor_num = cur_motor[0]%10;
+ motor_num *= 10;
+ motor_num += cur_motor[1];
+ switch (dispense_motor(motor_num)) {
+ case MOTOR_HOME_FAIL:
+ set_msg(" HOME FAIL ");
+ break;
+ case MOTOR_CURRENT_FAIL:
+ set_msg(" OVER CRNT ");
+ break;
+ case MOTOR_SUCCESS:
+ set_msg("THANK YOU");
+ break;
+ default:
+ set_msg("ERRRRRRRR?");
+ break;
+ }
+
+ display_buf[0] = ' ';
+ display_buf[1] = ' ';
+ cur_motor[0] = 0xff;
+ delay(500);
+ } else {
+ cur_motor[0] = last_key;
+ display_buf[0] = (last_key%10)+'0';
+ set_msg(display_buf);
+ }
+ }
+ send_keypress(last_key);
+ }
+
+ /*
+ if (coin_value != last_coin_value) {
+ send_balance();
+ last_coin_value = coin_value;
+ }
+ */
+ }