addd ssd1306 lib

This commit is contained in:
2021-06-14 08:24:14 +02:00
parent 5c031f67ca
commit 5a5f977a5f
136 changed files with 17364 additions and 168 deletions

View File

@@ -0,0 +1,8 @@
#pragma once
#define SDA_OLED 4
#define SCL_OLED 5
#define RST_OLED 13
#define DSMR_DATA 12
#define DSMR_REQ 14

View File

@@ -0,0 +1,13 @@
#include "connection.h"
void initConnection(void)
{
}
void handleConnection(void)
{
}
wifiStrength getWifiStrength(void)
{
return signal_3;
}

View File

@@ -0,0 +1,16 @@
#pragma once
void initConnection( void );
void handleConnection(void);
enum wifiStrength
{
notConnected,
connecting,
signal_1,
signal_2,
signal_3,
signal_4
};
wifiStrength getWifiStrength(void);

View File

@@ -0,0 +1,120 @@
#include "display.h"
SSD1306Wire display(0x3c, SDA, SCL);
bool initOK = false;
OLEDDisplayUi ui(&display);
void msOverlay(OLEDDisplay *display, OLEDDisplayUiState *state)
{
}
void initFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y)
{
// draw an xbm image.
// Please note that everything that should be transitioned
// needs to be drawn relative to x and y
//display->drawXbm(x + 34, y + 14, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits);
display->setTextAlignment(TEXT_ALIGN_CENTER);
display->setFont(ArialMT_Plain_16);
display->drawString(64 + x, 8 + y, "DU-DSMR-W");
//display->
}
void DSMRStatsFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y)
{
// Demonstrates the 3 included default sizes. The fonts come from SSD1306Fonts.h file
// Besides the default fonts there will be a program to convert TrueType fonts into this format
display->setTextAlignment(TEXT_ALIGN_LEFT);
display->setFont(ArialMT_Plain_10);
display->drawString(0 + x, 0 + y, "DSMR stats:");
display->drawString(0 + x, 11 + y, getMsgCount());
}
void ElecFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y)
{
display->setTextAlignment(TEXT_ALIGN_LEFT);
display->setFont(ArialMT_Plain_10);
display->drawString(0 + x, 0 + y, "Electricity:");
display->drawString(0 + x, 11 + y, getElecDelivered());
display->drawString(0 + x, 22 + y, getElecReturned());
}
void GasFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y)
{
display->setTextAlignment(TEXT_ALIGN_LEFT);
display->setFont(ArialMT_Plain_10);
display->drawString(0 + x, 0 + y, getGasDelivered());
}
void drawFrame5(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y)
{
}
// This array keeps function pointers to all frames
// frames are the single views that slide in
FrameCallback frames[] = {initFrame, DSMRStatsFrame, ElecFrame, GasFrame};//, drawFrame5};
// how many frames are there?
int frameCount = 4;
// Overlays are statically drawn on top of a frame eg. a clock
//OverlayCallback overlays[] = {msOverlay};
//int overlaysCount = 1;
void initDisplay(void)
{
// The ESP is capable of rendering 60fps in 80Mhz mode
// but that won't give you much time for anything else
// run it in 160Mhz mode or just set it to 30 fps
ui.setTargetFPS(30);
// Customize the active and inactive symbol
//ui.setActiveSymbol(activeSymbol);
//ui.setInactiveSymbol(inactiveSymbol);
// You can change this to
// TOP, LEFT, BOTTOM, RIGHT
ui.setIndicatorPosition(RIGHT);
// Defines where the first frame is located in the bar.
ui.setIndicatorDirection(LEFT_RIGHT);
// You can change the transition that is used
// SLIDE_LEFT, SLIDE_RIGHT, SLIDE_UP, SLIDE_DOWN
ui.setFrameAnimation(SLIDE_UP);
// Add frames
ui.setFrames(frames, frameCount);
// Add overlays
//ui.setOverlays(overlays, overlaysCount);
//ui.disableAllIndicators();
// Initialising the UI will init the display too.
ui.init();
Serial.printf("initdisplay: h=%u, w=%u", display.height(), display.width());
//display.flipScreenVertically();
}
void handleDisplay(void)
{
int remainingTimeBudget = ui.update();
if (remainingTimeBudget > 0)
{
// You can do some work here
// Don't do stuff if you are below your
// time budget.
Serial.println(remainingTimeBudget);
//delay(remainingTimeBudget);
}
}

View File

