1.8 TFT display module

Announce wiki articles, source code or projects here

1.8 TFT display module

Postby skartalov » Thu Oct 03, 2013 2:07 pm

Hi,

I've recently started to play with cheap 1.8' TFT display like this:

http://www.ebay.co.uk/itm/1-8-Serial-SP ... 3cd595cf9b

The SD card on board is easy to interface with FIREWING, but I like to run the display itself.

I modified the TFT module by BITFOGAV a little bit and I succeed to run this little TFT, featuring ST7735 controller, running on SPI connection.

I still have some issues when loading bitmaps, or displaying filled shapes, but hope to fix that.

If someone want to help, I will share the code.

Image
skartalov
 
Posts: 69
Joined: Sun Sep 15, 2013 1:12 pm

Re: 1.8 TFT display module

Postby David John Barker » Fri Oct 04, 2013 6:38 am

That's good work - please keep us informed of progress...
User avatar
David John Barker
 
Posts: 485
Joined: Thu Nov 08, 2012 12:21 pm

Re: 1.8 TFT display module

Postby skartalov » Sat Oct 05, 2013 10:03 am

After testing all functions, (shapes, lines, Images) in both Portrait and Landscape mode, I can confirm that the ST7735 TFT Module is complete.

I am not good in WiKi, so I am sharing the code here.

If somebody wants to help, please publish it in WiKi on my behalf.

Thanks again for the great compiler.

Image

Here it is the modified TFT.BAS:

Code: Select all
'*****************************************************************************
'*  Name    : Tft.BAS                                                        *
'*  Author  : David John Barker                                              *
'*          : Steven Wright                                                  *
'*  Notice  : Copyright (c) 2012 Mecanique                                   *
'*          : All Rights Reserved                                            *
'*  Date    : 11/05/2012                                                     *
'*  Version : 1.0                                                            *
'*  Notes   :   ST7735 Edit by Simeon Kartalov                               *
'*          :                                                                *
'*****************************************************************************
module Tft

' validate model...
#option TFT_MODEL = ST7735
#if not (TFT_MODEL in (S6D1121,ST7735))
   #error TFT_MODEL, "Invalid option. TFT model not recognized."
#endif



imports TFT_MODEL
imports TftGraphic
imports Math

' import driver...
#if TFT_MODEL = S6D1121
public const Width = S6D1121.Width
public const Height = S6D1121.Height

public Clear as S6D1121.Clear
public SetOrientation as S6D1121.SetOrientation
public SetPixel as S6D1121.SetPixel
public SetPenColor as S6D1121.SetPenColor
public SetBrushColor as S6D1121.SetBrushColor
public SetBitmap as S6D1121.SetBitmap

#define IMPLEMENTS_HLINE
#define IMPLEMENTS_VLINE
private SetHLine as S6D1121.HLine
private SetVLine as S6D1121.VLine
#endif

#if TFT_MODEL = ST7735
public const Width = ST7735.Width
public const Height = ST7735.Height

public Clear as ST7735.Clear
public SetOrientation as ST7735.SetOrientation
public SetPixel as ST7735.SetPixel
public SetPenColor as ST7735.SetPenColor
public SetBrushColor as ST7735.SetBrushColor
public SetBitmap as ST7735.SetBitmap
Public SetBounds as ST7735.SetBounds
public WriteData as ST7735.WriteWordData

'#define IMPLEMENTS_HLINE
'#define IMPLEMENTS_VLINE
'private SetHLine as ST7735.HLine
'private SetVLine as ST7735.VLine
#endif

' make graphics variables available for this module interface
public SetFont as Graphic.SetFont
public TextWidth as Graphic.TextWidth
public Pen as Graphic.Pen
public Brush as Graphic.Brush
public Font as Graphic.Font
public Align as Graphic.Align
public Pos as GraphicPosition

' default is equal apect ratio, change in GLCD model core...
#option GLCD_ASPECT_RATIO = 100
private const _aspectRatio = GLCD_ASPECT_RATIO

'****************************************************************************
'* Name    : line                                                           *
'* Purpose : Draws a line using a Bresenham line drawing algorithm          *
'****************************************************************************
public sub line(x1 as UShort, y1 as UShort, x2 as UShort, y2 as UShort) 
   dim p, diff as integer
   dim incx, incy  as integer
   dim dx_ge_dy as boolean
   dim dy, dx, delta as uShort
   dim penPosition as byte
   dim length as byte
   
   ' x direction...
   incx = 1
   if x1 > x2 then
      incx = -1
   end if   
 
   ' y direction
   incy = 1
   if y1 > y2 then
      incy = -1
   end if   

   dx = abs(x2 - x1)
   dy = abs(y2 - y1)

   if dx >= dy then
      dx_ge_dy = true
      dy = dy * 2
      p = dy - dx
      delta = dx
   else
      dx_ge_dy = false
      dx = dx * 2
      p = dx - dy
      delta = dy
   end if   
   diff = p - delta

   delta += 1
   penPosition = 0
   length = cbyte(Pen.Style)
   
   do                 
      if Pen.Style <> PenStyle.IsNormal then
         if penPosition < length then
            SetPixel(x1, y1)
         end if
         penPosition += 1
         if penPosition > length then
            penPosition = 0
         end if
      else
         SetPixel(x1, y1)
      end if
      if P < 0 then
         if dx_ge_dy then
            p += dy
            x1 += incx
         else
            p += dx
            y1 += incy
         end if   
      else
         p += diff
         x1 += incx
         y1 += incy
     end if
     delta -= 1
   loop until delta = 0
