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

@@ -34,12 +34,24 @@ const char* password = STAPSK;

// PWM
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 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
void handleNotFound();
@@ -47,10 +59,6 @@ HTTPServer httpServer(80, "/");

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

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

void handleNotFound() {
String message = "File Not Found\n\n";
@@ -93,7 +101,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);
@@ -110,8 +118,8 @@ void setup() {
Serial.print("IP address: ");
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.print("IP address: ");
@@ -127,54 +135,70 @@ void setup() {
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 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: ");
@@ -184,8 +208,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);
@@ -214,21 +238,14 @@ uint32_t _t=0;
#define TIMEIF_US(_f, _l, _str) _t=micros(); _f; _t=micros()-_t; if(_t > _l) { SP_US(_str, _t); }

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