'Oled Buffer Subroutines

'VlaM ========================Global Variables===================================

Dim Obuf$(1152) As Byte       'display data buffer
Dim Obuf$ind As Word          'display data buffer index
Dim Obuf$$1 As Byte , Obuf$$2 As Byte       'common temp bytes
Dim Obuf$$12 As Word At Obuf$$1 Overlay

'VlaM ======================== Subroutines ======================================

$include "Oled$SE.sub"        'include OLED subroutines

'VlaM ======================== Clear OLED Buffer ================================

Sub Obuf$clear

For Obuf$ind = 0 To 1023
   Obuf$(obuf$ind) = 0        'clear display data buffer, row 0-7
Next

End Sub


'VlaM ======================== Sends OLED Buffer to OLED ========================

Sub Obuf$send()

Local Oled$page As Byte

For Oled$page = 0 To 7        'send pages 0 to 7 from OBUF to OLED
   Obuf$send_page Oled$page
Next

End Sub


'VlaM ======================== Sends Buffer Page to OLED ========================

Sub Obuf$send_page(byval Oled$page As Byte)

Local Oled$col As Byte

Oled$page = Oled$page And &B00000111
Obuf$ind = Oled$page * 128

Oled$$set_adr Oled$page , 0   'set OLED address

I2cwbyte &B01000000           'next bytes are data bytes
For Oled$col = 0 To 127       'send 128B data block (one page)
   I2cwbyte Obuf$(obuf$ind)
   Incr Obuf$ind
Next
I2cstop

End Sub


'VlaM ======================== Sends Buffer Block to OLED =======================

Sub Obuf$send_block(byval Oled$page As Byte , Byval Oled$col1 As Byte , Byval Oled$col2 As Byte)

Local Oled$col As Byte

Oled$page = Oled$page And &B00000111       'prepare page and column addresses
Oled$col1 = Oled$col1 And &B01111111
Oled$col2 = Oled$col2 And &B01111111

Obuf$ind = Oled$page * 128    'calculate OBUF index
Obuf$ind = Obuf$ind + Oled$col1

Oled$$set_adr Oled$page , Oled$col1       'set OLED address

I2cwbyte &B01000000           'next bytes are data bytes
For Oled$col = Oled$col1 To Oled$col2       'send data block
   I2cwbyte Obuf$(obuf$ind)
   Incr Obuf$ind
Next
I2cstop                       'stop condition

End Sub


'VlaM ======================== Reads Buffer Block from OLED =====================

Sub Obuf$read_block(byval Oled$page As Byte , Byval Oled$col1 As Byte , Byval Oled$col2 As Byte)

Local Oled$col As Byte , Oled$byte As Byte

Oled$page = Oled$page And &B00000111       'prepare page and column addresses
Oled$col1 = Oled$col1 And &B01111111
Oled$col2 = Oled$col2 And &B01111111

Obuf$ind = Oled$page * 128    'calculate OBUF index
Obuf$ind = Obuf$ind + Oled$col1

Oled$$set_adr Oled$page , Oled$col1       'set OLED address

I2cwbyte &B01000000           'instruct OLED to send data bytes
I2cstart                      'start condition
I2cwbyte Oled$addr_r          'OLED read address
I2crbyte Oled$byte , Ack      'dummy read

For Oled$col = Oled$col1 To Oled$col2       'read data block
   If Oled$col < Oled$col2 Then
      I2crbyte Obuf$(obuf$ind) , Ack
   Else
      I2crbyte Obuf$(obuf$ind) , Nack
   End If
   Incr Obuf$ind
Next
I2cstop                       'stop condition

End Sub


'VlaM ======================== Reads Page from OLED =============================

Sub Obuf$read_page(byval Oled$page As Byte)

Local Oled$col As Byte , Oled$byte As Byte

Oled$page = Oled$page And &B00000111       'prepare page address
Obuf$ind = Oled$page * 128    'calculate OBUF index

Oled$$set_adr Oled$page , 0   'set OLED address

