new command codes for power switches
[svn42.git] / rf433ctl / rf433ctl.pde
index a00e914..9837f3e 100644 (file)
@@ -1,10 +1,24 @@
 #include <avr/io.h>
 #include <avr/interrupt.h>
 #include <inttypes.h>
+#include <OneWire.h>
+#include <DallasTemperature.h>
 
 //********************************************************************//
 
-#define DATA_OUT_PIN 13
+#define RF_DATA_OUT_PIN 13
+#define IR_MOVEMENT_PIN 9
+#define ONE_WIRE_PIN 8
+#define PANIC_BUTTON_PIN 7
+//movement is reported if during IR_SAMPLE_DURATION at least IR_TRESHOLD ir signals are detectd
+#define IR_SAMPLE_DURATION 20000
+#define IR_TRESHOLD 13000
+//duration PanicButton needs to be pressed before status change occurs (i.e. for two PanicButton Reports, the buttons needs to be pressed 1000 cycles, releases 1000 cycles and again pressed 1000 cycles)
+#define PB_TRESHOLD 1000
+
+OneWire  onewire(ONE_WIRE_PIN);
+DallasTemperature dallas_sensors(&onewire);
+DeviceAddress onShieldTemp = { 0x10, 0xE7, 0x77, 0xD3, 0x01, 0x08, 0x00, 0x3F };
 
 typedef struct {
   byte offset;
@@ -122,9 +136,9 @@ void init_word(const word_t w)
   bit_cnt = 0;
 
   if(bit_defs[current_word[bit_cnt]][chunk_cnt].state)
-    digitalWrite(DATA_OUT_PIN, HIGH);
+    digitalWrite(RF_DATA_OUT_PIN, HIGH);
   else
-    digitalWrite(DATA_OUT_PIN, LOW);
+    digitalWrite(RF_DATA_OUT_PIN, LOW);
 
   start_timer();
 }
@@ -138,9 +152,9 @@ ISR(TIMER1_COMPA_vect)
   chunk_cnt++;
   if(bit_defs[current_word[bit_cnt]][chunk_cnt].offset != 0) {
     if(bit_defs[current_word[bit_cnt]][chunk_cnt].state)
-      digitalWrite(DATA_OUT_PIN, HIGH);
+      digitalWrite(RF_DATA_OUT_PIN, HIGH);
     else
-      digitalWrite(DATA_OUT_PIN, LOW);
+      digitalWrite(RF_DATA_OUT_PIN, LOW);
     return;
   }
   
@@ -149,13 +163,13 @@ ISR(TIMER1_COMPA_vect)
     alpha_cnt = 0;
     chunk_cnt = 0;
     if(bit_defs[current_word[bit_cnt]][chunk_cnt].state)
-      digitalWrite(DATA_OUT_PIN, HIGH);
+      digitalWrite(RF_DATA_OUT_PIN, HIGH);
     else
-      digitalWrite(DATA_OUT_PIN, LOW);
+      digitalWrite(RF_DATA_OUT_PIN, LOW);
     return;
   }
   stop_timer();
-  digitalWrite(DATA_OUT_PIN, LOW);
+  digitalWrite(RF_DATA_OUT_PIN, LOW);
 
   word_cnt++;
   if(word_cnt < FRAME_LEN)
@@ -176,57 +190,126 @@ void send_frame(const word_t w)
   for(;;)
     if(frame_finished)
       break;
