bitbank2 / SPI_LCD

A simple C library for directly communicating with SPI-connected LCD displays
GNU General Public License v3.0
112 stars 24 forks source link

How to use SPI_LCD for 2.2" TFT01-2.2S LCD? #1

Closed rezaee closed 6 years ago

rezaee commented 6 years ago

Hi this is my LCD : http://www.elecfreaks.com/wiki/index.php?title=2.2S%22_TFT_LCD:_TFT01-2.2S and I could run it by fbtft driver and notro library with SPI protocol. My board is NanoPi-M1 and my wiring was this:

LCD pins    GPIO pin numbers on Linux
reset:                1
led:                   6
sdi/mosi:          64
sdo/miso:        65
sck:                 66
cs:                   67
d/c:                 201

So, If I want to use SPI_LCD, which files should be edited to match my board, my LCD(ili9341) and my wiring?

bitbank2 commented 6 years ago

The mosi/sck/cs pins don't need to be specified for my code, just wire them correctly. You do need to specify the GPIO pins used for D/C, RESET, LED. To make things easier, in my library, I reference the physical pin numbers and use lookup tables to reference the correct GPIO number. Your board (NanoPi M1) is not yet in my list, so you would need to add a pin translation list based on the info here: http://wiki.friendlyarm.com/wiki/index.php/NanoPi_M1 . Then you would initialize it like this (based on your numbers above):

int spilcdInit(int iLCDType, int bFlipped, int iSPIChannel, int iSPIFreq, int iDCPin, int iResetPin, int iLEDPin); For your numbers above, they would translate to the pin numbers below: spilcdInit(LCD_ILI9341, 0, <probably 1 for your board>, 40000000, 18, 22, 12);

After you initialize it, you can call the functions to draw text, rectangles and image tiles.

rezaee commented 6 years ago

using the appropriate #ifdef in spi_lcd.c and in the make_sample makefile.

May you explain how can we do this?

rezaee commented 6 years ago

Why you say I use this numbers: spilcdInit(LCD_ILI9341, 0, <probably 1 for your board>, 40000000, 18, 22, 12); While my pin numbers for dc, reset, led is different as I wrote above? dc: 201 , reset: 1, led: 6

rezaee commented 6 years ago

I changed the spilcdinit function in main.c file, as below: spilcdInit(LCD_ILI9341, 0, 0, 40000000, 18, 22, 12); Then tried make and make -f make_sample commands. then I ran ./lcd and got this result in command line: Full screen updates max out at 1.09 FPS

But nothing happened on LCD.

bitbank2 commented 6 years ago

The pin header numbers (1-40 or 24/28/36 depending on your board) are how I make it easier to work with my library. GPIO remapping is unique for each board. In your case, pin 18 of your board is GPIO 201. I have translation tables for several boards already in spi_lcd.c. If your board is not listed, you need to add a table for it named iGenericPins[] which will have the correct numbers to translate pins into GPIO numbers. In your case, the 19th entry will need to be 201 (table index starts at 0).

rezaee commented 6 years ago

I added #define USE_NANOPIM1 and the below code to the spi_lcd.c file :

#ifdef USE_NANOPIM1
static int iGenericPins[] = {-1,-1,-1,-1,-1,-1,-1,203,198,-1,199,0,6,2,-1,3,200,-1,201,64,-1,65,1,66,67,-1,17,19,18,20,-1,21,7,8,-1,16,13,9,15,-1,14};
#endif // USE_NANOPIM1

Then compile them again. but I have nothing yet when running ./lcd !

rezaee commented 6 years ago

I tried GENERIC and WIRINGPI (by uncommenting defines in both spi_lcd.c and make_sample files) but didn't work! I can compile the make and make_sample files without error but the result is same: Full screen updates max out at 1.09 FPS I see this comment in my command line(I use ubuntu-core with ssh in my board) And nothing on LCD!

bitbank2 commented 6 years ago

I'll help you through an email thread instead. Please send me the code you've written so far and I'll see what's wrong (bitbank@pobox.com)

rezaee commented 6 years ago

Sorry bitbank, It was a wiring mistake and solved. now I have a white screen with colorful rectangles on it. So I have 3 questions: 1- How can I turn of the backlight? I mean instead of white background I like to have black background. Also less energy consumption. 2- Can I show .jpg or .png or any type of images directly? How? 3- What about Videos? Can I show videos by your library? how?

bitbank2 commented 6 years ago

I'm glad you got it working 1- you can modify the code in spi_lcd.c to not turn on the backlight, but all of the ili9341 displays I've seen don't have a back reflector and will be unusable without the backlight. 2- This capability is beyond the scope of this project. If you can decode images with other code, displaying them is pretty simple. 3- Again, this is beyond the scope of my project. The most difficult part of dealing with the SPI LCD displays is initializing the registers and figuring out how to write pixels. This code is exposed in a simple way in my project. Since my code doesn't create a virtual frame buffer, you have to be more hands-on with writing pixels to the display. You can use the spilcdDrawTile() function to write a few scan lines (240 x Y) at a time from an image or video. Since the driver only allows 4k to be written at once, you can just write 8 scan lines at a time (240 x 8 x 16bpp = 3840 bytes). I created a dirty-tile scheme for playing emulated games at a better framerate than doing fullscreen updates. If you really want the best framerate, then you would need to integrate the LCD code into the video codec to write only the pixels that change each frame. I wrote my own MPEG1/H.263 decoder and this would allow frame rates greater than fullscreen updates.