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.

112 lines
2.6 KiB

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