IRRemote fuer Yamaha Reciever
[svn42.git] / rf433ctl / rf433ctl.pde
index 6b541e2..ae61d39 100644 (file)
@@ -3,6 +3,7 @@
 #include <inttypes.h>
 #include <OneWire.h>
 #include <DallasTemperature.h>
+#include <IRremote.h>
 
 //********************************************************************//
 
 #define IR_MOVEMENT_PIN 9
 #define ONE_WIRE_PIN 8
 #define PANIC_BUTTON_PIN 7
+#define BLUELED_PWM_PIN 6
+#define BLUELED2_PWM_PIN 11
+#define PHOTO_ANALOGPIN 0
 //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
+#define IR_SAMPLE_DURATION 12000
+#define IR_TRESHOLD 10000
 //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
+#define PHOTO_SAMPLE_INTERVAL 4000
+#define IRREMOTE_SEND_PIN 3   //hardcoded in library
+//WARNING IRremote Lib uses TCCR2
 
 OneWire  onewire(ONE_WIRE_PIN);
 DallasTemperature dallas_sensors(&onewire);
 DeviceAddress onShieldTemp = { 0x10, 0xE7, 0x77, 0xD3, 0x01, 0x08, 0x00, 0x3F };
+IRsend irsend; 
+#define TEMPC_OFFSET_ARDUINO_GENEREATED_HEAT 
+
+//********************************************************************//
+// IR Codes, 32 bit, NEC
+const int YAMAHA_CODE_BITS=32;
+const unsigned long int YAMAHA_POWER = 0x000000005EA1F807;
+const unsigned long int YAMAHA_CD = 0x000000005EA1A857;
+const unsigned long int YAMAHA_TUNER = 0x000000005EA16897;
+const unsigned long int YAMAHA_SAT = 0x000000005EA19867;
+const unsigned long int YAMAHA_DVD = 0x000000005EA118E7;
+const unsigned long int YAMAHA_DVD_SPDIF = 0x000000005EA1E817;
+const unsigned long int YAMAHA_VCR_1 = 0x000000005EA1F00F;
+const unsigned long int YAMAHA_TUNER_PLUS = 0x000000005EA108F7;
+const unsigned long int YAMAHA_TUNER_ABCDE = 0x000000005EA148B7;
+const unsigned long int YAMAHA_FRONT_LEVEL_P = 0x000000005EA101FE;
+const unsigned long int YAMAHA_FRONT_LEVEL_M = 0x000000005EA1817E;
+const unsigned long int YAMAHA_CENTRE_LEVEL_P = 0x000000005EA141BE;
+const unsigned long int YAMAHA_CENTRE_LEVEL_M = 0x000000005EA1C13E;
+const unsigned long int YAMAHA_REAR_LEVEL_P = 0x000000005EA17A85;
+const unsigned long int YAMAHA_REAR_LEVEL_M = 0x000000005EA1FA05;
+const unsigned long int YAMAHA_DELAY_TIME_P = 0x000000005EA14AB5;
+const unsigned long int YAMAHA_DELAY_TIME_M = 0x000000005EA1CA35;
+const unsigned long int YAMAHA_MUTE = 0x000000005EA138C7;
+const unsigned long int YAMAHA_VOLUME_UP = 0x000000005EA158A7;
+const unsigned long int YAMAHA_VOLUME_DOWN = 0x000000005EA1D827;
+
+//********************************************************************//
 
 typedef struct {
   byte offset;
@@ -136,9 +171,9 @@ void init_word(const word_t w)
   bit_cnt = 0;
 
   if(bit_defs[current_word[bit_cnt]][chunk_cnt].state)
-    digitalWrite(RF_DATA_OUT_PIN, HIGH);
+    digitalWrite(RF_DATA_OUT_PIN, LOW); //neue 12V MosFET Verstärkung invertiert Logik !
   else
-    digitalWrite(RF_DATA_OUT_PIN, LOW);
+    digitalWrite(RF_DATA_OUT_PIN, HIGH);
 
   start_timer();
 }
@@ -152,9 +187,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(RF_DATA_OUT_PIN, HIGH);
+      digitalWrite(RF_DATA_OUT_PIN, LOW); //neue 12V MosFET Verstärkung invertiert Logik !
     else
-      digitalWrite(RF_DATA_OUT_PIN, LOW);
+      digitalWrite(RF_DATA_OUT_PIN, HIGH);
     return;
   }
   
