251 lines
6.6 KiB
C++
Raw Normal View History

2021-06-10 18:36:16 +02:00
#include <Arduino.h>
2021-06-22 20:25:27 +02:00
#ifdef WITH_DEBUGGING_ON
2021-06-23 16:49:53 +02:00
#include <GDBStub.h> // debugging support via GDBStub over UART
#endif
2021-06-10 20:58:48 +02:00
#include "PCA9685.h"
2021-06-23 16:49:53 +02:00
extern "C" {
#include "user_interface.h"
2021-06-22 20:25:27 +02:00
}
// OTA & WEB
2021-06-10 18:36:16 +02:00
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
2021-06-23 16:49:53 +02:00
#include "ota.h"
2021-06-10 18:36:16 +02:00
2021-06-23 16:49:53 +02:00
#include "wifi_credentials.h"
2021-06-23 15:22:38 +02:00
#include "httpserver.h"
2021-06-10 18:36:16 +02:00
2021-06-23 16:49:53 +02:00
// BOARD
2021-06-21 15:32:38 +02:00
#define ESP12_LED 2
#define NODEMCU_LED 16
2021-06-23 16:49:53 +02:00
// PWM
2021-06-22 20:36:53 +02:00
os_timer_t timer1;
2021-06-23 16:49:53 +02:00
uint8_t timer_flag = 0;
PCA9685 pwmController;
2021-06-22 20:25:27 +02:00
2021-06-21 09:43:06 +02:00
void setup_pwm_pca9685();
2021-06-21 12:26:52 +02:00
2021-06-23 16:49:53 +02:00
// WIFI
const char* ssid = STASSID;
const char* password = STAPSK;
2021-06-21 13:11:18 +02:00
2021-06-23 16:49:53 +02:00
// PWM
2021-06-21 13:11:18 +02:00
uint32_t dimmtimer = 0;
2021-06-23 16:51:15 +02:00
uint16_t time_per_stair = 500; // global parameter: dimmtime per stair [ms]
2021-06-21 13:11:18 +02:00
uint8_t direction = 1;
uint8_t onoff = 1;
2021-06-23 16:51:15 +02:00
void ledsequence(uint8_t direction, uint8_t onoff);
uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval);
2021-06-21 13:11:18 +02:00
2021-06-21 12:26:52 +02:00
void timerCallback(void *pArg)
2021-06-21 13:11:24 +02:00
{
2021-06-21 12:26:52 +02:00
*((int *) pArg) += 1;
2021-06-23 16:51:15 +02:00
ledsequence(direction, onoff);
// Serial.print("[");
// Serial.print(micros()-m);
// Serial.print("] timerCallback\n");
// m = micros();
2021-06-21 15:32:38 +02:00
}
2021-06-21 12:26:52 +02:00
2021-06-23 16:49:53 +02:00
// HTTP
2021-06-21 09:43:06 +02:00
void handleNotFound();
2021-06-23 16:12:19 +02:00
HTTPServer httpServer(80, "/");
2021-06-21 09:43:06 +02:00
2021-06-23 16:49:53 +02:00
// ===============================================
2021-06-10 18:36:16 +02:00
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
2021-06-23 16:12:19 +02:00
message += httpServer.uri();
2021-06-10 18:36:16 +02:00
message += "\nMethod: ";
2021-06-23 16:12:19 +02:00
message += (httpServer.method() == HTTP_GET) ? "GET" : "POST";
2021-06-10 18:36:16 +02:00
message += "\nArguments: ";
2021-06-23 16:12:19 +02:00
message += httpServer.args();
2021-06-10 18:36:16 +02:00
message += "\n";
2021-06-23 16:12:19 +02:00
for (uint8_t i = 0; i < httpServer.args(); i++) {
message += " " + httpServer.argName(i) + ": " + httpServer.arg(i) + "\n";
2021-06-10 18:36:16 +02:00
}
2021-06-23 16:12:19 +02:00
httpServer.send(404, "text/plain", message);
2021-06-21 09:43:06 +02:00
}
2021-06-10 20:19:25 +02:00
2021-06-21 09:43:06 +02:00
void setup_pwm_pca9685() {
2021-06-10 20:19:25 +02:00
pwmController.resetDevices(); // Software resets all PCA9685 devices on Wire line
2021-06-10 20:21:57 +02:00
pwmController.init(B000000); // Address pins A5-A0 set to B000000
pwmController.setPWMFrequency(200); // Default is 200Hz, supports 24Hz to 1526Hz
2021-06-10 20:19:25 +02:00
2021-06-21 09:43:06 +02:00
Serial.println(pwmController.getChannelPWM(0)); // Should output 2048, which is 128 << 4
}
2021-06-22 20:36:53 +02:00
2021-06-21 09:43:06 +02:00
void setup() {
2021-06-22 20:25:27 +02:00
#ifdef WITH_DEBUGGING_ON
2021-06-22 15:33:48 +02:00
Serial.begin(460800);
gdbstub_init();
#else
Serial.begin(115200);
#endif
2021-06-22 15:33:48 +02:00
2021-06-21 09:43:06 +02:00
Serial.println(F("Booting ...."));
2021-06-21 15:32:38 +02:00
pinMode(NODEMCU_LED, OUTPUT);
pinMode(ESP12_LED, OUTPUT);
2021-06-21 09:43:06 +02:00
Wire.begin(); // Wire must be started first
2021-06-23 16:51:15 +02:00
Wire.setClock(1000000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
2021-06-21 09:43:06 +02:00
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
2021-06-22 20:36:53 +02:00
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
2021-06-21 12:26:52 +02:00
os_timer_setfn(&timer1, timerCallback, &timer_flag);
os_timer_arm(&timer1, 20, true);
2021-06-21 12:26:52 +02:00
2021-06-21 09:43:06 +02:00
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
2021-06-23 16:49:53 +02:00
ota_setup();
2021-06-23 01:30:28 +02:00
2021-06-23 15:22:38 +02:00
httpServer.start();
2021-06-23 16:12:19 +02:00
httpServer.onNotFound(handleNotFound);
2021-06-23 13:37:13 +02:00
Serial.println("HTTP server started !");
2021-06-21 09:43:06 +02:00
setup_pwm_pca9685();
2021-06-23 13:37:13 +02:00
Serial.println("PCA9685 connected !");
2021-06-10 18:36:16 +02:00
}
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); }
2021-06-10 18:36:16 +02:00
2021-06-23 16:51:15 +02:00
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
*/
2021-06-21 10:48:27 +02:00
static uint8_t lastled = 255;
2021-06-23 16:51:15 +02:00
static float current_pwm = 0;
static float stepsize = 1.0;
2021-06-21 10:48:27 +02:00
if(led != lastled){
2021-06-23 16:51:15 +02:00
pwmController.setChannelPWM(led, (uint16_t)startval);
2021-06-21 10:48:27 +02:00
lastled = led;
current_pwm = startval;
2021-06-23 16:51:15 +02:00
stepsize = 20*abs(stopval - startval)/(float)time_per_stair; // only valid at 1ms function call interval
2021-06-21 10:48:27 +02:00
return 1;
}
2021-06-23 16:51:15 +02:00
if(current_pwm > stopval - stepsize && current_pwm < stopval + stepsize) return 0;
// todo: duty cycle zero!
if(startval > stopval){
current_pwm -= stepsize;
2021-06-21 10:48:27 +02:00
}
else {
2021-06-23 16:51:15 +02:00
current_pwm += stepsize;
2021-06-21 10:48:27 +02:00
}
2021-06-23 16:51:15 +02:00
Serial.println((uint16_t)current_pwm);
pwmController.setChannelPWM(led, (uint16_t)current_pwm);
2021-06-21 10:48:27 +02:00
return 1;
}
2021-06-21 15:32:38 +02:00
#define LEDCOUNT 16
2021-06-21 10:48:27 +02:00
2021-06-23 16:51:15 +02:00
void ledsequence(uint8_t direction, uint8_t onoff){
2021-06-21 10:48:27 +02:00
static int8_t led = 0;
2021-06-23 16:51:15 +02:00
static uint16_t brightness = 0;
static uint16_t lastbrightness = 0;
2021-06-21 10:48:27 +02:00
static uint8_t finish = 1;
2021-06-23 16:51:15 +02:00
static uint16_t status = 0;
uint16_t status_build = 0;
status_build |= direction << 8;
status_build |= onoff;
2021-06-21 10:48:27 +02:00
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;
2021-06-21 11:10:01 +02:00
if(onoff){
2021-06-23 16:51:15 +02:00
brightness = 2048; // set brightness value depending of on/off
lastbrightness = 200;
2021-06-21 11:10:01 +02:00
}
else{
2021-06-23 16:51:15 +02:00
brightness = 200;
lastbrightness = 2048;
2021-06-21 11:10:01 +02:00
}
2021-06-21 10:48:27 +02:00
status = status_build; // set parameter memory
2021-06-21 11:10:01 +02:00
Serial.print("----Status Changed! onoff: ");
Serial.print(onoff);
Serial.print(" dir: ");
Serial.println(direction);
2021-06-21 10:48:27 +02:00
}
if(!finish){ // finish == 0 -> action pending
2021-06-23 16:51:15 +02:00
if(!softstart_led(led,lastbrightness, brightness)){
Serial.print("one LED finished: ");
2021-06-21 10:48:27 +02:00
Serial.print(led);
Serial.print(" last: ");
Serial.print(lastbrightness);
Serial.print(" curr: ");
Serial.println(brightness);
if(direction){
led++;
if(led >= LEDCOUNT) {
finish = 1;
2021-06-21 11:10:01 +02:00
//lastbrightness = brightness;
2021-06-21 10:48:27 +02:00
}
}
else{
led--;
if(led < 0){
2021-06-21 11:10:01 +02:00
//lastbrightness = brightness;
2021-06-21 10:48:27 +02:00
finish = 1;
}
}
}
}
}
2021-06-23 01:30:28 +02:00
uint32_t _t=0;
2021-06-22 23:38:39 +02:00
#define SP_US(_str,_a) Serial.print(_str); Serial.print(" took: "); Serial.print(_a); Serial.println("us")
2021-06-23 01:30:28 +02:00
#define TIMEIF_US(_f, _l, _str) _t=micros(); _f; _t=micros()-_t; if(_t > _l) { SP_US(_str, _t); }
2021-06-21 10:48:27 +02:00
2021-06-10 18:36:16 +02:00
void loop() {
2021-06-21 10:48:27 +02:00
2021-06-21 13:11:18 +02:00
2021-06-21 11:10:01 +02:00
if(millis() > 25000 && onoff == 1 && direction == 1) onoff = 0;
2021-06-23 16:51:15 +02:00
if(millis() > 45000 && direction == 1){
2021-06-21 11:10:01 +02:00
onoff = 1;
2021-06-23 01:30:28 +02:00
direction = 0;
2021-06-21 10:48:27 +02:00
}
2021-06-23 01:30:28 +02:00
TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA");
2021-06-23 15:22:38 +02:00
TIMEIF_US(httpServer.handleClient(), 1000, "HTTP");
2021-06-23 16:51:15 +02:00
2021-06-10 18:36:16 +02:00
}