diff --git a/src/main.cpp b/src/main.cpp index 5dd7ac2..b15c2ba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,18 +21,19 @@ bool toggle = false; uint32_t m=0; uint32_t dimmtimer = 0; +uint16_t time_per_stair = 500; // global parameter: dimmtime per stair [ms] uint8_t direction = 1; uint8_t onoff = 1; -void ledsequence(uint8_t direction, uint8_t onoff, uint8_t factor); -uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval, uint8_t factor); +void ledsequence(uint8_t direction, uint8_t onoff); +uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval); void timerCallback(void *pArg) { *((int *) pArg) += 1; - ledsequence(direction, onoff, 4); + ledsequence(direction, onoff); // Serial.print("["); // Serial.print(micros()-m); // Serial.print("] timerCallback\n"); @@ -196,7 +197,7 @@ void setup() { pinMode(ESP12_LED, OUTPUT); Wire.begin(); // Wire must be started first - Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz + Wire.setClock(1000000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); @@ -208,7 +209,7 @@ void setup() { } os_timer_setfn(&Timer1, timerCallback, &c); - os_timer_arm(&Timer1, 1, true); + os_timer_arm(&Timer1, 20, true); Serial.println("Ready"); @@ -224,54 +225,66 @@ 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){ +uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval){ + + /* + softstart task + + - get's called at regular intervals (1ms at the moment) + - dimms single led (0 - 15, PCA9685 outputs) with linear intervals vom startval to stopval + - calculates pwm steps depending on startval, stopval and timeinterval + - -> results in constanst speed + - returns 1 if led dimming is running + - returns 0 if led dimming is finished + + */ + static uint8_t lastled = 255; - static uint8_t current_pwm = 0; - + static float current_pwm = 0; + static float stepsize = 1.0; if(led != lastled){ - pwmController.setChannelPWM(led, startval); + pwmController.setChannelPWM(led, (uint16_t)startval); lastled = led; current_pwm = startval; + stepsize = 20*abs(stopval - startval)/(float)time_per_stair; // only valid at 1ms function call interval return 1; } - if(current_pwm == stopval){ - return 0; - } - - else if(startval > stopval){ - current_pwm -= 1; + if(current_pwm > stopval - stepsize && current_pwm < stopval + stepsize) return 0; + // todo: duty cycle zero! + if(startval > stopval){ + current_pwm -= stepsize; } else { - current_pwm += 1; + current_pwm += stepsize; } - pwmController.setChannelPWM(led, current_pwm*factor); + Serial.println((uint16_t)current_pwm); + pwmController.setChannelPWM(led, (uint16_t)current_pwm); return 1; } #define LEDCOUNT 16 -void ledsequence(uint8_t direction, uint8_t onoff, uint8_t factor){ +void ledsequence(uint8_t direction, uint8_t onoff){ static int8_t led = 0; - static uint8_t brightness = 0; - static uint8_t lastbrightness = 0; + static uint16_t brightness = 0; + static uint16_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; + static uint16_t status = 0; + uint16_t status_build = 0; + status_build |= direction << 8; + status_build |= onoff; 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 - lastbrightness = 0; + brightness = 2048; // set brightness value depending of on/off + lastbrightness = 200; } else{ - brightness = 0; - lastbrightness = 127; + brightness = 200; + lastbrightness = 2048; } status = status_build; // set parameter memory Serial.print("----Status Changed! onoff: "); @@ -281,8 +294,8 @@ void ledsequence(uint8_t direction, uint8_t onoff, uint8_t factor){ } if(!finish){ // finish == 0 -> action pending - if(!softstart_led(led,lastbrightness, brightness, factor)){ - Serial.print("one LED finished, new set led: "); + if(!softstart_led(led,lastbrightness, brightness)){ + Serial.print("one LED finished: "); Serial.print(led); Serial.print(" last: "); Serial.print(lastbrightness); @@ -310,16 +323,13 @@ void ledsequence(uint8_t direction, uint8_t onoff, uint8_t factor){ void loop() { - - if(millis() - dimmtimer > 2){ - //ledsequence(direction, onoff, 4); - dimmtimer = millis(); - } if(millis() > 25000 && onoff == 1 && direction == 1) onoff = 0; - if(millis() > 35000 && direction == 1){ + if(millis() > 45000 && direction == 1){ onoff = 1; direction = 0; } + + TIMEIF_US("OTA", ArduinoOTA.handle(), 1000); TIMEIF_US("HTTP", server.handleClient(), 1000); } \ No newline at end of file