Manage the 128x32 monochrome SSD1306 OLED display
published: 10 October 2020 / updated 12 October 2020
Structure of the SSD1306 128x32 display screen
The SSD1306 128x32 display screen uses the same internal component as SSD1306 128x64 display
The internal memory is common to both models, the 128x32 display screen does not use that part of this memory.
Organization of memory
The internal memory of the display has 1KB RAM.
In this diagram, here is the organization of the screen for a definition of 128x64 pixels:

Each column contains 8 bits. A line is designated by page :
- the 128x64 display: contains 8 pages, numbered from 0 to 7
- the 128x32 display: contains 4 pages, numbered from 0 to 3
In the rest of the article, we will not only focus on the display 128x32 pixels. But most of the explanations still apply to the 128x64 display.
Each page is divided into segments:

Here, in blue in the figure, a segment represents a byte. The least significant bit is at the top.
Transmission of a graphic icon
From the organization of pages and segments, it becomes easy to create a graphic pattern:
. X X X X X X . . X . . . . X . X X . . . . X X X . X X X X . X X . X X X X . X X . X X X X . X X . . . . . . X X . X X X X . X X . X X X X . X X . X X X X . X X . . . . . . X X . X X X X . X X . X X X X . X X . X X X X . X X . . . . . . X X . X X X X . X X . X X X X . X X . X X X X . X X . . . . . . X X X X X X X X X
Tilt your head to the right. You will guess the logo of a battery. We will code this logo in binary:
flash
stream: BATT
CTRL_DATAS c,
%01111110 c,
%01000010 c,
%11000011 c,
%10111101 c,
%10111101 c,
%10111101 c,
%10000001 c,
%10111101 c,
%10111101 c,
%10111101 c,
%10000001 c,
%10111101 c,
%10111101 c,
%10111101 c,
%10000001 c,
%10111101 c,
%10111101 c,
%10111101 c,
%10000001 c,
%11111111 c,
;stream
ram
In this code, each bit set to 1 represents a lit pixel. Once compiled, we can
run BATT from the Tera Term terminal. And here's what we get on display:

Here, this logo is displayed at the beginning of page 0. Question: How to display our logo elsewhere?
Set display position
Here, to define the display position of our battery logo, we will generate a positioning command:
flash
stream: xy.batt
CTRL_COMMANDS c,
$21 c, \ COL START_END
$6b c, \ start
$7f c, \ end
$22 c, \ PAGE START_END
$00 c, \ start
$00 c, \ end
;stream
ram
The $21 command is used to define the start and end display segment: $6b (107 in decimal) will be the starting segment, $7f (127 in decimal) will be the segment end.
The $22 command is used to define the start and end display page: $00 will be the start and end page.
We run xy.bat BATT and we have on display:

Define a dynamic logo
It's fine to display a battery logo. but to be really useful, it would be nice if this logo indicates the real state of charge of a real battery.
If we analyze the design of the logo, we can see that the internal motif is repeated. It's about
obviously four internal pavers. We will therefore define a word BATTfull for this
only part:
flash
stream: BATTfull
CTRL_DATAS c,
%10111101 c,
%10111101 c,
%10111101 c,
%10000001 c,
;stream
ram
Then we define the same drum section, but empty:
flash
stream: BATTempty
CTRL_DATAS c,
%10000001 c,
%10000001 c,
%10000001 c,
%10000001 c,
;stream
ram
And finally, we define the end and the beginning of the battery:
flash
stream: BATTend
CTRL_DATAS c,
%11111111 c,
;stream
stream: BATTstart
CTRL_DATAS c,
%01111110 c,
%01000010 c,
%11000011 c,
;stream
ram
And now it's enough to put the pieces back together:
stream: 4SPACES
CTRL_DATAS c,
%00000000 c,
%00000000 c,
%00000000 c,
%00000000 c,
;stream
ram
: bat04 ( ---)
BATTstart
BATTempty BATTempty BATTempty BATTempty
BATTend ;
: bat14 ( ---)
BATTstart
BATTempty BATTempty BATTempty BATTfull
BATTend ;
: bat24 ( ---)
BATTstart
BATTempty BATTempty BATTfull BATTfull
BATTend ;
: bat34 ( ---)
BATTstart
BATTempty BATTfull BATTfull BATTfull
BATTend ;
: bat44 ( ---)
BATTstart
BATTfull BATTfull BATTfull BATTfull
BATTend ;
: testBATTS ( ---)
disp.reset
bat04 4SPACES
bat14 4SPACES
bat24 4SPACES
bat34 4SPACES
bat44 ;
Running testBATTS displays this:

So to display text just set as many logos for each character to display. This is what we will see in the next article.