@@ -163,19 +198,19 @@ ISR(TIMER1_COMPA_vect)
     alpha_cnt = 0;
     chunk_cnt = 0;
     if(bit_defs[current_word[bit_cnt]][chunk_cnt].state)
-      digitalWrite(RF_DATA_OUT_PIN, HIGH);
+      digitalWrite(RF_DATA_OUT_PIN, LOW); //neue 12V MosFET Verstärkung invertiert Logik !
     else
-      digitalWrite(RF_DATA_OUT_PIN, LOW);
+      digitalWrite(RF_DATA_OUT_PIN, HIGH);
     return;
   }
   stop_timer();
-  digitalWrite(RF_DATA_OUT_PIN, LOW);
+  digitalWrite(RF_DATA_OUT_PIN, HIGH);
 
   word_cnt++;
   if(word_cnt < FRAME_LEN)
     init_word(current_word);
-
-  frame_finished = 1;
+  else
+    frame_finished = 2;
 }
 
 //***********//
@@ -183,15 +218,25 @@ ISR(TIMER1_COMPA_vect)
 
 void send_frame(const word_t w)
 {
+  if (frame_finished == 0)
+    for(;;)
+      if (frame_finished)
+      {
+        delay(150);
+        break;
+      }
   word_cnt = 0;
   frame_finished = 0;
-  init_word(w);
-
-  for(;;)
-    if(frame_finished)
-      break;
+  init_word(w);      
+}
 
-  Serial.println("Ok");
+void check_frame_done()
+{
+  if (frame_finished==2)
+  {
+    Serial.println("Ok");
+    frame_finished=1;
+  }
 }
 
 //********************************************************************//
@@ -200,32 +245,118 @@ void printTemperature(DeviceAddress deviceAddress)
 {
   dallas_sensors.requestTemperatures();
   float tempC = dallas_sensors.getTempC(deviceAddress);
-  Serial.print("Temp C: ");
-  Serial.println(tempC);
+  //Serial.print("Temp C: ");
+  Serial.println(tempC TEMPC_OFFSET_ARDUINO_GENEREATED_HEAT);
   //Serial.print(" Temp F: ");
   //Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
 }
 
 //********************************************************************//
 
+unsigned int light_level_mean_ = 0;
+unsigned int light_sample_time_ = 0;
+
+void updateLightLevel(unsigned int pin)
+{
+  light_sample_time_++;
+  if (light_sample_time_ < PHOTO_SAMPLE_INTERVAL)
+    return;
+  light_sample_time_ = 0;
+  
+  unsigned int value = analogRead(pin);
+  if (value == light_level_mean_)
+    return;
+  
+  unsigned int diff = abs(value - light_level_mean_);
+  if (diff > 100)
+    light_level_mean_ = value;
+  else
+      light_level_mean_=(unsigned int) ( ((float) light_level_mean_) * 0.90 + ((float)value)*0.10 );
+}
+
+void printLightLevel()
+{
+  //Serial.print("Photo: ");
+  Serial.println(light_level_mean_);
+}
+
+//********************************************************************//
+
+unsigned long wm_start_=0;
+bool wait_millis(unsigned long ms)
+{
+  if (wm_start_ > 0)
+  {
+    if (millis() < wm_start_ || millis() > wm_start_+ ms)
+    {
+      wm_start_=0;
+      return false;
+    }
+    else
+      return true;
+  }
+  else
+  {
+    wm_start_=millis();
+    return true;
+  }
+}
+
+unsigned int flash_led_time_=0;
+unsigned int flash_led_brightness_=255;
+unsigned int flash_led_delay_=8;
+void calculate_led_level(unsigned int pwm_pin)
+{
+  if (flash_led_time_ == 0)
+    return;
+  if (wait_millis(flash_led_delay_))
+    return;
+  flash_led_time_--;
+  int c = abs(sin(float(flash_led_time_) / 100.0)) * flash_led_brightness_;
+  //int d = abs(sin(float(flash_led_time_) / 100.0)) * flash_led_brightness_;
+  analogWrite(BLUELED2_PWM_PIN, 255-c);
+  if (flash_led_brightness_ == 255)
+  {
+    if (flash_led_time_)
+      analogWrite(BLUELED_PWM_PIN, 255-c);
+    else
+      analogWrite(BLUELED_PWM_PIN, c);
+  }
+}
+
+void flash_led(unsigned int times, unsigned int brightness_divisor, unsigned int delay_divisor)
+{
+  unsigned int new_flash_led_brightness = 255 / brightness_divisor;
+  unsigned int new_flash_led_delay = 8 / delay_divisor;
+  if (flash_led_time_ == 0 || new_flash_led_brightness > flash_led_brightness_)
+    flash_led_brightness_=new_flash_led_brightness;
+  if (flash_led_time_ == 0 || new_flash_led_delay < flash_led_delay_)
+    flash_led_delay_=new_flash_led_delay;
+  flash_led_time_ += 314*times;
+}
+
+//********************************************************************//
+
 void setup()
 {
   pinMode(RF_DATA_OUT_PIN, OUTPUT);
-  digitalWrite(RF_DATA_OUT_PIN, LOW);
+  digitalWrite(RF_DATA_OUT_PIN, HIGH);
   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 
+  analogWrite(BLUELED_PWM_PIN,0);
+  analogWrite(BLUELED2_PWM_PIN,255); //pwm sink(-) instead of pwm + (better for mosfets)
+
+  Serial.begin(9600);
   
   onewire.reset();
   onewire.reset_search();
   dallas_sensors.begin();
   //in case we change temp sensor:
   if (!dallas_sensors.getAddress(onShieldTemp, 0)) 
-    Serial.println("Unable to find address for Device 0"); 
-  dallas_sensors.setResolution(onShieldTemp, 9);
-  
-  Serial.begin(9600);
+    Serial.println("Error: Unable to find address for Device 0"); 
+  dallas_sensors.setResolution(onShieldTemp, 9);  
 }
 
 unsigned int ir_time=IR_SAMPLE_DURATION;
