
本文来源:http://www.lpqz.cn/news/106563
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。
1. 项目缘起与整体设计思路1.1 为什么选择小尺寸彩屏做翻页时钟翻页时钟这个东西玩嵌入式的人多多少少都动过心思。机械翻页结构复杂、噪音大、零件磨损快做出来好看但维护成本高。用彩屏模拟翻页效果既保留了那种复古的视觉冲击力又省去了机械结构的麻烦。我前后做过三版第一版用Arduino Uno加2.4寸TFT第二版换ESP32加2.8寸IPS第三版定型在ESP32-C3配1.69寸圆角彩屏体积缩到巴掌大放在桌面上刚刚好。选小尺寸彩屏有几个实际考量。桌面空间有限3.5寸以上的屏幕放桌上显得笨重1.5到2寸的屏幕配合合适的边框整体尺寸能控制在6厘米以内。功耗方面小屏背光电流通常在20到40毫安比大屏省不少用USB供电或者小容量锂电池都能撑住。分辨率上240×280或者240×320这个级别足够显示时分秒翻页动画也不会因为像素太多而卡顿。核心需求其实就三条走时准、翻页动画流畅、外观简洁。走时准靠ESP
32的WiFi对时或者外接RTC模块翻页流畅靠TFT_eSPI库的Sprite双缓冲机制外观简洁靠合理的UI布局和外壳设计。这三条听起来简单实际做的时候每一块都有坑。1.2 硬件选型背后的取舍逻辑主控选ESP32而不是Arduino Uno原因很直接。Uno的SRAM只有2KB驱动彩屏做全屏刷新根本不够用更别说双缓冲了。ESP32有520KB SRAM随便划一块240×280×2字节的缓冲区也就134KB还剩一大半给程序跑。另外ESP32自带WiFiNTP对时不需要额外模块省了硬件成本和接线麻烦。屏幕我试过好几款。ILI9341驱动芯片的2.4寸屏最便宜但可视角度一般斜着看偏色明显。ST7789驱动的1.69寸IPS屏贵一些但色彩和可视角度好很多而且圆角设计不用自己切外壳。最终选了ST7789240×280分辨率SPI接口接线简单。电源部分如果用USB供电就直接从开发板的5V取电屏幕背光串
一个33欧姆电阻限流。如果要便携加一块3.7V锂电池和TP4056充电模块升压到5V给屏幕和主控供电。实测整机工作电流在80到120毫安之间1000毫安时的电池能撑8小时左右。注意ESP32的GPIO输出电流有限屏幕背光不要直接接GPIO必须通过三极管或者MOS管驱动否则烧引脚是迟早的事。1.3 软件框架的搭建思路软件层面我用了Arduino框架没上ESP-IDF。原因很简单TFT_eSPI库在Arduino下成熟稳定翻页动画的Sprite功能开箱即用ESP-IDF下虽然也能跑LVGL但配置复杂度高不少对于这种小项目没必要。整体软件架构分三层。底层是硬件驱动层负责屏幕初始化、SPI通信、RTC读写。中间是逻辑层处理时间获取、翻页状态机、动画帧计算。上层是UI层管理数字的绘制、颜色方案、过渡效果。三层之间通过简单的函数调用和全局状态变量通信没有上RTOS因为任务不复杂一个主循环加定时器中断就
够了。翻页动画的核心思路是把每个数字拆成上下两半上半部分向下翻转下半部分向上翻转中间用渐变和阴影模拟立体感。具体实现时每个数字用两个Sprite一个存上半部分一个存下半部分翻转过程中动态计算每个像素的Y轴偏移和亮度衰减。这部分后面会详细展开。2. 核心细节解析与实操要点2.1 屏幕驱动与SPI通信配置ST7789这块屏用SPI接口接线就那几根SCLK、MOSI、CS、DC、RST、BLK。ESP32的硬件SPI引脚可以映射到任意GPIO但为了稳定我固定在VSPI上SCLK接GPIO18MOSI接GPIO23CS接GPIO15DC接GPIO2RST接GPIO4BLK接GPIO5。接线尽量短超过10厘米就容易出现花屏或者刷新撕裂。TFT_eSPI库的配置在User_Setup.h文件里改不用动库源码。关键配置项如下#define ST7789_DRIVER #define TFT_WIDTH
240 #define TFT_HEIGHT 280 #define TFT_MOSI 23 #define TFT_SCLK 18 #define TFT_CS 15 #define TFT_DC 2 #define TFT_RST 4 #define TFT_BL 5 #define LOAD_GLCD #define SPI_FREQUENCY 40000000SPI频率设40MHz实测下来很稳再高到80MHz会出现偶发花屏。如果你的接线比较长或者用了杜邦线建议降到20MHz先跑通再往上调。提示TFT_eSPI库的User_Setup.h每次更新库都会被覆盖建议把配置单独放到User_Setup_Select.h里引用自定义文件或者直接备份一份。初始化流程没什么特别的tft.init()之后设置旋转方向、填充黑屏、打开背光。这里有个细节背光打开前先填黑屏否则会闪一下白屏观感很差。2.
2 时间获取方案NTP与RTC双保险时间源我用了两套。主用NTPESP32连上WiFi后向pool.ntp.org请求时间同步到内部RTC。备用是DS3231外部RTC模块I2C接口精度比ESP32内部RTC高一个数量级。为什么两套都要因为WiFi不是每次都连得上尤其是路由器重启或者密码改了之后没有外部RTC的话时间就丢了重新对时又得等WiFi。NTP对时的代码用configTime函数设置时区和NTP服务器configTime(8 * 3600, 0, pool.ntp.org, time.nist.gov); struct tm timeinfo; if (getLocalTime(timeinfo)) { // 同步到DS3231 rtc.adjust(DateTime(timeinfo.tm_year 1900, timeinfo.tm_mon 1, timeinfo.tm_m
day, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec)); }DS3231的读写用RTClib库I2C地址0x68SDA接GPIO21SCL接GPIO22。DS3231自带温度补偿晶振年误差在2分钟以内比ESP32内部RTC的精度好太多。ESP32内部RTC在断电后完全丢失DS3231有纽扣电池断电也能走。实际运行逻辑是开机先读DS3231如果时间有效就直接用然后尝试连WiFi连上了就NTP对时并写回DS3231连不上就继续用DS3231的时间。这样无论有没有网络时间都不会差太多。2.3 翻页动画的Sprite双缓冲实现翻页动画是这个小项目的灵魂也是最耗内存和CPU的部分。TFT_eSPI的Sprite机制允许你在内存里画好一整帧然后一次性推送到屏幕避免闪烁。每个数字我用了两个Sprite分别存上半部分和下半部分尺寸是60×80像素
16位色深每个Sprite占60×80×29600字节两个就是19.2KB。六个数字时分秒各两位一共12个Sprite总共230KB左右ESP32的520KB SRAM扛得住。动画的数学原理不复杂。假设翻转角度从0度到180度上半部分绕底边旋转下半部分绕顶边旋转。对于上半部分Y轴缩放系数是cos(角度)当角度从0到90度时cos从1到0上半部分逐渐压扁下半部分同理但方向相反。同时亮度随角度衰减模拟光照变化。float angle progress * PI; // 0到PI float scaleY cos(angle); uint8_t brightness 255 * (1.0 - progress * 0.7);实际绘制时不是真的做3D旋转而是逐行扫描原Sprite根据scaleY计算目标行然后写入新Sprite。这个过程用pushImage和自定义的像素操作函数实现。为了性能
我只在动画帧变化时重绘静止时直接显示静态SpriteCPU占用率从40%降到5%以下。注意Sprite数量多的时候内存碎片是个问题建议在setup里一次性创建所有Sprite不要动态创建销毁否则跑几个小时就可能因为内存碎片崩溃。2.4 颜色方案与UI布局颜色方案我试过好几套。纯白数字配黑底最经典但看久了单调。后来改成渐变蓝到紫配合深灰背景晚上看着不刺眼。数字用抗锯齿字体TFT_eSPI自带FreeFonts选FreeSansBold24pt显示效果比默认字体好很多。布局上时分秒三组数字横向排列中间用冒号分隔。冒号用两个小圆点每秒闪烁一次。屏幕顶部留一条细线显示日期和星期底部留一条显示WiFi状态和电池电量。整体留白要够数字不要顶到屏幕边缘否则外壳一装就遮住了。字体大小根据屏幕分辨率调。240×280的屏数字高度80像素宽度60像素三组数字加两个冒号总共60×620×2400像素超了。所以要
么缩小数字要么改成两行显示。我最后选了上下两行上面一行显示时和分下面一行显示秒和日期。这样每个数字可以做到80×100像素视觉冲击力更强。3. 实操过程与核心环节实现3.1 硬件焊接与组装步骤焊接顺序很重要先低后高先小后大。第一步焊ESP32开发板的排针注意方向不要焊反。第二步焊屏幕的FPC座或者排针ST7789模块通常已经焊好了排针直接插杜邦线就行。第三步焊DS3231模块的排针。第四步焊TP4056充电模块和电池座。如果要做便携版电池用603048规格的1000mAh锂电池尺寸刚好塞进3D打印外壳。充电模块的B和B-接电池OUT和OUT-接升压模块或者直接接ESP32的5V引脚ESP32开发板自带LDO3.7V输入也能工作但屏幕背光可能偏暗。外壳我用Fusion 360画的屏幕开窗尺寸比可视区域大0.5毫米避免装配误差导致遮边。后盖留了USB开口和几个散热孔。3D打印用PETG材料比P
LA耐温好一些夏天不容易变形。提示焊接FPC座的时候烙铁温度不要超过350度接触时间不要超过3秒否则塑料座子会熔化。3.2 软件环境搭建与库安装Arduino IDE我用的是2.3.2版本ESP32开发板支持包用 Boards Manager 安装选 esp32 by Espressif Systems版本2.0.14。这个版本比较稳定3.0以上的版本API有变动TFT_eSPI可能不兼容。需要安装的库TFT_eSPI最新版RTClib by AdafruitWiFiESP32自带time.hESP32自带安装完TFT_eSPI后找到库目录下的User_Setup.h按照前面说的配置改好。然后编译一个简单的测试程序确认屏幕能点亮、能显示颜色。这一步不过后面都白搭。测试代码#include TFT_eSPI.h TFT_eSPI tft TFT_eSPI(); void setup() {
tft.init(); tft.setRotation(0); tft.fillScreen(TFT_BLACK); tft.setTextColor(TFT_WHITE); tft.setTextSize(2); tft.setCursor(20, 100); tft.println(Hello TFT); } void loop() {}如果屏幕显示Hello TFT说明SPI通信和驱动配置都对了。如果花屏或者白屏先检查接线再降SPI频率最后检查User_Setup.h的引脚定义。3.3 翻页动画的完整代码实现翻页动画的核心函数是drawFlipDigit接收数字、位置、进度三个参数。内部逻辑分四步读取当前数字的Sprite、计算翻转缩放、生成中间帧、推送到屏幕。void drawFlipDigit(int digit, int x, int y, float progress) {
nt16_t newColor (r 11) | (g 5) | b; tft.drawPixel(x col, y row, newColor); } } // 下半部分同理方向相反 }实际跑的时候每帧计算量不小240×280的屏幕全屏刷新一次大概需要15毫秒翻页动画持续300毫秒大概20帧每帧15毫秒总共300毫秒刚好匹配。如果觉得卡可以降低帧数到15帧或者只刷新变化的区域。注意readPixel和drawPixel是逐像素操作速度慢。优化方法是直接用pushImage推送整块区域但需要提前把缩放后的图像画到临时Sprite里。我实测下来逐像素在ESP32上跑20帧没问题CPU占用率在60%左右不影响WiFi和RTC任务。3.4 WiFi对时与断网处理WiFi连接用ESP32的WiFi库设置自动重连。连接成功后启动NTP对时对时成功写回DS3231。断网后不阻塞主循环继续用D

