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);
48 void sleep_led_toggle(void)
50 /* Disable Compare Match Interrupt */
51 TIMSK1 ^= _BV(OCIE1A);
55 /* Breathing Sleep LED brighness(PWM On period) table
56 * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
58 * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
59 * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
61 static const uint8_t breathing_table[64] PROGMEM = {
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
63 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
64 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
65 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
68 ISR(TIMER1_COMPA_vect)
71 * timer:1111 1111 1111 1111
72 * \_____/\/ \_______/____ count(0-255)
73 * \ \______________ duration of step(4)
74 * \__________________ index of step table(0-63)
83 } timer = { .row = 0 };
88 if (timer.pwm.count == 0) {
89 led_set(1<<USB_LED_CAPS_LOCK);
92 if (timer.pwm.count == pgm_read_byte(&breathing_table[timer.pwm.index])) {