Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
***************************************************************************
** Modified: as OTGW actually uses the Serial interface, so no more debug to serial please.
*/
#pragma once

/*---- start macro's ------------------------------------------------------------------*/

Expand Down
127 changes: 127 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# -*- make -*-

PROJ = $(notdir $(PWD))
SOURCES = $(wildcard *.ino *.cpp *.h)
FSDIR = data
FILES = $(wildcard $(FSDIR)/*)

# Don't use -DATOMIC_FS_UPDATE
CFLAGS_DEFAULT = -DNO_GLOBAL_HTTPUPDATE
CFLAGS = $(CFLAGS_DEFAULT)

CLI := arduino-cli
PLATFORM := esp8266:esp8266
CFGFILE := arduino-cli.yaml
ESP8266URL := https://github.com/esp8266/Arduino/releases/download/2.7.4/package_esp8266com_index.json
LIBRARIES := libraries/WiFiManager libraries/ArduinoJson libraries/pubsubclient libraries/TelnetStream libraries/Acetime libraries/Time libraries/OneWire libraries/DallasTemperature libraries/U8g2
BOARDS := arduino/package_esp8266com_index.json
# PORT can be overridden by the environment or on the command line. E.g.:
# export PORT=/dev/ttyUSB2; make upload, or: make upload PORT=/dev/ttyUSB2
PORT ?= /dev/ttyUSB0
BAUD ?= 460800

INO = $(PROJ).ino
MKFS = $(wildcard arduino/packages/esp8266/tools/mklittlefs/*/mklittlefs)
TOOLS = $(wildcard arduino/packages/esp8266/hardware/esp8266/*/tools)
ESPTOOL = python3 $(TOOLS)/esptool/esptool.py
BOARD = $(PLATFORM):d1_mini
FQBN = $(BOARD):eesz=4M1M
IMAGE = build/$(subst :,.,$(BOARD))/$(INO).bin
FILESYS = filesys.bin

export PYTHONPATH = $(TOOLS)/pyserial

binaries: $(IMAGE)

publish: $(PROJ)-fs.bin $(PROJ)-fw.bin

platform: $(BOARDS)

clean:
find $(FSDIR) -name '*~' -exec rm {} +

$(CFGFILE):
$(CLI) config init --dest-file $(CFGFILE)
$(CLI) config set board_manager.additional_urls $(ESP8266URL)
$(CLI) config set directories.data $(PWD)/arduino
$(CLI) config set directories.downloads $(PWD)/staging
$(CLI) config set directories.user $(PWD)
$(CLI) config set sketch.always_export_binaries true
$(CLI) config set library.enable_unsafe_install true

$(BOARDS): | $(CFGFILE)
$(CLI) core update-index
$(CLI) core install $(PLATFORM)

refresh: | $(CFGFILE)
$(CLI) lib update-index

flush: | $(CFGFILE)
$(CLI) cache clean

libraries/WiFiManager: | $(BOARDS)
$(CLI) lib install [email protected]

libraries/ArduinoJson:
$(CLI) lib install [email protected]

libraries/pubsubclient:
$(CLI) lib install [email protected]

libraries/TelnetStream:
$(CLI) lib install [email protected]

libraries/Acetime:
$(CLI) lib install [email protected]

libraries/u8g2:
$(CLI) lib install [email protected]

libraries/Time:
$(CLI) lib install --git-url https://github.com/PaulStoffregen/Time
# https://github.com/PaulStoffregen/Time/archive/refs/tags/v1.6.1.zip

libraries/OneWire:
$(CLI) lib install [email protected]

libraries/DallasTemperature:
$(CLI) lib install [email protected]

$(IMAGE): $(BOARDS) $(LIBRARIES) $(SOURCES)
$(CLI) compile --config-file $(CFGFILE) --fqbn=$(FQBN) --warnings default --verbose --build-property compiler.cpp.extra_flags="$(CFLAGS)"

$(FILESYS): $(FILES) $(CONF) | $(BOARDS) clean
$(MKFS) -p 256 -b 8192 -s 1024000 -c $(FSDIR) $@

$(PROJ)-fs.bin: $(FILES) $(CONF) | $(BOARDS) clean
$(MKFS) -p 256 -b 8192 -s 1024000 -c $(FSDIR) $@

$(PROJ)-fw.bin: $(IMAGE)
cp $(IMAGE) $@

$(PROJ).zip: $(PROJ)-fw.bin $(PROJ)-fs.bin
rm -f $@
zip $@ $^

# Build the image with debugging output
debug: CFLAGS = $(CFLAGS_DEFAULT) -DDEBUG
debug: $(IMAGE)

# Load only the sketch into the device
upload: $(IMAGE)
$(ESPTOOL) --port $(PORT) -b $(BAUD) write_flash 0x0 $(IMAGE)

# Load only the file system into the device
upload-fs: $(FILESYS)
$(ESPTOOL) --port $(PORT) -b $(BAUD) write_flash 0x300000 $(FILESYS)

# Load both the sketch and the file system into the device
install: $(IMAGE) $(FILESYS)
$(ESPTOOL) --port $(PORT) -b $(BAUD) write_flash 0x0 $(IMAGE) 0x300000 $(FILESYS)

.PHONY: binaries platform publish clean upload upload-fs install debug

### Allow customization through a local Makefile: Makefile-local.mk

# Include the local make file, if it exists
-include Makefile-local.mk
45 changes: 45 additions & 0 deletions OTGW-Display.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef OTGW_DISPLAY
#define OTGW_DISPLAY
#include <Arduino.h>
#include <U8g2lib.h>

class OTGW_Display
{

public:
enum class Pages {
DISPLAY_IP,
DISPLAY_TIME,
DISPLAY_UPTIME,
DISPLAY_VERSION
};
OTGW_Display ();
~OTGW_Display ();

// Start
void begin();

// Function to periodically call to update itself.
void tick ();

// Put a message over top.
// This will also force an update.
void message ( const String &str = "" );

private:
void draw_welcome();
void draw_message();

void draw_display_ip();
void draw_display_time();
void draw_display_uptime();
void draw_display_version();

private:
// U8g2 Object used for interaction with Oled.
U8G2 *u8g2 = nullptr;
String _message = "";
Pages page = Pages::DISPLAY_IP;
};

#endif // OTGW_DISPLAY
167 changes: 167 additions & 0 deletions OTGW-Display.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
#include "OTGW-Display.h"
#include "ESP8266WiFi.h"
#include "Wire.h"
#include "Debug.h"
#include "clib/u8g2.h"
#include "OTGW-Core.h"

OTGW_Display::~OTGW_Display()
{
if ( u8g2 ) {
delete u8g2;
}
}
void OTGW_Display::draw_welcome()
{

u8g2->drawFrame(0,0,128,64);
u8g2->drawUTF8(4,7, "OTGW Booting..");
char buf[16];
snprintf(buf, sizeof(buf), "Version %s", _VERSION );
u8g2->drawUTF8(4,24, buf );
u8g2->sendBuffer();
}

OTGW_Display::OTGW_Display()
{
}

void OTGW_Display::begin()
{
Wire.beginTransmission(0x3c);
int error = Wire.endTransmission();
if ( error == 0 ) {
u8g2 = new U8G2_SH1106_128X64_NONAME_F_HW_I2C (U8G2_R0);
} else {
// No display found.
}


if ( u8g2 != nullptr ) {
// Initialize u8g2
u8g2->beginSimple();
// Disable power save.
u8g2->setPowerSave(0);
// Lower the contrast, this will extend the lifetime of the
// OLED screen.
u8g2->setContrast(1);

// Set some defaults
u8g2->setFont(u8g2_font_t0_14b_mr);
u8g2->setFontRefHeightExtendedText();
u8g2->setDrawColor(1);
u8g2->setFontPosTop();
u8g2->setFontDirection(0);
draw_welcome ();
}
}

void OTGW_Display::draw_message()
{
u8g2->drawUTF8(4,7, _message.c_str());

}

void OTGW_Display::draw_display_ip()
{
char buf[32];
snprintf(buf, sizeof(buf), "IP");
u8g2->drawUTF8(4,4, buf);
snprintf(buf, sizeof(buf), "%d.%d.%d.%d", WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3]);
u8g2->drawUTF8(4,24, buf);
}

void OTGW_Display::draw_display_time()
{

char buf[50];
snprintf(buf, sizeof(buf), "Time");
u8g2->drawUTF8(4,4, buf);
time_t now = time(nullptr);
//Timezone based devtime
TimeZone myTz = timezoneManager.createForZoneName(CSTR(settingNTPtimezone));
ZonedDateTime myTime = ZonedDateTime::forUnixSeconds64(now, myTz);
snprintf(buf, 49, PSTR("%04d-%02d-%02d"), myTime.year(), myTime.month(), myTime.day());

u8g2->drawUTF8(4,24, buf);
snprintf(buf, 49, PSTR("%02d:%02d:%02d"), myTime.hour(), myTime.minute(), myTime.second());

u8g2->drawUTF8(4,40, buf);
}

void OTGW_Display::draw_display_uptime()
{

char buf[50];
snprintf(buf, sizeof(buf), "Uptime");
u8g2->drawUTF8(4,4, buf);
uint32_t seconds= upTimeSeconds;
uint32_t days = seconds/(60*60*24);
seconds -= days*60*60*24;
uint16_t hours = seconds/(60*60);
seconds -= hours*60*60;
uint16_t minutes = seconds/(60);
seconds -= minutes*60;
snprintf(buf, 49, PSTR("%03dd %02d:%02d:%02d"),
days, hours, minutes,seconds);

u8g2->drawUTF8(4,24, buf);
}
void OTGW_Display::draw_display_version()
{
u8g2->drawUTF8(4,4, "OTGW version");
char buf[12];
snprintf(buf, sizeof(buf), "%s", _VERSION);
u8g2->drawUTF8(4,24, buf);
}


void OTGW_Display::tick()
{
// If no display is available, lets not do anything.
if ( u8g2 == nullptr ) {
return;
}
// Draw border around the screen.
u8g2->clearBuffer();
u8g2->drawFrame(0,0,128,64);
u8g2->drawLine(108, 0,108, 64);

if ( _message.length() > 0 ) {
draw_message ();
} else {

switch ( page ) {
case OTGW_Display::Pages::DISPLAY_IP:
draw_display_ip ();
page = OTGW_Display::Pages::DISPLAY_TIME;
break;
case OTGW_Display::Pages::DISPLAY_TIME:
draw_display_time ();
page = OTGW_Display::Pages::DISPLAY_UPTIME;
break;
case OTGW_Display::Pages::DISPLAY_UPTIME:
draw_display_uptime ();
page = OTGW_Display::Pages::DISPLAY_VERSION;
break;
case OTGW_Display::Pages::DISPLAY_VERSION:
draw_display_version ();
page = OTGW_Display::Pages::DISPLAY_IP;
break;
}
}
if ( ((OTdata.valueLB) & 0x08) ) {
u8g2->drawUTF8( 112, 2, "F");
} else {
u8g2->drawUTF8( 112, 2, "X");
}

// Send internal buffer towards screen.
u8g2->sendBuffer();
}

void OTGW_Display::message(const String &str )
{
_message = str;
this->tick();
}
12 changes: 9 additions & 3 deletions OTGW-firmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "version.h"
#include "OTGW-firmware.h"
#include "OTGW-Display.h"

#define SetupDebugTln(...) ({ if (bPICavailable) DebugTln(__VA_ARGS__); })
#define SetupDebugln(...) ({ if (bPICavailable) Debugln(__VA_ARGS__); })
Expand All @@ -41,6 +42,9 @@

DECLARE_TIMER_SEC(timerpollsensor, settingGPIOSENSORSinterval, CATCH_UP_MISSED_TICKS);
DECLARE_TIMER_SEC(timers0counter, settingS0COUNTERinterval, CATCH_UP_MISSED_TICKS);

// Small status display interface.
OTGW_Display display;

//=====================================================================
void setup() {
Expand All @@ -53,8 +57,8 @@ void setup() {

SetupDebugln(F("\r\n[OTGW firmware - Nodoshop version]\r\n"));
SetupDebugf("Booting....[%s]\r\n\r\n", _VERSION);


detectPIC();

//setup randomseed the right way
Expand Down Expand Up @@ -93,6 +97,7 @@ void setup() {
rebootCount = updateRebootCount();
updateRebootLog(lastReset);

display.begin();
SetupDebugln(F("Setup finished!\r\n"));

// After resetting the OTGW PIC never send anything to Serial for debug
Expand Down Expand Up @@ -245,7 +250,8 @@ void doTaskEvery1s(){
//===[ Do task every 5s ]===
void doTaskEvery5s(){
//== do tasks ==

//== Update Display ==
display.tick();
}

//===[ Do task every 30s ]===
Expand Down