TFT and LCD displays with BASIC - slviajero/tinybasic Wiki

Connecting an LCD module

LCD modules come in various shapes. The 16 character, 2 lines shield is found frequently (16x2 LCD shield ). The definition LCDSHIELD activates the code for shields. The buttons of the standard LCD shields are accessed reading analog input 14. This corresponds to A0 in the Arduino IDE. This shield works nicely with BASIC as an device for displaying data and for code that is autorun from the EEPROM.

Well suited for the use with BASIC as a standalone computer are the 20x4 modules (LCD tutorial). I used one of these modules to build a AIM 65 clone computer (http://www.obsoletecomputermuseum.org/aim65/). Both serial I2C display and a directly connected model are supported. The definition ARDUINOLCDI2C activates the I2C code.

Libraries used here are the standard Arduino LiquidCrystal.h for directly attached displays like the shield and Wire.h with LiquidCrystal_I2C.h for the I2C modules.

Display prototype

Connecting a TFT module

TFT code is included and tested for a 7 inch SSD1963 display with a TFT shield on an Arduino Mega and DUE. The UTFT library is used for this.

TFT Display

The generic display driver code

All display modules use a generic display driver code. It handles scrolling and buffering. It needs some functions and variables defined for the specific hardware. As an example the code for a TFT is used. There is a hardware depended part of the code between #ifdef ARDUINOTFT and #endif. This part of the code has to define all low level objects, include the header and set the definition DISPLAYDRIVER. The display driver code is hardware independent and needs the following functions and variables:

const int dsp_rows=...;

const int dsp_columns=...;

void dspbegin() { ... }

void dspprintchar(char c, short col, short row) { ... }

void dspclear() { ... }

dsp_rows and dsp_columns set the size of the display. The display driver generates a buffer of rows*columns bytes to track all activities. dspbegin() is a low level start function to initialise the display. dspprintchar(...) prints one character cat the cursor position col and row. dspclear() hardware clears the display. All the function in the display driver are based on these 3 functions only. Scrolling is implemented as a full redraw of the display from the buffer. No hardware scrolling support is added. This is slow on a TFT. Please note the function dspsetscrollmode(,) in dspbegin(). This function tell the scroll code how scrolling should be done. Please look into the code for more info.

The definition DISPLAYCANSCROLL activates the compilation of the full display driver. If this is undefined, a minimal driver is compiled for low memory systems.

Using a TFT or LCD as output device

After upload and start of the program the Arduino communicates on the serial device. set 2, 1 activates display output, set 2, 0 switches back to serial. When a program is going back to interactive mode, the display is disabled and serial is used again. If the flag STANDALONE is set, output goes directly to the display and input is taken from a keyboard. Alternative ways of addressing the display is to use &2 in PRINT, INPUT, PUT and GET. Example: PRINT &2; "hello world" writes to the display.

The display driver code in the BASIC code emulates a simple terminal. It handles scrolling down and cursor control. The following control characters are implemented

This is the minimum of cursor control for a terminal. PRINT, INPUT and program editing work well with this minimal terminal. PUT can be used to send any on the commands and create clean display output.

If the code is compiled with VT52 support by setting HASVT52, currently the following escape sequences can be uses

These codes work for buffered and unbuffered displays. Other vt52 sequences are prepared but unimplemented (https://en.wikipedia.org/wiki/VT52).

The display output vector is &2. Typical commands going to the display are

PRINT &2, "Hello world"

PUT &2, 12

Compiling the BASIC interpreter with the option #define STANDALONE makes the display the standard output device.

Special features of the display code

If the interpreter is compiled with #DISPLAYCANSCROLL a display buffer is generated. This display buffer can be accessed through the special array @D(). This array starts at 1 in the top left corner of the display and addressed the character buffer line by line. Typical code could be

IF @D(1)=65 then print "character is A" @D(1)="B"

The two special variables are @X, @Y. They address the cursor position. Like @D() these variable are read/write and can be used to get or set the position of the cursor on the screen.

Graphics

Some part of the UTFT library are implemented as BASIC commands and tested for the SSD1963. Supported commands are

COLOR R, G, B

PLOT X, Y

LINE X0, Y0, X1, Y1

RECT X0, Y0, X1, Y1

FRECT X0, Y0, X1, Y1

CIRCLE X0, Y0, R

FCIRCLE X0, Y0, R

The commands hand through the parameters directly to the respective UTFT functions. FRECT and FCIRCLE are filled shapes while RECT and CIRCLE are empty shapes. The UTFT library addresses RGB 24 bit color values with the display I use (SD1963). Color values R, G, B are between 0-255. X and Y values are in the range of 0 to 799 and 0 to 479.

VGA displays are in preparation. They will use a COLOR setting based on VGA colors.