code beautifying

This commit is contained in:
Wolfgang Hottgenroth 2018-01-30 16:44:38 +01:00
parent 10f155caa3
commit bcb7d511a7
5 changed files with 94 additions and 58 deletions

View File

@ -13,17 +13,20 @@ configItems = [
{"label":"MQTT Username", "key":"mqttUser", "type":"C", "length":32, "default":"TouchSwitch"},
{"label":"MQTT Password", "key":"mqttPass", "type":"C", "length":32, "default":"geheim123"},
{"label":"MQTT ClientId", "key":"mqttClientId", "type":"C", "length":32, "default":"TouchSwitch"},
{"label":"MQTT Topic", "key":"mqttTopic", "type":"C", "length":64, "default":"IoT/TouchSwitch/Press"},
{"label":"MQTT Port", "key":"mqttPort", "type":"I", "default":8883},
{"label":"DebugMode", "key":"debugMode", "type":"I", "default":0},
{"label":"MQTT Topic", "key":"mqttTopic", "type":"C", "length":64, "default":"IoT/TouchSwitch/Press"},
{"label":"MQTT DebugTopic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/TouchSwitch/Debug"},
{"label":"MQTT BaselineTopic", "key":"mqttBaselineTopic", "type":"C", "length":64, "default":"IoT/TouchSwitch/Baseline"},
{"label":"DebugMode", "key":"debugMode", "type":"I", "default":0},
{"label":"Period", "key":"period", "type":"I", "default":500},
{"label":"CalibrationTimeout", "key":"calibrationTimeout", "type":"I", "default":20000},
{"label":"Threshold", "key":"threshold", "type":"I", "default":10}
]
h_file = Template(file="configuration_h.tmpl", searchList=[{"configItems":configItems}])
magic = 0xC0DE0002
h_file = Template(file="configuration_h.tmpl", searchList=[{"magic":magic, "configItems":configItems}])
open('configuration.h','w').write(str(h_file))
c_file = Template(file="configuration_c.tmpl", searchList=[{"configItems":configItems}])
c_file = Template(file="configuration_c.tmpl", searchList=[{"magic":magic, "configItems":configItems}])
open('configuration.cpp','w').write(str(c_file))

View File

@ -11,7 +11,7 @@
tConfigBlock configBlock;
const uint32_t MAGIC = 0xC0DE0001;
const uint32_t MAGIC = $magic;
extern ESP8266WebServer webServer;
bool configSaved = false;

View File

