Browse Source

variable PWM step calculation

tags/LEDFlickr
Dominik Bartsch 3 years ago
parent
commit
9d30423ca3
1 changed files with 47 additions and 37 deletions
  1. 47
    37
      src/main.cpp

+ 47
- 37
src/main.cpp View File

uint32_t m=0; uint32_t m=0;


uint32_t dimmtimer = 0; uint32_t dimmtimer = 0;
uint16_t time_per_stair = 500; // global parameter: dimmtime per stair [ms]
uint8_t direction = 1; uint8_t direction = 1;
uint8_t onoff = 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) void timerCallback(void *pArg)
{ {
*((int *) pArg) += 1; *((int *) pArg) += 1;
ledsequence(direction, onoff, 4);
ledsequence(direction, onoff);
// Serial.print("["); // Serial.print("[");
// Serial.print(micros()-m); // Serial.print(micros()-m);
// Serial.print("] timerCallback\n"); // Serial.print("] timerCallback\n");
pinMode(ESP12_LED, OUTPUT); pinMode(ESP12_LED, OUTPUT);


Wire.begin(); // Wire must be started first 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.mode(WIFI_STA);
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
} }


os_timer_setfn(&Timer1, timerCallback, &c); os_timer_setfn(&Timer1, timerCallback, &c);
os_timer_arm(&Timer1, 1, true);
os_timer_arm(&Timer1, 20, true);




Serial.println("Ready"); Serial.println("Ready");
#define SP_US(_str,_a) Serial.print(_str); Serial.print(" took: "); Serial.print(_a); Serial.println("µs") #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); } #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 lastled = 255;
static uint8_t current_pwm = 0;
static float current_pwm = 0;
static float stepsize = 1.0;
if(led != lastled){ if(led != lastled){
pwmController.setChannelPWM(led, startval);
pwmController.setChannelPWM(led, (uint16_t)startval);
lastled = led; lastled = led;
current_pwm = startval; current_pwm = startval;
stepsize = 20*abs(stopval - startval)/(float)time_per_stair; // only valid at 1ms function call interval
return 1; 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 { 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; return 1;
} }


#define LEDCOUNT 16 #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 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 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 if(status_build != status){ // check if any parameter changed
finish = 0; // set state unfinished -> start action finish = 0; // set state unfinished -> start action
if(direction) led = 0; // reset led counter depending of direction if(direction) led = 0; // reset led counter depending of direction
else led = LEDCOUNT-1; else led = LEDCOUNT-1;
if(onoff){ 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{ else{
brightness = 0;
lastbrightness = 127;
brightness = 200;
lastbrightness = 2048;
} }
status = status_build; // set parameter memory status = status_build; // set parameter memory
Serial.print("----Status Changed! onoff: "); Serial.print("----Status Changed! onoff: ");


} }
if(!finish){ // finish == 0 -> action pending 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(led);
Serial.print(" last: "); Serial.print(" last: ");
Serial.print(lastbrightness); Serial.print(lastbrightness);
void loop() { void loop() {



if(millis() - dimmtimer > 2){
//ledsequence(direction, onoff, 4);
dimmtimer = millis();
}
if(millis() > 25000 && onoff == 1 && direction == 1) onoff = 0; if(millis() > 25000 && onoff == 1 && direction == 1) onoff = 0;
if(millis() > 35000 && direction == 1){
if(millis() > 45000 && direction == 1){
onoff = 1; onoff = 1;
direction = 0; direction = 0;
} }

TIMEIF_US("OTA", ArduinoOTA.handle(), 1000); TIMEIF_US("OTA", ArduinoOTA.handle(), 1000);
TIMEIF_US("HTTP", server.handleClient(), 1000); TIMEIF_US("HTTP", server.handleClient(), 1000);
} }

Loading…
Cancel
Save