 '// *********************************************************************
 '//
 '// Display driver for Solomon SSD2119 controller connected to mbed board
 '// with simple terminal program
 '//
 '// ** szdiditalsquare (eBay) display connected on SPI + control pins  **
 '//  mbed            SSD2219 (on szdigitalsquare board)
 '//  pin15 -mosi        (28) SDA
 '//  pin10 -miso         NC
 '//  pin16 -sck         (26) SCL
 '//  pin14 -cs          (19) CS
 '//  pin18 -res         (23) RESET
 '//  pin17 -c/d         (20) RS
 '//
 '// Pins PS0 - PS3 should be open on system board = 4bit SPI mode
 '//
 '// After sucessful init you'll see color bars on whole display and
 '// welcome message. Then you can start typing in your serial terminal
 '// and information will appear on the display
 '//
 '// 28.11.2010 port from CrystalFonts & Nokia LCD lib by Imrich Konkol
 '// 29.11.2010 ZX Spectrum SCR viewer added
 '//            - to show test.scr from internal mbed drive type \\ at
 '//              PC terminal prompt
 '// *********************************************************************
 SREAD =10
 SDATA =15
 SCLK  =16   '// mosi, miso, sclk
'----------------------------------
 DCS =   14   '// Display chip select
 RESET = 18   '// Display reset
 DCD =   17   '// Display command/data
' DRD =  10   '// Display read
' DWR =   19   '// Display write
'----------------------------------
Pin(SREAD)=1
Pin(SDATA)=0
Pin(SCLK) =1
Pin(DCS)  =1
Pin(RESET)=1
Pin(DCD)  =1
Pin(DWR)  =1
'----------------------------------
SetPin DCS,8
SetPin DCD,8
SetPin SDATA,8
SetPin SCLK,8
SetPin SREAD,2
SetPin reset,8
'---------------------------------- MAIN-------------------------------

 '// Define Spectrum colors
       Dim colors (16)
       For I=0 To 15
              Read COLORS(I)
       Next I
       'BLACK,HBLUE,HRED,HMAGENTA,HGREEN,HCYAN,HYELLOW,HWHITE,BLUE,RED
       'MAGENTA,GREEN,CYAN,YELLOW,WHITE

     HRES     = 320     '// X axis # of pixels
     VRES     = 240     '// Y axis # of pixels

     START_X  = 31
     START_Y  = 23

init






' LCD_CACHE_SIZE 5
'*----------------------------------------------------------------------------                                       Global Variables
'-----------------------------------------------------------------------------
'//static uint8_t LcdCache [ LCD_CACHE_SIZE ];
'//static int   LcdCacheIdx;
'//static int   LoWaterMark;
'//static int   HiWaterMark;
  yy=0
  xx=0
Do
For I=0 To 14
    '   RGB (colors(I))
    Next I
TEST
Loop
End
 '// Essential display commands
 /*************************************************/
 Sub write_command (command)
     Local raw

'     Pin(DRD) = 1;
     Pin(DCS) = 0
     Pin(DCD) = 0

     raw = command ':Print "C=&H";Hex$(raw);
    '//raw &= = &H00FF;      '// Keep bit9 low - means command
    '//printf("Raw data: %d\n",raw);
     D=SPI(SREAD,SDATA,SCLK,raw)


     Pin(DCS) = 1

 End Sub
 /*************************************************/

 '//typedef unsigned char u8;
 '//typedef unsigned int u16;
 Sub write_data( value)
    Local raw

    ' Pin(DRD) = 1
     Pin(DCD) = 1
     Pin(DCS) = 0

     raw = Int(value/256)   ':Print "|DH=&H";Hex$(raw);
     '// raw &= = &H00FF;      '// Keep bit9 high - means data
     '// raw |= = &H0100;
     '// printf("Raw data: %d\n",raw);
     D=SPI(SREAD,SDATA,SCLK,raw)

     Pin(DCS) = 1
     '//wait_ms(1);
     Pin(DCS) = 0

     raw = value And &HFF ': Print "|DL=&H";Hex$(raw)
     '// raw &= = &H00FF;      '// Keep bit9 high - means data
     '// raw |= = &H0100;
    '// printf("Raw data: %d\n",raw);

     D=SPI(SREAD,SDATA,SCLK,raw)

     Pin(DCS) = 1
