SSD1351 RGB Color OLED Display driver for libDaisy Nim wrapper
The SSD1351 is a 128x128 full-color OLED controller with 65K colors (RGB565 format). Each pixel can display any of 65,536 colors using 16-bit color depth.
Features:
- 128x128 resolution
- 65K colors (RGB565: 5-bit red, 6-bit green, 5-bit blue)
- SPI transport only (no I2C variant)
- Large framebuffer: 2 bytes per pixel (32,768 bytes total)
- Separate foreground and background colors
- Compatible drawing API with other OLED drivers
Hardware Notes:
- Premium OLED modules (higher cost than monochrome)
- RGB565 format: RRRRRGGGGGGBBBBBbit packed into uint16
- Default colors: white foreground, black background
Example - Basic usage:
import nimphea import nimphea/dev/oled_ssd1351 var display = initSSD1351Spi(128, 128) display.fill(false) # Clear to black display.setColorRGB(31, 0, 0) # Red (max R, no G/B) display.drawCircle(64, 64, 30, true) display.setColorRGB(0, 63, 0) # Green (max G) display.drawRect(30, 30, 68, 68, true) display.update()
Example - Using color constants:
display.setColor(COLOR_RED) display.drawLine(0, 0, 127, 127, true) display.setColor(COLOR_BLUE) display.drawLine(0, 127, 127, 0, true) display.update()
Types
SSD1351Spi128x128 {.importcpp: "SSD1351Spi128x128".} = object
- Source Edit
SSD1351SpiConfig {.importcpp: "SSD1351Spi128x128::Config", bycopy.} = object transport_config* {.importc: "transport_config".}: SSD13514WireSpiTransportConfig
- Source Edit
SSD1351SpiPinConfig {.importcpp: "daisy::SSD13514WireSpiTransport::Config::pin_config", bycopy.} = object dc* {.importc: "dc".}: Pin reset* {.importc: "reset".}: Pin
- Source Edit
SSD13514WireSpiTransportConfig {.importcpp: "daisy::SSD13514WireSpiTransport::Config", bycopy.} = object spi_config* {.importc: "spi_config".}: SpiConfig pin_config* {.importc: "pin_config".}: SSD1351SpiPinConfig
- Source Edit
Consts
COLOR_BLACK = 0x0000'u16
- Black (R=0, G=0, B=0) Source Edit
COLOR_BLUE = 0x001F'u16
- Pure blue (R=0, G=0, B=31) Source Edit
COLOR_CYAN = 0x07FF'u16
- Cyan (green + blue) Source Edit
COLOR_GRAY = 0x7BEF'u16
- Medium gray (R=15, G=31, B=15) Source Edit
COLOR_GREEN = 0x07E0'u16
- Pure green (R=0, G=63, B=0) Source Edit
COLOR_MAGENTA = 0xF81F'u16
- Magenta (red + blue) Source Edit
COLOR_ORANGE = 0xFC00'u16
- Orange (R=31, G=32, B=0) Source Edit
COLOR_PURPLE = 0x8010'u16
- Purple (R=16, G=0, B=16) Source Edit
COLOR_WHITE = 0xFFFF'u16
- White (R=31, G=63, B=31) Source Edit
COLOR_YELLOW = 0xFFE0'u16
- Yellow (red + green) Source Edit
Procs
proc Defaults(config: var SSD13514WireSpiTransportConfig) {. importcpp: "#.Defaults()", header: "daisy_seed.h", ...raises: [], tags: [], forbids: [].}
- Source Edit
proc drawCircle(display: var SSD1351Spi128x128; x0, y0, radius: int; on: bool = true) {....raises: [], tags: [], forbids: [].}
-
Draw a circle outline using midpoint circle algorithm
Parameters:
- x0, y0 - Center point
- radius - Circle radius in pixels
- on - true for foreground color, false for background color
proc drawLine(display: var SSD1351Spi128x128; x0, y0, x1, y1: int; on: bool = true) {....raises: [], tags: [], forbids: [].}
-
Draw a line using Bresenham's algorithm
Parameters:
- x0, y0 - Start point
- x1, y1 - End point
- on - true for foreground color, false for background color
proc drawPixel(display: var SSD1351Spi128x128; x, y: int; on: bool = true) {. ...raises: [], tags: [], forbids: [].}
-
Draw a single pixel with current foreground/background color
Parameters:
- x, y - Pixel coordinates (0-based)
- on - true for foreground color, false for background color
Note: Use setColorRGB() or setColor() to change colors before calling this.
Source Edit proc drawRect(display: var SSD1351Spi128x128; x, y, w, h: int; on: bool = true) {. ...raises: [], tags: [], forbids: [].}
-
Draw a rectangle outline
Parameters:
- x, y - Top-left corner
- w, h - Width and height
- on - true for foreground color, false for background color
proc fill(display: var SSD1351Spi128x128; on: bool = true) {....raises: [], tags: [], forbids: [].}
-
Fill entire display with a color
Parameters:
- on - true for foreground color, false for background color
proc fillRect(display: var SSD1351Spi128x128; x, y, w, h: int; on: bool = true) {. ...raises: [], tags: [], forbids: [].}
-
Draw a filled rectangle
Parameters:
- x, y - Top-left corner
- w, h - Width and height
- on - true for foreground color, false for background color
proc height(display: SSD1351Spi128x128): int {.inline, ...raises: [], tags: [], forbids: [].}
- Get display height in pixels (always 128) Source Edit
proc rgb565(red, green, blue: uint8): uint16 {.inline, ...raises: [], tags: [], forbids: [].}
-
Convert RGB values to RGB565 format
Parameters:
- red - Red component (0-31, 5-bit)
- green - Green component (0-63, 6-bit)
- blue - Blue component (0-31, 5-bit)
Returns: RGB565 packed uint16
Example:
let purple = rgb565(16, 0, 16) # Medium purple display.setColor(purple)
Source Edit proc setBackground(display: var SSD1351Spi128x128; color: uint16) {....raises: [], tags: [], forbids: [].}
- Set background color using RGB565 value Source Edit
proc setBackgroundRGB(display: var SSD1351Spi128x128; red, green, blue: uint8) {. ...raises: [], tags: [], forbids: [].}
-
Set the current background color using RGB values
Parameters:
- red - Red component (0-31, 5-bit)
- green - Green component (0-63, 6-bit)
- blue - Blue component (0-31, 5-bit)
proc setColor(display: var SSD1351Spi128x128; color: uint16) {....raises: [], tags: [], forbids: [].}
-
Set foreground color using RGB565 value
Parameters:
- color - RGB565 color (use COLOR_* constants)
Example:
display.setColor(COLOR_RED) display.setColor(COLOR_CYAN) display.setColor(0xF800) # Raw RGB565 value
Source Edit proc setColorRGB(display: var SSD1351Spi128x128; red, green, blue: uint8) {. ...raises: [], tags: [], forbids: [].}
-
Set the current foreground color using RGB values
Parameters:
- red - Red component (0-31, 5-bit)
- green - Green component (0-63, 6-bit)
- blue - Blue component (0-31, 5-bit)
Note: RGB565 format has 6 bits for green (human eye sensitivity)
Example:
display.setColorRGB(31, 0, 0) # Pure red display.setColorRGB(0, 63, 0) # Pure green (note: 63, not 31) display.setColorRGB(0, 0, 31) # Pure blue display.setColorRGB(31, 63, 31) # White display.setColorRGB(15, 31, 15) # Medium gray
Source Edit proc update(display: var SSD1351Spi128x128) {....raises: [], tags: [], forbids: [].}
-
Send framebuffer to display hardware Call this after drawing to make changes visible
Note: Transfers 32KB over SPI, takes ~50ms at default speed
Source Edit proc width(display: SSD1351Spi128x128): int {.inline, ...raises: [], tags: [], forbids: [].}
- Get display width in pixels (always 128) Source Edit
Templates
template initSSD1351Spi(width, height: static[int]; dcPin: Pin = newPin(PORTB, 4); resetPin: Pin = newPin(PORTB, 15); sclkPin: Pin = newPin(PORTG, 11); mosiPin: Pin = newPin(PORTB, 5); nssPin: Pin = newPin(PORTG, 10)): untyped
-
Initialize SSD1351 color OLED via SPI
Parameters:
- width, height - Display dimensions (only 128x128 supported)
- dcPin - Data/Command pin (default: PB4)
- resetPin - Reset pin (default: PB15)
- sclkPin - SPI clock pin (default: PG11)
- mosiPin - SPI MOSI pin (default: PB5)
- nssPin - SPI chip select pin (default: PG10)
Returns: Initialized display instance with white FG, black BG
Example:
var oled = initSSD1351Spi(128, 128) oled.setColorRGB(31, 0, 0) # Red oled.fill(false) oled.update()
Source Edit