+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;
+unsigned int flash_led_selected_=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))
+ {
+ if (flash_led_time_)
+ analogWrite(PANICLED_PWM_PIN, c);
+ else
+ analogWrite(PANICLED_PWM_PIN, 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)
+{
+ 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)
+ {
+ analogWrite(PANICLED_PWM_PIN,255); //off
+ analogWrite(BLUELED_PWM_PIN,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;
+}
+
+//********************************************************************//
+
+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
+
+ irsend.sendNEC(code,YAMAHA_CODE_BITS);
+
+ 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");
+}
+
+//********************************************************************//
+