diff --git a/src/main.cpp b/src/main.cpp index 0ae0943..5b5b285 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,7 @@ +#include +#include #include +#include #include "sensesp_app.h" #include "sensesp_app_builder.h" @@ -8,6 +11,39 @@ // 1-Wire data pin on SH-ESP32 #define ONEWIRE_PIN 4 +// SDA and SCL pins on SH-ESP32 +#define SDA_PIN 16 +#define SCL_PIN 17 + +// OLED display width and height, in pixels +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 64 + +// define temperature display units +#define TEMP_DISPLAY_FUNC KelvinToCelsius +//#define TEMP_DISPLAY_FUNC KelvinToFahrenheit + +TwoWire* i2c; +Adafruit_SSD1306* display; + +/// Clear a text row on an Adafruit graphics display +void ClearRow(int row) { display->fillRect(0, 8 * row, SCREEN_WIDTH, 8, 0); } + +float KelvinToCelsius(float temp) { + return temp - 273.15; +} + +float KelvinToFahrenheit(float temp) { + return (temp - 273.15) * 9./5. + 32.; +} + +void PrintTemperature(int row, String title, float temperature) { + ClearRow(row); + display->setCursor(0, 8 * row); + display->printf("%s: %.1f", title.c_str(), TEMP_DISPLAY_FUNC(temperature)); + display->display(); +} + ReactESP app([]() { // Some initialization boilerplate when in debug mode... #ifndef SERIAL_DEBUG_DISABLED @@ -43,6 +79,29 @@ ReactESP app([]() { main_engine_exhaust_temperature->connectTo(new SKOutput( "propulsion.main.exhaustTemperature", "/mainEngineExhaustTemp/skPath")); + // initialize the display + i2c = new TwoWire(0); + i2c->begin(SDA_PIN, SCL_PIN); + display = new Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, i2c, -1); + if (!display->begin(SSD1306_SWITCHCAPVCC, 0x3C)) { + Serial.println(F("SSD1306 allocation failed")); + } + delay(100); + display->setRotation(2); + display->clearDisplay(); + display->setTextSize(1); + display->setTextColor(SSD1306_WHITE); + display->setCursor(0, 0); + display->printf("Host: %s", sensesp_app->get_hostname().c_str()); + display->display(); + + // Add display updaters for temperature values + main_engine_temperature->connect_to(new LambdaConsumer( + [](float temperature) { PrintTemperature(1, "Engine", temperature); })); + main_engine_coolant_temperature->connect_to(new LambdaConsumer( + [](float temperature) { PrintTemperature(2, "Coolant", temperature); })); + main_engine_exhaust_temperature->connect_to(new LambdaConsumer( + [](float temperature) { PrintTemperature(3, "Exhaust", temperature); })); sensesp_app->enable(); });