Reference manual for M68HC11
[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 "comm.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         tx_buffer[0] = 'M';
19         tx_buffer[1] = code + '0';
20         tx_buffer[2] = *slotptr;
21         tx_buffer[3] = *(slotptr+1);
22         tx_buffer[4] = '\n';
23         tx_buffer[5] = 0;
24         send_packet();
25 }
26
27 void dispense_something() {
28         /* process a message VXX in msg_buf where XX is motor number */
29         u8 slot;
30
31         if ((msg_buf[1] < '0') || (msg_buf[1] > '9') ||
32                 (msg_buf[2] < '0') || (msg_buf[2] > '9')) {
33                 msg_buf[1] = msg_buf[2] = '0';
34                 motor_reply((char*)&msg_buf[1], MOTOR_NOSLOT);
35                 return;
36         }
37
38         slot = (msg_buf[1] - '0') * 10;
39         slot += msg_buf[2] - '0';
40
41         motor_reply((char*)&msg_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 (msg_buf[i+1])
50                         buf[i] = msg_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         tx_buffer[0] = 'C';
64         tx_buffer[1] = have_change?'0':'1';
65         tx_buffer[2] = (coin_value/10000)%10;
66         tx_buffer[3] = (coin_value/1000)%10;
67         tx_buffer[4] = (coin_value/100)%10;
68         tx_buffer[5] = (coin_value/10)%10;
69         tx_buffer[6] = coin_value%10;
70         tx_buffer[7] = '\n';
71         tx_buffer[8] = 0;
72         send_packet();
73 }
74
75 void give_change() {
76         u16 cost;
77
78         if ((msg_buf[1] < '0') || (msg_buf[1] > '9') ||
79                 (msg_buf[2] < '0') || (msg_buf[2] > '9') ||
80                 (msg_buf[3] < '0') || (msg_buf[3] > '9') ||
81                 (msg_buf[4] < '0') || (msg_buf[4] > '9') ||
82                 (msg_buf[5] < '0') || (msg_buf[5] > '9')) {
83                 send_nack();
84         }
85         cost = msg_buf[1] - '0';
86         cost *= 10; cost = msg_buf[2] - '0';
87         cost *= 10; cost = msg_buf[3] - '0';
88         cost *= 10; cost = msg_buf[4] - '0';
89         cost *= 10; cost = msg_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         tx_buffer[0] = 'K';
99         if (key == KEY_RESET)
100                 tx_buffer[1] = 'R';
101         else
102                 tx_buffer[1] = (key%10)+'0';
103         tx_buffer[2] = '\n';
104         tx_buffer[3] = 0;
105         send_packet();
106 }
107
108 void send_door_msg(bool open) {
109         wait_for_tx_free();
110         tx_buffer[0] = 'D';
111         tx_buffer[1] = open?'1':'0';
112         tx_buffer[2] = '\n';
113         tx_buffer[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 (msg_buf[1] != 'I' ||
125                 msg_buf[2] != 'N' ||
126                 msg_buf[3] != 'G') {
127                 send_nack();
128                 return;
129         }
130         /* respond with ack & pong */
131         wait_for_tx_free();
132         tx_buffer[0] = 'P';
133         tx_buffer[1] = 'O';
134         tx_buffer[2] = 'N';
135         tx_buffer[3] = 'G';
136         tx_buffer[4] = '\n';
137         tx_buffer[5] = 0;
138         send_packet();
139 }
140
141 u16 last_coin_value;
142 bool last_door_open;
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. */
146 u8 cur_motor[2];
147
148 int main() {
149         u8 i;
150         for (i = 0; i < 11; i++)
151                 display_buf[i] = ' ';
152         display_buf[10] = '\0';
153
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] = 0x12;
159         set_misc_output(0x00);
160         
161         display_init();
162         set_msg(" HELLO    ");
163
164         unlock(); /* enable interrupts */
165
166         comm_init();
167         coinmech_init();
168         keypad_init();
169         last_coin_value = 0;
170         last_door_open = 0;
171
172         delay(1000);
173
174         set_msg("  CRUEL   ");
175
176         delay(1000);
177
178         set_msg("   WORLD  ");
179         delay(1000);
180
181         chime_start();
182
183         cur_motor[0] = 0xff;
184         while(1) {
185                 if (cur_motor[0] == 0xff) { /* signal to say redraw screen */
186                         set_msg("*5N4X0RZ* ");
187                         cur_motor[0] = 0;
188                 }
189
190                 if (door_open() != last_door_open) {
191                         last_door_open = door_open();
192                         send_door_msg(last_door_open);
193                         chime_start();
194                         if (last_door_open) {
195                                 set_msg("DOOR OPEND");
196                         } else {
197                                 set_msg("DOOR CLOSE");
198                         }
199                 }
200
201                 /*
202                 if (rx_queue_state) {
203                         switch (msg_buf[0]) {
204                                 case 'V':
205                                         dispense_something();
206                                         break;
207                                 case 'D':
208                                         write_to_display();
209                                         break;
210                                 case 'B': 
211                                         do_chime();
212                                         break;
213                                 case 'U':
214                                         send_balance();
215                                         break;
216                                 case 'G':
217                                         give_change();
218                                         break;
219                                 case 'P':
220                                         ping_pong();
221                                         break;
222                                 default:
223                                         // shurg
224                                         send_nack();
225                                         break;
226                         }
227                         msg_clr();
228                 }
229                 */
230
231                 keypad_read();
232                 if (keypad_pressed()) {
233                         if (last_key == KEY_RESET) {
234                                 cur_motor[0] = 0xff;
235                         } else {
236                                 if (cur_motor[0]) {
237                                         u8 motor_num;
238                                         cur_motor[1] = last_key%10;
239                                         display_buf[1] = cur_motor[1]+'0';
240                                         set_msg(display_buf);
241
242                                         motor_num = cur_motor[0]%10;
243                                         motor_num *= 10;
244                                         motor_num += cur_motor[1];
245                                         switch (dispense_motor(motor_num)) {
246                                                 case MOTOR_HOME_FAIL:
247                                                         set_msg(" HOME FAIL ");
248                                                         break;
249                                                 case MOTOR_CURRENT_FAIL:
250                                                         set_msg(" OVER CRNT ");
251                                                         break;
252                                                 case MOTOR_SUCCESS:
253                                                         set_msg("THANK  YOU");
254                                                         break;
255                                                 default:
256                                                         set_msg("ERRRRRRRR?");
257                                                         break;
258                                         }
259
260                                         display_buf[0] = ' ';
261                                         display_buf[1] = ' ';
262                                         cur_motor[0] = 0xff;
263                                         delay(500);
264                                 } else {
265                                         cur_motor[0] = last_key;
266                                         display_buf[0] = (last_key%10)+'0';
267                                         set_msg(display_buf);
268                                 }
269                         }
270                         send_keypress(last_key);
271                 }
272
273                 /*
274                 if (coin_value != last_coin_value) {
275                         send_balance();
276                         last_coin_value = coin_value;
277                 }
278                 */
279         }
280 }

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