I2cwbyte &B01000000           'instruct OLED to send data bytes
I2cstart                      'start condition
I2cwbyte Oled$addr_r          'OLED read address
I2crbyte Oled$byte , Ack      'dummy read

For Oled$col = 0 To 126       'read 128B data block
   I2crbyte Obuf$(obuf$ind) , Ack
   Incr Obuf$ind
Next

I2crbyte Obuf$(obuf$ind) , Nack
Incr Obuf$ind
I2cstop                       'stop condition

End Sub


'VlaM ======================== Reads entire OLED ================================

Sub Obuf$read()

Local Oled$page As Byte

For Oled$page = 0 To 7        'read pages 0 to 7
   Obuf$read_page Oled$page
Next

End Sub


'VlaM ==================== Set OBUF pixel ON/OFF ================================

Sub Obuf$set_pix(byval Oled$bit_value As Byte , Byval Oled$row As Byte , Byval Oled$col As Byte)

Local Oled$bit As Byte

   Oled$bit = Oled$row And &B00000111       'convert row and col address to OBUF index
   Oled$row = Oled$row And &B00111000
   Obuf$ind = Oled$row * 16
   Oled$col = Oled$col And &B01111111
   Obuf$ind = Obuf$ind + Oled$col
   Obuf$(obuf$ind).oled$bit = Oled$bit_value.0       'set pixel ON/OFF

End Sub


'VlaM ==================== Set OBUF pixel ON ====================================

Sub Obuf$set_pix_on(byval Oled$row As Byte , Byval Oled$col As Byte)

Local Oled$bit As Byte

   Oled$bit = Oled$row And &B00000111       'convert row and col address to OBUF index
   Oled$row = Oled$row And &B00111000
   Obuf$ind = Oled$row * 16
   Oled$col = Oled$col And &B01111111
   Obuf$ind = Obuf$ind + Oled$col
   Set Obuf$(obuf$ind).oled$bit       'set pixel ON

End Sub

'VlaM ==================== Reset OBUF pixel OFF =================================

Sub Obuf$set_pix_off(byval Oled$row As Byte , Byval Oled$col As Byte)

Local Oled$bit As Byte

   Oled$bit = Oled$row And &B00000111       'convert row and col address to OBUF index
   Oled$row = Oled$row And &B00111000
   Obuf$ind = Oled$row * 16
   Oled$col = Oled$col And &B01111111
   Obuf$ind = Obuf$ind + Oled$col
   Reset Obuf$(obuf$ind).oled$bit       'set pixel OFF

End Sub

'VlaM ======================== Writes a byte to OBUF ============================

Sub Obuf$write_byte(byval Obuf$byte_to_write As Byte , Byval Oled$page As Byte , Byval Oled$col As Byte)

Oled$page = Oled$page And &B00000111       'convert row and col address to OBUF index
Oled$col = Oled$col And &B01111111
Obuf$ind = Oled$page * 128
Obuf$ind = Obuf$ind + Oled$col

Obuf$(obuf$ind) = Obuf$byte_to_write       'write a byte to OBUF

End Sub


'VlaM ======================== Writes text to OBUF ==============================

Sub Obuf$write_text(byval Obuf$text_to_write As String * 22 , Byval Oled$row As Byte , Byval Oled$col As Byte)
Local Obuf$char_to_write As String * 1 , Obuf$row As Byte
Local Obuf$temp1 As Byte , Obuf$temp2 As Word , Obuf$temp3 As Byte

   Obuf$row = Oled$row And &B00000111       'convert row and col address to OBUF index
   Oled$row = Oled$row And &B00111000
   Obuf$ind = Oled$row * 16
   Oled$col = Oled$col And &B01111111
   Obuf$ind = Obuf$ind + Oled$col

