qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

backlight_timer.c (5259B)


      1 #include "backlight.h"
      2 #include "backlight_driver_common.h"
      3 #include "wait.h"
      4 
      5 #ifndef BACKLIGHT_GPT_DRIVER
      6 #    define BACKLIGHT_GPT_DRIVER GPTD15
      7 #endif
      8 
      9 // Platform specific implementations
     10 static void     backlight_timer_configure(bool enable);
     11 static void     backlight_timer_set_duty(uint16_t duty);
     12 static uint16_t backlight_timer_get_duty(void);
     13 
     14 // See http://jared.geek.nz/2013/feb/linear-led-pwm
     15 static uint16_t cie_lightness(uint16_t v) {
     16     if (v <= 5243)    // if below 8% of max
     17         return v / 9; // same as dividing by 900%
     18     else {
     19         uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
     20         // to get a useful result with integer division, we shift left in the expression above
     21         // and revert what we've done again after squaring.
     22         y = y * y * y >> 8;
     23         if (y > 0xFFFFUL) // prevent overflow
     24             return 0xFFFFU;
     25         else
     26             return (uint16_t)y;
     27     }
     28 }
     29 
     30 void backlight_init_ports(void) {
     31     backlight_pins_init();
     32 
     33     backlight_set(get_backlight_level());
     34 
     35 #ifdef BACKLIGHT_BREATHING
     36     if (is_backlight_breathing()) {
     37         breathing_enable();
     38     }
     39 #endif
     40 }
     41 
     42 void backlight_set(uint8_t level) {
     43     if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS;
     44 
     45     backlight_pins_off();
     46 
     47     backlight_timer_set_duty(cie_lightness(0xFFFFU / BACKLIGHT_LEVELS * level));
     48     backlight_timer_configure(level != 0);
     49 }
     50 
     51 static void backlight_timer_top(void) {
     52 #ifdef BACKLIGHT_BREATHING
     53     if (is_breathing()) {
     54         breathing_task();
     55     }
     56 #endif
     57 
     58     if (backlight_timer_get_duty() > 256) {
     59         backlight_pins_on();
     60     }
     61 }
     62 
     63 static void backlight_timer_cmp(void) {
     64     backlight_pins_off();
     65 }
     66 
     67 void backlight_task(void) {}
     68 
     69 #ifdef BACKLIGHT_BREATHING
     70 #    define BREATHING_STEPS 128
     71 
     72 static bool     breathing         = false;
     73 static uint16_t breathing_counter = 0;
     74 
     75 /* To generate breathing curve in python:
     76  * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
     77  */
     78 static const uint8_t breathing_table[BREATHING_STEPS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 17, 20, 24, 28, 32, 36, 41, 46, 51, 57, 63, 70, 76, 83, 91, 98, 106, 113, 121, 129, 138, 146, 154, 162, 170, 178, 185, 193, 200, 207, 213, 220, 225, 231, 235, 240, 244, 247, 250, 252, 253, 254, 255, 254, 253, 252, 250, 247, 244, 240, 235, 231, 225, 220, 213, 207, 200, 193, 185, 178, 170, 162, 154, 146, 138, 129, 121, 113, 106, 98, 91, 83, 76, 70, 63, 57, 51, 46, 41, 36, 32, 28, 24, 20, 17, 15, 12, 10, 8, 6, 5, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
     79 
     80 // Use this before the cie_lightness function.
     81 static inline uint16_t scale_backlight(uint16_t v) {
     82     return v / BACKLIGHT_LEVELS * get_backlight_level();
     83 }
     84 
     85 void breathing_task(void) {
     86     uint8_t  breathing_period = get_breathing_period();
     87     uint16_t interval         = (uint16_t)breathing_period * 256 / BREATHING_STEPS;
     88     // resetting after one period to prevent ugly reset at overflow.
     89     breathing_counter = (breathing_counter + 1) % (breathing_period * 256);
     90     uint8_t index     = breathing_counter / interval % BREATHING_STEPS;
     91 
     92     // printf("index:%u\n", index);
     93 
     94     backlight_timer_set_duty(cie_lightness(scale_backlight((uint16_t)breathing_table[index] * 256)));
     95 }
     96 
     97 bool is_breathing(void) {
     98     return breathing;
     99 }
    100 
    101 void breathing_enable(void) {
    102     breathing_counter = 0;
    103     breathing         = true;
    104 }
    105 void breathing_disable(void) {
    106     breathing = false;
    107 }
    108 
    109 void breathing_pulse(void) {
    110     backlight_set(is_backlight_enabled() ? 0 : BACKLIGHT_LEVELS);
    111     wait_ms(10);
    112     backlight_set(is_backlight_enabled() ? get_backlight_level() : 0);
    113 }
    114 #endif
    115 
    116 #ifdef PROTOCOL_CHIBIOS
    117 // On Platforms where timers fire every tick and have no capture/top events
    118 //   - fake event in the normal timer callback
    119 uint16_t s_duty = 0;
    120 
    121 static void timerCallback(void) {
    122     /* Software PWM
    123      * timer:1111 1111 1111 1111
    124      *       \______/| \_______/____  count(0-255)
    125      *          \    \______________  unused(1)
    126      *           \__________________  index of step table(0-127)
    127      */
    128 
    129     // this works for cca 65536 irqs/sec
    130     static union {
    131         uint16_t raw;
    132         struct {
    133             uint16_t count : 8;
    134             uint8_t  dummy : 1;
    135             uint8_t  index : 7;
    136         } pwm;
    137     } timer = {.raw = 0};
    138 
    139     timer.raw++;
    140 
    141     if (timer.pwm.count == 0) {
    142         // LED on
    143         backlight_timer_top();
    144     } else if (timer.pwm.count == (s_duty / 256)) {
    145         // LED off
    146         backlight_timer_cmp();
    147     }
    148 }
    149 
    150 static void backlight_timer_set_duty(uint16_t duty) {
    151     s_duty = duty;
    152 }
    153 static uint16_t backlight_timer_get_duty(void) {
    154     return s_duty;
    155 }
    156 
    157 // ChibiOS - Map GPT timer onto Software PWM
    158 static void gptTimerCallback(GPTDriver *gptp) {
    159     (void)gptp;
    160     timerCallback();
    161 }
    162 
    163 static void backlight_timer_configure(bool enable) {
    164     static const GPTConfig gptcfg = {1000000, gptTimerCallback, 0, 0};
    165 
    166     static bool s_init = false;
    167     if (!s_init) {
    168         gptStart(&BACKLIGHT_GPT_DRIVER, &gptcfg);
    169         s_init = true;
    170     }
    171 
    172     if (enable) {
    173         gptStartContinuous(&BACKLIGHT_GPT_DRIVER, gptcfg.frequency / 0xFFFF);
    174     } else {
    175         gptStopTimer(&BACKLIGHT_GPT_DRIVER);
    176     }
    177 }
    178 #endif