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