Share My Creation TFT Text from PC

Display text on TFT screen sent from PC.
Arduino is Nano or Uno. TFT screen is 1.8" driver ST7735. Connected to PC via USB. Color and size are selectable.
B4X:
'   TFT module with ST7735 driver
'   1 LED   3.3 V or 5V via 47 ohm
'   2 SCK   13
'   3 SDA   11
'   4 A0 Or DC  9
'   5 RESET   8
'   6 CS  10
'   7 GND   GND
'   8 VCC   5 V

Sub Process_Globals

    Public Serial1 As Serial
    Private astream As AsyncStreams
    Public CS As Pin
    Public DC As Pin
    Public SDA As Pin
    Public SCK As Pin
    Public RST As Pin
    Public  BLACK   As UInt=0x0000
    Public  BLUE  As UInt=0x01F
    Public  RED   As UInt=0xF800
    Public  GREEN   As UInt=0x07E0
    Public  CYAN   As UInt=0x07FF
    Public  MAGENTA   As UInt=0xF81F
    Public  YELLOW   As UInt=0xFFE0
    Public  WHITE   As UInt=0xFFFF
    Private cColor As UInt
    Private spires, spidata As Byte
    Private cx=2, cy=2, csize=1 As Byte
   
End Sub

Private Sub AppStart
    Serial1.Initialize(9600)
    astream.Initialize(Serial1.Stream, "astream_NewData", Null)
    CS.Initialize(10, CS.MODE_OUTPUT)
    DC.Initialize(9, DC.MODE_OUTPUT)
    SCK.Initialize(13, SCK.MODE_OUTPUT)
    RST.Initialize(8, RST.MODE_OUTPUT)
    SDA.Initialize(11, SDA.MODE_OUTPUT)
    RunNative ("set_spi",Null)
    TFTinit
    rectan(0,0,159,127,WHITE)    'clear screen
   
End Sub

Sub astream_NewData (Buffer() As Byte)
   
    If Buffer.Length > 0 Then
        Select Buffer(0)
            Case 0
                rectan(0,0,159,127,WHITE)    'clear screen
                cx=2
                cy=2
            Case 1
                csize=1
            Case 2
                csize=2
            Case 3
                csize=3
            Case 4
                csize=4
            Case 5
                cColor=0x00
            Case 6
                cColor=0xF800
            Case 7
                cColor=0x07E0
            Case 8
                cColor=0x01F
            Case Else
                drawC(cx,cy,Buffer(0),cColor,csize)
                cx=cx+(csize*6)
                If cx>157 Then
                    cx=2
                    cy=cy+(csize*9)
                End If
                If cy>125 Then cy=2          
        End Select
       
    End If
End Sub


        'draw char