end sub

'****************************************************************************
'* Name    : Plot (PRIVATE)                                                 *
'* Purpose : Plots a set of ellipse quadrant points                         *
'****************************************************************************
sub Plot(pCX as UShort, pCY as UShort, posX as UShort, posY as UShort)
   SetPixel(pCX + posX, pCY + posY)
   SetPixel(pCX - posX, pCY + posY)
   SetPixel(pCX - posX, pCY - posY)
   SetPixel(pCX + posX, pCY - posY) 
end sub

'****************************************************************************
'* Name    : Ellipse                                                        *
'* Purpose : Draws an ellipse                                               *
'*         : A Fast Bresenham Type Algorithm For Drawing Ellipses           *
'*         : John Kennedy                                                   *
'*         : Mathematics Department, Santa Monica College                   *
'*         : Santa Monica, CA 90405 - http:'homepage.smc.edu/kennedy_john  *
'****************************************************************************
public sub Ellipse(pCX as UShort, pCY as UShort, pXRadius as UShort, pYRadius as UShort)
  dim x, y as UShort
  dim xChange, yChange as integer
  dim ellipseError as integer
  dim twoASquare, twoBSquare as integer
  dim stoppingX, stoppingY as integer
  dim penPosition as byte
  dim length as byte

  length = cbyte(Pen.Style)

  twoASquare = 2 * pXRadius * pXRadius
  twoBSquare = 2 * pYRadius * pYRadius
  x = pXRadius
  y = 0
  xChange = pYRadius * pYRadius * (1 - cint(2 * pXRadius))

  yChange = pXRadius * pXRadius
  ellipseError = 0
  stoppingX = twoBSquare * pXRadius
  stoppingY = 0
  penPosition = 0
  while stoppingX >= stoppingY 
      if Pen.Style <> PenStyle.IsNormal then
         if penPosition < length then
            Plot(pCX, pCY, x, y)
         end if
         penPosition += 1
         if penPosition > length then
            penPosition = 0
         end if
      else
         Plot(pCX, pCY, x, y)
      end if
      y += 1
      stoppingY += twoASquare
      ellipseError +=  yChange
      yChange += twoASquare
      if (2 * ellipseError + xChange) > 0  then
          x -= 1
          stoppingX -= twoBSquare
          ellipseError += xChange
          xChange += twoBSquare
      end if
  end while

  x = 0
  y = pYRadius
  xChange = pYRadius * pYRadius
  yChange = pXRadius * pXRadius * (1 - cint(2 * pYRadius))

  ellipseError = 0
  stoppingX = 0
  stoppingY = twoASquare * pYRadius
  penPosition = 0
  while stoppingX <= stoppingY
      if Pen.Style <> PenStyle.IsNormal then
         if penPosition < length then
            Plot(pCX, pCY, x, y)
         end if
         penPosition += 1
         if penPosition > length then
            penPosition = 0
         end if
      else
         Plot(pCX, pCY, x, y)
      end if
      x += 1
      stoppingX += twoBSquare
      ellipseError += xChange
      xChange += twoBSquare
      if (2 * ellipseError + yChange) > 0  then
          y -= 1
          stoppingY -= twoASquare
          ellipseError += yChange
          yChange += twoASquare
      end if
  end while
end sub

'****************************************************************************
'* Name    : Circle                                                         *
'* Purpose : Draws a circle, Y is scaled to compensate for aspect ratio     *
'****************************************************************************
public sub Circle(centerX as UShort, centerY as UShort, radius as UShort)   
   #if GLCD_ASPECT_RATIO <> 100
   Ellipse(centerX, centerY, radius, radius * _aspectRatio / 100)
   #else
   Ellipse(centerX, centerY, radius, radius)
   #endif
end sub

