blavery
Posts: 95
Joined: Sun Jul 01, 2012 2:57 am
Location: QLD. Australia

Python driver for 2.4" SPI 240x320 LCD touch

Fri Apr 03, 2015 6:18 am

I have just uploaded a python driver for the red “2.4 inch Touch 320x240 SPI LCD” module currently popular on eBay for about $7. The marking says TJCTM24024-SPI. This is a ILI9341 driven LCD display, with XPT2046 chip for touch device, and it also includes a SD card holder. All of which would want three SPI channels!
30209.jpeg
30209.jpeg (25.14 KiB) Viewed 8103 times
30209-6.jpeg
30209-6.jpeg (39.32 KiB) Viewed 8103 times
My interest is for straightforward python module(s) for LCD and Touch. The SD card I will ignore. My application is for Raspberry Pi (and for Virtual-GPIO after I check that out, but speed may be an issue).

The module uploaded is working but provisional and a bit rough, and for the LCD only so far. I have it working quite well. I haven't started on the touchscreen part yet. I am sure there will be changes when the LCD task starts interacting with the Touch task.

My LCD driver is NOT a framebuffer method. (320x240 is too tiny to format any normal desktop display onto.) Also it is NOT an instant direct write-to-LCD method. It instead uses the Python Image Library (PIL) to prepare all display content on a “canvas” or buffer. It gets a transfer of the total buffer to screen hardware when ready. This give some transfer delay, as 0.25MB gets transferred by SPI each screen update! So it's not invisibly “snappy”, but I find it quite acceptable for my requirement. The advantage of using PIL is that a well-tried and versatile graphics and text/font library is all usable directly. The disadvantage is that PIL is discontinued, I believe, and is not there for python3, only python 2.x. Its successor PILLOW seems still to be “coming” for Raspberry Pi. (Any correction to this??)

This implementation supports portrait (320Hx240W) and landscape (240Hx320W) modes fairly naturally to the user, despite that the hardware is inherently operating in portrait mode.

The hardware uses 5V for its Vcc power, but the logic levels are 3.3V max. (Vcc can be changed to 3.3 with a soldered link, but 5V is easy enough on the Pi.) I note several vague references on eBay supplier sites that 5V can be used for the logic I/O, but as far as I can see they are wrong.

For best screen update time, I have cranked up the SPI speed to 32,000,000 Hz, which seems to be maximum for the venerable old Rpi, but it is an option, and perhaps the Rpi2 may allow faster?

You need:
- Raspberry Pi (duh), any version, and I used stock current Raspbian on a 256k early Rpi.
- python2.7
- PIL installed
- SPIDEV installed
- SPI enabled (Module blacklist? Device tree?)

For the LCD you need to connect this logic:
- MOSI
- SCK
- CS – I used CE0
- D/C – to any GPIO pin – I used #22
- RESET – to any GPIO pin (option: just tie it to 3.3) – I used #18
- LED – to any GPIO pin (option: just tie it to 3.3) – I used #23
- MISO – not used
DSCN6008.JPG
DSCN6008.JPG (16.01 KiB) Viewed 8103 times
Files:
https://github.com/BLavery/lib_tft24T

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: Python driver for 2.4" SPI 240x320 LCD touch

Fri Apr 03, 2015 12:13 pm

Interesting way of going about things. Thanks for sharing!
note: I may or may not know what I'm talking about...

blavery
Posts: 95
Joined: Sun Jul 01, 2012 2:57 am
Location: QLD. Australia

Re: Python driver for 2.4" SPI 240x320 LCD touch

Tue Apr 07, 2015 2:26 am

Well the touchscreen hardware seems easy enough to get an output from:

- Like the display hardware, its native orientation is portrait.

- Touch device seems a mediocre performer by finger, but a great performer by stylus/pen. I use a "dead" ball-point pen.

- A straightforward "penDown()" can be read from the GPIO pin that pen-irq is connected to.

- A raw X or Y pen coordinate (0-4095) can be read from a 3-byte SPI transfer (2MHz max):

responseData = SPI.xfer([channel , 0, 0]) # where channel = 0xD0 for X or 0x90 for Y
return (responseData[1] << 5) | (responseData[2] >> 3) # Pick off the 12-bit reply

Not ready to post up code yet, still work in progress, on jitter filtering, easy calibration system, code testing etc and more etc. But I have been having fun:
11.jpg
1. A touchpad calculator that works rather well.
2. Leaving a trail of "penprints" to the display
11.jpg (37.44 KiB) Viewed 7985 times
22.jpg
3. A few graphic things on display
4. Doing a "calibration", scaling the touch outputs (each 0-4095) to map correctly to 320x240 display
22.jpg (37.29 KiB) Viewed 7985 times

blavery
Posts: 95
Joined: Sun Jul 01, 2012 2:57 am
Location: QLD. Australia

