UART code for a 16550.
[uccvend-snackrom.git] / ROM2 / main.c
1 #include "display.h"
2 #include "keypad.h"
3 #include "chime.h"
4 #include "server.h"
5 #include "coinmech.h"
6 #include "vend.h"
7
8 u16 uid;
9 u16 pin;
10 u8 selection;
11
12 u16 scroll_timer;
13
14 bool uid_enter() {
15         u8 uidpos;
16         /* the user has started to type in his/her UID. grab the last key and continue
17          * reading. Returns true if a UID was entered successfully.
18          * The UID will be entered as 5 digits. (shorter uids should be pre-padded
19          * with zeros)
20          */
21         uid = last_key%10; /* 10 => 0, it was _not_ reset (hopefuly :) */
22         set_msg("UID?      ", WRAP_NONE);
23         set_char((last_key%10)+'0', 5);
24
25         for (uidpos = 2; uidpos <= 5; uidpos++) {
26                 keypad_getkey();
27                 if (last_key == KEY_RESET) {
28                         uid = 0;
29                         return 0;
30                 }
31                 uid = (uid*10) + (last_key%10);
32                 set_char((last_key%10)+'0', 4+uidpos);
33         }
34         return (uid!=0);
35 }
36
37 bool pin_enter() {
38         u8 pinpos;
39         /* We ask for a pin, display a PIN: prompt. PINs must be a 4 digit number.
40          * Strictly, they must be 16-bit, but it's easier to use the guarantee that
41          * 4-digits numbers are < 65536
42          *
43          * Also display X's on screen as the pin is entered.
44          */
45         pin = 0;
46         set_msg("PIN?      ", WRAP_NONE);
47         for (pinpos = 1; pinpos <= 4; pinpos++) {
48                 keypad_getkey();
49                 if (last_key == KEY_RESET) {
50                         pin = 0;
51                         return 0;
52                 }
53                 pin = (pin*10) + (last_key%10);
54                 set_char('X', 4+pinpos);
55         }
56         return 1;
57 }
58
59 void make_request(u8 selection) {
60         set_msg("REQUESTING", WRAP_NONE); /* XXX: maybe this isn't needed? */
61         switch(server_request(uid, pin, selection)) {
62                 case REQUEST_OK:
63                         set_msg("THANK YOU!", WRAP_NONE);
64                         break;
65                 case REQUEST_NO_MONEY:
66                         set_msg("NO MONEY!", WRAP_NONE);
67                         break;
68                 case REQUEST_SERVFAIL:
69                         set_msg("SERV FAIL!", WRAP_NONE);
70                         break;
71                 case REQUEST_EMPTY:
72                         set_msg("NONE LEFT!", WRAP_NONE);
73                         break;
74                 case REQUEST_INVAL:
75                         set_msg(" BAD SELN ", WRAP_NONE);
76                         break;
77         }
78         delay(1000);
79 }
80
81 void selection_menu() {
82         /* we have a valid username & PIN number */
83         /* either ask for a 2-digit selection, or wait for coins to be entered */
84         /* get the username somehow? */
85         set_msg("ENTER SELECTION OR INSERT COINS  ", WRAP_SCROLL);
86         selection = 0;
87         while(1) {
88                 if (coin_value) { /* we have coins inserted */
89                         int prev_coin = 0;
90                         /* alternate between the price and a confirm message */
91                         while (coin_value) {
92                                 if (prev_coin != coin_value) {
93                                         print_amount(coin_value);
94                                         append_msg("0: CONFIRM", WRAP_ALTERNATE);
95                                         prev_coin = coin_value;
96                                 }
97                                 keypad_read();
98                                 if (keypad_pressed()) {
99                                         switch (last_key) {
100                                                 case KEY_RESET:
101                                                         scroll_msg("PRESS COIN REFUND");
102                                                         while (coin_value);
103                                                         break;
104                                                 case KEY_0:
105                                                         switch (server_credit_account(uid, pin, coin_value)) {
106                                                                 case CREDIT_OK:
107                                                                         coin_eat();
108                                                                         set_msg(" SUCCESS! ", WRAP_NONE);
109                                                                         delay(1000);
110                                                                         break;
111                                                                 case CREDIT_FAIL:
112                                                                         set_msg(" FAILED!  " "PRESS COIN" "  REFUND  ",
113                                                                                         WRAP_ALTERNATE);
114                                                                         while (coin_value);
115                                                                         break;
116                                                         }
117                                                         break;
118                                         }
119                                 }
120                         }
121                         /* coins were refunded */
122                 }
123
124                 if (selection) { /* half way through a selection */
125                         keypad_read();
126                         if (keypad_pressed()) {
127                                 switch (last_key) {
128                                         case KEY_RESET:
129                                                 selection = 0;
130                                                 break;
131                                         case 0:
132                                                 break;
133                                         default:
134                                                 selection = selection * 10 + (last_key%10);
135                                                 make_request(selection);
136                                                 selection = 0;
137                                 }
138                         }
139                 } else { /* else listen for the first key of a selection */
140                         keypad_read();
141                         if (keypad_pressed()) {
142                                 switch (last_key) {
143                                         case KEY_RESET:
144                                                 return;
145                                         case 0:
146                                                 break;
147                                         default:
148                                                 selection = last_key%10;
149                                 }
150                         }
151                 }
152         }
153 }
154
155 void load_default_msg() {
156         /* FIXME: want this message to be changeable from the server */
157         set_msg("UNIVERSITY COMPUTER CLUB *** INSERT COINS OR USER-ID *** ",
158                         WRAP_SCROLL);
159 }
160
161 void service_menu() {
162         while (door_open()) { /* don't quit until door is closed */
163
164         }
165 }
166
167 int main() {
168         /* do stuff */
169         set_msg("UNIVERSITY", WRAP_NONE);
170         delay(1000);
171         set_msg(" COMPUTER ", WRAP_NONE);
172         delay(1000);
173         set_msg("   CLUB   ", WRAP_NONE);
174         delay(1000);
175
176         load_default_msg();
177         while(1) {
178                 /* this needs to be a relatively tight loop to make sure we catch
179                  * keypresses at the main menu
180                  */
181                 keypad_read();
182                 if (keypad_pressed()) {
183                         if (last_key != KEY_RESET) {
184                                 if (uid_enter() && pin_enter()) {
185                                         /* authenticate them */
186                                         switch (server_authenticate(uid, pin)) {
187                                                 case AUTH_GOOD:
188                                                         selection_menu();
189                                                         break;
190                                                 case AUTH_BAD:
191                                                         set_msg(" BAD USER ", WRAP_NONE);
192                                                         delay(1000);
193                                                         break;
194                                                 case AUTH_NO_MONEY:
195                                                         set_msg(" NO MONEY ", WRAP_NONE);
196                                                         delay(1000);
197                                                         break;
198                                                 case AUTH_LOCKED:
199                                                         set_msg("YOUR ACCOUNT IS LOCKED", WRAP_SCROLL);
200                                                         delay(1000);
201                                                         break;
202                                         }
203                                 }
204                                 uid = 0;
205                                 pin = 0;
206                                 /* move on */
207                         }
208                         load_default_msg();
209                 }
210
211                 /* test door switch   */
212                 if (door_open()) {
213                         service_menu();
214                         load_default_msg();
215                 }
216
217                 /* check on coin mech */
218         }
219 }
220
221 void _start() {
222         /* Initialize Stuff.  Particularly some memory locations that can only be
223          * written in the first 64 clock cycles upon reset.
224          */
225         display_init();
226         set_bus_expanded();
227         /* enable RTI & set rate */
228         /* init coin mech */
229         main();
230 }
231
232 void rti() {
233         chime(); /* turn chime on or of as need be */
234
235         /* scroll the display if need be too */
236         if (scroll_timer == 0) {
237                 display_shift();
238                 scroll_timer = SCROLL_TIME;
239         }
240         scroll_timer--;
241 }

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