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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

breathing_led.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <avr/pgmspace.h>
  4. #include "led.h"
  5. #include "breathing_led.h"
  6. #include "backlight.h"
  7. #include "debug.h"
  8. #define BREATHING_LED_TIMER_TOP F_CPU/64
  9. breathing_led_config_t breathing_led_config;
  10. void breathing_led_init(void)
  11. {
  12. /* Timer3 setup */
  13. /* CTC mode */
  14. TCCR3B |= _BV(WGM12);
  15. /* Clock selelct: clk/1 */
  16. TCCR3B |= _BV(CS10);
  17. /* Set TOP value */
  18. uint8_t sreg = SREG;
  19. cli();
  20. OCR3AH = (BREATHING_LED_TIMER_TOP>>8)&0xff;
  21. OCR3AL = BREATHING_LED_TIMER_TOP&0xff;
  22. SREG = sreg;
  23. }
  24. bool breathing_led_is_enabled(void)
  25. {
  26. return breathing_led_config.enable;
  27. }
  28. void breathing_led_enable(void)
  29. {
  30. /* Enable Compare Match Interrupt */
  31. TIMSK3 |= _BV(OCIE3A);
  32. breathing_led_config.enable = true;
  33. dprintf("breathing led on: %u\n", breathing_led_config.enable);
  34. eeconfig_write_breathing_led(breathing_led_config.raw);
  35. }
  36. void breathing_led_disable(void)
  37. {
  38. /* Disable Compare Match Interrupt */
  39. TIMSK3 &= ~_BV(OCIE3A);
  40. breathing_led_config.enable = false;
  41. dprintf("breathing led off: %u\n", breathing_led_config.enable);
  42. eeconfig_write_breathing_led(breathing_led_config.raw);
  43. }
  44. void breathing_led_toggle(void)
  45. {
  46. /* Disable Compare Match Interrupt */
  47. TIMSK3 ^= _BV(OCIE3A);
  48. breathing_led_config.enable ^= 1;
  49. dprintf("breathing led toggle: %u\n", breathing_led_config.enable);
  50. eeconfig_write_breathing_led(breathing_led_config.raw);
  51. }
  52. void breathing_led_increase(void)
  53. {
  54. if (breathing_led_config.enable) {
  55. if (breathing_led_config.level < BREATHING_LED_LEVELS) {
  56. breathing_led_config.level++;
  57. eeconfig_write_breathing_led(breathing_led_config.raw);
  58. }
  59. dprintf("breathing led speed increase: %u\n", breathing_led_config.level);
  60. }
  61. }
  62. void breathing_led_decrease(void)
  63. {
  64. if (breathing_led_config.enable) {
  65. if (breathing_led_config.level > 0)
  66. {
  67. breathing_led_config.level--;
  68. eeconfig_write_breathing_led(breathing_led_config.raw);
  69. }
  70. dprintf("breathing led speed decrease: %u\n", breathing_led_config.level);
  71. }
  72. }
  73. /* Breathing LED brighness(PWM On period) table
  74. * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
  75. *
  76. * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
  77. * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
  78. */
  79. static const uint8_t breathing_table[64] PROGMEM = {
  80. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
  81. 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
  82. 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
  83. 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  84. };
  85. ISR(TIMER3_COMPA_vect)
  86. {
  87. uint8_t index = 0;
  88. uint8_t step = 0;
  89. step++;
  90. if (step >= BREATHING_LED_LEVELS - breathing_led_config.level) {
  91. step = 0;
  92. index++;
  93. backlight_set_raw(pgm_read_byte(&breathing_table[index]));
  94. }
  95. }