@@ -0,0 +1,12 @@
#pragma once
#include "Arduino.h"
#include "OLEDDisplayUi.h"
#include "SSD1306Wire.h" // legacy include: `#include "SSD1306.h"`
#include "board.h"
#include "images.h"
#include "dsmrhandler.h"
#include "connection.h"
void initDisplay(void);
void handleDisplay(void);

View File

View File

@@ -0,0 +1,2 @@
#pragma once

View File

@@ -0,0 +1,102 @@
#include "dsmrhandler.h"
/* -------------------------------------------------*/
/* https://github.com/matthijskooijman/arduino-dsmr */
/* -------------------------------------------------*/
SoftwareSerial DMSRSerial;
P1Reader reader(&DMSRSerial, DSMR_REQ);
unsigned long last;
DSMRData data;
unsigned long msgCount;
void dsmrParser(void)
{
msgCount = 1322;
ParseResult<void> res = P1Parser::parse(&data, testData, lengthof(testData), true);
if (res.err)
{
// Parsing error, show it
Serial.println(res.fullError(testData, testData + lengthof(testData)));
}
else
{
// Parsed succesfully, print all values
data.applyEach(Printer());
}
}
void dsmrReader(void)
{
//process msg
reader.loop();
unsigned long now = millis();
if (now - last > 60000)
{
reader.enable(true);
last = now;
}
if (reader.available())
{
msgCount++;
String err;
if (reader.parse(&data, &err))
{
// Parse succesful, print result
data.applyEach(Printer());
}
else
{
// Parser error, print error
Serial.println(err);
}
}
}
void initDSMR(void)
{
//init soft serial
DMSRSerial.begin(115200, SWSERIAL_8N1, -1, DSMR_DATA);
reader.enable(true);
last = millis();
#ifdef TEST
dsmrParser();
#endif
}
void handleDSMR(void)
{
dsmrReader();
}
String getElecDelivered(void)
{
String tmpstr("Cons: ");
tmpstr += data.power_delivered;
tmpstr += " kwh";
return tmpstr;
}
String getElecReturned(void)
{
String tmpstr("Prod: ");
tmpstr += data.power_returned;
tmpstr += " kwh";
return tmpstr;
}
String getGasDelivered(void)
{
String tmpstr("Cons: ");
tmpstr += data.gas_delivered;
tmpstr += " m3";
return tmpstr;
}
String getMsgCount(void)
{
String tmpstr("#Msg: ");
tmpstr += msgCount;
return tmpstr;
}

View File

