-
Notifications
You must be signed in to change notification settings - Fork 1
/
led.c
63 lines (50 loc) · 1.97 KB
/
led.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
/* Copyright (c) 2015 Antumbra
This program 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
#include "led.h"
#include "option.h"
#include <avr/io.h>
#include <util/atomic.h>
#define MULTIPLIER_OPTION_ID 0x4d554c54
static uint8_t numer, denom;
void led_init(void)
{
DDRB &= ~(_BV(DDB5) | _BV(DDB6) | _BV(DDB7));
PORTB &= ~(_BV(PORTB5) | _BV(PORTB6) | _BV(PORTB7));
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1C1) | _BV(WGM11);
TCCR1B = _BV(WGM13) | _BV(WGM12);
ICR1 = 0x3ff; // 10 bit
OCR1A = 0;
OCR1B = 0;
OCR1C = 0;
TIMSK1 = 0;
TCNT1 = 0;
TCCR1B |= _BV(CS10);
}
uint8_t multdef[2] = {4, 5};
option_get(MULTIPLIER_OPTION_ID, multdef, sizeof multdef);
numer = multdef[0];
denom = multdef[1];
led_set_rgb(0, 0, 0);
}
void led_set_rgb(uint16_t r, uint16_t g, uint16_t b)
{
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
OCR1A = ((uint32_t)r * numer / denom) >> 6;
OCR1B = ((uint32_t)g * numer / denom) >> 6;
OCR1C = ((uint32_t)b * numer / denom) >> 6;
/* 0% duty cycle is not typically possible with AVR fast PWM. Special-case 0 to tri-state the sink pins. */
DDRB = r == 0 ? DDRB & ~_BV(DDB5) : DDRB | _BV(DDB5);
DDRB = g == 0 ? DDRB & ~_BV(DDB6) : DDRB | _BV(DDB6);
DDRB = b == 0 ? DDRB & ~_BV(DDB7) : DDRB | _BV(DDB7);
}
}