Skip to content

Commit ea13fa8

Browse files
authored
Merge pull request #814 from WattleFoxxo/tdeck
LilyGo TDeck support
2 parents 4aa58ad + f100894 commit ea13fa8

File tree

8 files changed

+520
-0
lines changed

8 files changed

+520
-0
lines changed

boards/t-deck.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"build": {
3+
"arduino": {
4+
"ldscript": "esp32s3_out.ld",
5+
"partitions": "default_16MB.csv",
6+
"memory_type": "qio_opi"
7+
},
8+
"core": "esp32",
9+
"extra_flags": [
10+
"-DARDUINO_USB_MODE=1",
11+
"-DARDUINO_RUNNING_CORE=1",
12+
"-DARDUINO_EVENT_RUNNING_CORE=1"
13+
],
14+
"f_cpu": "240000000L",
15+
"f_flash": "80000000L",
16+
"flash_mode": "qio",
17+
"hwids": [["0x303A", "0x1001"]],
18+
"mcu": "esp32s3",
19+
"variant": "esp32s3"
20+
},
21+
"connectivity": ["wifi", "bluetooth"],
22+
"debug": {
23+
"default_tool": "esp-builtin",
24+
"onboard_tools": ["esp-builtin"],
25+
"openocd_target": "esp32s3.cfg"
26+
},
27+
"frameworks": ["arduino", "espidf"],
28+
"name": "LilyGo T-Deck (16M Flash 8M PSRAM)",
29+
"upload": {
30+
"flash_size": "16MB",
31+
"maximum_ram_size": 327680,
32+
"maximum_size": 16777216,
33+
"require_upload_port": true,
34+
"speed": 921600
35+
},
36+
"url": "https://www.lilygo.cc",
37+
"vendor": "LilyGo"
38+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#include "ST7789LCDDisplay.h"
2+
3+
#ifndef DISPLAY_ROTATION
4+
#define DISPLAY_ROTATION 3
5+
#endif
6+
7+
#ifndef DISPLAY_SCALE_X
8+
#define DISPLAY_SCALE_X 2.5f // 320 / 128
9+
#endif
10+
11+
#ifndef DISPLAY_SCALE_Y
12+
#define DISPLAY_SCALE_Y 3.75f // 240 / 64
13+
#endif
14+
15+
#define DISPLAY_WIDTH 240
16+
#define DISPLAY_HEIGHT 320
17+
18+
bool ST7789LCDDisplay::i2c_probe(TwoWire& wire, uint8_t addr) {
19+
return true;
20+
}
21+
22+
bool ST7789LCDDisplay::begin() {
23+
if (!_isOn) {
24+
if (_peripher_power) _peripher_power->claim();
25+
26+
pinMode(PIN_TFT_LEDA_CTL, OUTPUT);
27+
digitalWrite(PIN_TFT_LEDA_CTL, HIGH);
28+
digitalWrite(PIN_TFT_RST, HIGH);
29+
30+
// Im not sure if this is just a t-deck problem or not, if your display is slow try this.
31+
#ifdef LILYGO_TDECK
32+
displaySPI.begin(PIN_TFT_SCL, -1, PIN_TFT_SDA, PIN_TFT_CS);
33+
#endif
34+
35+
display.init(DISPLAY_WIDTH, DISPLAY_HEIGHT);
36+
display.setRotation(DISPLAY_ROTATION);
37+
38+
display.setSPISpeed(40e6);
39+
40+
display.fillScreen(ST77XX_BLACK);
41+
display.setTextColor(ST77XX_WHITE);
42+
display.setTextSize(2);
43+
display.cp437(true); // Use full 256 char 'Code Page 437' font
44+
45+
_isOn = true;
46+
}
47+
48+
return true;
49+
}
50+
51+
void ST7789LCDDisplay::turnOn() {
52+
ST7789LCDDisplay::begin();
53+
}
54+
55+
void ST7789LCDDisplay::turnOff() {
56+
if (_isOn) {
57+
digitalWrite(PIN_TFT_LEDA_CTL, HIGH);
58+
digitalWrite(PIN_TFT_RST, LOW);
59+
digitalWrite(PIN_TFT_LEDA_CTL, LOW);
60+
_isOn = false;
61+
62+
if (_peripher_power) _peripher_power->release();
63+
}
64+
}
65+
66+
void ST7789LCDDisplay::clear() {
67+
display.fillScreen(ST77XX_BLACK);
68+
}
69+
70+
void ST7789LCDDisplay::startFrame(Color bkg) {
71+
display.fillScreen(ST77XX_BLACK);
72+
display.setTextColor(ST77XX_WHITE);
73+
display.setTextSize(1); // This one affects size of Please wait... message
74+
display.cp437(true); // Use full 256 char 'Code Page 437' font
75+
}
76+
77+
void ST7789LCDDisplay::setTextSize(int sz) {
78+
display.setTextSize(sz);
79+
}
80+
81+
void ST7789LCDDisplay::setColor(Color c) {
82+
switch (c) {
83+
case DisplayDriver::DARK :
84+
_color = ST77XX_BLACK;
85+
break;
86+
case DisplayDriver::LIGHT :
87+
_color = ST77XX_WHITE;
88+
break;
89+
case DisplayDriver::RED :
90+
_color = ST77XX_RED;
91+
break;
92+
case DisplayDriver::GREEN :
93+
_color = ST77XX_GREEN;
94+
break;
95+
case DisplayDriver::BLUE :
96+
_color = ST77XX_BLUE;
97+
break;
98+
case DisplayDriver::YELLOW :
99+
_color = ST77XX_YELLOW;
100+
break;
101+
case DisplayDriver::ORANGE :
102+
_color = ST77XX_ORANGE;
103+
break;
104+
default:
105+
_color = ST77XX_WHITE;
106+
break;
107+
}
108+
display.setTextColor(_color);
109+
}
110+
111+
void ST7789LCDDisplay::setCursor(int x, int y) {
112+
display.setCursor(x * DISPLAY_SCALE_X, y * DISPLAY_SCALE_Y);
113+
}
114+
115+
void ST7789LCDDisplay::print(const char* str) {
116+
display.print(str);
117+
}
118+
119+
void ST7789LCDDisplay::fillRect(int x, int y, int w, int h) {
120+
display.fillRect(x * DISPLAY_SCALE_X, y * DISPLAY_SCALE_Y, w * DISPLAY_SCALE_X, h * DISPLAY_SCALE_Y, _color);
121+
}
122+
123+
void ST7789LCDDisplay::drawRect(int x, int y, int w, int h) {
124+
display.drawRect(x * DISPLAY_SCALE_X, y * DISPLAY_SCALE_Y, w * DISPLAY_SCALE_X, h * DISPLAY_SCALE_Y, _color);
125+
}
126+
127+
void ST7789LCDDisplay::drawXbm(int x, int y, const uint8_t* bits, int w, int h) {
128+
display.drawBitmap(x * DISPLAY_SCALE_X, y * DISPLAY_SCALE_Y, bits, w, h, _color);
129+
}
130+
131+
uint16_t ST7789LCDDisplay::getTextWidth(const char* str) {
132+
int16_t x1, y1;
133+
uint16_t w, h;
134+
display.getTextBounds(str, 0, 0, &x1, &y1, &w, &h);
135+
136+
return w / DISPLAY_SCALE_X;
137+
}
138+
139+
void ST7789LCDDisplay::endFrame() {
140+
// display.display();
141+
}