End Sub
'=====================================================================
  '// color definitions (H = half)
 Data           &H0000      'BLACK
 Data           &H0010      'HBLUE
 Data           &H001F      'BLUE
 Data           &H8000      'HRED
 Data           &HF800      'RED
 Data           &H0400      'HGREEN
 Data           &H07E0      'GREEN
 Data           &H0410      'HCYAN
 Data           &H7BE0      'OLIVE
 Data           &H07FF      'CYAN
 Data           &H8010      'HMAGENTA
 Data           &HF81F      'MAGENTA
 Data           &H8400      'HYELLOW
 Data           &HFFE0      'YELLOW
 Data           &H8410      'HWHITE
 Data           &HFFFF      'WHITE
'=======================================================================

' // Set X & Y axis of next pixel write
 Sub LcdSetPenXY ( x,  y)
     write_command (&H4F)     '// RAM address set For Y axis
     write_data (y)           '// Page 58 of SSD2119 datasheet
     write_command (&H4E)     '// RAM address set For X axis
     write_data (x)           '// Page 58 of SSD2119 datasheet
     write_command (&H22)     '// Return back To pixel write mode

End Sub

'*-----------------------------------------------------------------------------
'   Name         :  LcdGotoXY
'   Description  :  Sets cursor location to xy location corresponding to basic
'                   font size.
'   Argument(s)  :  x, y -> Coordinate for new cursor position.Range: 1,1..14,6
'   Return value :  None.
'------------------------------------------------------------------------------
 Sub LcdGotoXY (  x,  y )
    ' // uint16_t xx, yy;
     xx = y * 8
     yy = x * 6
    ' //LcdCacheIdx = (x - 1) * HRES + (y - 1) * VRES;
     LcdSetPenXY(xx,yy)

     FONT_1X = 1
     FONT_2X = 2

     LcdFontSize


     PIXEL_OFF =  0,
     PIXEL_ON  =  1,
     PIXEL_XOR =  2

     LcdPixelMode;

' /*-----------------------------------------------------------------------------
   Name         :  LcdChr
   Description  :  Displays a character at current cursor location
                   and increment cursor location.
   Argument(s)  :  size -> Font size. See enum.
                   ch   -> Character To write.
   Return value :  None.
 ----------------------------------------------------------------------------*/
 Sub LcdChr (   ch )
     Local i, j, c

     If ch < &H20) Or ch > &H7F Then ch = &H80

         '//  Convert To a printable character.


     '//write_command(0x0022);

     For  i = 0 To 5
         If (i < 5) Then c = FontLookup(ch - 32,i) Else c = 0x00                   // Space after each character

         For j = 0 To 7
             If c And &H01 = &H01then write_data(BLACK)Else write_data(WHITE)

                 c = Int(c/2)

         yy=yy+1
         write_command (&H4F)    '// RAM address set For Y axis
         write_data (yy)       '// Page 58 of SSD2119 datasheet
         write_command (&H22)
     End Sub

' /*-----------------------------------------------------------------------------
'   Name         :  LcdStr
'   Description  :  Displays a character at current cursor location and increment
'                   cursor location according to font size.
'   Argument(s)  :  size    -> Font size. See enum.
'                   dataPtr -> Pointer to null terminated ASCII string to display.
'   Return value :  None.
' -----------------------------------------------------------------------------*/
 void LcdStr (  char *dataPtr ) {
     While ( *dataPtr ) {
         LcdChr( *dataPtr++ );
     }
 }


