-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathleds.c
131 lines (120 loc) · 2.8 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* This file is part of ukbdc.
*
* ukbdc is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* ukbdc is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ukbdc; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <avr/interrupt.h>
#include "leds.h"
#include "io.h"
#include "platforms.h"
#include "auxiliary.h"
#include "system.h"
#include "timer.h"
void LED_init()
{
for (int i = 0; i < NUM_LEDS; ++i) {
if (leds[i].pin == -1)
continue;
IO_config(leds[i].pin, OUTPUT);
IO_set(leds[i].pin, true);
}
TCCR0A = 0x00;
TCCR0B = 0x01;
TIMSK0 = _BV(TOIE0);
extern void LED_timer_slow_handler();
int led_tmr = TIMER_add(256, true);
SYSTEM_subscribe(TIMER, led_tmr, LED_timer_slow_handler);
}
void LED_set(uint8_t led, bool state)
{
if (state == leds[led].state)
return;
leds[led].state = state;
if (state) {
leds[led].action = ACTION_UP;
} else {
leds[led].action = ACTION_DOWN;
}
}
bool LED_stable(uint8_t led)
{
return leds[led].action == ACTION_NORMAL;
}
bool LED_all_stable()
{
for (int i = 0; i < NUM_LEDS; ++i) {
if (leds[i].pin == -1)
continue;
if (!LED_stable(i))
return false;
}
return true;
}
void LED_set_indicators(uint8_t hid_leds)
{
/* FIXME: take led numbers from config, when config API is implemented */
if (hid_leds & CAPS_MASK)
LED_set(1, true);
else
LED_set(1, false);
}
static const int brightness[] = {0, 1, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64,
96, 128, 192};
void LED_timer_slow_handler()
{
for (int i = 0; i < NUM_LEDS; ++i) {
if (leds[i].pin == -1)
continue;
switch (leds[i].action) {
case ACTION_DOWN:
if (--leds[i].level < 0) {
leds[i].level = 0;
leds[i].action = ACTION_NORMAL;
}
break;
case ACTION_UP:
if (++leds[i].level > (int)ARR_SZ(brightness) - 1) {
leds[i].level = ARR_SZ(brightness) - 1;
leds[i].action = ACTION_NORMAL;
}
break;
default:
break;
}
}
}
void LED_timer_pwm_handler()
{
static int phase = 0;
if (phase > 192) {
phase = 0;
/* all leds off */
for (int i = 0; i < NUM_LEDS; ++i) {
if (leds[i].pin == -1)
continue;
IO_set(leds[i].pin, true);
}
}
for (int i = 0; i < NUM_LEDS; ++i) {
if (leds[i].pin == -1)
continue;
if (193 - brightness[leds[i].level] == phase)
IO_set(leds[i].pin, false);
}
++phase;
}
ISR(TIMER0_OVF_vect)
{
LED_timer_pwm_handler();
}