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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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_led_state = 0;
  9. static uint8_t softpwm_led_ocr[LED_COUNT] = {0};
  10. static uint8_t softpwm_led_ocr_buff[LED_COUNT] = {0};
  11. void softpwm_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. softpwm_led_init();
  39. }
  40. void softpwm_led_enable(void)
  41. {
  42. /* Enable Compare Match Interrupt */
  43. #ifdef SOFTPWM_LED_TIMER3
  44. TIMSK3 |= (1<<OCIE3A);
  45. //dprintf("softpwm led on: %u\n", TIMSK3 & (1<<OCIE3A));
  46. #else
  47. TIMSK1 |= (1<<OCIE1A);
  48. //dprintf("softpwm led on: %u\n", TIMSK1 & (1<<OCIE1A));
  49. #endif
  50. softpwm_led_state = 1;
  51. #ifdef LEDMAP_ENABLE
  52. softpwm_led_state_change(softpwm_led_state);
  53. #endif
  54. }
  55. void softpwm_led_disable(void)
  56. {
  57. /* Disable Compare Match Interrupt */
  58. #ifdef SOFTPWM_LED_TIMER3
  59. TIMSK3 &= ~(1<<OCIE3A);
  60. //dprintf("softpwm led off: %u\n", TIMSK3 & (1<<OCIE3A));
  61. #else
  62. TIMSK1 &= ~(1<<OCIE1A);
  63. //dprintf("softpwm led off: %u\n", TIMSK1 & (1<<OCIE1A));
  64. #endif
  65. softpwm_led_state = 0;
  66. for (uint8_t i = 0; i < LED_COUNT; i++) {
  67. softpwm_led_off(i);
  68. }
  69. #ifdef LEDMAP_ENABLE
  70. softpwm_led_state_change(softpwm_led_state);
  71. #endif
  72. }
  73. void softpwm_led_toggle(void)
  74. {
  75. if (softpwm_led_state) {
  76. softpwm_led_disable();
  77. }
  78. else {
  79. softpwm_led_enable();
  80. }
  81. }
  82. void softpwm_led_set(uint8_t index, uint8_t val)
  83. {
  84. softpwm_led_ocr_buff[index] = val;
  85. }
  86. void softpwm_led_set_all(uint8_t val)
  87. {
  88. for (uint8_t i = 0; i < LED_COUNT; i++) {
  89. softpwm_led_ocr_buff[i] = val;
  90. }
  91. }
  92. void softpwm_led_increase(uint8_t index, uint8_t offset)
  93. {
  94. if (softpwm_led_ocr_buff[index] > 0xFF - offset) {
  95. softpwm_led_ocr_buff[index] = 0xFF;
  96. }
  97. else {
  98. softpwm_led_ocr_buff[index] += offset;
  99. }
  100. }
  101. void softpwm_led_increase_all(uint8_t offset)
  102. {
  103. for (uint8_t i = 0; i < LED_COUNT; i++) {
  104. softpwm_led_increase(i, offset);
  105. }
  106. }
  107. void softpwm_led_decrease(uint8_t index, uint8_t offset)
  108. {
  109. if (softpwm_led_ocr_buff[index] < offset) {
  110. softpwm_led_ocr_buff[index] = 0;
  111. }
  112. else {
  113. softpwm_led_ocr_buff[index] -= offset;
  114. }
  115. }
  116. void softpwm_led_decrease_all(uint8_t offset)
  117. {
  118. for (uint8_t i = 0; i < LED_COUNT; i++) {
  119. softpwm_led_decrease(i, offset);
  120. }
  121. }
  122. inline uint8_t softpwm_led_get_state(void)
  123. {
  124. return softpwm_led_state;
  125. }
  126. #ifdef FADING_LED_ENABLE
  127. static led_pack_t fading_led_state = 0;
  128. static led_pack_t fading_led_direction = 0;
  129. static uint8_t fading_led_duration = 0;
  130. void fading_led_enable(uint8_t index)
  131. {
  132. LED_BIT_SET(fading_led_state, index);
  133. }
  134. void fading_led_enable_all(void)
  135. {
  136. for (uint8_t i = 0; i < LED_COUNT; i++) {
  137. LED_BIT_SET(fading_led_state, i);
  138. }
  139. }
  140. void fading_led_disable(uint8_t index)
  141. {
  142. LED_BIT_CLEAR(fading_led_state, index);
  143. }
  144. void fading_led_disable_all(void)
  145. {
  146. fading_led_state = 0;
  147. }
  148. void fading_led_toggle(uint8_t index)
  149. {
  150. LED_BIT_XOR(fading_led_state, index);
  151. }
  152. void fading_led_toggle_all(void)
  153. {
  154. for (uint8_t i = 0; i < LED_COUNT; i++) {
  155. LED_BIT_XOR(fading_led_state, i);
  156. }
  157. }
  158. void fading_led_set_direction(uint8_t dir)
  159. {
  160. fading_led_direction = dir;
  161. }
  162. void fading_led_set_duration(uint8_t dur)
  163. {
  164. fading_led_duration = dur;
  165. }
  166. #endif
  167. #ifdef BREATHING_LED_ENABLE
  168. /* Breathing LED brighness(PWM On period) table
  169. *
  170. * 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
  171. * Table[floor((exp(sin(x/256*2*pi+3/2*pi))-1/e)*(256/(e-1/e))), {x,0,255,1}]
  172. * (0..255).each {|x| print ((exp(sin(x/256.0*2*PI+3.0/2*PI))-1/E)*(256/(E-1/E))).to_i, ', ' }
  173. */
  174. static const uint8_t breathing_table[128] PROGMEM = {
  175. 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
  176. };
  177. static led_pack_t breathing_led_state = 0;
  178. static uint8_t breathing_led_duration = 0;
  179. void breathing_led_enable(uint8_t index)
  180. {
  181. LED_BIT_SET(breathing_led_state, index);
  182. }
  183. void breathing_led_enable_all(void)
  184. {
  185. for (uint8_t i = 0; i < LED_COUNT; i++) {
  186. LED_BIT_SET(breathing_led_state, i);
  187. }
  188. }
  189. void breathing_led_disable(uint8_t index)
  190. {
  191. LED_BIT_CLEAR(breathing_led_state, index);
  192. }
  193. void breathing_led_disable_all(void)
  194. {
  195. breathing_led_state = 0;
  196. }
  197. void breathing_led_toggle(uint8_t index)
  198. {
  199. LED_BIT_XOR(breathing_led_state, index);
  200. }
  201. void breathing_led_toggle_all(void)
  202. {
  203. for (uint8_t i = 0; i < LED_COUNT; i++) {
  204. LED_BIT_XOR(breathing_led_state, i);
  205. }
  206. }
  207. void breathing_led_set_duration(uint8_t dur)
  208. {
  209. breathing_led_duration = dur;
  210. }
  211. #endif
  212. #ifdef SOFTPWM_LED_TIMER3
  213. ISR(TIMER3_COMPA_vect)
  214. #else
  215. ISR(TIMER1_COMPA_vect)
  216. #endif
  217. {
  218. static uint8_t pwm = 0;
  219. pwm++;
  220. // LED on
  221. if (pwm == 0) {
  222. for (uint8_t i = 0; i < LED_COUNT; i++) {
  223. softpwm_led_on(i);
  224. softpwm_led_ocr[i] = softpwm_led_ocr_buff[i];
  225. }
  226. }
  227. // LED off
  228. for (uint8_t i = 0; i < LED_COUNT; i++) {
  229. if (pwm == softpwm_led_ocr[i]) {
  230. softpwm_led_off(i);
  231. }
  232. }
  233. #ifdef FADING_LED_ENABLE
  234. static uint8_t fading_led_counter = 0;
  235. static uint8_t fading_led_step = 0;
  236. if (fading_led_state) {
  237. if (++fading_led_counter > SOFTPWM_LED_FREQ) {
  238. fading_led_counter = 0;
  239. if (++fading_led_step > fading_led_duration) {
  240. fading_led_step = 0;
  241. for (uint8_t i = 0; i < LED_COUNT; i++) {
  242. if (fading_led_state & LED_BIT(i)) {
  243. if (fading_led_direction) {
  244. softpwm_led_decrease(i, 1);
  245. }
  246. else {
  247. softpwm_led_increase(i, 1);
  248. }
  249. }
  250. }
  251. }
  252. }
  253. }
  254. #endif
  255. #ifdef BREATHING_LED_ENABLE
  256. static uint8_t breathing_led_counter = 0;
  257. static uint8_t breathing_led_step = 0;
  258. static uint8_t breathing_led_index = 0;
  259. static uint8_t breathing_led_direction = 0;
  260. if (breathing_led_state) {
  261. if (++breathing_led_counter > SOFTPWM_LED_FREQ) {
  262. breathing_led_counter = 0;
  263. if (++breathing_led_step > breathing_led_duration) {
  264. breathing_led_step = 0;
  265. uint8_t value = pgm_read_byte(&breathing_table[breathing_led_index]);
  266. for (uint8_t i = 0; i < LED_COUNT; i++) {
  267. if (breathing_led_state & LED_BIT(i)) {
  268. softpwm_led_ocr_buff[i] = value;
  269. }
  270. }
  271. if (breathing_led_direction) {
  272. if (breathing_led_index == 0) {
  273. breathing_led_direction = 0;
  274. }
  275. else {
  276. breathing_led_index--;
  277. }
  278. }
  279. else {
  280. if (breathing_led_index == 0x7F) {
  281. breathing_led_direction = 1;
  282. }
  283. else {
  284. breathing_led_index++;
  285. }
  286. }
  287. }
  288. }
  289. }
  290. #endif
  291. }