Refactor the oled_i2c example (#217)

This PR does a number of things:

1. Renames the oled_i2c example to sssd1306_i2c to match other example folder
   names that use the device name
2. Merge code from another ssd1306 exmaple that was sitting in another PR
3. Add a few more demo parts to the code for extra fun.
4. Couple of bug fixes that were reported in original code to fix handling different
   display sizes, so code now works for 32 and 64 pixel height displays by changing
   a define at top of code.
This commit is contained in:
James Hughes
2022-11-25 16:38:11 +00:00
committed by GitHub
parent 75c6a5e9db
commit aa9a72b494
14 changed files with 508 additions and 328 deletions

View File

@@ -7,7 +7,7 @@ if (NOT PICO_NO_HARDWARE)
add_subdirectory(mma8451_i2c)
add_subdirectory(mpl3115a2_i2c)
add_subdirectory(mpu6050_i2c)
add_subdirectory(oled_i2c)
add_subdirectory(ssd1306_i2c)
add_subdirectory(pa1010d_i2c)
add_subdirectory(pcf8523_i2c)
endif ()

View File

@@ -1,12 +0,0 @@
add_executable(oled_i2c
oled_i2c.c
)
# pull in common dependencies and additional i2c hardware support
target_link_libraries(oled_i2c pico_stdlib hardware_i2c)
# create map/bin/hex file etc.
pico_add_extra_outputs(oled_i2c)
# add url via pico_set_program_url
example_auto_set_url(oled_i2c)

View File

@@ -1,298 +0,0 @@
/**
* Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/i2c.h"
#include "raspberry26x32.h"
/* Example code to talk to an SSD1306-based OLED display
NOTE: Ensure the device is capable of being driven at 3.3v NOT 5v. The Pico
GPIO (and therefore I2C) cannot be used at 5v.
You will need to use a level shifter on the I2C lines if you want to run the
board at 5v.
Connections on Raspberry Pi Pico board, other boards may vary.
GPIO PICO_DEFAULT_I2C_SDA_PIN (on Pico this is GP4 (pin 6)) -> SDA on display
board
GPIO PICO_DEFAULT_I2C_SCK_PIN (on Pico this is GP5 (pin 7)) -> SCL on
display board
3.3v (pin 36) -> VCC on display board
GND (pin 38) -> GND on display board
*/
// commands (see datasheet)
#define OLED_SET_CONTRAST _u(0x81)
#define OLED_SET_ENTIRE_ON _u(0xA4)
#define OLED_SET_NORM_INV _u(0xA6)
#define OLED_SET_DISP _u(0xAE)
#define OLED_SET_MEM_ADDR _u(0x20)
#define OLED_SET_COL_ADDR _u(0x21)
#define OLED_SET_PAGE_ADDR _u(0x22)
#define OLED_SET_DISP_START_LINE _u(0x40)
#define OLED_SET_SEG_REMAP _u(0xA0)
#define OLED_SET_MUX_RATIO _u(0xA8)
#define OLED_SET_COM_OUT_DIR _u(0xC0)
#define OLED_SET_DISP_OFFSET _u(0xD3)
#define OLED_SET_COM_PIN_CFG _u(0xDA)
#define OLED_SET_DISP_CLK_DIV _u(0xD5)
#define OLED_SET_PRECHARGE _u(0xD9)
#define OLED_SET_VCOM_DESEL _u(0xDB)
#define OLED_SET_CHARGE_PUMP _u(0x8D)
#define OLED_SET_HORIZ_SCROLL _u(0x26)
#define OLED_SET_SCROLL _u(0x2E)
#define OLED_ADDR _u(0x3C)
#define OLED_HEIGHT _u(32)
#define OLED_WIDTH _u(128)
#define OLED_PAGE_HEIGHT _u(8)
#define OLED_NUM_PAGES OLED_HEIGHT / OLED_PAGE_HEIGHT
#define OLED_BUF_LEN (OLED_NUM_PAGES * OLED_WIDTH)
#define OLED_WRITE_MODE _u(0xFE)
#define OLED_READ_MODE _u(0xFF)
struct render_area {
uint8_t start_col;
uint8_t end_col;
uint8_t start_page;
uint8_t end_page;
int buflen;
};
void fill(uint8_t buf[], uint8_t fill) {
// fill entire buffer with the same byte
for (int i = 0; i < OLED_BUF_LEN; i++) {
buf[i] = fill;
}
};
void fill_page(uint8_t *buf, uint8_t fill, uint8_t page) {
// fill entire page with the same byte
memset(buf + (page * OLED_WIDTH), fill, OLED_WIDTH);
};
// convenience methods for printing out a buffer to be rendered
// mostly useful for debugging images, patterns, etc
void print_buf_page(uint8_t buf[], uint8_t page) {
// prints one page of a full length (128x4) buffer
for (int j = 0; j < OLED_PAGE_HEIGHT; j++) {
for (int k = 0; k < OLED_WIDTH; k++) {
printf("%u", (buf[page * OLED_WIDTH + k] >> j) & 0x01);
}
printf("\n");
}
}
void print_buf_pages(uint8_t buf[]) {
// prints all pages of a full length buffer
for (int i = 0; i < OLED_NUM_PAGES; i++) {
printf("--page %d--\n", i);
print_buf_page(buf, i);
}
}
void print_buf_area(uint8_t *buf, struct render_area *area) {
// print a render area of generic size
int area_width = area->end_col - area->start_col + 1;
int area_height = area->end_page - area->start_page + 1; // in pages, not pixels
for (int i = 0; i < area_height; i++) {
for (int j = 0; j < OLED_PAGE_HEIGHT; j++) {
for (int k = 0; k < area_width; k++) {
printf("%u", (buf[i * area_width + k] >> j) & 0x01);
}
printf("\n");
}
}
}
void calc_render_area_buflen(struct render_area *area) {
// calculate how long the flattened buffer will be for a render area
area->buflen = (area->end_col - area->start_col + 1) * (area->end_page - area->start_page + 1);
}
#ifdef i2c_default
void oled_send_cmd(uint8_t cmd) {
// I2C write process expects a control byte followed by data
// this "data" can be a command or data to follow up a command
// Co = 1, D/C = 0 => the driver expects a command
uint8_t buf[2] = {0x80, cmd};
i2c_write_blocking(i2c_default, (OLED_ADDR & OLED_WRITE_MODE), buf, 2, false);
}
void oled_send_buf(uint8_t buf[], int buflen) {
// in horizontal addressing mode, the column address pointer auto-increments
// and then wraps around to the next page, so we can send the entire frame
// buffer in one gooooooo!
// copy our frame buffer into a new buffer because we need to add the control byte
// to the beginning
// TODO find a more memory-efficient way to do this..
// maybe break the data transfer into pages?
uint8_t *temp_buf = malloc(buflen + 1);
for (int i = 1; i < buflen + 1; i++) {
temp_buf[i] = buf[i - 1];
}
// Co = 0, D/C = 1 => the driver expects data to be written to RAM
temp_buf[0] = 0x40;
i2c_write_blocking(i2c_default, (OLED_ADDR & OLED_WRITE_MODE), temp_buf, buflen + 1, false);
free(temp_buf);
}
void oled_init() {
// some of these commands are not strictly necessary as the reset
// process defaults to some of these but they are shown here
// to demonstrate what the initialization sequence looks like
// some configuration values are recommended by the board manufacturer
oled_send_cmd(OLED_SET_DISP | 0x00); // set display off
/* memory mapping */
oled_send_cmd(OLED_SET_MEM_ADDR); // set memory address mode
oled_send_cmd(0x00); // horizontal addressing mode
/* resolution and layout */
oled_send_cmd(OLED_SET_DISP_START_LINE); // set display start line to 0
oled_send_cmd(OLED_SET_SEG_REMAP | 0x01); // set segment re-map
// column address 127 is mapped to SEG0
oled_send_cmd(OLED_SET_MUX_RATIO); // set multiplex ratio
oled_send_cmd(OLED_HEIGHT - 1); // our display is only 32 pixels high
oled_send_cmd(OLED_SET_COM_OUT_DIR | 0x08); // set COM (common) output scan direction
// scan from bottom up, COM[N-1] to COM0
oled_send_cmd(OLED_SET_DISP_OFFSET); // set display offset
oled_send_cmd(0x00); // no offset
oled_send_cmd(OLED_SET_COM_PIN_CFG); // set COM (common) pins hardware configuration
oled_send_cmd(0x02); // manufacturer magic number
/* timing and driving scheme */
oled_send_cmd(OLED_SET_DISP_CLK_DIV); // set display clock divide ratio
oled_send_cmd(0x80); // div ratio of 1, standard freq
oled_send_cmd(OLED_SET_PRECHARGE); // set pre-charge period
oled_send_cmd(0xF1); // Vcc internally generated on our board
oled_send_cmd(OLED_SET_VCOM_DESEL); // set VCOMH deselect level
oled_send_cmd(0x30); // 0.83xVcc
/* display */
oled_send_cmd(OLED_SET_CONTRAST); // set contrast control
oled_send_cmd(0xFF);
oled_send_cmd(OLED_SET_ENTIRE_ON); // set entire display on to follow RAM content
oled_send_cmd(OLED_SET_NORM_INV); // set normal (not inverted) display
oled_send_cmd(OLED_SET_CHARGE_PUMP); // set charge pump
oled_send_cmd(0x14); // Vcc internally generated on our board
oled_send_cmd(OLED_SET_SCROLL | 0x00); // deactivate horizontal scrolling if set
// this is necessary as memory writes will corrupt if scrolling was enabled
oled_send_cmd(OLED_SET_DISP | 0x01); // turn display on
}
void render(uint8_t *buf, struct render_area *area) {
// update a portion of the display with a render area
oled_send_cmd(OLED_SET_COL_ADDR);
oled_send_cmd(area->start_col);
oled_send_cmd(area->end_col);
oled_send_cmd(OLED_SET_PAGE_ADDR);
oled_send_cmd(area->start_page);
oled_send_cmd(area->end_page);
oled_send_buf(buf, area->buflen);
}
#endif
int main() {
stdio_init_all();
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
#warning i2c / oled_i2d example requires a board with I2C pins
puts("Default I2C pins were not defined");
#else
// useful information for picotool
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C));
bi_decl(bi_program_description("OLED I2C example for the Raspberry Pi Pico"));
printf("Hello, OLED display! Look at my raspberries..\n");
// I2C is "open drain", pull ups to keep signal high when no data is being
// sent
i2c_init(i2c_default, 400 * 1000);
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
// run through the complete initialization process
oled_init();
// initialize render area for entire frame (128 pixels by 4 pages)
struct render_area frame_area = {start_col: 0, end_col : OLED_WIDTH - 1, start_page : 0, end_page : OLED_NUM_PAGES -
1};
calc_render_area_buflen(&frame_area);
// zero the entire display
uint8_t buf[OLED_BUF_LEN];
fill(buf, 0x00);
render(buf, &frame_area);
// intro sequence: flash the screen 3 times
for (int i = 0; i < 3; i++) {
oled_send_cmd(0xA5); // ignore RAM, all pixels on
sleep_ms(500);
oled_send_cmd(0xA4); // go back to following RAM
sleep_ms(500);
}
// render 3 cute little raspberries
struct render_area area = {start_col: 0, end_col : IMG_WIDTH - 1, start_page : 0, end_page : OLED_NUM_PAGES - 1};
calc_render_area_buflen(&area);
render(raspberry26x32, &area);
for (int i = 1; i < 3; i++) {
uint8_t offset = 5 + IMG_WIDTH; // 5px padding
area.start_col += offset;
area.end_col += offset;
render(raspberry26x32, &area);
}
// configure horizontal scrolling
oled_send_cmd(OLED_SET_HORIZ_SCROLL | 0x00);
oled_send_cmd(0x00); // dummy byte
oled_send_cmd(0x00); // start page 0
oled_send_cmd(0x00); // time interval
oled_send_cmd(0x03); // end page 3
oled_send_cmd(0x00); // dummy byte
oled_send_cmd(0xFF); // dummy byte
// let's goooo!
oled_send_cmd(OLED_SET_SCROLL | 0x01);
#endif
return 0;
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 153 KiB

