Lots of changes! Takes us to rom S
[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                 delay(1);
28
29                 /* clock pulse */
30                 bset((void*)&_io_ports[M6811_PORTA], PORTA_MOTOR_CLOCK);
31                 delay(1);
32                 bclr((void*)&_io_ports[M6811_PORTA], PORTA_MOTOR_CLOCK);
33                 delay(1);
34                 
35                 data = data << 1;
36         }
37 }
38
39 void set_motor(u8 slot) {
40         u8 row, col;
41         row = slot%10;
42         col = slot/10;
43         if (row >= 5) row--; 
44
45         /* loads up the shift register with the right bits */
46         bclr_misc_output(A3800_MOTOR_COL8_ENABLE|A3800_MOTOR_COL9_ENABLE);
47         switch (col) {
48                 case 8:
49                         bset_misc_output(A3800_MOTOR_COL8_ENABLE);
50                         motor_shift_send(0);
51                         break;
52                 case 9:
53                         bset_misc_output(A3800_MOTOR_COL9_ENABLE);
54                         motor_shift_send(0);
55                         break;
56                 default: /* < 8 */
57                         motor_shift_send(1 << col); /* cols from 0..7 */
58                         bclr((void*)&_io_ports[M6811_PORTA], PORTA_MOTOR_COL_DISABLE);
59         }
60
61         motor_shift_send(1 << (row-1)); /* rows from 1..8 here */
62
63         bclr((void*)&_io_ports[M6811_PORTA], PORTA_MOTOR_CLOCK);
64 }
65
66 void motor_start() {
67         bclr_changer_output(A3000_MOTOR_ROW_DISABLE);
68 }
69
70 void motor_pause() {
71         bset_changer_output(A3000_MOTOR_ROW_DISABLE);
72 }
73
74 void motor_stop() {
75         bset_changer_output(A3000_MOTOR_ROW_DISABLE);
76         bset((void*)&_io_ports[M6811_PORTA], PORTA_MOTOR_COL_DISABLE);
77         bclr_misc_output(A3800_MOTOR_COL8_ENABLE | A3800_MOTOR_COL9_ENABLE);
78 }
79
80 bool motor_here(u8 slot) {
81         u8 i, c = 0;
82         set_motor(slot);
83         for (i=0; i < 8; i++) {
84                 motor_start();
85                 delay(5);
86                 if ((_io_ports[M6811_PORTE] & PORTE_MOTOR_OVERVOLTAGE) == 0) {
87                         c++;
88                         if (c == 3) {
89                                 motor_stop();
90                                 return 1;
91                         }
92                 }
93                 motor_pause();
94         }
95         motor_stop();
96         return 0;
97 }
98
99 bool is_motor(u8 slot) {
100         /* FIXME - does more need to be done? */
101         return motor_here(slot);
102 }
103
104 bool start_motor(u8 slot) {
105         u16 i;
106         u8 r = slot%10;
107         if (r >= 5) r--; 
108         r = 1 << (r-1);
109         set_motor(slot);
110         motor_start();
111         delay(50);
112         for (i = 0; i < 1000; i++) {
113                 if ((home_sensors & r) != 0) return 1;
114                 delay(1);
115         }
116         /* it never left */
117         motor_stop();
118         return 0;
119 }
120
121 bool back_home(u8 slot) {
122         u8 i, r = slot%10;
123         if (r >= 5) r--; 
124         r = 1 << (r-1);
125
126         for (i = 0; i < 5; i++) {
127                 if ((home_sensors & r) == 0) return 1;
128                 delay(1);
129         }
130
131         /* it never arrived */
132         return 0;
133 }
134
135 #define is_overcurrent() ((_io_ports[M6811_PORTE] & PORTE_MOTOR_NOT_OVERCURRENT)==0)
136 bool motor_overcurrent() {
137         u8 good_passes = 0, t;
138         for (t = 0; t < 8; t++) {
139                 delay(1);
140                 if (is_overcurrent()) continue;
141                 t = 0;
142                 good_passes++;
143                 if (good_passes == 5) return 0;
144         }
145         return 1;
146 }
147
148 u8 dispense_motor(u8 slot) {
149         if (!is_motor(slot)) return MOTOR_NOSLOT;
150         if (!start_motor(slot)) return MOTOR_HOME_FAIL;
151         delay(100);
152
153         while (1) {
154                 if (motor_overcurrent()) {
155                         motor_stop();
156                         return MOTOR_CURRENT_FAIL;
157                 }
158                 if (back_home(slot)) {
159                         motor_stop();
160                         return MOTOR_SUCCESS;
161                 }
162         }
163 }
164

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