From 32352962eff2615f70fe3e3f29541d0b35f96e0c Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Mon, 22 Apr 2024 23:31:49 +0200 Subject: [PATCH] Adds simple display, uss and imu service --- esp32/lib/ESP32-sveltekit/DisplayService.h | 47 ++++++++++++++++++ esp32/lib/ESP32-sveltekit/ESP32SvelteKit.h | 1 + esp32/lib/ESP32-sveltekit/IMUService.h | 55 +++++++++++++++++++++ esp32/lib/ESP32-sveltekit/USSService.h | 56 ++++++++++++++++++++++ esp32/platformio.ini | 12 ++--- esp32/src/main.cpp | 29 +++++++++++ 6 files changed, 194 insertions(+), 6 deletions(-) create mode 100644 esp32/lib/ESP32-sveltekit/DisplayService.h create mode 100644 esp32/lib/ESP32-sveltekit/IMUService.h create mode 100644 esp32/lib/ESP32-sveltekit/USSService.h diff --git a/esp32/lib/ESP32-sveltekit/DisplayService.h b/esp32/lib/ESP32-sveltekit/DisplayService.h new file mode 100644 index 0000000..3549cfb --- /dev/null +++ b/esp32/lib/ESP32-sveltekit/DisplayService.h @@ -0,0 +1,47 @@ +#pragma once + +#include +#include +#include +#include + +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 64 + +class DisplayService +{ +public: + DisplayService() : display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1) {}; + + bool begin() + { + std::lock_guard guard(displayMutex); + if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { + return false; + } + display.clearDisplay(); + return true; + }; + + void clear() { + std::lock_guard guard(displayMutex); + display.clearDisplay(); + } + + void drawPixel(int16_t x, int16_t y, uint16_t color) { + std::lock_guard guard(displayMutex); + display.drawPixel(x, y, color); + } + + void displayMessage(const String &message, int x, int y) { + std::lock_guard lock(displayMutex); + display.setCursor(x, y); + display.clearDisplay(); + display.println(message); + display.display(); + } + +private: + Adafruit_SSD1306 display; + std::mutex displayMutex; +}; diff --git a/esp32/lib/ESP32-sveltekit/ESP32SvelteKit.h b/esp32/lib/ESP32-sveltekit/ESP32SvelteKit.h index baad7eb..f0b5e20 100644 --- a/esp32/lib/ESP32-sveltekit/ESP32SvelteKit.h +++ b/esp32/lib/ESP32-sveltekit/ESP32SvelteKit.h @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include diff --git a/esp32/lib/ESP32-sveltekit/IMUService.h b/esp32/lib/ESP32-sveltekit/IMUService.h new file mode 100644 index 0000000..9759e38 --- /dev/null +++ b/esp32/lib/ESP32-sveltekit/IMUService.h @@ -0,0 +1,55 @@ +#pragma once + +#include + +#define IMU_INTERVAL 2000 + +class IMUService +{ +public: + IMUService():_imu(Wire){}; + + void begin() + { + byte status = _imu.begin(); + if(status != 0) { + ESP_LOGE("IMUService", "MPU initialize failed"); + vTaskDelete(NULL); + return; + } + xTaskCreatePinnedToCore(this->_loopImpl, "IMU Service", 4096, this, tskIDLE_PRIORITY, NULL, ESP32SVELTEKIT_RUNNING_CORE); + }; + + float getTemp() { + return _imu.getTemp(); + } + + float getAngleX() { + return _imu.getAngleX(); + } + + float getAngleY() { + return _imu.getAngleX(); + } + + float getAngleZ() { + return _imu.getAngleZ(); + } + +protected: + static void _loopImpl(void *_this) { static_cast(_this)->_loop(); } + void _loop() + { + vTaskDelay(100); + _imu.calcOffsets(true,true); + TickType_t xLastWakeTime = xTaskGetTickCount(); + while (1) + { + _imu.update(); + vTaskDelayUntil(&xLastWakeTime, IMU_INTERVAL / portTICK_PERIOD_MS); + } + }; + +private: + MPU6050 _imu; +}; diff --git a/esp32/lib/ESP32-sveltekit/USSService.h b/esp32/lib/ESP32-sveltekit/USSService.h new file mode 100644 index 0000000..e024f13 --- /dev/null +++ b/esp32/lib/ESP32-sveltekit/USSService.h @@ -0,0 +1,56 @@ +#pragma once + +#include +#include +#include + +#define USS_INTERVAL 500 + +#define MAX_DISTANCE 200 + +#define LEFT_TRIG 12 +#define RIGHT_TRIG 13 + +class USSService +{ +public: + USSService(const std::vector& triggerPins, unsigned int maxDistance = MAX_DISTANCE) + : distances(triggerPins.size(), 0) { + for (size_t i = 0; i < triggerPins.size(); i++) { + sensors.emplace_back(triggerPins[i], triggerPins[i], maxDistance); + } + }; + + void begin() + { + xTaskCreatePinnedToCore(this->_loopImpl, "USS Service", 4096, this, tskIDLE_PRIORITY, NULL, ESP32SVELTEKIT_RUNNING_CORE); + }; + + unsigned int getDistance(size_t index) { + if (index >= distances.size()) return 0; + std::lock_guard lock(mutex_); + return distances[index]; + } + +protected: + static void _loopImpl(void *_this) { static_cast(_this)->_loop(); } + void _loop() + { + const TickType_t xFrequency = pdMS_TO_TICKS(USS_INTERVAL); + TickType_t xLastWakeTime = xTaskGetTickCount(); + while (1) + { + for (size_t i = 0; i < sensors.size(); i++) { + std::lock_guard lock(mutex_); + distances[i] = sensors[i].ping_cm(); + vTaskDelay(50); + } + vTaskDelayUntil(&xLastWakeTime, xFrequency); + } + }; + +private: + std::vector sensors; + std::vector distances; + std::mutex mutex_; +}; diff --git a/esp32/platformio.ini b/esp32/platformio.ini index 525a174..747ea0c 100644 --- a/esp32/platformio.ini +++ b/esp32/platformio.ini @@ -39,17 +39,17 @@ lib_deps = bblanchon/ArduinoJson@^6.21.2 https://github.com/theelims/PsychicMqttClient.git#0.1.1 ; thomasfredericks/Bounce2@ ^2.7.0 - ; teckel12/NewPing@^1.9.7 - ; adafruit/Adafruit SSD1306@^2.5.7 - ; adafruit/Adafruit GFX Library@^1.11.5 - ; adafruit/Adafruit BusIO@^1.9.3 + teckel12/NewPing@^1.9.7 + adafruit/Adafruit SSD1306@^2.5.7 + adafruit/Adafruit GFX Library@^1.11.5 + adafruit/Adafruit BusIO@^1.9.3 ; adafruit/Adafruit PWM Servo Driver Library@^2.4.1 ; adafruit/Adafruit ADS1X15@^2.4.0 ; adafruit/Adafruit HMC5883 Unified@^1.2.1 ; adafruit/Adafruit Unified Sensor@^1.1.11 ; plageoj/UrlEncode@ ^1.0.1 - ; rfetick/MPU6050_light@^1.1.0 - ; SPI + rfetick/MPU6050_light@^1.1.0 + SPI ; board_build.partitions = config/no_oat.csv extra_scripts = pre:scripts/build_app.py diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 1221053..33f4d97 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -21,12 +21,41 @@ LightStateService lightStateService = LightStateService(&server, esp32sveltekit.getMqttClient(), &lightMqttSettingsService); +/* + * Camera settings service + * Camera state service + * Initialize the camera + * + * Idle task for calculating usage - CPU usage + * Place to define the task priorities + * General setting like metric or imperial + * Global definitions + * Global connection manager - SDA, SCL, BUTTON, SCREEN, USS, SERVO, CAMERA + * + * Servo Service + * Servo Settings Service + * + * JSON serializable - PsRamAllocator + * Buffered JSON reader / writer + * + * FT for PSRAM + * FT for bluetooth + * FT for ESPNOW controller + * + * + * Button input service - Add callback for button press + * */ + void setup() { Serial.begin(SERIAL_BAUD_RATE); esp32sveltekit.begin(); + // Wire.begin(SDA, SCL); + + // InitializeCamera(); + lightStateService.begin(); lightMqttSettingsService.begin();