+
+  Serial.println("Ok");
+}
+
+//********************************************************************//
+
+void printTemperature(DeviceAddress deviceAddress)
+{
+  dallas_sensors.requestTemperatures();
+  float tempC = dallas_sensors.getTempC(deviceAddress);
+  Serial.print("Temp C: ");
+  Serial.println(tempC);
+  //Serial.print(" Temp F: ");
+  //Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
 }
 
 //********************************************************************//
 
 void setup()
 {
-  pinMode(DATA_OUT_PIN, OUTPUT);
-  digitalWrite(DATA_OUT_PIN, LOW);
+  pinMode(RF_DATA_OUT_PIN, OUTPUT);
+  digitalWrite(RF_DATA_OUT_PIN, LOW);
+  pinMode(IR_MOVEMENT_PIN, INPUT);      // set pin to input
+  digitalWrite(IR_MOVEMENT_PIN, LOW);  // turn off pullup resistors  
+  pinMode(PANIC_BUTTON_PIN, INPUT);      // set pin to input
+  digitalWrite(PANIC_BUTTON_PIN, HIGH);  // turn on pullup resistors 
+  
+  onewire.reset();
+  onewire.reset_search();
+  dallas_sensors.begin();
+  //in case we change temp sensor:
+  if (!dallas_sensors.getAddress(onShieldTemp, 0)) 
+    Serial.println("Error: Unable to find address for Device 0"); 
+  dallas_sensors.setResolution(onShieldTemp, 9);
+  
   Serial.begin(9600);
 }
 
+unsigned int ir_time=IR_SAMPLE_DURATION;
+unsigned int ir_count=0;
+boolean pb_last_state=0;
+boolean pb_state=0;
+boolean pb_postth_state=0;
+unsigned int pb_time=0;
+
 void loop()
 {
+  ir_time--;
+  ir_count += (digitalRead(IR_MOVEMENT_PIN) == HIGH);
+
+  if (pb_time < PB_TRESHOLD)
+    pb_time++;
+  pb_state=(digitalRead(PANIC_BUTTON_PIN) == LOW);
+  
+  if (ir_time == 0)
+  {
+    if (ir_count >= IR_TRESHOLD)
+      Serial.println("movement");
+    ir_time=IR_SAMPLE_DURATION;
+    ir_count=0;
+  }
+  
+  if (pb_state == pb_last_state && pb_time >= PB_TRESHOLD)
+  {
+    if (pb_state && ! pb_postth_state)
+    {   
+      pb_postth_state=1;
+      Serial.println("PanicButton");
+    }
+    else if (!pb_state)
+      pb_postth_state=0;
+  }
+  else if (pb_state != pb_last_state)
+  {
+    pb_time=0;
+    pb_last_state=pb_state;
+  }
+    
   if(Serial.available()) {
     char command = Serial.read();
     
-    if(command == 'q')
+    if(command == 'A')
       send_frame(words[A1_ON]);
     else if(command == 'a')
       send_frame(words[A1_OFF]);
-    else if(command == 'w')
+    else if(command == 'B')
       send_frame(words[A2_ON]);
-    else if(command == 's')
+    else if(command == 'b')
       send_frame(words[A2_OFF]);
 
-    else if(command == 'e')
+    else if(command == 'C')
       send_frame(words[B1_ON]);
-    else if(command == 'd')
+    else if(command == 'c')
       send_frame(words[B1_OFF]);
-    else if(command == 'r')
+    else if(command == 'D')
       send_frame(words[B2_ON]);
-    else if(command == 'f')
+    else if(command == 'd')
       send_frame(words[B2_OFF]);
 
-    else if(command == 't')
+    else if(command == 'E')
       send_frame(words[C1_ON]);
-    else if(command == 'g')
+    else if(command == 'e')
       send_frame(words[C1_OFF]);
-    else if(command == 'z')
+    else if(command == 'F')
       send_frame(words[C2_ON]);
-    else if(command == 'h')
+    else if(command == 'f')
       send_frame(words[C2_OFF]);
 
-    else if(command == 'u')
+    else if(command == 'G')
       send_frame(words[D1_ON]);
-    else if(command == 'j')
+    else if(command == 'g')
       send_frame(words[D1_OFF]);
-    else if(command == 'i')
+    else if(command == 'H')
       send_frame(words[D2_ON]);
-    else if(command == 'k')
+    else if(command == 'h')
       send_frame(words[D2_OFF]);
+    else if(command == 'T')
+      printTemperature(onShieldTemp);
 
+    else
+      Serial.println("Error: unknown command");
   }
 }