2014-11-19 23:57:35 +01:00
|
|
|
|
/*
|
|
|
|
|
* Led.h
|
|
|
|
|
*
|
|
|
|
|
* Created: 03.11.2013 17:11:58
|
|
|
|
|
* Author: BlubbFish
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef STRIPE_H_
|
|
|
|
|
#define STRIPE_H_
|
|
|
|
|
|
|
|
|
|
#include "hardware/pin.hpp"
|
|
|
|
|
|
|
|
|
|
template <typename PortR, int pin_red, typename PortG, int pin_green, typename PortB, int pin_blue>
|
|
|
|
|
class Stripe {
|
|
|
|
|
public:
|
|
|
|
|
Stripe() {
|
2015-02-08 00:22:32 +01:00
|
|
|
|
this->init();
|
2014-11-19 23:57:35 +01:00
|
|
|
|
}
|
|
|
|
|
void color(uint8_t r, uint8_t g, uint8_t b) {
|
2015-02-10 12:05:04 +01:00
|
|
|
|
this->sp(0, r); //ROT
|
|
|
|
|
this->sp(1, g); //GR<47>N
|
|
|
|
|
this->sp(2, b); //BLAU
|
2014-11-19 23:57:35 +01:00
|
|
|
|
}
|
|
|
|
|
void setcolor(uint8_t mask) {
|
2015-02-10 12:05:04 +01:00
|
|
|
|
(mask & (1<<0)) ? this->sp(2, 0xFF) : this->sp(2, 0x00); //Blau
|
|
|
|
|
(mask & (1<<1)) ? this->sp(1, 0xFF) : this->sp(1, 0x00); //Gr<47>n
|
|
|
|
|
(mask & (1<<2)) ? this->sp(0, 0xFF) : this->sp(0, 0x00); //Rot
|
2014-11-19 23:57:35 +01:00
|
|
|
|
}
|
|
|
|
|
static const uint8_t BLACK = 0;
|
|
|
|
|
static const uint8_t BLUE = 1;
|
|
|
|
|
static const uint8_t GREEN = 2;
|
|
|
|
|
static const uint8_t CYAN = 3;
|
|
|
|
|
static const uint8_t RED = 4;
|
|
|
|
|
static const uint8_t MAGENTA = 5;
|
|
|
|
|
static const uint8_t YELLOW = 6;
|
|
|
|
|
static const uint8_t WHITE = 7;
|
2015-02-08 00:22:32 +01:00
|
|
|
|
void off() {
|
|
|
|
|
this->timerOff();
|
|
|
|
|
stripered::make_low();
|
|
|
|
|
stripegreen::make_low();
|
|
|
|
|
stripeblue::make_low();
|
|
|
|
|
}
|
|
|
|
|
void on() {
|
|
|
|
|
this->timerOn();
|
|
|
|
|
}
|
|
|
|
|
void lower() {
|
|
|
|
|
if(OCR0B >= 5) {
|
|
|
|
|
OCR0B = OCR0B - 5;
|
|
|
|
|
} else {
|
|
|
|
|
OCR0B = 0;
|
|
|
|
|
}
|
|
|
|
|
if(OCR2B >= 5) {
|
|
|
|
|
OCR2B = OCR2B - 5;
|
|
|
|
|
} else {
|
|
|
|
|
OCR2B = 0;
|
|
|
|
|
}
|
|
|
|
|
if(OCR2A >= 5) {
|
|
|
|
|
OCR2A = OCR2A - 5;
|
|
|
|
|
} else {
|
|
|
|
|
OCR2A = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void higher() {
|
|
|
|
|
if(OCR0B <= 250) {
|
|
|
|
|
OCR0B = OCR0B + 5;
|
|
|
|
|
} else {
|
|
|
|
|
OCR0B = 0;
|
|
|
|
|
}
|
|
|
|
|
if(OCR2B <= 250) {
|
|
|
|
|
OCR2B = OCR2B + 5;
|
|
|
|
|
} else {
|
|
|
|
|
OCR2B = 0;
|
|
|
|
|
}
|
|
|
|
|
if(OCR2A <= 250) {
|
|
|
|
|
OCR2A = OCR2A + 5;
|
|
|
|
|
} else {
|
|
|
|
|
OCR2A = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-09 18:10:07 +01:00
|
|
|
|
void fadeto(uint8_t red, uint8_t green, uint8_t blue, uint8_t time) {
|
|
|
|
|
this->setfadecolor(0, red, green, blue, time);
|
2015-02-11 18:10:07 +01:00
|
|
|
|
this->fade(1);
|
|
|
|
|
}
|
|
|
|
|
void fade(uint8_t num) {
|
|
|
|
|
if(num <= max_map_size && num >= 0) {
|
|
|
|
|
this->fade_zylk = num;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
this->fade_zylk = 0;
|
|
|
|
|
}
|
|
|
|
|
this->_l = 0;
|
2015-02-09 18:10:07 +01:00
|
|
|
|
}
|
|
|
|
|
void setfadecolor(uint8_t index, uint8_t red, uint8_t green, uint8_t blue, uint8_t time) {
|
|
|
|
|
this->fademap[0][index] = red;
|
|
|
|
|
this->fademap[1][index] = green;
|
|
|
|
|
this->fademap[2][index] = blue;
|
|
|
|
|
this->fademap[3][index] = time;
|
|
|
|
|
}
|
2015-02-11 18:10:07 +01:00
|
|
|
|
uint8_t gp(uint8_t i) {
|
|
|
|
|
switch(i) {
|
|
|
|
|
case 0: return OCR0B; //ROT
|
|
|
|
|
case 1: return OCR2B; //GR<47>N
|
|
|
|
|
case 2: return OCR2A; //BLAU
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2015-02-09 18:10:07 +01:00
|
|
|
|
void interrupt() {
|
|
|
|
|
if(this->fade_zylk > 0) {
|
|
|
|
|
if(this->fadesteps[0] == 0 && this->fadesteps[1] == 0 && this->fadesteps[2] == 0) {
|
2015-02-11 18:10:07 +01:00
|
|
|
|
this->_l = (this->_l + 1 < this->fade_zylk) ? this->_l + 1 : 0;
|
2015-02-10 12:05:04 +01:00
|
|
|
|
for(uint8_t i = 0; i<3; i++) { //0=red,1=green,2=blue
|
2015-02-11 18:10:07 +01:00
|
|
|
|
this->fadesteps[i] = (this->fademap[i][this->_l]-this->gp(i))/this->fademap[3][this->_l];
|
2015-02-10 12:05:04 +01:00
|
|
|
|
this->virtualcolor[i] = this->gp(i);
|
|
|
|
|
}
|
2015-02-09 18:10:07 +01:00
|
|
|
|
} else {
|
2015-02-10 12:05:04 +01:00
|
|
|
|
for(uint8_t i = 0; i<3; i++) { //0=red,1=green,2=blue
|
2015-02-11 18:10:07 +01:00
|
|
|
|
if((this->fadesteps[i] > 0 && (this->virtualcolor[i] + this->fadesteps[i]) <= this->fademap[i][this->_l]) || (this->fadesteps[i] < 0 && (this->virtualcolor[i] + this->fadesteps[i]) >= this->fademap[i][this->_l])) {
|
2015-02-10 12:05:04 +01:00
|
|
|
|
this->virtualcolor[i] = this->virtualcolor[i] + this->fadesteps[i];
|
|
|
|
|
this->sp(i, this->virtualcolor[i]);
|
|
|
|
|
} else {
|
2015-02-11 18:10:07 +01:00
|
|
|
|
this->sp(i, this->fademap[i][this->_l]);
|
2015-02-10 12:05:04 +01:00
|
|
|
|
this->fadesteps[i] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-11 18:10:07 +01:00
|
|
|
|
}
|
2015-02-09 18:10:07 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-19 23:57:35 +01:00
|
|
|
|
private:
|
2015-02-11 18:10:07 +01:00
|
|
|
|
static const uint8_t max_map_size = 6;
|
2015-02-09 18:10:07 +01:00
|
|
|
|
uint8_t fade_zylk;
|
2015-02-11 18:10:07 +01:00
|
|
|
|
uint8_t fademap[4][max_map_size];
|
2015-02-09 18:10:07 +01:00
|
|
|
|
float fadesteps[3];
|
2015-02-10 12:05:04 +01:00
|
|
|
|
float virtualcolor[3];
|
2015-02-11 18:10:07 +01:00
|
|
|
|
uint8_t _l;
|
2014-11-19 23:57:35 +01:00
|
|
|
|
void init() {
|
|
|
|
|
stripered::make_output();
|
|
|
|
|
stripegreen::make_output();
|
|
|
|
|
stripeblue::make_output();
|
2015-02-09 18:10:07 +01:00
|
|
|
|
this->fade_zylk = 0;
|
2015-02-11 18:10:07 +01:00
|
|
|
|
this->_l = 0;
|
2015-02-08 00:22:32 +01:00
|
|
|
|
this->initTimer();
|
|
|
|
|
this->color(0, 0, 0);
|
|
|
|
|
}
|
2015-02-10 12:05:04 +01:00
|
|
|
|
void sp(uint8_t i, uint8_t c) {
|
|
|
|
|
switch(i) {
|
|
|
|
|
case 0: OCR0B = c; break; //ROT
|
|
|
|
|
case 1: OCR2B = c; break; //GR<47>N
|
|
|
|
|
case 2: OCR2A = c; break; //BLAU
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-08 00:22:32 +01:00
|
|
|
|
void timerOff() {
|
|
|
|
|
// OC0B output: Disconnected
|
|
|
|
|
// OC2A output: Disconnected
|
|
|
|
|
// OC2B output: Disconnected
|
|
|
|
|
TCCR0A &= ~(1<<COM0B1);
|
|
|
|
|
TCCR2A &= ~((1<<COM2A1) | (1<<COM2B1));
|
|
|
|
|
}
|
|
|
|
|
void timerOn() {
|
|
|
|
|
// OC0B output: Non-Inverted PWM
|
|
|
|
|
// OC2A output: Non-Inverted PWM
|
|
|
|
|
// OC2B output: Non-Inverted PWM
|
|
|
|
|
TCCR0A |= (1<<COM0B1);
|
|
|
|
|
TCCR2A |= (1<<COM2A1) | (1<<COM2B1);
|
2014-11-19 23:57:35 +01:00
|
|
|
|
}
|
|
|
|
|
void uninit();
|
|
|
|
|
void initTimer() {
|
|
|
|
|
// Timer/Counter 0 initialization
|
|
|
|
|
// Clock source: System Clock
|
|
|
|
|
// Clock value: 8000,000 kHz
|
|
|
|
|
// Mode: Phase correct PWM top=0xFF
|
|
|
|
|
// OC0A output: Disconnected
|
|
|
|
|
// Timer Period: 0,06375 ms
|
|
|
|
|
// Output Pulse(s):
|
|
|
|
|
// OC0B Period: 0,06375 ms Width: 0 us
|
2015-02-08 00:22:32 +01:00
|
|
|
|
TCCR0A=(0<<COM0A1) | (0<<COM0A0) | (0<<COM0B0) | (0<<WGM01) | (1<<WGM00);
|
|
|
|
|
TCCR0B=(0<<WGM02) | (1<<CS02) | (0<<CS01) | (0<<CS00);
|
2014-11-19 23:57:35 +01:00
|
|
|
|
TCNT0=0x00;
|
2015-02-09 18:10:07 +01:00
|
|
|
|
// Timer/Counter 1 initialization
|
|
|
|
|
// Clock source: System Clock
|
|
|
|
|
// Clock value: 20000,000 kHz
|
|
|
|
|
// Mode: Normal top=0xFFFF
|
|
|
|
|
// OC1A output: Disconnected
|
|
|
|
|
// OC1B output: Disconnected
|
|
|
|
|
// Noise Canceler: Off
|
|
|
|
|
// Input Capture on Falling Edge
|
|
|
|
|
// Timer Period: 3,2768 ms
|
|
|
|
|
// Timer1 Overflow Interrupt: On
|
|
|
|
|
// Input Capture Interrupt: Off
|
|
|
|
|
// Compare A Match Interrupt: Off
|
|
|
|
|
// Compare B Match Interrupt: Off
|
|
|
|
|
TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (0<<WGM10);
|
|
|
|
|
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (1<<CS11) | (0<<CS10);
|
|
|
|
|
TCNT1H=0x00;
|
|
|
|
|
TCNT1L=0x00;
|
2014-11-19 23:57:35 +01:00
|
|
|
|
// Timer/Counter 2 initialization
|
|
|
|
|
// Clock source: System Clock
|
|
|
|
|
// Clock value: 8000,000 kHz
|
|
|
|
|
// Mode: Phase correct PWM top=0xFF
|
|
|
|
|
// Timer Period: 0,06375 ms
|
|
|
|
|
// Output Pulse(s):
|
|
|
|
|
// OC2A Period: 0,06375 ms Width: 0 us
|
|
|
|
|
// OC2B Period: 0,06375 ms Width: 0 us
|
|
|
|
|
ASSR=(0<<EXCLK) | (0<<AS2);
|
2015-02-08 00:22:32 +01:00
|
|
|
|
TCCR2A=(0<<COM2A0) | (0<<COM2B0) | (0<<WGM21) | (1<<WGM20);
|
|
|
|
|
TCCR2B=(0<<WGM22) | (1<<CS22) | (0<<CS21) | (1<<CS20);
|
2014-11-19 23:57:35 +01:00
|
|
|
|
TCNT2=0x00;
|
|
|
|
|
// Timer/Counter 0 Interrupt(s) initialization
|
|
|
|
|
TIMSK0=(0<<OCIE0B) | (0<<OCIE0A) | (0<<TOIE0);
|
2015-02-09 18:10:07 +01:00
|
|
|
|
// Timer/Counter 1 Interrupt(s) initialization
|
|
|
|
|
TIMSK1=(0<<ICIE1) | (0<<OCIE1B) | (0<<OCIE1A) | (1<<TOIE1);
|
2014-11-19 23:57:35 +01:00
|
|
|
|
// Timer/Counter 2 Interrupt(s) initialization
|
|
|
|
|
TIMSK2=(0<<OCIE2B) | (0<<OCIE2A) | (0<<TOIE2);
|
2015-02-08 00:22:32 +01:00
|
|
|
|
this->timerOn();
|
2014-11-19 23:57:35 +01:00
|
|
|
|
}
|
|
|
|
|
const typedef avrlib::pin<PortR, pin_red> stripered;
|
|
|
|
|
const typedef avrlib::pin<PortG, pin_green> stripegreen;
|
|
|
|
|
const typedef avrlib::pin<PortB, pin_blue> stripeblue;
|
|
|
|
|
};
|
|
|
|
|
#endif /* STRIPE_H_ */
|