@@ -0,0 +1,119 @@
#pragma once
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "board.h"
#include "dsmr.h"
#define TEST
void initDSMR(void);
void handleDSMR(void);
String getElecDelivered(void);
String getElecReturned(void);
String getGasDelivered(void);
String getMsgCount(void);
constexpr SoftwareSerialConfig swSerialConfig = SWSERIAL_8E1;
using DSMRData = ParsedData<
/* String */ identification,
/* String */ p1_version,
/* String */ timestamp,
/* String */ equipment_id,
/* FixedValue */ energy_delivered_tariff1,
/* FixedValue */ energy_delivered_tariff2,
/* FixedValue */ energy_returned_tariff1,
/* FixedValue */ energy_returned_tariff2,
/* String */ electricity_tariff,
/* FixedValue */ power_delivered,
/* FixedValue */ power_returned,
/* FixedValue */ electricity_threshold,
/* uint8_t */ electricity_switch_position,
/* uint32_t */ electricity_failures,
/* uint32_t */ electricity_long_failures,
/* String */ electricity_failure_log,
/* uint32_t */ electricity_sags_l1,
/* uint32_t */ electricity_sags_l2,
/* uint32_t */ electricity_sags_l3,
/* uint32_t */ electricity_swells_l1,
/* uint32_t */ electricity_swells_l2,
/* uint32_t */ electricity_swells_l3,
/* String */ message_short,
/* String */ message_long,
/* FixedValue */ voltage_l1,
/* FixedValue */ voltage_l2,
/* FixedValue */ voltage_l3,
/* FixedValue */ current_l1,
/* FixedValue */ current_l2,
/* FixedValue */ current_l3,
/* FixedValue */ power_delivered_l1,
/* FixedValue */ power_delivered_l2,
/* FixedValue */ power_delivered_l3,
/* FixedValue */ power_returned_l1,
/* FixedValue */ power_returned_l2,
/* FixedValue */ power_returned_l3,
/* uint16_t */ gas_device_type,
/* String */ gas_equipment_id,
/* uint8_t */ gas_valve_position,
/* TimestampedFixedValue */ gas_delivered,
/* uint16_t */ thermal_device_type,
/* String */ thermal_equipment_id,
/* uint8_t */ thermal_valve_position,
/* TimestampedFixedValue */ thermal_delivered,
/* uint16_t */ water_device_type,
/* String */ water_equipment_id,
/* uint8_t */ water_valve_position,
/* TimestampedFixedValue */ water_delivered,
/* uint16_t */ slave_device_type,
/* String */ slave_equipment_id,
/* uint8_t */ slave_valve_position,
/* TimestampedFixedValue */ slave_delivered>;
struct Printer
{
template <typename Item>
void apply(Item &i)
{
if (i.present())
{
Serial.print(Item::name);
Serial.print(F(": "));
Serial.print(i.val());
Serial.print(Item::unit());
Serial.println();
}
}
};
const char testData[] =
"/KFM5KAIFA-METER\r\n"
"\r\n"
"1-3:0.2.8(40)\r\n"
"0-0:1.0.0(150117185916W)\r\n"
"0-0:96.1.1(0000000000000000000000000000000000)\r\n"
"1-0:1.8.1(000671.578*kWh)\r\n"
"1-0:1.8.2(000842.472*kWh)\r\n"
"1-0:2.8.1(000000.000*kWh)\r\n"
"1-0:2.8.2(000000.000*kWh)\r\n"
"0-0:96.14.0(0001)\r\n"
"1-0:1.7.0(00.333*kW)\r\n"
"1-0:2.7.0(00.000*kW)\r\n"
"0-0:17.0.0(999.9*kW)\r\n"
"0-0:96.3.10(1)\r\n"
"0-0:96.7.21(00008)\r\n"
"0-0:96.7.9(00007)\r\n"
"1-0:99.97.0(1)(0-0:96.7.19)(000101000001W)(2147483647*s)\r\n"
"1-0:32.32.0(00000)\r\n"
"1-0:32.36.0(00000)\r\n"
"0-0:96.13.1()\r\n"
"0-0:96.13.0()\r\n"
"1-0:31.7.0(001*A)\r\n"
"1-0:21.7.0(00.332*kW)\r\n"
"1-0:22.7.0(00.000*kW)\r\n"
"0-1:24.1.0(003)\r\n"
"0-1:96.1.0(0000000000000000000000000000000000)\r\n"
"0-1:24.2.1(150117180000W)(00473.789*m3)\r\n"
"0-1:24.4.0(1)\r\n"
"!6F4A\r\n";

View File

