Compare commits
18
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4e6445fea8 | ||
|
|
10b57ca516 | ||
|
|
e8329f2587 | ||
|
|
014aa028d7 | ||
|
|
b47f778fc1 | ||
|
|
c7eaffda00 | ||
|
|
69e1e869a5 | ||
|
|
1a284080e8 | ||
|
|
3490e7ba44 | ||
|
|
d059c3d72d | ||
|
|
33499beb32 | ||
|
|
38c81ae182 | ||
|
|
a8ceb258b8 | ||
|
|
7e5a6fac9f | ||
|
|
b7ba521c59 | ||
|
|
55ebd21975 | ||
|
|
2b2a135bd5 | ||
|
|
a400b0464d |
No files matched your search
@@ -1,6 +1,7 @@
|
||||
.pio
|
||||
.vscode
|
||||
compress
|
||||
data_gz
|
||||
platformio.ini
|
||||
include/wifi_credentials.h
|
||||
include/*.gz.h
|
||||
Vendored
+2
-1
@@ -48,5 +48,6 @@
|
||||
"streambuf": "cpp",
|
||||
"cinttypes": "cpp",
|
||||
"typeinfo": "cpp"
|
||||
}
|
||||
},
|
||||
"python.pythonPath": "C:\\Users\\simon\\AppData\\Local\\Programs\\Python\\Python37\\python.exe"
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import os, gzip
|
||||
# https://www.mischianti.org/2020/10/26/web-server-with-esp8266-and-esp32-byte-array-gzipped-pages-and-spiffs-2/
|
||||
|
||||
def convert_to_gzip(src, out, f):
|
||||
input_file = f'{src}{f}'
|
||||
output_file = f'{out}{f}.gz.h'
|
||||
output_charp = f'{f.replace(".", "_")}_gz'
|
||||
|
||||
top = ''
|
||||
with open(input_file, 'rb') as f_in:
|
||||
gz = gzip.compress(f_in.read())
|
||||
gzlen = len(gz)
|
||||
|
||||
top += f'// filename: {f}.gz.h\n'
|
||||
top += f'#define {output_charp}_len {gzlen}\n'
|
||||
top += f'const char {output_charp}[] = '
|
||||
top += '{'
|
||||
|
||||
with open(output_file, 'wb') as f_out:
|
||||
for i, b in enumerate(gz):
|
||||
if not i%10:
|
||||
top += '\n '
|
||||
top += f'0x{b:02X}, '
|
||||
top = top[:-2]
|
||||
top += '};'
|
||||
f_out.write(top.encode(encoding='utf-8'))
|
||||
|
||||
|
||||
src='data/'
|
||||
out='compress/'
|
||||
|
||||
if not 'compress' in os.listdir():
|
||||
os.mkdir('compress')
|
||||
|
||||
for f in os.listdir(src):
|
||||
convert_to_gzip(src, out, f)
|
||||
+9
-32
@@ -1,38 +1,15 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import os, gzip
|
||||
# https://www.mischianti.org/2020/10/26/web-server-with-esp8266-and-esp32-byte-array-gzipped-pages-and-spiffs-2/
|
||||
# https://stackoverflow.com/questions/8156707/gzip-a-file-in-python
|
||||
|
||||
def convert_to_gzip(src, out, f):
|
||||
input_file = f'{src}{f}'
|
||||
output_file = f'{out}{f}.gz.h'
|
||||
output_charp = f'{f.replace(".", "_")}_gz'
|
||||
src_dir='data'
|
||||
out_dir='data_gz'
|
||||
|
||||
top = ''
|
||||
with open(input_file, 'rb') as f_in:
|
||||
gz = gzip.compress(f_in.read())
|
||||
gzlen = len(gz)
|
||||
if not 'data_gz' in os.listdir():
|
||||
os.mkdir('data_gz')
|
||||
|
||||
top += f'// filename: {f}.gz.h\n'
|
||||
top += f'#define {output_charp}_len {gzlen}\n'
|
||||
top += f'const char {output_charp}[] = '
|
||||
top += '{'
|
||||
|
||||
with open(output_file, 'wb') as f_out:
|
||||
for i, b in enumerate(gz):
|
||||
if not i%10:
|
||||
top += '\n '
|
||||
top += f'0x{b:02X}, '
|
||||
top = top[:-2]
|
||||
top += '};'
|
||||
f_out.write(top.encode(encoding='utf-8'))
|
||||
|
||||
|
||||
src='data/'
|
||||
out='compress/'
|
||||
|
||||
if not 'compress' in os.listdir():
|
||||
os.mkdir('compress')
|
||||
|
||||
for f in os.listdir(src):
|
||||
convert_to_gzip(src, out, f)
|
||||
for f in os.listdir(src_dir):
|
||||
with open(f'{src_dir}/{f}', 'rb') as f_in:
|
||||
with gzip.open(f'{out_dir}/{f}.gz', 'wb') as f_out:
|
||||
f_out.writelines(f_in)
|
||||
@@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Ich bin ein komrpimierter Test</title>
|
||||
I bims der Head
|
||||
</head>
|
||||
<body>
|
||||
I bims der Body
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -54,6 +54,12 @@ else
|
||||
server.streamFile(f, mime::getContentType(SRH::_path), requestMethod);
|
||||
```
|
||||
|
||||
LED_sequence_v1:
|
||||
TODO:
|
||||
- disable led stripes after defined time (webserver paramter?)
|
||||
- define what to do if people enter stairs from different directions
|
||||
- CAUTION: Sensor-Deadtime at LEAST 8 seconds
|
||||
- --> switch off with person leaving stairs might not be possible
|
||||
|
||||
Webserver on v0.3.0 now working with LittleFS
|
||||
TODO:
|
||||
@@ -62,7 +68,15 @@ TODO:
|
||||
- maybe compress files ?
|
||||
|
||||
|
||||
LED Flickering issue
|
||||
### LED Flickering issue
|
||||
https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
|
||||
PlatformIO Library veraltet !!
|
||||
|
||||
PlatformIO Library veraltet
|
||||
|
||||
### TIMER in OTA unterbrechen !!!!!
|
||||
|
||||
### further improvments
|
||||
with streamFile gzip encoded Files can be sent
|
||||
this reduces space on fs !
|
||||
new easier script needed to just convert to .gz
|
||||
maybe measure timings on ESP
|
||||
@@ -1,48 +0,0 @@
|
||||
#ifndef __HTTPSERVER_H
|
||||
#define __HTTPSERVER_H
|
||||
|
||||
#include "filesys.h"
|
||||
#include <ESP8266WebServer.h>
|
||||
|
||||
|
||||
class HTTPServer : public ESP8266WebServer {
|
||||
private:
|
||||
const char* rootDir;
|
||||
|
||||
bool addRootFileHandler();
|
||||
bool formatFS() {
|
||||
return format_fs();
|
||||
}
|
||||
void listRoot() {
|
||||
ls(rootDir);
|
||||
}
|
||||
|
||||
public:
|
||||
HTTPServer(const int _port, const char* _rootDir) :
|
||||
ESP8266WebServer(_port), rootDir(_rootDir)
|
||||
{ }
|
||||
~HTTPServer()
|
||||
{
|
||||
Serial.printf("[HTTPServer] shut down ...\n\r");
|
||||
}
|
||||
|
||||
bool start() {
|
||||
if(!mount_fs())
|
||||
return false;
|
||||
Serial.printf("[HTTPServer] LittleFS mounted !\n\r");
|
||||
|
||||
Serial.printf("[HTTPServer] root:\n\r");
|
||||
this->listRoot();
|
||||
Serial.printf("\n\r");
|
||||
|
||||
if( this->addRootFileHandler() ){
|
||||
this->begin();
|
||||
Serial.printf("[HTTPServer] Server active on Port 80 !\n\r");
|
||||
return true;
|
||||
}
|
||||
Serial.printf("[HTTPServer] Not starting Server, something went wrong !\n\r");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // __HTTPSERVER_H
|
||||
+5
-5
@@ -1,5 +1,8 @@
|
||||
#ifndef __OTA_H
|
||||
#define __OTA_H
|
||||
#pragma once
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include <ArduinoOTA.h>
|
||||
|
||||
void ota_setup() {
|
||||
ArduinoOTA.setPort(8266);
|
||||
@@ -43,6 +46,3 @@ void ota_setup() {
|
||||
ArduinoOTA.begin();
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // __OTA_H
|
||||
@@ -1,38 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "PCA9685.h"
|
||||
|
||||
class Treppe {
|
||||
private:
|
||||
uint8_t stairs;
|
||||
uint16_t time_per_stair = 300; // dimmtime per stair [ms]
|
||||
uint16_t idle_brightness = 200;
|
||||
uint16_t active_brightness = 2048;
|
||||
|
||||
uint8_t direction = 0;
|
||||
uint8_t state = 0;
|
||||
uint8_t switch_state = 0;
|
||||
|
||||
// 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();
|
||||
|
||||
public:
|
||||
Treppe(uint8_t _stairs) : stairs(_stairs){}
|
||||
void task(); // call periodically
|
||||
|
||||
void setup();
|
||||
|
||||
// Parameter section
|
||||
uint16_t setIdle(uint16_t _idle_brightness);
|
||||
uint16_t setActive(uint16_t _active_brightness);
|
||||
uint16_t setTime(uint16_t _time_per_stair);
|
||||
|
||||
// Runtime Parameter section
|
||||
uint8_t setDirection(uint8_t _direction);
|
||||
uint8_t setState(uint8_t _state);
|
||||
|
||||
uint8_t getState() { return state;};
|
||||
uint8_t getDirection() {return direction;};
|
||||
};
|
||||
@@ -4,7 +4,7 @@
|
||||
// deleteFile("/hello.txt");
|
||||
// writeFile("/hello.txt", "Hello ");
|
||||
// appendFile("/hello.txt", "World!\n\r");
|
||||
// readFile("/hello.txt");
|
||||
// printFile("/hello.txt");
|
||||
// listDir("/");
|
||||
|
||||
FSInfo fsinfo;
|
||||
@@ -58,7 +58,12 @@ void ls(const char * dirname) {
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void readFile(const char * path) {
|
||||
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");
|
||||
@@ -1,16 +1,15 @@
|
||||
#ifndef __FILESYS_H
|
||||
#define __FILESYS_H
|
||||
#pragma once
|
||||
|
||||
#include <LittleFS.h>
|
||||
|
||||
bool mount_fs();
|
||||
bool format_fs();
|
||||
|
||||
File open(const char * path);
|
||||
|
||||
void ls(const char * dirname);
|
||||
void readFile(const char * path);
|
||||
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);
|
||||
|
||||
#endif // __FILESYS_H
|
||||
File renamed without changes.
@@ -0,0 +1,86 @@
|
||||
#pragma once
|
||||
|
||||
#include "filesys.h"
|
||||
#include <ESP8266WebServer.h>
|
||||
|
||||
class HTTPServer : public ESP8266WebServer {
|
||||
private:
|
||||
const char* rootDir;
|
||||
|
||||
bool addRootFileHandler();
|
||||
bool formatFS() {
|
||||
return format_fs();
|
||||
}
|
||||
void listRoot() {
|
||||
ls(rootDir);
|
||||
}
|
||||
|
||||
public:
|
||||
HTTPServer(const int _port, const char* _rootDir) :
|
||||
ESP8266WebServer(_port), rootDir(_rootDir)
|
||||
{ }
|
||||
~HTTPServer()
|
||||
{
|
||||
Serial.printf("[HTTPServer] shut down ...\n\r");
|
||||
}
|
||||
|
||||
bool start() {
|
||||
if(!mount_fs())
|
||||
return false;
|
||||
Serial.printf("[HTTPServer] LittleFS mounted !\n\r");
|
||||
|
||||
Serial.printf("[HTTPServer] root:\n\r");
|
||||
this->listRoot();
|
||||
Serial.printf("\n\r");
|
||||
|
||||
// https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11
|
||||
on("/compress.html", [this]() {
|
||||
File f = open("/compress.html.gz");
|
||||
if (!f) {
|
||||
Serial.println("Failed to open file for reading");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Streaming /compress.html.gz to client");
|
||||
const String cont = "text/html";
|
||||
streamFile(f, cont);
|
||||
}
|
||||
});
|
||||
on("/comp.html", [this]() {
|
||||
File f = open("/comp.html.gz");
|
||||
if (!f) {
|
||||
Serial.println("Failed to open file for reading");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Streaming /comp.html.gz to client");
|
||||
const String cont = "text/html";
|
||||
streamFile(f, cont);
|
||||
}
|
||||
});
|
||||
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);
|
||||
});
|
||||
|
||||
if( this->addRootFileHandler() ){
|
||||
this->begin();
|
||||
Serial.printf("[HTTPServer] Server active on Port 80 !\n\r");
|
||||
return true;
|
||||
}
|
||||
Serial.printf("[HTTPServer] Not starting Server, something went wrong !\n\r");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,232 @@
|
||||
#include "treppe.h"
|
||||
|
||||
uint8_t Treppe::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 float current_pwm = 0;
|
||||
static float stepsize = 1.0;
|
||||
if(led != lastled){
|
||||
pwmController.setChannelPWM(led, (uint16_t)startval);
|
||||
lastled = led;
|
||||
current_pwm = startval;
|
||||
stepsize = INT_TIME*abs(stopval - startval)/(float)time_per_stair; // only valid at 1ms function call interval
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(startval > stopval){
|
||||
current_pwm -= stepsize;
|
||||
}
|
||||
else {
|
||||
current_pwm += stepsize;
|
||||
}
|
||||
// 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);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Treppe::ledsequence(){
|
||||
static int8_t led = 0;
|
||||
static uint16_t brightness = 0;
|
||||
static uint16_t lastbrightness = 0;
|
||||
static uint16_t status = 0;
|
||||
uint16_t status_build = 0;
|
||||
status_build |= direction << 8;
|
||||
status_build |= state;
|
||||
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 = stairs-1;
|
||||
if(state){
|
||||
brightness = active_brightness; // set brightness value depending of on/off
|
||||
lastbrightness = idle_brightness;
|
||||
}
|
||||
else{
|
||||
brightness = idle_brightness;
|
||||
lastbrightness = active_brightness;
|
||||
}
|
||||
status = status_build; // set parameter memory
|
||||
|
||||
Serial.printf("----Status Changed! onoff: %d, dir: %d\n", state, direction);
|
||||
}
|
||||
if(!finish){ // finish == 0 -> action pending
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 !");
|
||||
finish = 1;
|
||||
return;
|
||||
}
|
||||
tick++; // eins hoch
|
||||
}
|
||||
else { // abwärts
|
||||
if(tick <= 0) { // ziel erreicht
|
||||
Serial.println("[Treppe] unterster tick !");
|
||||
finish = 1;
|
||||
return;
|
||||
}
|
||||
tick--; // eins runter
|
||||
}
|
||||
|
||||
stufe = tick / ticks_pro_stufe;
|
||||
|
||||
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",
|
||||
tick,
|
||||
stufe,
|
||||
(tick - ticks_pro_stufe*stufe),
|
||||
new_pwm
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void Treppe::setup(){
|
||||
Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick);
|
||||
|
||||
pwmController.resetDevices();
|
||||
|
||||
// Deactive PCA9685 Phase Balancer due to LED Flickering
|
||||
// https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
|
||||
// see also lib/PCA9685-Arduin/PCA9685.h:204
|
||||
pwmController.init(PCA9685_PhaseBalancer_None);
|
||||
pwmController.setPWMFrequency(200);
|
||||
pwmController.setAllChannelsPWM(idle_brightness);
|
||||
|
||||
pinMode(SENSOR1, INPUT);
|
||||
pinMode(SENSOR2, INPUT);
|
||||
Serial.println("Hello from Treppe");
|
||||
Serial.print("Treppe: initial parameters: stairs=");
|
||||
Serial.println(stairs);
|
||||
}
|
||||
|
||||
void Treppe::task(){
|
||||
|
||||
if(finish){
|
||||
direction = switch_direction;
|
||||
state = switch_state;
|
||||
}
|
||||
static uint8_t last_sensor_state[2] = {0,0};
|
||||
uint8_t current_sensor_state[2] = {0,0};
|
||||
current_sensor_state[0] = digitalRead(SENSOR1);
|
||||
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];
|
||||
|
||||
ledsequence();
|
||||
}
|
||||
|
||||
|
||||
uint16_t Treppe::setIdle(uint16_t _idle_brightness){
|
||||
idle_brightness = _idle_brightness;
|
||||
Serial.println("Treppe: idle brightness changed!");
|
||||
return idle_brightness;
|
||||
}
|
||||
uint16_t Treppe::setActive(uint16_t _active_brightness){
|
||||
active_brightness = _active_brightness;
|
||||
Serial.println("Treppe: active brightness changed!");
|
||||
return active_brightness;
|
||||
}
|
||||
uint16_t Treppe::setTime(uint16_t _time_per_stair){
|
||||
time_per_stair = _time_per_stair;
|
||||
Serial.println("Treppe: time changed!");
|
||||
return time_per_stair;
|
||||
}
|
||||
|
||||
void Treppe::setDirection(uint8_t _direction){
|
||||
switch_direction = _direction;
|
||||
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
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
#pragma once
|
||||
|
||||
#include "PCA9685.h"
|
||||
|
||||
#define SENSOR1 15
|
||||
#define SENSOR2 12
|
||||
|
||||
#define INT_TIME 20 // interrupt intervall [ms]
|
||||
|
||||
class Treppe {
|
||||
private:
|
||||
uint8_t stairs;
|
||||
uint16_t time_per_stair = 300; // dimmtime per stair [ms]
|
||||
uint16_t idle_brightness = 0;
|
||||
uint16_t active_brightness = 500;
|
||||
|
||||
uint8_t direction = 0;
|
||||
uint8_t switch_direction = 0;
|
||||
uint8_t state = 0;
|
||||
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){
|
||||
ticks_pro_stufe = time_per_stair / 20; // [ms]
|
||||
ticks_treppe = ticks_pro_stufe * stairs;
|
||||
|
||||
differenz_pwm_pro_tick = (float) (active_brightness - idle_brightness)
|
||||
/ (float) ticks_pro_stufe;
|
||||
}
|
||||
|
||||
void setup();
|
||||
void task(); // call periodically
|
||||
|
||||
// Parameter section
|
||||
uint16_t setIdle(uint16_t _idle_brightness);
|
||||
uint16_t setActive(uint16_t _active_brightness);
|
||||
uint16_t setTime(uint16_t _time_per_stair);
|
||||
|
||||
void setTick(uint32_t _tick) {
|
||||
tick = _tick;
|
||||
Serial.printf("Treppe: Tick: %u!\n", tick);
|
||||
}
|
||||
uint32_t getTicks() {
|
||||
return ticks_treppe;
|
||||
}
|
||||
|
||||
// Runtime Parameter section
|
||||
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;};
|
||||
};
|
||||
+21
-39
@@ -9,12 +9,8 @@ extern "C" {
|
||||
#include "user_interface.h"
|
||||
}
|
||||
// OTA & WEB
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#include "ota.h"
|
||||
|
||||
#include "wifi_credentials.h"
|
||||
#include "ota.h"
|
||||
#include "httpserver.h"
|
||||
|
||||
// BOARD
|
||||
@@ -22,13 +18,10 @@ extern "C" {
|
||||
#define NODEMCU_LED 16
|
||||
|
||||
// PWM
|
||||
|
||||
#include "pwm.h"
|
||||
#include "treppe.h"
|
||||
os_timer_t timer1;
|
||||
uint8_t timer_flag = 0;
|
||||
|
||||
Treppe stairs(13);
|
||||
|
||||
Treppe stairs(16);
|
||||
|
||||
// WIFI
|
||||
const char* ssid = STASSID;
|
||||
@@ -41,8 +34,7 @@ void timerCallback(void *pArg)
|
||||
stairs.task();
|
||||
}
|
||||
|
||||
// HTTP
|
||||
void handleNotFound();
|
||||
// port 80, root directory of server '/'
|
||||
HTTPServer httpServer(80, "/");
|
||||
|
||||
uint32_t _t=0;
|
||||
@@ -51,22 +43,6 @@ uint32_t _t=0;
|
||||
// ===============================================
|
||||
|
||||
|
||||
void handleNotFound() {
|
||||
String message = "File Not Found\n\n";
|
||||
message += "URI: ";
|
||||
message += httpServer.uri();
|
||||
message += "\nMethod: ";
|
||||
message += (httpServer.method() == HTTP_GET) ? "GET" : "POST";
|
||||
message += "\nArguments: ";
|
||||
message += httpServer.args();
|
||||
message += "\n";
|
||||
|
||||
for (uint8_t i = 0; i < httpServer.args(); i++) {
|
||||
message += " " + httpServer.argName(i) + ": " + httpServer.arg(i) + "\n";
|
||||
}
|
||||
|
||||
httpServer.send(404, "text/plain", message);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
#ifdef WITH_DEBUGGING_ON
|
||||
@@ -102,7 +78,6 @@ void setup() {
|
||||
ota_setup();
|
||||
|
||||
httpServer.start();
|
||||
httpServer.onNotFound(handleNotFound);
|
||||
Serial.println("HTTP server started !");
|
||||
|
||||
stairs.setup();
|
||||
@@ -110,17 +85,24 @@ void setup() {
|
||||
|
||||
os_timer_setfn(&timer1, timerCallback, &timer_flag);
|
||||
os_timer_arm(&timer1, 20, true);
|
||||
|
||||
stairs.setState(1);
|
||||
stairs.setDirection(1);
|
||||
}
|
||||
|
||||
#include <random>
|
||||
|
||||
void loop() {
|
||||
if(millis() > 25000 && stairs.getState() == 1 && stairs.getDirection() == 1) stairs.setState(0);
|
||||
if(millis() > 45000 && stairs.getDirection() == 1){
|
||||
stairs.setState(1);
|
||||
stairs.setDirection(0);
|
||||
}
|
||||
TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA");
|
||||
TIMEIF_US(httpServer.handleClient(), 1000, "HTTP");
|
||||
// if(stairs.getState() == 0) {
|
||||
// delay(1000);
|
||||
|
||||
// // uint32_t t = rand() % stairs.getTicks();
|
||||
// // uint32_t d = rand() % 2;
|
||||
|
||||
// // stairs.setTick(t);
|
||||
// // stairs.setDirection(d);
|
||||
|
||||
// stairs.setDirection(!stairs.getDirection());
|
||||
// stairs.setState(1);
|
||||
// }
|
||||
|
||||
TIMEIF_US(ArduinoOTA.handle(), 10000, "OTA");
|
||||
TIMEIF_US(httpServer.handleClient(), 10000, "HTTP");
|
||||
}
|
||||
-140
@@ -1,140 +0,0 @@
|
||||
#include "pwm.h"
|
||||
|
||||
uint8_t Treppe::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 float current_pwm = 0;
|
||||
static float stepsize = 1.0;
|
||||
if(led != lastled){
|
||||
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 - stepsize && current_pwm < stopval + stepsize) return 0;
|
||||
// todo: duty cycle zero!
|
||||
if(startval > stopval){
|
||||
current_pwm -= stepsize;
|
||||
}
|
||||
else {
|
||||
current_pwm += stepsize;
|
||||
}
|
||||
Serial.println((uint16_t)current_pwm);
|
||||
pwmController.setChannelPWM(led, (uint16_t)current_pwm);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Treppe::ledsequence(){
|
||||
static int8_t led = 0;
|
||||
static uint16_t brightness = 0;
|
||||
static uint16_t lastbrightness = 0;
|
||||
static uint8_t finish = 1;
|
||||
static uint16_t status = 0;
|
||||
uint16_t status_build = 0;
|
||||
status_build |= direction << 8;
|
||||
status_build |= state;
|
||||
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 = stairs-1;
|
||||
if(state){
|
||||
brightness = active_brightness; // set brightness value depending of on/off
|
||||
lastbrightness = idle_brightness;
|
||||
}
|
||||
else{
|
||||
brightness = idle_brightness;
|
||||
lastbrightness = active_brightness;
|
||||
}
|
||||
status = status_build; // set parameter memory
|
||||
Serial.print("----Status Changed! onoff: ");
|
||||
Serial.print(state);
|
||||
Serial.print(" dir: ");
|
||||
Serial.println(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) {
|
||||
finish = 1;
|
||||
//lastbrightness = brightness;
|
||||
}
|
||||
}
|
||||
else{
|
||||
led--;
|
||||
if(led < 0){
|
||||
//lastbrightness = brightness;
|
||||
finish = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Treppe::setup(){
|
||||
pwmController.resetDevices();
|
||||
|
||||
// Deactive PCA9685 due to LED Flickering
|
||||
// https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15
|
||||
// see also lib/PCA9685-Arduin/PCA9685.h:204
|
||||
pwmController.init(PCA9685_PhaseBalancer_None);
|
||||
pwmController.setPWMFrequency(200);
|
||||
Serial.println("Hello from Treppe");
|
||||
Serial.print("Treppe: initial parameters: stairs=");
|
||||
Serial.println(stairs);
|
||||
}
|
||||
void Treppe::task(){
|
||||
ledsequence();
|
||||
}
|
||||
|
||||
uint16_t Treppe::setIdle(uint16_t _idle_brightness){
|
||||
idle_brightness = _idle_brightness;
|
||||
Serial.println("Treppe: idle brightness changed!");
|
||||
return idle_brightness;
|
||||
}
|
||||
uint16_t Treppe::setActive(uint16_t _active_brightness){
|
||||
active_brightness = _active_brightness;
|
||||
Serial.println("Treppe: active brightness changed!");
|
||||
return active_brightness;
|
||||
}
|
||||
uint16_t Treppe::setTime(uint16_t _time_per_stair){
|
||||
time_per_stair = _time_per_stair;
|
||||
Serial.println("Treppe: time changed!");
|
||||
return time_per_stair;
|
||||
}
|
||||
|
||||
uint8_t Treppe::setDirection(uint8_t _direction){
|
||||
direction = _direction;
|
||||
Serial.println("Treppe: Direction changed!");
|
||||
// to do: implement state command variable to determine dimm-state
|
||||
return direction;
|
||||
}
|
||||
|
||||
uint8_t Treppe::setState(uint8_t _state){
|
||||
if(state == _state) return 1;
|
||||
else{
|
||||
state = _state;
|
||||
Serial.println("Treppe: State changed!");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
+30
-30
@@ -1,40 +1,40 @@
|
||||
#include <Arduino.h>
|
||||
#include <unity.h>
|
||||
// #include <Arduino.h>
|
||||
// #include <unity.h>
|
||||
|
||||
String STR_TO_TEST;
|
||||
// String STR_TO_TEST;
|
||||
|
||||
void setUp(void) {
|
||||
// set stuff up here
|
||||
STR_TO_TEST = "Hello, world!";
|
||||
}
|
||||
// // void setUp(void) {
|
||||
// // // set stuff up here
|
||||
// // STR_TO_TEST = "Hello, world!";
|
||||
// // }
|
||||
|
||||
void tearDown(void) {
|
||||
// clean stuff up here
|
||||
STR_TO_TEST = "";
|
||||
}
|
||||
// // void tearDown(void) {
|
||||
// // // clean stuff up here
|
||||
// // STR_TO_TEST = "";
|
||||
// // }
|
||||
|
||||
void test_led_builtin_pin_number(void) {
|
||||
TEST_ASSERT_EQUAL(2, LED_BUILTIN);
|
||||
}
|
||||
// void test_led_builtin_pin_number(void) {
|
||||
// TEST_ASSERT_EQUAL(2, LED_BUILTIN);
|
||||
// }
|
||||
|
||||
void test_string_concat(void) {
|
||||
String hello = "Hello, ";
|
||||
String world = "world!";
|
||||
TEST_ASSERT_EQUAL_STRING(STR_TO_TEST.c_str(), (hello + world).c_str());
|
||||
}
|
||||
// void test_string_concat(void) {
|
||||
// String hello = "Hello, ";
|
||||
// String world = "world!";
|
||||
// TEST_ASSERT_EQUAL_STRING(STR_TO_TEST.c_str(), (hello + world).c_str());
|
||||
// }
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
delay(2000); // service delay
|
||||
UNITY_BEGIN();
|
||||
// void setup()
|
||||
// {
|
||||
// delay(2000); // service delay
|
||||
// UNITY_BEGIN();
|
||||
|
||||
RUN_TEST(test_led_builtin_pin_number);
|
||||
RUN_TEST(test_string_concat);
|
||||
// RUN_TEST(test_led_builtin_pin_number);
|
||||
// RUN_TEST(test_string_concat);
|
||||
|
||||
UNITY_END(); // stop unit testing
|
||||
}
|
||||
// UNITY_END(); // stop unit testing
|
||||
// }
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
// void loop()
|
||||
// {
|
||||
// }
|
||||
@@ -0,0 +1,47 @@
|
||||
// #include <Arduino.h>
|
||||
#include "treppe.h"
|
||||
#include <unity.h>
|
||||
|
||||
Treppe stairs(10);
|
||||
|
||||
void setUp(void) {
|
||||
// run before each test
|
||||
// set stuff up here
|
||||
stairs.setup();
|
||||
Serial.println("Treppe initialized !");
|
||||
Serial.println("PCA9685 connected !");
|
||||
}
|
||||
|
||||
void tearDown(void) {
|
||||
// clean stuff up here
|
||||
}
|
||||
|
||||
void test_set_state(void) {
|
||||
stairs.setState(1);
|
||||
TEST_ASSERT_EQUAL(1, stairs.getState());
|
||||
stairs.setState(0);
|
||||
TEST_ASSERT_EQUAL(0, stairs.getState());
|
||||
}
|
||||
|
||||
|
||||
void test_set_direction(void) {
|
||||
stairs.setDirection(1);
|
||||
TEST_ASSERT_EQUAL(1, stairs.getDirection());
|
||||
stairs.setDirection(0);
|
||||
TEST_ASSERT_EQUAL(0, stairs.getDirection());
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
delay(2000); // service delay
|
||||
UNITY_BEGIN();
|
||||
|
||||
RUN_TEST(test_set_state);
|
||||
RUN_TEST(test_set_direction);
|
||||
|
||||
UNITY_END(); // stop unit testing
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
Reference in new issue
Block a user