Sub drawC(x As Byte, y As Byte, c As Byte, color As UInt, size As Byte)
    Dim i, j, line As Byte
    Private font() As Byte = Array As Byte( _
    0x00, 0x00, 0x00, 0x00, 0x00, _ ' (space)
    0x00, 0x00, 0x5F, 0x00, 0x00, _ ' !
    0x00, 0x07, 0x00, 0x07, 0x00, _ ' "
    0x14, 0x7F, 0x14, 0x7F, 0x14, _ ' #
    0x24, 0x2A, 0x7F, 0x2A, 0x12, _ ' $
    0x23, 0x13, 0x08, 0x64, 0x62, _ ' %
    0x36, 0x49, 0x55, 0x22, 0x50, _ ' &
    0x00, 0x05, 0x03, 0x00, 0x00, _ ' '
    0x00, 0x1C, 0x22, 0x41, 0x00, _ ' (
    0x00, 0x41, 0x22, 0x1C, 0x00, _ ' )
    0x08, 0x2A, 0x1C, 0x2A, 0x08, _ ' *
    0x08, 0x08, 0x3E, 0x08, 0x08, _ ' +
    0x00, 0x50, 0x30, 0x00, 0x00, _ ' ,
    0x08, 0x08, 0x08, 0x08, 0x08, _ ' -
    0x00, 0x60, 0x60, 0x00, 0x00, _ ' .
    0x20, 0x10, 0x08, 0x04, 0x02, _ ' /
    0x3E, 0x51, 0x49, 0x45, 0x3E, _ ' 0
    0x00, 0x42, 0x7F, 0x40, 0x00, _ ' 1
    0x72, 0x49, 0x49, 0x49, 0x46, _
    0x21, 0x41, 0x49, 0x4D, 0x33, _
    0x18, 0x14, 0x12, 0x7F, 0x10, _
    0x27, 0x45, 0x45, 0x45, 0x39, _ ' 5
    0x3C, 0x4A, 0x49, 0x49, 0x31, _
    0x41, 0x21, 0x11, 0x09, 0x07, _
    0x36, 0x49, 0x49, 0x49, 0x36, _
    0x46, 0x49, 0x49, 0x29, 0x1E, _ ' 9
    0x00, 0x36, 0x36, 0x00, 0x00, _ ' :
    0x00, 0x56, 0x36, 0x00, 0x00, _ ' ;
    0x00, 0x08, 0x14, 0x22, 0x41, _ ' <
    0x14, 0x14, 0x14, 0x14, 0x14, _ ' =
    0x41, 0x22, 0x14, 0x08, 0x00, _ ' >
    0x02, 0x01, 0x51, 0x09, 0x06, _ ' ?
    0x32, 0x49, 0x79, 0x41, 0x3E, _ ' @
    0x7E, 0x11, 0x11, 0x11, 0x7E, _ ' A
    0x7F, 0x49, 0x49, 0x49, 0x36, _ ' B
    0x3E, 0x41, 0x41, 0x41, 0x22, _ ' C
    0x7F, 0x41, 0x41, 0x22, 0x1C, _ ' D
    0x7F, 0x49, 0x49, 0x49, 0x41, _ ' E
    0x7F, 0x09, 0x09, 0x01, 0x01, _ ' F
    0x3E, 0x41, 0x41, 0x51, 0x32, _ ' G
    0x7F, 0x08, 0x08, 0x08, 0x7F, _ ' H
    0x00, 0x41, 0x7F, 0x41, 0x00, _ ' I
    0x20, 0x40, 0x41, 0x3F, 0x01, _ ' J
    0x7F, 0x08, 0x14, 0x22, 0x41, _ ' K
    0x7F, 0x40, 0x40, 0x40, 0x40, _ ' L
    0x7F, 0x02, 0x04, 0x02, 0x7F, _ ' M
    0x7F, 0x04, 0x08, 0x10, 0x7F, _ ' N
    0x3E, 0x41, 0x41, 0x41, 0x3E, _ ' O
    0x7F, 0x09, 0x09, 0x09, 0x06, _ ' P
    0x3E, 0x41, 0x51, 0x21, 0x5E, _ ' Q
    0x7F, 0x09, 0x19, 0x29, 0x46, _ ' R
    0x46, 0x49, 0x49, 0x49, 0x31, _ ' S
    0x01, 0x01, 0x7F, 0x01, 0x01, _ ' T
    0x3F, 0x40, 0x40, 0x40, 0x3F, _ ' U
    0x1F, 0x20, 0x40, 0x20, 0x1F, _ ' V
    0x7F, 0x20, 0x18, 0x20, 0x7F, _ ' W
    0x63, 0x14, 0x08, 0x14, 0x63, _ ' X
    0x03, 0x04, 0x78, 0x04, 0x03, _ ' Y
    0x61, 0x51, 0x49, 0x45, 0x43, _ ' Z
    0x00, 0x00, 0x7F, 0x41, 0x41, _ ' [
    0x02, 0x04, 0x08, 0x10, 0x20, _ ' "\"
    0x41, 0x41, 0x7F, 0x00, 0x00, _ ' ]
    0x04, 0x02, 0x01, 0x02, 0x04, _ ' ^
    0x40, 0x40, 0x40, 0x40, 0x40, _ ' _
    0x00, 0x01, 0x02, 0x04, 0x00, _ ' `
    0x20, 0x54, 0x54, 0x54, 0x78, _ ' a
    0x7F, 0x48, 0x44, 0x44, 0x38, _ ' b
    0x38, 0x44, 0x44, 0x44, 0x20, _ ' c
    0x38, 0x44, 0x44, 0x48, 0x7F, _ ' d
    0x38, 0x54, 0x54, 0x54, 0x18, _ ' e
    0x08, 0x7E, 0x09, 0x01, 0x02, _ ' f
    0x08, 0x14, 0x54, 0x54, 0x3C, _ ' g
    0x7F, 0x08, 0x04, 0x04, 0x78, _ ' h
    0x00, 0x44, 0x7D, 0x40, 0x00, _ ' i
    0x20, 0x40, 0x44, 0x3D, 0x00, _ ' j
    0x00, 0x7F, 0x10, 0x28, 0x44, _ ' k
    0x00, 0x41, 0x7F, 0x40, 0x00, _ ' l
    0x7C, 0x04, 0x18, 0x04, 0x78, _ ' m
    0x7C, 0x08, 0x04, 0x04, 0x78, _ ' n
    0x38, 0x44, 0x44, 0x44, 0x38, _ ' o
    0x7C, 0x14, 0x14, 0x14, 0x08, _ ' p
    0x08, 0x14, 0x14, 0x18, 0x7C, _ ' q
    0x7C, 0x08, 0x04, 0x04, 0x08, _ ' r
    0x48, 0x54, 0x54, 0x54, 0x20, _ ' s
    0x04, 0x3F, 0x44, 0x40, 0x20, _ ' t
    0x3C, 0x40, 0x40, 0x20, 0x7C, _ ' u
    0x1C, 0x20, 0x40, 0x20, 0x1C, _ ' v
    0x3C, 0x40, 0x30, 0x40, 0x3C, _ ' w
    0x44, 0x28, 0x10, 0x28, 0x44, _ ' x
    0x0C, 0x50, 0x50, 0x50, 0x3C, _ ' y
    0x44, 0x64, 0x54, 0x4C, 0x44, _ ' z
    0x00, 0x08, 0x36, 0x41, 0x00, _ ' {
    0x00, 0x00, 0x7F, 0x00, 0x00, _ ' |
    0x00, 0x41, 0x36, 0x08, 0x00, _ ' }
    0x08, 0x08, 0x2A, 0x1C, 0x08, _ ' ->
    0x08, 0x1C, 0x2A, 0x08, 0x08, _ ' <-
    0x00, 0x06, 0x09, 0x09, 0x06) ' degree symbol
   
    For i=0 To 5
    If i = 5 Then
      line = 0
    Else
      line = font(((c-32)*5)+i)
        For j = 0 To 7
              If (line Mod 2)=1 Then
                    If size = 1 Then
                      pixel(x+i, y+j, color)
                    Else  
                      rectan(x+(i*size), y+(j*size), x+(i*size)+size, y+(j*size)+size, color)
                    End If
              End If
          line = line / 2
        Next
    End If
  Next
