Hey everyone!
I'm on the NC State SolarPack solar car team and we're building a custom instrument cluster display for our vehicle using a Particle Photon 2 and a 4.0" ST7796S 480×320 TFT display. When I went looking for a native ST7796S library for Particle, I found nothing. The closest option was using Adafruit_ILI9341 as a stand-in, which has initialization differences and drags in the entire Adafruit GFX stack as a dependency.
So I wrote one from scratch. It's now public on GitHub and I wanted to share it here in case anyone else runs into the same wall.
What it does:
Full SPI display driver for the ST7796S, no external dependencies beyond Particle firmware. Everything you'd expect:
-
fillScreen,fillRect,drawRect,drawBorder,drawRoundRect,fillRoundRect -
drawLine,drawHLine,drawVLine,drawCircle,fillCircle -
drawTriangle,fillTriangle -
Scaled bitmap text (5×7 font, any integer scale) with optional background color fill
-
print(string),print(int),print(float),println() -
textWidth()/textHeight()for layout math -
color565(r, g, b)andblendColor()utilities -
Arc gauge helper (
drawGauge) for dashboard-style displays -
Proper bounds checking on all draw calls — no out-of-bounds SPI writes
Tested hardware
-
Particle Photon 2
-
4.0" TFT SPI 480×320 V1.0 (MSP4031) — ST7796S driver IC
Quick example
cpp
#include "ST7796S.h"
ST7796S tft(D5, D7, D6); // CS, DC, RST
void setup() {
tft.begin();
tft.setRotation(0);
tft.fillScreen(ST7796_BLACK);
tft.setTextColor(ST7796_ORANGE, ST7796_BLACK);
tft.setTextSize(4);
tft.setCursor(60, 130);
tft.print("Hello Photon 2!");
}
One important hardware note
If you're using this specific module and your colors look inverted (red appears cyan, green appears magenta), the culprit is command 0x21 (Display Inversion ON) in the init sequence. Some example code floating around sends it (it should not be sent for this module). The library handles this correctly.
Links
-
Module datasheet/wiki: https://www.lcdwiki.com/4.0inch_Capacitive_SPI_Module_ST7796
Happy to answer questions or take PRs. This is my first published embedded library so feedback is welcome.
— Estyntech / NC State SolarPack