sleep_led.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /** \brief Sleep LED initialization
  20. *
  21. * FIXME: needs doc
  22. */
  23. void sleep_led_init(void) {
  24. /* Timer1 setup */
  25. /* CTC mode */
  26. TCCR1B |= _BV(WGM12);
  27. /* Clock selelct: clk/1 */
  28. TCCR1B |= _BV(CS10);
  29. /* Set TOP value */
  30. uint8_t sreg = SREG;
  31. cli();
  32. OCR1AH = (SLEEP_LED_TIMER_TOP >> 8) & 0xff;
  33. OCR1AL = SLEEP_LED_TIMER_TOP & 0xff;
  34. SREG = sreg;
  35. }
  36. /** \brief Sleep LED enable
  37. *
  38. * FIXME: needs doc
  39. */
  40. void sleep_led_enable(void) {
  41. /* Enable Compare Match Interrupt */
  42. TIMSK1 |= _BV(OCIE1A);
  43. }
  44. /** \brief Sleep LED disable
  45. *
  46. * FIXME: needs doc
  47. */
  48. void sleep_led_disable(void) {
  49. /* Disable Compare Match Interrupt */
  50. TIMSK1 &= ~_BV(OCIE1A);
  51. }
  52. /** \brief Sleep LED toggle
  53. *
  54. * FIXME: needs doc
  55. */
  56. void sleep_led_toggle(void) {
  57. /* Disable Compare Match Interrupt */
  58. TIMSK1 ^= _BV(OCIE1A);
  59. }
  60. /** \brief Breathing Sleep LED brighness(PWM On period) table
  61. *
  62. * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
  63. *
  64. * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
  65. * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
  66. */
  67. static const uint8_t breathing_table[64] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10, 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252, 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23, 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  68. ISR(TIMER1_COMPA_vect) {
  69. /* Software PWM
  70. * timer:1111 1111 1111 1111
  71. * \_____/\/ \_______/____ count(0-255)
  72. * \ \______________ duration of step(4)
  73. * \__________________ index of step table(0-63)
  74. */
  75. static union {
  76. uint16_t row;
  77. struct {
  78. uint8_t count : 8;
  79. uint8_t duration : 2;
  80. uint8_t index : 6;
  81. } pwm;
  82. } timer = {.row = 0};
  83. timer.row++;
  84. // LED on
  85. if (timer.pwm.count == 0) {
  86. led_set(1 << USB_LED_CAPS_LOCK);
  87. }
  88. // LED off
  89. if (timer.pwm.count == pgm_read_byte(&breathing_table[timer.pwm.index])) {
  90. led_set(0);
  91. }
  92. }