David Johnson-Davies' Tiny TFT Graphics Library Gains an Odd New Ability: Reading From Displays

Tweak is built to offer graphics-handling features like screenshotting, mirroring, and collision detection with a minimal memory footprint.

Gareth Halfacree
2 years ago β€’ Displays / HW101

David Johnson-Davies has added a new feature to his Tiny TFT Graphics Library, and it's one which works exactly backwards to the way you'd expect: it reads from, rather than writes to, a compatible display panel.

"[This] extension to my Tiny TFT Graphics Library to allow you to read pixels from the display," Johnson-Davies explains of the new update. "It works with TFT displays available from AliExpress. Being able to read pixels from the display allows you to do several things that wouldn't otherwise be possible."

Reading from rather than writing to a display might not sound that useful, but Johnson-Davies makes some sound arguments for its utility courtesy of four sample applications. The first is a virtual kaleidoscope, which cuts a triangle from an existing screen image and then reflects it to create a symmetrical version. A stopwatch, meanwhile, uses exclusive-OR (XOR) capabilities to remove and redraw the hand as it moves once a second, without leaving behind any corruption of the background clock face.

Another sample shows how the extension could be used in gaming, bouncing a ball between two paddles like a game of Pong β€” detecting when the ball hits the paddle and bouncing it back off. The final sample application, meanwhile, allows for capturing whatever is currently on the display as a bitmap and saving it to an SD Card.

"To implement these applications without the ability to read pixels from the display would require you to keep a mirror of the display in RAM, and update the mirror every time you draw to the actual display," Johnson-Davies explains. "This would slow down graphics and require a lot of memory: for example, to mirror a 320Γ—240 color display would require 153.6kbytes of RAM. To put this in context, the ATtiny414 used to run these examples only has 256 bytes of RAM."

The extension is compatible with a range of TFT displays based on ST7735, ST7735R, ST7789, and ST7789V controllers; it is not, however, compatible with Adafruit's range of TFT displays. "The reason is that their displays all include a unidirectional on-board logic-level converter to allow them to be used with either 3.3V or 5V," Johnson-Davies says, "but this has the downside of preventing them from being able to read back from the display memory."

More details are available on Johnson-Davies' blog, along with the source code for the modified Tiny TFT Graphics Library under a permissive Creative Commons Attribution 4.0 International license.

Gareth Halfacree
Freelance journalist, technical author, hacker, tinkerer, erstwhile sysadmin. For hire: freelance@halfacree.co.uk.
Latest articles
Sponsored articles
Related articles
Latest articles
Read more
Related articles