'  ----------------------- LCD subroutines -----------------------

'----------------------- Lcd_cmd (Command) ----------------------
Sub Lcd_cmd(byval Command As Byte )
  
Reset Lcd_sce                                            ' SPI enable
  
Reset Lcd_dc                                             ' command mode
  
Spiout Command , 1
  
Set Lcd_dc                                               ' data mode
  
Set Lcd_sce                                              ' data mode
End Sub

'----------------------- Lcdinit ------------------------------
Sub Lcdinit
  
Waitms 5
  
Reset Lcd_rst                                            'RESET LCD
  
Waitms 5
  
Set Lcd_rst                                              'end of reset
  
Set Lcd_sce                                              'in Lcd_cmd() will be "L"!
  
Call Lcd_cmd(&H21)                                       'extended command mode
  
Call Lcd_cmd(&Hf8)                                       'c8LCD contrast (mayby you must change) - oryginał: Lcd_cmd(&Hf8)
  
Call Lcd_cmd(&H06)                                       'temperature compensation
  
Call Lcd_cmd(&H13)                                       'bias 1:48
  
Call Lcd_cmd(&H20)                                       'normal command mode(&H20)
  
Call Lcd_cmd(&H0c)                                       'normal display
  
Call Lcd_cmd(&H40)                                       'Y=0
  
Call Lcd_cmd(&H80)                                       'X=0
  
Call Lcdclear
  
Call Lcdupdate
End Sub

'---------------------- Lcdclear ------------------------------
Sub Lcdclear
  
Local I As Word
  
For I = 1 To 504
       Lcd_cache
(i) = 0
  
Next I
'    Lcdcacheidx = 1
End Sub
'--------------------- Lcdupdate -------------------------------
'copy full LCD_cache to LCD RAM
Sub Lcdupdate
  
Local I As Word
  
Call Lcd_cmd(&H80)                                       'sets 0 X-addr of RAM
  
Call Lcd_cmd(&H40)                                       'sets 0 Y-addr of RAM
  
For I = 1 To 504
     Lcd_data
= Lcd_cache(i)
    
Reset Lcd_sce                                          'SPI to LCD enable
    
Set Lcd_dc                                             'Data
    
Spiout Lcd_data , 1
    
While Spsr.7 = 1                                       'wait until SPIF = 0
    
Wend
    
Reset Lcd_dc
    
Set Lcd_sce                                            'SPI to LCD disable
  
Next I
   Updatelcd
= 0
End Sub

'--------------------- Lcdgoxy--------------------------------
Sub Lcdgoxy(byval X As Byte , Byval Y As Byte)              'from 1,1 to 14,6
  
Local I As Word
  
Local J As Word
                    
'PRZYKŁAD 1,6 (Tw)
'*******************************************************************
'*******************************************************************
  
Select Case X
     
Case 1 : X = 14
     
Case 2 : X = 13
     
Case 3 : X = 12
     
Case 4 : X = 11
     
Case 5 : X = 10
     
Case 6 : X = 9
     
Case 7 : X = 8
     
Case 8 : X = 7
     
Case 9 : X = 6
     
Case 10 : X = 5
     
Case 11 : X = 4
     
Case 12 : X = 3
     
Case 13 : X = 2
     
Case 14 : X = 1
  
End Select

  
Select Case Y
     
Case 1 : Y = 6
     
Case 2 : Y = 5
     
Case 3 : Y = 4
     
Case 4 : Y = 3
     
Case 5 : Y = 2
     
Case 6 : Y = 1
  
End Select
'*******************************************************************
'*******************************************************************
'   X = X - 1    '*******************************************************************                                             'X=0
   I
= X                                                    'I=0
   I
= I * 6                                                'I=0
'   Y = Y - 1                                                'Y=5
   J
= Y                                                    'J=5
   J
= Y * 84                                               'J=420
   Lcdcacheidx
= I + J                                      'lcdcacheidx=420
  
Decr Lcdcacheidx                                         '*******************************************************************             'Lcdchacheidx=421
End Sub

'------------------ Lcdchr-------------------------------------
Sub Lcdchr(byval Ch As Byte , Byval Lcdfontsize As Byte , Byval Ch_inv As Byte)       '1 character on LCD
  
Local I As Byte
  
Local J As Word
  
Local K As Byte
  
Local Ba As Byte
  
Local Bb As Byte
  
Local Tmpidx As Word
  
Local Tmpidx1 As Word
  
If Ch < &H20 Then Ch = 92
  
If Ch > &H7B Then Ch = 92
   J
= Ch - 32
   J
= J * 5
  
If Lcdfontsize = 1 Then
     
For I = 1 To 6
         K
= Lookup(j , Fontlookup)
'*******************************************************************
'*******************************************************************
         Abc
= 7                                            'zmienna pomocnicza
        
For Iji = 0 To 7                                   'PĘTLA ODWRACAJĄCA FONT DO GÓRY NOGAMI
           
