document.getElementById(range).oninput = function () { | document.getElementById(range).oninput = function () { | ||||
document.getElementById(output).innerHTML = this.value; | document.getElementById(output).innerHTML = this.value; | ||||
// add to pending range changes | // add to pending range changes | ||||
rangeValues[range] = val; | |||||
rangeValues[this.id] = this.value; | |||||
} | } | ||||
} | } | ||||
// use data- attributes for action | // use data- attributes for action | ||||
document.querySelectorAll('.control').forEach((button) => { | document.querySelectorAll('.control').forEach((button) => { | ||||
button.onclick = () => { | button.onclick = () => { | ||||
fetch(`/action=${button.dataset.action}`) | |||||
fetch(`/action`, { | |||||
method: 'POST', | |||||
headers: { | |||||
'Content-Type': 'application/x-www-form-urlencoded', | |||||
}, | |||||
body: `control=${button.dataset.action}` | |||||
}) | |||||
.then(response => console.log(response)) | .then(response => console.log(response)) | ||||
.catch(error => console.log('Error:', error)); | .catch(error => console.log('Error:', error)); | ||||
} | } |
// add static root file handler for http | // add static root file handler for http | ||||
this->serveStatic("/", LittleFS, "/"); | this->serveStatic("/", LittleFS, "/"); | ||||
this->begin(); | this->begin(); | ||||
Serial.printf("Server active on Port 80 !\n\r"); | |||||
logf("Server active on Port 80 !\n\r"); | |||||
return true; | return true; | ||||
} | } | ||||
void HTTPServer::start_apps() { | void HTTPServer::start_apps() { | ||||
this->on("/action=s_oben", [this]() { | |||||
// treppe->setsensor("oben") | |||||
this->on("/action", HTTP_POST, [this]() { | |||||
if (args()) { | |||||
logf("%s=%s\n", argName(0).c_str(), arg(0).c_str()); | |||||
if (argName(0).equals("control")) { | |||||
if (arg(0).equals("s_oben")) { | |||||
treppe->overwrite_sensors(true, false); | |||||
logt("control => s_oben !\n"); | |||||
} | |||||
else if (arg(0).equals("s_unten")) { | |||||
treppe->overwrite_sensors(false, true); | |||||
logt("control => s_unten !\n"); | |||||
} | |||||
} | |||||
} | |||||
send(200, "text/plain", "accepted"); | send(200, "text/plain", "accepted"); | ||||
}); | }); | ||||
this->on("/update", HTTP_POST, [this]() { | this->on("/update", HTTP_POST, [this]() { | ||||
if (args()) { | if (args()) { | ||||
for (int i = 0; i < args() - 1; i++) { | for (int i = 0; i < args() - 1; i++) { | ||||
Serial.printf("%s=%s\n", argName(i).c_str(), arg(i).c_str()); | |||||
logf("%s=%s\n", argName(i).c_str(), arg(i).c_str()); | |||||
if (argName(i).equals("range_act_pwm")) { | if (argName(i).equals("range_act_pwm")) { | ||||
treppe->set_active_pwm(arg(i).toInt(), VORGABE_PROZENT); | treppe->set_active_pwm(arg(i).toInt(), VORGABE_PROZENT); | ||||
}); | }); | ||||
this->on("/terminal", HTTP_POST, [this]() { | this->on("/terminal", HTTP_POST, [this]() { | ||||
// Serial.printf("got /terminal\n"); | |||||
// logf("got /terminal\n"); | |||||
if (tbuf_head) { | if (tbuf_head) { | ||||
send(200, "text/plain", tbuf); | send(200, "text/plain", tbuf); | ||||
tbuf_head = 0; | tbuf_head = 0; |
Serial.printf("DIM_LDR: start: %d, ziel: %d\n", dimmer->start_pwm, | Serial.printf("DIM_LDR: start: %d, ziel: %d\n", dimmer->start_pwm, | ||||
dimmer->ziel_pwm); | dimmer->ziel_pwm); | ||||
return true; | return true; | ||||
} else // DIM_STUFEN | |||||
{ | |||||
} else { // DIM_STUFEN | |||||
Serial.printf("DIM_STUFEN: stufe: %d, start: %d, ziel: %d\n", | Serial.printf("DIM_STUFEN: stufe: %d, start: %d, ziel: %d\n", | ||||
dimmer->stufe, dimmer->start_pwm, dimmer->ziel_pwm); | dimmer->stufe, dimmer->start_pwm, dimmer->ziel_pwm); | ||||
if (dimmer->stufe >= stufen - 1) | if (dimmer->stufe >= stufen - 1) | ||||
return true; | return true; | ||||
dimmer->stufe++; | dimmer->stufe++; | ||||
} else // LR_RUNTER | |||||
{ | |||||
} else { // LR_RUNTER | |||||
if (dimmer->stufe <= 0) | if (dimmer->stufe <= 0) | ||||
return true; | return true; | ||||
dimmer->stufe--; | dimmer->stufe--; | ||||
} | } | ||||
} | } | ||||
bool Treppe::read_sensor(int sensor) { | |||||
void Treppe::overwrite_sensors(bool s_oben, bool s_unten) { | |||||
fsm_pend.web_ctrl_s_oben = s_oben; | |||||
fsm_pend.web_ctrl_s_unten = s_unten; | |||||
} | |||||
/* | |||||
reads sensors with edge detection | |||||
void Treppe::read_sensors() { | |||||
const bool s_oben = digitalRead(SENSOR_OBEN); | |||||
const bool s_unten = digitalRead(SENSOR_UNTEN); | |||||
returns true if motion was detected | |||||
returns false if no motion was detected | |||||
returns false if motion was detected, but state did not change back to not | |||||
detected | |||||
*/ | |||||
uint8_t pegel = digitalRead(sensor); | |||||
static uint8_t pegel_alt[2] = {0, 0}; | |||||
fsm_pend.sensor_oben = false; | |||||
fsm_pend.sensor_unten = false; | |||||
uint8_t index = 0; | |||||
if (sensor == SENSOR_OBEN) | |||||
index = 0; | |||||
else | |||||
index = 1; | |||||
// rising trigger => 1 cycle true ! | |||||
if (s_oben && !fsm_pend.last_s_oben) { | |||||
fsm_pend.sensor_oben = true; | |||||
} | |||||
if (s_unten && !fsm_pend.last_s_unten) { | |||||
fsm_pend.sensor_unten = true; | |||||
} | |||||
if (pegel == 1 && pegel_alt[index] == 0) { | |||||
pegel_alt[index] = pegel; | |||||
return true; | |||||
} else { | |||||
pegel_alt[index] = pegel; | |||||
return false; | |||||
fsm_pend.last_s_oben = s_oben; | |||||
fsm_pend.last_s_unten = s_unten; | |||||
// check for manipulation over webserver | |||||
if(fsm_pend.web_ctrl_s_oben) { | |||||
fsm_pend.sensor_oben = true; | |||||
fsm_pend.web_ctrl_s_oben = false; | |||||
} | |||||
if(fsm_pend.web_ctrl_s_unten) { | |||||
fsm_pend.sensor_unten = true; | |||||
fsm_pend.web_ctrl_s_unten = false; | |||||
} | } | ||||
// return static_cast<bool>(pegel); | |||||
} | } | ||||
float Treppe::read_ldr() { | float Treppe::read_ldr() { | ||||
// TODO wenn LDR geändert => idle_pwm_soll anpassen | // TODO wenn LDR geändert => idle_pwm_soll anpassen | ||||
// fsm_pend.ldr_changed = true; | // fsm_pend.ldr_changed = true; | ||||
fsm_inputs.ldr_schwelle = check_ldr(); | fsm_inputs.ldr_schwelle = check_ldr(); | ||||
#ifdef DEBUG_TIMING | #ifdef DEBUG_TIMING | ||||
m = micros(); | m = micros(); | ||||
#endif | #endif | ||||
fsm_inputs.sensor_oben = read_sensor(SENSOR_OBEN); | |||||
fsm_inputs.sensor_unten = read_sensor(SENSOR_UNTEN); | |||||
read_sensors(); | |||||
fsm_inputs.sensor_oben = fsm_pend.sensor_oben; | |||||
fsm_inputs.sensor_unten = fsm_pend.sensor_unten; | |||||
fsm_inputs.anim_beendet = fsm_pend.anim_beendet; | fsm_inputs.anim_beendet = fsm_pend.anim_beendet; | ||||
#ifdef DEBUG_TIMING | #ifdef DEBUG_TIMING |
struct fsm_pending_inputs_t { | struct fsm_pending_inputs_t { | ||||
bool anim_beendet = true; | bool anim_beendet = true; | ||||
bool sensor_unten = false; | bool sensor_unten = false; | ||||
bool last_s_unten = false; | |||||
bool web_ctrl_s_unten = false; | |||||
bool sensor_oben = false; | bool sensor_oben = false; | ||||
bool last_s_oben = false; | |||||
bool web_ctrl_s_oben = false; | |||||
bool ldr_changed = false; | bool ldr_changed = false; | ||||
}; | }; | ||||
fsm_pending_inputs_t fsm_pend; | fsm_pending_inputs_t fsm_pend; | ||||
enum fsm_laufrichtung_t { LR_RUNTER = 0, LR_HOCH = 1 }; | enum fsm_laufrichtung_t { LR_RUNTER = 0, LR_HOCH = 1 }; | ||||
enum fsm_dimmrichtung_t { DR_ABDIMMEN = 0, DR_AUFDIMMEN = 1 }; | enum fsm_dimmrichtung_t { DR_ABDIMMEN = 0, DR_AUFDIMMEN = 1 }; | ||||
void read_sensors(); | |||||
void print_state_on_change(); | |||||
/* DIMM */ | /* DIMM */ | ||||
// bool dimmer(dimmer_t* dimmer, bool dim_type); | |||||
bool dimmer_tick(dimmer_t *dimmer, bool dim_type); | bool dimmer_tick(dimmer_t *dimmer, bool dim_type); | ||||
void start_animation(dimmer_t *dimmer, bool dim_type, uint16_t on_pwm, | void start_animation(dimmer_t *dimmer, bool dim_type, uint16_t on_pwm, | ||||
uint16_t off_pwm); | uint16_t off_pwm); | ||||
// void berechne_dimmer(dimmer_t* dimmer, bool dim_type); | |||||
void print_state_on_change(); | |||||
/* LDR */ | /* LDR */ | ||||
bool read_sensor(int sensor); | bool read_sensor(int sensor); | ||||
void set_time_ldr(const uint16_t value); | void set_time_ldr(const uint16_t value); | ||||
void set_time_per_stair(const uint16_t value); | void set_time_per_stair(const uint16_t value); | ||||
void set_ldr_schwelle(const uint16_t value, const vorgabe_typ_t vorgabe_typ); | void set_ldr_schwelle(const uint16_t value, const vorgabe_typ_t vorgabe_typ); | ||||
void overwrite_sensors(bool s_oben, bool s_unten); | |||||
}; | }; | ||||
#endif // __TREPPE_H | #endif // __TREPPE_H |