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.

softpwm_led.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include "led.h"
  4. #include "softpwm_led.h"
  5. #include "debug.h"
  6. #define SOFTPWM_LED_FREQ 64
  7. #define SOFTPWM_LED_TIMER_TOP F_CPU / (256 * SOFTPWM_LED_FREQ)
  8. static uint8_t softpwm_state = 0;
  9. static uint8_t softpwm_ocr = 0;
  10. static uint8_t softpwm_ocr_buff = 0;
  11. void softpwm_led_init(void)
  12. {
  13. #ifdef SOFTPWM_LED_TIMER3
  14. /* Timer3 setup */
  15. /* CTC mode */
  16. TCCR3B |= (1<<WGM32);
  17. /* Clock selelct: clk/8 */
  18. TCCR3B |= (1<<CS30);
  19. /* Set TOP value */
  20. uint8_t sreg = SREG;
  21. cli();
  22. OCR3AH = (SOFTPWM_LED_TIMER_TOP >> 8) & 0xff;
  23. OCR3AL = SOFTPWM_LED_TIMER_TOP & 0xff;
  24. SREG = sreg;
  25. #else
  26. /* Timer1 setup */
  27. /* CTC mode */
  28. TCCR1B |= (1<<WGM12);
  29. /* Clock selelct: clk/8 */
  30. TCCR1B |= (1<<CS10);
  31. /* Set TOP value */
  32. uint8_t sreg = SREG;
  33. cli();
  34. OCR1AH = (SOFTPWM_LED_TIMER_TOP >> 8) & 0xff;
  35. OCR1AL = SOFTPWM_LED_TIMER_TOP & 0xff;
  36. SREG = sreg;
  37. #endif
  38. }
  39. void softpwm_led_enable(void)
  40. {
  41. /* Enable Compare Match Interrupt */
  42. #ifdef SOFTPWM_LED_TIMER3
  43. TIMSK3 |= (1<<OCIE3A);
  44. //dprintf("softpwm led on: %u\n", TIMSK3 & (1<<OCIE3A));
  45. #else
  46. TIMSK1 |= (1<<OCIE1A);
  47. //dprintf("softpwm led on: %u\n", TIMSK1 & (1<<OCIE1A));
  48. #endif
  49. softpwm_state = 1;
  50. softpwm_led_state_change(softpwm_state);
  51. }
  52. void softpwm_led_disable(void)
  53. {
  54. /* Disable Compare Match Interrupt */
  55. #ifdef SOFTPWM_LED_TIMER3
  56. TIMSK3 &= ~(1<<OCIE3A);
  57. //dprintf("softpwm led off: %u\n", TIMSK3 & (1<<OCIE3A));
  58. #else
  59. TIMSK1 &= ~(1<<OCIE1A);
  60. //dprintf("softpwm led off: %u\n", TIMSK1 & (1<<OCIE1A));
  61. #endif
  62. softpwm_state = 0;
  63. softpwm_led_off();
  64. softpwm_led_state_change(softpwm_state);
  65. }
  66. void softpwm_led_toggle(void)
  67. {
  68. /* Disable Compare Match Interrupt */
  69. #ifdef SOFTPWM_LED_TIMER3
  70. TIMSK3 ^= (1<<OCIE3A);
  71. //dprintf("softpwm led toggle: %u\n", TIMSK3 & (1<<OCIE3A));
  72. #else
  73. TIMSK1 ^= (1<<OCIE1A);
  74. //dprintf("softpwm led toggle: %u\n", TIMSK1 & (1<<OCIE1A));
  75. #endif
  76. softpwm_state ^= 1;
  77. if (!softpwm_state) softpwm_led_off();
  78. softpwm_led_state_change(softpwm_state);
  79. }
  80. void softpwm_led_set(uint8_t val)
  81. {
  82. softpwm_ocr_buff = val;
  83. }
  84. inline uint8_t softpwm_led_get_state(void)
  85. {
  86. return softpwm_state;
  87. }
  88. #ifdef BREATHING_LED_ENABLE
  89. /* Breathing LED brighness(PWM On period) table
  90. *
  91. * 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
  92. * Table[floor((exp(sin(x/256*2*pi+3/2*pi))-1/e)*(256/(e-1/e))), {x,0,255,1}]
  93. * (0..255).each {|x| print ((exp(sin(x/256.0*2*PI+3.0/2*PI))-1/E)*(256/(E-1/E))).to_i, ', ' }
  94. */
  95. static const uint8_t breathing_table[256] PROGMEM = {
  96. 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
  97. };
  98. static uint8_t breathing_led_state = 0;
  99. static uint8_t breathing_led_duration = 0;
  100. void breathing_led_enable(void)
  101. {
  102. breathing_led_state = 1;
  103. }
  104. void breathing_led_disable(void)
  105. {
  106. breathing_led_state = 0;
  107. }
  108. void breathing_led_toggle(void)
  109. {
  110. breathing_led_state ^= 1;
  111. }
  112. void breathing_led_set_duration(uint8_t dur)
  113. {
  114. breathing_led_duration = dur;
  115. //dprintf("breathing led set duration: %u\n", breathing_led_duration);
  116. }
  117. #endif
  118. #ifdef SOFTPWM_LED_TIMER3
  119. ISR(TIMER3_COMPA_vect)
  120. #else
  121. ISR(TIMER1_COMPA_vect)
  122. #endif
  123. {
  124. static uint8_t pwm = 0;
  125. pwm++;
  126. // LED on
  127. if (pwm == 0) {
  128. softpwm_led_on();
  129. softpwm_ocr = softpwm_ocr_buff;
  130. }
  131. // LED off
  132. if (pwm == softpwm_ocr) {
  133. softpwm_led_off();
  134. }
  135. #ifdef BREATHING_LED_ENABLE
  136. static uint8_t count = 0;
  137. static uint8_t index = 0;
  138. static uint8_t step = 0;
  139. if (breathing_led_state) {
  140. count++;
  141. if (count > SOFTPWM_LED_FREQ) {
  142. count = 0;
  143. step++;
  144. if (step > breathing_led_duration) {
  145. step = 0;
  146. softpwm_ocr_buff = pgm_read_byte(&breathing_table[index]);
  147. index++;
  148. }
  149. }
  150. }
  151. #endif
  152. }