If K.iji = 1 Then                               'jeśli pixel zapalona to
               Kk
.abc = 1
           
Else
               Kk
.abc = 0
           
End If
           
Decr Abc
        
Next Iji
         K
= Kk                                             'przepisz odwrócony font
'*******************************************************************
'*******************************************************************
        
If Ch_inv = 1 Then                                 'negative
           
If I = 6 Then                                   'black for 6 row
               K
= 255
           
Else
               K
= K Xor &HFF
           
End If
        
Else
           
If I = 6 Then                                   'white for 6 row
               K
= 0
           
End If
         
End If

         Lcd_cache
(lcdcacheidx) = K
        
Decr Lcdcacheidx                                   '*******************************************************************
        
Incr J
     
Next I
  
Else                                                     'high 2
      Tmpidx
= Lcdcacheidx - 84                             'signs go up and can't be at 1,x
     
For I = 1 To 6
        
If I = 6 Then
           
If Ch_inv = 1 Then
               K
= 255
            
Else
               K
= 0
           
End If
        
Else
            K
= Lookup(j , Fontlookup)
'(
           
Abc = 7                                         'zmienna pomocnicza
           
For Iji = 0 To 7                                'PĘTLA ODWRACAJĄCA FONT DO GÓRY NOGAMI
              
If K.iji = 1 Then                            'jeśli pixel zapalona to
                 
Kk.abc = 1
              
Else
                 
Kk.abc = 0
              
End If
              
Decr Abc
           
Next Iji
           
K = Kk
')
           
If Ch_inv = 1 Then
               K
= K Xor &HFF
           
End If
        
End If
         Ba
.0 = K.0
         Ba
.1 = K.0
         Ba
.2 = K.1
         Ba
.3 = K.1
         Ba
.4 = K.2
         Ba
.5 = K.2
         Ba
.6 = K.3
         Ba
.7 = K.3

         Bb
.0 = K.4
         Bb
.1 = K.4
         Bb
.2 = K.5
         Bb
.3 = K.5
         Bb
.4 = K.6
         Bb
.5 = K.6
         Bb
.6 = K.7
         Bb
.7 = K.7
'*******************************************************************
'*******************************************************************
            Abc
= 7                                         'zmienna pomocnicza
           
For Iji = 0 To 7                                'PĘTLA ODWRACAJĄCA FONT DO GÓRY NOGAMI
              
If Ba.iji = 1 Then                           'jeśli pixel zapalona to
                  Kk
.abc = 1
              
Else
                  Kk
.abc = 0
              
End If
              
Decr Abc
           
Next Iji
            Ba
= Kk
            Abc
= 7                                         'zmienna pomocnicza
           
For Iji = 0 To 7                                'PĘTLA ODWRACAJĄCA FONT DO GÓRY NOGAMI
               
If Bb.iji = 1 Then                           'jeśli pixel zapalona to
                  Kk
.abc = 1
              
Else
                  Kk
.abc = 0
              
End If
              
Decr Abc
           
Next Iji
            Bb
= Kk
'*******************************************************************
'*******************************************************************
         Lcd_cache
(tmpidx) = Ba                             'up of character
        
Decr Tmpidx                                        '*******************************************************************
         Lcd_cache
(tmpidx) = Ba                             'double wide
        
Decr Tmpidx                                        '*******************************************************************
         Tmpidx1
= Tmpidx - 82                              '*******************************************************************
         Lcd_cache
(tmpidx1) = Bb                            'down of character
        
Decr Tmpidx1
         Lcd_cache
(tmpidx1) = Bb                            'double wide
        
Incr J
     
Next I
      Lcdcacheidx
= Lcdcacheidx - 12                        '******************************************************************* '6+6
 
End If
  Kon_lcdchr
:
End Sub
'-------------------- Lcdstr----------------------------------------
Sub Lcdstr(lcdstring As String , Lcdfontsize As Byte , Ch_inv As Byte)
  
Local M As Byte
  
Local N As String * 1
  
For M = 1 To Len(lcdstring)
      N
= Mid(lcdstring , M , 1)
      Ch
= Asc(n)
     
Call Lcdchr(ch , Lcdfontsize , Ch_inv )
  
Next M
End Sub
'------------------- Lcdpixel --------------------------------------
Sub Lcdpixel(x As Byte , Y As Byte , Pixelmode As Byte )
  
Local Index As Word
  
Local Offset As Byte
  
Local I As Byte
  
If X > 84 Then Goto End_lcdpixel
  
If Y > 48 Then Goto End_lcdpixel
   Y
= Y - 1
   Index
= Y / 8
   Index
= Index * 84
   Index
= Index + X
   Offset
= Y / 8
   Offset
= Offset * 8
   Offset
= Y - Offset
   Lcd_data
= Lcd_cache(index)
   I
= 1
  
