#define COINMECH_ID 0x20
volatile u8 last_byte;
-volatile u8 packet_pos = 0;
-volatile u16 value_1 = 0;
-volatile u8 value_2 = 0;
-volatile u8 dec_point = 0;
+volatile u8 packet_pos;
+volatile u16 value_1;
+volatile u8 value_2;
+volatile u8 dec_point;
-volatile u16 coin_value = 0;
-u8 item_cost = 0;
-volatile bool have_change = 0;
+volatile u16 coin_value;
+u8 item_cost;
+volatile bool have_change;
u8 parity_test(u8 c) {
u8 parity = 0;
}
void coinmech_init() {
+ packet_pos = 0;
+ value_1 = value_2 = 0;
+ dec_point = 0;
+ coin_value = 0;
+ item_cost = 0;
+ have_change = 0;
_io_ports[M6811_SCCR1] = 0x10;
_io_ports[M6811_SCCR2] = 0x2e;
_io_ports[M6811_BAUD] = 0x03;
#include "vend.h"
extern volatile u16 coin_value;
+extern volatile bool have_change;
void coin_eat();
void coin_cost(u16 cost); /* specify the cost of an item. */
*
* Messages Sent:
* KX - keypad press where X is (ascii 0..9 or R)
- * CXXXXX - coin balance, XXXXX is number of cents.
+ * CYXXXXX - coin balance, XXXXX is number of cents. Y is 0 if we have change,
+ * 1 otherwise
* MXYY - dispense ack/nack. X is what happened (0..MOTOR_*_FAIL), YY is the motor
* DX - door open/close event where X is 1 for open, 0 for closed.
+ * PONG - response to a ping
*
* Messages Received:
* VXX - vend a slot XX
* U - query current coin balance. - replies with CXXXXX
* GXXXXX - give change, XXXXX is the amount of the cost of the item hence change
* is the current value in the coin mech - XXXXX
+ * PING - ping. responds with PONG
*
*/
-#define TX_BUFFER_LEN 6 /* maximum 12 due to the way tx_int works with the FIFO */
+#define TX_BUFFER_LEN 8 /* 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 */
#include "keypad.h"
u8 last_key; /* the last key registered */
-bool new_key = 0;
+bool new_key;
/* first 8 from the first row, then 3 from the second row */
/* keys are 1-9, 0, reset */
new_key = 0;
return 1;
}
+
+void keypad_init() {
+ last_key = 0;
+ new_key = 0;
+}
bool keypad_pressed();
void keypad_read();
u8 keypad_getkey();
+void keypad_init();
#endif /* _KEYPAD_H_ */
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();
}
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;
display_init();
comm_init();
coinmech_init();
+ keypad_init();
last_coin_value = 0;
last_door_open = 0;
case 'G':
give_change();
break;
+ case 'P':
+ ping_pong();
+ break;
default:
/* shrug */
send_nack();