3fd99a2bf5077081b4ef8517e1bc86c39b39737b
[uccvend-snackrom.git] / ROM2 / motors.c
1 #include "motors.h"
2 #include "vend.h"
3
4 void motor_shift_send(u8 data) {
5         u8 i;
6         /* load it in, MSB first */
7         for (i = 0; i < 8; i++) {
8                 if (data & 0x80)
9                         bset_misc_output(A3800_MOTOR_DATA);
10                 else
11                         bclr_misc_output(A3800_MOTOR_DATA);
12
13                 /* clock pulse */
14                 bset((void*)&_io_ports[M6811_PORTA], PORTA_MOTOR_CLOCK);
15                 bclr((void*)&_io_ports[M6811_PORTA], PORTA_MOTOR_CLOCK);
16                 
17                 data = data << 1;
18         }
19 }
20
21 void motor_on(u8 slot) {
22         u8 row, col;
23         row = slot%10;
24         col = slot/10;
25         if (row >= 5) row--; 
26
27         /* loads up the shift register with the right bits */
28         bclr_misc_output(A3800_MOTOR_COL8_ENABLE|A3800_MOTOR_COL9_ENABLE);
29         switch (col) {
30                 case 8:
31                         bset_misc_output(A3800_MOTOR_COL8_ENABLE);
32                         motor_shift_send(0);
33                         break;
34                 case 9:
35                         bset_misc_output(A3800_MOTOR_COL9_ENABLE);
36                         motor_shift_send(0);
37                         break;
38                 default: /* < 8 */
39                         motor_shift_send(1 << col); /* cols from 0..7 */
40         }
41
42         motor_shift_send(1 << (row-1)); /* rows from 1..8 here */
43
44         bclr((void*)&_io_ports[M6811_PORTA], PORTA_MOTOR_CLOCK);
45         bclr_changer_output(A3000_MOTOR_ROW_DISABLE);
46 }
47
48 void motors_off() {
49         bset_changer_output(A3000_MOTOR_ROW_DISABLE);
50         delay(10); /* XXX cf motors_off */
51         bset((void*)&_io_ports[M6811_PORTA], PORTA_MOTOR_COL_DISABLE);
52         bclr_misc_output(A3800_MOTOR_COL8_ENABLE | A3800_MOTOR_COL9_ENABLE);
53 }
54
55 bool motor_here(u8 slot) {
56         u8 i, c = 0;
57         motor_on(slot);
58         for (i=0; i < 8; i++) {
59                 delay(5);
60                 if (_io_ports[M6811_PORTE] & PORTE_MOTOR_OVERVOLTAGE) {
61                         c++;
62                         if (c == 0xff) {
63                                 motors_off();
64                                 return 1;
65                         } else
66                                 continue;
67                 }
68         }
69         motors_off();
70         return 0;
71 }
72
73 bool is_motor(u8 slot) {
74         /* FIXME - does more need to be done? */
75         return motor_here(slot);
76 }
77
78 bool left_home(u8 slot) {
79         u8 i, r = slot%10;
80         if (r >= 5) r--; 
81         r = 1 << r;
82
83         for (i = 0; i < 5; i++)
84                 if ((home_sensors & r) == 0) return 1;
85
86         /* it never left */
87         return 0;
88 }
89
90 bool back_home(u8 slot) {
91         u8 i, r = slot%10;
92         if (r >= 5) r--; 
93         r = 1 << r;
94
95         for (i = 0; i < 5; i++) {
96                 if (home_sensors & r) return 1;
97                 if ((_io_ports[M6811_PORTE] & PORTE_MOTOR_OVERCURRENT) == 0) return 1;
98         }
99
100         /* it never left */
101         return 0;
102 }
103
104 bool motor_overcurrent() {
105         u8 t = 0, i = 0;
106         while(1) {
107                 t++;
108                 if (7 == t) return 1;
109                 if (_io_ports[M6811_PORTE] & PORTE_MOTOR_OVERCURRENT) continue;
110                 t = 0;
111                 i++;
112                 if (5 == i) return 0;
113         }
114 }
115
116 u8 dispense_motor(u8 slot) {
117         if (!is_motor(slot)) return MOTOR_NOSLOT;
118
119         motor_on(slot);
120         
121         if (!left_home(slot)) {
122                 motors_off();
123                 return MOTOR_HOME_FAIL;
124         }
125
126         while (1) {
127                 if (motor_overcurrent()) {
128                         motors_off();
129                         return MOTOR_CURRENT_FAIL;
130                 }
131                 /* something should call motor_here? */
132                 if (back_home(slot)) {
133                         motors_off();
134                         return MOTOR_SUCCESS;
135                 }
136         }
137 }
138

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