Simon Schmidt 3 years ago
parent
commit
908c4298be
1 changed files with 63 additions and 46 deletions
  1. 63
    46
      src/main.cpp

+ 63
- 46
src/main.cpp View File



// PWM // PWM
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 setup_pwm_pca9685();

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);
// Serial.print("[");
// Serial.print(micros()-m);
// Serial.print("] timerCallback\n");
// m = micros();
}


// HTTP // HTTP
void handleNotFound(); void handleNotFound();


// =============================================== // ===============================================


void timer_callback(void *pArg)
{
*((uint8_t *) pArg) = 1;
}


void handleNotFound() { void handleNotFound() {
String message = "File Not Found\n\n"; String message = "File Not Found\n\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);
Serial.print("IP address: "); Serial.print("IP address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());


os_timer_setfn(&timer1, timer_callback, &timer_flag);
os_timer_arm(&timer1, 1, true);
os_timer_setfn(&timer1, timerCallback, &timer_flag);
os_timer_arm(&timer1, 20, true);


Serial.println("Ready"); Serial.println("Ready");
Serial.print("IP address: "); Serial.print("IP address: ");
Serial.println("PCA9685 connected !"); Serial.println("PCA9685 connected !");
} }


uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval, uint8_t factor){
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){

/*
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);
#define TIMEIF_US(_f, _l, _str) _t=micros(); _f; _t=micros()-_t; if(_t > _l) { SP_US(_str, _t); } #define TIMEIF_US(_f, _l, _str) _t=micros(); _f; _t=micros()-_t; if(_t > _l) { SP_US(_str, _t); }


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(ArduinoOTA.handle(), 1000, "OTA"); TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA");
TIMEIF_US(httpServer.handleClient(), 1000, "HTTP"); TIMEIF_US(httpServer.handleClient(), 1000, "HTTP");


if(timer_flag) {
timer_flag = 0;
ledsequence(direction, onoff, 4);
//Serial.printf("[%lu] interrupt\n\r", millis());
}
} }

Loading…
Cancel
Save