From 917d1df964b63ea50db33f38143aba2dd8540c31 Mon Sep 17 00:00:00 2001
From: Eric Duminil <eric.duminil@gmail.com>
Date: Tue, 20 Apr 2021 10:54:10 +0200
Subject: [PATCH] Force F strings doc for commands

---
 ampel-firmware/co2_sensor.cpp     | 13 +++++++------
 ampel-firmware/csv_writer.cpp     |  6 +++---
 ampel-firmware/led_effects.cpp    |  2 +-
 ampel-firmware/mqtt.cpp           |  4 ++--
 ampel-firmware/sensor_console.cpp |  7 ++++---
 ampel-firmware/sensor_console.h   |  4 ++--
 ampel-firmware/util.cpp           |  6 +++---
 7 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/ampel-firmware/co2_sensor.cpp b/ampel-firmware/co2_sensor.cpp
index 5155cf0..39c90ba 100644
--- a/ampel-firmware/co2_sensor.cpp
+++ b/ampel-firmware/co2_sensor.cpp
@@ -71,14 +71,15 @@ namespace sensor {
     Serial.print(F("Auto-calibration is "));
     Serial.println(config::auto_calibrate_sensor ? "ON." : "OFF.");
 
-    sensor_console::defineIntCommand("co2", setCO2forDebugging, " 1500 (Sets co2 level, for debugging purposes)");
-    sensor_console::defineIntCommand("timer", setTimer, " 30 (Sets measurement interval, in s)");
-    sensor_console::defineCommand("calibrate", startCalibrationProcess, " (Starts calibration process)");
+    sensor_console::defineIntCommand("co2", setCO2forDebugging, F(" 1500 (Sets co2 level, for debugging purposes)"));
+    sensor_console::defineIntCommand("timer", setTimer, F(" 30 (Sets measurement interval, in s)"));
+    sensor_console::defineCommand("calibrate", startCalibrationProcess, F(" (Starts calibration process)"));
     sensor_console::defineIntCommand("calibrate", calibrateSensorToSpecificPPM,
-        " 600 (Starts calibration process, to given ppm)");
+        F(" 600 (Starts calibration process, to given ppm)"));
     sensor_console::defineIntCommand("calibrate!", calibrateSensorRightNow,
-        " 600 (Calibrates right now, to given ppm)");
-    sensor_console::defineIntCommand("auto_calibrate", setAutoCalibration, " 0/1 (Disables/enables autocalibration)");
+        F(" 600 (Calibrates right now, to given ppm)"));
+    sensor_console::defineIntCommand("auto_calibrate", setAutoCalibration,
+        F(" 0/1 (Disables/enables autocalibration)"));
   }
 
   //NOTE: should timer deviation be used to adjust measurement_timestep?
