-
Notifications
You must be signed in to change notification settings - Fork 1
/
leds.c
86 lines (74 loc) · 1.43 KB
/
leds.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "globals.h"
#include "leds.h"
#ifdef DEBUG
uint8_t led_power = 254;
#else
//uint8_t led_power = 7;
//uint8_t led_power = 1;
//uint8_t led_power = 24;
//uint8_t led_power = 36;
// uint8_t led_power = 80; // Enough to wake me
uint8_t led_power = 30;
#endif
bool_t want_led[3];
bool_t leds_blocked;
void led_on(uint8_t led);
void led_off(uint8_t led);
void led_block() {
leds_blocked = true;
}
void led_unblock() {
leds_blocked = false;
}
void led_reset(void) {
int i;
for(i=0; i<3; i++) {
want_led[i] = false;
}
leds_blocked = false;
}
void led_want(uint8_t i) {
want_led[i] = true;
}
void led_handle(void) {
int i;
for(i=0; i<3; i++) {
if (i == 2 || !leds_blocked) {
if (want_led[i]) {
led_on(i);
} else {
led_off(i);
}
}
}
}
void led_init(void) {
// fast pwm mode (8 bit)
// clock, no scaling
TCCR1A = _BV(WGM10);
TCCR1B = _BV(WGM12) | _BV(CS10);
}
void led_on(uint8_t led) {
if (led == 0) {
//PORTB |= _BV(PORTB1);
OCR1A = led_power;
TCCR1A |= _BV(COM1A1);
} else if (led == 1) {
//PORTB |= _BV(PORTB2);
OCR1B = led_power;
TCCR1A |= _BV(COM1B1);
} else if (led == 2) {
PORTD |= _BV(PORTD3);
}
}
void led_off(uint8_t led) {
if (led == 0) {
//PORTB &= ~(_BV(PORTB1));
TCCR1A &= ~(_BV(COM1A1));
} else if (led == 1) {
//PORTB &= ~(_BV(PORTB2));
TCCR1A &= ~(_BV(COM1B1));
} else if (led == 2) {
PORTD &= ~(_BV(PORTD3));
}
}