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 /** \brief Sleep LED initialization
25 void sleep_led_init(void)
30 /* Clock selelct: clk/1 */
35 OCR1AH = (SLEEP_LED_TIMER_TOP>>8)&0xff;
36 OCR1AL = SLEEP_LED_TIMER_TOP&0xff;
40 /** \brief Sleep LED enable
44 void sleep_led_enable(void)
46 /* Enable Compare Match Interrupt */
47 TIMSK1 |= _BV(OCIE1A);
50 /** \brief Sleep LED disable
54 void sleep_led_disable(void)
56 /* Disable Compare Match Interrupt */
57 TIMSK1 &= ~_BV(OCIE1A);
60 /** \brief Sleep LED toggle
64 void sleep_led_toggle(void)
66 /* Disable Compare Match Interrupt */
67 TIMSK1 ^= _BV(OCIE1A);
71 /** \brief Breathing Sleep LED brighness(PWM On period) table
73 * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
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 }
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
85 ISR(TIMER1_COMPA_vect)
88 * timer:1111 1111 1111 1111
89 * \_____/\/ \_______/____ count(0-255)
90 * \ \______________ duration of step(4)
91 * \__________________ index of step table(0-63)
100 } timer = { .row = 0 };
105 if (timer.pwm.count == 0) {
106 led_set(1<<USB_LED_CAPS_LOCK);
109 if (timer.pwm.count == pgm_read_byte(&breathing_table[timer.pwm.index])) {