End Sub

Sub spi(data As Byte)    'send byte over spi
    spidata=data
    RunNative ("spi",Null)
    Return
End Sub


Sub command(cmd As Byte)
    DC.DigitalWrite(False)    'command Mode
    CS.DigitalWrite(False)    'Select the LCD (active low)
      spi(cmd)                'set up data on bus
      CS.DigitalWrite(True)    'Deselect LCD (active low)  
End Sub

Sub send_data(data As Byte)
    DC.DigitalWrite(True)    'data mode
    CS.DigitalWrite(False)    'Select the LCD (active low)
    spi(data)                'set up data on bus
    CS.DigitalWrite(True)    'Deselect LCD (active low)
End Sub



Sub TFTinit
      Dim i As Byte
   
    RST.DigitalWrite(True)    'hardware reset
    Delay(200)
    RST.DigitalWrite(False)
    Delay(10)
    RST.DigitalWrite(True)
    Delay(10)

      command(0x01)    'sw reset
    Delay(200)
     command(0x11)    ' Sleep out
    Delay(200)
   
      command(0x3A)    'color mode
    send_data(0x05)    '16 bits
    'send_data(0x06)    '18 bits

      command(0x36)    'Memory access ctrl (directions)
      send_data(0x60)    '0B1100000=0x60
    'send_data(0x08)    '0x48=left to right,BRG,horizontal   0x40=vertical,RGB
      'command(0x21)    'color inversion on

      command(0x2D)    'color look up table
      send_data(0)
    For i = 1 To 31
        send_data(i * 2)
    Next

      For i = 0 To 63
        send_data(i)
      Next
      send_data(0)
    For i = 1 To 31
    send_data(i * 2)
    Next

      command(0x13)    'Normal display on
    command(0x29)    'Main screen turn on
End Sub

