Simple 1x1 and 2x2 scaled font generation for SSD1315 displays

Started by david, Jul 16, 2023, 12:48 AM

Previous topic - Next topic

david

Greetings to All,
This code example is a follow up to my OLED Lite article I posted a while back and includes both 1x1 scale fonts and 2x2 scale fonts.
I'm sure you're aware that there are already some excellent and versatile libraries for this display in the Wiki but if you're more familiar with the character LCDs or a bit of a plodder like me you may find them difficult to follow, especially when spread over several files.
Hopefully this effort is a bare bones, minimal overhead approach that does 2x2 scaled fonts in just under 1k bytes of program and should be easy to follow.

Scaling the font columns in the x axis is very simple - you simply print the columns twice.  Scaling the columns in the y axis is much more involved as you're now spreading in to the next page (print line).
Rather than the usual approach of breaking down the font columns into bits and then reconstructing them as 2x height (over 2 pages) I have used a seemingly useless command of the display called "Zoom" which actually only expands 2x in the y axis.  This feature combined with the double column print gives our 2x2 scaling and saves quite a bit of code. 

These two lines put the display in to Zoom mode-
HBusOut OLED_W,[$00,$DA,16]  'Alt COM mode.  DELETE FOR 1x1 FONT.
HBusOut OLED_W,[$00,$D6,1]   'Enable Zoom mode. DELETE FOR 1x1 FONT.

The only other code required is a repeat of the column print-
HBusOut OLED_W,[$40,num]   'repeat above line for double width.  DELETE FOR 1x1 FONT.

and finally a repeat of the spacing column-
HBusOut OLED_W,[$40,$00]     'repeat blank spacer column.  DELETE FOR 1x1 FONT.

Please note that this is a minimal approach and it can not do lines, circles, boxes etc so is best thought of like a LCD character display.
Also note that while it's limited to 1x1 and 2x2 scaling this equates to 21Char x8Lines and 10Chars x4Lines respectively.  Higher scaling factors would quickly consume display space but may be suitable if only a few characters or numbers are presented. If you need large characters then refer to the other OLED resources in the Wiki.

Regards to All,
David

'OLED drive for 1x1 or 2x2 fonts (hardwarwe I2C)     
'128x64 display using SSD1315 chip.   
'996 bytes with 2x2 fonts (ascii 32 to 122)
'954 bytes with 1x1 fonts (ascii 32 to 122) 
'
'                  Vdd <1 U 8> Gnd   
'    Spare I/O     RA5 <2   7> RA0  Spare I/O
'    Spare I/O     RA4 <3   6> RA1  SCL
'   Spare input    RA3 <4   5> RA2  SDA           


Device = 12F1840
Config1 FCMEN_OFF, IESO_OFF, CLKOUTEN_OFF, BOREN_OFF, CPD_OFF, CP_OFF, MCLRE_OFF, PWRTE_ON, WDTE_OFF, FOSC_INTOSC
Config2 LVP_OFF, BORV_19, STVREN_OFF, PLLEN_OFF, WRT_OFF

Declare Xtal = 16 'Internal OSC
OSCCON = %01111010    '16MHz=01111010, 4MHz=01101010, 8MHz =01110010
TRISA = %00000110     '
Declare Hbus_Bitrate 1000    '100kHz, 400kHz, 1000kHz
SSPCON1=%00111000       'enable port, 7 bit address, master mode
Symbol OLED_W= %1111000  'OLED address + write
Symbol OLED_R= %1111001  'OLED address + read 

'**********    variables   ******************
Dim Col As Byte  'columns of font data
Dim Pge As Byte  'text lines 0 to 7 down display
Dim num As Byte  'index number in Cdata array
Dim Chr As Byte  'character in Cdata array
Dim length As Byte
Dim mystring As String *10  'the words/numbers you want to print     
Dim ascii As Byte 'ascii value of character

