30#include "freertos/FreeRTOS.h"
31#include "freertos/task.h"
37#pragma GCC optimize ("O2")
40#define TFT_UPDATETASK_STACK 1024
41#define TFT_UPDATETASK_PRIORITY 5
43#define TFT_BACKGROUND_PRIMITIVE_TIMEOUT 10000
45#define TFT_SPI_WRITE_FREQUENCY 40000000
47#define TFT_DMACHANNEL 2
60inline uint16_t preparePixel(RGB888
const & px)
62 return ((uint16_t)(px.G & 0xe0) >> 5) |
63 ((uint16_t)(px.R & 0xf8)) |
64 ((uint16_t)(px.B & 0xf8) << 5) |
65 ((uint16_t)(px.G & 0x1c) << 11);
69inline RGB888 nativeToRGB888(uint16_t pattern)
73 ((pattern & 7) << 5) | ((pattern & 0xe000) >> 11),
74 ((pattern & 0x1f00) >> 5)
79inline RGBA8888 nativeToRGBA8888(uint16_t pattern)
83 ((pattern & 7) << 5) | ((pattern & 0xe000) >> 11),
84 ((pattern & 0x1f00) >> 5),
90inline uint16_t RGBA2222toNative(uint8_t rgba2222)
92 return preparePixel(RGB888((rgba2222 & 3) * 85, ((rgba2222 >> 2) & 3) * 85, ((rgba2222 >> 4) & 3) * 85));
96inline uint16_t RGBA8888toNative(RGBA8888
const & rgba8888)
98 return preparePixel(RGB888(rgba8888.R, rgba8888.G, rgba8888.B));
102TFTController::TFTController()
107 m_SPIDevHandle(nullptr),
109 m_controllerWidth(240),
110 m_controllerHeight(320),
113 m_updateTaskHandle(nullptr),
114 m_updateTaskRunning(false),
116 m_reverseHorizontal(false)
121TFTController::~TFTController()
128void TFTController::setupGPIO()
131 configureGPIO(m_DC, GPIO_MODE_OUTPUT);
132 gpio_set_level(m_DC, 1);
135 if (m_RESX != GPIO_UNUSED) {
136 configureGPIO(m_RESX, GPIO_MODE_OUTPUT);
137 gpio_set_level(m_RESX, 1);
141 if (m_CS != GPIO_UNUSED) {
142 configureGPIO(m_CS, GPIO_MODE_OUTPUT);
143 gpio_set_level(m_CS, 1);
151void TFTController::begin(SPIClass * spi, gpio_num_t DC, gpio_num_t RESX, gpio_num_t CS)
153 CurrentVideoMode::set(VideoMode::SPI);
171void TFTController::begin(SPIClass * spi,
int DC,
int RESX,
int CS)
173 begin(spi, int2gpio(DC), int2gpio(RESX), int2gpio(CS));
180void TFTController::begin(
int SCK,
int MOSI,
int DC,
int RESX,
int CS,
int host)
182 m_SPIHost = (spi_host_device_t)host;
183 m_SCK = int2gpio(SCK);
184 m_MOSI = int2gpio(MOSI);
186 m_RESX = int2gpio(RESX);
194void TFTController::begin()
196 begin(18, 23, 22, 21, 5, VSPI_HOST);
200void TFTController::end()
202 if (m_updateTaskHandle)
203 vTaskDelete(m_updateTaskHandle);
204 m_updateTaskHandle =
nullptr;
212void TFTController::setResolution(
char const * modeline,
int viewPortWidth,
int viewPortHeight,
bool doubleBuffered)
215 int pos = 0, swidth, sheight;
216 int count = sscanf(modeline,
"\"%[^\"]\" %d %d %n", label, &swidth, &sheight, &pos);
217 if (count != 3 || pos == 0)
220 m_screenWidth = swidth;
221 m_screenHeight = sheight;
226 setScreenSize(m_screenWidth, m_screenHeight);
228 setDoubleBuffered(doubleBuffered);
230 m_viewPortWidth = viewPortWidth < 0 ? m_screenWidth : viewPortWidth;
231 m_viewPortHeight = viewPortHeight < 0 ? m_screenHeight : viewPortHeight;
233 m_rot0ViewPortWidth = m_viewPortWidth;
234 m_rot0ViewPortHeight = m_viewPortHeight;
242 xTaskCreate(&updateTaskFunc,
"", TFT_UPDATETASK_STACK,
this, TFT_UPDATETASK_PRIORITY, &m_updateTaskHandle);
245 m_updateTaskFuncSuspended = 0;
249void TFTController::setScreenCol(
int value)
251 if (value != m_screenCol) {
252 m_screenCol = iclamp(value, 0, m_viewPortWidth - m_screenWidth);
253 Primitive p(PrimitiveCmd::Refresh,
Rect(0, 0, m_viewPortWidth - 1, m_viewPortHeight - 1));
259void TFTController::setScreenRow(
int value)
261 if (value != m_screenRow) {
262 m_screenRow = iclamp(value, 0, m_viewPortHeight - m_screenHeight);
263 Primitive p(PrimitiveCmd::Refresh,
Rect(0, 0, m_viewPortWidth - 1, m_viewPortHeight - 1));
269void TFTController::hardReset()
271 if (m_RESX != GPIO_UNUSED) {
274 configureGPIO(m_RESX, GPIO_MODE_OUTPUT);
275 gpio_set_level(m_RESX, 1);
276 vTaskDelay(5 / portTICK_PERIOD_MS);
277 gpio_set_level(m_RESX, 0);
278 vTaskDelay(20 / portTICK_PERIOD_MS);
279 gpio_set_level(m_RESX, 1);
283 vTaskDelay(150 / portTICK_PERIOD_MS);
288void TFTController::setupOrientation()
291 m_viewPortWidth = m_rot0ViewPortWidth;
292 m_viewPortHeight = m_rot0ViewPortHeight;
295 uint8_t MX = m_reverseHorizontal ? 0x40 : 0;
296 uint8_t madclt = 0x08 | MX;
297 switch (m_orientation) {
298 case TFTOrientation::Rotate90:
299 tswap(m_viewPortWidth, m_viewPortHeight);
303 case TFTOrientation::Rotate180:
306 m_rotOffsetY = m_controllerHeight - m_viewPortHeight;
307 m_rotOffsetX = m_controllerWidth - m_viewPortWidth;
309 case TFTOrientation::Rotate270:
310 tswap(m_viewPortWidth, m_viewPortHeight);
311 madclt |= 0x20 | 0x80;
312 m_rotOffsetX = m_controllerHeight - m_viewPortWidth;
318 writeCommand(TFT_MADCTL);
326 p.cmd = PrimitiveCmd::Reset;
333 if (m_orientation != value || force) {
334 suspendBackgroundPrimitiveExecution();
335 m_orientation = value;
339 resumeBackgroundPrimitiveExecution();
345void TFTController::setReverseHorizontal(
bool value)
347 m_reverseHorizontal = value;
348 setOrientation(m_orientation,
true);
352void TFTController::SPIBegin()
359 spi_bus_config_t busconf;
360 memset(&busconf, 0,
sizeof(busconf));
361 busconf.mosi_io_num = m_MOSI;
362 busconf.miso_io_num = -1;
363 busconf.sclk_io_num = m_SCK;
364 busconf.quadwp_io_num = -1;
365 busconf.quadhd_io_num = -1;
366 busconf.flags = SPICOMMON_BUSFLAG_MASTER;
367 auto r = spi_bus_initialize(m_SPIHost, &busconf, TFT_DMACHANNEL);
368 if (r == ESP_OK || r == ESP_ERR_INVALID_STATE) {
369 spi_device_interface_config_t devconf;
370 memset(&devconf, 0,
sizeof(devconf));
371 devconf.mode = TFT_SPI_MODE;
372 devconf.clock_speed_hz = TFT_SPI_WRITE_FREQUENCY;
373 devconf.spics_io_num = -1;
375 devconf.queue_size = 1;
376 spi_bus_add_device(m_SPIHost, &devconf, &m_SPIDevHandle);
379 if (m_updateTaskFuncSuspended)
380 resumeBackgroundPrimitiveExecution();
384void TFTController::SPIEnd()
391 suspendBackgroundPrimitiveExecution();
393 if (m_SPIDevHandle) {
394 spi_bus_remove_device(m_SPIDevHandle);
395 m_SPIDevHandle =
nullptr;
396 spi_bus_free(m_SPIHost);
401void TFTController::SPIBeginWrite()
405 m_spi->beginTransaction(SPISettings(TFT_SPI_WRITE_FREQUENCY, SPI_MSBFIRST, TFT_SPI_MODE));
409 if (m_SPIDevHandle) {
410 spi_device_acquire_bus(m_SPIDevHandle, portMAX_DELAY);
413 if (m_CS != GPIO_UNUSED) {
414 gpio_set_level(m_CS, 0);
419void TFTController::SPIEndWrite()
421 if (m_CS != GPIO_UNUSED) {
422 gpio_set_level(m_CS, 1);
426 gpio_set_level(m_DC, 1);
430 m_spi->endTransaction();
434 if (m_SPIDevHandle) {
435 spi_device_release_bus(m_SPIDevHandle);
440void TFTController::SPIWriteByte(uint8_t
data)
448 if (m_SPIDevHandle) {
449 spi_transaction_t ta;
450 ta.flags = SPI_TRANS_USE_TXDATA;
453 ta.tx_data[0] =
data;
454 ta.rx_buffer =
nullptr;
455 spi_device_polling_transmit(m_SPIDevHandle, &ta);
460void TFTController::SPIWriteWord(uint16_t
data)
464 m_spi->write(
data >> 8);
465 m_spi->write(
data & 0xff);
469 if (m_SPIDevHandle) {
470 spi_transaction_t ta;
471 ta.flags = SPI_TRANS_USE_TXDATA;
474 ta.tx_data[0] =
data >> 8;
475 ta.tx_data[1] =
data & 0xff;
476 ta.rx_buffer =
nullptr;
477 spi_device_polling_transmit(m_SPIDevHandle, &ta);
482void TFTController::SPIWriteBuffer(
void *
data,
size_t size)
486 m_spi->writeBytes((uint8_t*)
data, size);
490 if (m_SPIDevHandle) {
491 spi_transaction_t ta;
493 ta.length = 8 * size;
496 ta.rx_buffer =
nullptr;
497 spi_device_polling_transmit(m_SPIDevHandle, &ta);
502void TFTController::writeCommand(uint8_t cmd)
504 gpio_set_level(m_DC, 0);
509void TFTController::writeByte(uint8_t
data)
511 gpio_set_level(m_DC, 1);
516void TFTController::writeData(
void *
data,
size_t size)
518 gpio_set_level(m_DC, 1);
519 SPIWriteBuffer(
data, size);
524void TFTController::writeWord(uint16_t
data)
526 gpio_set_level(m_DC, 1);
531void TFTController::sendRefresh()
533 Primitive p(PrimitiveCmd::Refresh, Rect(0, 0, m_viewPortWidth - 1, m_viewPortHeight - 1));
538void TFTController::sendScreenBuffer(Rect updateRect)
542 updateRect = updateRect.intersection(Rect(0, 0, m_viewPortWidth - 1, m_viewPortHeight - 1));
545 writeCommand(TFT_CASET);
546 writeWord(m_rotOffsetX + updateRect.X1);
547 writeWord(m_rotOffsetX + updateRect.X2);
550 writeCommand(TFT_RASET);
551 writeWord(m_rotOffsetY + updateRect.Y1);
552 writeWord(m_rotOffsetY + updateRect.Y2);
554 writeCommand(TFT_RAMWR);
555 const int width = updateRect.width();
556 for (
int row = updateRect.Y1; row <= updateRect.Y2; ++row) {
557 writeData(m_viewPort[row] + updateRect.X1,
sizeof(uint16_t) *
width);
564void TFTController::allocViewPort()
566 m_viewPort = (uint16_t**) heap_caps_malloc(m_viewPortHeight *
sizeof(uint16_t*), MALLOC_CAP_32BIT | MALLOC_CAP_INTERNAL);
567 for (
int i = 0; i < m_viewPortHeight; ++i) {
568 m_viewPort[i] = (uint16_t*) heap_caps_malloc(m_viewPortWidth *
sizeof(uint16_t), MALLOC_CAP_DMA);
569 memset(m_viewPort[i], 0, m_viewPortWidth *
sizeof(uint16_t));
574void TFTController::freeViewPort()
577 for (
int i = 0; i < m_viewPortHeight; ++i)
578 heap_caps_free(m_viewPort[i]);
579 heap_caps_free(m_viewPort);
580 m_viewPort =
nullptr;
585void TFTController::updateTaskFunc(
void * pvParameters)
587 TFTController * ctrl = (TFTController*) pvParameters;
591 ctrl->waitForPrimitives();
594 if (ctrl->m_updateTaskFuncSuspended > 0)
595 ulTaskNotifyTake(
true, portMAX_DELAY);
597 ctrl->m_updateTaskRunning =
true;
599 Rect updateRect = Rect(SHRT_MAX, SHRT_MAX, SHRT_MIN, SHRT_MIN);
601 int64_t startTime = ctrl->backgroundPrimitiveTimeoutEnabled() ? esp_timer_get_time() : 0;
605 if (ctrl->getPrimitive(&prim, TFT_BACKGROUND_PRIMITIVE_TIMEOUT / 1000) ==
false)
608 ctrl->execPrimitive(prim, updateRect,
false);
610 if (ctrl->m_updateTaskFuncSuspended > 0)
613 }
while (!ctrl->backgroundPrimitiveTimeoutEnabled() || (startTime + TFT_BACKGROUND_PRIMITIVE_TIMEOUT > esp_timer_get_time()));
615 ctrl->showSprites(updateRect);
617 ctrl->m_updateTaskRunning =
false;
619 if (!ctrl->isDoubleBuffered())
620 ctrl->sendScreenBuffer(updateRect);
626void TFTController::suspendBackgroundPrimitiveExecution()
628 ++m_updateTaskFuncSuspended;
629 while (m_updateTaskRunning)
634void TFTController::resumeBackgroundPrimitiveExecution()
636 m_updateTaskFuncSuspended = tmax(0, m_updateTaskFuncSuspended - 1);
637 if (m_updateTaskFuncSuspended == 0)
638 xTaskNotifyGive(m_updateTaskHandle);
642void TFTController::setPixelAt(PixelDesc
const & pixelDesc,
Rect & updateRect)
644 genericSetPixelAt(pixelDesc, updateRect,
645 [&] (
RGB888 const & color) {
return preparePixel(color); },
646 [&] (
int X,
int Y, uint16_t pattern) { m_viewPort[
Y][
X] = pattern; }
653void TFTController::absDrawLine(
int X1,
int Y1,
int X2,
int Y2, RGB888 color)
655 genericAbsDrawLine(
X1,
Y1,
X2,
Y2, color,
656 [&] (RGB888
const & color) {
return preparePixel(color); },
657 [&] (
int Y,
int X1,
int X2, uint16_t pattern) { rawFillRow(
Y,
X1,
X2, pattern); },
658 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); },
659 [&] (
int X,
int Y, uint16_t pattern) { m_viewPort[
Y][
X] = pattern; },
660 [&] (
int X,
int Y) { m_viewPort[
Y][
X] = ~m_viewPort[
Y][
X]; }
666void TFTController::rawFillRow(
int y,
int x1,
int x2, uint16_t pattern)
668 auto px = m_viewPort[y] + x1;
669 for (
int x = x1; x <= x2; ++x, ++px)
675void TFTController::rawFillRow(
int y,
int x1,
int x2, RGB888 color)
677 rawFillRow(y, x1, x2, preparePixel(color));
683void TFTController::swapRows(
int yA,
int yB,
int x1,
int x2)
685 auto pxA = m_viewPort[yA] + x1;
686 auto pxB = m_viewPort[yB] + x1;
687 for (
int x = x1; x <= x2; ++x, ++pxA, ++pxB)
692void TFTController::rawInvertRow(
int y,
int x1,
int x2)
694 auto px = m_viewPort[y] + x1;
695 for (
int x = x1; x <= x2; ++x, ++px)
700void TFTController::drawEllipse(Size
const & size, Rect & updateRect)
702 genericDrawEllipse(size, updateRect,
703 [&] (RGB888
const & color) {
return preparePixel(color); },
704 [&] (
int X,
int Y, uint16_t pattern) { m_viewPort[
Y][
X] = pattern; }
709void TFTController::clear(Rect & updateRect)
711 hideSprites(updateRect);
712 auto pattern = preparePixel(getActualBrushColor());
713 for (
int y = 0; y < m_viewPortHeight; ++y)
714 rawFillRow(y, 0, m_viewPortWidth - 1, pattern);
718void TFTController::VScroll(
int scroll, Rect & updateRect)
720 genericVScroll(scroll, updateRect,
721 [&] (
int yA,
int yB,
int x1,
int x2) { swapRows(yA, yB, x1, x2); },
722 [&] (
int yA,
int yB) { tswap(m_viewPort[yA], m_viewPort[yB]); },
723 [&] (
int y,
int x1,
int x2, RGB888 pattern) { rawFillRow(y, x1, x2, pattern); }
728void TFTController::HScroll(
int scroll, Rect & updateRect)
730 genericHScroll(scroll, updateRect,
731 [&] (RGB888
const & color) {
return preparePixel(color); },
732 [&] (
int y) {
return m_viewPort[y]; },
733 [&] (uint16_t * row,
int x) {
return row[x]; },
734 [&] (uint16_t * row,
int x,
int pattern) { row[x] = pattern; }
739void TFTController::drawGlyph(Glyph
const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect)
741 genericDrawGlyph(glyph, glyphOptions, penColor, brushColor, updateRect,
742 [&] (RGB888
const & color) {
return preparePixel(color); },
743 [&] (
int y) {
return m_viewPort[y]; },
744 [&] (uint16_t * row,
int x, uint16_t pattern) { row[x] = pattern; }
749void TFTController::invertRect(Rect
const & rect, Rect & updateRect)
751 genericInvertRect(rect, updateRect,
752 [&] (
int Y,
int X1,
int X2) { rawInvertRow(
Y,
X1,
X2); }
759 genericSwapFGBG(rect, updateRect,
760 [&] (RGB888
const & color) {
return preparePixel(color); },
761 [&] (
int y) {
return m_viewPort[y]; },
762 [&] (uint16_t * row,
int x) {
return row[x]; },
763 [&] (uint16_t * row,
int x, uint16_t pattern) { row[x] = pattern; }
769void TFTController::copyRect(Rect
const & source, Rect & updateRect)
771 genericCopyRect(source, updateRect,
772 [&] (
int y) {
return m_viewPort[y]; },
773 [&] (uint16_t * row,
int x) {
return row[x]; },
774 [&] (uint16_t * row,
int x, uint16_t pattern) { row[x] = pattern; }
780void TFTController::readScreen(Rect
const & rect, RGB888 * destBuf)
782 for (
int y = rect.Y1; y <= rect.Y2; ++y) {
783 auto row = m_viewPort[y] + rect.X1;
784 for (
int x = rect.X1; x <= rect.X2; ++x, ++destBuf, ++row)
785 *destBuf = nativeToRGB888(*row);
790void TFTController::rawDrawBitmap_Native(
int destX,
int destY, Bitmap
const * bitmap,
int X1,
int Y1,
int XCount,
int YCount)
792 genericRawDrawBitmap_Native(destX, destY, (uint16_t*) bitmap->data, bitmap->width,
X1,
Y1, XCount, YCount,
793 [&] (
int y) { return m_viewPort[y]; },
794 [&] (uint16_t * row,
int x, uint16_t src) { row[x] = src; }
799void TFTController::rawDrawBitmap_Mask(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
801 auto foregroundPattern = preparePixel(bitmap->foregroundColor);
802 genericRawDrawBitmap_Mask(destX, destY, bitmap, (uint16_t*)saveBackground,
X1,
Y1, XCount, YCount,
803 [&] (
int y) {
return m_viewPort[y]; },
804 [&] (uint16_t * row,
int x) {
return row[x]; },
805 [&] (uint16_t * row,
int x) { row[x] = foregroundPattern; }
810void TFTController::rawDrawBitmap_RGBA2222(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
812 genericRawDrawBitmap_RGBA2222(destX, destY, bitmap, (uint16_t*)saveBackground,
X1,
Y1, XCount, YCount,
813 [&] (
int y) {
return m_viewPort[y]; },
814 [&] (uint16_t * row,
int x) {
return row[x]; },
815 [&] (uint16_t * row,
int x, uint8_t src) { row[x] = RGBA2222toNative(src); }
820void TFTController::rawDrawBitmap_RGBA8888(
int destX,
int destY, Bitmap
const * bitmap,
void * saveBackground,
int X1,
int Y1,
int XCount,
int YCount)
822 genericRawDrawBitmap_RGBA8888(destX, destY, bitmap, (uint16_t*)saveBackground,
X1,
Y1, XCount, YCount,
823 [&] (
int y) {
return m_viewPort[y]; },
824 [&] (uint16_t * row,
int x) {
return row[x]; },
825 [&] (uint16_t * row,
int x,
RGBA8888 const & src) { row[x] = RGBA8888toNative(src); }
830void TFTController::swapBuffers()
833 sendScreenBuffer(Rect(0, 0, getViewPortWidth() - 1, getViewPortHeight() - 1));
This file contains fabgl::TFTController definition.
This file contains some utility classes and functions.
TFTOrientation
This enum defines TFT orientation.
Represents a 24 bit RGB color.