Sub area(x0 As Byte, y0 As Byte,  x1 As Byte, y1 As Byte)
  command(0x2A)    'Column addr set
  send_data(0x00)
  send_data(x0)    ' XSTART
  send_data(0x00)
  send_data(x1)    ' XEND

  command(0x2B)    'Row addr set
  send_data(0x00)
  send_data(y0)    ' YSTART
  send_data(0x00)
  send_data(y1)    ' YEND

  command(0x2C)    'write To RAM
End Sub
 
Sub rectan(x0 As Byte, y0 As Byte,  x1 As Byte, y1 As Byte, color As UInt)
  Dim i As Int
  area(x0,y0,x1,y1)
  For i=(y1 - y0 + 1) * (x1 - x0 + 1) To 0 Step -1  

     DC.DigitalWrite(True)    ' data mode
    CS.DigitalWrite(False)
    spi(color / 256)
    spi(color Mod 256)
    CS.DigitalWrite(True)
   Next
End Sub

Sub pixel(x As Byte, y As Byte, color As UInt)
    area(x,y,x+1,y+1)
    send_data(color / 256)
    send_data(color Mod 256)
End Sub

    #if C
    void spi(B4R::Object* o)
    {  
      SPDR = b4r_main::_spidata;  // Start transmission
      while (!(SPSR & _BV(SPIF)));  // Wait For transmission To complete
      b4r_main::_spires = SPDR;    // received byte
    }
    #End if
   
    #if C
    void set_spi(B4R::Object* o)
    {  
      SPCR = 0B1011100;  // Enable SPI, Master, mode3, set clock rate fck/4 = 4MHz
      //SPSR = 1;    //set clock rate fck/2=2MHz
    }
    #End if
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private sp As Serial
    Private astream As AsyncStreams
    Private tmr1 As Timer
    Private cmb1 As ComboBox
    Private btnOpen As Button
   
    Private Label1 As Label
    Private tc(), tl, ti=0, outB(1) As Byte
    Private text1 As TextArea
    Private btnClear As Button
    Private btnSend As Button
    Private cmbSize As ComboBox
    Private cmbColor As ComboBox

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    sp.Initialize("")
    cmb1.Items.AddAll(sp.ListPorts)
    tmr1.Initialize("tmr1",50)
    tmr1.Enabled=False
    cmbSize.Items.AddAll(Array As String("1", "2", "3", "4"))
    cmbColor.Items.AddAll(Array As String("BLACK", "RED", "GREEN", "BLUE"))
End Sub

Sub cmb1_SelectedIndexChanged(Index As Int, Value As Object)
    btnOpen.Enabled = Index > -1 'enable the button if there is a selected item
End Sub

Sub btnOpen_Action
    sp.Open(cmb1.Value)
    sp.SetParams(9600,8,1,0)
    astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
    btnOpen.Enabled = False
    Label1.Text = "Port Opened"
    btnSend.Enabled=True
    cmbSize.Enabled=True
    cmbColor.Enabled=True
End Sub

Sub cmbSize_SelectedIndexChanged(Index As Int, Value As Object)
    outB(0)=cmbSize.Value
    astream.Write(outB)
End Sub

Sub cmbColor_SelectedIndexChanged(Index As Int, Value As Object)
    outB(0)=cmbColor.SelectedIndex + 5
    astream.Write(outB)
End Sub

Sub btnSend_Action
    tc=text1.Text.GetBytes("UTF8")
    tl=tc.Length
    If tc.Length>0 Then tmr1.Enabled=True
   
End Sub

Sub tmr1_Tick
    outB(0)=tc(ti)
    astream.Write(outB)
    ti=ti+1
    If ti > (tl-1) Then
        tmr1.Enabled=False
        ti=0
    End If
End Sub


Sub btnClear_Action
    outB(0)=0
    astream.Write(outB)
End Sub

Sub MainForm_Closed
    'sp.Close
End Sub

Sub AStream_NewData (Buffer() As Byte)

End Sub
 

Attachments

  • tft_text_b4j.zip
    3.1 KB · Views: 212
  • tft_text_b4r.zip
    3.3 KB · Views: 225
  • tft_text4x3.jpg
    tft_text4x3.jpg
    32.7 KB · Views: 1,475
  • b4j app.gif
    b4j app.gif
    14.6 KB · Views: 189
  • tft_text.jpg
    tft_text.jpg
    203.8 KB · Views: 197
Last edited:
Top