more progress
[uccvend-snackrom.git] / ROM2 / main.c
1 #include "display.h"
2 #include "keypad.h"
3 #include "chime.h"
4 #include "server.h"
5 #include "vend.h"
6
7 u16 uid;
8 u16 pin;
9
10 u16 scroll_timer;
11
12 bool uid_enter() {
13         u8 uidpos;
14         /* the user has started to type in his/her UID. grab the last key and continue
15          * reading. Returns true if a UID was entered successfully.
16          * The UID will be entered as 5 digits. (shorter uids should be pre-padded
17          * with zeros)
18          */
19         uid = last_key%10; /* 10 => 0, it was _not_ reset (hopefuly :) */
20         set_msg("UID?      ", WRAP_NONE);
21         set_char((last_key%10)+'0', 5);
22
23         for (uidpos = 2; uidpos <= 5; uidpos++) {
24                 keypad_getkey();
25                 if (last_key == KEY_RESET) {
26                         uid = 0;
27                         return 0;
28                 }
29                 uid = (uid*10) + (last_key%10);
30                 set_char((last_key%10)+'0', 4+uidpos);
31         }
32         return 1;
33 }
34
35
36 bool pin_enter() {
37         u8 pinpos;
38         /* We ask for a pin, display a PIN: prompt. PINs must be a 4 digit number.
39          * Strictly, they must be 16-bit, but it's easier to use the guarantee that
40          * 4-digits numbers are < 65536
41          *
42          * Also display X's on screen as the pin is entered.
43          */
44         set_msg("PIN?      ", WRAP_NONE);
45         for (pinpos = 1; pinpos <= 5; pinpos++) {
46                 keypad_getkey();
47                 if (last_key == KEY_RESET) {
48                         pin = 0;
49                         return 0;
50                 }
51                 pin = (pin*10) + (last_key%10);
52                 set_char('X', 4+pinpos);
53         }
54         return 1;
55 }
56
57 void selection_menu() {
58         set_msg("ENTER SELECTION OR INSERT COINS", WRAP_SCROLL);
59 }
60
61 void load_default_msg() {
62         /* FIXME: want this message to be changeable from the server */
63         set_msg("UNIVERSITY COMPUTER CLUB *** INSERT COINS OR USER-ID *** ",
64                         WRAP_SCROLL);
65 }
66
67 void service_menu() {
68         while (door_open()) { /* don't quit until door is closed */
69
70         }
71 }
72
73 int main() {
74         /* do stuff */
75         set_msg("UNIVERSITY", WRAP_NONE);
76         delay(1000);
77         set_msg(" COMPUTER ", WRAP_NONE);
78         delay(1000);
79         set_msg("   CLUB   ", WRAP_NONE);
80         delay(1000);
81
82         load_default_msg();
83         while(1) {
84                 /* this needs to be a relatively tight loop to make sure we catch
85                  * keypresses at the main menu
86                  */
87                 keypad_read();
88                 if (keypad_pressed()) {
89                         if (last_key != KEY_RESET) {
90                                 if (uid_enter() && pin_enter()) {
91                                         /* authenticate them */
92                                         switch (server_authenticate(uid, pin)) {
93                                                 case AUTH_GOOD:
94                                                         selection_menu();
95                                                         break;
96                                                 case AUTH_BAD:
97                                                         set_msg(" BAD USER ", WRAP_NONE);
98                                                         delay(1000);
99                                                         break;
100                                                 case AUTH_NO_MONEY:
101                                                         set_msg(" NO MONEY ", WRAP_NONE);
102                                                         delay(1000);
103                                                         break;
104                                                 case AUTH_LOCKED:
105                                                         set_msg("YOUR ACCOUNT IS LOCKED", WRAP_SCROLL);
106                                                         delay(1000);
107                                                         break;
108                                         }
109                                 }
110                                 uid = 0;
111                                 pin = 0;
112                                 /* move on */
113                         }
114                         load_default_msg();
115                 }
116
117                 /* test door switch   */
118                 if (door_open()) {
119                         service_menu();
120                         load_default_msg();
121                 }
122
123                 /* check on coin mech */
124         }
125 }
126
127 void _start() {
128         /* Initialize Stuff.  Particularly some memory locations that can only be
129          * written in the first 64 clock cycles upon reset.
130          */
131         display_init();
132         set_bus_expanded();
133         /* enable RTI & set rate */
134         /* init coin mech */
135         main();
136 }
137
138 void rti() {
139         chime(); /* turn chime on or of as need be */
140
141         /* scroll the display if need be too */
142         if (scroll_timer == 0) {
143                 display_shift();
144                 scroll_timer = SCROLL_TIME;
145         }
146         scroll_timer--;
147 }

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