Spell some things out, but don't be quite so verbose in other places
[uccvend-snackrom.git] / ROM2 / main_basic.c
index fc736d7..50614b3 100644 (file)
@@ -9,36 +9,36 @@
 #include "chime.h"
 #include "coinmech.h"
 #include "motors.h"
-#include "comm.h"
+#include "sci.h"
 #include "vend.h"
 
 void motor_reply(char* slotptr, u8 code) {
        /* returns a message of the form MXYY - X is return code, YY is motor */
        wait_for_tx_free();
-       tx_buffer[0] = 'M';
-       tx_buffer[1] = code + '0';
-       tx_buffer[2] = *slotptr;
-       tx_buffer[3] = *(slotptr+1);
-       tx_buffer[4] = '\n';
-       tx_buffer[5] = 0;
+       sci_tx_buf[0] = 'M';
+       sci_tx_buf[1] = code + '0';
+       sci_tx_buf[2] = *slotptr;
+       sci_tx_buf[3] = *(slotptr+1);
+       sci_tx_buf[4] = '\n';
+       sci_tx_buf[5] = 0;
        send_packet();
 }
 
 void dispense_something() {
-       /* process a message VXX in msg_buf where XX is motor number */
+       /* process a message VXX in sci_rx_buf where XX is motor number */
        u8 slot;
 
-       if ((msg_buf[1] < '0') || (msg_buf[1] > '9') ||
-               (msg_buf[2] < '0') || (msg_buf[2] > '9')) {
-               msg_buf[1] = msg_buf[2] = '0';
-               motor_reply((char*)&msg_buf[1], MOTOR_NOSLOT);
+       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((char*)&sci_rx_buf[1], MOTOR_NOSLOT);
                return;
        }
 
-       slot = (msg_buf[1] - '0') * 10;
-       slot += msg_buf[2] - '0';
+       slot = (sci_rx_buf[1] - '0') * 10;
+       slot += sci_rx_buf[2] - '0';
 
-       motor_reply((char*)&msg_buf[1], dispense_motor(slot));
+       motor_reply((char*)&sci_rx_buf[1], dispense_motor(slot));
 }
 
 void write_to_display() {
@@ -46,8 +46,8 @@ void write_to_display() {
        u8 i;
        char buf[10];
        for (i = 0; i < 10; i++)
-               if (msg_buf[i+1])
-                       buf[i] = msg_buf[i+1];
+               if (sci_rx_buf[i+1])
+                       buf[i] = sci_rx_buf[i+1];
                else
                        break;
 
@@ -60,33 +60,33 @@ void write_to_display() {
 
 void send_balance() {
        wait_for_tx_free();
-       tx_buffer[0] = 'C';
-       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;
+       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[7] = '\n';
+       sci_tx_buf[8] = 0;
        send_packet();
 }
 
 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')) {
+       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 = 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';
+       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();
@@ -95,22 +95,22 @@ void give_change() {
 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';
+       sci_tx_buf[0] = 'K';
        if (key == KEY_RESET)
-               tx_buffer[1] = 'R';
+               sci_tx_buf[1] = 'R';
        else
-               tx_buffer[1] = (key%10)+'0';
-       tx_buffer[2] = '\n';
-       tx_buffer[3] = 0;
+               sci_tx_buf[1] = (key%10)+'0';
+       sci_tx_buf[2] = '\n';
+       sci_tx_buf[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;
+       sci_tx_buf[0] = 'D';
+       sci_tx_buf[1] = open?'1':'0';
+       sci_tx_buf[2] = '\n';
+       sci_tx_buf[3] = 0;
        send_packet();
 }
 
@@ -121,20 +121,20 @@ void do_chime() {
 
 void ping_pong() {
        /* make sure it's really a ping */
-       if (msg_buf[1] != 'I' ||
-               msg_buf[2] != 'N' ||
-               msg_buf[3] != 'G') {
+       if (sci_rx_buf[1] != 'I' ||
+               sci_rx_buf[2] != 'N' ||
+               sci_rx_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;
+       sci_tx_buf[0] = 'P';
+       sci_tx_buf[1] = 'O';
+       sci_tx_buf[2] = 'N';
+       sci_tx_buf[3] = 'G';
+       sci_tx_buf[4] = '\n';
+       sci_tx_buf[5] = 0;
        send_packet();
 }
 
@@ -155,7 +155,7 @@ int main() {
        _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;
+       _io_ports[M6811_SPCR] = M6811_MSTR | M6811_SPR1;
        set_misc_output(0x00);
        
        display_init();
@@ -163,8 +163,9 @@ int main() {
 
        unlock(); /* enable interrupts */
 
-       comm_init();
-       coinmech_init();
+       //comm_init();
+       //coinmech_init();
+       sci_init();
        keypad_init();
        last_coin_value = 0;
        last_door_open = 0;
@@ -191,16 +192,11 @@ int main() {
                        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");
-                       }
+                       set_msg(last_door_open?"DOOR OPEN ":"DOOR CLOSE");
                }
 
-               /*
-               if (rx_queue_state) {
-                       switch (msg_buf[0]) {
+               if (sci_have_packet) {
+                       switch (sci_rx_buf[0]) {
                                case 'V':
                                        dispense_something();
                                        break;
@@ -226,7 +222,6 @@ int main() {
                        }
                        msg_clr();
                }
-               */
 
                keypad_read();
                if (keypad_pressed()) {
@@ -242,9 +237,24 @@ int main() {
                                        motor_num = cur_motor[0]%10;
                                        motor_num *= 10;
                                        motor_num += cur_motor[1];
-                                       dispense_motor(motor_num);
+                                       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;
+                                               case MOTOR_NOSLOT:
+                                                       set_msg(" NO MOTOR ");
+                                                       break;
+                                               default:
+                                                       set_msg("ERRRRRRRR?");
+                                                       break;
+                                       }
 
-                                       set_msg("THANK  YOU");
                                        display_buf[0] = ' ';
                                        display_buf[1] = ' ';
                                        cur_motor[0] = 0xff;

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