2.2' TFT module (ILI9225B chipset)

Announce wiki articles, source code or projects here

2.2' TFT module (ILI9225B chipset)

Postby skartalov » Sat Oct 26, 2013 9:31 am

Hi,
I am happy to present you the next TFT driver based about the ILI9225B chip.

This one is 2.2' TFT (176x220 resolution, also has touch screen), using 4 wires SPI mode.

http://www.aliexpress.com/item/2-2-SPI- ... 55135.html

I succeed to write a driver for it, so here it is:

Modified TFT.bas:

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

' validate model...
#option TFT_MODEL = S6D1121
#if not (TFT_MODEL in (S6D1121,ST7735,ILI9225B))
   #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

#if TFT_MODEL = ILI9225B
public const Width = ILI9225B.Width
public const Height = ILI9225B.Height
public Clear as ILI9225B.Clear
public SetOrientation as ILI9225B.SetOrientation
public SetPixel as ILI9225B.SetPixel
public SetPenColor as ILI9225B.SetPenColor
public SetBrushColor as ILI9225B.SetBrushColor
public SetBitmap as ILI9225B.SetBitmap
Public SetBounds as ILI9225B.SetBounds
public WriteData as ILI9225B.WriteWordData
'#define IMPLEMENTS_HLINE
'#define IMPLEMENTS_VLINE
'private SetHLine as ILI9225B.HLine
'private SetVLine as ILI9225B.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


The new module -> ILI9225B.bas:
Code: Select all
'*****************************************************************************
'*  Name    : ILI9225B.bas                                                   *
'*  Author  : Simeon Kartalov                                                *
'*  Notice  : Copyright (c) 2013 Mecanique                                   *
'*          : All Rights Reserved                                            *
'*  Date    : 26/10/2013                                                     *
'*  Version : 1.0                                                            *
'*  Notes   : 4 Wires SPI Mode                                               *
'*          :                                                                *
'*****************************************************************************
Module ILI9225B

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

' RESET pin
#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
' Low: indicates that data is commands,
' High: indicates that data is display data.
#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 = 176
public const Height = 220

' 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    : 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

   
    WriteCommand($20)           
    WriteWordData(x1)          
    WriteCommand($21)                 
    WriteWordData(y1)   

    WriteCommand($36)           
    WriteWordData(x1)
    WriteWordData(x2)
    WriteCommand($38)                            
    WriteWordData(y1)
    WriteWordData(y2)
   
    WriteCommand($22)
       
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(10)
   _rs=1
    DelayMS(50)

' ************* Start Initial Sequence **********//
writecommand($01)
writeworddata($011C)                      ' set SS and NL bit
writecommand($02)
writeworddata($0100)                      ' set 1 line inversion
writecommand($03)
writeworddata($1030)                      ' set GRAM write direction and BGR=1.
writecommand($08)
writeworddata($0808)                      ' set BP and FP
writecommand($0C)
writeworddata($0000)                      ' RGB interface setting R0Ch=0x0110 for RGB 18Bit and R0Ch=0111forRGB16Bit
writecommand($0F)
writeworddata($0801)                      ' Set frame rate
writecommand($20)
writeworddata($0000)                      ' Set GRAM Address
writecommand($21)
writeworddata($0000)                      ' Set GRAM Address
' *************Power On sequence ****************//
delayms(50)
writecommand($10)
writeworddata($0A00)                      ' Set SAP,DSTB,STB
writecommand($11)
writeworddata($1038)                      ' Set APON,PON,AON,VCI1EN,VC
delayms(50)
writecommand($12)
writeworddata($1121)                      ' Internal reference voltage= Vci;
writecommand($13)
writeworddata($0066)                      ' Set GVDD
writecommand($14)
writeworddata($5F60)                      ' Set VCOMH/VCOML voltage
' ------------------------ Set GRAM area --------------------------------//
writecommand ($30)
writeworddata($0000)
writecommand ($31)
writeworddata($00DB)
writecommand ($32)
writeworddata($0000)
writecommand ($33)
writeworddata($0000)
writecommand ($34)
writeworddata($00DB)
writecommand ($35)
writeworddata($0000)
writecommand ($36)
writeworddata($00AF)
writecommand ($37)
writeworddata($0000)
writecommand ($38)
writeworddata($00DB)
writecommand ($39)
writeworddata($0000)
' ----------- Adjust the Gamma Curve ----------//
writecommand($50)
writeworddata($0400)
writecommand($51)
writeworddata($060B)
writecommand($52)
writeworddata($0C0A)
writecommand($53)
writeworddata($0105)
writecommand($54)
writeworddata($0A0C)
writecommand($55)
writeworddata($0B06)
writecommand($56)
writeworddata($0004)
writecommand($57)
writeworddata($0501)
writecommand($58)
writeworddata($0E00)
writecommand($59)
writeworddata($000E)
delayms(50)           
writecommand($07)
writeworddata($1017)
_cs=1 
   SetPenColor(255, 255, 255)
   SetBrushColor(0, 0, 0)
end sub

End Module


And the TFT in action:
Image
skartalov
 
Posts: 69
Joined: Sun Sep 15, 2013 1:12 pm

Re: 2.2' TFT module (ILI9225B chipset)

Postby bitfogav » Mon Oct 28, 2013 5:35 pm

Great module, thank you for sharing :)
User avatar
bitfogav
 
Posts: 75
Joined: Sat Nov 17, 2012 11:46 am
Location: UK

Re: 2.2' TFT module (ILI9225B chipset)

Postby David John Barker » Tue Oct 29, 2013 12:14 pm

...yes, thanks for sharing you code - much appreciated!
User avatar
David John Barker
 
Posts: 491
Joined: Thu Nov 08, 2012 12:21 pm

Re: 2.2' TFT module (ILI9225B chipset)

Postby skartalov » Tue Oct 29, 2013 5:26 pm

I have few more tft's with different controllers, which I intend to run.
Will keep you posted!
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 2 guests

cron

x