Final workings
authorBernard Blackham <[email protected]>
Fri, 15 Aug 2003 08:59:50 +0000 (08:59 +0000)
committerBernard Blackham <[email protected]>
Fri, 15 Aug 2003 08:59:50 +0000 (08:59 +0000)
ROM2/comm.c
ROM2/comm.h
ROM2/main_basic.c
ROM2/motors.c
ROM2/motors.h
ROM2/vectors.s

index 09ad2a1..ad0a11a 100644 (file)
@@ -143,6 +143,28 @@ void uart_interrupt() {
  * End of interrupt handler
  */
 
+void send_ack() {
+       wait_for_tx_free();
+       tx_buffer[0] = '!';
+       tx_buffer[1] = '\n';
+       tx_buffer[2] = 0;
+       lock();
+       tx_int();
+       unlock();
+       send_packet();
+}
+
+void send_nack() {
+       wait_for_tx_free();
+       tx_buffer[0] = '?';
+       tx_buffer[1] = '\n';
+       tx_buffer[2] = 0;
+       lock();
+       tx_int();
+       unlock();
+       send_packet();
+}
+
 /* sends the packet in tx_buffer and doesn't return until it's been sent */
 void send_packet() {
        bset((void*)&tx_queue_state, 0x01);
index 69f8d00..9bff8bd 100644 (file)
@@ -23,7 +23,7 @@
  *
  */
 
-#define TX_BUFFER_LEN 6 /* maximum 13 due to the way tx_int works with the FIFO */
+#define TX_BUFFER_LEN 6 /* maximum 12 due to the way tx_int works with the FIFO */
 extern char tx_buffer[TX_BUFFER_LEN+2]; /* \n + null terminated */
 #define RX_BUFFER_LEN 11
 extern volatile char rx_buffer[RX_BUFFER_LEN+1]; /* null terminated */
@@ -42,8 +42,13 @@ extern volatile u8 rx_queue_state;
  */
 extern volatile u8 tx_queue_state;
 
+extern inline void wait_for_tx_free() { while (tx_queue_state & 0x01); }
+
 void comm_init();
 void msg_clr();
+void send_packet();
+void send_ack();
+void send_nack();
 
 /*************************************/
 /*** 16550 UART specific #defines  ***/
index 21844bc..e459471 100644 (file)
@@ -8,9 +8,107 @@
 #include "keypad.h"
 #include "chime.h"
 #include "coinmech.h"
+#include "motors.h"
 #include "comm.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;
+       send_packet();
+}
+
+void dispense_something() {
+       /* process a message VXX in msg_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);
+               return;
+       }
+
+       slot = (msg_buf[1] - '0') * 10;
+       slot += msg_buf[2] - '0';
+
+       motor_reply((char*)&msg_buf[1], dispense_motor(slot));
+}
+
+void write_to_display() {
+       /* process a message in the form DXXXXXXXXXXX to send to display */
+       u8 i;
+       char buf[10];
+       for (i = 0; i < 10; i++)
+               if (msg_buf[i+1])
+                       buf[i] = msg_buf[i+1];
+               else
+                       break;
+
+       for (; i < 10; i++) /* pad the rest out with spaces */
+               buf[i] = ' ';
+
+       set_msg(buf);
+       send_ack();
+}
+
+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;
+       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')) {
+               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+'0';
+       tx_buffer[2] = '\n';
+       tx_buffer[3] = 0;
+       send_packet();
+}
+
+void do_chime() {
+       chime_start();
+       send_ack();
+}
+
 int main() {
        u16 last_coin_value = coin_value;
 
@@ -20,15 +118,20 @@ int main() {
        while(1) {
                if (rx_queue_state) {
                        switch (msg_buf[0]) {
-                               case 'V': /* dispense something */
+                               case 'V':
+                                       dispense_something();
                                        break;
-                               case 'D': /* write string to display */
+                               case 'D':
+                                       write_to_display();
                                        break;
-                               case 'B': /* beep */
+                               case 'B': 
+                                       do_chime();
                                        break;
-                               case 'U': /* query current coin balance */
+                               case 'U':
+                                       send_balance();
                                        break;
-                               case 'G': /* give change */
+                               case 'G':
+                                       give_change();
                                        break;
                                default:
                                /* shrug */
@@ -36,18 +139,17 @@ int main() {
                        }
                        msg_clr();
                }
+
                keypad_read();
                if (keypad_pressed()) {
-                       /* send packet about it */
+                       send_keypress(last_key);
                        /* beep? */
                }
 
                if (coin_value != last_coin_value) {
-                       /* send a packet about it */
+                       send_balance();
+                       last_coin_value = coin_value;
                }
        }
 }
 
-void rti() {
-       chime(); /* turn chime on or off as need be */
-}
index 3fd99a2..b0e4ee3 100644 (file)
@@ -1,6 +1,20 @@
 #include "motors.h"
 #include "vend.h"
 
+const u8 motor_lookup[80] = 
+{ 1,12,23,34,46,57,68,79,
+ 11,22,33,44,56,67,78,89,
+ 21,32,43,54,66,77,88,99,
+ 31,42,53,64,76,87,98,
+  9,41,52,63,74,86,97,
+  8,19,51,62,73,84,96,
+  7,18,29,61,72,83,94,
+  6,17,28,39,71,82,93,
+  4,16,27,38,49,81,92,
+  3,14,26,37,48,59,91,
+  2,13,24,36,47,58,69,
+};
+
 void motor_shift_send(u8 data) {
        u8 i;
        /* load it in, MSB first */
index a18e4b2..f44e327 100644 (file)
 #define MOTOR_CURRENT_FAIL 3
 #define MOTOR_VOLTAGE_FAIL 4
 
-const u8 motor_lookup[80] =
-{ 1,12,23,34,46,57,68,79,
- 11,22,33,44,56,67,78,89,
- 21,32,43,54,66,77,88,99,
- 31,42,53,64,76,87,98,
-  9,41,52,63,74,86,97,
-  8,19,51,62,73,84,96,
-  7,18,29,61,72,83,94,
-  6,17,28,39,71,82,93,
-  4,16,27,38,49,81,92,
-  3,14,26,37,48,59,91,
-  2,13,24,36,47,58,69,
-};
+extern const u8 motor_lookup[80];
 
 bool is_motor(u8 slot);
 u8 dispense_motor(u8 slot);
index 76e6614..f420128 100644 (file)
@@ -38,6 +38,13 @@ def:
        .globl _debug_user_vectors
 _debug_user_vectors = 0
 
+
+;; RTI interrupt handler
+       .sect .text
+rti:
+       jsr chime
+       rti
+
 ;; 
 ;; Interrupt vectors are in a specific section that is
 ;; mapped at 0xffc0. 

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