diff --git a/ampel-firmware/csv_writer.cpp b/ampel-firmware/csv_writer.cpp
index 96042ab..199d9f3 100644
--- a/ampel-firmware/csv_writer.cpp
+++ b/ampel-firmware/csv_writer.cpp
@@ -115,9 +115,9 @@ namespace csv_writer {
     showFilesystemContent();
     Serial.println();
 
-    sensor_console::defineIntCommand("csv", setCSVinterval, " 60 (Sets CSV writing interval, in s)");
-    sensor_console::defineCommand("format_filesystem", formatFilesystem, " (Deletes the whole filesystem)");
-    sensor_console::defineCommand("show_csv", showCSVContent, " (Displays the complete CSV file on Serial)");
+    sensor_console::defineIntCommand("csv", setCSVinterval, F(" 60 (Sets CSV writing interval, in s)"));
+    sensor_console::defineCommand("format_filesystem", formatFilesystem, F(" (Deletes the whole filesystem)"));
+    sensor_console::defineCommand("show_csv", showCSVContent, F(" (Displays the complete CSV file on Serial)"));
   }
 
   File openOrCreate() {
diff --git a/ampel-firmware/led_effects.cpp b/ampel-firmware/led_effects.cpp
index 54db0ac..ee0fccd 100644
--- a/ampel-firmware/led_effects.cpp
+++ b/ampel-firmware/led_effects.cpp
@@ -74,7 +74,7 @@ namespace led_effects {
     pixels.begin();
     pixels.setBrightness(config::max_brightness);
     LEDsOff();
-    sensor_console::defineCommand("night_mode", toggleNightMode, " (Toggles night mode on/off)");
+    sensor_console::defineCommand("night_mode", toggleNightMode, F(" (Toggles night mode on/off)"));
   }
 
   void toggleNightMode() {
diff --git a/ampel-firmware/mqtt.cpp b/ampel-firmware/mqtt.cpp
index 1037aa5..fda7527 100644
--- a/ampel-firmware/mqtt.cpp
+++ b/ampel-firmware/mqtt.cpp
@@ -36,9 +36,9 @@ namespace mqtt {
     // mqttClient.setSocketTimeout(config::mqtt_timeout); //NOTE: somehow doesn't seem to have any effect on connect()
     mqttClient.setServer(config::mqtt_server, config::mqtt_port);
 
-    sensor_console::defineIntCommand("mqtt", setMQTTinterval, " 60 (Sets MQTT sending interval, in s)");
+    sensor_console::defineIntCommand("mqtt", setMQTTinterval, F(" 60 (Sets MQTT sending interval, in s)"));
     sensor_console::defineCommand("local_ip", sendInfoAboutLocalNetwork,
-        " (Sends local IP and SSID via MQTT. Can be useful to find sensor)");
+        F(" (Sends local IP and SSID via MQTT. Can be useful to find sensor)"));
   }
 
   void publish(const char *timestamp, int16_t co2, float temperature, float humidity) {
diff --git a/ampel-firmware/sensor_console.cpp b/ampel-firmware/sensor_console.cpp
index 92cca53..fd29d4a 100644
--- a/ampel-firmware/sensor_console.cpp
+++ b/ampel-firmware/sensor_console.cpp
@@ -19,8 +19,8 @@ namespace sensor_console {
   Command commands[MAX_COMMANDS];
 
   //NOTE: Probably possible to DRY (with templates?)
-  //TODO: Allow F-Strings
-  void defineCommand(const char *name, void (*function)(void), const char *doc) {
+  void defineCommand(const char *name, void (*function)(void), const __FlashStringHelper *doc_fstring) {
+    const char *doc = (const char PROGMEM*) doc_fstring;
     if (commands_count < MAX_COMMANDS) {
       commands[commands_count].name = name;
       commands[commands_count].voidFunction = function;
@@ -32,7 +32,8 @@ namespace sensor_console {
     }
   }
 
-  void defineIntCommand(const char *name, void (*function)(int32_t), const char *doc) {
+  void defineIntCommand(const char *name, void (*function)(int32_t), const __FlashStringHelper *doc_fstring) {
+    const char *doc = (const char PROGMEM*) doc_fstring;
     if (commands_count < MAX_COMMANDS) {
       commands[commands_count].name = name;
       commands[commands_count].intFunction = function;
diff --git a/ampel-firmware/sensor_console.h b/ampel-firmware/sensor_console.h
index 0434d48..e152414 100644
--- a/ampel-firmware/sensor_console.h
+++ b/ampel-firmware/sensor_console.h
@@ -10,8 +10,8 @@
 namespace sensor_console {
   void processSerialInput(const byte in_byte);
   void runCommand(const char *command);
-  void defineIntCommand(const char *command, void (*function)(int32_t), const char *doc);
-  void defineCommand(const char *command, void (*function)(void), const char *doc);
+  void defineIntCommand(const char *command, void (*function)(int32_t), const __FlashStringHelper *ifsh);
+  void defineCommand(const char *command, void (*function)(void), const __FlashStringHelper *ifsh);
 }
 
 #endif
diff --git a/ampel-firmware/util.cpp b/ampel-firmware/util.cpp
index 5ec1556..cc2967b 100644
--- a/ampel-firmware/util.cpp
+++ b/ampel-firmware/util.cpp
@@ -67,11 +67,11 @@ void Ampel::showFreeSpace() {
 
 Ampel::Ampel() :
     board(current_board), sensorId("ESP" + macToID()), max_loop_duration(0) {
-  sensor_console::defineIntCommand("set_time", ntp::setLocalTime, " 1618829570 (Sets time to the given UNIX time)");
-  sensor_console::defineCommand("free", Ampel::showFreeSpace, " (Displays available heap space)");
+  sensor_console::defineIntCommand("set_time", ntp::setLocalTime, F(" 1618829570 (Sets time to the given UNIX time)"));
+  sensor_console::defineCommand("free", Ampel::showFreeSpace, F(" (Displays available heap space)"));
   sensor_console::defineCommand("reset", []() {
     ESP.restart();
-  }, " (Restarts the sensor)");
+  }, F(" (Restarts the sensor)"));
 }
 
 Ampel ampel;
-- 
GitLab