#include "motors.h"
#include "vend.h"
+void motor_shift_send(u8 data) {
+ u8 i;
+ /* load it in, MSB first */
+ for (i = 0; i < 8; i++) {
+ if (data & 0x80)
+ bset_misc_output(A3800_MOTOR_DATA);
+ else
+ bclr_misc_output(A3800_MOTOR_DATA);
+
+ /* clock pulse */
+ bset((void*)&_io_ports[M6811_PORTA], PORTA_MOTOR_CLOCK);
+ bclr((void*)&_io_ports[M6811_PORTA], PORTA_MOTOR_CLOCK);
+
+ data = data << 1;
+ }
+}
+
void motor_on(u8 slot) {
- /* from jump34 */
+ u8 row, col;
+ row = slot%10;
+ col = slot/10;
+ if (row >= 5) row--;
+
+ /* loads up the shift register with the right bits */
+ bclr_misc_output(A3800_MOTOR_COL8_ENABLE|A3800_MOTOR_COL9_ENABLE);
+ switch (col) {
+ case 8:
+ bset_misc_output(A3800_MOTOR_COL8_ENABLE);
+ motor_shift_send(0);
+ break;
+ case 9:
+ bset_misc_output(A3800_MOTOR_COL9_ENABLE);
+ motor_shift_send(0);
+ break;
+ default: /* < 8 */
+ motor_shift_send(1 << col); /* cols from 0..7 */
+ }
+
+ motor_shift_send(1 << (row-1)); /* rows from 1..8 here */
+
+ bclr((void*)&_io_ports[M6811_PORTA], PORTA_MOTOR_CLOCK);
+ bclr_changer_output(A3000_MOTOR_ROW_DISABLE);
}
void motors_off() {
- bset_changer_output(A3000_MOTOR_ROW_ENABLE); /* XXX active low? */
+ bset_changer_output(A3000_MOTOR_ROW_DISABLE);
delay(10); /* XXX cf motors_off */
bset((void*)&_io_ports[M6811_PORTA], PORTA_MOTOR_COL_DISABLE);
bclr_misc_output(A3800_MOTOR_COL8_ENABLE | A3800_MOTOR_COL9_ENABLE);
}
bool motor_here(u8 slot) {
- int i, c = 0;
+ u8 i, c = 0;
motor_on(slot);
for (i=0; i < 8; i++) {
+ delay(5);
if (_io_ports[M6811_PORTE] & PORTE_MOTOR_OVERVOLTAGE) {
c++;
if (c == 0xff) {
}
bool is_motor(u8 slot) {
- /* FIXME */
- return 1;
+ /* FIXME - does more need to be done? */
+ return motor_here(slot);
}
-u8 dispense_motor(u8 slot) {
- if (!is_motor(slot)) {
- return MOTOR_NOSLOT;
+bool left_home(u8 slot) {
+ u8 i, r = slot%10;
+ if (r >= 5) r--;
+ r = 1 << r;
+
+ for (i = 0; i < 5; i++)
+ if (home_sensors & r == 0) return 1;
+
+ /* it never left */
+ return 0;
+}
+
+bool back_home(u8 slot) {
+ u8 i, r = slot%10;
+ if (r >= 5) r--;
+ r = 1 << r;
+
+ for (i = 0; i < 5; i++) {
+ if (home_sensors & r) return 1;
+ if (_io_ports[M6811_PORTE] & PORTE_MOTOR_OVERCURRENT == 0) return 1;
}
- motor_on(slot);
- /* FIXME */
- return MOTOR_SUCCESS;
+ /* it never left */
+ return 0;
}
-void scan_motors() {
- /* FIXME */
+bool motor_overcurrent() {
+ u8 t = 0, i = 0;
+ while(1) {
+ t++;
+ if (7 == t) return 1;
+ if (_io_ports[M6811_PORTE] & PORTE_MOTOR_OVERCURRENT) continue;
+ t = 0;
+ i++;
+ if (5 == i) return 0;
+ }
+}
+
+u8 dispense_motor(u8 slot) {
+ if (!is_motor(slot)) return MOTOR_NOSLOT;
+
+ motor_on(slot);
+
+ if (!left_home(slot)) {
+ motors_off();
+ return MOTOR_HOME_FAIL;
+ }
+
+ while (1) {
+ if (motor_overcurrent()) {
+ motors_off();
+ return MOTOR_CURRENT_FAIL;
+ }
+ /* something should call motor_here? */
+ if (back_home(slot)) {
+ motors_off();
+ return MOTOR_SUCCESS;
+ }
+ }
}
#include "vend.h"
/* error codes for dispense_motor */
-#define MOTOR_SUCCESS 0
-#define MOTOR_NOSLOT 1
+#define MOTOR_SUCCESS 0
+#define MOTOR_NOSLOT 1
+#define MOTOR_HOME_FAIL 2
+#define MOTOR_CURRENT_FAIL 3
+#define MOTOR_VOLTAGE_FAIL 4
const u8 motor_lookup[80] =
{ 1,12,23,34,46,57,68,79,
bool is_motor(u8 slot);
u8 dispense_motor(u8 slot);
-void scan_motors();
#endif /* _MOTORS_H_ */
buffered_addr(changer_output);
buffered_addr(misc_output);
-extern volatile u8 _switch_input;
-#define switch_input _switch_input
-extern volatile u8 _misc_input;
-#define misc_input _misc_input
-extern volatile u8 _home_sensors;
-#define home_sensors _home_sensors
+extern volatile u8* _switch_input;
+#define switch_input (*_switch_input)
+extern volatile u8* _misc_input;
+#define misc_input (*_misc_input)
+extern volatile u8* _home_sensors;
+#define home_sensors (*_home_sensors)
/******* from helpers.c *******/
void delay(u16 ms);
/******** Some meaningful bits ******/
#define PORTA_CHIME 0x10 /* chime is on when set */
+#define PORTA_MOTOR_CLOCK 0x20
#define PORTA_MOTOR_COL_DISABLE 0x40
#define PORTA_DISP_RESET 0x80 /* active low */
#define PORTD_KEYPAD_ROW 0x20 /* clear for row 0, set for row 1 */
+#define PORTE_MOTOR_OVERCURRENT 0x01
#define PORTE_MOTOR_OVERVOLTAGE 0x02
/* Address 3000 bits */
-#define A3000_MOTOR_ROW_ENABLE 0x80
+#define A3000_MOTOR_ROW_DISABLE 0x80
/* Address 1800 bits */
#define A1800_DOOR_OPEN 0x20
/* Address 3800 bits */
#define A3800_DISPLAY_WRITE 0x04
+#define A3800_MOTOR_DATA 0x10
#define A3800_MOTOR_COL8_ENABLE 0x20
#define A3800_MOTOR_COL9_ENABLE 0x40