B4R Library rAdafruit_STMPE610 & rAdafruit_ILI9341 - Touch Sensitive Screen

Erel

Administrator
Staff member
Licensed User


Libraries for Adafruit TFT screen: https://www.adafruit.com/products/1651
https://learn.adafruit.com/adafruit-2-8-tft-touch-shield-v2

rAdafruit_STMPE610 handles the touch events.
rAdafruit_ILI9341 handles the graphics. It depends on rAdafruitGFX library.

Example code (depends on rAdafruit_STMPE610, rAdafruit_ILI9341 and rAdafruitGFX libraries):
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private STMP As AdafruitSTMPE610
   Private ILI As AdafruitILI93411
   Private colors(8) As UInt
   Private currentColor As UInt
   Private colorsHeight As UInt = 20
   Private colorsWidth As UInt
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   If STMP.Initialize(8, "STMP_Touch") = False Then
     Log("Failed to initialize touch screen.")
     Return  
   End If
   ILI.Initialize(10, 9)
   ILI.FillScreen(ILI.COLOR_WHITE)
   ILI.GFX.ConfigureText(2, ILI.COLOR_BLACK, True)
   ILI.GFX.SetCursor(2, 2)
   ILI.GFX.DrawText("This is a test")
   Dim p As Pin
   p.Initialize(0, p.MODE_INPUT)
   RndSeed(p.AnalogRead)
   colorsWidth = ILI.GFX.Width / colors.Length
   For i = 0 To colors.Length - 1
     colors(i) = ILI.Color(Rnd(0, 256), Rnd(0, 256), Rnd(0, 256))
     ILI.GFX.DrawRect(colorsWidth * i, 0, colorsWidth, colorsHeight, colors(i), True)
   Next
   currentColor = colors(0)
End Sub

Sub STMP_Touch (X As Int, Y As Int)
   'map the touch range to the drawing screen range
   Dim fx As Int = MapRange(X, 150, 3800, 0, ILI.GFX.Width)
   Dim fy As Int = MapRange(Y, 130, 4000, 0, ILI.GFX.Height)
   If fy < colorsHeight Then
     currentColor = colors(fx / colorsWidth)
     ILI.GFX.DrawRect(0, ILI.GFX.Height - 3, ILI.GFX.Width, ILI.GFX.Height, currentColor, True)
     Log("CurrentColor: ", currentColor)
   Else
     ILI.GFX.DrawCircle(fx, fy, 5, currentColor,True)
   End If
End Sub
 

Attachments

Hypnos

Active Member
Licensed User
I have a LCD breakout (without touch) and I tested on adafruit ILI9341 graphicstest example and it can work. but it's can't work on this B4R library.
Except the CS and DS pin, seems my LCD also require MOSI, CLK, RST, MISO. Any easyway to make this B4R library also work on my screen?

Thanks!
 

Hypnos

Active Member
Licensed User
You need to change this line in rAdafruit_STMPE610.cpp:
B4X:
stm = new (backend) Adafruit_STMPE610(CSPin);

My screen worked on adafruit ILI9341 example, so am I correct that I should change the rAdafruit_ILI9341.cpp instead of Adafruit_STMPE610.cpp as I don't need handle the touch event?

I take a look of the rAdafruit_ILI9341.cpp, it just a few lines but I have no idea how to add MISO, MOSI, CLK and RST in the code.... : ( can you give some more hints.
Thanks !

here is the rAdafruit_ILI9341.cpp

B4X:
#include "B4RDefines.h"

namespace B4R {
    void B4RAdafruitILI9341::Initialize(Byte CS, Byte DC) {
        ili = new (be) Adafruit_ILI9341(CS, DC);
        ili->begin();
        init();
    }
    void B4RAdafruitILI9341::init() {
        gfx.gfx = ili;
        GFX = &gfx;
    }
    void B4RAdafruitILI9341::FillScreen (UInt Color) {
        ili->fillScreen(Color);
    }
    UInt B4RAdafruitILI9341::Color (Byte R, Byte G, Byte B) {
        return ili->color565(R, G, B);
    }
    void B4RAdafruitILI9341::SetRotation(Byte b) {
        ili->setRotation(b);
    }
}
 

Hypnos

Active Member
Licensed User
You can see all the constructor parameters in line 127: https://github.com/adafruit/Adafruit_ILI9341/blob/master/Adafruit_ILI9341.h#L127

Hard code the values of the other parameters.

Thanks for pointing me to the right direction!
I changed the lines in Adafruit_ILI9341.cpp (line 69-76)

B4X:
// Constructor when using hardware SPI.  Faster, but must use SPI pins
// specific to each board type (e.g. 11,13 for Uno, 51,52 for Mega, etc.)
Adafruit_ILI9341::Adafruit_ILI9341(int8_t cs, int8_t dc, int8_t rst) : Adafruit_GFX(ILI9341_TFTWIDTH, ILI9341_TFTHEIGHT) {
 // _cs   = cs;
 // _dc   = dc;
 // _rst  = rst;
 // hwSPI = true;
 // _mosi  = _sclk = 0;
  _cs   = 15;
  _dc   = 2;
  _mosi  = 13;
  _miso = 12;
  _sclk = 14;
  _rst  = -1;
  hwSPI = false;
}

I got some progress and got something display on my TFT!
But I still got some issue:

- Fill Screen is strange except if I use black color to fill :

IMG_1127.jpg


- If fill with black color, it's better but still have some issue on drawcircle, drawline, text have broken pixel, etc...

IMG_1126.jpg



Everything is good if directly using ardunio graphicstest example

IMG_1128.JPG




Here is my code in B4R:

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private ILI As AdafruitILI93411
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    ILI.Initialize(15,2)
    ILI.FillScreen(ILI.COLOR_BLACK)
    ILI.GFX.ConfigureText(2, ILI.COLOR_WHITE, True)
    ILI.GFX.SetCursor(2, 2)
    ILI.GFX.DrawText("This is a test")
    ILI.GFX.DrawText(CRLF)
    ILI.GFX.ConfigureText(4, ILI.COLOR_YELLOW, True)
    ILI.GFX.DrawText(CRLF)
    ILI.GFX.DrawText("Thanks")
    ILI.GFX.ConfigureText(4, ILI.COLOR_RED, True)
    ILI.GFX.DrawText("B4R")
    ILI.GFX.DrawCircle(100,150,50,ILI.COLOR_ORANGE,True)
    'ILI.GFX.DrawRect (50,150,150,100, 10,True)
    'ILI.GFX.DrawLine (10, 70, 200, 70, 10)
End Sub

Any idea please? Thank you!
 

Hypnos

Active Member
Licensed User
As you can see in the library code, it doesn't really do anything except of call the wrapped library methods.

You need to compare the example C code to your code. Maybe the initialization parameters are different.
I found that the Adafruit_ILI9341 and Adafruit_GFX library in my Arduino folder is different with B4R Libraries folder (maybe they are different version), after I copied the files into B4R. It worked!

Thanks Erel!

work.jpg
 
Top