nimphea/dev/oled_ssd1327

Source   Edit  

SSD1327 Grayscale OLED Display driver for libDaisy Nim wrapper

The SSD1327 is a 128x128 grayscale OLED controller with 16 gray levels (4-bit per pixel). Unlike monochrome displays, each pixel can display shades from black (0x0) to white (0xF).

Features:

  • 128x128 resolution
  • 16 grayscale levels (4-bit per pixel)
  • SPI transport only (no I2C variant)
  • Compact framebuffer: 2 pixels per byte (8192 bytes total)
  • Compatible drawing API with other OLED drivers

Hardware Notes:

  • Common in higher-end OLED modules
  • Buffer format: packed 4-bit pixels (upper nibble = even pixel, lower = odd pixel)
  • Default grayscale level: 0xF (white)

Example - Basic usage:

import nimphea
import nimphea/dev/oled_ssd1327

var display = initSSD1327Spi(128, 128)
display.fill(false)
display.setGrayscale(0x8)  # Medium gray
display.drawCircle(64, 64, 30, true)
display.setGrayscale(0xF)  # White
display.drawPixel(64, 64, true)
display.update()

Example - Grayscale gradient:

# Draw gradient from black to white
for x in 0..<128:
  let gray = uint8(x div 8)  # 0-15
  display.setGrayscale(gray)
  display.drawLine(x, 0, x, 127, true)
display.update()

Types

SSD1327Spi128x128 {.importcpp: "SSD1327Spi128x128".} = object
Source   Edit  
SSD1327SpiConfig {.importcpp: "SSD1327Spi128x128::Config", bycopy.} = object
  transport_config* {.importc: "transport_config".}: SSD13274WireSpiTransportConfig
Source   Edit  
SSD1327SpiPinConfig {.importcpp: "daisy::SSD13274WireSpiTransport::Config::pin_config",
                      bycopy.} = object
  dc* {.importc: "dc".}: Pin
  reset* {.importc: "reset".}: Pin
Source   Edit  
SSD13274WireSpiTransportConfig {.importcpp: "daisy::SSD13274WireSpiTransport::Config",
                                 bycopy.} = object
  spi_config* {.importc: "spi_config".}: SpiConfig
  pin_config* {.importc: "pin_config".}: SSD1327SpiPinConfig
Source   Edit  

Consts

GRAYSCALE_BLACK = 0x00'u8
Black (minimum brightness) Source   Edit  
GRAYSCALE_DARK = 0x04'u8
Dark gray (25% brightness) Source   Edit  
GRAYSCALE_LIGHT = 0x0C'u8
Light gray (75% brightness) Source   Edit  
GRAYSCALE_MEDIUM = 0x08'u8
Medium gray (50% brightness) Source   Edit  
GRAYSCALE_WHITE = 0x0F'u8
White (maximum brightness) Source   Edit  

Procs

proc Defaults(config: var SSD13274WireSpiTransportConfig) {.
    importcpp: "#.Defaults()", header: "daisy_seed.h", ...raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc drawCircle(display: var SSD1327Spi128x128; 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 to draw with current grayscale, false for black
Source   Edit  
proc drawLine(display: var SSD1327Spi128x128; 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 to draw with current grayscale, false for black
Source   Edit  
proc drawPixel(display: var SSD1327Spi128x128; x, y: int; on: bool = true) {.
    ...raises: [], tags: [], forbids: [].}

Draw a single pixel with current grayscale level

Parameters:

  • x, y - Pixel coordinates (0-based)
  • on - true to draw with current grayscale, false for black

Note: Use setGrayscale() to change the drawing color before calling this.

Source   Edit  
proc drawRect(display: var SSD1327Spi128x128; 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 to draw with current grayscale, false for black
Source   Edit  
proc fill(display: var SSD1327Spi128x128; on: bool = true) {....raises: [],
    tags: [], forbids: [].}

Fill entire display with a color

Parameters:

  • on - true for white (0xF), false for black (0x0)
Source   Edit  
proc fillRect(display: var SSD1327Spi128x128; 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 to draw with current grayscale, false for black
Source   Edit  
proc height(display: SSD1327Spi128x128): int {.inline, ...raises: [], tags: [],
    forbids: [].}
Get display height in pixels (always 128) Source   Edit  
proc setGrayscale(display: var SSD1327Spi128x128; level: uint8) {....raises: [],
    tags: [], forbids: [].}

Set the current grayscale drawing level

Parameters:

  • level - Grayscale value 0x0 (black) to 0xF (white)

Example:

display.setGrayscale(0x0)  # Black
display.drawCircle(32, 32, 10, true)
display.setGrayscale(0x8)  # Medium gray
display.drawCircle(64, 64, 10, true)
display.setGrayscale(0xF)  # White
display.drawCircle(96, 96, 10, true)

Source   Edit  
proc update(display: var SSD1327Spi128x128) {....raises: [], tags: [], forbids: [].}
Send framebuffer to display hardware Call this after drawing to make changes visible Source   Edit  
proc width(display: SSD1327Spi128x128): int {.inline, ...raises: [], tags: [],
    forbids: [].}
Get display width in pixels (always 128) Source   Edit  

Templates

template initSSD1327Spi(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 SSD1327 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

Example:

var oled = initSSD1327Spi(128, 128)
oled.setGrayscale(0xF)  # White
oled.fill(false)
oled.update()

Source   Edit