From 2facb805a55d6bdaf53f84c19b2d02d23c4270ff Mon Sep 17 00:00:00 2001
From: Jan Grewe <jan@faked.org>
Date: Wed, 19 Jun 2024 16:03:48 +0200
Subject: [PATCH] more logging, save region to EEPROM

---
 src/button.cpp   | 10 ++++++----
 src/ir.cpp       |  8 +++++++-
 src/led.cpp      |  2 ++
 src/main.cpp     |  2 ++
 src/ota.cpp      | 17 +++++++++--------
 src/settings.cpp | 25 +++++++++++++++++++++++++
 src/settings.h   |  8 ++++++++
 7 files changed, 59 insertions(+), 13 deletions(-)
 create mode 100644 src/settings.cpp
 create mode 100644 src/settings.h

diff --git a/src/button.cpp b/src/button.cpp
index 735388d..da05e54 100644
--- a/src/button.cpp
+++ b/src/button.cpp
@@ -5,6 +5,7 @@
 #include "button.h"
 #include "utils.h"
 #include "led.h"
+#include "settings.h"
 
 InterruptButton button(PIN_BUTTON, HIGH);
 
@@ -12,13 +13,13 @@ void handleShortButton()
 {
   if (!sendingEnabled)
   {
-    logLine("Starting to send. (Region: "+ String(region)+")");
+    logLine("BUTTON: Starting to send. (Region: "+ String(region)+")");
     blink(1);
     digitalWrite(PIN_LED, HIGH);
   }
   else
   {
-    logLine("Restarting sending.");
+    logLine("BUTTON: Restarting sending.");
     blink(1);
     digitalWrite(PIN_LED, HIGH);
     cancelSending = true;
@@ -36,7 +37,8 @@ void handleDoubleButton() {
     region = EU;
     blink(1);
   }
-  logLine("Switching Region to: " + String(region));
+  logLine("BUTTON: Switching Region to: " + String(region));
+  saveSettings();
   
   if (sendingEnabled) {
     digitalWrite(PIN_LED, HIGH);
@@ -48,7 +50,7 @@ void handleLongButton()
 {
   if (sendingEnabled)
   {
-    logLine("Stopping sending.");
+    logLine("BUTTON: Stopping sending.");
     sendingEnabled = false;
     cancelSending = true;
     blink(3);
diff --git a/src/ir.cpp b/src/ir.cpp
index 298397d..d24e1db 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -3,6 +3,7 @@
 #include <IRsend.h>
 
 #include "config.h"
+#include "utils.h"
 #include "WORLD_IR_CODES.h"
 
 #define MAX_WAIT_TIME 65535 // tens of us (ie: 655.350ms)
@@ -82,9 +83,12 @@ void sendAllCodes(uint8_t region)
         num_codes = num_NAcodes;
     }
 
+    logLine("IR: Sending " + String(num_codes) + " codes for region " + String(region) + ".");
+
     // for every POWER code in our collection
     for (i = 0; i < num_codes; i++)
     {
+        logLine("IR: Sending code: " + String(i + 1));
 
         // make sure all loop() handlers are still ran while sending
         callHandlers();
@@ -132,7 +136,6 @@ void sendAllCodes(uint8_t region)
 
         // Send Code with library
         irsend.sendRaw(rawData, (numpairs * 2), freq);
-        Serial.print("\n");
         yield();
         // Flush remaining bits, so that next code starts
         // with a fresh set of 8 bits.
@@ -144,11 +147,14 @@ void sendAllCodes(uint8_t region)
         if (cancelSending)
         {
             cancelSending = false;
+            logLine("IR: Cancelling sending codes.");
             break; // exit the POWER code "for" loop
         }
 
     } // end of POWER code for loop
 
+    logLine("IR: Finished sending codes.");
+
 } // end of sendAllCodes
 
 void setupIR()
diff --git a/src/led.cpp b/src/led.cpp
index b8f02ca..cad90a1 100644
--- a/src/led.cpp
+++ b/src/led.cpp
@@ -1,9 +1,11 @@
 #include <Arduino.h>
 #include "led.h"
 #include "config.h"
+#include "utils.h"
 
 void blink(int count)
 {
+  logLine("LED: Blinking " +String(count)+ " times.");
   digitalWrite(PIN_LED, LOW);
   delay(200);
   for (int i = 0; i < count; i++)
diff --git a/src/main.cpp b/src/main.cpp
index b7dbcb9..53fd393 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -8,6 +8,7 @@
 #include "ota.h"
 #include "network.h"
 #include "ir.h"
+#include "settings.h"
 
 bool sendingEnabled = false;
 bool cancelSending = false;
@@ -17,6 +18,7 @@ void setup()
 {
   Serial.begin(115200);
   Serial.println();
+  loadSettings();
   setupWifi();
   setupOTA();
   setupTelnet();
diff --git a/src/ota.cpp b/src/ota.cpp
index ec34f54..555f174 100644
--- a/src/ota.cpp
+++ b/src/ota.cpp
@@ -3,6 +3,7 @@
 #include <ESPmDNS.h>
 
 #include "config.h"
+#include "utils.h"
 
 void setupOTA()
 {
@@ -17,24 +18,24 @@ void setupOTA()
       }
 
       // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
-      Serial.println("Start updating " + type); })
+      logLine("Start updating " + type); })
       .onEnd([]()
-             { Serial.println("\nEnd"); })
+             { logLine("End"); })
       .onProgress([](unsigned int progress, unsigned int total)
-                  { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); })
+                  { logLine("OTA: " + (String)ceil(progress / (total / 100)) + "%"); })
       .onError([](ota_error_t error)
                {
       Serial.printf("Error[%u]: ", error);
       if (error == OTA_AUTH_ERROR) {
-        Serial.println("Auth Failed");
+        logLine("Auth Failed");
       } else if (error == OTA_BEGIN_ERROR) {
-        Serial.println("Begin Failed");
+        logLine("Begin Failed");
       } else if (error == OTA_CONNECT_ERROR) {
-        Serial.println("Connect Failed");
+        logLine("Connect Failed");
       } else if (error == OTA_RECEIVE_ERROR) {
-        Serial.println("Receive Failed");
+        logLine("Receive Failed");
       } else if (error == OTA_END_ERROR) {
-        Serial.println("End Failed");
+        logLine("End Failed");
       } });
   ArduinoOTA.setHostname(HOSTNAME);
   ArduinoOTA.begin();
diff --git a/src/settings.cpp b/src/settings.cpp
new file mode 100644
index 0000000..ef6a3c7
--- /dev/null
+++ b/src/settings.cpp
@@ -0,0 +1,25 @@
+#include <Arduino.h>
+#include <Preferences.h>
+
+Preferences preferences;
+
+extern uint8_t region;
+
+void loadSettings() {
+  preferences.begin("settings", true); // true = readonly
+  region = (uint8_t)preferences.getUInt("region", 0);
+  preferences.end();
+}
+
+void saveSettings() {
+  preferences.begin("settings", false);
+  preferences.putUInt("region", region);
+  preferences.end();
+}
+
+void clearSettings() {
+  preferences.begin("settings", false);
+  preferences.clear();
+  preferences.end();
+  ESP.restart();
+}
diff --git a/src/settings.h b/src/settings.h
new file mode 100644
index 0000000..6b00907
--- /dev/null
+++ b/src/settings.h
@@ -0,0 +1,8 @@
+#include <Arduino.h>
+#include <Preferences.h>
+
+extern uint8_t region;
+
+void loadSettings();
+void saveSettings();
+void clearSettings();
-- 
GitLab