Bunch of silly errors & debugging stuff.
[uccvend-snackrom.git] / ROM2 / main_basic.c
index e4709c2..d848401 100644 (file)
@@ -61,13 +61,14 @@ void write_to_display() {
 void send_balance() {
        wait_for_tx_free();
        tx_buffer[0] = 'C';
-       tx_buffer[1] = (coin_value/10000)%10;
-       tx_buffer[2] = (coin_value/1000)%10;
-       tx_buffer[3] = (coin_value/100)%10;
-       tx_buffer[4] = (coin_value/10)%10;
-       tx_buffer[5] = coin_value%10;
-       tx_buffer[6] = '\n';
-       tx_buffer[7] = 0;
+       tx_buffer[1] = have_change?'0':'1';
+       tx_buffer[2] = (coin_value/10000)%10;
+       tx_buffer[3] = (coin_value/1000)%10;
+       tx_buffer[4] = (coin_value/100)%10;
+       tx_buffer[5] = (coin_value/10)%10;
+       tx_buffer[6] = coin_value%10;
+       tx_buffer[7] = '\n';
+       tx_buffer[8] = 0;
        send_packet();
 }
 
@@ -90,6 +91,7 @@ void give_change() {
        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();
@@ -97,7 +99,7 @@ void send_keypress(u8 key) {
        if (key == KEY_RESET)
                tx_buffer[1] = 'R';
        else
-               tx_buffer[1] = key+'0';
+               tx_buffer[1] = (key%10)+'0';
        tx_buffer[2] = '\n';
        tx_buffer[3] = 0;
        send_packet();
@@ -117,28 +119,96 @@ void do_chime() {
        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();
+}
+
 int main() {
        u16 last_coin_value;
        bool last_door_open;
+       int display_pos;
+       unsigned int i;
+       char display_buf[11];
+       for (display_pos = 0; display_pos < 11; display_pos++)
+               display_buf[display_pos] = '\0';
+       display_pos = 0;
 
-       chime_start();
+       delay(1000);
+
+       chime_on();
+       for (i = 0; i < 40000; i++) {}
+       chime_off();
+       for (i = 0; i < 20000; i++) {}
+       chime_on();
+       for (i = 0; i < 60000; i++) {}
        
-       misc_output = 0;
        changer_output = 0x7f;
-       _io_ports[M6811_PORTA] = 0xc8;
+       //_io_ports[M6811_PORTA] = 0xc8; /* for talking to coin mech */
+       _io_ports[M6811_PORTA] = 0xc0; /* for talking to serial port */
+       _io_ports[M6811_DDRA] = 0xfc;
        _io_ports[M6811_DDRD] = 0x3e;
        _io_ports[M6811_SPCR] = 0x12;
+       set_misc_output(0x00);
+       
+       chime_off(); /* mainly for debugging */
 
        display_init();
+       set_msg(" HELLO    ");
+
+       unlock(); /* enable interrupts */
+
        comm_init();
        coinmech_init();
+       keypad_init();
+
+       chime_count = 100;
+       while (chime_count != 0);
+
+       set_msg("  CRUEL   ");
 
        last_coin_value = 0;
        last_door_open = 0;
+
+       dispense_motor(22);
+
+       chime_count = 50;
+       while (chime_count != 0);
+
+       set_msg("   WORLD  ");
+       delay(1000);
+       set_msg("*5N4X0RZ* ");
+       delay(1000);
+
        while(1) {
                if (door_open() != last_door_open) {
                        last_door_open = door_open();
                        send_door_msg(last_door_open);
+                       if (last_door_open) {
+                               chime_on();
+                               set_msg("DOOR OPEND");
+                               delay(100);
+                               chime_off();
+                       } else {
+                               chime_on();
+                               set_msg("DOOR CLOSE");
+                               delay(100);
+                               chime_off();
+                       }
                }
 
                if (rx_queue_state) {
@@ -158,6 +228,9 @@ int main() {
                                case 'G':
                                        give_change();
                                        break;
+                               case 'P':
+                                       ping_pong();
+                                       break;
                                default:
                                /* shrug */
                                        send_nack();
@@ -168,6 +241,13 @@ int main() {
 
                keypad_read();
                if (keypad_pressed()) {
+                       if (last_key == KEY_RESET)
+                               display_buf[display_pos] = 'R';
+                       else
+                               display_buf[display_pos] = '0'+(last_key%10);
+                       display_pos++;
+                       display_pos %= 10;
+                       set_msg(display_buf);
                        send_keypress(last_key);
                }
 

UCC git Repository :: git.ucc.asn.au