Adds simple display, uss and imu service
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <Wire.h>
|
||||
#include <mutex>
|
||||
|
||||
#define SCREEN_WIDTH 128
|
||||
#define SCREEN_HEIGHT 64
|
||||
|
||||
class DisplayService
|
||||
{
|
||||
public:
|
||||
DisplayService() : display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1) {};
|
||||
|
||||
bool begin()
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(displayMutex);
|
||||
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
|
||||
return false;
|
||||
}
|
||||
display.clearDisplay();
|
||||
return true;
|
||||
};
|
||||
|
||||
void clear() {
|
||||
std::lock_guard<std::mutex> guard(displayMutex);
|
||||
display.clearDisplay();
|
||||
}
|
||||
|
||||
void drawPixel(int16_t x, int16_t y, uint16_t color) {
|
||||
std::lock_guard<std::mutex> guard(displayMutex);
|
||||
display.drawPixel(x, y, color);
|
||||
}
|
||||
|
||||
void displayMessage(const String &message, int x, int y) {
|
||||
std::lock_guard<std::mutex> lock(displayMutex);
|
||||
display.setCursor(x, y);
|
||||
display.clearDisplay();
|
||||
display.println(message);
|
||||
display.display();
|
||||
}
|
||||
|
||||
private:
|
||||
Adafruit_SSD1306 display;
|
||||
std::mutex displayMutex;
|
||||
};
|
||||
@@ -37,6 +37,7 @@
|
||||
#include <RestartService.h>
|
||||
#include <SecuritySettingsService.h>
|
||||
#include <SleepService.h>
|
||||
#include <Wire.h>
|
||||
#include <SystemStatus.h>
|
||||
#include <WiFiScanner.h>
|
||||
#include <WiFiSettingsService.h>
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include <MPU6050_light.h>
|
||||
|
||||
#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<IMUService *>(_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;
|
||||
};
|
||||
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <NewPing.h>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
#define USS_INTERVAL 500
|
||||
|
||||
#define MAX_DISTANCE 200
|
||||
|
||||
#define LEFT_TRIG 12
|
||||
#define RIGHT_TRIG 13
|
||||
|
||||
class USSService
|
||||
{
|
||||
public:
|
||||
USSService(const std::vector<uint8_t>& 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<std::mutex> lock(mutex_);
|
||||
return distances[index];
|
||||
}
|
||||
|
||||
protected:
|
||||
static void _loopImpl(void *_this) { static_cast<USSService *>(_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<std::mutex> lock(mutex_);
|
||||
distances[i] = sensors[i].ping_cm();
|
||||
vTaskDelay(50);
|
||||
}
|
||||
vTaskDelayUntil(&xLastWakeTime, xFrequency);
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
std::vector<NewPing> sensors;
|
||||
std::vector<unsigned int> distances;
|
||||
std::mutex mutex_;
|
||||
};
|
||||
Reference in New Issue
Block a user