works now
This commit is contained in:
parent
e5e069e20c
commit
96710787d3
@ -184,10 +184,10 @@ environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.DT
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.DTS/value=3600
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.LOCAL/delimiter=\:
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.LOCAL/operation=replace
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.LOCAL/value=1524920161
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.LOCAL/value=1524929038
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.UTC/delimiter=\:
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.UTC/operation=replace
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.UTC/value=1524912961
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.UTC/value=1524921838
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.ZONE/delimiter=\:
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.ZONE/operation=replace
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.ZONE/value=3600
|
||||
@ -469,7 +469,7 @@ environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.COM_PORT
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.COM_PORT/value=/dev/ttyUSB4
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.ECLIPSE_LOCATION/delimiter=\:
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.ECLIPSE_LOCATION/operation=replace
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.ECLIPSE_LOCATION/value=${eclipse_home}////////////////////////////////////////////////////////////////////////////////////////////
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.ECLIPSE_LOCATION/value=${eclipse_home}//////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.EXTRA.ALL/delimiter=\:
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.EXTRA.ALL/operation=replace
|
||||
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.EXTRA.ALL/value=
|
||||
|
@ -20,8 +20,8 @@ configItems = [
|
||||
{"label":"MQTT DebugTopic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/MySwitch1/Debug"},
|
||||
{"label":"DebugMode", "key":"debugMode", "type":"I", "default":0},
|
||||
{"label":"Debounce (us)", "key":"debounce", "type":"I", "default":300},
|
||||
{"label":"Short press (ms)", "key":"shortPress", "type":"I", "default":300},
|
||||
{"label":"Long press (ms)", "key":"longPress", "type":"I", "default":2000},
|
||||
{"label":"Long press (ms)", "key":"longPress", "type":"I", "default":1500},
|
||||
{"label":"Long press repeat (ms)", "key":"longPressRepeat", "type":"I", "default":1000},
|
||||
]
|
||||
|
||||
|
||||
|
@ -40,8 +40,8 @@ void configServeIndex() {
|
||||
strcpy(configBlock.mqttDebugTopic, "IoT/MySwitch1/Debug");
|
||||
configBlock.debugMode = 0;
|
||||
configBlock.debounce = 300;
|
||||
configBlock.shortPress = 300;
|
||||
configBlock.longPress = 2000;
|
||||
configBlock.longPress = 1500;
|
||||
configBlock.longPressRepeat = 1000;
|
||||
}
|
||||
|
||||
if (! checkAuthentication()) {
|
||||
@ -260,12 +260,12 @@ void configServeIndex() {
|
||||
" </tr>"
|
||||
" <tr>"
|
||||
" <td>"
|
||||
" <label for\"shortPress\">Short press (ms)</label>"
|
||||
" <label for\"longPress\">Long press (ms)</label>"
|
||||
" </td><td>"
|
||||
" <input type=\"text\" name=\"shortPress\" id=\"shortPress\" ";
|
||||
" <input type=\"text\" name=\"longPress\" id=\"longPress\" ";
|
||||
|
||||
buffer += " value=\"";
|
||||
buffer += configBlock.shortPress;
|
||||
buffer += configBlock.longPress;
|
||||
buffer += "\"";
|
||||
|
||||
buffer +=
|
||||
@ -274,12 +274,12 @@ void configServeIndex() {
|
||||
" </tr>"
|
||||
" <tr>"
|
||||
" <td>"
|
||||
" <label for\"longPress\">Long press (ms)</label>"
|
||||
" <label for\"longPressRepeat\">Long press repeat (ms)</label>"
|
||||
" </td><td>"
|
||||
" <input type=\"text\" name=\"longPress\" id=\"longPress\" ";
|
||||
" <input type=\"text\" name=\"longPressRepeat\" id=\"longPressRepeat\" ";
|
||||
|
||||
buffer += " value=\"";
|
||||
buffer += configBlock.longPress;
|
||||
buffer += configBlock.longPressRepeat;
|
||||
buffer += "\"";
|
||||
|
||||
buffer +=
|
||||
@ -337,10 +337,10 @@ void configServeGetConfiguration() {
|
||||
configBlock.debugMode = atoi(arg.c_str());
|
||||
arg = webServer.arg("debounce");
|
||||
configBlock.debounce = atoi(arg.c_str());
|
||||
arg = webServer.arg("shortPress");
|
||||
configBlock.shortPress = atoi(arg.c_str());
|
||||
arg = webServer.arg("longPress");
|
||||
configBlock.longPress = atoi(arg.c_str());
|
||||
arg = webServer.arg("longPressRepeat");
|
||||
configBlock.longPressRepeat = atoi(arg.c_str());
|
||||
|
||||
configBlock.magic = MAGIC;
|
||||
|
||||
@ -417,14 +417,14 @@ void showConfiguration() {
|
||||
Serial.print(configBlock.debounce);
|
||||
Serial.println(">");
|
||||
|
||||
Serial.print("shortPress = <");
|
||||
Serial.print(configBlock.shortPress);
|
||||
Serial.println(">");
|
||||
|
||||
Serial.print("longPress = <");
|
||||
Serial.print(configBlock.longPress);
|
||||
Serial.println(">");
|
||||
|
||||
Serial.print("longPressRepeat = <");
|
||||
Serial.print(configBlock.longPressRepeat);
|
||||
Serial.println(">");
|
||||
|
||||
|
||||
Serial.println("---");
|
||||
}
|
@ -13,8 +13,8 @@ typedef struct {
|
||||
char mqttDebugTopic[64];
|
||||
uint32_t debugMode;
|
||||
uint32_t debounce;
|
||||
uint32_t shortPress;
|
||||
uint32_t longPress;
|
||||
uint32_t longPressRepeat;
|
||||
} tConfigBlock;
|
||||
|
||||
extern const uint32_t MAGIC;
|
||||
|
@ -16,10 +16,8 @@
|
||||
#define SWITCH_1 13
|
||||
#define SWITCH_2 12
|
||||
#define SWITCH_3 14
|
||||
#define SWITCH_4 16
|
||||
#define LED_1 2
|
||||
#define LED_2 0
|
||||
#define LED_3 4
|
||||
#define LED_4 5
|
||||
|
||||
#endif /* DEFINES_H_ */
|
||||
|
@ -28,122 +28,6 @@ WiFiClientSecure espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
|
||||
enum pressedState { IDLE, XPENDING, SHORT } ;
|
||||
|
||||
volatile pressedState pressed1 = IDLE;
|
||||
volatile pressedState pressed2 = IDLE;
|
||||
volatile pressedState pressed3 = IDLE;
|
||||
volatile pressedState pressed4 = IDLE;
|
||||
|
||||
|
||||
void change1() {
|
||||
static uint32_t lastEvent = 0;
|
||||
uint32_t currentEvent = micros();
|
||||
if (currentEvent > (lastEvent + configBlock.debounce)) {
|
||||
uint8_t buttonState = digitalRead(SWITCH_1);
|
||||
switch (pressed1) {
|
||||
case IDLE:
|
||||
digitalWrite(LED_1, HIGH);
|
||||
if (buttonState == LOW) {
|
||||
digitalWrite(LED_2, HIGH);
|
||||
pressed1 = XPENDING;
|
||||
}
|
||||
break;
|
||||
case XPENDING:
|
||||
digitalWrite(LED_1, LOW);
|
||||
if (buttonState == HIGH) {
|
||||
if (true || currentEvent > (lastEvent + (configBlock.shortPress * 1000))) {
|
||||
digitalWrite(LED_2, LOW);
|
||||
pressed1 = SHORT;
|
||||
}
|
||||
} else {
|
||||
pressed1 = IDLE;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
pressed1 = IDLE;
|
||||
}
|
||||
lastEvent = currentEvent;
|
||||
}
|
||||
}
|
||||
void change2() {
|
||||
static uint32_t lastEvent = 0;
|
||||
uint32_t currentEvent = micros();
|
||||
if (currentEvent > (lastEvent + configBlock.debounce)) {
|
||||
uint8_t buttonState = digitalRead(SWITCH_2);
|
||||
switch (pressed2) {
|
||||
case IDLE:
|
||||
if (buttonState == LOW) {
|
||||
pressed2 = XPENDING;
|
||||
}
|
||||
break;
|
||||
case XPENDING:
|
||||
if (buttonState == HIGH) {
|
||||
if (currentEvent > (lastEvent + configBlock.shortPress)) {
|
||||
pressed2 = SHORT;
|
||||
}
|
||||
} else {
|
||||
pressed2 = IDLE;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
pressed2 = IDLE;
|
||||
}
|
||||
lastEvent = currentEvent;
|
||||
}
|
||||
}
|
||||
void change3() {
|
||||
static uint32_t lastEvent = 0;
|
||||
uint32_t currentEvent = micros();
|
||||
if (currentEvent > (lastEvent + configBlock.debounce)) {
|
||||
uint8_t buttonState = digitalRead(SWITCH_3);
|
||||
switch (pressed3) {
|
||||
case IDLE:
|
||||
if (buttonState == LOW) {
|
||||
pressed3 = XPENDING;
|
||||
}
|
||||
break;
|
||||
case XPENDING:
|
||||
if (buttonState == HIGH) {
|
||||
if (currentEvent > (lastEvent + configBlock.shortPress)) {
|
||||
pressed3 = SHORT;
|
||||
}
|
||||
} else {
|
||||
pressed3 = IDLE;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
pressed3 = IDLE;
|
||||
}
|
||||
lastEvent = currentEvent;
|
||||
}
|
||||
}
|
||||
void change4() {
|
||||
static uint32_t lastEvent = 0;
|
||||
uint32_t currentEvent = micros();
|
||||
if (currentEvent > (lastEvent + configBlock.debounce)) {
|
||||
uint8_t buttonState = digitalRead(SWITCH_4);
|
||||
switch (pressed4) {
|
||||
case IDLE:
|
||||
if (buttonState == LOW) {
|
||||
pressed4 = XPENDING;
|
||||
}
|
||||
break;
|
||||
case XPENDING:
|
||||
if (buttonState == HIGH) {
|
||||
if (currentEvent > (lastEvent + configBlock.shortPress)) {
|
||||
pressed4 = SHORT;
|
||||
}
|
||||
} else {
|
||||
pressed4 = IDLE;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
pressed4 = IDLE;
|
||||
}
|
||||
lastEvent = currentEvent;
|
||||
}
|
||||
}
|
||||
|
||||
void setup_wifi() {
|
||||
delay(10);
|
||||
@ -222,23 +106,124 @@ bool mqtt_connect() {
|
||||
|
||||
void setupProduction() {
|
||||
pinMode(SWITCH_1, INPUT_PULLUP);
|
||||
attachInterrupt(SWITCH_1, change1, CHANGE);
|
||||
pinMode(LED_1, OUTPUT);
|
||||
pinMode(SWITCH_2, INPUT_PULLUP);
|
||||
attachInterrupt(SWITCH_2, change2, CHANGE);
|
||||
pinMode(LED_2, OUTPUT);
|
||||
pinMode(SWITCH_3, INPUT_PULLUP);
|
||||
attachInterrupt(SWITCH_3, change3, CHANGE);
|
||||
pinMode(LED_3, OUTPUT);
|
||||
pinMode(SWITCH_4, INPUT_PULLUP);
|
||||
attachInterrupt(SWITCH_4, change4, CHANGE);
|
||||
pinMode(LED_4, OUTPUT);
|
||||
|
||||
setup_wifi();
|
||||
client.setServer(configBlock.mqttBroker, configBlock.mqttPort);
|
||||
}
|
||||
|
||||
|
||||
static void sendMsg(uint8_t idx, char *msg) {
|
||||
char buf[BUF_SIZE];
|
||||
snprintf(buf, BUF_SIZE-1, "%s/%d", configBlock.mqttTopic, idx);
|
||||
client.publish(buf, msg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
enum tPressedState { psHIGH, psLOW, psACCEPTED_LOW, psLONG_START, psLONG_CONT, psLONG_CONT_SEND, psLONG_END, psSHORT, psINVALID };
|
||||
|
||||
typedef struct {
|
||||
uint8_t index;
|
||||
uint8_t buttonPin;
|
||||
tPressedState pressedState;
|
||||
tPressedState oldPressedState;
|
||||
uint32_t lastStateChange;
|
||||
} tButton;
|
||||
|
||||
static void buttonHandler(tButton *button) {
|
||||
uint32_t currentMicros = micros();
|
||||
uint8_t buttonState = digitalRead(button->buttonPin);
|
||||
|
||||
#ifdef DEBUG
|
||||
if (button->oldPressedState != button->pressedState) {
|
||||
Serial.print("Index ");
|
||||
Serial.print(button->index);
|
||||
Serial.print(", state changed from ");
|
||||
Serial.print(button->oldPressedState);
|
||||
Serial.print(" to ");
|
||||
Serial.print(button->pressedState);
|
||||
Serial.println();
|
||||
button->oldPressedState = button->pressedState;
|
||||
}
|
||||
#endif
|
||||
|
||||
switch (button->pressedState) {
|
||||
case psHIGH:
|
||||
if (buttonState == LOW) {
|
||||
button->pressedState = psLOW;
|
||||
button->lastStateChange = currentMicros;
|
||||
}
|
||||
break;
|
||||
case psLOW:
|
||||
if (buttonState == HIGH) {
|
||||
button->pressedState = psHIGH;
|
||||
button->lastStateChange = currentMicros;
|
||||
} else {
|
||||
if (currentMicros > (button->lastStateChange + configBlock.debounce)) {
|
||||
button->pressedState = psACCEPTED_LOW;
|
||||
button->lastStateChange = currentMicros;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case psACCEPTED_LOW:
|
||||
if (buttonState == HIGH) {
|
||||
button->pressedState = psSHORT;
|
||||
button->lastStateChange = currentMicros;
|
||||
}
|
||||
if (currentMicros > (button->lastStateChange + (configBlock.longPress * 1000))) {
|
||||
button->pressedState = psLONG_START;
|
||||
button->lastStateChange = currentMicros;
|
||||
}
|
||||
break;
|
||||
case psSHORT:
|
||||
sendMsg(button->index, "PRESS_SHORT");
|
||||
button->pressedState = psHIGH;
|
||||
button->lastStateChange = currentMicros;
|
||||
break;
|
||||
case psLONG_START:
|
||||
sendMsg(button->index, "PRESS_LONG_START");
|
||||
button->pressedState = psLONG_CONT;
|
||||
button->lastStateChange = currentMicros;
|
||||
break;
|
||||
case psLONG_CONT:
|
||||
if (buttonState == HIGH) {
|
||||
button->pressedState = psLONG_END;
|
||||
button->lastStateChange = currentMicros;
|
||||
}
|
||||
if (currentMicros > (button->lastStateChange + (configBlock.longPressRepeat * 1000))) {
|
||||
button->pressedState = psLONG_CONT_SEND;
|
||||
button->lastStateChange = currentMicros;
|
||||
}
|
||||
break;
|
||||
case psLONG_CONT_SEND:
|
||||
sendMsg(button->index, "PRESS_LONG_CONT");
|
||||
button->pressedState = psLONG_CONT;
|
||||
button->lastStateChange = currentMicros;
|
||||
break;
|
||||
case psLONG_END:
|
||||
sendMsg(button->index, "PRESS_LONG_END");
|
||||
button->pressedState = psHIGH;
|
||||
button->lastStateChange = currentMicros;
|
||||
break;
|
||||
default:
|
||||
button->pressedState = psHIGH;
|
||||
button->lastStateChange = currentMicros;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
tButton buttons[] = {
|
||||
{ 1, SWITCH_1, psHIGH, psINVALID, 0 },
|
||||
{ 2, SWITCH_2, psHIGH, psINVALID, 0 },
|
||||
{ 3, SWITCH_3, psHIGH, psINVALID, 0 },
|
||||
{ 0, 0, psINVALID, psINVALID, 0 } // END MARKER
|
||||
};
|
||||
|
||||
void loopProduction() {
|
||||
bool reconnected = mqtt_connect();
|
||||
static uint32_t reconnectTime = 0;
|
||||
@ -248,36 +233,19 @@ void loopProduction() {
|
||||
|
||||
uint32_t currentMillis = millis();
|
||||
|
||||
|
||||
static uint32_t lastUptimeMillis = 0;
|
||||
if (currentMillis > (lastUptimeMillis + 1000)) {
|
||||
lastUptimeMillis = currentMillis;
|
||||
uptime++;
|
||||
}
|
||||
|
||||
if (pressed1 == SHORT) {
|
||||
char buf[BUF_SIZE];
|
||||
snprintf(buf, BUF_SIZE-1, "%s/1", configBlock.mqttTopic);
|
||||
client.publish(buf, "PRESS_SHORT");
|
||||
pressed1 = IDLE;
|
||||
}
|
||||
if (pressed2 == SHORT) {
|
||||
char buf[BUF_SIZE];
|
||||
snprintf(buf, BUF_SIZE-1, "%s/2", configBlock.mqttTopic);
|
||||
client.publish(buf, "PRESS_SHORT");
|
||||
pressed2 = IDLE;
|
||||
}
|
||||
if (pressed3 == SHORT) {
|
||||
char buf[BUF_SIZE];
|
||||
snprintf(buf, BUF_SIZE-1, "%s/3", configBlock.mqttTopic);
|
||||
client.publish(buf, "PRESS_SHORT");
|
||||
pressed3 = IDLE;
|
||||
}
|
||||
if (pressed4 == SHORT) {
|
||||
char buf[BUF_SIZE];
|
||||
snprintf(buf, BUF_SIZE-1, "%s/4", configBlock.mqttTopic);
|
||||
client.publish(buf, "PRESS_SHORT");
|
||||
pressed4 = IDLE;
|
||||
static uint8_t i = 0;
|
||||
tButton *button = &(buttons[i]);
|
||||
i++;
|
||||
if (button->index != 0) {
|
||||
buttonHandler(button);
|
||||
} else {
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user