clear screen before score boards gets drawn
[svn42.git] / rf433ctl / rf433ctl.pde
index 68d9f08..fcfe7c0 100644 (file)
@@ -9,14 +9,15 @@
 
 #define RF_DATA_OUT_PIN 13
 #define IR_MOVEMENT_PIN 9
+#define IR_MOVEMENT_PIN2 12
 #define ONE_WIRE_PIN 8
 #define PANIC_BUTTON_PIN 7
 #define PANICLED_PWM_PIN 6
 #define BLUELED_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 12000
-#define IR_TRESHOLD 10000
+#define IR_SAMPLE_DURATION 6000
+#define IR_TRESHOLD 5900
 //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
@@ -26,8 +27,8 @@
 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 
+IRsend irsend;
+#define TEMPC_OFFSET_ARDUINO_GENEREATED_HEAT
 
 //********************************************************************//
 // IR Codes, 32 bit, NEC
@@ -111,12 +112,27 @@ const rf_bit_t sync_bit[] = { {   4, 1 },
                               { 128, 0 },
                               {   0, 0 } };
 
-typedef enum { ZERO = 0, ONE , FLOAT , SYNC } adbit_t;
+//WORKS @ alpha=0.0775ms
+//const rf_bit_t pwm_0_bit[] = {  {7, 1}, {24, 0}, {  0, 0 } };     // 1.86ms gesamt: { 0.46ms HIGH , 1.4ms LOW }
+//const rf_bit_t pwm_1_bit[] = {  {18, 1}, {24, 0}, {  0, 0 } };    // 1.86ms gesamt: { 1.4ms HIGH , 0.46ms LOW }
+//const rf_bit_t pwm_pause_bit[] = {  {168, 0}, {  0, 0 } };        // 13ms pause
+
+//WORKS @ alpha=0.08ms
+const rf_bit_t pwm_0_bit[] = {  {6, 1}, {23, 0}, {  0, 0 } };   // 1.86ms gesamt: { 0.46ms HIGH , 1.4ms LOW }
+const rf_bit_t pwm_1_bit[] = {  {18, 1}, {23, 0}, {  0, 0 } };  // 1.86ms gesamt: { 1.4ms HIGH , 0.46ms LOW }
+const rf_bit_t pwm_pause_bit[] = { {162, 0}, {  0, 0 } };      // 13ms pause
+const rf_bit_t pwm_00_bit[] = {  {6, 1},  {23, 0}, {29, 1}, {46, 0}, { 0, 0 } }; // pwm_0 pwm_0
+const rf_bit_t pwm_01_bit[] = {  {6, 1},  {23, 0}, {41, 1}, {46, 0}, { 0, 0 } }; // pwm_0 pwm_1
+const rf_bit_t pwm_10_bit[] = {  {18, 1}, {23, 0}, {29, 1}, {46, 0}, { 0, 0 } }; // pwm_1 pwm_0
+const rf_bit_t pwm_11_bit[] = {  {18, 1}, {23, 0}, {41, 1}, {46, 0}, { 0, 0 } }; // pwm_1 pwm_1
+const rf_bit_t pwm_end_bit[] = { {6, 1},  {23, 0}, {185, 0}, { 0, 0 } };      // pwm_0 pwm_pause
+
+typedef enum { ZERO = 0, ONE , FLOAT , SYNC , PWM0, PWM1, PWM_00, PWM_01, PWM_10, PWM_11, PWM_END, PWM_PAUSE, WORD_END } adbit_t;
 typedef byte ad_bit_t;
-#define WORD_LEN 13
-typedef ad_bit_t word_t[WORD_LEN];
+#define MAX_WORD_LEN 13
+typedef ad_bit_t word_t[MAX_WORD_LEN];
 
-const rf_bit_t* bit_defs[] = { zero_bit, one_bit, float_bit, sync_bit };
+const rf_bit_t* bit_defs[] = { zero_bit, one_bit, float_bit, sync_bit, pwm_0_bit, pwm_1_bit, pwm_00_bit, pwm_01_bit, pwm_10_bit, pwm_11_bit, pwm_end_bit, pwm_pause_bit };
 
 byte alpha_cnt = 0;
 byte bit_cnt = 0;
@@ -147,26 +163,143 @@ byte volatile frame_finished = 1;
 #define D2_ON  14
 #define D2_OFF 15
 
