|
| 1 | +#include"thing/FireThing.h" |
| 2 | +#include"Arduino.h" |
| 3 | +#include"FS.h" |
| 4 | + |
| 5 | +namespacething { |
| 6 | +namespace { |
| 7 | + |
| 8 | +ConfigkDefaultConfig = { |
| 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_([](constchar*) {}) {} |
| 25 | + |
| 26 | +boolFireThing::Setup() { |
| 27 | + Config config; |
| 28 | +if (!ReadConfigFromStorage(&config)) { |
| 29 | +debug_("Failed to read config from storage."); |
| 30 | +returnfalse; |
| 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 | +voidFireThing::Loop() { |
| 50 | + wifi_.Loop(); |
| 51 | + portal_.Loop(); |
| 52 | + transcriber_.Loop(); |
| 53 | +} |
| 54 | + |
| 55 | +boolFireThing::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 | +returntrue; |
| 62 | + } |
| 63 | +debug_("Failed to Connect."); |
| 64 | +returnfalse; |
| 65 | +} |
| 66 | + |
| 67 | +voidFireThing::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 | +boolFireThing::ReadConfigFromStorage(Config* config) { |
| 77 | +if (!SPIFFS.begin()) { |
| 78 | +debug_("Failed to mount FS."); |
| 79 | +returnfalse; |
| 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 | +returnfalse; |
| 91 | + } |
| 92 | + config->ReadFromJson(cfg.readString().c_str()); |
| 93 | +debug_("Config read from disk."); |
| 94 | + } |
| 95 | + |
| 96 | + SPIFFS.end(); |
| 97 | +returntrue; |
| 98 | +} |
| 99 | + |
| 100 | +boolFireThing::WriteConfigToStorage(const Config& config) { |
| 101 | +if (!SPIFFS.begin()) { |
| 102 | +debug_("Failed to mount FS."); |
| 103 | +returnfalse; |
| 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 | +returnfalse; |
| 111 | + } |
| 112 | + config.SerializeToJson(&cfg, [](int){}); |
| 113 | + |
| 114 | + SPIFFS.end(); |
| 115 | +returntrue; |
| 116 | +} |
| 117 | + |
| 118 | +voidFireThing::SetDebugHandler(std::function<void(constchar* message)> debug) { |
| 119 | + debug_ = debug; |
| 120 | + wifi_.SetDebugHandler(debug); |
| 121 | + portal_.SetDebugHandler(debug); |
| 122 | + transcriber_.SetDebugHandler(debug); |
| 123 | +} |
| 124 | + |
| 125 | +}// namespace thing |