'****************************************************************************
'* Name    :                                                                *
'* Purpose :                                                                *
'****************************************************************************
public sub FillCircle(x as ushort, y as ushort, radius as ushort)
   dim lastPen as GraphicPen = Pen
   Pen.Color.Value = Brush.Color.Value
   Pen.Style = PenStyle.IsNormal
   for y1 as short = -radius to 0
      for x1 as short = -radius to 0
         if (x1 * x1 + y1 * y1 <= radius * radius) then
            HLine(x + x1, x + abs(x1), y + y1) 
            HLine(x + x1, x + abs(x1), y - y1)
            exit for
         end if
      next
   next
   Pen = lastPen
end sub

'****************************************************************************
'* Name    : HLine (PRIVATE)                                                *
'* Purpose : Draws a horizontal line                                        *
'****************************************************************************
sub HLine(x1 as UShort, x2 as UShort, y as UShort)
   #ifdef IMPLEMENTS_HLINE
   if Pen.Style = PenStyle.IsNormal then
      SetHLine(x1,x2,y)
      exit sub
   end if
   #endif

   dim penPosition as byte
   dim length as byte
   penPosition = 0
   length = cbyte(Pen.Style)
   while x1 <= x2
      if Pen.Style <> PenStyle.IsNormal then
         if penPosition < length then
            SetPixel(x1,y)
         end if
         penPosition += 1
         if penPosition > length then
            penPosition = 0
         end if
      else
         SetPixel(x1,y)
      end if
      x1 += 1
   end while   
end sub

'****************************************************************************
'* Name    : VLine (PRIVATE)                                                *
'* Purpose : Draw a vertical line                                           *
'****************************************************************************
sub VLine(x as UShort, y1 as UShort, y2 as UShort)
   #ifdef IMPLEMENTS_VLINE
   if Pen.Style = PenStyle.IsNormal then
      SetVLine(x,y1,y2)
      exit sub   
   end if
   #endif
   
   dim penPosition as byte
   dim length as byte
   penPosition = 0
   length = cbyte(Pen.Style)
   while y1 <= y2
      if Pen.Style <> PenStyle.IsNormal then
         if penPosition < length then
            SetPixel(x,y1)
         end if
         penPosition += 1
         if penPosition > length then
            penPosition = 0
         end if
      else
         SetPixel(x,y1)
      end if
      y1 += 1
   end while   
end sub

'****************************************************************************
'* Name    : Rectangle                                                      *
'* Purpose : Draws a rectangle                                              *
'****************************************************************************
public sub Rect(x1 as UShort, y1 as UShort, x2 as UShort, y2 as UShort)
   dim Swap as UShort
   
   if x2 < x1 then
      Swap = x1
      x1 = x2
      x2 = Swap
   end if

   if y2 < y1 then
      Swap = y1
      y1 = y2
      y2 = Swap
   end if
   
   HLine(x1, x2, y1)
   VLine(x2, y1, y2)
   HLine(x1, x2, y2)
   VLine(x1, y1, y2)
end sub

'****************************************************************************
'* Name    : Rectangle                                                      *
'* Purpose : Draws a rectangle                                              *
'****************************************************************************
public sub FillRect(x1 as UShort, y1 as UShort, x2 as UShort, y2 as UShort)
   dim Swap as UShort
   
   if x2 < x1 then
      Swap = x1
      x1 = x2
      x2 = Swap
   end if

   if y2 < y1 then
      Swap = y1
      y1 = y2
      y2 = Swap
   end if
   
   dim lastPen as GraphicPen = Pen
   Pen.Color.Value = Brush.Color.Value
   Pen.Style = PenStyle.IsNormal
   for y as ushort = y1 to y2
      HLine(x1, x2, y)
   next
   Pen = lastPen
end sub

'****************************************************************************
'* Name    : RoundRect                                                      *
'* Purpose : Draws a rectangle with rounded edges                           *
'****************************************************************************
public sub RoundRect(x1 as ushort, y1 as ushort, x2 as ushort, y2 as ushort)
   dim Swap as UShort
   if x2 < x1 then
      Swap = x1
      x1 = x2
      x2 = Swap
   end if

   if y2 < y1 then
      Swap = y1
      y1 = y2
      y2 = Swap
   end if
   
   if (x2 - x1 > 4) and (y2 - y1 > 4) then
      SetPixel(x1+1,y1+1)
      SetPixel(x2-1,y1+1)
      SetPixel(x1+1,y2-1)
      SetPixel(x2-1,y2-1)
      HLine(x1 + 2, x2 - 2, y1)
      HLine(x1 + 2, x2 - 2, y2)
      VLine(x1, y1 + 2, y2 - 2)
      VLine(x2, y1 + 2, y2 - 2)
   end if
end sub