src/helpers/ui/ST7789LCDDisplay.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#pragma once
2+
3+
#include "DisplayDriver.h"
4+
#include <Wire.h>
5+
#include <SPI.h>
6+
#include <Adafruit_GFX.h>
7+
#include <Adafruit_ST7789.h>
8+
#include <helpers/RefCountedDigitalPin.h>
9+
10+
class ST7789LCDDisplay : public DisplayDriver {
11+
#ifdef LILYGO_TDECK
12+
SPIClass displaySPI;
13+
#endif
14+
Adafruit_ST7789 display;
15+
bool _isOn;
16+
uint16_t _color;
17+
RefCountedDigitalPin* _peripher_power;
18+
19+
bool i2c_probe(TwoWire& wire, uint8_t addr);
20+
public:
21+
#ifdef USE_PIN_TFT
22+
ST7789LCDDisplay(RefCountedDigitalPin* peripher_power=NULL) : DisplayDriver(128, 64),
23+
display(PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_SDA, PIN_TFT_SCL, PIN_TFT_RST),
24+
_peripher_power(peripher_power)
25+
{
26+
_isOn = false;
27+
}
28+
#elif LILYGO_TDECK
29+
ST7789LCDDisplay(RefCountedDigitalPin* peripher_power=NULL) : DisplayDriver(128, 64),
30+
displaySPI(HSPI),
31+
display(&displaySPI, PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_RST),
32+
_peripher_power(peripher_power)
33+
{
34+
_isOn = false;
35+
}
36+
#else
37+
ST7789LCDDisplay(RefCountedDigitalPin* peripher_power=NULL) : DisplayDriver(128, 64),
38+
display(&SPI, PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_RST),
39+
_peripher_power(peripher_power)
40+
{
41+
_isOn = false;
42+
}
43+
#endif
44+
bool begin();
45+
46+
bool isOn() override { return _isOn; }
47+
void turnOn() override;
48+
void turnOff() override;
49+
void clear() override;
50+
void startFrame(Color bkg = DARK) override;
51+
void setTextSize(int sz) override;
52+
void setColor(Color c) override;
53+
void setCursor(int x, int y) override;
54+
void print(const char* str) override;
55+
void fillRect(int x, int y, int w, int h) override;
56+
void drawRect(int x, int y, int w, int h) override;
57+
void drawXbm(int x, int y, const uint8_t* bits, int w, int h) override;
58+
uint16_t getTextWidth(const char* str) override;
59+
void endFrame() override;
60+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <Arduino.h>
2+
#include "TDeckBoard.h"
3+
4+
uint32_t deviceOnline = 0x00;
5+
6+
void TDeckBoard::begin() {
7+
8+
ESP32Board::begin();
9+
10+
// Enable peripheral power
11+
pinMode(PIN_PERF_POWERON, OUTPUT);
12+
digitalWrite(PIN_PERF_POWERON, HIGH);
13+
14+
// Configure user button
15+
pinMode(PIN_USER_BTN, INPUT);
16+
17+
// Configure LoRa Pins
18+
pinMode(P_LORA_MISO, INPUT_PULLUP);
19+
// pinMode(P_LORA_DIO_1, INPUT_PULLUP);
20+
21+
#ifdef P_LORA_TX_LED
22+
digitalWrite(P_LORA_TX_LED, HIGH); // inverted pin for SX1276 - HIGH for off
23+
#endif
24+
25+
esp_reset_reason_t reason = esp_reset_reason();
26+
if (reason == ESP_RST_DEEPSLEEP) {
27+
long wakeup_source = esp_sleep_get_ext1_wakeup_status();
28+
if (wakeup_source & (1 << P_LORA_DIO_1)) {
29+
startup_reason = BD_STARTUP_RX_PACKET; // received a LoRa packet (while in deep sleep)
30+
}
31+
32+
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
33+
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
34+
}
35+
}

variants/lilygo_tdeck/TDeckBoard.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#pragma once
2+
3+
#include <Wire.h>
4+
#include <Arduino.h>
5+
#include "helpers/ESP32Board.h"
6+
#include <driver/rtc_io.h>
7+
8+
#define PIN_VBAT_READ 4
9+
#define BATTERY_SAMPLES 8
10+
#define ADC_MULTIPLIER (2.0f * 3.3f * 1000)
11+
12+
class TDeckBoard : public ESP32Board {
13+
public:
14+
void begin();
15+
16+
#ifdef P_LORA_TX_LED
17+
void onBeforeTransmit() override{
18+
digitalWrite(P_LORA_TX_LED, LOW); // turn TX LED on - invert pin for SX1276
19+
}
20+
21+
void onAfterTransmit() override{
22+
digitalWrite(P_LORA_TX_LED, HIGH); // turn TX LED off - invert pin for SX1276
23+
}
24+
#endif
25+
26+
void enterDeepSleep(uint32_t secs, int pin_wake_btn) {
27+
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
28+
29+
// Make sure the DIO1 and NSS GPIOs are hold on required levels during deep sleep
30+
rtc_gpio_set_direction((gpio_num_t)P_LORA_DIO_1, RTC_GPIO_MODE_INPUT_ONLY);
31+
rtc_gpio_pulldown_en((gpio_num_t)P_LORA_DIO_1);
32+
33+
rtc_gpio_hold_en((gpio_num_t)P_LORA_NSS);
34+
35+
if (pin_wake_btn < 0) {
36+
esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet
37+
} else {
38+
esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1) | (1L << pin_wake_btn), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet OR wake btn
39+
}
40+
41+
if (secs > 0) {
42+
esp_sleep_enable_timer_wakeup(secs * 1000000);
43+
}
44+
45+
// Finally set ESP32 into sleep
46+
esp_deep_sleep_start(); // CPU halts here and never returns!
47+
}
48+
49+
uint16_t getBattMilliVolts() {
50+
#if defined(PIN_VBAT_READ) && defined(ADC_MULTIPLIER)
51+
analogReadResolution(12);
52+
53+
uint32_t raw = 0;
54+
for (int i = 0; i < BATTERY_SAMPLES; i++) {
55+
raw += analogRead(PIN_VBAT_READ);
56+
}
57+
58+
raw = raw / BATTERY_SAMPLES;
59+
return (ADC_MULTIPLIER * raw) / 4096;
60+
#else
61+
return 0;
62+
#endif
63+
}
64+
65+
const char* getManufacturerName() const{
66+
return "LilyGo T-Deck";
67+
}
68+
};

0 commit comments

Comments
 (0)