Skip to content

Commit

Permalink
remote timer control
Browse files Browse the repository at this point in the history
  • Loading branch information
vascofazza committed Dec 23, 2020
1 parent f6c0251 commit 5e7e7fb
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 14 deletions.
3 changes: 2 additions & 1 deletion nixie_firmware/src/cloxie_firmware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ void setup()
#endif
setup_configuration();
initWiFi();
setup_wifi(update_config_callback);

sensor_driver = new SensorDriver();
tube_driver = new TubeDriver(sensor_driver);
led_driver = new LedDriver(tube_driver, sensor_driver, NUM_LEDS, clock_patterns, ARRAY_SIZE(clock_patterns));
clock_driver = new ClockDriver(tube_driver);

setup_wifi(clock_driver, update_config_callback);

ota_handler.Every(GHOTA_INTERVAL, check_for_updates);
ota_handler.Start();

Expand Down
6 changes: 4 additions & 2 deletions nixie_firmware/src/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Config config{
7,
true,
true,
CATHODE_POISONING_TRIGGER_TIME
};

void printParams()
Expand All @@ -27,8 +28,6 @@ void printParams()
DEBUG_PRINTLN(config.h24);
DEBUG_PRINT(F("\tC°/F°: "));
DEBUG_PRINTLN(config.celsius);
//DEBUG_PRINT(F("\tGoogle Token: "));
//DEBUG_PRINTLN(config.google_token);
DEBUG_PRINT(F("\tAdaptive Brightness: "));
DEBUG_PRINTLN(config.adaptive_brightness);
DEBUG_PRINT(F("\tBrightness Offset: "));
Expand All @@ -47,6 +46,8 @@ void printParams()
DEBUG_PRINTLN(config.leds);
DEBUG_PRINT(F("\tLed Configuration: "));
DEBUG_PRINTLN(config.led_configuration);
DEBUG_PRINT(F("\tDepoisoning interval: "));
DEBUG_PRINTLN(config.depoisoning);
}

void check_params()
Expand All @@ -59,6 +60,7 @@ void check_params()
config.blink_mode = config.blink_mode > 3 || config.blink_mode < 0 ? 0 : config.blink_mode;
config.sleep_hour = config.sleep_hour > 23 || config.sleep_hour < 0 ? 0 : config.sleep_hour;
config.wake_hour = config.wake_hour > 23 || config.wake_hour < 0 ? 7 : config.wake_hour;
config.depoisoning = config.depoisoning < 60000 ? CATHODE_POISONING_TRIGGER_TIME : config.depoisoning;
}

void setup_configuration()
Expand Down
3 changes: 2 additions & 1 deletion nixie_firmware/src/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <Arduino.h>
#include <EEPROM.h>

#define FIRMWARE_VERSION PSTR("1.1.1")
#define FIRMWARE_VERSION PSTR("1.1.2")

//#define DEBUG

Expand Down Expand Up @@ -88,6 +88,7 @@ struct Config
int wake_hour;
bool termometer;
bool date;
unsigned int depoisoning;
};

extern Config config;
Expand Down
2 changes: 1 addition & 1 deletion nixie_firmware/src/drivers/tube_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ TubeDriver::TubeDriver(SensorDriver *sensor_driver)
pinMode(SHF_CLOCK, OUTPUT);
pinMode(SHF_DATA, OUTPUT);
pinMode(SHUTDOWN_PIN, INPUT);
cathode_poisoning_cycle.Every(CATHODE_POISONING_TRIGGER_TIME, std::bind(&TubeDriver::cathode_poisoning_prevention, this, CATHODE_POISONING_PREVENTION_TIME));
cathode_poisoning_cycle.Every(config.depoisoning, std::bind(&TubeDriver::cathode_poisoning_prevention, this, CATHODE_POISONING_PREVENTION_TIME));

run_test();
set_tubes(0, 0, 0, 0, 0, 0);
Expand Down
52 changes: 44 additions & 8 deletions nixie_firmware/src/network/wifi.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "wifi.hpp"

//WiFiManagerParameter *google_token;
WiFiManagerParameter *timezone_field;
WiFiManagerParameter *h24_field;
WiFiManagerParameter *blink_field;
Expand All @@ -15,11 +14,15 @@ WiFiManagerParameter *sleep_hour;
WiFiManagerParameter *wake_hour;
WiFiManagerParameter *termometer;
WiFiManagerParameter *date;
WiFiManagerParameter *depoisoning;

void (*custom_callback)(void) = nullptr;

void setup_wifi(void (*callback)(void))
static ClockDriver *_clock_driver = nullptr;

