2014-07-18 02:15:30 +00:00
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/interrupt.h>
|
2015-04-20 06:15:00 +00:00
|
|
|
#include <avr/pgmspace.h>
|
2014-07-18 02:15:30 +00:00
|
|
|
#include "led.h"
|
|
|
|
#include "softpwm_led.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
#define SOFTPWM_LED_FREQ 64
|
|
|
|
#define SOFTPWM_LED_TIMER_TOP F_CPU / (256 * SOFTPWM_LED_FREQ)
|
|
|
|
|
2014-08-02 01:15:57 +00:00
|
|
|
static uint8_t softpwm_led_state = 0;
|
|
|
|
static uint8_t softpwm_led_ocr[LED_COUNT] = {0};
|
|
|
|
static uint8_t softpwm_led_ocr_buff[LED_COUNT] = {0};
|
2014-07-18 02:15:30 +00:00
|
|
|
|
2015-04-22 02:56:39 +00:00
|
|
|
#ifdef FADING_LED_ENABLE
|
|
|
|
static void fading_led_proc(void);
|
|
|
|
#else
|
|
|
|
#define fading_led_proc()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BREATHING_LED_ENABLE
|
|
|
|
static void breathing_led_proc(void);
|
|
|
|
#else
|
|
|
|
#define fading_led_proc()
|
|
|
|
#endif
|
|
|
|
|
2014-08-03 03:00:18 +00:00
|
|
|
void softpwm_init(void)
|
2014-07-18 02:15:30 +00:00
|
|
|
{
|
|
|
|
#ifdef SOFTPWM_LED_TIMER3
|
|
|
|
/* Timer3 setup */
|
|
|
|
/* CTC mode */
|
|
|
|
TCCR3B |= (1<<WGM32);
|
|
|
|
/* Clock selelct: clk/8 */
|
|
|
|
TCCR3B |= (1<<CS30);
|
|
|
|
/* Set TOP value */
|
|
|
|
uint8_t sreg = SREG;
|
|
|
|
cli();
|
|
|
|
OCR3AH = (SOFTPWM_LED_TIMER_TOP >> 8) & 0xff;
|
|
|
|
OCR3AL = SOFTPWM_LED_TIMER_TOP & 0xff;
|
|
|
|
SREG = sreg;
|
|
|
|
#else
|
|
|
|
/* Timer1 setup */
|
|
|
|
/* CTC mode */
|
|
|
|
TCCR1B |= (1<<WGM12);
|
|
|
|
/* Clock selelct: clk/8 */
|
|
|
|
TCCR1B |= (1<<CS10);
|
|
|
|
/* Set TOP value */
|
|
|
|
uint8_t sreg = SREG;
|
|
|
|
cli();
|
|
|
|
OCR1AH = (SOFTPWM_LED_TIMER_TOP >> 8) & 0xff;
|
|
|
|
OCR1AL = SOFTPWM_LED_TIMER_TOP & 0xff;
|
|
|
|
SREG = sreg;
|
|
|
|
#endif
|
2014-08-03 03:00:18 +00:00
|
|
|
softpwm_led_init();
|
2014-07-18 02:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void softpwm_led_enable(void)
|
|
|
|
{
|
|
|
|
/* Enable Compare Match Interrupt */
|
|
|
|
#ifdef SOFTPWM_LED_TIMER3
|
|
|
|
TIMSK3 |= (1<<OCIE3A);
|
2014-07-20 03:52:56 +00:00
|
|
|
//dprintf("softpwm led on: %u\n", TIMSK3 & (1<<OCIE3A));
|
2014-07-18 02:15:30 +00:00
|
|
|
#else
|
|
|
|
TIMSK1 |= (1<<OCIE1A);
|
2014-07-20 03:52:56 +00:00
|
|
|
//dprintf("softpwm led on: %u\n", TIMSK1 & (1<<OCIE1A));
|
2014-07-18 02:15:30 +00:00
|
|
|
#endif
|
2014-08-02 01:15:57 +00:00
|
|
|
softpwm_led_state = 1;
|
|
|
|
#ifdef LEDMAP_ENABLE
|
|
|
|
softpwm_led_state_change(softpwm_led_state);
|
|
|
|
#endif
|
2014-07-18 02:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void softpwm_led_disable(void)
|
|
|
|
{
|
|
|
|
/* Disable Compare Match Interrupt */
|
|
|
|
#ifdef SOFTPWM_LED_TIMER3
|
|
|
|
TIMSK3 &= ~(1<<OCIE3A);
|
2014-07-20 03:52:56 +00:00
|
|
|
//dprintf("softpwm led off: %u\n", TIMSK3 & (1<<OCIE3A));
|
2014-07-18 02:15:30 +00:00
|
|
|
#else
|
|
|
|
TIMSK1 &= ~(1<<OCIE1A);
|
2014-07-20 03:52:56 +00:00
|
|
|
//dprintf("softpwm led off: %u\n", TIMSK1 & (1<<OCIE1A));
|
2014-07-18 02:15:30 +00:00
|
|
|
#endif
|
2014-08-02 01:15:57 +00:00
|
|
|
softpwm_led_state = 0;
|
|
|
|
for (uint8_t i = 0; i < LED_COUNT; i++) {
|
|
|
|
softpwm_led_off(i);
|
|
|
|
}
|
|
|
|
#ifdef LEDMAP_ENABLE
|
|
|
|
softpwm_led_state_change(softpwm_led_state);
|
|
|
|
#endif
|
2014-07-18 02:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void softpwm_led_toggle(void)
|
|
|
|
{
|
2014-08-02 01:15:57 +00:00
|
|
|
if (softpwm_led_state) {
|
|
|
|
softpwm_led_disable();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
softpwm_led_enable();
|
|
|
|
}
|
2014-07-18 02:15:30 +00:00
|
|
|
}
|
|
|
|
|
2014-08-02 01:15:57 +00:00
|
|
|
void softpwm_led_set(uint8_t index, uint8_t val)
|
2014-07-25 05:24:57 +00:00
|
|
|
{
|
2014-08-02 01:15:57 +00:00
|
|
|
softpwm_led_ocr_buff[index] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void softpwm_led_set_all(uint8_t val)
|
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < LED_COUNT; i++) {
|
|
|
|
softpwm_led_ocr_buff[i] = val;
|
|
|
|
}
|
2014-07-18 02:15:30 +00:00
|
|
|
}
|
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
void softpwm_led_increase(uint8_t index, uint8_t offset)
|
|
|
|
{
|
|
|
|
if (softpwm_led_ocr_buff[index] > 0xFF - offset) {
|
|
|
|
softpwm_led_ocr_buff[index] = 0xFF;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
softpwm_led_ocr_buff[index] += offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void softpwm_led_increase_all(uint8_t offset)
|
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < LED_COUNT; i++) {
|
|
|
|
softpwm_led_increase(i, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void softpwm_led_decrease(uint8_t index, uint8_t offset)
|
|
|
|
{
|
|
|
|
if (softpwm_led_ocr_buff[index] < offset) {
|
|
|
|
softpwm_led_ocr_buff[index] = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
softpwm_led_ocr_buff[index] -= offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void softpwm_led_decrease_all(uint8_t offset)
|
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < LED_COUNT; i++) {
|
|
|
|
softpwm_led_decrease(i, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 05:24:57 +00:00
|
|
|
inline uint8_t softpwm_led_get_state(void)
|
|
|
|
{
|
2014-08-02 01:15:57 +00:00
|
|
|
return softpwm_led_state;
|
2014-07-25 05:24:57 +00:00
|
|
|
}
|
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
#ifdef FADING_LED_ENABLE
|
2014-07-18 02:15:30 +00:00
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
static led_pack_t fading_led_state = 0;
|
|
|
|
static led_pack_t fading_led_direction = 0;
|
|
|
|
static uint8_t fading_led_duration = 0;
|
2014-07-18 02:15:30 +00:00
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
void fading_led_enable(uint8_t index)
|
2014-08-02 01:15:57 +00:00
|
|
|
{
|
2014-08-25 05:03:23 +00:00
|
|
|
LED_BIT_SET(fading_led_state, index);
|
2014-08-02 01:15:57 +00:00
|
|
|
}
|
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
void fading_led_enable_all(void)
|
2014-08-02 01:15:57 +00:00
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < LED_COUNT; i++) {
|
2014-08-25 05:03:23 +00:00
|
|
|
LED_BIT_SET(fading_led_state, i);
|
2014-08-02 01:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-18 02:15:30 +00:00
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
void fading_led_disable(uint8_t index)
|
2014-07-18 02:15:30 +00:00
|
|
|
{
|
2014-08-25 05:03:23 +00:00
|
|
|
LED_BIT_CLEAR(fading_led_state, index);
|
2014-07-18 02:15:30 +00:00
|
|
|
}
|
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
void fading_led_disable_all(void)
|
2014-07-18 02:15:30 +00:00
|
|
|
{
|
2014-08-25 05:03:23 +00:00
|
|
|
fading_led_state = 0;
|
2014-07-18 02:15:30 +00:00
|
|
|
}
|
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
void fading_led_toggle(uint8_t index)
|
2014-07-18 02:15:30 +00:00
|
|
|
{
|
2014-08-25 05:03:23 +00:00
|
|
|
LED_BIT_XOR(fading_led_state, index);
|
2014-07-18 02:15:30 +00:00
|
|
|
}
|
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
void fading_led_toggle_all(void)
|
2014-07-18 02:15:30 +00:00
|
|
|
{
|
2014-08-02 01:15:57 +00:00
|
|
|
for (uint8_t i = 0; i < LED_COUNT; i++) {
|
2014-08-25 05:03:23 +00:00
|
|
|
LED_BIT_XOR(fading_led_state, i);
|
2014-08-02 01:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
void fading_led_set_direction(uint8_t dir)
|
2014-08-22 10:18:12 +00:00
|
|
|
{
|
2014-08-25 05:03:23 +00:00
|
|
|
fading_led_direction = dir;
|
2014-08-22 10:18:12 +00:00
|
|
|
}
|
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
void fading_led_set_duration(uint8_t dur)
|
2014-08-22 10:18:12 +00:00
|
|
|
{
|
2014-08-25 05:03:23 +00:00
|
|
|
fading_led_duration = dur;
|
2014-08-22 10:18:12 +00:00
|
|
|
}
|
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BREATHING_LED_ENABLE
|
|
|
|
|
|
|
|
/* Breathing LED brighness(PWM On period) table
|
|
|
|
*
|
|
|
|
* http://www.wolframalpha.com/input/?i=Table%5Bfloor%28%28exp%28sin%28x%2F256*2*pi%2B3%2F2*pi%29%29-1%2Fe%29*%28256%2F%28e-1%2Fe%29%29%29%2C+%7Bx%2C0%2C255%2C1%7D%5D
|
|
|
|
* Table[floor((exp(sin(x/256*2*pi+3/2*pi))-1/e)*(256/(e-1/e))), {x,0,255,1}]
|
|
|
|
* (0..255).each {|x| print ((exp(sin(x/256.0*2*PI+3.0/2*PI))-1/E)*(256/(E-1/E))).to_i, ', ' }
|
|
|
|
*/
|
|
|
|
static const uint8_t breathing_table[128] PROGMEM = {
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 9, 10, 11, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 29, 30, 32, 34, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 56, 58, 61, 63, 66, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, 102, 105, 108, 112, 116, 119, 123, 126, 130, 134, 138, 142, 145, 149, 153, 157, 161, 165, 169, 173, 176, 180, 184, 188, 192, 195, 199, 203, 206, 210, 213, 216, 219, 223, 226, 228, 231, 234, 236, 239, 241, 243, 245, 247, 248, 250, 251, 252, 253, 254, 255, 255, 255
|
|
|
|
};
|
|
|
|
|
|
|
|
static led_pack_t breathing_led_state = 0;
|
|
|
|
static uint8_t breathing_led_duration = 0;
|
|
|
|
|
|
|
|
void breathing_led_enable(uint8_t index)
|
|
|
|
{
|
|
|
|
LED_BIT_SET(breathing_led_state, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void breathing_led_enable_all(void)
|
2014-08-22 10:18:12 +00:00
|
|
|
{
|
2014-08-25 05:03:23 +00:00
|
|
|
for (uint8_t i = 0; i < LED_COUNT; i++) {
|
|
|
|
LED_BIT_SET(breathing_led_state, i);
|
2014-08-22 10:18:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
void breathing_led_disable(uint8_t index)
|
2014-08-02 01:15:57 +00:00
|
|
|
{
|
2014-08-25 05:03:23 +00:00
|
|
|
LED_BIT_CLEAR(breathing_led_state, index);
|
2014-07-18 02:15:30 +00:00
|
|
|
}
|
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
void breathing_led_disable_all(void)
|
2014-08-22 10:18:12 +00:00
|
|
|
{
|
2014-08-25 05:03:23 +00:00
|
|
|
breathing_led_state = 0;
|
2014-08-22 10:18:12 +00:00
|
|
|
}
|
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
void breathing_led_toggle(uint8_t index)
|
2014-08-22 10:18:12 +00:00
|
|
|
{
|
2014-08-25 05:03:23 +00:00
|
|
|
LED_BIT_XOR(breathing_led_state, index);
|
2014-08-22 10:18:12 +00:00
|
|
|
}
|
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
void breathing_led_toggle_all(void)
|
2014-08-22 10:18:12 +00:00
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < LED_COUNT; i++) {
|
2014-08-25 05:03:23 +00:00
|
|
|
LED_BIT_XOR(breathing_led_state, i);
|
2014-08-22 10:18:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
void breathing_led_set_duration(uint8_t dur)
|
2014-08-02 01:15:57 +00:00
|
|
|
{
|
2014-08-25 05:03:23 +00:00
|
|
|
breathing_led_duration = dur;
|
2014-08-02 01:15:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 02:15:30 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SOFTPWM_LED_TIMER3
|
|
|
|
ISR(TIMER3_COMPA_vect)
|
|
|
|
#else
|
|
|
|
ISR(TIMER1_COMPA_vect)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
static uint8_t pwm = 0;
|
|
|
|
pwm++;
|
2014-08-28 00:59:53 +00:00
|
|
|
for (uint8_t i = 0; i < LED_COUNT; i++) {
|
|
|
|
// LED on
|
|
|
|
if (pwm == 0) {
|
2015-04-22 09:49:49 +00:00
|
|
|
if (softpwm_led_ocr[i]) softpwm_led_on(i);
|
2014-08-02 01:15:57 +00:00
|
|
|
softpwm_led_ocr[i] = softpwm_led_ocr_buff[i];
|
|
|
|
}
|
2014-08-28 00:59:53 +00:00
|
|
|
// LED off
|
2014-08-02 01:15:57 +00:00
|
|
|
if (pwm == softpwm_led_ocr[i]) {
|
|
|
|
softpwm_led_off(i);
|
|
|
|
}
|
2014-07-18 02:15:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-22 02:56:39 +00:00
|
|
|
#if defined(FADING_LED_ENABLE) || defined(BREATHING_LED_ENABLE) || defined(CUSTOM_LED_ENABLE)
|
|
|
|
static uint8_t counter = 0;
|
|
|
|
if (++counter >= SOFTPWM_LED_FREQ) {
|
|
|
|
counter = 0;
|
|
|
|
fading_led_proc();
|
|
|
|
breathing_led_proc();
|
2015-04-22 09:49:49 +00:00
|
|
|
softpwm_led_custom();
|
2015-04-22 02:56:39 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-08-25 05:03:23 +00:00
|
|
|
#ifdef FADING_LED_ENABLE
|
2015-04-22 02:56:39 +00:00
|
|
|
void fading_led_proc(void)
|
|
|
|
{
|
|
|
|
static uint8_t step = 0;
|
2014-08-25 05:03:23 +00:00
|
|
|
if (fading_led_state) {
|
2015-04-22 02:56:39 +00:00
|
|
|
if (++step > fading_led_duration) {
|
|
|
|
step = 0;
|
|
|
|
for (uint8_t i = 0; i < LED_COUNT; i++) {
|
|
|
|
if (fading_led_state & LED_BIT(i)) {
|
|
|
|
if (fading_led_direction) {
|
|
|
|
softpwm_led_decrease(i, 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
softpwm_led_increase(i, 1);
|
2014-08-02 01:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-18 02:15:30 +00:00
|
|
|
}
|
2015-04-22 09:49:49 +00:00
|
|
|
#ifdef CUSTOM_LED_ENABLE
|
|
|
|
fading_led_custom(softpwm_led_ocr);
|
|
|
|
#endif
|
2014-07-18 02:15:30 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-22 02:56:39 +00:00
|
|
|
}
|
2014-07-18 02:15:30 +00:00
|
|
|
#endif
|
2014-08-25 05:03:23 +00:00
|
|
|
|
|
|
|
#ifdef BREATHING_LED_ENABLE
|
2015-04-22 02:56:39 +00:00
|
|
|
void breathing_led_proc(void)
|
|
|
|
{
|
|
|
|
static uint8_t step = 0;
|
|
|
|
static uint8_t index = 0;
|
|
|
|
static uint8_t direction = 0;
|
2014-08-25 05:03:23 +00:00
|
|
|
if (breathing_led_state) {
|
2015-04-22 02:56:39 +00:00
|
|
|
if (++step > breathing_led_duration) {
|
|
|
|
step = 0;
|
|
|
|
uint8_t value = pgm_read_byte(&breathing_table[index]);
|
|
|
|
for (uint8_t i = 0; i < LED_COUNT; i++) {
|
|
|
|
if (breathing_led_state & LED_BIT(i)) {
|
|
|
|
softpwm_led_ocr_buff[i] = value;
|
2014-08-25 05:03:23 +00:00
|
|
|
}
|
2015-04-22 02:56:39 +00:00
|
|
|
}
|
2015-04-22 09:49:49 +00:00
|
|
|
#ifdef CUSTOM_LED_ENABLE
|
|
|
|
breathing_led_custom(softpwm_led_ocr);
|
|
|
|
#endif
|
2015-04-22 02:56:39 +00:00
|
|
|
if (direction) {
|
|
|
|
if (index == 0) {
|
|
|
|
direction = 0;
|
2014-08-25 05:03:23 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-04-22 02:56:39 +00:00
|
|
|
index--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (index == 0x7F) {
|
|
|
|
direction = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
index++;
|
2014-08-25 05:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-18 02:15:30 +00:00
|
|
|
}
|
2015-04-22 02:56:39 +00:00
|
|
|
#endif
|