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

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