void setup_wifi(ClockDriver *clock, void (*callback)(void))
{
_clock_driver = clock;
wifiManager.WiFiManagerInit();
wifiManager.setFirmwareVersion(FIRMWARE_VERSION);
if (callback != nullptr)
Expand Down Expand Up @@ -77,8 +80,8 @@ void setup_wifi(void (*callback)(void))
shutdown_delay = new WiFiManagerParameter(F("shutdown_delay"), F("shutdown_delay"), String(config.shutdown_delay).c_str(), 7);
wifiManager.addParameter(shutdown_delay);

//google_token = new WiFiManagerParameter(F("google_token"), F("google_token"), config.google_token, 40);
//wifiManager.addParameter(google_token);
depoisoning = new WiFiManagerParameter(F("depoisoning_field"), F("depoisoning_field"), String(config.depoisoning).c_str(), 7);
wifiManager.addParameter(depoisoning);

wifiManager.setSaveParamsCallback(saveParamsCallback);
wifiManager.setGetParameterCallback(getParamsCallback);
Expand Down Expand Up @@ -134,8 +137,6 @@ void wifi_loop()
DEBUG_PRINTLN(F("WiFi connection issue, resetting module."));
resetWiFi();
activeDelay(1000);
setup_wifi(nullptr);
activeDelay(1000);

if (WiFi.waitForConnectResult() == WL_CONNECTED && hasIPaddr())
{
Expand Down Expand Up @@ -163,6 +164,9 @@ void wifi_loop()
DEBUG_PRINTLN(F("Starting Portal"));
wifiManager.startWebPortal();
setup_ota_webserver(wifiManager.server.get());
wifiManager.server.get()->on("/timer_start", std::bind(&start_timer, _clock_driver, std::placeholders::_1));
wifiManager.server.get()->on("/timer_pause", std::bind(pause_timer, _clock_driver, std::placeholders::_1));
wifiManager.server.get()->on("/timer_stop", std::bind(stop_timer, _clock_driver, std::placeholders::_1));
}
MDNS.update();
}
Expand All @@ -185,6 +189,7 @@ void getParamsCallback(AsyncWebServerRequest *request)
root[F("wake_hour")] = config.wake_hour;
root[F("termometer")] = (int)config.termometer;
root[F("date")] = (int)config.date;
root[F("depoisoning_field")] = config.depoisoning;
root[F("uptime")] = wifiManager.getUpTime();
root[F("fw_ver")] = String(FIRMWARE_VERSION);

Expand All @@ -194,8 +199,6 @@ void getParamsCallback(AsyncWebServerRequest *request)

void saveParamsCallback(AsyncWebServerRequest *request)
{
//DEBUG_PRINT(F("PARAM google_token = "));
//DEBUG_PRINTLN(google_token->getValue());
DEBUG_PRINT(F("PARAM timezone_field = "));
DEBUG_PRINTLN(getParam(request, F("timezone_field")));
DEBUG_PRINT(F("PARAM h24_field = "));
Expand Down Expand Up @@ -224,6 +227,8 @@ void saveParamsCallback(AsyncWebServerRequest *request)
DEBUG_PRINTLN(getParam(request, F("termometer_field")));
DEBUG_PRINT(F("PARAM date_field = "));
DEBUG_PRINTLN(getParam(request, F("date_field")));
DEBUG_PRINT(F("PARAM depoisoning_field = "));
DEBUG_PRINTLN(depoisoning->getValue());

//strcpy(config.google_token, google_token->getValue());
config.timezone = getParam(request, F("timezone_field")).toInt();
Expand All @@ -240,6 +245,7 @@ void saveParamsCallback(AsyncWebServerRequest *request)
config.wake_hour = String(wake_hour->getValue()).toInt();
config.termometer = (bool)getParam(request, F("termometer_field")).toInt();
config.date = (bool)getParam(request, F("date_field")).toInt();
config.depoisoning = String(depoisoning->getValue()).toInt();
save_configuration();
if (custom_callback != nullptr)
{
Expand Down Expand Up @@ -309,4 +315,34 @@ bool hasIPaddr()
}
}
return configured;
}

void start_timer(ClockDriver *clock, AsyncWebServerRequest *request)
{
auto arg_name = "interval";
if (request->hasArg(arg_name))
{
String value = request->arg(arg_name);
clock->start_timer(value.toInt() * 1000);
}

AsyncWebServerResponse *response = request->beginResponse(200, PSTR("text/plain"), PSTR("OK"));
response->addHeader(PSTR("Connection"), PSTR("close"));
request->send(response);
}

void stop_timer(ClockDriver *clock, AsyncWebServerRequest *request)
{
clock->reset_timer();
AsyncWebServerResponse *response = request->beginResponse(200, PSTR("text/plain"), PSTR("OK"));
response->addHeader(PSTR("Connection"), PSTR("close"));
request->send(response);
}

void pause_timer(ClockDriver *clock, AsyncWebServerRequest *request)
{
clock->stop_timer();
AsyncWebServerResponse *response = request->beginResponse(200, PSTR("text/plain"), PSTR("OK"));
response->addHeader(PSTR("Connection"), PSTR("close"));
request->send(response);
}
9 changes: 8 additions & 1 deletion nixie_firmware/src/network/wifi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "AsyncJson.h"
#include "ArduinoJson.h"
#include "ota_webserver.hpp"
#include "../drivers/clock.hpp"
#include "../configuration.hpp"
#include "../utils.hpp"

Expand All @@ -19,7 +20,7 @@ void getParamsCallback(AsyncWebServerRequest*);

void saveParamsCallback(AsyncWebServerRequest*);

void setup_wifi(void (*)());
void setup_wifi(ClockDriver*, void (*)());

void wifi_loop();

Expand All @@ -33,4 +34,10 @@ bool hasIPaddr();

void postSaveFunction();

void start_timer(ClockDriver*, AsyncWebServerRequest*);

void pause_timer(ClockDriver*, AsyncWebServerRequest*);

void stop_timer(ClockDriver*, AsyncWebServerRequest*);

#endif

0 comments on commit 5e7e7fb

Please sign in to comment.