* Made static backlight pwm resolution configurable
* Made breathing backlighting configurable too
* Finished my ifdef
* Ran clang-format
* Added missing semi-colon
* Solved weird behaviour by right-shifting the right amount
* Made breathing period scaled on actual pwm frequency
* Made the low end deadzone scaled on the top value
* Moved 'pwm_frequency' declaration outside ifdef
* Fixed 'never used' error
* Fixed 'never used' error
* Fixed breathing ISR to 120Hz
* Removed pwm_frequency constant
Constant is no longer needed since running the breathing ISR at a fixed 120Hz
* Re-add brightness limiting
* re-introduce scaling
// reaches the backlight level, where we turn off the LEDs,
// reaches the backlight level, where we turn off the LEDs,
// but also an overflow interrupt when the counter rolls back to 0,
// but also an overflow interrupt when the counter rolls back to 0,
// in which we're going to turn on the LEDs.
// in which we're going to turn on the LEDs.
// The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz.
// The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz,
// or F_CPU/BACKLIGHT_CUSTOM_RESOLUTION if used.
// Triggered when the counter reaches the OCRx value
// Triggered when the counter reaches the OCRx value
ISR(TIMERx_COMPA_vect){backlight_pins_off();}
ISR(TIMERx_COMPA_vect){backlight_pins_off();}
// Triggered when the counter reaches the TOP value
// Triggered when the counter reaches the TOP value
// this one triggers at F_CPU/65536 =~ 244 Hz
// this one triggers at F_CPU/ICRx = 16MHz/65536 =~ 244 Hz
ISR(TIMERx_OVF_vect){
ISR(TIMERx_OVF_vect){
# ifdef BACKLIGHT_BREATHING
# ifdef BACKLIGHT_BREATHING
if(is_breathing()){
if(is_breathing()){
@ -220,8 +221,8 @@ ISR(TIMERx_OVF_vect) {
// artifacts (especially while breathing, because breathing_task
// artifacts (especially while breathing, because breathing_task
// takes many computation cycles).
// takes many computation cycles).
// so better not turn them on while the counter TOP is very low.
// so better not turn them on while the counter TOP is very low.
if(OCRxx>256){
if(OCRxx>ICRx/250+5){
backlight_pins_on();
FOR_EACH_LED(backlight_on(backlight_pin);)
}
}
}
}
@ -231,24 +232,26 @@ ISR(TIMERx_OVF_vect) {
// See http://jared.geek.nz/2013/feb/linear-led-pwm
// See http://jared.geek.nz/2013/feb/linear-led-pwm
staticuint16_tcie_lightness(uint16_tv){
staticuint16_tcie_lightness(uint16_tv){
if(v<=5243)// if below 8% of max
if(v<=ICRx/12)// If the value is less than or equal to ~8% of max
returnv/9;// same as dividing by 900%
{
else{
returnv/9;// Same as dividing by 900%
uint32_ty=(((uint32_t)v+10486)<<8)/(10486+0xFFFFUL);// add 16% of max and compare
}else{
// to get a useful result with integer division, we shift left in the expression above
// In the next two lines values are bit-shifted. This is to avoid loosing decimals in integer math.
// and revert what we've done again after squaring.
uint32_ty=(((uint32_t)v+ICRx/6)<<5)/(ICRx/6+ICRx);// If above 8%, add ~16% of max, and normalize with (max + ~16% max)
y=y*y*y>>8;
uint32_tout=(y*y*y*ICRx)>>15;// Cube it and undo the bit-shifting. (which is now three times as much due to the cubing)
if(y>0xFFFFUL)// prevent overflow
return0xFFFFU;
if(out>ICRx)// Avoid overflows
else
{
return(uint16_t)y;
out=ICRx;
}
returnout;
}
}
}
}
// rescale the supplied backlight value to be in terms of the value limit
// rescale the supplied backlight value to be in terms of the value limit // range for val is [0..ICRx]. PWM pin is high while the timer count is below val.
"In fast PWM mode, the compare units allow generation of PWM waveforms on the OCnx pins. Setting the COMnx1:0 bits to two will produce a non-inverted PWM [..]."
"In fast PWM mode, the compare units allow generation of PWM waveforms on the OCnx pins. Setting the COMnx1:0 bits to two will produce a non-inverted PWM [..]."
"In fast PWM mode the counter is incremented until the counter value matches either one of the fixed values 0x00FF, 0x01FF, or 0x03FF (WGMn3:0 = 5, 6, or 7), the value in ICRn (WGMn3:0 = 14), or the value in OCRnA (WGMn3:0 = 15)."
"In fast PWM mode the counter is incremented until the counter value matches either one of the fixed values 0x00FF, 0x01FF, or 0x03FF (WGMn3:0 = 5, 6, or 7), the value in ICRn (WGMn3:0 = 14), or the value in OCRnA (WGMn3:0 = 15)."