|
| 1 | +#include "thing/FireThing.h" |
| 2 | +#include "Arduino.h" |
| 3 | +#include "FS.h" |
| 4 | + |
| 5 | +namespace thing { |
| 6 | +namespace { |
| 7 | + |
| 8 | +Config kDefaultConfig = { |
| 9 | + "", // firebase host |
| 10 | + "", // firebase auth |
| 11 | + "/fthing", // path in firebase |
| 12 | + "", // wifi ssid |
| 13 | + "", // wifi key |
| 14 | + 0.1, // analog activation threshold |
| 15 | + D1, // digital in |
| 16 | + BUILTIN_LED, // digital out |
| 17 | + A0, // analog in |
| 18 | + D1, // analog out |
| 19 | + D0, // config mode button |
| 20 | +}; |
| 21 | + |
| 22 | +} // namespace |
| 23 | + |
| 24 | +FireThing::FireThing() : debug_([](const char*) {}) {} |
| 25 | + |
| 26 | +bool FireThing::Setup() { |
| 27 | + Config config; |
| 28 | + if (!ReadConfigFromStorage(&config)) { |
| 29 | + debug_("Failed to read config from storage."); |
| 30 | + return false; |
| 31 | + } |
| 32 | + SetPinModes(config); |
| 33 | + |
| 34 | + if (digitalRead(config.pins.config_mode_button) || !ConnectToWiFi(config)) { |
| 35 | + wifi_.StartAP(); |
| 36 | + } |
| 37 | + |
| 38 | + portal_.NotifyOnUpdate([this](const Config& config) { |
| 39 | + if (!WriteConfigToStorage(config)) { |
| 40 | + debug_("Failed to write config to storage."); |
| 41 | + } |
| 42 | + SetPinModes(config); |
| 43 | + transcriber_.UpdateConfig(config); |
| 44 | + ConnectToWiFi(config); |
| 45 | + }); |
| 46 | + portal_.Start(config); |
| 47 | +} |
| 48 | + |
| 49 | +void FireThing::Loop() { |
| 50 | + wifi_.Loop(); |
| 51 | + portal_.Loop(); |
| 52 | + transcriber_.Loop(); |
| 53 | +} |
| 54 | + |
| 55 | +bool FireThing::ConnectToWiFi(const Config& config) { |
| 56 | + debug_("Connecting to wifi:"); |
| 57 | + debug_(config.wifi_ssid.c_str()); |
| 58 | + debug_(config.wifi_key.c_str()); |
| 59 | + if (wifi_.Connect(config.wifi_ssid, config.wifi_key)) { |
| 60 | + debug_("Connected"); |
| 61 | + return true; |
| 62 | + } |
| 63 | + debug_("Failed to Connect."); |
| 64 | + return false; |
| 65 | +} |
| 66 | + |
| 67 | +void FireThing::SetPinModes(const Config& config) { |
| 68 | + pinMode(config.pins.digital_in, INPUT); |
| 69 | + pinMode(config.pins.digital_out, OUTPUT); |
| 70 | + pinMode(config.pins.analog_in, INPUT); |
| 71 | + pinMode(config.pins.analog_out, OUTPUT); |
| 72 | + |
| 73 | + pinMode(config.pins.config_mode_button, INPUT); |
| 74 | +} |
| 75 | + |
| 76 | +bool FireThing::ReadConfigFromStorage(Config* config) { |
| 77 | + if (!SPIFFS.begin()) { |
| 78 | + debug_("Failed to mount FS."); |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + if (!SPIFFS.exists("fthing.cfg")) { |
| 83 | + debug_("Config not found, using default."); |
| 84 | + *config = kDefaultConfig; |
| 85 | + } else { |
| 86 | + File cfg = SPIFFS.open("fthing.cfg", "r"); |
| 87 | + if (!cfg) { |
| 88 | + debug_("Failed to open config for read"); |
| 89 | + SPIFFS.end(); |
| 90 | + return false; |
| 91 | + } |
| 92 | + config->ReadFromJson(cfg.readString().c_str()); |
| 93 | + debug_("Config read from disk."); |
| 94 | + } |
| 95 | + |
| 96 | + SPIFFS.end(); |
| 97 | + return true; |
| 98 | +} |
| 99 | + |
| 100 | +bool FireThing::WriteConfigToStorage(const Config& config) { |
| 101 | + if (!SPIFFS.begin()) { |
| 102 | + debug_("Failed to mount FS."); |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + File cfg = SPIFFS.open("fthing.cfg", "w"); |
| 107 | + if (!cfg) { |
| 108 | + debug_("Failed to open config for write"); |
| 109 | + SPIFFS.end(); |
| 110 | + return false; |
| 111 | + } |
| 112 | + config.SerializeToJson(&cfg, [](int){}); |
| 113 | + |
| 114 | + SPIFFS.end(); |
| 115 | + return true; |
| 116 | +} |
| 117 | + |
| 118 | +void FireThing::SetDebugHandler(std::function<void(const char* message)> debug) { |
| 119 | + debug_ = debug; |
| 120 | + wifi_.SetDebugHandler(debug); |
| 121 | + portal_.SetDebugHandler(debug); |
| 122 | + transcriber_.SetDebugHandler(debug); |
| 123 | +} |
| 124 | + |
| 125 | +} // namespace thing |
0 commit comments