'****************************************************************************
'* Name    : FillRoundRect                                                  *
'* Purpose : Draws a filled rectangle with rounded edges                    *
'****************************************************************************
public sub FillRoundRect(x1 as ushort, y1 as ushort, x2 as ushort, y2 as ushort)
   dim Swap as UShort
   if x2 < x1 then
      Swap = x1
      x1 = x2
      x2 = Swap
   end if

   if y2 < y1 then
      Swap = y1
      y1 = y2
      y2 = Swap
   end if
   
   if (x2 - x1 > 4) and (y2 - y1 > 4) then 
      dim lastPen as GraphicPen = Pen
      Pen.Color.Value = Brush.Color.Value
      Pen.Style = PenStyle.IsNormal
      for i as ushort = 0 to (y2 - y1) / 2
         select i
            case 0:
               HLine(x1 + 2, x2 - 2, y1 + i)
               HLine(x1 + 2, x2 - 2, y2 - i)
            case 1:
               HLine(x1 + 1, x2 - 1, y1 + i)
               HLine(x1 + 1, x2 - 1, y2 - i)
            case else
               HLine(x1, x2, y1 + i)
               HLine(x1, x2, y2 - i)
         end select
      next
      Pen = lastPen
   end if
end sub

'****************************************************************************
'* Name    : Square                                                         *
'* Purpose : Draws a square, Y is scaled to compensate for aspect ratio     *
'****************************************************************************
public sub Square(posX as UShort, posY as UShort, size as UShort)
   #if GLCD_ASPECT_RATIO <> 100
   Rect(posX, posY, posX + size, posY + size * _aspectRatio / 100)
   #else
   Rect(posX, posY, posX + size, posY + size)
   #endif
end sub

'****************************************************************************
'* Name    : MoveTo                                                         *
'* Purpose : Moves graphics cursor to given point before calling LineTo()   *
'****************************************************************************
public sub MoveTo(posX as UShort, posY as UShort)
   Pos.x = posX
   Pos.y = posY
end sub

'****************************************************************************
'* Name    : LineTo                                                         *
'* Purpose : Draws a line from the current graphics cursor position         *
'****************************************************************************
public sub LineTo(posX as UShort, posY as UShort)
   line(Pos.x, Pos.y, posX, posY)
   Pos.x = posX
   Pos.y = posY
end sub

'****************************************************************************
'* Name    : WriteChar(PRIVATE)                                             *
'* Purpose : Write a single character to GLCD                               *
'*         : This routine only supports fonts with a y scan line (&H8x xx)  *
'*         : It's device independent, which makes it pretty slow - look at  *
'*         : implementing a device specific routine in the GLCD driver      *
'****************************************************************************
private sub WriteChar()
   dim x, y as ushort

   dim fontByte as byte
   dim lastPosY as ushort = Pos.y
   dim penColor as GraphicColor = Pen.Color
     
   dim posx as ushort = pos.x
   dim posy as ushort = pos.y
   
   if Font.IsScanY then
      if Brush.Style <> BrushStyle.IsClear then
         FillRect(Pos.x, Pos.y, Pos.x + Font.Width, Pos.y + Font.Height)
      end if
      x = 0
      do
         Posy = lastPosY         
         y = 0
         do
            if y mod 8 = 0 then
               fontByte = ReadFontByte
            end if   
           
            if fontByte.0 = 1 then
               SetPixel(Posx,Posy)
            end if
                         
            fontByte = fontByte >> 1
            Posy = posy + 1
            y = y + 1
         loop until y = Font.Height
         Posx = posx + 1
         x = x + 1
      loop until x = Font.Width 
   end if     

   Pen.Color = penColor 
   pos.x = posx
   pos.y = lastPosY 
end sub

'****************************************************************************
'* Name    : WriteFontChar                                                  *
'* Purpose : Write a font character to the GLCD                             *
'****************************************************************************
sub WriteFontChar(character as char)
   LoadCharAddress(character)
   if Font.IsVariable then
      Font.Width = ReadFontByte
   end if
   WriteChar 
end sub

'****************************************************************************
'* Name    : WriteLetterSpacing                                             *
'* Purpose :                                                                *
'****************************************************************************
sub WriteFontSpace()
   dim index as byte
   index = Font.LetterSpacing     
   while index > 0
      LoadLetterSpacing
      WriteChar
      index -= 1
   end while
end sub

'****************************************************************************
'* Name    : WriteItem (OVERLOAD)                                           *
'* Purpose : Writes a character to GLCD                                     *
'****************************************************************************
sub WriteItem(character as char)
   WriteFontChar(character)
end sub

'****************************************************************************
'* Name    : WriteItem (OVERLOAD)                                           *
'* Purpose : Writes a string to GLCD                                        *
'****************************************************************************
sub WriteItem(text as string)
   dim index as byte = 0
   dim ch as char = text(index)
   while ch <> nothing
      WriteFontChar(ch)
      index += 1
      ch = text(index)
      if Font.IsVariable and ch <> nothing then
         WriteFontSpace
      end if   
   end while
end sub

'****************************************************************************
'* Name    : SetLocationX (PRIVATE)                                         *
'* Purpose : Set cursor x                                                   *
'****************************************************************************
sub SetLocationX(posX as UShort)
   Pos.x = posX
end sub

'****************************************************************************
'* Name    : SetLocationY (PRIVATE)                                         *
'* Purpose : Set cursor y                                                   *
'****************************************************************************
sub SetLocationY(posY as UShort)
   Pos.y = posY
end sub

'****************************************************************************
'* Name    : Write (COMPOUND)                                               *
'* Purpose : Write one or more items to GLCD                                *
'****************************************************************************
public compound sub Write(WriteItem)

'****************************************************************************
'* Name    : WriteAt (COMPOUND)                                             *
'* Purpose : Write one or more items at position x, y                       *
'****************************************************************************
public compound sub WriteAt(SetLocationX, SetLocationY, WriteItem)

'****************************************************************************
'* Name    : WriteStr                                                       *
'* Purpose : Write one item centred in x At position x, y                   *
'****************************************************************************
public sub WriteStr(posX as UShort, posY as UShort, text as string)
   Pos.x = posX
   Pos.y = posY
   if Align = TextAlign.Center then
      Pos.x  -= TextWidth(text) / 2
   elseif Align = TextAlign.Right then
      Pos.x -= TextWidth(text)
   end if     
   WriteItem(text)
end sub


end module


And here is the new ST7735.BAS:

Code: Select all
'*****************************************************************************
'*  Name    : ST7735.bas                                                     *
'*  Author  : Simeon Kartalov                                                *
'*  Notice  : Copyright (c) 2013 Mecanique                                   *
'*          : All Rights Reserved                                            *
'*  Date    : 05/10/2013                                                     *
'*  Version : 1.0                                                            *
'*  Notes   :                                                                *
'*          :                                                                *
'*****************************************************************************
Module ST7735

imports TftGraphic

' Option section - if no values are given in the main program, this section
' will initialise them - all values are verified and if any errors are found,
' a custom message will be displayed in the main user program...

' Delay (US) of Shift clock...
#option SHIFT_CLK_TFT = 0 
#if IsOption(SHIFT_CLK_TFT) And Not (SHIFT_CLK_TFT in (0, 1, 2, 4, 8))
   #error SHIFT_CLK_TFT, "Invalid option. _data delay must be 0, 1, 2, 4 or 8 us."
#endif

' RS pin
' Low: indicates that data is commands,
' High: indicates that data is display data.
#option TFT_RS = A5
#if IsOption(TFT_RS) And Not IsValidPortPin(TFT_RS)
   #error TFT_RS, "Invalid option. TFT_RS must be a valid pin."
#endif

' RW pin (write strobe signal - active low)...
#option TFT_RW = A4
#if IsOption(TFT_RW) And Not IsValidPortPin(TFT_RW)
   #error TFT_RW, "Invalid option. TFT_RW must be a valid pin."
#endif

' CS pin (chip selection pin - active low)...
#option TFT_CS = A3
#if IsOption(TFT_CS) And Not IsValidPortPin(TFT_CS)
   #error TFT_CS, "Invalid option. TFT_CS must be a valid pin."
#endif

' data pins SPI interface...
#option TFT_SCK = A1
#option TFT_SDA = A2


' validate data pins...
#if IsOption(TFT_SCK) And Not IsValidPortPin(TFT_SCK)
   #error TFT_SCK, "Invalid option. TFT_SCK must be a valid pin."
#endif
#if IsOption(TFT_SDA) And Not IsValidPortPin(TFT_SDA)
   #error TFT_SDA, "Invalid option. TFT_SDA must be a valid pin."
#endif


' bring delay option into the module...
private Const _delayUS As Byte = SHIFT_CLK_TFT

' width and height constants...
public const Width = 128
public const Height = 160

' bring pin options into the program...
private _sck as TFT_SCK.TFT_SCK@
private _sda as TFT_SDA.TFT_SDA@
private _rs As TFT_RS.TFT_RS@     
private _rw As TFT_RW.TFT_RW@       
private _cs As TFT_CS.TFT_CS@       

' variables...
private _orientation as Orientation

'****************************************************************************
'* Name    : MakeOutput                                                     *
'* Purpose : Make data pins output                                          *
'****************************************************************************
private Inline Sub MakeOutput()
   output(_sck)
   output(_sda)
   output(_rs)
   output(_rw)
   output(_cs)
End Sub

'****************************************************************************
'* Name    : Delay                                                          *
'* Purpose : Variable delay command                                         *
'****************************************************************************
inline sub Delay()
   #if _core = 32
   if _delayUS = 0 then delayus(1)
   #endif
   delayus(_delayUS)
end sub

private Sub scl_strobe()
    _sck=0 
    _sck=1
End Sub




Public Sub WriteCommand (pData As Byte)
    _rw=0
    _cs=0 
    _SDA=pData.7
    scl_strobe()
    _SDA=pData.6
    scl_strobe()
    _SDA=pData.5
    scl_strobe()
    _SDA=pData.4
    scl_strobe()
    _SDA=pData.3
    scl_strobe()
    _SDA=pData.2
    scl_strobe()
    _SDA=pData.1
    scl_strobe()
    _SDA=pData.0
    scl_strobe()
    _cs=1
End Sub

Public Sub WriteData (pData As Byte)
    _rw=1
    _cs=0
    _SDA=pData.7
    scl_strobe()
    _SDA=pData.6
    scl_strobe()
    _SDA=pData.5
    scl_strobe()
    _SDA=pData.4
    scl_strobe()
    _SDA=pData.3
    scl_strobe()
    _SDA=pData.2
    scl_strobe()
    _SDA=pData.1
    scl_strobe()
    _SDA=pData.0
    scl_strobe()
    _cs=1
End Sub

Public Sub WriteWordData (pData As uShort)
    _rw=1
    _cs=0
    _SDA=pData.15
    scl_strobe()
    _SDA=pData.14
    scl_strobe()
    _SDA=pData.13
    scl_strobe()
    _SDA=pData.12
    scl_strobe()
    _SDA=pData.11
    scl_strobe()
    _SDA=pData.10
    scl_strobe()
    _SDA=pData.9
    scl_strobe()
    _SDA=pData.8
    scl_strobe()
    _SDA=pData.7
    scl_strobe()
    _SDA=pData.6
    scl_strobe()
    _SDA=pData.5
    scl_strobe()
    _SDA=pData.4
    scl_strobe()
    _SDA=pData.3
    scl_strobe()
    _SDA=pData.2
    scl_strobe()
    _SDA=pData.1
    scl_strobe()
    _SDA=pData.0
    scl_strobe()
    _cs=1
End Sub

'****************************************************************************
'* Name    : WriteCommandData                                               *
'* Purpose : Write Command and Data to TFT                                  *
'****************************************************************************
private sub WriteCommandData(command as byte, data as byte)
   WriteCommand(command)
   WriteData(data)
end sub

'****************************************************************************
'* Name    : SetOrientation                                                 *
'* Purpose : Set orientation to either portrait or landscape                *
'****************************************************************************
public sub SetOrientation(orient as Orientation)
   _orientation = orient
end sub

'****************************************************************************
'* Name    : SetPenColor                                                    *
'* Purpose : Set RGB pen color                                              *
'****************************************************************************
public sub SetPenColor(r as byte, g as byte, b as byte)
   Pen.Color.Value.Byte1 = ((r and 248) or g >> 5)
   Pen.Color.Value.Byte0 = ((g and 28) << 3 or b >> 3)
end sub

'****************************************************************************
'* Name    : SetPenColor                                                    *
'* Purpose : Set packed RGB pen color                                       *
'****************************************************************************
public sub SetPenColor(colour as ushort)
   Pen.Color.Value = colour
end sub

'****************************************************************************
'* Name    : SetBrushColor                                                  *
'* Purpose : Set RGB brush color                                            *
'****************************************************************************
public sub SetBrushColor(r as byte, g as byte, b as byte)
   Brush.Color.Value.Byte1 = ((r and 248) or g >> 5)
   Brush.Color.Value.Byte0 = ((g and 28) << 3 or b >> 3)
end sub

'****************************************************************************
'* Name    : SetBrushColor                                                  *
'* Purpose : Set packed RGB brush color                                     *
'****************************************************************************
public sub SetBrushColor(color as integer)
   Brush.Color.Value = color
end sub

'****************************************************************************
'* Name    : SetBounds                                                      *
'* Purpose : Set TFT update region                                          *
'****************************************************************************
public sub SetBounds(x1 as ushort, y1 as ushort, x2 as ushort, y2 as ushort)
   if (_orientation = Orientation.IsLandscape) then 
      dim tmp as ushort
     
      ' swap(x1, y1)
      tmp = x1
      x1 = y1
      y1 = tmp
         
      ' swap(x2, y2)
      tmp = x2
      x2 = y2
      y2 = tmp     
     
      y1 = (height - 1) - y1
      y2 = (height - 1) - y2
     
      ' swap(y1, y2)
      tmp = y1
      y1 = y2
      y2 = tmp
   end if

   'WriteCommandData(&H0046,(x2 << 8) or x1)
   'WriteCommandData(&H0047,y2)
   'WriteCommandData(&H0048,y1)
   'WriteCommandData(&H0020,x1)
   'WriteCommandData(&H0021,y1)
   'WriteCommand(&H0022)
   
    WriteCommand($2A)         '//Column Address Set     
    WriteWordData(x1)           
    WriteWordData(x2)
               
    WriteCommand($2B)         '//Row Address Set         
    WriteWordData(y1)
    WriteWordData(y2)
   
    WriteCommand($2c)
       
