led.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. Copyright 2012 Jun Wako <wakojun@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "hal.h"
  15. #include "led_custom.h"
  16. #include "keyboard.h"
  17. #include "printf.h"
  18. static void breathing_callback(PWMDriver *pwmp);
  19. static PWMConfig pwmCFG = {
  20. 0xFFFF, /* PWM clock frequency */
  21. 256, /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */
  22. NULL, /* No Callback */
  23. {
  24. {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* Enable Channel 0 */
  25. {PWM_OUTPUT_DISABLED, NULL},
  26. {PWM_OUTPUT_DISABLED, NULL},
  27. {PWM_OUTPUT_DISABLED, NULL}
  28. },
  29. 0, /* HW dependent part.*/
  30. 0
  31. };
  32. static PWMConfig pwmCFG_breathing = {
  33. 0xFFFF, /* 10kHz PWM clock frequency */
  34. 256, /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */
  35. breathing_callback, /* Breathing Callback */
  36. {
  37. {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* Enable Channel 0 */
  38. {PWM_OUTPUT_DISABLED, NULL},
  39. {PWM_OUTPUT_DISABLED, NULL},
  40. {PWM_OUTPUT_DISABLED, NULL}
  41. },
  42. 0, /* HW dependent part.*/
  43. 0
  44. };
  45. // See http://jared.geek.nz/2013/feb/linear-led-pwm
  46. static uint16_t cie_lightness(uint16_t v) {
  47. if (v <= 5243) // if below 8% of max
  48. return v / 9; // same as dividing by 900%
  49. else {
  50. uint32_t y = (((uint32_t) v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
  51. // to get a useful result with integer division, we shift left in the expression above
  52. // and revert what we've done again after squaring.
  53. y = y * y * y >> 8;
  54. if (y > 0xFFFFUL) // prevent overflow
  55. return 0xFFFFU;
  56. else
  57. return (uint16_t) y;
  58. }
  59. }
  60. uint8_t get_backlight_level(void){
  61. return kb_backlight_config.level;
  62. }
  63. void backlight_init_ports(void) {
  64. printf("backlight_init_ports()\n");
  65. palSetPadMode(GPIOA, 6, PAL_MODE_ALTERNATE(1));
  66. pwmStart(&PWMD3, &pwmCFG);
  67. // pwmEnableChannel(&PWMD3, 0, PWM_FRACTION_TO_WIDTH(&PWMD3, 0xFFFF,cie_lightness(0xFFFF)));
  68. if(kb_backlight_config.enable){
  69. backlight_set(kb_backlight_config.level);
  70. if(kb_backlight_config.breathing){
  71. breathing_enable();
  72. }
  73. } else {
  74. backlight_set(0);
  75. }
  76. }
  77. void backlight_set(uint8_t level) {
  78. printf("backlight_set(%d)\n", level);
  79. uint32_t duty = (uint32_t)(cie_lightness(0xFFFF * (uint32_t) level / BACKLIGHT_LEVELS));
  80. printf("duty: (%d)\n", duty);
  81. if (level == 0) {
  82. // Turn backlight off
  83. pwmDisableChannel(&PWMD3, 0);
  84. } else {
  85. // Turn backlight on
  86. if(!is_breathing()){
  87. pwmEnableChannel(&PWMD3, 0, PWM_FRACTION_TO_WIDTH(&PWMD3,0xFFFF,duty));
  88. }
  89. }
  90. }
  91. uint8_t backlight_tick = 0;
  92. void backlight_task(void) {
  93. }
  94. #define BREATHING_NO_HALT 0
  95. #define BREATHING_HALT_OFF 1
  96. #define BREATHING_HALT_ON 2
  97. #define BREATHING_STEPS 128
  98. static uint8_t breathing_period = BREATHING_PERIOD;
  99. static uint8_t breathing_halt = BREATHING_NO_HALT;
  100. static uint16_t breathing_counter = 0;
  101. bool is_breathing(void) {
  102. return PWMD3.config == &pwmCFG_breathing;
  103. }
  104. #define breathing_min() do {breathing_counter = 0;} while (0)
  105. #define breathing_max() do {breathing_counter = breathing_period * 256 / 2;} while (0)
  106. void breathing_interrupt_enable(void){
  107. pwmStop(&PWMD3);
  108. pwmStart(&PWMD3, &pwmCFG_breathing);
  109. chSysLockFromISR();
  110. pwmEnablePeriodicNotification(&PWMD3);
  111. pwmEnableChannelI(
  112. &PWMD3,
  113. 0,
  114. PWM_FRACTION_TO_WIDTH(
  115. &PWMD3,
  116. 0xFFFF,
  117. 0xFFFF
  118. )
  119. );
  120. chSysUnlockFromISR();
  121. }
  122. void breathing_interrupt_disable(void){
  123. pwmStop(&PWMD3);
  124. pwmStart(&PWMD3, &pwmCFG);
  125. }
  126. void breathing_enable(void)
  127. {
  128. breathing_counter = 0;
  129. breathing_halt = BREATHING_NO_HALT;
  130. breathing_interrupt_enable();
  131. }
  132. void breathing_pulse(void)
  133. {
  134. if (get_backlight_level() == 0)
  135. breathing_min();
  136. else
  137. breathing_max();
  138. breathing_halt = BREATHING_HALT_ON;
  139. breathing_interrupt_enable();
  140. }
  141. void breathing_disable(void)
  142. {
  143. printf("breathing_disable()\n");
  144. breathing_interrupt_disable();
  145. // Restore backlight level
  146. backlight_set(get_backlight_level());
  147. }
  148. void breathing_self_disable(void)
  149. {
  150. if (get_backlight_level() == 0)
  151. breathing_halt = BREATHING_HALT_OFF;
  152. else
  153. breathing_halt = BREATHING_HALT_ON;
  154. }
  155. void breathing_toggle(void) {
  156. if (is_breathing()){
  157. printf("disable breathing\n");
  158. breathing_disable();
  159. } else {
  160. printf("enable breathing\n");
  161. breathing_enable();
  162. }
  163. }
  164. void breathing_period_set(uint8_t value)
  165. {
  166. if (!value)
  167. value = 1;
  168. breathing_period = value;
  169. }
  170. void breathing_period_default(void) {
  171. breathing_period_set(BREATHING_PERIOD);
  172. }
  173. void breathing_period_inc(void)
  174. {
  175. breathing_period_set(breathing_period+1);
  176. }
  177. void breathing_period_dec(void)
  178. {
  179. breathing_period_set(breathing_period-1);
  180. }
  181. /* To generate breathing curve in python:
  182. * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
  183. */
  184. 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};
  185. // Use this before the cie_lightness function.
  186. static inline uint16_t scale_backlight(uint16_t v) {
  187. return v / BACKLIGHT_LEVELS * get_backlight_level();
  188. }
  189. static void breathing_callback(PWMDriver *pwmp)
  190. {
  191. (void)pwmp;
  192. uint16_t interval = (uint16_t) breathing_period * 256 / BREATHING_STEPS;
  193. // resetting after one period to prevent ugly reset at overflow.
  194. breathing_counter = (breathing_counter + 1) % (breathing_period * 256);
  195. uint8_t index = breathing_counter / interval % BREATHING_STEPS;
  196. if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) ||
  197. ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1)))
  198. {
  199. breathing_interrupt_disable();
  200. }
  201. uint32_t duty = cie_lightness(scale_backlight(breathing_table[index] * 256));
  202. chSysLockFromISR();
  203. pwmEnableChannelI(
  204. &PWMD3,
  205. 0,
  206. PWM_FRACTION_TO_WIDTH(
  207. &PWMD3,
  208. 0xFFFF,
  209. duty
  210. )
  211. );
  212. chSysUnlockFromISR();
  213. }