For Obuf$temp1 = 1 To Len(obuf$text_to_write)
   Obuf$char_to_write = Mid(obuf$text_to_write , Obuf$temp1 , 1)       'select next char from string
   Obuf$temp3 = Asc(obuf$char_to_write)       'determine its ASCII value
   If Obuf$temp3 >= 128 Then  'if Њ, read its ASCII value from table
      Obuf$temp3 = Lookdown(obuf$temp3 , Obuf$char_table_1 , 10) + 127
   End If

   Obuf$temp3 = Obuf$temp3 - 32
   Obuf$temp2 = Obuf$temp3 * 5

   For Obuf$temp3 = 1 To 5
      Obuf$$1 = Obuf$(obuf$ind)
      Obuf$$2 = Obuf$(obuf$ind + 128)
      Rotate Obuf$$12 , Right , Obuf$row
      Obuf$$1 = Lookup(obuf$temp2 , Obuf$char_table)
      Rotate Obuf$$12 , Left , Obuf$row
      Obuf$(obuf$ind) = Obuf$$1
      Obuf$(obuf$ind + 128) = Obuf$$2
      Incr Obuf$temp2
      Incr Obuf$ind
   Next

   Obuf$(obuf$ind) = &B00000000
   Incr Obuf$ind
Next

I2cstop
Goto Obuf$write_text_ex

