From c80bec1703a41ce6001cb26a13a8581ed9072578 Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Thu, 24 Oct 2024 15:18:53 +0800 Subject: [PATCH] Add a simple example https://github.com/Xinyuan-LilyGO/T-Deck/issues/59 --- examples/HelloWorld/HelloWorld.ino | 130 +++++++++++++++++++++++++++++ examples/HelloWorld/utilities.h | 85 +++++++++++++++++++ 2 files changed, 215 insertions(+) create mode 100644 examples/HelloWorld/HelloWorld.ino create mode 100644 examples/HelloWorld/utilities.h diff --git a/examples/HelloWorld/HelloWorld.ino b/examples/HelloWorld/HelloWorld.ino new file mode 100644 index 0000000..d472d2f --- /dev/null +++ b/examples/HelloWorld/HelloWorld.ino @@ -0,0 +1,130 @@ +#include +#include +#include +#include "utilities.h" + +typedef struct { + uint8_t cmd; + uint8_t data[14]; + uint8_t len; +} lcd_cmd_t; + +lcd_cmd_t lcd_st7789v[] = { + {0x01, {0}, 0 | 0x80}, + {0x11, {0}, 0 | 0x80}, + {0x3A, {0X05}, 1}, + {0x36, {0x55}, 1}, + {0xB2, {0x0C, 0x0C, 0X00, 0X33, 0X33}, 5}, + {0xB7, {0X75}, 1}, + {0xBB, {0X1A}, 1}, + {0xC0, {0X2C}, 1}, + {0xC2, {0X01}, 1}, + {0xC3, {0X13}, 1}, + {0xC4, {0X20}, 1}, + {0xC6, {0X0F}, 1}, + {0xD0, {0XA4, 0XA1}, 2}, + {0xD6, {0XA1}, 1}, + {0xE0, {0XD0, 0X0D, 0X14, 0X0D, 0X0D, 0X09, 0X38, 0X44, 0X4E, 0X3A, 0X17, 0X18, 0X2F, 0X30}, 14}, + {0xE1, {0XD0, 0X09, 0X0F, 0X08, 0X07, 0X14, 0X37, 0X44, 0X4D, 0X38, 0X15, 0X16, 0X2C, 0X3E}, 14}, + {0x21, {0}, 0}, //invertDisplay + {0x29, {0}, 0}, + {0x2C, {0}, 0}, +}; + +TFT_eSPI tft; + +// LilyGo T-Deck control backlight chip has 16 levels of adjustment range +// The adjustable range is 0~15, 0 is the minimum brightness, 16 is the maximum brightness +void setBrightness(uint8_t value) +{ + static uint8_t level = 0; + static uint8_t steps = 16; + if (value == 0) { + digitalWrite(BOARD_BL_PIN, 0); + delay(3); + level = 0; + return; + } + if (level == 0) { + digitalWrite(BOARD_BL_PIN, 1); + level = steps; + delayMicroseconds(30); + } + int from = steps - level; + int to = steps - value; + int num = (steps + to - from) % steps; + for (int i = 0; i < num; i++) { + digitalWrite(BOARD_BL_PIN, 0); + digitalWrite(BOARD_BL_PIN, 1); + } + level = value; +} + + +void setup() +{ + Serial.begin(115200); + + Serial.println("T-DECK factory"); + + //! The board peripheral power control pin needs to be set to HIGH when using the peripheral + pinMode(BOARD_POWERON, OUTPUT); + digitalWrite(BOARD_POWERON, HIGH); + + //! Set CS on all SPI buses to high level during initialization + pinMode(BOARD_SDCARD_CS, OUTPUT); + pinMode(RADIO_CS_PIN, OUTPUT); + pinMode(BOARD_TFT_CS, OUTPUT); + + digitalWrite(BOARD_SDCARD_CS, HIGH); + digitalWrite(RADIO_CS_PIN, HIGH); + digitalWrite(BOARD_TFT_CS, HIGH); + + pinMode(BOARD_SPI_MISO, INPUT_PULLUP); + SPI.begin(BOARD_SPI_SCK, BOARD_SPI_MISO, BOARD_SPI_MOSI); //SD + + Serial.print("Init display id:"); + Serial.println(USER_SETUP_ID); + + tft.begin(); + + /** + * * T-Deck-Plus and T-Deck display panels are different. + * * This initialization is used to override the initialization parameters. + * * It is used when the display is abnormal after the TFT_eSPI update. */ +#if 0 + for (uint8_t i = 0; i < (sizeof(lcd_st7789v) / sizeof(lcd_cmd_t)); i++) { + tft.writecommand(lcd_st7789v[i].cmd); + for (int j = 0; j < (lcd_st7789v[i].len & 0x7f); j++) { + tft.writedata(lcd_st7789v[i].data[j]); + } + + if (lcd_st7789v[i].len & 0x80) { + delay(120); + } + } +#endif + + tft.setRotation( 1 ); + tft.fillScreen(TFT_BLACK); + + tft.setTextDatum(MC_DATUM); + tft.setFreeFont(&FreeSansOblique12pt7b); + tft.drawString("Hello World", TFT_WIDTH / 2, TFT_HEIGHT / 2); + + + pinMode(BOARD_BL_PIN, OUTPUT); + for (int i = 0; i <= 16; ++i) { + setBrightness(i); + delay(30); + } +} + +void loop() +{ + tft.setTextColor(random(0xFFFF)); + tft.fillScreen(TFT_BLACK); + tft.drawString("Hello World", random(5, 320), random(5, 240)); + delay(5000); +} + diff --git a/examples/HelloWorld/utilities.h b/examples/HelloWorld/utilities.h new file mode 100644 index 0000000..3ca5aa7 --- /dev/null +++ b/examples/HelloWorld/utilities.h @@ -0,0 +1,85 @@ +/** + * @file utilities.h + * @author Lewis He (lewishe@outlook.com) + * @license MIT + * @copyright Copyright (c) 2023 Shenzhen Xin Yuan Electronic Technology Co., Ltd + * @date 2023-04-11 + * + */ +#pragma once + + + +//! The board peripheral power control pin needs to be set to HIGH when using the peripheral +#define BOARD_POWERON 10 + +#define BOARD_I2S_WS 5 +#define BOARD_I2S_BCK 7 +#define BOARD_I2S_DOUT 6 + +#define BOARD_I2C_SDA 18 +#define BOARD_I2C_SCL 8 + +#define BOARD_BAT_ADC 4 + +#define BOARD_TOUCH_INT 16 +#define BOARD_KEYBOARD_INT 46 + +#define BOARD_SDCARD_CS 39 +#define BOARD_TFT_CS 12 +#define RADIO_CS_PIN 9 + +#define BOARD_TFT_DC 11 +#define BOARD_TFT_BACKLIGHT 42 + +#define BOARD_SPI_MOSI 41 +#define BOARD_SPI_MISO 38 +#define BOARD_SPI_SCK 40 + +#define BOARD_TBOX_G02 2 +#define BOARD_TBOX_G01 3 +#define BOARD_TBOX_G04 1 +#define BOARD_TBOX_G03 15 + +#define BOARD_ES7210_MCLK 48 +#define BOARD_ES7210_LRCK 21 +#define BOARD_ES7210_SCK 47 +#define BOARD_ES7210_DIN 14 + +#define RADIO_BUSY_PIN 13 +#define RADIO_RST_PIN 17 +#define RADIO_DIO1_PIN 45 + +#define BOARD_BOOT_PIN 0 + +#define BOARD_BL_PIN 42 + + +#define BOARD_GPS_TX_PIN 43 +#define BOARD_GPS_RX_PIN 44 + + +#ifndef RADIO_FREQ +#define RADIO_FREQ 868.0 +#endif + +#ifndef RADIO_BANDWIDTH +#define RADIO_BANDWIDTH 125.0 +#endif + +#ifndef RADIO_SF +#define RADIO_SF 10 +#endif + +#ifndef RADIO_CR +#define RADIO_CR 6 +#endif + +#ifndef RADIO_TX_POWER +#define RADIO_TX_POWER 22 +#endif + +#define DEFAULT_OPA 100 + + +