Skip to content

SPIBus, Sensor SHTC3, Driver ST7789, MicroUI with json-settings - #2238

Draft
TobiasIvanSergeevich wants to merge 20 commits into
openshwprojects:mainfrom
TobiasIvanSergeevich:main
Draft

TobiasIvanSergeevich wants to merge 20 commits into
openshwprojects:mainfrom
TobiasIvanSergeevich:main

Conversation

@TobiasIvanSergeevich

@TobiasIvanSergeevich TobiasIvanSergeevich commented Sep 23, 2026 •

Copy link
Copy Markdown

SENSOR SHTC3

Added support for temperature and humidity sensor type SHTC3 (https://sensirion.com/products/catalog/SHTC3)
Communication via the I2C bus. Pin support for sensor power management. Lines SHTC3_SCK, SHTC3_SDA, SHTC3_PWR (or SHTC3_PWR_n) have assiged via WEB Config Module. Driver starts automaticaly. Additional error channel can be assigned for sensor state signaling thought command line: SHTC3_SetErrorOutput CH10

HARDWARE SPI-BUS DRIVER (HWSPI)

(HAL layer implemented only for ESP8266 platform) Оn ESP8266 platform supports only HSPI.
To start driver add to command line "startDriver HWSPI". On start driver have registred on SPPIBUS interface. Any SPI-device can attach to spi-bus, gives bus name, eg. "hspi".
To implement spi-device should add this code:

#include "drv_spibus.h"
...
static obk_spidevice_t     	spidev;
...
/* init spi */
const char* spi_bus = "hspi";
/* spi config */
spidev.config.mode = OBK_SPI_MODE_3|OBK_SPI_MSB|OBK_SPI_3WIRE;
spidev.config.data_width = 8;
spidev.config.max_hz = 20000000L;
/* ! nss_pin must be actual GPIO pin, not obk pin index or -1 if not use*/
spidev.config.nss_pin = HAL_GetGPIOPin(nss_obk_pin);
/* attach to spi bus */
if (obk_spi_bus_attach_device(&spidev, spi_bus, NULL) != OBK_EOK) {
	ADDLOG_INFO(LOG_FEATURE_DRV, "ST7789 can't attach to bus [%s]" , spi_bus);
	return;
}	

/* send or receive */
obk_spi_message_t message;
message.send_buf = (uint8_t*)&xfer_buf;
message.recv_buf = NULL;
message.length   = 1;
message.repeat   = 1;
message.cs_take    = 0;
message.cs_release = 0;
message.next = NULL;
spidev.bus->ops->xfer(&dev->spidev, &message);

!Important: HAL method xfer for ESP8266 platform used function spi_trans from sdk. This function is not applicable, then you need write and read during the same transfer. It should be corrected in future.

ST7789 TFT-DISPLAY DRIVER

ST7789 driver for TFT-display via spi interface. Can attach to spi-bus by this name. Eg. for platform ESP8266 - "hspi".
To start driver add to command line "startDriver ST7789 [SPI_NAME] [CS_PIN] [CS_LEVEL] [DC_PIN] [RESET_PIN]
[DYSP_WIDTH] [DYSP_HEIGHT] [DYSP_W_OFS] [DYSP_H_OFS] [ROTATION]", where
[SPI_NAME] - spi bus hame, eg. "hspi"
[CS_PIN] - chip select line, eg. "IO12" or "NA" if not use
[CS_LEVEL] - chip select line level, not implemented yet
[DC_PIN] - data/command line, eg. "IO15"
[RS_PIN] - reset line, eg. "IO2" or "NA" if not use
[DYSP_WIDTH] - actual dislay width, eg.135
[DYSP_HEIGHT] - actual dislay height, eg.240
[DYSP_W_OFS] - width, offset, eg.52
[DYSP_H_OFS] - height offset, eg.40
[ROTATION] - display rotation: 0, 90, 270, 360
Source code for use st7789 driver:

#include "drv_idisplay.h"
static obk_gui_service_t 	gui;
/* attach to IDisplay idisplay_name */
const char* idisplay_name = "st7789";
obk_display_attach_gui(&gui, idisplay_name, NULL);
...
/* draw fill rect */
obk_display_renderer_t *renderer = gui.renderer;
if (renderer->ops->drawFillRect) return;
  renderer->ops->drawFillRect(x, y, x+w-1, y+h-1, rgb565(red,green,blue));
...
/* draw text */
obk_display_renderer_t *renderer = gui.renderer;
if (renderer->ops->setFontSize)  renderer->ops->setFontSize(font_size);
if (renderer->ops->drawString) 
  renderer->ops->drawString(x, y, (uint8_t *)text, rgb565(red,green,blue), rgb565(0,0,0));
...
/* use frame mode */
obk_display_renderer_t *renderer = gui.renderer;
if (renderer->ops->beginFrame & renderer->ops->endFrame) {
  if (renderer->ops->beginFrame(x, y, w, h, rgb565(red,green,blue))) {
    /* next draw command will modify frame buffer in memory without output on display */
    ...
    /* end framw drawing and output it to display */
    renderer->ops->endFrame();
  }
}

MICROUI DRIVER

Launch: startDriver MicroUI st7789
st7789 is the name of the graphics driver that has the obk_display_renderer_t interface.
The reading of the GUI from the microui.json file has been implemented. Due to lack of memory on the ESP8266 platform, the jsmn_stream streaming reader module has been added.c
In the cJSON module.c in the JSON_SetValuestring function(cJSON added the valuestring check for NULL
The following gui objects are implemented:

  • window
  • label; you can specify the name of the channel, the value of which will be displayed in the text. Teskt should
    be a string with formatting.
  • button; you can specify the name of the command that is executed when the button is clicked.
  • header;
  • layout, including layoutset (arbitrary region), width, height, and row.
  • icon; you can specify the channel name and different icons for the ON and OFF states.
    To control button presses, the OnKeyPress command is available.
    The command can be linked to obk events such as OnClick, OnHold, OnDoubleClick, etc.
    The focus is transferred to the required element when the TAB key is pressed.
    Pressing the button is performed when the ENTER key is pressed.
    Example for a physical button at input IO0:
    addEventHandler OnClick 0 OnKeyPress TAB
    addEventHandler OnHold 0 OnKeyPress ENTER

Modification of the new_pins.c module.
Special channels have been added:
SPECIAL_CHANNEL_WIFICONNECTED 139
SPECIAL_CHANNEL_MQTTCONNECTED 140
indicating a connection to Wi‑Fi and MQTT, respectively. Used for display.

Full autoexec,bat for complex project ESP8266+SHTC3+ST7789+MICROUI

SHTC3_SetErrorOutput CH10
startDriver HWSPI
startDriver ST7789 hspi NA IO15 IO2 CH4 135 240 53 40 180
startDriver MicroUI st7789
addEventHandler OnClick 0 OnKeyPress TAB
addEventHandler OnHold 0 OnKeyPress ENTER

Example microui.json
{"driver":"MicroUI","style":{"font":"","font_size":2,"size":[30,130],"padding":5,"spacing":4,"indent":24,"title_height":26,"scrollbar_size":12,"thumb_size":8,"color_text":{"a":255,"b":255,"g":255,"r":255},"color_border":{"a":255,"b":255,"g":100,"r":100},"color_windowbg":{"a":255,"b":50,"g":0,"r":0},"color_titlebg":{"a":255,"b":25,"g":0,"r":0},"color_titletext":{"a":255,"b":255,"g":255,"r":255},"color_panelbg":{"a":0,"b":0,"g":0,"r":0},"color_button":{"a":255,"b":115,"g":0,"r":0},"color_buttonhover":{"a":255,"b":95,"g":0,"r":0},"color_buttonfocus":{"a":255,"b":255,"g":0,"r":0},"color_base":{"a":255,"b":30,"g":0,"r":0},"color_basehover":{"a":255,"b":35,"g":0,"r":0},"color_basefocus":{"a":255,"b":40,"g":0,"r":0},"color_scrollbase":{"a":255,"b":43,"g":0,"r":0},"color_scrollthumb":{"a":255,"b":30,"g":0,"r":0}},"gui":[{"window":{"title":"","opt":["notitle"],"rect":{"h":237,"w":130,"x":2,"y":2},"items":[{"layout":{"row":[27,27,27],"height":27}},{"icon":{"on":{"icon":6},"off":{"icon":7},"state":"CH139"}},{"icon":{"on":{"icon":6},"off":{"icon":7},"state":"CH140"}},{"icon":{"on":{"icon":9},"off":{"icon":8},"state":"CH10"}},{"header":{"title":"T,oC:","opt":["expanded"],"items":[{"layout":{"height":50}},{"label":{"align":"center","size":4,"text":"%0.1f","arguments":["CH0"]}}]}},{"header":{"title":"H,%:","opt":["expanded"],"items":[{"layout":{"height":50}},{"label":{"align":"center","size":4,"text":"%0.1f","arguments":["CH1"]}}]}},{"layout":{"row":[27,27,27,27],"height":27}},{"button":{"title":"","icon":5,"command":"restart"}},{"button":{"title":"","icon":11,"command":"restart"}},{"button":{"title":"","icon":9,"command":"restart"}},{"button":{"title":"","icon":10,"command":"restart"}}]}}]}

Some illustrations:
esp32_st7789_microui_Shtc3_normal

esp32_st7789_microui_Shtc3_error

TobiasIvanSergeevich and others added 16 commits July 16, 2026 09:03
…/sensirion.com/products/catalog/SHTC3 ). Communication via the I2C bus. Pin support for sensor power management.
Add HWSPI driver, that support on ESP8266 platform only HSPI.
To start driver add to command line "startDriver HWSPI". On start
driver have registred on SPPIBUS interface. Any SPI-device can attach to
spi-bus, gives bus name, eg. "hspi".
Add ST7789 driver for TFT-display vis spi interface. Can attach to spi-bus
by this name. Eg. for platform ESP8266 - "hspi".
To start driver add to command line
"startDriver ST7789 [SPI_NAME] [CS_PIN] [CS_LEVEL] [DC_PIN] [RESET_PIN]
[DYSP_WIDTH] [DYSP_HEIGHT] [DYSP_W_OFS] [DYSP_H_OFS] [ROTATION]", where
[SPI_NAME] - spi bus hame, eg. "hspi"
[CS_PIN] - chip select line, eg. "IO12" or "NA" if not use
[CS_LEVEL] - chip select line level, not implemented yet
[DC_PIN] - data/command line, eg. "IO15"
[RS_PIN] - reset line, eg. "IO2" or "NA" if not use
[DYSP_WIDTH] - actual dislay width, eg.135
[DYSP_HEIGHT] - actual dislay height, eg.240
[DYSP_W_OFS] - width, offset, eg.52
[DYSP_H_OFS] - height offset, eg.40
[ROTATION] - display rotation: 0, 90, 270, 360, not implemented yet.
The main functions of the microui core are in the file drv_microui_core.c.
The rendering functions for basic primitives are in the drv_microui_renderer.c file.
The interface for interacting with the graphics driver is described in the drv_idisplay.h file.
General interaction and functions of the obk driver are in the drv_microui.c file:
Launch: startDriver MicroUI st7789
st7789 is the name of the graphics driver that has the obk_display_renderer_t interface.
The reading of the GUI from the microui.json file has been implemented. Due to lack of memory on the ESP8266 platform, the jsmn_stream streaming reader module has been added.c
In the cJSON module.c in the JSON_SetValuestring function(cJSON added the valuestring check for NULL
The following gui objects are implemented:
- window
- label; you can specify the name of the channel, the value of which will be displayed in the text. Teskt should
be a string with formatting.
- button; you can specify the name of the command that is executed when the button is clicked.
- header;
- layout, including layoutset (arbitrary region), width, height, and row.
- icon; you can specify the channel name and different icons for the ON and OFF states.
To control button presses, the OnKeyPress command is available.
The command can be linked to obk events such as OnClick, OnHold, OnDoubleClick, etc.
The focus is transferred to the required element when the TAB key is pressed.
Pressing the button is performed when the ENTER key is pressed.
Example for a physical button at input IO0:
addEventHandler OnClick 0 OnKeyPress TAB
addEventHandler OnHold 0 OnKeyPress ENTER

Modification of the SHTC3 driver:
- initialization of the pulse time via the I2C bus has been added;
- the error in calculating the CRC has been fixed;
- an installation command has been added that allows specifying the channel for saving the measurement error
(can be used for display indication)

Modification of the new_pins.c module.
Special channels have been added:
SPECIAL_CHANNEL_WIFICONNECTED   139
SPECIAL_CHANNEL_MQTTCONNECTED   140
indicating a connection to Wi‑Fi and MQTT, respectively. Used for display.
@TobiasIvanSergeevich
TobiasIvanSergeevich marked this pull request as ready for review September 24, 2026 11:40
@TobiasIvanSergeevich
TobiasIvanSergeevich marked this pull request as draft September 25, 2026 09:50
A result of calling the doHomeAssistantDiscovery method, the heap size
decreases by more than 10 kB. On the esp8266 platform this leads to a
critical memory level, which causes the http and mqtt modules to stop
working. An error related to memory allocation is visible in the logs.
As a result, the information required for HA Discovery is not published.

Now it runs in separate task and wait while actual sending every peace
of information will complete.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant