OLED Display
Intro
I really like the neat little OLED displays. They are crisp and draw only some miliamps. Cheap OLED display have a resolution 128x32 that's enough for about 4 lines and 20 characters.

Driver oled.c, fonts from https://www.mikrocontroller.net/topic/54860.

Code page 850

Code page 850, 9x14

OLED and I2C Words

oled-emit works like the standard word emit. It blocks the calling thread, as long as the character is not written to the OLED display (less than 300 us for a 6x8 character and 400 kHz I2C). Horizontal (x) position is in pixel (0 to 127), vertical position (y) is in lines, a line consists of 8 pixels. 0, 0 is upper left corner. Larger fonts takes more than one line.

oled-emit    ( Char -- )        Emits a character (writes a character to the OLED display)
oled-emit?   ( -- Flag )        OLED ready to get a character (I2C not busy)

hook-emit    ( -- a-addr )      Hooks for redirecting terminal IO on the fly
hook-emit?   ( -- a-addr )    

oledpos!     ( x y -- )         Set OLED cursor position, 
                                x (column) horizontal position, max. 127  
                                y (row) vertical position (a line consists of 8 pixels), max. 3 for 128x32 or 7 for 128x64 displays.
oledpos@     (  -- x y )        Get the current OLED cursor position
oledcmd      ( c-addr -- )      Send command to the OLED controller SSD1306. First byte contains the length of the command.
oledclr      (  --  )           Clears the OLED display, sets the cursor to 0, 0
oledfont     ( u --  )          Select the font, u: 0 6x8, 1 8x8, 2 8X16 , 3 12X16
oledcolumn!  ( u -- )           Write a column (8 pixels) to the current position. Increment position. Bit 0 on top
oledcolumn@  ( -- u )           Read a column (8 pixels) from the current position

I2Cdev       ( u -- )           Sets the 7-bit I2C address
I2Cput       ( c-addr u -- )    c-addr buffer address for the message with lengh u to send
I2Cget       ( c-addr u -- )    c-addr buffer address for the message to receive
I2Cputget    ( a1 u1 a2 u2 -- ) a1 buffer address for the message to send, a2 buffer address for the message to receive

>oled        ( -- addr1 addr2 ) redirect to oled *)
>term        ( addr1 addr2 -- ) terminate redirection *)

*) part of redirection.fs

Usage

It is easy to redirect the terminal output to the OLED display, to use the string formatting words.

: oled-hallo (  -- )
  hook-emit @              \ save emit hook
  ['] oled-emit hook-emit ! \ redirect terminal to oled-emit
  ." Hallo Velo! " cr
  ." ciao"
  hook-emit !         \ restore old hook
;

or even simpler

: oled-hallo (  -- )
  >oled         \ redirect terminal to oled-emit
  ." Hallo Velo! " cr
  ." ciao"
  >term         \ terminate redirection
;

or on a command line

>oled .( Hallo Velo!) >term

show date and time on OLED (see CmsisRtos#How_to_use_Tasks for a background task).

: clock (  -- )
  oledclr
  3 oledfont
  -1 -1 -1 alarm!  \ set an alarm every second
  begin
    wait-alarm     \ wait a second
    0 0 oledpos!
    >oled .time >term
  key? until
  key drop
;

To change the display type you have to edit the oled.h and rebuild the project:

#define OLED_4PIN_128X32	0		// the cheap one with 4 pins, Adafruit Raspberry Pi #3527
#define	OLED_BONNET_128X64	1		// Adafruit Bonnet for Raspberry Pi #3531
#define OLED_FEATHER_128X32	2		// Adafruit FeatherWing #2900
#define OLED_FEATHER_128X64	3		// Adafruit FeatherWing #4650
#define OLED_OCTOPUS_128X64	4		// Octopus PIS-1277

#define OLED_LANDSCAPE		0
#define	OLED_PORTRAIT		1

// configure your OLED type
#define OLED_DISPLAY_TYPE	OLED_OCTOPUS_128X64
#define	OLED_ORIENTATION	OLED_LANDSCAPE

Links

SSD1306 Driver

Driver SW

Other OLEDs with SSD1306

SH1107 Driver

Slave address bit (SA0), D/C pin acts as SA0.

FeatherWing 128x64 OLED

-- Peter Schmid - 2020-12-29

Creative Commons License
This work by Peter Schmid is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng Codepage-850.png r1 manage 2.4 K 2021-01-05 - 21:21 PeterSchmid  
JPEGjpg oled-mecrisp-cube-header.jpg r1 manage 66.2 K 2021-01-02 - 22:14 PeterSchmid  
JPEGjpg oled-mecrisp-cube.JPG r1 manage 5100.2 K 2021-01-02 - 22:14 PeterSchmid  
Edit | Attach | Watch | Print version | History: r34 < r33 < r32 < r31 < r30 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r34 - 2022-12-23 - PeterSchmid
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback