moved raspberry and router gpio to pi_as_powerwitch on github
[svn42.git] / firmware / tuer.pde
index 0f2246c..0fea4da 100644 (file)
@@ -20,6 +20,14 @@ byte next_led = 0;
 #define LIMIT_OPENED_PIN 18 // A4: limit switch for open
 #define LIMIT_CLOSED_PIN 19 // A5: limit switch for close
 
+#define AJAR_PIN 14 // input pin for reed relais (door ajar/shut)
+#define SHUT 10
+#define AJAR 5
+byte ajar_last_state = SHUT;
+#define AJAR_LOW_PASS_TAU 200
+byte ajar_low_pass_counter = 0;
+byte ajar_low_pass_last_value = ajar_last_state;
+
 #define MANUAL_OPEN_PIN 12  // keys for manual open and close
 #define MANUAL_CLOSE_PIN 13 // 
 #define DEBOUNCE_DELAY 6250  // * 16us = 100ms
@@ -77,6 +85,28 @@ boolean is_closed()
 
 //**********//
 
+byte get_ajar_status()
+{
+  byte b = (digitalRead(AJAR_PIN) == LOW) ? SHUT : AJAR;
+  ajar_low_pass_counter = (b == ajar_low_pass_last_value) ? ajar_low_pass_counter+1 : 0;
+  ajar_low_pass_last_value = b;
+  if(ajar_low_pass_counter >= AJAR_LOW_PASS_TAU) {
+    ajar_low_pass_counter = 0;
+    return b;
+  }
+  else
+    return ajar_last_state;
+}
+
+void init_ajar()
+{
+  pinMode(AJAR_PIN, INPUT);      // set pin to input
+  digitalWrite(AJAR_PIN, HIGH);  // turn on pullup resistors  
+  ajar_last_state = get_ajar_status();
+}
+
+//**********//
+
 void init_manual()
 {
   pinMode(MANUAL_OPEN_PIN, INPUT);      // set pin to input
@@ -369,7 +399,11 @@ ISR(TIMER1_COMPA_vect)
       Serial.print("opened");
     else if(is_closed())
       Serial.print("closed");
-    Serial.println(", idle");
+    Serial.print(", idle");
+    if(get_ajar_status() == SHUT)
+      Serial.println(", shut");
+    else
+      Serial.println(", ajar");
     return;
   }
   else if(current_state == ERROR) {
@@ -482,7 +516,7 @@ void start_close()
   start_step_timer();
 }
 
-void print_status()
+void print_status(byte as)
 {
   Serial.print("Status: ");
   if(is_opened())
@@ -493,13 +527,16 @@ void print_status()
     Serial.print("<->");
 
   switch(current_state) {
-  case IDLE: Serial.println(", idle"); break;
-  case OPENING: Serial.println(", opening"); break;
-  case CLOSING: Serial.println(", closing"); break;
-  case WAIT: Serial.println(", waiting"); break;
-  default: Serial.println(", <undefined state>"); break;
+  case IDLE: Serial.print(", idle"); break;
+  case OPENING: Serial.print(", opening"); break;
+  case CLOSING: Serial.print(", closing"); break;
+  case WAIT: Serial.print(", waiting"); break;
+  default: Serial.print(", <undefined state>"); break;
   }
-  
+  if(as == SHUT)
+    Serial.println(", shut");
+  else
+    Serial.println(", ajar");
 }
 
 //**********//
@@ -507,6 +544,7 @@ void print_status()
 void setup()
 {
   init_limits();
+  init_ajar();
   init_stepper();
   init_leds();
   init_heartbeat();
@@ -523,6 +561,7 @@ void setup()
     current_state = CLOSING;
     start_step_timer();
   }
+  Serial.println("init complete");
 }
 
 void loop()
@@ -538,8 +577,8 @@ void loop()
     }
     else if (command == CMD_OPEN) {
       if(current_state == IDLE) {
-       if(is_opened())
-         Serial.println("Already open");
+        if(is_opened())
+          Serial.println("Already open");
         else {
           start_open();
           Serial.println("Ok");
@@ -550,8 +589,8 @@ void loop()
     }
     else if (command == CMD_CLOSE) {
       if(current_state == IDLE) {
-       if(is_closed())
-         Serial.println("Already closed");
+        if(is_closed())
+          Serial.println("Already closed");
         else {
           start_close();
           Serial.println("Ok");
@@ -562,17 +601,17 @@ void loop()
     }
     else if (command == CMD_TOGGLE) {
       if(current_state == IDLE) {
-       if(is_closed())
-         start_open();
-       else
-         start_close();
-       Serial.println("Ok");
+        if(is_closed())
+          start_open();
+        else
+          start_close();
+        Serial.println("Ok");
       }
       else
         Serial.println("Error: Operation in progress");
     }
     else if (command == CMD_STATUS)
-      print_status();
+      print_status(get_ajar_status());
     else
       Serial.println("Error: unknown command");
   }
@@ -584,7 +623,7 @@ void loop()
     Serial.println("close forced manually");
     start_close();
   }
-  if (current_state == IDLE) {
+  if(current_state == IDLE) {
     if(is_opened()) {
       leds_green();
       PORTD = LEDS_ON;
@@ -594,4 +633,9 @@ void loop()
       PORTD = LEDS_ON;
     }
   }
+  byte a = get_ajar_status();
+  if(a != ajar_last_state) {
+    print_status(a);
+    ajar_last_state = a;
+  }
 }