led.c 7.2 KB

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