Tuer-Schild/Door/io/Stripe.hpp

139 lines
3.5 KiB
C++

/*
* Led.h
*
* Created: 03.11.2013 17:11:58
* Author: BlubbFish
*/
#ifndef STRIPE_H_
#define STRIPE_H_
#include "hardware/pin.hpp"
#include <util/delay.h>
template <typename PortR, int pin_red, typename PortG, int pin_green, typename PortB, int pin_blue>
class Stripe {
public:
Stripe() {
this->init();
}
void color(uint8_t r, uint8_t g, uint8_t b) {
OCR0B=r; //ROT
OCR2B=g; //GRÜN
OCR2A=b; //BLAU
}
void setcolor(uint8_t mask) {
(mask & (1<<0)) ? OCR2A=0xFF : OCR2A=0x00;
(mask & (1<<1)) ? OCR2B=0xFF : OCR2B=0x00;
(mask & (1<<2)) ? OCR0B=0xFF : OCR0B=0x00;
}
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;
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;
}
}
private:
void init() {
stripered::make_output();
stripegreen::make_output();
stripeblue::make_output();
this->initTimer();
this->color(0, 0, 0);
}
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);
}
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
TCCR0A=(0<<COM0A1) | (0<<COM0A0) | (0<<COM0B0) | (0<<WGM01) | (1<<WGM00);
TCCR0B=(0<<WGM02) | (1<<CS02) | (0<<CS01) | (0<<CS00);
TCNT0=0x00;
// 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);
TCCR2A=(0<<COM2A0) | (0<<COM2B0) | (0<<WGM21) | (1<<WGM20);
TCCR2B=(0<<WGM22) | (1<<CS22) | (0<<CS21) | (1<<CS20);
TCNT2=0x00;
// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=(0<<OCIE0B) | (0<<OCIE0A) | (0<<TOIE0);
// Timer/Counter 2 Interrupt(s) initialization
TIMSK2=(0<<OCIE2B) | (0<<OCIE2A) | (0<<TOIE2);
this->timerOn();
}
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_ */