@ -9,7 +9,7 @@
tConfigBlock configBlock;
const uint32_t MAGIC = 0xC0DE0001;
const uint32_t MAGIC = 3235774466;
extern ESP8266WebServer webServer;
bool configSaved = false;
@ -24,10 +24,11 @@ void configServeIndex() {
strcpy(configBlock.mqttUser, "TouchSwitch");
strcpy(configBlock.mqttPass, "geheim123");
strcpy(configBlock.mqttClientId, "TouchSwitch");
strcpy(configBlock.mqttTopic, "IoT/TouchSwitch/Press");
configBlock.mqttPort = 8883;
configBlock.debugMode = 0;
strcpy(configBlock.mqttTopic, "IoT/TouchSwitch/Press");
strcpy(configBlock.mqttDebugTopic, "IoT/TouchSwitch/Debug");
strcpy(configBlock.mqttBaselineTopic, "IoT/TouchSwitch/Baseline");
configBlock.debugMode = 0;
configBlock.period = 500;
configBlock.calibrationTimeout = 20000;
configBlock.threshold = 10;
@ -136,6 +137,20 @@ void configServeIndex() {
buffer += configBlock.mqttClientId;
buffer += "\"";
buffer +=
" />"
" </td>"
" </tr>"
" <tr>"
" <td>"
" <label for\"mqttPort\">MQTT Port</label>"
" </td><td>"
" <input type=\"text\" name=\"mqttPort\" id=\"mqttPort\" ";
buffer += " value=\"";
buffer += configBlock.mqttPort;
buffer += "\"";
buffer +=
" />"
" </td>"
@ -157,12 +172,28 @@ void configServeIndex() {
" </tr>"
" <tr>"
" <td>"
" <label for\"mqttPort\">MQTT Port</label>"
" <label for\"mqttDebugTopic\">MQTT DebugTopic</label>"
" </td><td>"
" <input type=\"text\" name=\"mqttPort\" id=\"mqttPort\" ";
" <input type=\"text\" name=\"mqttDebugTopic\" id=\"mqttDebugTopic\" ";
buffer += " value=\"";
buffer += configBlock.mqttPort;
buffer += " size=\"64\" ";
buffer += " value=\"";
buffer += configBlock.mqttDebugTopic;
buffer += "\"";
buffer +=
" />"
" </td>"
" </tr>"
" <tr>"
" <td>"
" <label for\"mqttBaselineTopic\">MQTT BaselineTopic</label>"
" </td><td>"
" <input type=\"text\" name=\"mqttBaselineTopic\" id=\"mqttBaselineTopic\" ";
buffer += " size=\"64\" ";
buffer += " value=\"";
buffer += configBlock.mqttBaselineTopic;
buffer += "\"";
buffer +=
@ -179,21 +210,6 @@ void configServeIndex() {
buffer += configBlock.debugMode;
buffer += "\"";
buffer +=
" />"
" </td>"
" </tr>"
" <tr>"
" <td>"
" <label for\"mqttDebugTopic\">MQTT DebugTopic</label>"
" </td><td>"
" <input type=\"text\" name=\"mqttDebugTopic\" id=\"mqttDebugTopic\" ";
buffer += " size=\"64\" ";
buffer += " value=\"";
buffer += configBlock.mqttDebugTopic;
buffer += "\"";
buffer +=
" />"
" </td>"
@ -273,14 +289,16 @@ void configServeGetConfiguration() {
strcpy(configBlock.mqttPass, arg.c_str());
arg = webServer.arg("mqttClientId");
strcpy(configBlock.mqttClientId, arg.c_str());
arg = webServer.arg("mqttTopic");
strcpy(configBlock.mqttTopic, arg.c_str());
arg = webServer.arg("mqttPort");
configBlock.mqttPort = atoi(arg.c_str());
arg = webServer.arg("debugMode");
configBlock.debugMode = atoi(arg.c_str());
arg = webServer.arg("mqttTopic");
strcpy(configBlock.mqttTopic, arg.c_str());
arg = webServer.arg("mqttDebugTopic");
strcpy(configBlock.mqttDebugTopic, arg.c_str());
arg = webServer.arg("mqttBaselineTopic");
strcpy(configBlock.mqttBaselineTopic, arg.c_str());
arg = webServer.arg("debugMode");
configBlock.debugMode = atoi(arg.c_str());
arg = webServer.arg("period");
configBlock.period = atoi(arg.c_str());
arg = webServer.arg("calibrationTimeout");
@ -328,18 +346,21 @@ void showConfiguration() {
Serial.print("mqttClientId = ");
Serial.println(configBlock.mqttClientId);
Serial.print("mqttTopic = ");
Serial.println(configBlock.mqttTopic);
Serial.print("mqttPort = ");
Serial.println(configBlock.mqttPort);
Serial.print("debugMode = ");
Serial.println(configBlock.debugMode);
Serial.print("mqttTopic = ");
Serial.println(configBlock.mqttTopic);
Serial.print("mqttDebugTopic = ");
Serial.println(configBlock.mqttDebugTopic);
Serial.print("mqttBaselineTopic = ");
Serial.println(configBlock.mqttBaselineTopic);
Serial.print("debugMode = ");
Serial.println(configBlock.debugMode);
Serial.print("period = ");
Serial.println(configBlock.period);

View File

@ -6,10 +6,11 @@ typedef struct {
char mqttUser[32];
char mqttPass[32];
char mqttClientId[32];
char mqttTopic[64];
uint32_t mqttPort;
uint32_t debugMode;
char mqttTopic[64];
char mqttDebugTopic[64];
char mqttBaselineTopic[64];
uint32_t debugMode;
uint32_t period;
uint32_t calibrationTimeout;
uint32_t threshold;

View File

@ -10,7 +10,9 @@
#define MQTT_MAX_PACKET_SIZE 256
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <PubSubClient.h>
@ -18,7 +20,6 @@
#include "configuration.h"
typedef enum { NOT_PRESSED, PRESSED, LONG_PRESSED_PREPARE, LONG_PRESSED } tPressedState;
@ -26,9 +27,6 @@ typedef enum { NOT_PRESSED, PRESSED, LONG_PRESSED_PREPARE, LONG_PRESSED } tPress
WiFiClientSecure espClient;
PubSubClient client(espClient);
extern uint32_t startTime;
void setup_wifi() {
delay(10);
WiFi.mode(WIFI_STA);
@ -88,11 +86,14 @@ void reconnect() {
}
void mqtt_connect() {
bool mqtt_connect() {
bool reconnected = false;
if (!client.connected()) {
reconnect();
reconnected = true;
}
client.loop();
return reconnected;
}
@ -103,8 +104,11 @@ void setupProduction() {
void loopProduction() {
mqtt_connect();
bool reconnected = mqtt_connect();
noInterrupts();
// discharge
digitalWrite(TOUCH_PIN_SEND, LOW);
pinMode(TOUCH_PIN_SEND, OUTPUT);
@ -116,26 +120,24 @@ void loopProduction() {
digitalWrite(TOUCH_PIN_SEND, HIGH);
const uint32_t MAX_CHARGE_CNT = 1000;
static uint32_t senseCnt = 0;
static uint32_t chargeCntSum = 0;
uint32_t chargeCnt = 0;
noInterrupts();
while ((! digitalRead(TOUCH_PIN_RECEIVE)) && (chargeCnt < MAX_CHARGE_CNT)) {
chargeCnt++;
}
interrupts();
static uint32_t senseCnt = 0;
static uint32_t chargeCntSum = 0;
chargeCntSum += chargeCnt;
senseCnt++;
static uint32_t lastMillis = 0;
uint32_t currentMillis = millis();
static uint32_t lastCalibration = 0;
if (currentMillis - lastMillis > configBlock.period) {
if ((currentMillis - lastMillis > configBlock.period) && (! reconnected)) {
lastMillis = currentMillis;
uint32_t chargeCntAvg = chargeCntSum / senseCnt;
@ -143,6 +145,10 @@ void loopProduction() {
static uint32_t baseline = UINT32_MAX;
if (chargeCntAvg < baseline) {
baseline = chargeCntAvg;
char payload[128];
snprintf(payload, 127, "%ld", baseline);
client.publish(configBlock.mqttBaselineTopic, payload);
}
uint32_t capNum = chargeCntAvg - baseline;
@ -153,7 +159,10 @@ void loopProduction() {
client.publish(configBlock.mqttDebugTopic, payload);
}
static tPressedState state = NOT_PRESSED;
chargeCntSum = 0;
senseCnt = 0;
static enum { NOT_PRESSED, PRESSED, LONG_SHORT_GAP_1, LONG_SHORT_GAP_2, LONG_PRESSED } state = NOT_PRESSED;
if (capNum > configBlock.threshold) {
#ifdef DEBUG
char payload[128];
@ -166,9 +175,12 @@ void loopProduction() {
state = PRESSED;
break;
case PRESSED:
state = LONG_PRESSED_PREPARE;
state = LONG_SHORT_GAP_1;
break;
case LONG_PRESSED_PREPARE:
case LONG_SHORT_GAP_1:
state = LONG_SHORT_GAP_2;
break;
case LONG_SHORT_GAP_2:
state = LONG_PRESSED;
client.publish(configBlock.mqttTopic, "LONG_BEGIN");
break;
@ -178,6 +190,7 @@ void loopProduction() {
break;
}
} else {
static uint32_t lastCalibration = 0;
if (currentMillis - lastCalibration > configBlock.calibrationTimeout) {
baseline = UINT32_MAX;
lastCalibration = currentMillis;
@ -187,7 +200,8 @@ void loopProduction() {
case NOT_PRESSED:
break;
case PRESSED:
case LONG_PRESSED_PREPARE:
case LONG_SHORT_GAP_1:
case LONG_SHORT_GAP_2:
client.publish(configBlock.mqttTopic, "SHORT");
break;
case LONG_PRESSED:
@ -196,9 +210,6 @@ void loopProduction() {
}
state = NOT_PRESSED;
}
chargeCntSum = 0;
senseCnt = 0;
}
}