2 * main_basic.c - a simplified main procedure that relies upon a ersver to do
3 * anything smart. Just be a dumb interface to a display, keypad, coin mech
7 #include "display_basic.h"
15 void motor_reply(char* slotptr, u8 code) {
16 /* returns a message of the form MXYY - X is return code, YY is motor */
19 sci_tx_buf[1] = code + '0';
20 sci_tx_buf[2] = *slotptr;
21 sci_tx_buf[3] = *(slotptr+1);
27 void dispense_something() {
28 /* process a message VXX in sci_rx_buf where XX is motor number */
31 if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') ||
32 (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9')) {
33 sci_rx_buf[1] = sci_rx_buf[2] = '0';
34 motor_reply((char*)&sci_rx_buf[1], MOTOR_NOSLOT);
38 slot = (sci_rx_buf[1] - '0') * 10;
39 slot += sci_rx_buf[2] - '0';
41 motor_reply((char*)&sci_rx_buf[1], dispense_motor(slot));
44 void write_to_display() {
45 /* process a message in the form DXXXXXXXXXXX to send to display */
48 for (i = 0; i < 10; i++)
50 buf[i] = sci_rx_buf[i+1];
54 for (; i < 10; i++) /* pad the rest out with spaces */
64 sci_tx_buf[1] = have_change?'0':'1';
65 sci_tx_buf[2] = (coin_value/10000)%10;
66 sci_tx_buf[3] = (coin_value/1000)%10;
67 sci_tx_buf[4] = (coin_value/100)%10;
68 sci_tx_buf[5] = (coin_value/10)%10;
69 sci_tx_buf[6] = coin_value%10;
78 if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') ||
79 (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9') ||
80 (sci_rx_buf[3] < '0') || (sci_rx_buf[3] > '9') ||
81 (sci_rx_buf[4] < '0') || (sci_rx_buf[4] > '9') ||
82 (sci_rx_buf[5] < '0') || (sci_rx_buf[5] > '9')) {
85 cost = sci_rx_buf[1] - '0';
86 cost *= 10; cost = sci_rx_buf[2] - '0';
87 cost *= 10; cost = sci_rx_buf[3] - '0';
88 cost *= 10; cost = sci_rx_buf[4] - '0';
89 cost *= 10; cost = sci_rx_buf[5] - '0';
95 void send_keypress(u8 key) {
96 /* send a packet of the form KX with X being the key, or R for reset */
102 sci_tx_buf[1] = (key%10)+'0';
103 sci_tx_buf[2] = '\n';
108 void send_door_msg(bool open) {
111 sci_tx_buf[1] = open?'1':'0';
112 sci_tx_buf[2] = '\n';
123 /* make sure it's really a ping */
124 if (sci_rx_buf[1] != 'I' ||
125 sci_rx_buf[2] != 'N' ||
126 sci_rx_buf[3] != 'G') {
130 /* respond with ack & pong */
136 sci_tx_buf[4] = '\n';
143 char display_buf[11];
144 /* cur_motor[0] is 0 for nothing, or 1..10, or 0xff to say redraw.
145 * cur_motor[1] is 0..9 and only meaningful is cur_motor[0] != 0. */
150 for (i = 0; i < 11; i++)
151 display_buf[i] = ' ';
152 display_buf[10] = '\0';
154 changer_output = 0x7f;
155 _io_ports[M6811_PORTA] = 0xc0; /* display on. talking to serial port */
156 _io_ports[M6811_DDRA] = 0xfc;
157 _io_ports[M6811_DDRD] = 0x3e;
158 _io_ports[M6811_SPCR] = M6811_MSTR | M6811_SPR1;
159 set_misc_output(0x00);
164 unlock(); /* enable interrupts */
183 my_strncpy(sci_tx_buf, "5N4X0RZRUS\n", BUFFER_LEN);
188 if (cur_motor[0] == 0xff) { /* signal to say redraw screen */
189 set_msg("*5N4X0RZ* ");
193 if (door_open() != last_door_open) {
194 last_door_open = door_open();
195 send_door_msg(last_door_open);
197 set_msg(last_door_open?"DOOR OPEN ":"DOOR CLOSE");
200 if (sci_have_packet) {
201 switch (sci_rx_buf[0]) {
203 dispense_something();
229 if (keypad_pressed()) {
230 if (last_key == KEY_RESET) {
235 cur_motor[1] = last_key%10;
236 display_buf[1] = cur_motor[1]+'0';
237 set_msg(display_buf);
239 motor_num = cur_motor[0]%10;
241 motor_num += cur_motor[1];
242 switch (dispense_motor(motor_num)) {
243 case MOTOR_HOME_FAIL:
244 set_msg(" HOME FAIL ");
246 case MOTOR_CURRENT_FAIL:
247 set_msg(" OVER CRNT ");
250 set_msg("THANK YOU");
253 set_msg(" NO MOTOR ");
256 set_msg("ERRRRRRRR?");
260 display_buf[0] = ' ';
261 display_buf[1] = ' ';
265 cur_motor[0] = last_key;
266 display_buf[0] = (last_key%10)+'0';
267 set_msg(display_buf);
270 send_keypress(last_key);
274 if (coin_value != last_coin_value) {
276 last_coin_value = coin_value;