Obuf$char_table:              'pixel patterns of supported characters
Data 0 , 0 , 0 , 0 , 0        '
Data 0 , 0 , 95 , 0 , 0       ' !
Data 3 , 7 , 0 , 3 , 7 ' "
Data 36 , 126 , 36 , 126 , 36 ' #
Data 36 , 43 , 106 , 18 , 0   ' $
Data 99 , 19 , 8 , 100 , 99   ' %
Data 54 , 73 , 86 , 32 , 80   ' &
Data 0 , 3 , 7 , 0 , 0        ' '
Data 0 , 62 , 65 , 0 , 0      ' (
Data 0 , 65 , 62 , 0 , 0      ' )
Data 8 , 62 , 28 , 62 , 8     ' *
Data 8 , 8 , 62 , 8 , 8       ' +
Data 0 , 96 , 224 , 0 , 0     ' ,
Data 8 , 8 , 8 , 8 , 8        ' -
Data 0 , 0 , 96 , 96 , 0      ' ;
Data 32 , 16 , 8 , 4 , 2      ' /
Data 62 , 81 , 73 , 69 , 62   ' 0
Data 0 , 66 , 127 , 64 , 0    ' 1
Data 98 , 81 , 73 , 73 , 70   ' 2
Data 34 , 73 , 73 , 73 , 54   ' 3
Data 24 , 20 , 18 , 127 , 16  ' 4
Data 47 , 73 , 73 , 73 , 49   ' 5
Data 60 , 74 , 73 , 73 , 48   ' 6
Data 1 , 113 , 9 , 5 , 3      ' 7
Data 54 , 73 , 73 , 73 , 54   ' 8
Data 6 , 73 , 73 , 41 , 30    ' 9
Data 0 , 0 , 108 , 108 , 0    ' :
Data 0 , 0 , 108 , 236 , 0    ' ;
Data 8 , 20 , 34 , 65 , 0     ' <
Data 36 , 36 , 36 , 36 , 36   ' =
Data 0 , 65 , 34 , 20 , 8     ' >
Data 2 , 1 , 89 , 9 , 6       ' ?
Data 62 , 65 , 93 , 85 , 30   ' @
Data 126 , 17 , 17 , 17 , 126 ' A
Data 127 , 73 , 73 , 73 , 54  ' B
Data 62 , 65 , 65 , 65 , 34   ' C
Data 127 , 65 , 65 , 65 , 62  ' D
Data 127 , 73 , 73 , 73 , 65  ' E
Data 127 , 9 , 9 , 9 , 1      ' F
Data 62 , 65 , 73 , 73 , 122  ' G
Data 127 , 8 , 8 , 8 , 127    ' H
Data 0 , 65 , 127 , 65 , 0    ' I
Data 48 , 64 , 64 , 64 , 63   ' J
Data 127 , 8 , 20 , 34 , 65   ' K
Data 127 , 64 , 64 , 64 , 64  ' L
Data 127 , 2 , 4 , 2 , 127    ' M
Data 127 , 2 , 4 , 8 , 127    ' N
Data 62 , 65 , 65 , 65 , 62   ' O
Data 127 , 9 , 9 , 9 , 6      ' P
Data 62 , 65 , 81 , 33 , 94   ' Q
Data 127 , 9 , 9 , 25 , 102   ' R
Data 38 , 73 , 73 , 73 , 50   ' S
Data 1 , 1 , 127 , 1 , 1      ' T
Data 63 , 64 , 64 , 64 , 63   ' U
Data 31 , 32 , 64 , 32 , 31   ' V
Data 63 , 64 , 60 , 64 , 63   ' W
Data 99 , 20 , 8 , 20 , 99    ' X
Data 7 , 8 , 112 , 8 , 7      ' Y
Data 113 , 73 , 69 , 67 , 0   ' Z
Data 0 , 127 , 65 , 65 , 0    ' [
Data 2 , 4 , 8 , 16 , 32      ' \
Data 0 , 65 , 65 , 127 , 0    ' ]
Data 4 , 2 , 1 , 2 , 4        ' ^
Data 128 , 128 , 128 , 128 , 128       ' _
Data 0 , 3 , 7 , 0 , 0        ' `
Data 32 , 84 , 84 , 84 , 120  ' a
Data 127 , 68 , 68 , 68 , 56  ' b
Data 56 , 68 , 68 , 68 , 40   ' c
Data 56 , 68 , 68 , 68 , 127  ' d
Data 56 , 84 , 84 , 84 , 8    ' e
Data 8 , 126 , 9 , 9 , 0      ' f
Data 24 , 164 , 164 , 164 , 124       ' g
Data 127 , 4 , 4 , 120 , 0    ' h
Data 0 , 68 , 125 , 64 , 0    ' i
Data 64 , 128 , 132 , 125 , 0 ' j
Data 127 , 16 , 40 , 68 , 0   ' k
Data 0 , 65 , 127 , 64 , 0    ' l
Data 124 , 4 , 24 , 4 , 120   ' m
Data 124 , 4 , 4 , 120 , 0    ' n
Data 56 , 68 , 68 , 68 , 56   ' o
Data 252 , 68 , 68 , 68 , 56  ' p
Data 56 , 68 , 68 , 68 , 252  ' q
Data 68 , 120 , 68 , 4 , 8    ' r
Data 8 , 84 , 84 , 84 , 32    ' s
Data 4 , 62 , 68 , 36 , 0     ' t
Data 60 , 64 , 32 , 124 , 0   ' u
Data 28 , 32 , 64 , 32 , 28   ' v
Data 60 , 96 , 48 , 96 , 60   ' w
Data 108 , 16 , 16 , 108 , 0  ' x
Data 156 , 160 , 96 , 60 , 0  ' y
Data 100 , 84 , 84 , 76 , 0   ' z
Data 8 , 62 , 65 , 65 , 0     ' {
Data 0 , 0 , 119 , 0 , 0      ' |
Data 0 , 65 , 65 , 62 , 8     ' }
Data 2 , 1 , 2 , 1 , 0        ' ~
Data 0 , 255 , 255 , 255 , 0  ' 
Data 60 , 67 , 66 , 67 , 36   ' 
Data 60 , 66 , 66 , 67 , 36   ' 
Data 127 , 73 , 65 , 65 , 62  ' 
Data 36 , 75 , 74 , 75 , 52   ' 
Data 98 , 83 , 74 , 71 , 66   ' 
Data 56 , 70 , 68 , 70 , 40   ' 
Data 56 , 68 , 68 , 70 , 40   ' 
Data 48 , 72 , 72 , 74 , 127  ' 
Data 8 , 86 , 84 , 86 , 32    ' 
Data 100 , 86 , 84 , 78 , 68  ' 

Obuf$char_table_1:            'Auxiliary table for Њ, helps to determine its ASCII values
Data "Њ"

Obuf$write_text_ex:
End Sub