Re: Python driver for 2.4" SPI 240x320 LCD touch

Tue Apr 07, 2015 11:39 am

Version 0.3 is now posted to github:

https://github.com/BLavery/lib_tft24T

It includes
- display driver
- touch-device driver
- calibration utility to scale the touchscreen output against display pixels
- demo of display doing its tricks
- a complete GUI calculator running on the display. Input by stylus (or by finger if you really have to).
calc.png
GUI calculator. Each button
is a touchscreen hotspot.
Input by stylus.
calc.png (22.2 KiB) Viewed 7944 times
A lot more care could go into getting clean jitter-free stylus readings, although the very crude V0.3 filtering routines are working fairly OK for now.

linedock
Posts: 1
Joined: Thu May 07, 2015 6:03 am

Re: Python driver for 2.4" SPI 240x320 LCD touch

Thu May 07, 2015 6:10 am

Hi, I just bought this screen for my RPi. Do you have a manual on how to install your driver? Thanks.

blavery
Posts: 95
Joined: Sun Jul 01, 2012 2:57 am
Location: QLD. Australia

Re: Python driver for 2.4" SPI 240x320 LCD touch

Thu May 07, 2015 9:35 am

>> Do you have a manual on how to install your driver?

Er, no.

Download the github ZIP, unzip into one folder.
Wire up SPI & other pins as per example py files.
Run one of the example files: eg sudo python example-tft24T-display-demo.py

blavery
Posts: 95
Joined: Sun Jul 01, 2012 2:57 am
Location: QLD. Australia

Re: Python driver for 2.4" SPI 240x320 LCD touch

Sun Jul 05, 2015 1:42 pm

And the totally other way to get this device going is by framebuffer. The following worked for me.

Start with CURRENT Raspbian. We can then use the waveshare32b (not included) driver as the same chips are used.

1. Set up the GPIO pins to match what the plug-on-top waveshare 3.2inch device would use:
Reset - #27 *
Pen IRQ - #17 *
DC/A0 - #22
LED – to +3.3V *
* These 3 are different from what my earlier code examples used.

2. Download waveshare32b-overlay.dtb from
https://github.com/swkim01/waveshare-dtoverlays
and place it in /boot/overlays.

3. Add to file /etc/modules this entry:

Code: Select all

fbtft_device name=waveshare32b rotate=90
This activates LCD screen on device "/dev/fb1".

4. Add to end of the one line of /boot/cmdline.txt this phrase:

Code: Select all

fbcon=map:1
This shows boot diagnostics on LCD instead of HDMI.

5. Add to config.txt the line:

Code: Select all

dtoverlay=ads7846, penirq=17, swapxy=1
This activates touch screen for stylus.

6. In file /usr/share/X11/xorg.conf.d change fb0 entry to read fb1
This make regular X desktop show on LCD instead of HDMI (fb1 instead of fb0).

7. In folder /usr/share/X11/xorg.conf.d create a file 99-calibration.conf, then edit the following into it. The cursor calibration should be enough to get started:

Code: Select all

Section "InputClass"
    Identifier "calibration"
    MatchProduct "ADS7846 Touchscreen"
    Option "Calibration" "3800 300 3800 300"
EndSection
This is calibration table for stylus / mouse pointer. Both axes are reversed. There is correct calibration guidance elsewhere on web.

8. Reboot. Good luck working in 320x240 !! Despite virtual keyboard like "florence" may be useable if you are desperate, a REAL keyboard is a must for doing ALT-WINDOWDRAG in many programs.

-----------
And a footnote: I used separate ads7846 touch driver in config.txt and left waveshare part in modules file. Although the waveshare driver includes ads7846 function itself, I couldn't work out how to do a coordinates "swapxy" for the touch system that way. I suspect maybe it's not needed/implemented for the original target hardware??

blavery
Posts: 95
Joined: Sun Jul 01, 2012 2:57 am
Location: QLD. Australia

Re: Python driver for 2.4" SPI 240x320 LCD touch

Mon Jul 06, 2015 12:09 am

A few more thoughts on using this device.

The 320x240 desktop is small, and using the device as an auxiliary touchscreen instead of main desktop still appeals.
If I now removed the ads7846 driver from /boot/config.txt and went back to my own python coded version of touch driver above, but still kept the framebuffer display part in /etc/modules, I could make a blend of the two paradigms.

fbi can paste an image to the display. fbcat can grab an image from the display. If I kept the PIL/Pillow method of constructing a canvas image of whatever graphics or text I needed, the fbi tool could replace all the the "display()" function of the original python code.

Why throw out the devicetree method of ads7846? Because then I don't have to learn how to read my own stylus/mouse "events" from that engine.

And anyway ... other things in life are calling. Birthdays, daughters, study, travel.

Return to “Interfacing (DSI, CSI, I2C, etc.)”