1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

B4R Library rAdafruit_STMPE610 & rAdafruit_ILI9341 - Touch Sensitive Screen

Discussion in 'B4R Libraries' started by Erel, Jan 16, 2017.

Similar threads

B4R Library rAdafruitGFX
B4R Library rAdafruitSSD1306 - OLED displays
B4R Library rAdafruitSH1106 - SH1106 lcd
B4R Library Adafruit RGB Led Matrix
B4R Code Snippet GFX and Bitmaps
  1. Erel

    Erel Administrator Staff Member Licensed User

    [​IMG]

    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):
    Code:
    Sub Process_Globals
       
    Public Serial1 As Serial
       
    Private STMP As AdafruitSTMPE610
       
    Private ILI As AdafruitILI93411
       
    Private colors(8As 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(
    109)
       ILI.FillScreen(ILI.COLOR_WHITE)
       ILI.GFX.ConfigureText(
    2, ILI.COLOR_BLACK, True)
       ILI.GFX.SetCursor(
    22)
       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(0256), Rnd(0256), Rnd(0256))
         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, 15038000, ILI.GFX.Width)
       
    Dim fy As Int = MapRange(Y, 13040000, 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
     

    Attached Files:

    Johan Hormaza, rwblinn and Toley like this.
  2. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this in the wishlist forum.
     
  3. Hypnos

    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!
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    You need to change this line in rAdafruit_STMPE610.cpp:
    Code:
    stm = new (backend) Adafruit_STMPE610(CSPin);
     
  5. Hypnos

    Hypnos Active Member Licensed User


    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

    Code:
    #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);
        }
    }
     
  6. Erel

    Erel Administrator Staff Member Licensed User

  7. Hypnos

    Hypnos Active Member Licensed User


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

    Code:
    // 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:

    Code:
    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(
    22)
        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!
     
  8. Erel

    Erel Administrator Staff 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.
     
  9. Hypnos

    Hypnos Active Member Licensed User

    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
     
    spairo and Erel like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice