Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sleep_led.c 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <stdint.h>
  2. #include <avr/io.h>
  3. #include <avr/interrupt.h>
  4. #include <avr/pgmspace.h>
  5. #include "led.h"
  6. #include "sleep_led.h"
  7. /* Software PWM
  8. * ______ ______ __
  9. * | ON |___OFF___| ON |___OFF___| ....
  10. * |<-------------->|<-------------->|<- ....
  11. * PWM period PWM period
  12. *
  13. * 256 interrupts/period[resolution]
  14. * 64 periods/second[frequency]
  15. * 256*64 interrupts/second
  16. * F_CPU/(256*64) clocks/interrupt
  17. */
  18. #define SLEEP_LED_TIMER_TOP F_CPU/(256*64)
  19. void sleep_led_init(void)
  20. {
  21. /* Timer1 setup */
  22. /* CTC mode */
  23. TCCR1B |= _BV(WGM12);
  24. /* Clock selelct: clk/1 */
  25. TCCR1B |= _BV(CS10);
  26. /* Set TOP value */
  27. uint8_t sreg = SREG;
  28. cli();
  29. OCR1AH = (SLEEP_LED_TIMER_TOP>>8)&0xff;
  30. OCR1AL = SLEEP_LED_TIMER_TOP&0xff;
  31. SREG = sreg;
  32. }
  33. void sleep_led_enable(void)
  34. {
  35. /* Enable Compare Match Interrupt */
  36. TIMSK1 |= _BV(OCIE1A);
  37. }
  38. void sleep_led_disable(void)
  39. {
  40. /* Disable Compare Match Interrupt */
  41. TIMSK1 &= ~_BV(OCIE1A);
  42. }
  43. __attribute__ ((weak))
  44. void sleep_led_on(void)
  45. {
  46. led_set(1<<USB_LED_CAPS_LOCK);
  47. }
  48. __attribute__ ((weak))
  49. void sleep_led_off(void)
  50. {
  51. led_set(0);
  52. }
  53. /* Breathing Sleep LED brighness(PWM On period) table
  54. * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
  55. *
  56. * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
  57. * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
  58. */
  59. static const uint8_t breathing_table[64] PROGMEM = {
  60. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
  61. 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
  62. 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
  63. 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  64. };
  65. ISR(TIMER1_COMPA_vect)
  66. {
  67. /* Software PWM
  68. * timer:1111 1111 1111 1111
  69. * \_____/\/ \_______/____ count(0-255)
  70. * \ \______________ duration of step(4)
  71. * \__________________ index of step table(0-63)
  72. */
  73. static union {
  74. uint16_t row;
  75. struct {
  76. uint8_t count:8;
  77. uint8_t duration:2;
  78. uint8_t index:6;
  79. } pwm;
  80. } timer = { .row = 0 };
  81. timer.row++;
  82. // LED on
  83. if (timer.pwm.count == 0) {
  84. sleep_led_on();
  85. }
  86. // LED off
  87. if (timer.pwm.count == pgm_read_byte(&breathing_table[timer.pwm.index])) {
  88. sleep_led_off();
  89. }
  90. }