*
*/
-#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 */
*/
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 ***/
#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;
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 */
}
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 */
-}
#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 */
#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);