888932e5908dd182150a342a38d2f7c00764b97d
[uccvend-snackrom.git] / ROM2 / main_basic.c
1 /*
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
4  * and snacks.
5  */
6
7 #include "display_basic.h"
8 #include "keypad.h"
9 #include "chime.h"
10 #include "coinmech.h"
11 #include "motors.h"
12 #include "sci.h"
13 #include "vend.h"
14
15 void motor_reply(char* slotptr, u8 code) {
16         /* returns a message of the form MXYY - X is return code, YY is motor */
17         wait_for_tx_free();
18         sci_tx_buf[0] = 'M';
19         sci_tx_buf[1] = code + '0';
20         sci_tx_buf[2] = *slotptr;
21         sci_tx_buf[3] = *(slotptr+1);
22         sci_tx_buf[4] = '\n';
23         sci_tx_buf[5] = 0;
24         send_packet();
25 }
26
27 void dispense_something() {
28         /* process a message VXX in sci_rx_buf where XX is motor number */
29         u8 slot;
30
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);
35                 return;
36         }
37
38         slot = (sci_rx_buf[1] - '0') * 10;
39         slot += sci_rx_buf[2] - '0';
40
41         motor_reply((char*)&sci_rx_buf[1], dispense_motor(slot));
42 }
43
44 void write_to_display() {
45         /* process a message in the form DXXXXXXXXXXX to send to display */
46         u8 i;
47         char buf[10];
48         for (i = 0; i < 10; i++)
49                 if (sci_rx_buf[i+1])
50                         buf[i] = sci_rx_buf[i+1];
51                 else
52                         break;
53
54         for (; i < 10; i++) /* pad the rest out with spaces */
55                 buf[i] = ' ';
56
57         set_msg(buf);
58         send_ack();
59 }
60
61 void send_balance() {
62         wait_for_tx_free();
63         sci_tx_buf[0] = 'C';
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;
70         sci_tx_buf[7] = '\n';
71         sci_tx_buf[8] = 0;
72         send_packet();
73 }
74
75 void give_change() {
76         u16 cost;
77
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')) {
83                 send_nack();
84         }
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';
90
91         coin_cost(cost);
92         send_ack();
93
94
95 void send_keypress(u8 key) {
96         /* send a packet of the form KX with X being the key, or R for reset */
97         wait_for_tx_free();
98         sci_tx_buf[0] = 'K';
99         if (key == KEY_RESET)
100                 sci_tx_buf[1] = 'R';
101         else
102                 sci_tx_buf[1] = (key%10)+'0';
103         sci_tx_buf[2] = '\n';
104         sci_tx_buf[3] = 0;
105         send_packet();
106 }
107
108 void send_door_msg(bool open) {
109         wait_for_tx_free();
110         sci_tx_buf[0] = 'D';
111         sci_tx_buf[1] = open?'1':'0';
112         sci_tx_buf[2] = '\n';
113         sci_tx_buf[3] = 0;
114         send_packet();
115 }
116
117 void do_chime() {
118         chime_start();
119         send_ack();
120 }
121
122 void ping_pong() {
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' ||
127                 sci_rx_buf[4] != '\0') {
128                 send_nack();
129                 return;
130         }
131         /* respond with ack & pong */
132         wait_for_tx_free();
133         my_strncpy(sci_tx_buf, "PONG\n", BUFFER_LEN);
134         send_packet();
135 }
136
137 u16 last_coin_value;
138 bool last_door_open;
139 char display_buf[11];
140 /* cur_motor[0] is 0 for nothing, or 1..10, or 0xff to say redraw.
141  * cur_motor[1] is 0..9 and only meaningful is cur_motor[0] != 0. */
142 u8 cur_motor[2];
143
144 int main() {
145         u8 i;
146         for (i = 0; i < 11; i++)
147                 display_buf[i] = ' ';
148         display_buf[10] = '\0';
149
150         changer_output = 0x7f;
151         _io_ports[M6811_PORTA] = 0xc0; /* display on. talking to serial port */
152         _io_ports[M6811_DDRA] = 0xfc;
153         _io_ports[M6811_DDRD] = 0x3e;
154         _io_ports[M6811_SPCR] = M6811_MSTR | M6811_SPR1;
155         set_misc_output(0x00);
156         
157         display_init();
158         set_msg(" HELLO    ");
159
160         unlock(); /* enable interrupts */
161
162         delay(1000);
163         set_msg("  CRUEL   ");
164
165         //comm_init();
166         //coinmech_init();
167         sci_init();
168         keypad_init();
169         last_coin_value = 0;
170         last_door_open = 0;
171
172         delay(1000);
173
174         set_msg("   WORLD  ");
175         delay(1000);
176
177         chime_start();
178
179         my_strncpy(sci_tx_buf, "5N4X0RZRUS\n", BUFFER_LEN);
180         send_packet();
181         
182         cur_motor[0] = 0xff;
183         while(1) {
184                 if (cur_motor[0] == 0xff) { /* signal to say redraw screen */
185                         set_msg("*5N4X0RZ* ");
186                         cur_motor[0] = 0;
187                 }
188
189                 if (door_open() != last_door_open) {
190                         last_door_open = door_open();
191                         send_door_msg(last_door_open);
192                         chime_start();
193                         set_msg(last_door_open?"DOOR OPEN ":"DOOR CLOSE");
194                 }
195
196                 if (sci_have_packet) {
197                         switch (sci_rx_buf[0]) {
198                                 case 'V':
199                                         dispense_something();
200                                         break;
201                                 case 'D':
202                                         write_to_display();
203                                         break;
204                                 case 'B': 
205                                         do_chime();
206                                         break;
207                                 case 'U':
208                                         send_balance();
209                                         break;
210                                 case 'G':
211                                         give_change();
212                                         break;
213                                 case 'P':
214                                         ping_pong();
215                                         break;
216                                 default:
217                                         // shurg
218                                         send_nack();
219                                         break;
220                         }
221                         msg_clr();
222                 }
223
224                 keypad_read();
225                 if (keypad_pressed()) {
226                         if (last_key == KEY_RESET) {
227                                 cur_motor[0] = 0xff;
228                         } else {
229                                 if (cur_motor[0]) {
230                                         u8 motor_num;
231                                         cur_motor[1] = last_key%10;
232                                         display_buf[1] = cur_motor[1]+'0';
233                                         set_msg(display_buf);
234
235                                         motor_num = cur_motor[0]%10;
236                                         motor_num *= 10;
237                                         motor_num += cur_motor[1];
238                                         switch (dispense_motor(motor_num)) {
239                                                 case MOTOR_HOME_FAIL:
240                                                         set_msg(" HOME FAIL ");
241                                                         break;
242                                                 case MOTOR_CURRENT_FAIL:
243                                                         set_msg(" OVER CRNT ");
244                                                         break;
245                                                 case MOTOR_SUCCESS:
246                                                         set_msg("THANK  YOU");
247                                                         break;
248                                                 case MOTOR_NOSLOT:
249                                                         set_msg(" NO MOTOR ");
250                                                         break;
251                                                 default:
252                                                         set_msg("ERRRRRRRR?");
253                                                         break;
254                                         }
255
256                                         display_buf[0] = ' ';
257                                         display_buf[1] = ' ';
258                                         cur_motor[0] = 0xff;
259                                         delay(500);
260                                 } else {
261                                         cur_motor[0] = last_key;
262                                         display_buf[0] = (last_key%10)+'0';
263                                         set_msg(display_buf);
264                                 }
265                         }
266                         send_keypress(last_key);
267                 }
268
269                 /*
270                 if (coin_value != last_coin_value) {
271                         send_balance();
272                         last_coin_value = coin_value;
273                 }
274                 */
275         }
276 }

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