Ver Fonte

DK-56 make all important constants configurable

Bence Balint há 2 anos atrás
pai
commit
f4b3f47498

+ 2 - 12
components/dk/include/tasks/event_manager.h

@@ -9,17 +9,7 @@
 #include <kbf/rtos.h>
 #include <kbf/task.h>
 
-#define EVENT_QUEUE_SIZE 10  // TODO use Kconfig
-
-/** @brief default milliseconds to wait before sending a NOOP */
-#define DEFAULT_NOOP_INTERVAL 10000  // TODO use Kconfig
-
-/** @brief milliseconds to wait before retrying a failed request */
-#define RETRY_INTERVAL 2000  // TODO use Kconfig
-
-#define HTTP_TIMEOUT 3000  // TODO use Kconfig
-
-#define API_URL "http://dev.api.doorkeeper.kraxor.net/dev/event" // use Kconfig or NVS or eFuse
+#define API_URL CONFIG_DK_SERVER_API_URL "/dev/event"
 
 class EventManager : public kbf::Task {
 public:
@@ -52,7 +42,7 @@ private:
 
     bool postEvent(Event event);
 
-    unsigned int noopInterval = DEFAULT_NOOP_INTERVAL;
+    unsigned int noopInterval = CONFIG_DK_DEFAULT_NOOP_INTERVAL;
 };
 
 #endif //DOORKEEPER_DEV_EVENT_MANAGER_H

+ 3 - 6
components/dk/include/tasks/ultrasonic.h

@@ -8,12 +8,9 @@
 #include <kbf/task.h>
 #include <kbf/rtos.h>
 