'initialize display
        HBusOut OLED_W,[$80,$AE,_      'display OFF
                        $00,$20,$00,_  'Mem mode  Horiz addressing
                        $00,$81,$7F,_  'contrast control reg/value
                        $80,$A1,_      'set segment remap
                        $80,$A4,_      'graphic RAM displayed
                        $80,$A6,_      'display norm/reverse
                        $00,$A8,$3F,_  'multiplex ratio/value
                        $80,$C8,_      'com scan direction
                        $00,$D5,$90,_  'set osc division/value
                        $00,$D9,$22,_  'set precharge period/value
                        $00,$DA,$12,_  'set Com pins/value
                        $00,$DB,$30,_  'set vcomh/value
                        $00,$8D,$14,_  'set charge pump enable/value
                        $80,$AF]       'display ON
   
       
Wipe:  'Clear random display in RAM
       For Pge=0 To 7
         For Col=0 To 127
           HBusOut OLED_W,[$40,$00]  'write zero down each font col
         Next Col       
       Next Pge
       DelayMS 500
     
       HBusOut OLED_W,[$00,$DA,16]  'Alt COM mode.  DELETE FOR 1x1 FONT.
       HBusOut OLED_W,[$00,$D6,1]   'Enable zoom mode. DELETE FOR 1x1 FONT.
'*******************************************
'1st line of text.     
      mystring="Test One"      'print input
      HBusOut OLED_W,[$00,$21,$00,$7F]  'Set print cursor x at 0. (max 127)
      HBusOut OLED_W,[$00,$22,$00,$07]  'Set print cursor y at 0. (max7)
      GoSub printing
'*******************************************
'2nd line of text     
      mystring="23.4Volts"      'print input
      HBusOut OLED_W,[$00,$21,$00,$7F]  'Set print cursor x at 0. (max 127)
      HBusOut OLED_W,[$00,$22,$01,$07]  'Set print cursor y at 1. (max7)
      GoSub printing
      DelayMS 500
      GoTo Wipe          'Repeat again but cursor not restored so 2nd line flashes.
'*******************************************
'Print subroutine     
printing:
      For Chr=0 To Len (mystring)-1   'indexing the look up table
      ascii=mystring[Chr]-32
        For Col=0 To 4                '5 bytes per character
          num= CRead8 Fonts [ascii*5+Col]
          HBusOut OLED_W,[$40,num]    'print values into columns
          HBusOut OLED_W,[$40,num]   'repeat above line for double width.  DELETE FOR 1x1 FONT.
        Next Col                   
        HBusOut OLED_W,[$40,$00]     'print blank spacer column.
        HBusOut OLED_W,[$40,$00]     'repeat blank spacer column.  DELETE FOR 1x1 FONT.
       Next Chr
       Return
           