@@ -235,6 +366,13 @@ boolean pb_state=0;
 boolean pb_postth_state=0;
 unsigned int pb_time=0;
 
+void sensorEchoCommand(char command)
+{
+  Serial.print("Sensor ");
+  Serial.print(command);
+  Serial.print(": ");
+}
+
 void loop()
 {
   ir_time--;
@@ -247,7 +385,10 @@ void loop()
   if (ir_time == 0)
   {
     if (ir_count >= IR_TRESHOLD)
+    {
+      flash_led(1,8,1);
       Serial.println("movement");
+    }
     ir_time=IR_SAMPLE_DURATION;
     ir_count=0;
   }
@@ -258,6 +399,7 @@ void loop()
     {   
       pb_postth_state=1;
       Serial.println("PanicButton");
+      flash_led(7,1,2);
     }
     else if (!pb_state)
       pb_postth_state=0;
@@ -267,48 +409,94 @@ void loop()
     pb_time=0;
     pb_last_state=pb_state;
   }
-    
+  
+  updateLightLevel(PHOTO_ANALOGPIN);
+  calculate_led_level(BLUELED_PWM_PIN);
+  check_frame_done();
+  
   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')
+    {
+      sensorEchoCommand(command);
       printTemperature(onShieldTemp);
-
+    }
+    else if(command == 'P')
+    {
+      sensorEchoCommand(command);
+      printLightLevel();
+    }
+    else if (command == '1')
+    {
+      irsend.sendNEC(YAMAHA_POWER,YAMAHA_CODE_BITS);
+      Serial.println("Ok");
+    }
+    else if (command == '2')
+    {
+      irsend.sendNEC(YAMAHA_CD,YAMAHA_CODE_BITS);
+      Serial.println("Ok");
+    }
+    else if (command == '3')
+    {
+      irsend.sendNEC(YAMAHA_DVD_SPDIF,YAMAHA_CODE_BITS);
+      Serial.println("Ok");
+    }
+    else if (command == '4')
+    {
+      irsend.sendNEC(YAMAHA_TUNER,YAMAHA_CODE_BITS);
+      Serial.println("Ok");
+    }    
+    else if (command == '5')
+    {
+      irsend.sendNEC(YAMAHA_VOLUME_UP,YAMAHA_CODE_BITS);
+      Serial.println("Ok");
+    }
+    else if (command == '6')
+    {
+      irsend.sendNEC(YAMAHA_VOLUME_DOWN,YAMAHA_CODE_BITS);
+      Serial.println("Ok");
+    }
+    else if (command == '7')
+    {
+      irsend.sendNEC(YAMAHA_MUTE,YAMAHA_CODE_BITS);
+      Serial.println("Ok");
+    }
     else
       Serial.println("Error: unknown command");
   }