View File

@@ -0,0 +1,12 @@
add_executable(ssd1306_i2c
ssd1306_i2c.c
)
# pull in common dependencies and additional i2c hardware support
target_link_libraries(ssd1306_i2c pico_stdlib hardware_i2c)
# create map/bin/hex file etc.
pico_add_extra_outputs(ssd1306_i2c)
# add url via pico_set_program_url
example_auto_set_url(ssd1306_i2c)

View File

@@ -2,16 +2,13 @@
This example code shows how to interface the Raspberry Pi Pico with an 128x32 OLED display board based on the SSD1306 display driver, datasheet https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf[here].
The code displays a series of tiny raspberries that scroll horizontally, in the process showing you how to initialize the display, write to the entire display, write to only a portion of the display, and configure scrolling.
The code displays a series of small demo graphics; tiny raspberries that scroll horizontally, some text, and some line drawing, in the process showing you how to initialize the display, write to the entire display, write to only a portion of the display, configure scrolling, invert the display etc.
The SSD1306 is operated via a list of versatile commands (see datasheet) that allows the user to access all the capabilities of the driver. After sending a slave address, the data that follows can be either a command, flags to follow up a command or data to be written directly into the display's RAM. A control byte is required for each write after the slave address so that the driver knows what type of data is being sent.
This display is 32 pixels high by 128 pixels wide. These 32 vertical pixels are partitioned into 4 pages, each 8 pixels in height. In RAM, this looks roughly like:
The example code supports displays of 32 pixel or 64 pixels high by 128 pixels wide by changing a define at the top of the code.
[NOTE]
======
The SSD1306 can drive displays that are up to 64 pixels high and 128 pixels wide.
======
In the 32 vertical pixels case, the display is partitioned into 4 pages, each 8 pixels in height. In RAM, this looks roughly like:
----
| COL0 | COL1 | COL2 | COL3 | ... | COL126 | COL127 |
@@ -42,30 +39,24 @@ The driver has 3 modes of transferring the pixels in RAM to the display (provide
Horizontal addressing mode has the key advantage that we can keep one single 512 byte buffer (128 columns x 4 pages and each byte fills a page's rows) and write this in one go to the RAM (column address auto increments on writes as well as reads) instead of working with 2D matrices of pixels and adding more overhead.
[NOTE]
======
* The SSD1306 is able to drive 128x64 displays but as our display is 128x32, only half of the COM (common) pins are connected to the display.
* The specific display model being used is UG-2832HSWEG02
======
== Wiring information
Wiring up the device requires 4 jumpers, to connect VCC (3.3v), GND, SDA and SCL and optionally a 5th jumper for the driver RESET pin. The example here uses the default I2C port 0, which is assigned to GPIO 4 (SDA) and 5 (SCL) in software. Power is supplied from the 3.3V pin from the Pico.
[[oled_i2c_wiring]]
[[ssd1306_i2c_wiring]]
[pdfwidth=75%]
.Wiring Diagram for oled display via I2C.
image::oled_i2c_bb.png[]
image::ssd1306_i2c_bb.png[]
== List of Files
CMakeLists.txt:: CMake file to incorporate the example into the examples build tree.
oled_i2c.c:: The example code.
sd1306_i2c.c:: The example code.
== Bill of Materials
.A list of materials required for the example
[[oled_i2c-bom-table]]
[[ssd1306_i2c-bom-table]]
[cols=3]
|===
| *Item* | *Quantity* | Details

View File

Before

Width:  |  Height:  |  Size: 190 B

After

Width:  |  Height:  |  Size: 190 B

View File

@@ -0,0 +1,48 @@
/**
* Copyright (c) 2022 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
// Vertical bitmaps, A-Z, 0-9. Each is 8 pixels high and wide
// Theses are defined vertically to make them quick to copy to FB
static uint8_t font[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Nothing
0x1e, 0x28, 0x48, 0x88, 0x48, 0x28, 0x1e, 0x00, //A
0xfe, 0x92, 0x92, 0x92, 0x92, 0x92, 0xfe, 0x00, //B
0x7e, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x00, //C
0xfe, 0x82, 0x82, 0x82, 0x82, 0x82, 0x7e, 0x00, //D
0xfe, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x00, //E
0xfe, 0x90, 0x90, 0x90, 0x90, 0x80, 0x80, 0x00, //F
0xfe, 0x82, 0x82, 0x82, 0x8a, 0x8a, 0xce, 0x00, //G
0xfe, 0x10, 0x10, 0x10, 0x10, 0x10, 0xfe, 0x00, //H
0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0x00, //I
0x84, 0x82, 0x82, 0xfc, 0x80, 0x80, 0x80, 0x00, //J
0x00, 0xfe, 0x10, 0x10, 0x28, 0x44, 0x82, 0x00, //K
0xfe, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, //L
0xfe, 0x40, 0x20, 0x10, 0x20, 0x40, 0xfe, 0x00, //M
0xfe, 0x40, 0x20, 0x10, 0x08, 0x04, 0xfe, 0x00, //N
0x7c, 0x82, 0x82, 0x82, 0x82, 0x82, 0x7c, 0x00, //O
0xfe, 0x88, 0x88, 0x88, 0x88, 0x88, 0x70, 0x00, //P
0x7c, 0x82, 0x82, 0x92, 0x8a, 0x86, 0x7e, 0x00, //Q
0xfe, 0x88, 0x88, 0x88, 0x8c, 0x8a, 0x70, 0x00, //R
0x62, 0x92, 0x92, 0x92, 0x92, 0x0c, 0x00, 0x00, //S
0x80, 0x80, 0x80, 0xfe, 0x80, 0x80, 0x80, 0x00, //T
0xfc, 0x02, 0x02, 0x02, 0x02, 0x02, 0xfc, 0x00, //U
0xf0, 0x08, 0x04, 0x02, 0x04, 0x08, 0xf0, 0x00, //V
0xfe, 0x04, 0x08, 0x10, 0x08, 0x04, 0xfe, 0x00, //W
0x00, 0x82, 0x44, 0x28, 0x28, 0x44, 0x82, 0x00, //X
0x80, 0x40, 0x20, 0x1e, 0x20, 0x40, 0x80, 0x00, //Y
0x82, 0x86, 0x9a, 0xa2, 0xc2, 0x82, 0x00, 0x00, //Z
0x7c, 0x82, 0x82, 0x92, 0x82, 0x82, 0x7c, 0x00, //0
0x00, 0x00, 0x42, 0xfe, 0x02, 0x00, 0x00, 0x00, //1
0x0c, 0x92, 0x92, 0x92, 0x92, 0x62, 0x00, 0x00, //2
0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x6c, 0x00, //3
0xfc, 0x04, 0x04, 0x1e, 0x04, 0x04, 0x00, 0x00, //4
0xf2, 0x92, 0x92, 0x92, 0x92, 0x0c, 0x00, 0x00, //5
0xfc, 0x12, 0x12, 0x12, 0x12, 0x12, 0x0c, 0x00, //6
0x80, 0x80, 0x80, 0x86, 0x8c, 0xb0, 0xc0, 0x00, //7
0x6c, 0x92, 0x92, 0x92, 0x92, 0x92, 0x6c, 0x00, //8
0x60, 0x90, 0x90, 0x90, 0x90, 0x90, 0xfe, 0x00, //9
};

View File

@@ -0,0 +1,439 @@
/**
* Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/i2c.h"
#include "raspberry26x32.h"
#include "ssd1306_font.h"
/* Example code to talk to an SSD1306-based OLED display
The SSD1306 is an OLED/PLED driver chip, capable of driving displays up to
128x64 pixels.
NOTE: Ensure the device is capable of being driven at 3.3v NOT 5v. The Pico
GPIO (and therefore I2C) cannot be used at 5v.
You will need to use a level shifter on the I2C lines if you want to run the
board at 5v.
Connections on Raspberry Pi Pico board, other boards may vary.
GPIO PICO_DEFAULT_I2C_SDA_PIN (on Pico this is GP4 (pin 6)) -> SDA on display
board
GPIO PICO_DEFAULT_I2C_SCL_PIN (on Pico this is GP5 (pin 7)) -> SCL on
display board
3.3v (pin 36) -> VCC on display board
GND (pin 38) -> GND on display board
*/
// Define the size of the display we have attached. This can vary, make sure you
// have the right size defined or the output will look rather odd!
// Code has been tested on 128x32 and 128x64 OLED displays
#define SSD1306_HEIGHT 32
#define SSD1306_WIDTH 128
#define SSD1306_I2C_ADDR _u(0x3C)
// 400 is usual, but often these can be overclocked to improve display response.
// Tested at 1000 on both 32 and 84 pixel height devices and it worked.
#define SSD1306_I2C_CLK 400
//#define SSD1306_I2C_CLK 1000
// commands (see datasheet)
#define SSD1306_SET_MEM_MODE _u(0x20)
#define SSD1306_SET_COL_ADDR _u(0x21)
#define SSD1306_SET_PAGE_ADDR _u(0x22)
#define SSD1306_SET_HORIZ_SCROLL _u(0x26)
#define SSD1306_SET_SCROLL _u(0x2E)
#define SSD1306_SET_DISP_START_LINE _u(0x40)
#define SSD1306_SET_CONTRAST _u(0x81)
#define SSD1306_SET_CHARGE_PUMP _u(0x8D)
#define SSD1306_SET_SEG_REMAP _u(0xA0)
#define SSD1306_SET_ENTIRE_ON _u(0xA4)
#define SSD1306_SET_ALL_ON _u(0xA5)
#define SSD1306_SET_NORM_DISP _u(0xA6)
#define SSD1306_SET_INV_DISP _u(0xA7)
#define SSD1306_SET_MUX_RATIO _u(0xA8)
#define SSD1306_SET_DISP _u(0xAE)
#define SSD1306_SET_COM_OUT_DIR _u(0xC0)
#define SSD1306_SET_COM_OUT_DIR_FLIP _u(0xC0)
#define SSD1306_SET_DISP_OFFSET _u(0xD3)
#define SSD1306_SET_DISP_CLK_DIV _u(0xD5)
#define SSD1306_SET_PRECHARGE _u(0xD9)
#define SSD1306_SET_COM_PIN_CFG _u(0xDA)
#define SSD1306_SET_VCOM_DESEL _u(0xDB)
#define SSD1306_PAGE_HEIGHT _u(8)
#define SSD1306_NUM_PAGES (SSD1306_HEIGHT / SSD1306_PAGE_HEIGHT)
#define SSD1306_BUF_LEN (SSD1306_NUM_PAGES * SSD1306_WIDTH)
#define SSD1306_WRITE_MODE _u(0xFE)
#define SSD1306_READ_MODE _u(0xFF)
struct render_area {
uint8_t start_col;
uint8_t end_col;
uint8_t start_page;
uint8_t end_page;
int buflen;
};
void calc_render_area_buflen(struct render_area *area) {
// calculate how long the flattened buffer will be for a render area
area->buflen = (area->end_col - area->start_col + 1) * (area->end_page - area->start_page + 1);
}
#ifdef i2c_default
inline void SSD1306_send_cmd(uint8_t cmd) {
// I2C write process expects a control byte followed by data
// this "data" can be a command or data to follow up a command
// Co = 1, D/C = 0 => the driver expects a command
uint8_t buf[2] = {0x80, cmd};
i2c_write_blocking(i2c_default, (SSD1306_I2C_ADDR & SSD1306_WRITE_MODE), buf, 2, false);
}
void SSD1306_send_cmd_list(uint8_t *buf, int num) {
for (int i=0;i<num;i++)
SSD1306_send_cmd(buf[i]);
}
void SSD1306_send_buf(uint8_t buf[], int buflen) {
// in horizontal addressing mode, the column address pointer auto-increments
// and then wraps around to the next page, so we can send the entire frame
// buffer in one gooooooo!
// copy our frame buffer into a new buffer because we need to add the control byte
// to the beginning
uint8_t *temp_buf = malloc(buflen + 1);
temp_buf[0] = 0x40;
memcpy(temp_buf+1, buf, buflen);
i2c_write_blocking(i2c_default, (SSD1306_I2C_ADDR & SSD1306_WRITE_MODE), temp_buf, buflen + 1, false);
free(temp_buf);
}
void SSD1306_init() {
// Some of these commands are not strictly necessary as the reset
// process defaults to some of these but they are shown here
// to demonstrate what the initialization sequence looks like
// Some configuration values are recommended by the board manufacturer
uint8_t cmds[] = {
SSD1306_SET_DISP, // set display off
/* memory mapping */
SSD1306_SET_MEM_MODE, // set memory address mode 0 = horizontal, 1 = vertical, 2 = page
0x00, // horizontal addressing mode
/* resolution and layout */
SSD1306_SET_DISP_START_LINE, // set display start line to 0
SSD1306_SET_SEG_REMAP | 0x01, // set segment re-map, column address 127 is mapped to SEG0
SSD1306_SET_MUX_RATIO, // set multiplex ratio
SSD1306_HEIGHT - 1, // Display height - 1
SSD1306_SET_COM_OUT_DIR | 0x08, // set COM (common) output scan direction. Scan from bottom up, COM[N-1] to COM0
SSD1306_SET_DISP_OFFSET, // set display offset
0x00, // no offset
SSD1306_SET_COM_PIN_CFG, // set COM (common) pins hardware configuration. Board specific magic number.
// 0x02 Works for 128x32, 0x12 Possibly works for 128x64. Other options 0x22, 0x32
#if ((SSD1306_WIDTH == 128) && (SSD1306_HEIGHT == 32))
0x02,
#elif ((SSD1306_WIDTH == 128) && (SSD1306_HEIGHT == 64))
0x12,
#else
0x02,
#endif
/* timing and driving scheme */
SSD1306_SET_DISP_CLK_DIV, // set display clock divide ratio
0x80, // div ratio of 1, standard freq
SSD1306_SET_PRECHARGE, // set pre-charge period
0xF1, // Vcc internally generated on our board
SSD1306_SET_VCOM_DESEL, // set VCOMH deselect level
0x30, // 0.83xVcc
/* display */
SSD1306_SET_CONTRAST, // set contrast control
0xFF,
SSD1306_SET_ENTIRE_ON, // set entire display on to follow RAM content
SSD1306_SET_NORM_DISP, // set normal (not inverted) display
SSD1306_SET_CHARGE_PUMP, // set charge pump
0x14, // Vcc internally generated on our board
SSD1306_SET_SCROLL | 0x00, // deactivate horizontal scrolling if set. This is necessary as memory writes will corrupt if scrolling was enabled
SSD1306_SET_DISP | 0x01, // turn display on
};
SSD1306_send_cmd_list(cmds, count_of(cmds));
}
void SSD1306_scroll(bool on) {
// configure horizontal scrolling
uint8_t cmds[] = {
SSD1306_SET_HORIZ_SCROLL | 0x00,
0x00, // dummy byte
0x00, // start page 0
0x00, // time interval
0x03, // end page 3 SSD1306_NUM_PAGES ??
0x00, // dummy byte
0xFF, // dummy byte
SSD1306_SET_SCROLL | (on ? 0x01 : 0) // Start/stop scrolling
};
SSD1306_send_cmd_list(cmds, count_of(cmds));
}
void render(uint8_t *buf, struct render_area *area) {
// update a portion of the display with a render area
uint8_t cmds[] = {
SSD1306_SET_COL_ADDR,
area->start_col,
area->end_col,
SSD1306_SET_PAGE_ADDR,
area->start_page,
area->end_page
};
SSD1306_send_cmd_list(cmds, count_of(cmds));
SSD1306_send_buf(buf, area->buflen);
}
static void SetPixel(uint8_t *buf, int x,int y, bool on) {
assert(x >= 0 && x < SSD1306_WIDTH && y >=0 && y < SSD1306_HEIGHT);
// The calculation to determine the correct bit to set depends on which address
// mode we are in. This code assumes horizontal
// The video ram on the SSD1306 is split up in to 8 rows, one bit per pixel.
// Each row is 128 long by 8 pixels high, each byte vertically arranged, so byte 0 is x=0, y=0->7,
// byte 1 is x = 1, y=0->7 etc
// This code could be optimised, but is like this for clarity. The compiler
// should do a half decent job optimising it anyway.
const int BytesPerRow = SSD1306_WIDTH ; // x pixels, 1bpp, but each row is 8 pixel high, so (x / 8) * 8
int byte_idx = (y / 8) * BytesPerRow + x;
uint8_t byte = buf[byte_idx];
if (on)
byte |= 1 << (y % 8);
else
byte &= ~(1 << (y % 8));
buf[byte_idx] = byte;
}
// Basic Bresenhams.
static void DrawLine(uint8_t *buf, int x0, int y0, int x1, int y1, bool on) {
int dx = abs(x1-x0);
int sx = x0<x1 ? 1 : -1;
int dy = -abs(y1-y0);
int sy = y0<y1 ? 1 : -1;
int err = dx+dy;
int e2;
while (true) {
SetPixel(buf, x0, y0, on);
if (x0 == x1 && y0 == y1)
break;
e2 = 2*err;
if (e2 >= dy) {
err += dy;
x0 += sx;
}
if (e2 <= dx) {
err += dx;
y0 += sy;
}
}
}
static inline int GetFontIndex(uint8_t ch) {
if (ch >= 'A' && ch <='Z') {
return ch - 'A' + 1;
}
else if (ch >= '0' && ch <='9') {
return ch - '0' + 27;
}
else return 0; // Not got that char so space.
}
static uint8_t reversed[sizeof(font)] = {0};
static uint8_t reverse(uint8_t b) {
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b;
}
static void FillReversedCache() {
// calculate and cache a reversed version of fhe font, because I defined it upside down...doh!
for (int i=0;i<sizeof(font);i++)
reversed[i] = reverse(font[i]);
}
static void WriteChar(uint8_t *buf, int16_t x, int16_t y, uint8_t ch) {
if (reversed[0] == 0)
FillReversedCache();
if (x > SSD1306_WIDTH - 8 || y > SSD1306_HEIGHT - 8)
return;
// For the moment, only write on Y row boundaries (every 8 vertical pixels)
y = y/8;
ch = toupper(ch);
int idx = GetFontIndex(ch);
int fb_idx = y * 128 + x;
for (int i=0;i<8;i++) {
buf[fb_idx++] = reversed[idx * 8 + i];
}
}
static void WriteString(uint8_t *buf, int16_t x, int16_t y, char *str) {
// Cull out any string off the screen
if (x > SSD1306_WIDTH - 8 || y > SSD1306_HEIGHT - 8)
return;
while (*str) {
WriteChar(buf, x, y, *str++);
x+=8;
}
}
#endif
int main() {
stdio_init_all();
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
#warning i2c / SSD1306_i2d example requires a board with I2C pins
puts("Default I2C pins were not defined");
#else
// useful information for picotool
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C));
bi_decl(bi_program_description("SSD1306 OLED driver I2C example for the Raspberry Pi Pico"));
printf("Hello, SSD1306 OLED display! Look at my raspberries..\n");
// I2C is "open drain", pull ups to keep signal high when no data is being
// sent
i2c_init(i2c_default, SSD1306_I2C_CLK * 1000);
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
// run through the complete initialization process
SSD1306_init();
// Initialize render area for entire frame (SSD1306_WIDTH pixels by SSD1306_NUM_PAGES pages)
struct render_area frame_area = {
start_col: 0,
end_col : SSD1306_WIDTH - 1,
start_page : 0,
end_page : SSD1306_NUM_PAGES - 1
};
calc_render_area_buflen(&frame_area);
// zero the entire display
uint8_t buf[SSD1306_BUF_LEN];
memset(buf, 0, SSD1306_BUF_LEN);
render(buf, &frame_area);
// intro sequence: flash the screen 3 times
for (int i = 0; i < 3; i++) {
SSD1306_send_cmd(SSD1306_SET_ALL_ON); // Set all pixels on
sleep_ms(500);
SSD1306_send_cmd(SSD1306_SET_ENTIRE_ON); // go back to following RAM for pixel state
sleep_ms(500);
}
// render 3 cute little raspberries
struct render_area area = {
start_page : 0,
end_page : (IMG_HEIGHT / SSD1306_PAGE_HEIGHT) - 1
};
restart:
area.start_col = 0;
area.end_col = IMG_WIDTH - 1;
calc_render_area_buflen(&area);
uint8_t offset = 5 + IMG_WIDTH; // 5px padding
for (int i = 0; i < 3; i++) {
render(raspberry26x32, &area);
area.start_col += offset;
area.end_col += offset;
}
SSD1306_scroll(true);
sleep_ms(5000);
SSD1306_scroll(false);
char *text[] = {
"A long time ago",
" on an OLED ",
" display",
" far far away",
"Lived a small",
"red raspberry",
"by the name of",
" PICO"
};
int y = 0;
for (int i = 0 ;i < count_of(text); i++) {
WriteString(buf, 5, y, text[i]);
y+=8;
}
render(buf, &frame_area);
// Test the display invert function
sleep_ms(3000);
SSD1306_send_cmd(SSD1306_SET_INV_DISP);
sleep_ms(3000);
SSD1306_send_cmd(SSD1306_SET_NORM_DISP);
bool pix = true;
for (int i = 0; i < 2;i++) {
for (int x = 0;x < SSD1306_WIDTH;x++) {
DrawLine(buf, x, 0, SSD1306_WIDTH - 1 - x, SSD1306_HEIGHT - 1, pix);
render(buf, &frame_area);
}
for (int y = SSD1306_HEIGHT-1; y >= 0 ;y--) {
DrawLine(buf, 0, y, SSD1306_WIDTH - 1, SSD1306_HEIGHT - 1 - y, pix);
render(buf, &frame_area);
}
pix = false;
}
goto restart;
#endif
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 KiB