diff --git a/src/main.cpp b/src/main.cpp index 5e1cc7f..066d41a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -163,8 +163,114 @@ uint32_t t; #define SP_US(_str,_a) Serial.print(_str); Serial.print(" took: "); Serial.print(_a); Serial.println("µs") #define TIMEIF_US(_str,_f, _l) t=micros(); _f; t=micros()-t; if(t > _l) { SP_US(_str, t); } +uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval, uint8_t factor){ + static uint8_t lastled = 255; + static uint8_t current_pwm = 0; + + if(led != lastled){ + pwmController.setChannelPWM(led, startval); + lastled = led; + current_pwm = startval; + return 1; + } + + if(current_pwm == stopval){ + return 0; + } + + else if(startval > stopval){ + current_pwm -= 1; + } + else { + current_pwm += 1; + } + pwmController.setChannelPWM(led, current_pwm*factor); + return 1; +} + +#define LEDCOUNT 13 + +void ledsequence(uint8_t direction, uint8_t onoff, uint8_t factor){ + static int8_t led = 0; + static uint8_t brightness = 0; + static uint8_t lastbrightness = 0; + static uint8_t finish = 1; + static uint32_t status = 0; + uint32_t status_build = 0; + status_build |= direction << 16; + status_build |= onoff << 8; + status_build |= factor; + if(status_build != status){ // check if any parameter changed + finish = 0; // set state unfinished -> start action + if(direction) led = 0; // reset led counter depending of direction + else led = LEDCOUNT-1; + if(onoff) brightness = 127; // set brightness value depending of on/off + else brightness = 0; + status = status_build; // set parameter memory + Serial.println("----Status Changed!"); + } + if(!finish){ // finish == 0 -> action pending + if(!softstart_led(led,lastbrightness, brightness, factor)){ + Serial.print("one LED finished, new set led: "); + Serial.print(led); + Serial.print(" last: "); + Serial.print(lastbrightness); + Serial.print(" curr: "); + Serial.println(brightness); + if(direction){ + led++; + if(led >= LEDCOUNT) { + finish = 1; + lastbrightness = brightness; + } + } + else{ + led--; + if(led < 0){ + lastbrightness = brightness; + finish = 1; + } + } + } + } +} + + void loop() { - TIMEIF_US("OTA", ArduinoOTA.handle(), 20000); - + static uint32_t dimmtimer = 0; + /* static uint8_t led = 0; + static uint16_t brightness = 127; + static uint16_t lastbrightness = 0; + + if(millis() - dimmtimer > 2){ + if(softstart_led(led, lastbrightness, brightness)){ + // do nothing + } + else{ + if(led < LEDCOUNT-1) led++; + else { + led = 0; + uint16_t mem = lastbrightness; + lastbrightness = brightness; + brightness = mem; + } + softstart_led((led), lastbrightness, brightness); + } + if(led >= LEDCOUNT){ + //led = 0; + //uint16_t mem = lastbrightness; + //lastbrightness = brightness; + // brightness = mem; + } + dimmtimer = millis(); + }*/ + + static uint8_t direction = 1; + static uint8_t onoff = 1; + if(millis() - dimmtimer > 2){ + ledsequence(direction, onoff, 4); + } + if(millis() > 25000) onoff = 0; + TIMEIF_US("OTA", ArduinoOTA.handle(), 20000); TIMEIF_US("HTTP", server.handleClient(), 20000); } \ No newline at end of file