Merge branch 'master' of https://git.efi.th-nuernberg.de/gitea/schmidtsi76327/ESP8266_Treppenlicht
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
#include "filesys.h"
|
||||
|
||||
FSInfo fsinfo;
|
||||
|
||||
bool mount_fs() {
|
||||
if(!LittleFS.begin()) {
|
||||
Serial.println("[ERROR] LittleFS.info(), reset ...");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!LittleFS.info(fsinfo)) {
|
||||
Serial.println("[ERROR] LittleFS.info(), reset ...");
|
||||
return false;
|
||||
}
|
||||
|
||||
printf("Filesystem opened:\n\r");
|
||||
printf("\ttotalBytes:\t%d\n\r", fsinfo.totalBytes);
|
||||
printf("\tusedBytes:\t%d\n\r", fsinfo.usedBytes);
|
||||
printf("\tblockSize:\t%d\n\r", fsinfo.blockSize);
|
||||
printf("\tpageSize:\t%d\n\r", fsinfo.pageSize);
|
||||
printf("\tmaxOpenFiles:\t%d\n\r", fsinfo.maxOpenFiles);
|
||||
printf("\tmaxPathLength:\t%d\n\r", fsinfo.maxPathLength);
|
||||
printf("\n\r");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool format_fs() {
|
||||
printf("Formatting FS ! \n\r");
|
||||
return LittleFS.format();
|
||||
}
|
||||
|
||||
void ls(const char * dirname) {
|
||||
Serial.printf("ls -l %s\n\r", dirname);
|
||||
Dir root = LittleFS.openDir(dirname);
|
||||
|
||||
while (root.next()) {
|
||||
File file = root.openFile("r");
|
||||
|
||||
time_t lw = file.getLastWrite();
|
||||
struct tm * tmstruct = localtime(&lw);
|
||||
Serial.printf("%8d %02d %02d %02d:%02d %s\n\r",
|
||||
file.size(),
|
||||
tmstruct->tm_mon + 1,
|
||||
tmstruct->tm_mday,
|
||||
tmstruct->tm_hour,
|
||||
tmstruct->tm_min,
|
||||
root.fileName().c_str());
|
||||
file.close();
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
File open(const char * path) {
|
||||
return LittleFS.open(path, "r");
|
||||
}
|
||||
|
||||
|
||||
void printFile(const char * path) {
|
||||
Serial.printf("Reading file: %s\n\r", path);
|
||||
|
||||
File file = LittleFS.open(path, "r");
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file for reading");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Read from file: ");
|
||||
while (file.available()) {
|
||||
Serial.write(file.read());
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
void writeFile(const char * path, const char * message) {
|
||||
Serial.printf("Writing file: %s\n\r", path);
|
||||
|
||||
File file = LittleFS.open(path, "w");
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file for writing");
|
||||
return;
|
||||
}
|
||||
if (file.print(message)) {
|
||||
Serial.println("File written");
|
||||
} else {
|
||||
Serial.println("Write failed");
|
||||
}
|
||||
delay(2000); // Make sure the CREATE and LASTWRITE times are different
|
||||
file.close();
|
||||
}
|
||||
|
||||
void appendFile(const char * path, const char * message) {
|
||||
Serial.printf("Appending to file: %s\n\r", path);
|
||||
|
||||
File file = LittleFS.open(path, "a");
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file for appending");
|
||||
return;
|
||||
}
|
||||
if(file.print(message)) {
|
||||
Serial.println("Message appended");
|
||||
} else {
|
||||
Serial.println("Append failed");
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
void renameFile(const char * path1, const char * path2) {
|
||||
Serial.printf("Renaming file %s to %s\n\r", path1, path2);
|
||||
if (LittleFS.rename(path1, path2)) {
|
||||
Serial.println("File renamed");
|
||||
} else {
|
||||
Serial.println("Rename failed");
|
||||
}
|
||||
}
|
||||
|
||||
void deleteFile(const char * path) {
|
||||
Serial.printf("Deleting file: %s\n\r", path);
|
||||
if (LittleFS.remove(path)) {
|
||||
Serial.println("File deleted");
|
||||
} else {
|
||||
Serial.println("Delete failed");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <LittleFS.h>
|
||||
|
||||
// some usefull wrappers for Filesystem
|
||||
|
||||
bool mount_fs();
|
||||
bool format_fs();
|
||||
|
||||
File open(const char * path);
|
||||
|
||||
void ls(const char * dirname);
|
||||
void printFile(const char * path);
|
||||
void writeFile(const char * path, const char * message);
|
||||
void appendFile(const char * path, const char * message);
|
||||
void renameFile(const char * path1, const char * path2);
|
||||
void deleteFile(const char * path);
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "httpserver.h"
|
||||
|
||||
bool HTTPServer::start() {
|
||||
if(!mount_fs()) {
|
||||
logf("cant mount filesystem, EXIT !\n\r");
|
||||
return false;
|
||||
}
|
||||
logf("[HTTPServer] LittleFS mounted !\n\r");
|
||||
logf("[HTTPServer] root:\n\r");
|
||||
this->listRoot();
|
||||
logf("\n\r");
|
||||
|
||||
// default handler
|
||||
this->onNotFound([this]() {
|
||||
String message = "File Not Found\n\n";
|
||||
message += "URI: ";
|
||||
message += uri();
|
||||
message += "\nMethod: ";
|
||||
message += (method() == HTTP_GET) ? "GET" : "POST";
|
||||
message += "\nArguments: ";
|
||||
message += args();
|
||||
message += "\n";
|
||||
|
||||
for (uint8_t i = 0; i < args(); i++) {
|
||||
message += " " + argName(i) + ": " + arg(i) + "\n";
|
||||
}
|
||||
send(404, "text/plain", message);
|
||||
});
|
||||
|
||||
// add static root file handler for http
|
||||
this->serveStatic("/", LittleFS, "/");
|
||||
|
||||
// application handler
|
||||
this->on("/app=terminal", HTTP_POST, [this]() {
|
||||
String log_msg = "terminal: millis: ";
|
||||
log_msg += millis();
|
||||
send(200, "text/plain", log_msg);
|
||||
});
|
||||
|
||||
this->begin();
|
||||
Serial.printf("[HTTPServer] Server active on Port 80 !\n\r");
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// Wrapper for ESP8266WebServer with Filesystem as HTTP source
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <stdarg.h>
|
||||
#include "filesys.h"
|
||||
|
||||
// debug log <ESP8266WebServer.h>
|
||||
// #define DEBUGV(f,...) do { Serial.printf(PSTR(f), ##__VA_ARGS__); } while (0)
|
||||
|
||||
#define LOG_STR "[HTTPServer]"
|
||||
|
||||
|
||||
class HTTPServer : public ESP8266WebServer {
|
||||
|
||||
private:
|
||||
const char* rootDir = "/";
|
||||
|
||||
void listRoot() {
|
||||
ls(rootDir);
|
||||
}
|
||||
void logf(const char *format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
Serial.print(LOG_STR);
|
||||
Serial.printf(format, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
public:
|
||||
HTTPServer(const int _port, const char* _rootDir) :
|
||||
ESP8266WebServer(_port), rootDir(_rootDir)
|
||||
{ }
|
||||
~HTTPServer()
|
||||
{
|
||||
Serial.printf("[HTTPServer] shut down ...\n\r");
|
||||
}
|
||||
|
||||
bool start();
|
||||
};
|
||||
+57
-51
@@ -30,7 +30,7 @@ uint8_t Treppe::softstart_led(uint8_t led, uint16_t startval, uint16_t stopval){
|
||||
else {
|
||||
current_pwm += stepsize;
|
||||
}
|
||||
Serial.println((uint16_t)current_pwm);
|
||||
// Serial.println((uint16_t)current_pwm);
|
||||
pwmController.setChannelPWM(led, (uint16_t)current_pwm);
|
||||
if(current_pwm > stopval - stepsize && current_pwm < stopval + stepsize){
|
||||
if(stopval == 0) pwmController.setChannelPWM(led, 0);
|
||||
@@ -60,46 +60,40 @@ void Treppe::ledsequence(){
|
||||
lastbrightness = active_brightness;
|
||||
}
|
||||
status = status_build; // set parameter memory
|
||||
Serial.print("----Status Changed! onoff: ");
|
||||
Serial.print(state);
|
||||
Serial.print(" dir: ");
|
||||
Serial.println(direction);
|
||||
|
||||
Serial.printf("----Status Changed! onoff: %d, dir: %d\n", state, direction);
|
||||
}
|
||||
if(!finish){ // finish == 0 -> action pending
|
||||
if(!softstart_led(led,lastbrightness, brightness)){
|
||||
Serial.print("one LED finished: ");
|
||||
Serial.print(led);
|
||||
Serial.print(" last: ");
|
||||
Serial.print(lastbrightness);
|
||||
Serial.print(" curr: ");
|
||||
Serial.println(brightness);
|
||||
if(direction){
|
||||
led++;
|
||||
if(led >= stairs) {
|
||||
if(!softstart_led(led,lastbrightness, brightness)){
|
||||
Serial.printf("one LED finished: led: %d, last: %d, curr %d\n",
|
||||
led, lastbrightness, brightness);
|
||||
|
||||
if(direction){
|
||||
led++;
|
||||
if(led >= stairs)
|
||||
finish = 1;
|
||||
}
|
||||
else{
|
||||
led--;
|
||||
if(led < 0)
|
||||
finish = 1;
|
||||
//lastbrightness = brightness;
|
||||
}
|
||||
}
|
||||
else{
|
||||
led--;
|
||||
if(led < 0){
|
||||
//lastbrightness = brightness;
|
||||
finish = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Treppe::task_2()
|
||||
void Treppe::rampe()
|
||||
{
|
||||
if(state) {
|
||||
finish = 0;
|
||||
state = 0; // set parameter memory
|
||||
}
|
||||
|
||||
if(!finish) {
|
||||
if(direction) { // aufwärts
|
||||
if(tick >= ticks_treppe-1) { // ziel erreicht
|
||||
Serial.println("[Treppe] oberster tick !");
|
||||
state = 0;
|
||||
finish = 1;
|
||||
return;
|
||||
}
|
||||
tick++; // eins hoch
|
||||
@@ -107,17 +101,25 @@ void Treppe::task_2()
|
||||
else { // abwärts
|
||||
if(tick <= 0) { // ziel erreicht
|
||||
Serial.println("[Treppe] unterster tick !");
|
||||
state = 0;
|
||||
finish = 1;
|
||||
return;
|
||||
}
|
||||
tick--; // eins runter
|
||||
}
|
||||
|
||||
stufe = tick / ticks_pro_stufe;
|
||||
float new_pwm = differenz_pwm_pro_tick * (tick - ticks_pro_stufe*stufe);
|
||||
if(direction)
|
||||
new_pwm += differenz_pwm_pro_tick;
|
||||
new_pwm += idle_brightness;
|
||||
|
||||
float new_pwm = 0.0;
|
||||
if(an_aus) {
|
||||
new_pwm = differenz_pwm_pro_tick * (tick - ticks_pro_stufe*stufe);
|
||||
new_pwm += idle_brightness;
|
||||
if(direction) new_pwm += differenz_pwm_pro_tick;
|
||||
}
|
||||
else {
|
||||
new_pwm = active_brightness - differenz_pwm_pro_tick * (tick - ticks_pro_stufe*stufe);
|
||||
new_pwm += idle_brightness;
|
||||
if(direction) new_pwm -= differenz_pwm_pro_tick;
|
||||
}
|
||||
|
||||
pwmController.setChannelPWM(stufe, (uint16_t) new_pwm);
|
||||
Serial.printf("tick %04u, led %02d:%02u, pwm %4.1f\n",
|
||||
@@ -129,12 +131,6 @@ void Treppe::task_2()
|
||||
}
|
||||
}
|
||||
|
||||
// if(stufe > stairs || stufe < 0 || tick < 0 || tick > ticks_treppe-1) {
|
||||
// Serial.println("[Treppe] ERROR, Something went wrong !");
|
||||
// state = 0;
|
||||
// return;
|
||||
// }
|
||||
|
||||
void Treppe::setup(){
|
||||
Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick);
|
||||
|
||||
@@ -168,29 +164,41 @@ void Treppe::task(){
|
||||
current_sensor_state[1] = digitalRead(SENSOR2);
|
||||
|
||||
if(current_sensor_state[0] && !last_sensor_state[0] && state == 0){
|
||||
setTick(0);
|
||||
setAnAus(1);
|
||||
setDirection(1);
|
||||
setState(1);
|
||||
}
|
||||
|
||||
if(current_sensor_state[1] && !last_sensor_state[1] && state == 0){
|
||||
setTick(0);
|
||||
setAnAus(0);
|
||||
setDirection(0);
|
||||
setState(1);
|
||||
}
|
||||
|
||||
// first switch - off approach, use timer later
|
||||
if(!current_sensor_state[0] && last_sensor_state[0] && state == 1){
|
||||
setTick(ticks_treppe);
|
||||
setAnAus(1);
|
||||
setDirection(1);
|
||||
setState(0);
|
||||
}
|
||||
|
||||
if(!current_sensor_state[1] && last_sensor_state[1] && state == 1){
|
||||
setTick(ticks_treppe);
|
||||
setAnAus(1);
|
||||
setDirection(0);
|
||||
setState(0);
|
||||
}
|
||||
|
||||
last_sensor_state[0] = current_sensor_state[0];
|
||||
last_sensor_state[1] = current_sensor_state[1];
|
||||
<<<<<<< HEAD
|
||||
|
||||
=======
|
||||
|
||||
>>>>>>> 3d13e761f497a0408c134059ff0dfadaa93ebbf4
|
||||
ledsequence();
|
||||
}
|
||||
|
||||
@@ -211,22 +219,20 @@ uint16_t Treppe::setTime(uint16_t _time_per_stair){
|
||||
return time_per_stair;
|
||||
}
|
||||
|
||||
uint8_t Treppe::setDirection(uint8_t _direction){
|
||||
void Treppe::setDirection(uint8_t _direction){
|
||||
switch_direction = _direction;
|
||||
Serial.println("Treppe: Direction changed!");
|
||||
Serial.printf("Treppe: switch_direction=%d!\n", switch_direction);
|
||||
if(finish) Serial.println("apply direction request immediately");
|
||||
else Serial.println("currently active, dir change afterwards");
|
||||
// to do: implement state command variable to determine dimm-state
|
||||
return switch_direction;
|
||||
}
|
||||
|
||||
uint8_t Treppe::setState(uint8_t _state){
|
||||
if(state == _state) return 1;
|
||||
else{
|
||||
switch_state = _state;
|
||||
Serial.println("Treppe: State Request changed!");
|
||||
if(finish) Serial.println("apply state request immediately");
|
||||
else Serial.println("currently active, state changes after activity");
|
||||
}
|
||||
return 0;
|
||||
void Treppe::setState(uint8_t _state){
|
||||
if(state == _state) return;
|
||||
else {
|
||||
switch_state = _state;
|
||||
Serial.printf("Treppe: switch_state=%d!\n", switch_state);
|
||||
if(finish) Serial.println("apply state request immediately");
|
||||
else Serial.println("currently active, state changes after activity");
|
||||
}
|
||||
}
|
||||
+12
-5
@@ -21,17 +21,22 @@ private:
|
||||
uint8_t switch_state = 0;
|
||||
uint8_t finish = 1;
|
||||
|
||||
// alternative
|
||||
uint32_t tick = 0;
|
||||
uint32_t stufe = 0;
|
||||
uint8_t an_aus = 0;
|
||||
|
||||
uint32_t ticks_treppe = 0;
|
||||
uint32_t ticks_pro_stufe = 0;
|
||||
float differenz_pwm_pro_tick = 0.0;
|
||||
// alternative
|
||||
|
||||
|
||||
// initialize with i2c-Address 0, use Wire Library
|
||||
PCA9685 pwmController;
|
||||
uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval);
|
||||
void ledsequence();
|
||||
void rampe();
|
||||
|
||||
public:
|
||||
Treppe(uint8_t _stairs) : stairs(_stairs){
|
||||
@@ -42,10 +47,8 @@ public:
|
||||
/ (float) ticks_pro_stufe;
|
||||
}
|
||||
|
||||
void task(); // call periodically
|
||||
|
||||
void setup();
|
||||
void task_2();
|
||||
void task(); // call periodically
|
||||
|
||||
// Parameter section
|
||||
uint16_t setIdle(uint16_t _idle_brightness);
|
||||
@@ -61,9 +64,13 @@ public:
|
||||
}
|
||||
|
||||
// Runtime Parameter section
|
||||
uint8_t setDirection(uint8_t _direction);
|
||||
uint8_t setState(uint8_t _state);
|
||||
void setDirection(uint8_t _direction);
|
||||
void setState(uint8_t _state);
|
||||
void setAnAus(uint8_t _an_aus) {
|
||||
an_aus = _an_aus;
|
||||
}
|
||||
|
||||
uint8_t getState() { return state;};
|
||||
uint8_t getFinished() { return finish;};
|
||||
uint8_t getDirection() {return direction;};
|
||||
};
|
||||
Reference in New Issue
Block a user