End Sub


'****************************************************************************
'* Name    : ClearXY                                                        *
'* Purpose : Set TFT update region to whole of screen                       *
'****************************************************************************
private sub ClearXY()
   if (_orientation = Orientation.IsLandscape) then
      SetBounds(0,0,Height - 1,Width - 1)
   else
      SetBounds(0,0,Width - 1,Height - 1)
   end if
   
end sub

'****************************************************************************
'* Name    : SetPixel                                                       *
'* Purpose : Set a single pixel value                                       *
'****************************************************************************
public sub SetPixel(x as integer, y as integer)
   SetBounds(x, y, x, y)
   WriteWordData(Pen.Color.Value)
   _cs=1
end sub

'****************************************************************************
'* Name    : Fill                                                           *
'* Purpose : Fill the whole of the screen in a given color                  *
'****************************************************************************
private sub Fill(data as ushort)
   ClearXY()
   for i as ushort = 0 to height-1
    for j as ushort = 0 to width -1   
         WriteWordData(data)           
    next
   next
   _cs=1
end sub

 
'****************************************************************************
'* Name    : Clear                                                          *
'* Purpose : Clear screen with optional packed RGB color value              *
'****************************************************************************
public sub Clear(optional color as ushort = 0)
   
   Fill(color)
   
end sub

'****************************************************************************
'* Name    : Clear                                                          *
'* Purpose : Clear the screen using RGB color values                        *
'****************************************************************************
public sub Clear(r as byte, g as byte, b as byte)
   dim color as ushort
   Color.Byte1 = ((r and 248) or g >> 5)
   Color.Byte0 = ((g and 28) << 3 or b >> 3)
   Fill(color)
end sub

'****************************************************************************
'* Name    : HLine                                                          *
'* Purpose : Optimised horizontal line drawing                              *
'****************************************************************************
public sub HLine(x1 as ushort, x2 as ushort, y1 as ushort) 
   dim len as ushort = x2 - x1 + 1
   SetBounds(x1, y1, x1 + len - 1, y1)
   while len > 0 
      WriteData(Pen.Color.Value)
      len = len - 1
   end while
   high(_cs)
end sub

'****************************************************************************
'* Name    : VLine                                                          *
'* Purpose : Optimised vertical line drawing                                *
'****************************************************************************
public sub VLine(x1 as UShort, y1 as UShort, y2 as UShort)
   dim len as ushort = y2 - y1 + 1
   SetBounds(x1, y1, x1, y1 + len - 1)
   while len > 0 
      WriteData(Pen.Color.Value)
      len = len - 1
   end while
   high(_cs)
end sub

'****************************************************************************
'* Name    : SetBitmap                                                      *
'* Purpose : Draw a ROM bitmap at position x,y                              *
'****************************************************************************
public sub SetBitmap(x as ushort, y as ushort, byrefConst data() as ushort)
   dim width as ushort = data(0)
   dim height as ushort = data(1)
   dim index as ushort = 2
     
     
   if (_orientation = Orientation.IsPortrait) then
      SetBounds(x, y, x + width - 1, y + height - 1)
      while index < ubound(data) 
         WritewordData(data(index))
         index += 1
      end while 
   else 
      dim ty as ushort = 0
      while ty < height
         SetBounds(x, y + ty, x + width - 1, y + ty)
         dim tx as ushort = width - 1
         while true   
            dim index as ushort = (ty * width) + tx + 2
            WritewordData(data(index))
            if tx = 0 then exit while 
            tx = tx - 1
         end while
         ty += 1
      end while
   end if
   _cs=1
end sub

'****************************************************************************
'* Name    : Main                                                           *
'* Purpose : Module entry point                                             *
'****************************************************************************
sub Main() 
   
   _orientation = Orientation.IsPortrait
   MakeOutput()
   _cs=0
   _rs=0
    DelayMS(100)
   _rs=1
    DelayMS(100)