Dim Fonts As Code=As Byte  $00, $00, $00, $00, $00,_      ' char space  0 Start +4chr ascii 32
                           $00, $00, $BE, $00, $00,_      ' char !      1  *5   +4chr
                           $00, $00, $03, $00, $03,_      ' char "
                           $50, $F8, $50, $F8, $50,_      ' char #
                           $48, $54, $FE, $54, $24,_      ' char $
                           $98, $58, $20, $D0, $C8,_      ' char %
                           $60, $9C, $AA, $44, $80,_      ' char &
                           $00, $00, $00, $03, $00,_      ' char '
                           $00, $38, $44, $82, $00,_      ' char (
                           $00, $82, $44, $38, $00,_      ' char )
                           $02, $06, $03, $06, $02,_      ' char *
                           $10, $10, $7C, $10, $10,_      ' char +
                           $A0, $60, $00, $00, $00,_      ' char ,
                           $10, $10, $10, $10, $10,_      ' char -
                           $C0, $C0, $00, $00, $00,_      ' char . 
                           $80, $40, $20, $10, $08,_      ' char / 
                           $7C, $A2, $92, $8A, $7C,_      ' char 0 
                           $00, $84, $FE, $80, $00,_      ' char 1 
                           $C4, $A2, $92, $92, $8C,_      ' char 2 
                           $44, $82, $92, $92, $6C,_      ' char 3 
                           $18, $14, $12, $FE, $10,_      ' char 4 
                           $9E, $92, $92, $92, $62,_      ' char 5 
                           $7C, $92, $92, $92, $64,_      ' char 6 
                           $06, $02, $E2, $12, $0E,_      ' char 7 
                           $6C, $92, $92, $92, $6C,_      ' char 8 
                           $4C, $92, $92, $92, $7C,_      ' char 9 
                           $CC, $CC, $00, $00, $00,_      ' char : 
                           $AC, $6C, $00, $00, $00,_      ' char ; 
                           $00, $10, $28, $44, $82,_      ' char < 
                           $48, $48, $48, $48, $48,_      ' char = 
                           $00, $82, $44, $28, $10,_      ' char > 
                           $04, $02, $B2, $12, $0C,_      ' char ? 
                           $7C, $82, $BA, $AA, $BC,_      ' char @ 
                           $F8, $14, $12, $14, $F8,_      ' char A 
                           $FE, $92, $92, $92, $6C,_      ' char B 
                           $7C, $82, $82, $82, $44,_      ' char C 
                           $FE, $82, $82, $44, $38,_      ' char D 
                           $FE, $92, $92, $82, $82,_      ' char E 
                           $FE, $12, $12, $02, $02,_      ' char F 
                           $7C, $82, $92, $92, $F4,_      ' char G 
                           $FE, $10, $10, $10, $FE,_      ' char H 
                           $00, $82, $FE, $82, $00,_      ' char I 
                           $60, $80, $80, $80, $7E,_      ' char J 
                           $FE, $10, $18, $24, $C2,_      ' char K 
                           $FE, $80, $80, $80, $80,_      ' char L 
                           $FE, $04, $38, $04, $FE,_      ' char M 
                           $FE, $04, $08, $10, $FE,_      ' char N 
                           $7C, $82, $82, $82, $7C,_      ' char O 
                           $FE, $12, $12, $12, $0C,_      ' char P 
                           $7C, $82, $A2, $C2, $FC,_      ' char Q 
                           $FE, $12, $12, $12, $EC,_      ' char R 
                           $4C, $92, $92, $92, $64,_      ' char S 
                           $02, $02, $FE, $02, $02,_      ' char T 
                           $7E, $80, $80, $80, $7E,_      ' char U 
                           $3E, $40, $80, $40, $3E,_      ' char V 
                           $FE, $80, $70, $80, $FE,_      ' char W 
                           $C6, $28, $10, $28, $C6,_      ' char X     
                           $06, $08, $F0, $08, $06,_      ' char Y 
                           $C2, $A2, $92, $8A, $86,_      ' char Z 
                           $00, $FE, $82, $82, $00,_      ' char [ 
                           $08, $10, $20, $40, $80,_      ' char \
                           $00, $82, $82, $FE, $00,_      ' char ] 
                           $00, $08, $04, $02, $04,_      ' char ^ 
                           $80, $80, $80, $80, $80,_      ' char _ 
                           $00, $00, $02, $04, $00,_      ' char ` 
                           $40, $A8, $A8, $A8, $F0,_      ' char a 
                           $FE, $88, $88, $88, $70,_      ' char b 
                           $70, $88, $88, $88, $10,_      ' char c 
                           $70, $88, $88, $88, $FE,_      ' char d 
                           $70, $A8, $A8, $A8, $30,_      ' char e 
                           $10, $FC, $12, $12, $04,_      ' char f 
                           $90, $A8, $A8, $A8, $70,_      ' char g 
                           $FE, $10, $10, $10, $E0,_      ' char h 
                           $00, $90, $F4, $80, $00,_      ' char i 
                           $40, $80, $80, $90, $74,_      ' char j 
                           $FE, $20, $50, $88, $00,_      ' char k 
                           $7E, $80, $80, $00, $00,_      ' char l 
                           $F8, $08, $70, $08, $F0,_      ' char m 
                           $F8, $08, $08, $08, $F0,_      ' char n 
                           $70, $88, $88, $88, $70,_      ' char o 
                           $F8, $28, $28, $28, $10,_      ' char p 
                           $10, $28, $28, $F8, $80,_      ' char q 
                           $F8, $08, $08, $08, $10,_      ' char r 
                           $90, $A8, $A8, $A8, $48,_      ' char s 
                           $08, $08, $FE, $88, $88,_      ' char t 
                           $78, $80, $80, $80, $F8,_      ' char u 
                           $38, $40, $80, $40, $38,_      ' char v 
                           $F8, $80, $70, $80, $F8,_      ' char w 
                           $88, $50, $20, $50, $88,_      ' char x 
                           $18, $A0, $A0, $A0, $78,_      ' char y 
                           $88, $C8, $A8, $98, $88        ' char z     ascii 122
 End