Shift I , Left , Offset
  
Select Case Pixelmode
  
Case 0:                                                  'pixel off
      I
= I Xor &HFF
      Lcd_data
= Lcd_data And I
  
Case 1:                                                  'pixel on
      Lcd_data
= Lcd_data Or I
  
Case 2:                                                  'pixel xor
      Lcd_data
= Lcd_data Xor I
  
End Select
   Lcd_cache
(index) = Lcd_data
End_lcdpixel
:
End Sub
'----------------------- Lcdline ------------------------------------
Sub Lcdline(x1 As Byte , Y1 As Byte , X2 As Byte , Y2 As Byte , Pixelmode As Byte )
  
Local Dx As Integer
  
Local Dy As Integer
  
Local Stepx As Integer
  
Local Stepy As Integer
  
Local Fraction As Integer
  
Local I As Byte

   Dy
= Y2 - Y1
   Dx
= X2 - X1

  
If Dy < 0 Then
      Dy
= -dy
      Stepy
= -1
  
Else
      Stepy
= 1
  
End If
  
If Dx < 0 Then
      Dx
= -dx
      Stepx
= -1
  
Else
      Stepx
= 1
  
End If
  
Shift Dx , Left , 1                                      '-2
  
Shift Dy , Left , 1                                      '-2
  
Call Lcdpixel(x1 , Y1 , Pixelmode)

  
If Dx > Dy Then
      I
= Dx
     
Shift I , Right , 1                                   '/2
      Fraction
= Dy - I
     
While X1 <> X2
        
If Fraction >= 0 Then
            Y1
= Y1 + Stepy
            Fraction
= Fraction - Dx
        
End If
         X1
= X1 + Stepx
         Fraction
= Fraction + Dy
        
Call Lcdpixel(x1 , Y1 , Pixelmode)
     
Wend
  
Else
      I
= Dy
     
Shift I , Right , 1                                   '/2
      Fraction
= Dx - I
     
While Y1 <> Y2
        
If Fraction >= 0 Then
            X1
= X1 + Stepx
            Fraction
= Fraction - Dy
        
End If
         Y1
= Y1 + Stepy
         Fraction
= Fraction + Dx
        
Call Lcdpixel(x1 , Y1 , Pixelmode)
     
Wend
  
End If
Updatelcd
= 1
End Sub

'  ----------------------- LCDrectangle -----------------------

Sub Lcdrectangle(x3 As Byte , Y3 As Byte , Wide As Byte , Height As Byte)
    Wide
= X3 + Wide
   
Decr Wide
    Height
= Y3 + Height
   
Decr Height
   
Call Lcdline(x3 , Y3 , Wide , Y3 , 1 )
   
Call Lcdline(x3 , Height , Wide , Height , 1 )
   
Call Lcdline(x3 , Y3 , X3 , Height , 1 )
   
Call Lcdline(wide , Y3 , Wide , Height , 1 )
End Sub

 
'--------------------- Show ----------------------
 
'XX 1 to (84-_width), YY 1 to 6 (n*8)
 
'must be RESTORE "data to show" before CALL it subrotine
Sub Show(xx As Byte , Yy As Byte)
  
Local Swidth As Byte
  
Local Shigh As Byte
  
Local Cacheidx As Word
  
Local I As Byte
  
Local J As Byte
  
Local K As Byte
  
Local L As Byte
  
Read Swidth
  
Read Shigh
   K
= Yy + Shigh
   K
= K - 1
   L
= Xx + Swidth
   L
= L - 1
  
If K > 6 Then Goto End_show
  
If L > 84 Then Goto End_show

  
For I = Yy To K
   Cacheidx
= I - 1
      Cacheidx
= Cacheidx * 84
      Cacheidx
= Cacheidx + Xx
     
For J = Xx To L
        
Read Ch
         Lcd_cache
(cacheidx) = Ch
        
Incr Cacheidx
     
Next J
  
Next I
   Updatelcd
= 1
   End_show
:
End Sub

'--------------------- ClearShow ----------------------
'XX 1 to (84-_width), YY 1 to 6 (n*8)
'must be RESTORE "data to show" before CALL it subrotine
Sub Clearshow(xx As Byte , Yy As Byte)
  
Local Swidth As Byte
  
Local Shigh As Byte
  
Local Cacheidx As Word
  
Local I As Byte
  
Local J As Byte
  
Local K As Byte
  
Local L As Byte
  
Read Swidth
  
Read Shigh
   K
= Yy + Shigh
   K
= K - 1
   L
= Xx + Swidth
   L
= L - 1
  
For I = Yy To K
   Cacheidx
= I - 1
      Cacheidx
= Cacheidx * 84
      Cacheidx
= Cacheidx + Xx
     
For J = Xx To L
         Lcd_cache
(cacheidx) = 0
        
Incr Cacheidx
     
Next J
  
Next I
   Updatelcd
= 1
End Sub