'/*************************************************/
 Sub init



     Pin(DCS) = 1
     Pin(DCD) = 1

     Pin(reset) = 0
     Pause 200
     Pin(reset) = 1
     Pause 500

     write_command (&h28)    '// VCOM OTP
     write_data (&H0006);    '// Page 55-56 of SSD2119 datasheet
      Pause 10
     write_command (&H00)  '// start Oscillator
     write_data (&H0001)     '// Page 36 of SSD2119 datasheet

     write_command (&H10)    '// Sleep mode
     write_data (&H0000)      '// Page 49 of SSD2119 datasheet

     write_command (&H01)    '// Driver Output Control
     write_data (&H32EF)      '// Page 36-39 of SSD2119 datasheet

     write_command (&H02)    '// LCD Driving Waveform Control
     write_data (&H0600)      '// Page 40-42 of SSD2119 datasheet

     write_command (&H03)    '// Power Control 1
     write_data (&H6A38)      '// Page 43-44 of SSD2119 datasheet

    write_command (&H11)    '// Entry Mode
    write_data (&H6870)       '// Page 50-52 of SSD2119 datasheet

     write_command (&H0F)    '// Gate Scan Position
     write_data (&H0000)      '// Page 49 of SSD2119 datasheet

     write_command (&H0B)    '// Frame Cycle Control
     write_data (&H5308)      '// Page 45 of SSD2119 datasheet

     write_command (&H0C)    '// Power Control 2
     write_data (&H0003)      '// Page 47 of SSD2119 datasheet

     write_command (&H0D)    '// Power Control 3
     write_data (&H000A)      '// Page 48 of SSD2119 datasheet

     write_command (&H0E)    '// Power Control 4
     write_data (&H2E00)      '// Page 48 of SSD2119 datasheet

     write_command (&H1E)    '// Power Control 5
     write_data (&H00BE)      '// Page 53 of SSD2119 datasheet

     write_command (&H25)    '// Frame Frequency Control
     write_data (&H8000)      '// Page 53 of SSD2119 datasheet

     write_command (&H26)    '// Analog setting
     write_data (&H7800)      '// Page 54 of SSD2119 datasheet

     write_command (&H4E)    '// Ram Address Set
     write_data (&H0000)      '// Page 58 of SSD2119 datasheet

     write_command (&H4F)    '// Ram Address Set
     write_data (&H0000)      ' // Page 58 of SSD2119 datasheet

     write_command(&H12)    '// Sleep mode
     write_data (&H08D9)     '// Page 49 of SSD2119 datasheet

     '// Gamma Control (R30h To R3Bh) -- Page 56 of SSD2119 datasheet
     write_command (&H30)
     write_data (&H0000)

     write_command (&H31)
     write_data (&H0104)

     write_command (&H32)
     write_data (&H0100)

     write_command(&H33)
     write_data (&H0305)

     write_command (&H34)
     write_data (&H0505)

     write_command (&H35)
     write_data (&H0305)

     write_command (&H36)
     write_data (&H0707)

     write_command (&H37)
     write_data (&H0300)

     write_command (&H3A)
     write_data (&H1200)

     write_command (&H3B)
     write_data (&H0800)

     write_command (&H07)    '// Display Control
     write_data (&H0033)      '// Page 45 of SSD2119 datasheet

     Pause 150

     write_command (&H22)    '// RAM data write/read

 End Sub
' /*************************************************/
Sub HOME
  XX=0
  YY=0
     write_command (&H4E)    '//
     write_data (&H0000)      '//
     write_command (&H4F)    '//
     write_data (&H0000)      '//
     write_command (&H44)    '//
     write_data (&HEF00)      '//
     write_command (&H45)    '//
     write_data (&H0000)      '//
     write_command (&H46)    '//
     write_data (&H013F)      '//
          write_command (&H22)    '//

End Sub
'--------------------------------------------------
Sub RGB (DATA)
Local I,J
  HOME
  For I=0 To HRES
  For J=0 To VRES
    WRITE_DATA (DATA)
  Next J
  Next I

End Sub
'----------------------------------------------------
Sub TEST
  CLEAR_S
  For I=0 To 319
    For J=0 To 239
    If I>299 Then WRITE_DATA (COLORS(1))
    If I>279 And I< 300 Then WRITE_DATA (COLORS(9))
    If I>259 And I<280 Then WRITE_DATA (COLORS(2))
        If I>239 And I<260 Then WRITE_DATA (COLORS(0))
    If I>219 And I< 240  Then WRITE_DATA (COLORS(3))
    If I>199 And I< 220 Then WRITE_DATA (COLORS(4))
    If I>179 And I<200 Then WRITE_DATA (COLORS(5))
    If I>159 And I<180 Then WRITE_DATA (COLORS(6))
    If I>139 And I<160 Then WRITE_DATA (COLORS(7))
    If I>119 And I<140 Then WRITE_DATA (COLORS(8))
    If I>99 And I<120 Then WRITE_DATA (COLORS(10))
    If I>79 And I<100 Then WRITE_DATA (COLORS(11))
    If I>59 And I<80 Then WRITE_DATA (COLORS(12))
    If I>39 And I<60 Then WRITE_DATA (COLORS(13))
    If I>19 And I<40 Then WRITE_DATA (COLORS(14))
    If I>0 And I< 20 Then WRITE_DATA (COLORS(15))

  Next J
  Next I
  End Sub

  Sub CLEAR_S

  For I=0 To 319
  For J=0 To 239
  WRITE_DATA (COLORS(0))
  Next J
  Next I

End Sub













                                                                                                  