Accept both CR and LF to denote newline.
[uccvend-snackrom.git] / ROM2 / motors.c
index 64158ae..e7cb6b5 100644 (file)
@@ -73,19 +73,18 @@ void motors_off() {
 
 bool motor_here(u8 slot) {
        u8 i, c = 0;
-       motor_on(slot);
        for (i=0; i < 8; i++) {
+               motor_on(slot);
                delay(5);
-               if (_io_ports[M6811_PORTE] & PORTE_MOTOR_OVERVOLTAGE) {
+               if ((_io_ports[M6811_PORTE] & PORTE_MOTOR_OVERVOLTAGE) == 0) {
                        c++;
-                       if (c == 0xff) {
+                       if (c == 3) {
                                motors_off();
                                return 1;
-                       } else
-                               continue;
+                       }
                }
+               motors_off();
        }
-       motors_off();
        return 0;
 }
 
@@ -94,15 +93,19 @@ bool is_motor(u8 slot) {
        return motor_here(slot);
 }
 
-bool left_home(u8 slot) {
-       u8 i, r = slot%10;
+bool start_motor(u8 slot) {
+       u16 i;
+       u8 r = slot%10;
        if (r >= 5) r--; 
        r = 1 << (r-1);
-
-       for (i = 0; i < 5; i++)
-               if ((home_sensors & r) == 0) return 1;
-
+       motor_on(slot);
+       delay(100);
+       for (i = 0; i < 1000; i++) {
+               if ((home_sensors & r) != 0) return 1;
+               delay(1);
+       }
        /* it never left */
+       motors_off();
        return 0;
 }
 
@@ -112,36 +115,30 @@ bool back_home(u8 slot) {
        r = 1 << (r-1);
 
        for (i = 0; i < 5; i++) {
-               if (home_sensors & r) return 1;
-               if ((_io_ports[M6811_PORTE] & PORTE_MOTOR_OVERCURRENT) == 0) return 1;
+               if ((home_sensors & r) == 0) return 1;
+               delay(1);
        }
 
-       /* it never left */
+       /* it never arrived */
        return 0;
 }
 
+#define is_overcurrent() ((_io_ports[M6811_PORTE] & PORTE_MOTOR_NOT_OVERCURRENT)==0)
 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;
+       u8 good_passes = 0, t;
+       for (t = 0; t < 8; t++) {
+               delay(1);
+               if (is_overcurrent()) continue;
                t = 0;
-               i++;
-               if (5 == i) return 0;
+               good_passes++;
+               if (good_passes == 5) return 0;
        }
+       return 1;
 }
 
 u8 dispense_motor(u8 slot) {
-       //if (!is_motor(slot)) return MOTOR_NOSLOT;
-
-       motor_on(slot);
-       delay(100);
-       
-       if (!left_home(slot)) {
-               motors_off();
-               return MOTOR_HOME_FAIL;
-       }
+       if (!is_motor(slot)) return MOTOR_NOSLOT;
+       if (!start_motor(slot)) return MOTOR_HOME_FAIL;
 
        while (1) {
                if (motor_overcurrent()) {

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