firmware: reset with manual keys
[svn42.git] / firmware / tuer.pde
index c0c4bf1..6c966a2 100644 (file)
@@ -22,7 +22,7 @@ byte next_led = 0;
 
 #define MANUAL_OPEN_PIN 12  // keys for manual open and close
 #define MANUAL_CLOSE_PIN 13 // 
-#define DEBOUNCE_DELAY 625  // * 16us = 10ms
+#define DEBOUNCE_DELAY 6250  // * 16us = 100ms
 #define DEBOUNCE_IDLE 0     // currently no debouncing
 #define DEBOUNCE_OPEN 1     // debouncing open key
 #define DEBOUNCE_CLOSE 2    // debouncing close key
@@ -38,6 +38,7 @@ int debounce_cnt = 0;
 
 #define CMD_OPEN 'o'
 #define CMD_CLOSE 'c'
+#define CMD_TOGGLE 't'
 #define CMD_STATUS 's'
 #define CMD_RESET 'r'
 
@@ -109,7 +110,7 @@ void start_debounce_timer()  // this breaks millis() function, but who cares
   debounce_cnt = DEBOUNCE_DELAY;
 
   TCCR0A = 0;         // no prescaler, WGM = 0 (normal)
-  TCCR0B = 1<<WGM01;  // 
+  TCCR0B = 1<<CS00;   // 
   OCR0A = 255;        // 1+255 = 256 -> 16us @ 16 MHz
   TCNT0 = 0;          // reseting timer
   TIMSK0 = 1<<OCF0A;  // enable Interrupt
@@ -125,8 +126,8 @@ void stop_debounce_timer()
 
 ISR(TIMER0_COMPA_vect)
 {
-  if((debounce_state & DEBOUNCE_OPEN && manual_open_pressed()) ||
-     (debounce_state & DEBOUNCE_CLOSE && manual_close_pressed())) {
+  if(((debounce_state & DEBOUNCE_OPEN) && manual_open_pressed()) ||
+     ((debounce_state & DEBOUNCE_CLOSE) && manual_close_pressed())) {
     if(debounce_cnt) {
       debounce_cnt--;
       return;
@@ -155,7 +156,7 @@ boolean manual_open()
       return true;
     }
   }
-  else {
+  else if(debounce_state & DEBOUNCE_OPEN) {
     stop_debounce_timer();
     debounce_state = DEBOUNCE_IDLE;
   }
@@ -182,7 +183,7 @@ boolean manual_close()
       return true;
     }
   }
-  else {
+  else if(debounce_state & DEBOUNCE_CLOSE) {
     stop_debounce_timer();
     debounce_state = DEBOUNCE_IDLE;
   }
@@ -359,6 +360,12 @@ ISR(TIMER1_COMPA_vect)
     stop_timer();
     reset_stepper();
     current_state = IDLE;
+    Serial.print("Status: ");
+    if(is_opened())
+      Serial.print("opened");
+    else if(is_closed())
+      Serial.print("closed");
+    Serial.println(", idle");
     return;
   }
   else if(current_state == ERROR) {
@@ -454,32 +461,20 @@ void reset_after_error()
 
 void start_open()
 {
-  if(is_opened()) {
-    Serial.println("Already open");
-    return;
-  }
-
   reset_stepper();
   reset_leds();
   leds_green();
   current_state = OPENING;
   start_step_timer();
-  Serial.println("Ok");
 }
 
 void start_close()
 {
-  if(is_closed()) {
-    Serial.println("Already closed");
-    return;
-  }
-    
   reset_stepper();
   reset_leds();
   leds_red();
   current_state = CLOSING;
   start_step_timer();
-  Serial.println("Ok");
 }
 
 void print_status()
@@ -531,20 +526,43 @@ void loop()
     char command = Serial.read();
 
     if(current_state == ERROR && command != CMD_RESET) {
-      Serial.println("Error: last open/close operation took to long!");
+      Serial.println("Error: last open/close operation took too long!");
     }
     else if (command == CMD_RESET) {
       reset_after_error();
     }
     else if (command == CMD_OPEN) {
-      if(current_state == IDLE) 
-        start_open();
+      if(current_state == IDLE) {
+       if(is_opened())
+         Serial.println("Already open");
+        else {
+          start_open();
+          Serial.println("Ok");
+        }
+      }
       else
         Serial.println("Error: Operation in progress");
     }
     else if (command == CMD_CLOSE) {
-      if(current_state == IDLE) 
-        start_close();
+      if(current_state == IDLE) {
+       if(is_closed())
+         Serial.println("Already closed");
+        else {
+          start_close();
+          Serial.println("Ok");
+        }
+      }
+      else
+        Serial.println("Error: Operation in progress");
+    }
+    else if (command == CMD_TOGGLE) {
+      if(current_state == IDLE) {
+       if(is_closed())
+         start_open();
+       else
+         start_close();
+       Serial.println("Ok");
+      }
       else
         Serial.println("Error: Operation in progress");
     }
@@ -553,11 +571,11 @@ void loop()
     else
       Serial.println("Error: unknown command");
   }
-  if(manual_open() && !is_opened() && current_state == IDLE) {
+  if(manual_open() && !is_opened() && (current_state == IDLE || current_state == ERROR)) {
     Serial.println("open forced manually");
     start_open();
   }
-  if(manual_close() && !is_closed() && current_state == IDLE) {
+  if(manual_close() && !is_closed() && (current_state == IDLE || current_state == ERROR)) {
     Serial.println("close forced manually");
     start_close();
   }