Doch nicht sooo großer Schmutz

This commit is contained in:
Max Braungardt 2018-03-28 18:49:24 +02:00
parent 468e113a30
commit a3eda7eb86

View File

@ -14,7 +14,7 @@
#define HUE_DIF (HUE_MAX - HUE_MIN) #define HUE_DIF (HUE_MAX - HUE_MIN)
#define HUE_SAT 100 // 0 = white, 100 = color #define HUE_SAT 100 // 0 = white, 100 = color
#define HUE_VAL 20 // 0 = black, 100 = color #define HUE_VAL 20 // 0 = black, 100 = color
#define HUE_STEP 8 // 8 #define HUE_STEP 8 // Bis 3 sollte gehen
const uint32_t colorSteps = HUE_DIF / HUE_STEP; const uint32_t colorSteps = HUE_DIF / HUE_STEP;
//~ const uint32_t colorSteps = 5; //~ const uint32_t colorSteps = 5;
@ -24,6 +24,7 @@ const uint32_t colorSteps = HUE_DIF / HUE_STEP;
void writeZero(void); void writeZero(void);
void writeOne(void); void writeOne(void);
void writeRGB(uint8_t red, uint8_t green, uint8_t blue); void writeRGB(uint8_t red, uint8_t green, uint8_t blue);
void nextColor(hsv_t* hsvList, rgb_t* rgbList, hsv_t start, hsv_t end);
// Interrupt variables: // Interrupt variables:
int8_t volatile status = 1; int8_t volatile status = 1;
@ -32,28 +33,36 @@ uint16_t volatile max = HUE_MID;
uint32_t volatile time = 0; uint32_t volatile time = 0;
uint32_t volatile T_half = 0; uint32_t volatile T_half = 0;
uint32_t volatile oldTime = 0; uint32_t volatile oldTime = 0;
uint32_t volatile cooldown = 14000000; uint32_t volatile cooldown = 3000000; // * 50 ns = 150 ms disabled interrupt
uint8_t volatile triggered = 0;
//~ uint32_t volatile delay = 0; //~ uint32_t volatile delay = 0;
// On External Interrupt: // On External Interrupt:
ISR(INT1_vect) { ISR(INT1_vect) {
EIMSK &= 0xfd; // Disable INT1 uint32_t now = time + TCNT1;
PORTC &= 0xef; // Turn LED off while INT1 disabled //~ triggered = (triggered + 1) % 4;
if(now > cooldown) {
triggered = true;
//~ EIMSK &= 0xfd; // Disable INT1
//~ EICRA &= 0xf3; //
PORTC &= 0xef; // Turn LED off while INT1 disabled (PORTC3 = PIN26)
PORTC ^= 0x02; // toggle LED on INT1 rising edge (PORTC1 = PIN24)
//~ uint64_t time2 = TCNT2; //~ uint64_t time2 = TCNT2;
//~ uint64_t time1 = TCNT1; //~ uint64_t time1 = TCNT1;
//~ uint64_t time0 = TCNT0; //~ uint64_t time0 = TCNT0;
//~ T_half = time2 | (time1 << 9) | (time0 << 26); //~ T_half = time2 | (time1 << 9) | (time0 << 26);
T_half = time + TCNT1; T_half = now;
//~ delay = T_half / 16; // Delay for the next possible external interrupt //~ delay = T_half / 16; // Delay for the next possible external interrupt
//~ cooldown = T_half / 2; cooldown = T_half / 2;
TCNT2 = 0; // Reset Timer/Counter2 //~ TCNT2 = 0; // Reset Timer/Counter2
TCNT1 = 0; // Reset Timer/Counter1 //~ TCNT1 = 0; // Reset Timer/Counter1
TCNT0 = 0; // Reset Timer/Counter0 TCNT0 = 0; // Reset Timer/Counter0
time = 0; time = 0;
oldTime = 0; oldTime = 0;
PORTC ^= 0x02; }
//~ min = HUE_MID; //~ triggered = 0;
//~ max = HUE_MID; min = HUE_MID;
max = HUE_MID;
//~ status = (status == 1 ? -1 : 1); //~ status = (status == 1 ? -1 : 1);
} }
@ -67,7 +76,7 @@ int main(void) {
EIMSK |= 0x02; // External Interrupt Mask Register EIMSK |= 0x02; // External Interrupt Mask Register
// Enable INT1 (PIN5) for interrupt // Enable INT1 (PIN5) for interrupt
EICRA |= 0x0c; // External Interrupt Control Register EICRA |= 0x0c; // External Interrupt Control Register
// INT1 on rising edge // INT1 on falling edge
TIMSK1 |= 0x01; // Timer/Counter1 Interrupt Mask Register TIMSK1 |= 0x01; // Timer/Counter1 Interrupt Mask Register
// Enable overflow interrupt // Enable overflow interrupt
@ -125,7 +134,7 @@ int main(void) {
} }
// Next color // Next color
int8_t sign = 1; int8_t sign = -1;
min += sign * HUE_STEP; min += sign * HUE_STEP;
max -= sign * HUE_STEP; max -= sign * HUE_STEP;
interpolate( interpolate(
@ -150,18 +159,19 @@ int main(void) {
//~ } //~ }
if(now > cooldown) { if(now > cooldown) {
EIMSK |= 0x02; // Enable INT1 //~ EIMSK |= 0x02; // Enable INT1
PORTC |= 0x10; // Turn on LED while INT1 enabled PORTC |= 0x10; // Turn on LED while INT1 enabled (PORTC3 = PIN26)
} }
uint32_t T_step = T_half / colorSteps; uint32_t T_step = T_half / colorSteps;
// Check if it is time for next color // Check if it is time for next color
if(now > oldTime + T_step) { if(now > oldTime) { // + T_step || (now < T_step && oldTime == 0)) {
oldTime += T_step; oldTime += T_step;
PORTC ^= 0x04; PORTC ^= 0x04; // toggle LED when writing new color (PORTC2 = PIN25)
// Assign color // Assign color
//~ EIMSK &= 0xfd; //~ cli(); //~ EIMSK &= 0xfd; //~ cli();
nextColor( colorsHSV, colorsRGB, init_hsv_t(min, HUE_SAT, HUE_VAL), init_hsv_t(max, HUE_SAT, HUE_VAL));
for(int i = 0; i < LEDS; i++) { for(int i = 0; i < LEDS; i++) {
writeRGB( colorsRGB[i].r, colorsRGB[i].g, colorsRGB[i].b); writeRGB( colorsRGB[i].r, colorsRGB[i].g, colorsRGB[i].b);
} }
@ -171,13 +181,14 @@ int main(void) {
// Next color // Next color
min += sign * HUE_STEP; min += sign * HUE_STEP;
max -= sign * HUE_STEP; max -= sign * HUE_STEP;
interpolate(
init_hsv_t(min, HUE_SAT, HUE_VAL), // from color //~ interpolate(
init_hsv_t(max, HUE_SAT, HUE_VAL), // to color //~ init_hsv_t(min, HUE_SAT, HUE_VAL), // from color
LEDS, //~ init_hsv_t(max, HUE_SAT, HUE_VAL), // to color
colorsHSV //~ LEDS,
); //~ colorsHSV
hsv2rgbList(colorsHSV, colorsRGB, LEDS); //~ );
//~ hsv2rgbList(colorsHSV, colorsRGB, LEDS);
// Check if reached amplitude // Check if reached amplitude
if(sign == 1 && (min >= HUE_MAX || max <= HUE_MIN)) { if(sign == 1 && (min >= HUE_MAX || max <= HUE_MIN)) {
sign = -1; sign = -1;
@ -197,6 +208,15 @@ int main(void) {
} }
} }
void inline nextColor(hsv_t* hsvList, rgb_t* rgbList, hsv_t start, hsv_t end) {
interpolate(start, // from color
end, // to color
LEDS,
hsvList
);
hsv2rgbList(hsvList, rgbList, LEDS);
}
/*********************************************************************** /***********************************************************************
* *
* TIMING * TIMING