-#define READING_INTERVAL_MS 200  // TODO use Kconfig or SettingsManager
-
-/** @brief number of "closed" readings required to acknowledge closed state */
-#define CLOSED_CONFIDENCE 5  // TODO use Kconfig
-
-#define OPEN_CONFIDENCE (-3)  // TODO use Kconfig
+#define READING_INTERVAL_MS CONFIG_DK_ULTRASONIC_READING_INTERVAL
+#define CLOSED_CONFIDENCE CONFIG_DK_ULTRASONIC_CLOSED_CONFIDENCE
+#define OPEN_CONFIDENCE (-CONFIG_DK_ULTRASONIC_OPEN_CONFIDENCE)
 
 class Ultrasonic : public kbf::Task {
 public:

+ 6 - 6
components/dk/src/api/calibrate_controller.cpp

@@ -1,5 +1,7 @@
 #include "api/calibrate_controller.h"
 
+#include <limits>
+
 #include <esp_log.h>
 #include <kbf/driver/ultrasonic.h>
 #include <kbf.h>
@@ -25,15 +27,13 @@ Response CalibrateController::post(const Request &request) {
     DisplayManager::setMessage("calibrating...");
 
     Ultrasonic::disable();
-    auto sensor = driver::Ultrasonic(5, 18); // TODO use Kconfig
+    auto sensor = driver::Ultrasonic(CONFIG_DK_ULTRASONIC_TRIGGER, CONFIG_DK_ULTRASONIC_ECHO);
 
-    int  min   = 99999;
+    int  min   = std::numeric_limits<int>::max();
     int  max   = 0;
     bool error = false;
 
-    const int sampleSize = 32; // TODO use Kconfig
-
-    for (int i = 0; i < sampleSize; i++) {
+    for (int i = 0; i < CONFIG_DK_ULTRASONIC_CALIB_SAMPLE_SIZE; i++) {
         auto reading = (int) sensor.read();
         if (reading >= 0x200) {
             ESP_LOGW(TAG, "reading failed: %d", reading);
@@ -48,7 +48,7 @@ Response CalibrateController::post(const Request &request) {
             max = reading + 1;
         }
 
-        kbf::sleep(100);
+        kbf::sleep(CONFIG_DK_ULTRASONIC_CALIB_SLEEP_MS);
     }
 
     ESP_LOGI(TAG, "%s: min = %d, max = %d", __func__, min, max);

+ 3 - 6
components/dk/src/dk.cpp

@@ -13,9 +13,6 @@
 #include "tasks/ultrasonic.h"
 
 
-#define MESSAGE_TIMEOUT 1000 // TODO use Kconfig
-
-
 using namespace kbf;
 using std::vector;
 using std::atomic;
@@ -40,10 +37,10 @@ namespace dk {
     void clearPairing() {
         ESP_LOGI(TAG, "%s", __func__);
         Ultrasonic::disable();
-        EventManager::stop();
         SettingsManager::instance()->setPairingId("");
         wifi::stop();
         mainEventGroup->setBit(MainEvents::SET_PAIRING_MODE);
+        EventManager::stop();
     }
 
     bool paired() {
@@ -63,8 +60,8 @@ namespace dk {
 
         auto pairingId = SettingsManager::instance()->pairingId();
         ESP_LOGI(TAG, "  pairingId = %s", pairingId.c_str());
-        auto client   = http::Client();
-        auto response = client.get("http://dev.api.doorkeeper.kraxor.net/dev/test_notification",
+        auto client   = http::Client(CONFIG_DK_HTTP_CLIENT_TIMEOUT);
+        auto response = client.get(CONFIG_DK_SERVER_API_URL "/dev/test_notification",
                                     {{{"X-Pairing-ID", pairingId}}});
         ESP_LOGI(TAG, "test_notification response: %d", response->status);
 

+ 2 - 2
components/dk/src/pairing_service/pair_controller.cpp

@@ -32,12 +32,12 @@ http::Response PairController::post(const http::Request &request) {
 
     DisplayManager::setMessage("sending request");
 
-    auto client = http::Client();
+    auto client = http::Client(CONFIG_DK_HTTP_CLIENT_TIMEOUT);
     nlohmann::json data = { // TODO get ID from efuse or nvs
         { "device_uuid", "5efe1f65-0e42-4071-aef9-4a1da46ea12a" },
         { "firebase_token", firebaseToken }
     };
-    auto response = client.post("http://dev.api.doorkeeper.kraxor.net/register/pairing", data); // TODO get server URL from efuse or nvs
+    auto response = client.post(CONFIG_DK_SERVER_API_URL "/register/pairing", data);
 
     if (response->status != 200) {
         ESP_LOGW(TAG, "failed: %d", response->status);

+ 3 - 4
components/dk/src/pairing_service/settings_controller.cpp

@@ -34,7 +34,7 @@ enum ConnectStatus {
 };
 
 static bool connect(const string &ssid, const string &password) {
-    const char *TAG = "SettingsController::connect";
+    static const char *TAG = "SettingsController::connect";
     ESP_LOGI(TAG, "connect(\"%s\", ...)", ssid.c_str());
     ESP_LOGD(TAG, "credentials: %s; %s", ssid.c_str(), password.c_str());
 
@@ -42,10 +42,9 @@ static bool connect(const string &ssid, const string &password) {
     wifi::sta::start();
     wifi::sta::onDisconnect = {[]() { eventGroup.setBit(DISCONNECTED); }};
     wifi::sta::onIp         = {[]() { eventGroup.setBit(GOT_IP); }};
-    wifi::sta::connect(ssid, password, true, 2);
+    wifi::sta::connect(ssid, password, true, CONFIG_DK_PAIRING_WIFI_MAX_RETRY);
 
-    const int timeoutMillis = 10000; // TODO use Kconfig and / or a proper timer
-    eventGroup.waitForAny({DISCONNECTED, GOT_IP}, timeoutMillis);
+    eventGroup.waitForAny({DISCONNECTED, GOT_IP}, CONFIG_DK_PAIRING_WIFI_TIMEOUT);
 
     if (eventGroup.getBit(GOT_IP)) {
         ESP_LOGI(TAG, "connection successful");

+ 2 - 2
components/dk/src/pairing_service/verify_controller.cpp

@@ -32,12 +32,12 @@ http::Response VerifyController::post(const http::Request &request) {
 
     DisplayManager::setMessage("verifying");
 
-    auto client = http::Client();
+    auto client = http::Client(CONFIG_DK_HTTP_CLIENT_TIMEOUT);
     nlohmann::json data = { // TODO get ID from efuse or nvs
         { "device_uuid", "5efe1f65-0e42-4071-aef9-4a1da46ea12a" },
         { "pairing_id", pairingId }
     };
-    auto response = client.post("http://dev.api.doorkeeper.kraxor.net/register/verify", data); // TODO get server URL from efuse or nvs
+    auto response = client.post(CONFIG_DK_SERVER_API_URL "/register/verify", data);
 
     if (response->status != 200) {
         ESP_LOGW(TAG, "failed: %d", response->status);

+ 1 - 1
components/dk/src/tasks/boot.cpp

@@ -44,7 +44,7 @@ void Boot::run(void *arg) {
     mainEventGroup = make_shared<rtos::EventGroup>();
 
     ESP_LOGI(TAG, "setting up button driver");
-    button = make_shared<driver::Button>(19); // TODO use Kconfig
+    button = make_shared<driver::Button>(CONFIG_DK_BUTTON1);
     button->onRelease   = {[](driver::Button &) {
         ESP_LOGI(TAG, "button->onRelease()");
         mainEventGroup->setBit(MainEvents::SEND_TEST_MESSAGE);

+ 9 - 2
components/dk/src/tasks/display_manager.cpp

@@ -21,7 +21,7 @@ atomic<DisplayManager::DoorState> DisplayManager::doorState;
 string                            DisplayManager::status;
 string                            DisplayManager::message;
 
-const int         DisplayManager::defaultTimeout = 2000;
+const int         DisplayManager::defaultTimeout = CONFIG_DK_DEFAULT_MESSAGE_TIMEOUT;
 atomic<int>       DisplayManager::timeoutMs_     = 0;
 
 shared_ptr<rtos::EventGroup> DisplayManager::eventGroup;
@@ -44,7 +44,14 @@ DisplayManager::DisplayManager(const std::string &name, void *arg, uint32_t stac
         Task(name, arg, stackSize, priority) {
     ESP_LOGI(TAG, "%s()", __func__);
 
-    lcd = make_shared<driver::LCD>(32, 33, 25, 26, 27, 13); // TODO use Kconfig
+    lcd = make_shared<driver::LCD>(
+            CONFIG_DK_LCD_RS,
+            CONFIG_DK_LCD_E,
+            CONFIG_DK_LCD_D4,
+            CONFIG_DK_LCD_D5,
+            CONFIG_DK_LCD_D6,
+            CONFIG_DK_LCD_D7
+    );
     lcd->setChr(CHR_KEY, chr_key);
     lcd->setChr(CHR_KEY_INV, chr_key_inv);
     lcd->setChr(CHR_WIFI, chr_wifi);

+ 4 - 4
components/dk/src/tasks/event_manager.cpp

@@ -16,7 +16,7 @@ using std::nullopt;
 using std::to_string;
 using std::shared_ptr;
 
-Queue<EventManager::Event> EventManager::eventQueue{EVENT_QUEUE_SIZE};
+Queue<EventManager::Event> EventManager::eventQueue{CONFIG_DK_EVENT_QUEUE_SIZE};
 
 static shared_ptr<Task> instance;
 
@@ -35,8 +35,8 @@ static shared_ptr<Task> instance;
         }
 
         while (!postEvent(lastEvent.value())) {
-            ESP_LOGI(TAG, "retrying in %d ms...", RETRY_INTERVAL);
-            kbf::sleep(RETRY_INTERVAL);
+            ESP_LOGI(TAG, "retrying in %d ms...", CONFIG_DK_EVENT_HTTP_RETRY_INTERVAL);
+            kbf::sleep(CONFIG_DK_EVENT_HTTP_RETRY_INTERVAL);
         }
     }
 }
@@ -66,7 +66,7 @@ bool EventManager::event(EventManager::Event event) {
 bool EventManager::postEvent(Event event) {
     ESP_LOGI(TAG, "%s(%d)", __func__, (int) event);
 
-    http::Client client(HTTP_TIMEOUT);  // TODO reuse client
+    http::Client client(CONFIG_DK_HTTP_CLIENT_TIMEOUT);  // TODO reuse client
 
     try {
         auto response = client.post(API_URL, {{"type", event}}, {{{"X-Pairing-ID", pairingId}}});

+ 1 - 1
components/dk/src/tasks/ultrasonic.cpp

@@ -28,7 +28,7 @@ inline bool isOpen(uint32_t reading) { return reading < low || reading > high; }
 
 [[noreturn]] void Ultrasonic::run(void *arg) {
     eventGroup = make_shared<rtos::EventGroup>();
-    auto sensor = driver::Ultrasonic(5, 18);  // TODO use Kconfig
+    auto sensor = driver::Ultrasonic(CONFIG_DK_ULTRASONIC_TRIGGER, CONFIG_DK_ULTRASONIC_ECHO);
 
     confidence = 0;
     bool first = true;

+ 146 - 0
main/Kconfig.projbuild

@@ -0,0 +1,146 @@
+menu "Doorkeeper Configuration"
+
+    menu "Ultrasonic sensor"
+        config DK_ULTRASONIC_CALIB_SAMPLE_SIZE
+            int "Calibration sample size"
+            range 1 1000
+            default 32
+            help
+                Number of readings to take when calibrating.
+        config DK_ULTRASONIC_CALIB_SLEEP
+            int "Calibration reading interval (milliseconds)"
+            range 1 1000
+            default 100
+            help
+                Milliseconds to sleep between calibration readings.
+        config DK_ULTRASONIC_CLOSED_CONFIDENCE
+            int "Closed confidence"
+            range 1 1000
+            default 5
+            help
+                Number of consecutive readings required to determine the door state as closed.
+        config DK_ULTRASONIC_OPEN_CONFIDENCE
+            int "Open confidence"
+            range 1 1000
+            default 3
+            help
+                Number of consecutive readings required to determine the door state as open.
+        config DK_ULTRASONIC_READING_INTERVAL
+            int "Reading interval (milliseconds)"
+            range 10 10000
+            default 200
+        config DK_ULTRASONIC_TRIGGER
+            int "Trigger pin"
+            range 0 39
+            default 25
+            help
+                GPIO pin connected to the trigger pin of the ultrasonic sensor.
+        config DK_ULTRASONIC_ECHO
+            int "Echo pin"
+            range 0 39
+            default 32
+            help
+                GPIO pin connected to the echo pin of the ultrasonic sensor.
+    endmenu
+    menu "DisplayManager"
+        config DK_DEFAULT_MESSAGE_TIMEOUT
+            int "Default message timeout (milliseconds)"
+            range 100 60000
+            default 1000
+            help
+                Default timeout for temporary messages to show on the display.
+        config DK_LCD_RS
+            int "RS pin"
+            range 0 39
+            default 17
+            help
+                GPIO pin connected to the RS pin of the LCD module.
+        config DK_LCD_E
+            int "E (EN) pin"
+            range 0 39
+            default 16
+            help
+                GPIO pin connected to the E (or EN) pin of the LCD module.
+        config DK_LCD_D4
+            int "D4 pin"
+            range 0 39
+            default 18
+            help
+                GPIO pin connected to the D4 pin of the LCD module.
+        config DK_LCD_D5
+            int "D5 pin"
+            range 0 39
+            default 19
+            help
+                GPIO pin connected to the D5 pin of the LCD module.
+        config DK_LCD_D6
+            int "D6 pin"
+            range 0 39
+            default 21
+            help
+                GPIO pin connected to the D6 pin of the LCD module.
+        config DK_LCD_D7
+            int "D7 pin"
+            range 0 39
+            default 22
+            help
+                GPIO pin connected to the D7 pin of the LCD module.
+    endmenu
+    menu "Buttons"
+        config DK_BUTTON1
+            int "Button 1 pin"
+            range 0 39
+            default 13
+            help
+                GPIO pin connected to button 1.
+        config DK_BUTTON2
+            int "Button 2 pin"
+            range 0 39
+            default 27
+            help
+                GPIO pin connected to button 2.
+    endmenu
+    menu "Networking"
+        config DK_SERVER_API_URL
+            string "Server API URL"
+            default "http://dev.api.doorkeeper.kraxor.net"
+        config DK_HTTP_CLIENT_TIMEOUT
+            int "HTTP client request timeout (milliseconds)"
+            range 0 60000
+            default 3000
+        config DK_PAIRING_WIFI_TIMEOUT
+            int "Pairing WiFi connection timeout (milliseconds)"
+            range 1000 60000
+            default 10000
+            help
+                Milliseconds to wait for the device to connect to the user's home WiFi network.
+        config DK_PAIRING_WIFI_MAX_RETRY
+            int "Pairing WiFi max retry attempts"
+            range 0 10
+            default 2
+            help
+                Maximum number of attempts to connect to the user's home WiFi.
+                NOTE: sometimes the connection fails on first attempt, even if the WPA password is correct.
+    endmenu
+    menu "EventManager"
+        config DK_EVENT_HTTP_RETRY_INTERVAL
+            int "EventManager HTTP retry interval (milliseconds)"
+            range 0 10000
+            default 2000
+            help
+                Milliseconds to wait before retrying sending an event to the server.
+        config DK_DEFAULT_NOOP_INTERVAL
+            int "Default NOOP interval (milliseconds)"
+            range 1000 3600000
+            default 10000
+            help
+                Milliseconds to wait between sending NOOP events.
+                NOTE: this will be overridden with the server setting once the first response is received from the server.
+        config DK_EVENT_QUEUE_SIZE
+            int "Event queue size"
+            range 1 1000
+            default 10
+            help
+                Maximum number of events to store in case the server is unreachable.
+    endmenu
+endmenu

+ 53 - 0
sdkconfig

@@ -125,6 +125,59 @@ CONFIG_PARTITION_TABLE_OFFSET=0x8000
 CONFIG_PARTITION_TABLE_MD5=y
 # end of Partition Table
 
+#
+# Doorkeeper Configuration
+#
+
+#
+# Ultrasonic sensor
+#
+CONFIG_DK_ULTRASONIC_CALIB_SAMPLE_SIZE=32
+CONFIG_DK_ULTRASONIC_CALIB_SLEEP_MS=100
+CONFIG_DK_ULTRASONIC_CLOSED_CONFIDENCE=5
+CONFIG_DK_ULTRASONIC_OPEN_CONFIDENCE=3
+CONFIG_DK_ULTRASONIC_READING_INTERVAL=200
+CONFIG_DK_ULTRASONIC_TRIGGER=25
+CONFIG_DK_ULTRASONIC_ECHO=32
+# end of Ultrasonic sensor
+
+#
+# DisplayManager
+#
+CONFIG_DK_DEFAULT_MESSAGE_TIMEOUT=1000
+CONFIG_DK_LCD_RS=17
+CONFIG_DK_LCD_E=16
+CONFIG_DK_LCD_D4=18
+CONFIG_DK_LCD_D5=19
+CONFIG_DK_LCD_D6=21
+CONFIG_DK_LCD_D7=22
+# end of DisplayManager
+
+#
+# Buttons
+#
+CONFIG_DK_BUTTON1=13
+CONFIG_DK_BUTTON2=27
+# end of Buttons
+
+#
+# Networking
+#
+CONFIG_DK_SERVER_API_URL="http://dev.api.doorkeeper.kraxor.net"
+CONFIG_DK_HTTP_CLIENT_TIMEOUT=3000
+CONFIG_DK_PAIRING_WIFI_TIMEOUT=10000
+CONFIG_DK_PAIRING_WIFI_MAX_RETRY=2
+# end of Networking
+
+#
+# EventManager
+#
+CONFIG_DK_EVENT_HTTP_RETRY_INTERVAL=2000
+CONFIG_DK_DEFAULT_NOOP_INTERVAL=10000
+CONFIG_DK_EVENT_QUEUE_SIZE=10
+# end of EventManager
+# end of Doorkeeper Configuration
+
 #
 # Compiler options
 #