]>
git.gir.st - tmk_keyboard.git/blob - common/sleep_led.c
3 #include <avr/interrupt.h>
4 #include <avr/pgmspace.h>
10 * | ON |___OFF___| ON |___OFF___| ....
11 * |<-------------->|<-------------->|<- ....
12 * PWM period PWM period
14 * 256 interrupts/period[resolution]
15 * 64 periods/second[frequency]
16 * 256*64 interrupts/second
17 * F_CPU/(256*64) clocks/interrupt
19 #define SLEEP_LED_TIMER_TOP F_CPU/(256*64)
21 void sleep_led_init(void)
26 /* Clock selelct: clk/1 */
31 OCR1AH
= (SLEEP_LED_TIMER_TOP
>>8)&0xff;
32 OCR1AL
= SLEEP_LED_TIMER_TOP
&0xff;
36 void sleep_led_enable(void)
38 /* Enable Compare Match Interrupt */
39 TIMSK1
|= _BV(OCIE1A
);
42 void sleep_led_disable(void)
44 /* Disable Compare Match Interrupt */
45 TIMSK1
&= ~_BV(OCIE1A
);
49 /* Breathing Sleep LED brighness(PWM On period) table
50 * (32[steps] * 8[duration]) / 64[PWM periods/s] = 4 second breath cycle
52 static const uint8_t breathing_table
[32] PROGMEM
= {
53 0, 0, 0, 2, 9, 21, 37, 56, 78, 127, 151, 175, 197, 216, 232, 244,
54 254, 244, 216, 197, 175, 151, 127, 78, 56, 37, 21, 9, 2, 0, 0, 0
57 ISR(TIMER1_COMPA_vect
)
60 * timer:1111 1111 1111 1111
61 * \----/\-/ \-------/+--- count(0-255)
62 * | +--------------- duration of step(8)
63 * +-------------------- index of step table(0-31)
72 } timer
= { .row
= 0 };
77 if (timer
.pwm
.count
== 0) {
78 led_set(1<<USB_LED_CAPS_LOCK
);
81 if (timer
.pwm
.count
== pgm_read_byte(&breathing_table
[timer
.pwm
.index
])) {