WriteCommand($11)             
DelayMS (150)
'//ST7735R Frame Rate
WriteCommand($B1)
WriteData($01)
WriteData($2C)
WriteData($2D)
WriteCommand($B2)
WriteData($01)
WriteData($2C)
WriteData($2D)
WriteCommand($B3)
WriteData($01)
WriteData($2C)
WriteData($2D)
WriteData($01)
WriteData($2C)
WriteData($2D)
WriteCommand($B4) '//Column inversion
WriteData($07)    '//$07
'//ST7735R Power Sequence
WriteCommand($C0)
WriteData($A2)
WriteData($02)
WriteData($84)
WriteCommand($C1)
WriteData($C5)
WriteCommand($C2)
WriteData($0A)
WriteData($00)
WriteCommand($C3)
WriteData($8A)
WriteData($2A)
WriteCommand($C4)
WriteData($8A)
WriteData($EE) 
WriteCommand($C5) '//VCOM
WriteData($0E) 
WriteCommand($36) '//MX, MY, RGB mode
WriteData($c0)    '//$80
'//ST7735R Gamma Sequence
WriteCommand($e0)
WriteData($0f)
WriteData($1a)
WriteData($0f)
WriteData($18)
WriteData($2f)
WriteData($28)
WriteData($20)
WriteData($22)
WriteData($1f)
WriteData($1b)
WriteData($23)
WriteData($37)
WriteData($00)
WriteData($07)
WriteData($02)
WriteData($10)
WriteCommand($e1)
WriteData($0f)
WriteData($1b)
WriteData($0f)
WriteData($17)
WriteData($33)
WriteData($2c)
WriteData($29)
WriteData($2e)
WriteData($30)
WriteData($30)
WriteData($39)
WriteData($3f)
WriteData($00)
WriteData($07)
WriteData($03)
WriteData($10) 
WriteCommand($2a)
WriteData($00)
WriteData($00)
WriteData($00)
WriteData($7f)
WriteCommand($2b)
WriteData($00)
WriteData($00)
WriteData($00)
WriteData($9f)
WriteCommand($F0)      '//Enable test command 
WriteData($01)
WriteCommand($F6)      '//Disable ram power save mode
WriteData($00) 
WriteCommand($3A)        '//65k mode
WriteData($05)         
WriteCommand($29)     '//Display on
_cs=1 
   SetPenColor(255, 255, 255)
   SetBrushColor(0, 0, 0)
end sub

End Module
skartalov
 
Posts: 69
Joined: Sun Sep 15, 2013 1:12 pm

Re: 1.8 TFT display module

Postby bitfogav » Sat Oct 05, 2013 7:29 pm

That's great!, its nice to see that it is possible to add other TFT driver's to the TFT Module. I've actually got some of those small display's on order, I'll have to try out your module when they arrive. :)

When I get time I will add your module to the TFT wiki page, Thank you for sharing.
User avatar
bitfogav
 
Posts: 75
Joined: Sat Nov 17, 2012 11:46 am
Location: UK

Re: 1.8 TFT display module

Postby David John Barker » Sun Oct 06, 2013 10:10 am

I posted a basic wiki article here

http://www.firewing.info/pmwiki.php?n=FirewingUser.ST7735

feel free to add to it - for example, any code as I don't have one of the displays here to test. Note that the IDE will automatically unzip a user library file and install. Instructions on the page. In addition, clicking on the down arrow in the explorer window and selecting "View User Library..." will do just that - you can then drag and drop files into the user library space directly.
User avatar
David John Barker
 
Posts: 485
Joined: Thu Nov 08, 2012 12:21 pm

Re: 1.8 TFT display module

Postby bitfogav » Wed Oct 16, 2013 9:13 pm

skartalov can you please confirm what the connections are from the display to firewing board?..

*Edit* This is what I think it is for the tft display, looking at your code:
RESET = A5
AO = A4
SDA = A3
SCK = A2
CS = A1
User avatar
bitfogav
 
Posts: 75
Joined: Sat Nov 17, 2012 11:46 am
Location: UK

Re: 1.8 TFT display module

Postby bitfogav » Wed Oct 16, 2013 10:10 pm

Got it!.. nice small tft display :)
smalltft.jpg
smalltft.jpg (16.67 KiB) Viewed 6595 times


My sample code:
Code: Select all
clock = 80

#option TFT_MODEL = ST7735
imports Tft
imports TftGraphic

imports ArialSmall

' program entry point
Sub Main()
   Tft.Clear 
   Tft.Brush.Style = BrushStyle.IsClear
   Tft.SetFont(Arial) 
   Tft.WriteAt(10,10,"Verdana") 
   Tft.SetFont(ArialBold)
   Tft.WriteAt(10,35,"Verdana Bold")
   Tft.SetFont(ArialItalic)
   Tft.WriteAt(10,60,"Verdana Italic")
   Tft.SetFont(ArialBoldItalic)
   Tft.WriteAt(10,85,"Verdana Bold Italic")       
End Sub


You might want to download the Arial font module I used:
ArialSmall.zip
(10.72 KiB) Downloaded 432 times
User avatar
bitfogav
 
Posts: 75
Joined: Sat Nov 17, 2012 11:46 am
Location: UK

Re: 1.8 TFT display module

Postby skartalov » Thu Oct 17, 2013 6:01 am

Good, I am glad to see you got it running. :-)
skartalov
 
Posts: 69
Joined: Sun Sep 15, 2013 1:12 pm


Return to Firewing Wiki

Who is online

Users browsing this forum: No registered users and 1 guest

cron

x