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 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include "led.h"
  4. #include "breathing_led.h"
  5. #include "debug.h"
  6. #define BREATHING_LED_TIMER_TOP F_CPU/256
  7. static uint8_t breathing_led_duration = 0;
  8. void breathing_led_init(void)
  9. {
  10. #ifdef BREATHING_LED_TIMER1
  11. /* Timer1 setup */
  12. /* CTC mode */
  13. TCCR1B |= (1<<WGM12);
  14. /* Clock selelct: clk/8 */
  15. TCCR1B |= (1<<CS10);
  16. /* Set TOP value */
  17. uint8_t sreg = SREG;
  18. cli();
  19. OCR1AH = (BREATHING_LED_TIMER_TOP>>8)&0xff;
  20. OCR1AL = BREATHING_LED_TIMER_TOP&0xff;
  21. #else
  22. /* Timer3 setup */
  23. /* CTC mode */
  24. TCCR3B |= (1<<WGM32);
  25. /* Clock selelct: clk/8 */
  26. TCCR3B |= (1<<CS30);
  27. /* Set TOP value */
  28. uint8_t sreg = SREG;
  29. cli();
  30. OCR3AH = (BREATHING_LED_TIMER_TOP>>8)&0xff;
  31. OCR3AL = BREATHING_LED_TIMER_TOP&0xff;
  32. #endif
  33. SREG = sreg;
  34. }
  35. void breathing_led_enable(void)
  36. {
  37. /* Enable Compare Match Interrupt */
  38. #ifdef BREATHING_LED_TIMER1
  39. TIMSK1 |= (1<<OCIE1A);
  40. dprintf("breathing led on: %u\n", TIMSK1 & (1<<OCIE1A));
  41. #else
  42. TIMSK3 |= (1<<OCIE3A);
  43. dprintf("breathing led on: %u\n", TIMSK3 & (1<<OCIE3A));
  44. #endif
  45. }
  46. void breathing_led_disable(void)
  47. {
  48. /* Disable Compare Match Interrupt */
  49. #ifdef BREATHING_LED_TIMER1
  50. TIMSK1 &= ~(1<<OCIE1A);
  51. dprintf("breathing led off: %u\n", TIMSK1 & (1<<OCIE1A));
  52. #else
  53. TIMSK3 &= ~(1<<OCIE3A);
  54. dprintf("breathing led off: %u\n", TIMSK3 & (1<<OCIE3A));
  55. #endif
  56. }
  57. void breathing_led_toggle(void)
  58. {
  59. /* Disable Compare Match Interrupt */
  60. #ifdef BREATHING_LED_TIMER1
  61. TIMSK1 ^= (1<<OCIE1A);
  62. dprintf("breathing led toggle: %u\n", TIMSK1 & (1<<OCIE1A));
  63. #else
  64. TIMSK3 ^= (1<<OCIE3A);
  65. dprintf("breathing led toggle: %u\n", TIMSK3 & (1<<OCIE3A));
  66. #endif
  67. }
  68. void breathing_led_set_duration(uint8_t dur)
  69. {
  70. breathing_led_duration = dur;
  71. dprintf("breathing led set duration: %u\n", breathing_led_duration);
  72. }
  73. /* Breathing LED brighness(PWM On period) table
  74. *
  75. * http://www.wolframalpha.com/input/?i=Table%5Bfloor%28%28exp%28sin%28x%2F256*2*pi%2B3%2F2*pi%29%29-1%2Fe%29*%28256%2F%28e-1%2Fe%29%29%29%2C+%7Bx%2C0%2C255%2C1%7D%5D
  76. * Table[floor((exp(sin(x/256*2*pi+3/2*pi))-1/e)*(256/(e-1/e))), {x,0,255,1}]
  77. * (0..255).each {|x| print ((exp(sin(x/256.0*2*PI+3.0/2*PI))-1/E)*(256/(E-1/E))).to_i, ', ' }
  78. */
  79. static const uint8_t breathing_table[256] PROGMEM = {
  80. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 9, 10, 11, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 29, 30, 32, 34, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 56, 58, 61, 63, 66, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, 102, 105, 108, 112, 116, 119, 123, 126, 130, 134, 138, 142, 145, 149, 153, 157, 161, 165, 169, 173, 176, 180, 184, 188, 192, 195, 199, 203, 206, 210, 213, 216, 219, 223, 226, 228, 231, 234, 236, 239, 241, 243, 245, 247, 248, 250, 251, 252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 254, 253, 252, 251, 250, 248, 247, 245, 243, 241, 239, 236, 234, 231, 228, 226, 223, 219, 216, 213, 210, 206, 203, 199, 195, 192, 188, 184, 180, 176, 173, 169, 165, 161, 157, 153, 149, 145, 142, 138, 134, 130, 126, 123, 119, 116, 112, 108, 105, 102, 98, 95, 92, 89, 86, 83, 80, 77, 74, 71, 68, 66, 63, 61, 58, 56, 53, 51, 49, 47, 45, 43, 41, 39, 37, 35, 34, 32, 30, 29, 27, 26, 25, 23, 22, 21, 19, 18, 17, 16, 15, 14, 13, 12, 11, 11, 10, 9, 8, 8, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
  81. };
  82. #ifdef BREATHING_LED_TIMER1
  83. ISR(TIMER1_COMPA_vect)
  84. #else
  85. ISR(TIMER3_COMPA_vect)
  86. #endif
  87. {
  88. static uint8_t index = 0;
  89. static uint8_t step = 0;
  90. step++;
  91. if (step > breathing_led_duration) {
  92. step = 0;
  93. breathing_led_set_raw(pgm_read_byte(&breathing_table[index]));
  94. index++;
  95. }
  96. }