-const word_t words[]  = { 
-{ ZERO,  ZERO,  FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // A1_ON
-{ ZERO,  ZERO,  FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // A1_OFF
-{ ZERO,  ZERO,  FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // A2_ON
-{ ZERO,  ZERO,  FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // A2_OFF
-
-{ FLOAT, ZERO,  FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // B1_ON
-{ FLOAT, ZERO,  FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // B1_OFF
-{ FLOAT, ZERO,  FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // B2_ON
-{ FLOAT, ZERO,  FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // B2_OFF
-
-{ ZERO,  FLOAT, FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // C1_ON
-{ ZERO,  FLOAT, FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // C1_OFF
-{ ZERO,  FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // C2_ON
-{ ZERO,  FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // C2_OFF
-
-{ FLOAT, FLOAT, FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // D1_ON
-{ FLOAT, FLOAT, FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // D1_OFF
-{ FLOAT, FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // D2_ON
-{ FLOAT, FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }  // D2_OFF
+#define BLACK_A1_ON 16
+#define BLACK_A1_OFF 17
+#define BLACK_A2_ON 18
+#define BLACK_A2_OFF 19
+#define BLACK_A3_ON 20
+#define BLACK_A3_OFF 21
+
+#define BLACK_B1_ON 22
+#define BLACK_B1_OFF 23
+#define BLACK_B2_ON 24
+#define BLACK_B2_OFF 25
+#define BLACK_B3_ON 26
+#define BLACK_B3_OFF 27
+
+#define BLACK_C1_ON 28
+#define BLACK_C1_OFF 29
+#define BLACK_C2_ON 30
+#define BLACK_C2_OFF 31
+#define BLACK_C3_ON 32
+#define BLACK_C3_OFF 33
+
+#define BLACK_D1_ON 34
+#define BLACK_D1_OFF 35
+#define BLACK_D2_ON 36
+#define BLACK_D2_OFF 37
+#define BLACK_D3_ON 38
+#define BLACK_D3_OFF 39
+
+//SW 0..3 / BT 0..3 / OFF? 1  ON? 0
+//#define RSL336T_INDEX(SW,BT,OFF)   40+(2*4*SW)+(2*BT)+OFF
+
+//WORD_END can be used to terminate word prematurely, otherwise word ends after 13 bits
+
+#define BLACK_SW_A  PWM_11,PWM_01,PWM_01,PWM_01
+#define BLACK_SW_B  PWM_01,PWM_11,PWM_01,PWM_01
+#define BLACK_SW_C  PWM_01,PWM_01,PWM_11,PWM_01
+#define BLACK_SW_D  PWM_01,PWM_01,PWM_01,PWM_11
+
+#define BLACK_BT_1  PWM_11,PWM_01,PWM_01
+#define BLACK_BT_2  PWM_01,PWM_11,PWM_01
+#define BLACK_BT_3  PWM_01,PWM_01,PWM_11
+
+const word_t words[]  = {
+{ ZERO,  ZERO,  FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC}, // A1_ON
+{ ZERO,  ZERO,  FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC}, // A1_OFF
+{ ZERO,  ZERO,  FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC}, // A2_ON
+{ ZERO,  ZERO,  FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC}, // A2_OFF
+
+{ FLOAT, ZERO,  FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC}, // B1_ON
+{ FLOAT, ZERO,  FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC}, // B1_OFF
+{ FLOAT, ZERO,  FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC}, // B2_ON
+{ FLOAT, ZERO,  FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC}, // B2_OFF
+
+{ ZERO,  FLOAT, FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC}, // C1_ON
+{ ZERO,  FLOAT, FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC}, // C1_OFF
+{ ZERO,  FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC}, // C2_ON
+{ ZERO,  FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC}, // C2_OFF
+
+{ FLOAT, FLOAT, FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC}, // D1_ON
+{ FLOAT, FLOAT, FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC}, // D1_OFF
+{ FLOAT, FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC}, // D2_ON
+{ FLOAT, FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC}, // D2_OFF
+
+{BLACK_SW_A,BLACK_BT_1,PWM_00,PWM_00,PWM_00,PWM_11,PWM_00,PWM_END}, // BLACK_A1_ON
+{BLACK_SW_A,BLACK_BT_1,PWM_00,PWM_00,PWM_00,PWM_00,PWM_11,PWM_END}, // BLACK_A1_OFF
+{BLACK_SW_A,BLACK_BT_2,PWM_00,PWM_00,PWM_00,PWM_11,PWM_00,PWM_END}, // BLACK_A2_ON
+{BLACK_SW_A,BLACK_BT_2,PWM_00,PWM_00,PWM_00,PWM_00,PWM_11,PWM_END}, // BLACK_A2_OFF
+{BLACK_SW_A,BLACK_BT_3,PWM_00,PWM_00,PWM_00,PWM_11,PWM_00,PWM_END}, // BLACK_A3_ON
+{BLACK_SW_A,BLACK_BT_3,PWM_00,PWM_00,PWM_00,PWM_00,PWM_11,PWM_END}, // BLACK_A3_OFF
+
+{BLACK_SW_B,BLACK_BT_1,PWM_00,PWM_00,PWM_00,PWM_11,PWM_00,PWM_END}, // BLACK_B1_ON
+{BLACK_SW_B,BLACK_BT_1,PWM_00,PWM_00,PWM_00,PWM_00,PWM_11,PWM_END}, // BLACK_B1_OFF
+{BLACK_SW_B,BLACK_BT_2,PWM_00,PWM_00,PWM_00,PWM_11,PWM_00,PWM_END}, // BLACK_B2_ON
+{BLACK_SW_B,BLACK_BT_2,PWM_00,PWM_00,PWM_00,PWM_00,PWM_11,PWM_END}, // BLACK_B2_OFF
+{BLACK_SW_B,BLACK_BT_3,PWM_00,PWM_00,PWM_00,PWM_11,PWM_00,PWM_END}, // BLACK_B3_ON
+{BLACK_SW_B,BLACK_BT_3,PWM_00,PWM_00,PWM_00,PWM_00,PWM_11,PWM_END}, // BLACK_B3_OFF
+
+{BLACK_SW_C,BLACK_BT_1,PWM_00,PWM_00,PWM_00,PWM_11,PWM_00,PWM_END}, // BLACK_C1_ON
+{BLACK_SW_C,BLACK_BT_1,PWM_00,PWM_00,PWM_00,PWM_00,PWM_11,PWM_END}, // BLACK_C1_OFF
+{BLACK_SW_C,BLACK_BT_2,PWM_00,PWM_00,PWM_00,PWM_11,PWM_00,PWM_END}, // BLACK_C2_ON
+{BLACK_SW_C,BLACK_BT_2,PWM_00,PWM_00,PWM_00,PWM_00,PWM_11,PWM_END}, // BLACK_C2_OFF
+{BLACK_SW_C,BLACK_BT_3,PWM_00,PWM_00,PWM_00,PWM_11,PWM_00,PWM_END}, // BLACK_C3_ON
+{BLACK_SW_C,BLACK_BT_3,PWM_00,PWM_00,PWM_00,PWM_00,PWM_11,PWM_END}, // BLACK_C3_OFF
+
+{BLACK_SW_D,BLACK_BT_1,PWM_00,PWM_00,PWM_00,PWM_11,PWM_00,PWM_END}, // BLACK_D1_ON
+{BLACK_SW_D,BLACK_BT_1,PWM_00,PWM_00,PWM_00,PWM_00,PWM_11,PWM_END}, // BLACK_D1_OFF
+{BLACK_SW_D,BLACK_BT_2,PWM_00,PWM_00,PWM_00,PWM_11,PWM_00,PWM_END}, // BLACK_D2_ON
+{BLACK_SW_D,BLACK_BT_2,PWM_00,PWM_00,PWM_00,PWM_00,PWM_11,PWM_END}, // BLACK_D2_OFF
+{BLACK_SW_D,BLACK_BT_3,PWM_00,PWM_00,PWM_00,PWM_11,PWM_00,PWM_END}, // BLACK_D3_ON
+{BLACK_SW_D,BLACK_BT_3,PWM_00,PWM_00,PWM_00,PWM_00,PWM_11,PWM_END}  // BLACK_D3_OFF
+};
+
+//SW 0..3 / BT 0..3 / OFF? 1  ON? 0
+#define RSL336T_INDEX(SW,BT,OFF)   (8*SW)+(2*BT)+OFF
+
+#define RSL336T_SWBT_1  PWM_00,PWM_01,PWM_01,PWM_01
+#define RSL336T_SWBT_2  PWM_01,PWM_00,PWM_01,PWM_01
+#define RSL336T_SWBT_3  PWM_01,PWM_01,PWM_00,PWM_01
+#define RSL336T_SWBT_4  PWM_01,PWM_01,PWM_01,PWM_00
+
+// note: code on atmel breaks if array below becomes too big
+const word_t rsl336T_words[]  = {
+{RSL336T_SWBT_1,RSL336T_SWBT_1,PWM_01,PWM_01,PWM_01,PWM_01,PWM_END}, // RSL366T_I_1_ON
+{RSL336T_SWBT_1,RSL336T_SWBT_1,PWM_01,PWM_01,PWM_01,PWM_00,PWM_END}, // RSL366T_I_1_OFF
+{RSL336T_SWBT_1,RSL336T_SWBT_2,PWM_01,PWM_01,PWM_01,PWM_01,PWM_END}, // RSL366T_I_2_ON
+{RSL336T_SWBT_1,RSL336T_SWBT_2,PWM_01,PWM_01,PWM_01,PWM_00,PWM_END}, // RSL366T_I_2_OFF
+{RSL336T_SWBT_1,RSL336T_SWBT_3,PWM_01,PWM_01,PWM_01,PWM_01,PWM_END}, // RSL366T_I_3_ON
+{RSL336T_SWBT_1,RSL336T_SWBT_3,PWM_01,PWM_01,PWM_01,PWM_00,PWM_END}, // RSL366T_I_3_OFF
+{RSL336T_SWBT_1,RSL336T_SWBT_4,PWM_01,PWM_01,PWM_01,PWM_01,PWM_END}, // RSL366T_I_4_ON
+{RSL336T_SWBT_1,RSL336T_SWBT_4,PWM_01,PWM_01,PWM_01,PWM_00,PWM_END}, // RSL366T_I_4_OFF
+
+{RSL336T_SWBT_2,RSL336T_SWBT_1,PWM_01,PWM_01,PWM_01,PWM_01,PWM_END}, // RSL366T_II_1_ON
+{RSL336T_SWBT_2,RSL336T_SWBT_1,PWM_01,PWM_01,PWM_01,PWM_00,PWM_END}, // RSL366T_II_1_OFF
+{RSL336T_SWBT_2,RSL336T_SWBT_2,PWM_01,PWM_01,PWM_01,PWM_01,PWM_END}, // RSL366T_II_2_ON
+{RSL336T_SWBT_2,RSL336T_SWBT_2,PWM_01,PWM_01,PWM_01,PWM_00,PWM_END}, // RSL366T_II_2_OFF
+//{RSL336T_SWBT_2,RSL336T_SWBT_3,PWM_01,PWM_01,PWM_01,PWM_01,PWM_END}, // RSL366T_II_3_ON
+//{RSL336T_SWBT_2,RSL336T_SWBT_3,PWM_01,PWM_01,PWM_01,PWM_00,PWM_END}, // RSL366T_II_3_OFF
+//{RSL336T_SWBT_2,RSL336T_SWBT_4,PWM_01,PWM_01,PWM_01,PWM_01,PWM_END}, // RSL366T_II_4_ON
+//{RSL336T_SWBT_2,RSL336T_SWBT_4,PWM_01,PWM_01,PWM_01,PWM_00,PWM_END}, // RSL366T_II_4_OFF
+//
+//{RSL336T_SWBT_3,RSL336T_SWBT_1,PWM_01,PWM_01,PWM_01,PWM_01,PWM_END}, // RSL366T_III_1_ON
+//{RSL336T_SWBT_3,RSL336T_SWBT_1,PWM_01,PWM_01,PWM_01,PWM_00,PWM_END}, // RSL366T_III_1_OFF
+//{RSL336T_SWBT_3,RSL336T_SWBT_2,PWM_01,PWM_01,PWM_01,PWM_01,PWM_END}, // RSL366T_III_2_ON
+//{RSL336T_SWBT_3,RSL336T_SWBT_2,PWM_01,PWM_01,PWM_01,PWM_00,PWM_END}, // RSL366T_III_2_OFF
+//{RSL336T_SWBT_3,RSL336T_SWBT_3,PWM_01,PWM_01,PWM_01,PWM_01,PWM_END}, // RSL366T_III_3_ON
+//{RSL336T_SWBT_3,RSL336T_SWBT_3,PWM_01,PWM_01,PWM_01,PWM_00,PWM_END}, // RSL366T_III_3_OFF
+//{RSL336T_SWBT_3,RSL336T_SWBT_4,PWM_01,PWM_01,PWM_01,PWM_01,PWM_END}, // RSL366T_III_4_ON
+//{RSL336T_SWBT_3,RSL336T_SWBT_4,PWM_01,PWM_01,PWM_01,PWM_00,PWM_END}, // RSL366T_III_4_OFF
+//
+//{RSL336T_SWBT_4,RSL336T_SWBT_1,PWM_01,PWM_01,PWM_01,PWM_01,PWM_END}, // RSL366T_IV_1_ON
+//{RSL336T_SWBT_4,RSL336T_SWBT_1,PWM_01,PWM_01,PWM_01,PWM_00,PWM_END}, // RSL366T_IV_1_OFF
+//{RSL336T_SWBT_4,RSL336T_SWBT_2,PWM_01,PWM_01,PWM_01,PWM_01,PWM_END}, // RSL366T_IV_2_ON
+//{RSL336T_SWBT_4,RSL336T_SWBT_2,PWM_01,PWM_01,PWM_01,PWM_00,PWM_END}, // RSL366T_IV_2_OFF
+//{RSL336T_SWBT_4,RSL336T_SWBT_3,PWM_01,PWM_01,PWM_01,PWM_01,PWM_END}, // RSL366T_IV_3_ON
+//{RSL336T_SWBT_4,RSL336T_SWBT_3,PWM_01,PWM_01,PWM_01,PWM_00,PWM_END}, // RSL366T_IV_3_OFF
+//{RSL336T_SWBT_4,RSL336T_SWBT_4,PWM_01,PWM_01,PWM_01,PWM_01,PWM_END}, // RSL366T_IV_4_ON
+//{RSL336T_SWBT_4,RSL336T_SWBT_4,PWM_01,PWM_01,PWM_01,PWM_00,PWM_END}  // RSL366T_IV_4_OFF
 };
 
 
@@ -176,8 +309,11 @@ void start_timer()
 {
   // timer 1: 2 ms
   TCCR1A = 0;                    // prescaler 1:8, WGM = 4 (CTC)
-  TCCR1B = 1<<WGM12 | 1<<CS11;   // 
+  TCCR1B = 1<<WGM12 | 1<<CS11;   //
+//  OCR1A = 39;        // (1+39)*8 = 320 -> 0.02ms @ 16 MHz -> 1*alpha
+//default: alpha=0.08
   OCR1A = 159;        // (1+159)*8 = 1280 -> 0.08ms @ 16 MHz -> 1*alpha
+//  OCR1A = 154;        // (1+154)*8 = 1240 -> 0.0775ms @ 16 MHz -> 1*alpha
 //  OCR1A = 207;        // (1+207)*8 = 1664 -> 0.104ms @ 16 MHz -> 1*alpha
   TCNT1 = 0;          // reseting timer
   TIMSK1 = 1<<OCIE1A; // enable Interrupt
@@ -219,9 +355,9 @@ ISR(TIMER1_COMPA_vect)
       digitalWrite(RF_DATA_OUT_PIN, HIGH);
     return;
   }
-  
+
   bit_cnt++;
-  if(bit_cnt < WORD_LEN) {
+  if(current_word[bit_cnt] != WORD_END && bit_cnt < MAX_WORD_LEN) {
     alpha_cnt = 0;
     chunk_cnt = 0;
     if(bit_defs[current_word[bit_cnt]][chunk_cnt].state)
@@ -290,11 +426,11 @@ void updateLightLevel(unsigned int pin)
   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;
@@ -310,14 +446,16 @@ void printLightLevel()
 
 //********************************************************************//
 
-unsigned long wm_start_=0;
-bool wait_millis(unsigned long ms)
+unsigned long wm_start_[3]={0,0,0};
+bool wait_millis(unsigned long *start_time, unsigned long ms)
 {
-  if (wm_start_ > 0)
+  if (ms == 0)
+    return false;
+  else if (*start_time > 0)
   {
-    if (millis() < wm_start_ || millis() > wm_start_+ ms)
+    if (millis() < *start_time || millis() > (*start_time) + ms)
     {
-      wm_start_=0;
+      *start_time = 0;
       return false;
     }
     else
@@ -325,81 +463,93 @@ bool wait_millis(unsigned long ms)
   }
   else
   {
-    wm_start_=millis();
+    *start_time=millis();
     return true;
   }
 }
-
-unsigned int flash_led_time_=0;
-unsigned int flash_led_brightness_=255;
-unsigned int flash_led_delay_=8;
-unsigned int flash_led_selected_=0;
+#define NUM_LEDS 2
+char flash_led_pins_[NUM_LEDS]={BLUELED_PWM_PIN,PANICLED_PWM_PIN};
+unsigned int flash_led_time_[3]={0,0,0};
+unsigned int flash_led_brightness_[3]={255,255,255};
+unsigned int flash_led_delay_[3]={8,8,8};
+unsigned int flash_led_initial_delay_[3]={0,0,0};
 void calculate_led_level()
 {
-  if (flash_led_time_ == 0 || flash_led_selected_ == 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_;
-  if (flash_led_selected_ && (1 << BLUELED_PWM_PIN))
-    analogWrite(BLUELED_PWM_PIN, 255-c);
-  else
-    analogWrite(BLUELED_PWM_PIN,255); //off
-  if (flash_led_selected_ && (1 << PANICLED_PWM_PIN))
+  for (int ledid = 0; ledid < NUM_LEDS; ledid++)
   {
-    if (flash_led_time_)
-      analogWrite(PANICLED_PWM_PIN, c);
-    else
-      analogWrite(PANICLED_PWM_PIN, 255-c);
+    if (flash_led_time_[ledid] == 0)
+      continue;
+    if (wait_millis(wm_start_ + ledid, flash_led_initial_delay_[ledid]))
+      continue;
+    flash_led_initial_delay_[ledid]=0;
+    if (wait_millis(wm_start_ + ledid, flash_led_delay_[ledid]))
+      continue;
+    flash_led_time_[ledid]--;
+    int c = abs(sin(float(flash_led_time_[ledid]) / 100.0)) * flash_led_brightness_[ledid];
+    //int d = abs(sin(float(flash_led_time_) / 100.0)) * flash_led_brightness_;
+    analogWrite(flash_led_pins_[ledid], 255-c);
   }
-  else
-    analogWrite(PANICLED_PWM_PIN,255); //off
 }
 
-void flash_led(unsigned int times, unsigned int brightness_divisor, unsigned int delay_divisor, unsigned int led_selector)
+// id: id of LED to flash (0,1)
+// times: # of times the LED should flash
+// brightness_divisor: 1: full brightness, 2: half brightness, ...
+// delay_divisor: 1: slow... 8: fastest
+// phase_divisor: 0.. same phase; 2.. pi/2 phase, 4.. pi phase, 6.. 3pi/2 phase
+void flash_led(unsigned int id, unsigned int times, unsigned int brightness_divisor, unsigned int delay_divisor, unsigned int phase_divisor)
 {
+  if (id >= NUM_LEDS)
+    return;
   unsigned int new_flash_led_brightness = 255;
   unsigned int new_flash_led_delay = 8;
-  flash_led_selected_=led_selector;
-  if (times == 0 || led_selector == 0)
+  if (times == 0)
   {
-    analogWrite(PANICLED_PWM_PIN,255); //off
-    analogWrite(BLUELED_PWM_PIN,255); //off
+    analogWrite(flash_led_pins_[id],255); //off
     return;
   }
   if (brightness_divisor > 1) //guard against div by zero
     new_flash_led_brightness /= brightness_divisor;
   if (delay_divisor > 1)  //guard against div by zero
     new_flash_led_delay /= 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;
+  if (flash_led_time_[id] == 0 || new_flash_led_brightness > flash_led_brightness_[id])
+    flash_led_brightness_[id]=new_flash_led_brightness;
+  if (flash_led_time_[id] == 0 || new_flash_led_delay < flash_led_delay_[id])
+    flash_led_delay_[id]=new_flash_led_delay;
+  flash_led_time_[id] += 314*times;
+  flash_led_initial_delay_[id] = flash_led_delay_[id]*314*phase_divisor/8;
 }
 
 //********************************************************************//
 
+int save_tcnt2=0;
+int save_tccr2a=0;
+int save_tccr2b=0;
+void reset_timer2()
+{
+  TCNT2 = save_tcnt2;
+  TCCR2A = save_tccr2a;  // normal mode
+  TCCR2B = save_tccr2b;
+  //TCNT2 = 256 - (50*(16000000/8/1000000)) + 5;
+  //TCCR2A = 0;  // normal mode
+  //TCCR2B = 0;
+}
+
 void send_yamaha_ir_signal(char codebyte)
 {
   unsigned long int code = codebyte & 0xFF;
   code <<= 8;
   code |= (0xff ^ codebyte) & 0xFF;
   code |= YAMAHA_CODE_BASE;
-  
+
   //irsend changes PWM Timer Frequency among other things
   //.. doesn't go well with PWM output using the same timer
   //.. thus we just set output to 255 so whatever frequency is used, led is off for the duration
-  unsigned int flash_prev_selected = flash_led_selected_; //save prev. selected leds
-  flash_led_selected_ &= !(1 << BLUELED_PWM_PIN); //prevent calculate_led_level() from setting blueled
-  analogWrite(BLUELED_PWM_PIN,255); // switch led off
+  //analogWrite(BLUELED_PWM_PIN,255); // switch led off
 
   irsend.sendNEC(code,YAMAHA_CODE_BITS);
 
+  reset_timer2();
   analogWrite(BLUELED_PWM_PIN,255); // switch off led again to be sure
-  flash_led_selected_ = flash_prev_selected;  //restore led settings for calculate_led_level()
                                       //is actually not necessary, since we are not multitasking/using interrupts, but just to be sure in case this might change
 
   Serial.println("Ok");
@@ -412,27 +562,34 @@ void setup()
   pinMode(RF_DATA_OUT_PIN, OUTPUT);
   digitalWrite(RF_DATA_OUT_PIN, HIGH);
   pinMode(IR_MOVEMENT_PIN, INPUT);      // set pin to input
-  digitalWrite(IR_MOVEMENT_PIN, LOW);  // turn off pulldown resistors  
+  digitalWrite(IR_MOVEMENT_PIN, LOW);  // turn off pullup resistors
+  digitalWrite(IR_MOVEMENT_PIN2, LOW);  // turn off pullup resistors
   pinMode(PANIC_BUTTON_PIN, INPUT);      // set pin to input
-  digitalWrite(PANIC_BUTTON_PIN, LOW);  // turn on pulldown resistors 
+  digitalWrite(PANIC_BUTTON_PIN, LOW);  // turn of pullup resistors
   analogWrite(PANICLED_PWM_PIN,255);
   analogWrite(BLUELED_PWM_PIN,255); //pwm sink(-) instead of pwm + (better for mosfets)
   pinMode(IRREMOTE_SEND_PIN, OUTPUT);
   digitalWrite(IRREMOTE_SEND_PIN, HIGH);
-  
+
   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("Error: Unable to find address for Device 0"); 
-  dallas_sensors.setResolution(onShieldTemp, 9);  
+  if (!dallas_sensors.getAddress(onShieldTemp, 0))
+    Serial.println("Error: Unable to find address for Device 0");
+  dallas_sensors.setResolution(onShieldTemp, 9);
+
+  //save prev timer states:
+  save_tcnt2 = TCNT2;
+  save_tccr2a = TCCR2A;  // normal mode
+  save_tccr2b = TCCR2B;
 }
 
 unsigned int ir_time=IR_SAMPLE_DURATION;
 unsigned int ir_count=0;
+unsigned int ir_count2=0;
 boolean pb_last_state=0;
 boolean pb_state=0;
 boolean pb_postth_state=0;
@@ -449,29 +606,32 @@ void loop()
 {
   ir_time--;
   ir_count += (digitalRead(IR_MOVEMENT_PIN) == HIGH);
+  ir_count2 += (digitalRead(IR_MOVEMENT_PIN2) == HIGH);
 
   if (pb_time < PB_TRESHOLD)
     pb_time++;
   pb_state=(digitalRead(PANIC_BUTTON_PIN) == HIGH);
-  
+
   if (ir_time == 0)
   {
-    if (ir_count >= IR_TRESHOLD)
+    if (ir_count >= IR_TRESHOLD || ir_count2 >= IR_TRESHOLD)
     {
-      flash_led(1, 8, 1, (1<<BLUELED_PWM_PIN) );
+      flash_led(0, 1, 8, 1, 0 );
       Serial.println("movement");
     }
     ir_time=IR_SAMPLE_DURATION;
     ir_count=0;
+    ir_count2=0;
   }
-  
+
   if (pb_state == pb_last_state && pb_time >= PB_TRESHOLD)
   {
     if (pb_state && ! pb_postth_state)
-    {   
+    {
       pb_postth_state=1;
       Serial.println("PanicButton");
-      flash_led(14, 1, 2, (1<<BLUELED_PWM_PIN)|(1<<PANICLED_PWM_PIN) );
+      flash_led(0, 28, 1, 4, 0 );
+      flash_led(1, 28, 1, 4, 4 );
     }
     else if (!pb_state)
       pb_postth_state=0;
@@ -481,14 +641,14 @@ void loop()
     pb_time=0;
     pb_last_state=pb_state;
   }
-  
+
   updateLightLevel(PHOTO_ANALOGPIN);
   calculate_led_level();
   check_frame_done();
-  
+
   if(Serial.available()) {
     char command = Serial.read();
-    
+
     if(command == 'A')
       send_frame(words[A1_ON]);
     else if(command == 'a')
@@ -524,62 +684,159 @@ void loop()
       send_frame(words[D2_ON]);
     else if(command == 'h')
       send_frame(words[D2_OFF]);
+
+    else if(command == 'I')
+      send_frame(words[BLACK_A1_ON]);
+    else if(command == 'i')
+      send_frame(words[BLACK_A1_OFF]);
+    else if(command == 'J')
+      send_frame(words[BLACK_A2_ON]);
+    else if(command == 'j')
+      send_frame(words[BLACK_A2_OFF]);
+    else if(command == 'K')
+      send_frame(words[BLACK_A3_ON]);
+    else if(command == 'k')
+      send_frame(words[BLACK_A3_OFF]);
+
+    else if(command == 'L')
+      send_frame(words[BLACK_B1_ON]);
+    else if(command == 'l')
+      send_frame(words[BLACK_B1_OFF]);
+    else if(command == 'M')
+      send_frame(words[BLACK_B2_ON]);
+    else if(command == 'm')
+      send_frame(words[BLACK_B2_OFF]);
+    else if(command == 'N')
+      send_frame(words[BLACK_B3_ON]);
+    else if(command == 'n')
+      send_frame(words[BLACK_B3_OFF]);
+
+    else if(command == 'O')
+      send_frame(words[BLACK_C1_ON]);
+    else if(command == 'o')
+      send_frame(words[BLACK_C1_OFF]);
+    else if(command == 'P')
+      send_frame(words[BLACK_C2_ON]);
+    else if(command == 'p')
+      send_frame(words[BLACK_C2_OFF]);
+    else if(command == 'Q')
+      send_frame(words[BLACK_C3_ON]);
+    else if(command == 'q')
+      send_frame(words[BLACK_C3_OFF]);
+
+    else if(command == 'R')
+      send_frame(words[BLACK_D1_ON]);
+    else if(command == 'r')
+      send_frame(words[BLACK_D1_OFF]);
+    else if(command == 'S')
+      send_frame(words[BLACK_D2_ON]);
+    else if(command == 's')
+      send_frame(words[BLACK_D2_OFF]);
     else if(command == 'T')
+      send_frame(words[BLACK_D3_ON]);
+    else if(command == 't')
+      send_frame(words[BLACK_D3_OFF]);
+
+    else if (command == 'U')
+      send_frame(rsl336T_words[RSL336T_INDEX(0,0,0)]);
+    else if (command == 'u')
+      send_frame(rsl336T_words[RSL336T_INDEX(0,0,1)]);
+    else if (command == 'V')
+      send_frame(rsl336T_words[RSL336T_INDEX(0,1,0)]);
+    else if (command == 'v')
+      send_frame(rsl336T_words[RSL336T_INDEX(0,1,1)]);
+    else if (command == 'W')
+      send_frame(rsl336T_words[RSL336T_INDEX(0,2,0)]);
+    else if (command == 'w')
+      send_frame(rsl336T_words[RSL336T_INDEX(0,2,1)]);
+    else if (command == 'X')
+      send_frame(rsl336T_words[RSL336T_INDEX(0,3,0)]);
+    else if (command == 'x')
+      send_frame(rsl336T_words[RSL336T_INDEX(0,3,1)]);
+    else if (command == 'Y')
+      send_frame(rsl336T_words[RSL336T_INDEX(1,0,0)]);
+    else if (command == 'y')
+      send_frame(rsl336T_words[RSL336T_INDEX(1,0,1)]);
+    else if (command == 'Z')
+      send_frame(rsl336T_words[RSL336T_INDEX(1,1,0)]);
+    else if (command == 'z')
+      send_frame(rsl336T_words[RSL336T_INDEX(1,1,1)]);
+
+    else if(command == '*')
     {
       sensorEchoCommand(command);
       printTemperature(onShieldTemp);
     }
-    else if(command == 'P')
+    else if(command == '?')
     {
       sensorEchoCommand(command);
       printLightLevel();
     }
     else if (command == '^')
-      flash_led(1, 2, 1, (1 << PANICLED_PWM_PIN));
-    else if (command == '0')
-      send_yamaha_ir_signal(YAMAHA_POWER_OFF);
+    {
+      //flash_led(1, 1, 2, 1, 0);
+      flash_led(1, 1, 1, 1, 0);
+      Serial.println("Ok");
+    }
+    else if (command == '&')
+    {
+      flash_led(0, 1, 2, 1, 0);
+      Serial.println("Ok");
+    }
     else if (command == '1')
-      send_yamaha_ir_signal(YAMAHA_POWER_TOGGLE);
+      send_yamaha_ir_signal(YAMAHA_CD);
     else if (command == '2')
-      send_yamaha_ir_signal(YAMAHA_VOLUME_UP);
+      send_yamaha_ir_signal(YAMAHA_TUNER);
     else if (command == '3')
-      send_yamaha_ir_signal(YAMAHA_VOLUME_DOWN);
+      send_yamaha_ir_signal(YAMAHA_TAPE);
     else if (command == '4')
-      send_yamaha_ir_signal(YAMAHA_MUTE);
+      send_yamaha_ir_signal(YAMAHA_DVD_SPDIF);
     else if (command == '5')
-      send_yamaha_ir_signal(YAMAHA_CD);
+      send_yamaha_ir_signal(YAMAHA_SAT_SPDIFF);
     else if (command == '6')
-      send_yamaha_ir_signal(YAMAHA_TUNER);
-    else if (command == '7')
-      send_yamaha_ir_signal(YAMAHA_DVD_SPDIF);
+      send_yamaha_ir_signal(YAMAHA_VCR);
+//    else if (command == '7')
+//      send_yamaha_ir_signal();
     else if (command == '8')
-      send_yamaha_ir_signal(YAMAHA_MENU);
+      send_yamaha_ir_signal(YAMAHA_AUX);
+    else if (command == '9')
+      send_yamaha_ir_signal(YAMAHA_EXT51DEC);
+    else if (command == '0')
+      send_yamaha_ir_signal(YAMAHA_TEST);
+    else if (command == '/')
+      send_yamaha_ir_signal(YAMAHA_TUNER_ABCDE);
+    else if (command == '\\')
+      send_yamaha_ir_signal(YAMAHA_EFFECT_TOGGLE);
+    else if (command == '-')
+      send_yamaha_ir_signal(YAMAHA_TUNER_MINUS);
     else if (command == '+')
+      send_yamaha_ir_signal(YAMAHA_TUNER_PLUS);
+    else if (command == ':')
+      send_yamaha_ir_signal(YAMAHA_POWER_OFF);
+    else if (command == '.')
+      send_yamaha_ir_signal(YAMAHA_POWER_TOGGLE);
+    else if (command == ';')
+      send_yamaha_ir_signal(YAMAHA_VOLUME_UP);
+    else if (command == ',')
+      send_yamaha_ir_signal(YAMAHA_VOLUME_DOWN);
+    else if (command == '_')
+      send_yamaha_ir_signal(YAMAHA_MUTE);
+    else if (command == '#')
+      send_yamaha_ir_signal(YAMAHA_MENU);
+    else if (command == '"')
       send_yamaha_ir_signal(YAMAHA_PLUS);
-    else if (command == '-')
+    else if (command == '!')
       send_yamaha_ir_signal(YAMAHA_MINUS);
-    else if (command == 0xa7) // ยง
-      send_yamaha_ir_signal(YAMAHA_TEST);
-    else if (command == '$')
+    else if (command == '=')
       send_yamaha_ir_signal(YAMAHA_TIME_LEVEL);
-    else if (command == '%')
-      send_yamaha_ir_signal(YAMAHA_EFFECT_TOGGLE);
-    else if (command == '&')
+    else if (command == '$')
       send_yamaha_ir_signal(YAMAHA_PRG_DOWN);
-    else if (command == '/')
+    else if (command == '%')
       send_yamaha_ir_signal(YAMAHA_PRG_UP);
     else if (command == '(')
-      send_yamaha_ir_signal(YAMAHA_TUNER_PLUS);
-    else if (command == '[')
-      send_yamaha_ir_signal(YAMAHA_TUNER_MINUS);
+      send_yamaha_ir_signal(YAMAHA_SLEEP);
     else if (command == ')')
-      send_yamaha_ir_signal(YAMAHA_TUNER_ABCDE);
-    else if (command == '9')
-      send_yamaha_ir_signal(YAMAHA_TAPE);
-    else if (command == '?')
-      send_yamaha_ir_signal(YAMAHA_VCR);
-    else if (command == '=')
-      send_yamaha_ir_signal(YAMAHA_EXT51DEC);
+      send_yamaha_ir_signal(YAMAHA_P5);
     else
       Serial.println("Error: unknown command");
   }