本文来源:http://www.lpqz.cn/news/106563
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。
1. Project Origin and Overall Design Concept 1.1 Why Choose a Small Color Screen for a Flip Clock? Most people who work with embedded systems have considered making a flip clock at some point. Mechanical flip clocks have a complex structure, are noisy, and their parts wear out quickly. They look nice when finished, but the maintenance costs are high. Using a color screen to simulate the page-turning effect both preserves that retro visual impact and eliminates the hassle of a mechanical structure. I have made three versions in total. The first version used an Arduino Uno with a 2.4-inch TFT display. The second version switched to an ESP32 with a 2.8-inch IPS display. The third version was finalized with an ESP32-C3 and a 1.69-inch rounded-corner color display, reducing the size to about the palm of a hand, making it just right for placing on a desk. There are several practical considerations when choosing a small-sized color screen. Desktop space is limited. Screens larger than 3.5 inches look bulky on a desk. With a suitable frame, a 1.5- to 2-inch screen can keep the overall size within 6 centimeters. In terms of power consumption, the backlight current of a small screen is usually between 20 and 40 milliamps, which is significantly less than that of a larger screen. It can be powered by USB or a small-capacity lithium battery. In terms of resolution, 240×280 or 240×320 is sufficient to display the hours, minutes, and seconds, and the page-turning animation won't lag due to too many pixels. There are actually three core requirements: accurate timekeeping, smooth page-turning animations, and a simple appearance. Accurate timekeeping relies on the ESP.
The ESP32's WiFi time synchronization or the external RTC module ensures smooth page turning thanks to the TFT_eSPI library's Sprite double buffering mechanism. The appearance is simple and elegant due to the reasonable UI layout and case design. These three points sound simple, but each one presents challenges when it comes to actual implementation. 1.2 The rationale behind the hardware selection. The reason for choosing the ESP32 as the main controller instead of the Arduino Uno is quite straightforward. The Uno's SRAM is only 2KB, which is simply not enough to drive a color screen for a full-screen refresh, let alone double buffering. The ESP32 has 520 KB of SRAM. Allocating a 240×280×2-byte buffer would only take up 134 KB, leaving more than half for the program to run. In addition, the ESP32 comes with built-in WiFi and NTP time synchronization, eliminating the need for additional modules, which saves on hardware costs and avoids the hassle of wiring. I've tried several different screens. The 2.4-inch screen with the ILI9341 driver chip is the cheapest, but its viewing angle is average, and color shifting is obvious when viewed at an angle. The 1.69-inch IPS screen driven by the ST7789 is more expensive, but the color and viewing angle are much better, and the rounded corner design means you don't have to cut the casing yourself. In the end, I chose the ST7789 with a resolution of 240×280 and an SPI interface, as the wiring is simple. If the power supply is provided via USB, the screen backlight is directly powered from the 5V supply on the development board.
A 33-ohm resistor limits the current. If you want to make it portable, add a 3.7V lithium battery and a TP4056 charging module to boost the voltage to 5V to power the screen and the main controller. Actual measurements show that the operating current of the entire device is between 80 and 120 milliamperes. A 1000-milliampere-hour battery can last for about 8 hours. Note that the ESP32's GPIO output current is limited. The screen backlight should not be directly connected to the GPIO. It must be driven through a transistor or a MOSFET, otherwise, the pins will burn out sooner or later. 1.3 Approach to Building the Software Framework. At the software level, I used the Arduino framework instead of ESP-IDF. The reason is simple: the TFT_eSPI library is mature and stable under Arduino, and the Sprite function for page-turning animations is ready to use out of the box. Although LVGL can also run under ESP-IDF, the configuration is significantly more complex, which is unnecessary for such a small project. The overall software architecture is divided into three layers. The bottom layer is the hardware driver layer, which is responsible for screen initialization, SPI communication, and RTC read/write operations. The middle layer is the logic layer, which handles time retrieval, the page-turning state machine, and animation frame calculations. The top layer is the UI layer, which manages the rendering of numbers, color schemes, and transition effects. The three layers communicate through simple function calls and global state variables. There is no RTOS because the tasks are not complex. A main loop plus a timer interrupt is sufficient.
That's enough. The core idea of the page-turning animation is to split each number into two halves, the upper half flips downward and the lower half flips upward, and gradients and shadows are used in the middle to simulate a three-dimensional effect. In the specific implementation, each digit is represented by two sprites, one for the upper half and one for the lower half. During the flipping process, the Y-axis offset and brightness attenuation of each pixel are dynamically calculated. This part will be elaborated on later. 2. Analysis of Core Details and Practical Implementation Points 2.1 Screen Driver and SPI Communication Configuration The ST7789 display uses an SPI interface, with connections for SCLK, MOSI, CS, DC, RST, and BLK. The ESP32's hardware SPI pins can be mapped to any GPIO, but for stability, I fixed them to VSPI: SCLK to GPIO18, MOSI to GPIO23, CS to GPIO15, DC to GPIO2, RST to GPIO4, and BLK to GPIO5. Keep the wiring as short as possible. If it exceeds 10 centimeters, screen glitches or refresh tearing are likely to occur. The configuration of the TFT_eSPI library is modified in the User_Setup.h file without changing the library source code. The key configuration items are as follows: #define ST7789_DRIVER #define TFT_WIDTH
240 #define TFT_HEIGHT 280 #define TFT_MOSI 23 #define TFT_SCLK 18 #define TFT_CS 15 #define TFT_DC 2 #define TFT_RST 4 #define TFT_BL 5 #define LOAD_GLCD #define SPI_FREQUENCY 40000000 Setting the SPI frequency to 40 MHz has proven to be very stable in tests. However, increasing it to 80 MHz may occasionally result in a distorted display. If your wiring is relatively long or you are using DuPont wires, it is recommended to reduce the frequency to 20 MHz, run it first, and then increase it. Note: The User_Setup.h file in the TFT_eSPI library will be overwritten every time the library is updated. It is recommended to place the configuration separately in User_Setup_Select.h to reference the custom file, or to make a backup copy directly. There is nothing special about the initialization process. After tft.init(), set the rotation direction, fill the screen with black, and turn on the backlight. Here's a detail: Fill the screen with black before turning on the backlight; otherwise, there will be a brief flash of a white screen, which looks bad. 2.
2. Time Acquisition Solution: I used two sets of time sources, NTP and RTC, for dual redundancy. The primary method uses NTP. After the ESP32 connects to WiFi, it requests time synchronization from pool.ntp.org and synchronizes it to the internal RTC. The backup is the DS3231 external RTC module. The accuracy of the I2C interface is one order of magnitude higher than that of the ESP32's internal RTC. Why are both sets needed? Because WiFi doesn't always connect, especially after the router is restarted or the password is changed. Without an external RTC, the time is lost, and you have to wait for WiFi to synchronize the time again. The code for NTP time synchronization uses the configTime function to set the time zone and NTP servers: configTime(8 * 3600, 0, pool.ntp.org, time.nist.gov); struct tm timeinfo; if (getLocalTime(timeinfo)) { // Synchronize to DS3231 rtc.adjust(DateTime(timeinfo.tm_year 1900, timeinfo.tm_mon 1, timeinfo.tm_m
day, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec)); } The DS3231 uses the RTClib library for reading and writing. The I2C address is 0x68. SDA is connected to GPIO21, and SCL is connected to GPIO22. The DS3231 comes with a temperature-compensated crystal oscillator with an annual error of less than 2 minutes, which is much more accurate than the ESP32's internal RTC. The ESP32's internal RTC loses all data after a power outage, whereas the DS3231 has a button cell battery that allows it to continue running even during a power outage. The actual operating logic is to read the DS3231 first when powering on. If the time is valid, it is used directly. Then, an attempt is made to connect to WiFi. If the connection is successful, NTP time synchronization is performed and the time is written back to the DS3231. If the connection fails, the DS3231's time continues to be used. This way, the time won't be too far off, whether there is a network connection or not. 2.3 Sprite double buffering for page-turning animations. The page-turning animation is the heart of this small project and also the most memory- and CPU-intensive part. The Sprite mechanism of TFT_eSPI allows you to draw an entire frame in memory and then push it to the screen all at once to avoid flickering. For each digit, I used two Sprites to store the upper and lower halves, respectively. The size is 60 × 80 pixels.
With a 16-bit color depth, each sprite takes up 60 × 80 × 29600 bytes, and two sprites take up 19.2 KB. There are six numbers for hours, minutes, and seconds, each consisting of two digits, making a total of 12 sprites. This amounts to approximately 230 KB, which the ESP32's 520 KB SRAM can handle. The mathematical principles behind animation are not complicated. Suppose the flip angle ranges from 0 degrees to 180 degrees, with the upper half rotating around the bottom edge and the lower half rotating around the top edge. For the upper half, the Y-axis scaling factor is cos(angle). When the angle goes from 0 to 90 degrees, cos goes from 1 to 0, and the upper half gradually flattens. The same applies to the lower half, but in the opposite direction. At the same time, the brightness decreases with the angle to simulate changes in illumination. float angle = progress * PI; // 0 to PI float scaleY = cos(angle); uint8_t brightness = 255 * (1.0 - progress * 0.7); During the actual rendering, a true 3D rotation is not performed. Instead, the original sprite is scanned line by line, the target line is calculated based on scaleY, and then written to the new sprite. This process is implemented using pushImage and custom pixel manipulation functions. For performance reasons,
I only redraw when the animation frame changes, and directly display the static Sprite when stationary. The CPU usage drops from 40% to below 5%. Note that when there are a large number of sprites, memory fragmentation becomes an issue. It is recommended to create all sprites at once in the setup and avoid dynamically creating and destroying them. Otherwise, the game may crash due to memory fragmentation after running for a few hours. 2.4 Color Scheme and UI Layout I tried several color schemes. Pure white numbers on a black background are the most classic, but they become monotonous after looking at them for a long time. Later, I changed it to a gradient from blue to purple with a dark gray background, which is not glaring at night. The numbers use an anti-aliased font. TFT_eSPI comes with FreeFonts, and selecting FreeSansBold24pt results in a much better display than the default font. In terms of layout, the three sets of numbers for hours, minutes, and seconds are arranged horizontally, separated by colons in between. The colon is represented by two small dots that flash once per second. A thin line is reserved at the top of the screen to display the date and day of the week, and another line is reserved at the bottom to display the WiFi status and battery level. There should be enough blank space overall, and the numbers shouldn't reach the edges of the screen, otherwise they will be covered once the casing is installed. The font size is adjusted according to the screen resolution. For a 240×280 screen, the numbers should be 80 pixels high and 60 pixels wide. Three sets of numbers plus two colons total 60×620×2400 pixels, which exceeds the limit. Therefore,
either reduce the digit size or switch to a two-row display. I finally settled on two rows: the upper row shows hours and minutes, the lower row shows seconds and the date. This way each digit can be 80x100 pixels, giving a stronger visual impact. 3. Hands-On Process and Core Implementation 3.1 Hardware Soldering and Assembly Steps The soldering order is important: low components before high ones, small before large. First, solder the pin headers on the ESP32 dev board, being careful not to install them backwards. Second, solder the screen's FPC socket or pin header; ST7789 modules usually come with the pin header already soldered, so you can simply plug in Dupont wires. Third, solder the pin header on the DS3231 module. Fourth, solder the TP4056 charging module and the battery holder. For a portable version, use a 603048-size 1000mAh lithium battery, which fits perfectly into the 3D-printed enclosure. The charging module's B+ and B- connect to the battery, OUT+ and OUT- connect to the boost module (or directly to the ESP32's 5V pin; the ESP32 dev board has an onboard LDO and also runs on 3.7V input, though the screen backlight may be a bit dim). I drew the enclosure in Fusion 360; the screen cutout is 0.5 mm larger than the visible area to avoid assembly tolerances covering the edges. The back cover has a USB opening and several ventilation holes. For 3D printing I used PETG filament, which tolerates heat better than P
LA (polylactic acid), so it deforms less easily in summer. Tip: when soldering the FPC socket, keep the soldering iron temperature below 350 degrees C and the contact time under 3 seconds, otherwise the plastic socket will melt. 3.2 Software Environment Setup and Library Installation I used Arduino IDE version 2.3.2, and installed the ESP32 board support package through Boards Manager, selecting "esp32 by Espressif Systems" version 2.0.14. This version is fairly stable; versions 3.0 and above have API changes that TFT_eSPI may not be compatible with. Libraries to install: the latest TFT_eSPI, RTClib by Adafruit, plus the WiFi and time.h libraries that ship with the ESP32 core. After installing TFT_eSPI, locate User_Setup.h in the library folder and edit it with the configuration described earlier. Then compile a simple test program to confirm the screen lights up and can display colors. If this step fails, everything after it is pointless. Test code: #include
tft.init(); tft.setRotation(0); tft.fillScreen(TFT_BLACK); tft.setTextColor(TFT_WHITE); tft.setTextSize(2); tft.setCursor(20, 100); tft.println("Hello TFT"); } void loop() {} If the screen displays "Hello TFT", then the SPI communication and driver configuration are correct. If you get a garbled or white screen, first check the wiring, then lower the SPI frequency, and finally check the pin definitions in User_Setup.h. 3.3 Full Code Implementation of the Flip Animation The core function of the flip animation is drawFlipDigit, which takes three parameters: the digit, the position, and the progress. Its internal logic has four steps: read the Sprite of the current digit, compute the flip scaling, generate the intermediate frame, and push it to the screen. void drawFlipDigit(int digit, int x, int y, float progress) {
uint16_t newColor = (r << 11) | (g << 5) | b; tft.drawPixel(x + col, y + row, newColor); } } // The lower half is handled the same way, in the opposite direction } In actual operation the per-frame computation is quite heavy: a full-screen refresh of the 240x280 display takes about 15 ms, and a flip animation lasting 300 ms is roughly 20 frames; at 15 ms per frame that totals 300 ms, which matches nicely. If it feels sluggish, you can reduce the frame count to 15 or refresh only the changed regions. Note that readPixel and drawPixel operate pixel by pixel and are slow. The optimization is to use pushImage to push an entire block at once, which requires drawing the scaled image into a temporary Sprite beforehand. In my tests, running 20 frames pixel-by-pixel on the ESP32 was fine, with CPU usage around 60%, without affecting the WiFi and RTC tasks. 3.4 WiFi Time Sync and Offline Handling For the WiFi connection, use the ESP32's WiFi library with automatic reconnection enabled. After a successful connection, start NTP synchronization and, on success, write the time back to the DS3231. After losing the network, do not block the main loop; continue using the time held by the DS3231.