@@ -1,3 +1,4 @@
#include "Arduino.h"
#define WiFi_Logo_width 60
#define WiFi_Logo_height 36
const uint8_t WiFi_Logo_bits[] PROGMEM = {
@@ -27,7 +28,7 @@ const uint8_t WiFi_Logo_bits[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
const unsigned char activeSymbol[] PROGMEM = {
const uint8_t activeSymbol[] PROGMEM = {
B00000000,
B00000000,
B00011000,
@@ -38,7 +39,7 @@ const unsigned char activeSymbol[] PROGMEM = {
B00011000
};
const unsigned char inactiveSymbol[] PROGMEM = {
const uint8_t inactiveSymbol[] PROGMEM = {
B00000000,
B00000000,
B00000000,

View File

@@ -1,50 +1,16 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 by ThingPulse, Daniel Eichhorn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* ThingPulse invests considerable time and money to develop these open source libraries.
* Please support us by buying our products (and not the clones) from
* https://thingpulse.com
*
*/
#include <Arduino.h>
#include "display.h"
#include "dsmrhandler.h"
#include "connection.h"
#include <TimeLib.h>
// Include the correct display library
// For a connection via I2C using Wire include
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
#include "SSD1306Wire.h" // legacy include: `#include "SSD1306.h"`
// or #include "SH1106Wire.h", legacy include: `#include "SH1106.h"`
// For a connection via I2C using brzo_i2c (must be installed) include
// #include <brzo_i2c.h> // Only needed for Arduino 1.6.5 and earlier
// #include "SSD1306Brzo.h"
// #include "SH1106Brzo.h"
// For a connection via SPI include
// #include <SPI.h> // Only needed for Arduino 1.6.5 and earlier
// #include "SSD1306Spi.h"
// #include "SH1106SPi.h"
// Include the UI lib
#include "OLEDDisplayUi.h"
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(500);
initDisplay();
initConnection();
initDSMR();
// Include custom images
#include "images.h"
@@ -98,123 +64,9 @@ void clockOverlay(OLEDDisplay *display, OLEDDisplayUiState *state)
{
}
void analogClockFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y)
{
// ui.disableIndicator();
// Draw the clock face
// display->drawCircle(clockCenterX + x, clockCenterY + y, clockRadius);
display->drawCircle(clockCenterX + x, clockCenterY + y, 2);
//
//hour ticks
for (int z = 0; z < 360; z = z + 30)
{
//Begin at 0° and stop at 360°
float angle = z;
angle = (angle / 57.29577951); //Convert degrees to radians
int x2 = (clockCenterX + (sin(angle) * clockRadius));
int y2 = (clockCenterY - (cos(angle) * clockRadius));
int x3 = (clockCenterX + (sin(angle) * (clockRadius - (clockRadius / 8))));
int y3 = (clockCenterY - (cos(angle) * (clockRadius - (clockRadius / 8))));
display->drawLine(x2 + x, y2 + y, x3 + x, y3 + y);
}
// display second hand
float angle = second() * 6;
angle = (angle / 57.29577951); //Convert degrees to radians
int x3 = (clockCenterX + (sin(angle) * (clockRadius - (clockRadius / 5))));
int y3 = (clockCenterY - (cos(angle) * (clockRadius - (clockRadius / 5))));
display->drawLine(clockCenterX + x, clockCenterY + y, x3 + x, y3 + y);
//
// display minute hand
angle = minute() * 6;
angle = (angle / 57.29577951); //Convert degrees to radians
x3 = (clockCenterX + (sin(angle) * (clockRadius - (clockRadius / 4))));
y3 = (clockCenterY - (cos(angle) * (clockRadius - (clockRadius / 4))));
display->drawLine(clockCenterX + x, clockCenterY + y, x3 + x, y3 + y);
//
// display hour hand
angle = hour() * 30 + int((minute() / 12) * 6);
angle = (angle / 57.29577951); //Convert degrees to radians
x3 = (clockCenterX + (sin(angle) * (clockRadius - (clockRadius / 2))));
y3 = (clockCenterY - (cos(angle) * (clockRadius - (clockRadius / 2))));
display->drawLine(clockCenterX + x, clockCenterY + y, x3 + x, y3 + y);
}
void digitalClockFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y)
{
String timenow = String(hour()) + ":" + twoDigits(minute()) + ":" + twoDigits(second());
display->setTextAlignment(TEXT_ALIGN_CENTER);
display->setFont(ArialMT_Plain_24);
display->drawString(clockCenterX + x, clockCenterY + y, timenow);
}
// This array keeps function pointers to all frames
// frames are the single views that slide in
FrameCallback frames[] = {analogClockFrame, digitalClockFrame};
// how many frames are there?
int frameCount = 2;
// Overlays are statically drawn on top of a frame eg. a clock
OverlayCallback overlays[] = {clockOverlay};
int overlaysCount = 1;
void setup()
{
Serial.begin(115200);
Serial.println();
// The ESP is capable of rendering 60fps in 80Mhz mode
// but that won't give you much time for anything else
// run it in 160Mhz mode or just set it to 30 fps
ui.setTargetFPS(60);
// Customize the active and inactive symbol
ui.setActiveSymbol(activeSymbol);
ui.setInactiveSymbol(inactiveSymbol);
// You can change this to
// TOP, LEFT, BOTTOM, RIGHT
ui.setIndicatorPosition(TOP);
// Defines where the first frame is located in the bar.
ui.setIndicatorDirection(LEFT_RIGHT);
// You can change the transition that is used
// SLIDE_LEFT, SLIDE_RIGHT, SLIDE_UP, SLIDE_DOWN
ui.setFrameAnimation(SLIDE_LEFT);
ui.disableAllIndicators();
// Add frames
ui.setFrames(frames, frameCount);
// Add overlays
ui.setOverlays(overlays, overlaysCount);
// Initialising the UI will init the display too.
ui.init();
//display.flipScreenVertically();
unsigned long secsSinceStart = millis();
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSinceStart - seventyYears * SECS_PER_HOUR;
setTime(epoch);
}
void loop()
{
int remainingTimeBudget = ui.update();
if (remainingTimeBudget > 0)
{
// You can do some work here
// Don't do stuff if you are below your
// time budget.
delay(remainingTimeBudget);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print(".");
handleDisplay();
handleConnection();
}