Skip to content

Commit

Permalink
Merge pull request #373 from pimoroni/experimental/8bitfb
Browse files Browse the repository at this point in the history
ST7789 / PicoGraphics rewrite - Support for 4-bit, 8-bit and 16-bit framebuffers and more.
  • Loading branch information
Gadgetoid authored Jun 17, 2022
2 parents 438002f + 27f1ecb commit f101ffd
Show file tree
Hide file tree
Showing 249 changed files with 10,799 additions and 7,064 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/micropython-badger2040.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
types: [created]

env:
MICROPYTHON_VERSION: v1.18
MICROPYTHON_VERSION: v1.19
BOARD_TYPE: PICO
# MicroPython version will be contained in github.event.release.tag_name for releases
RELEASE_FILE: pimoroni-badger2040-${{github.event.release.tag_name || github.sha}}-micropython.uf2
Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/micropython.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
types: [created]

env:
MICROPYTHON_VERSION: v1.18
MICROPYTHON_VERSION: v1.19

jobs:
deps:
Expand Down Expand Up @@ -66,6 +66,8 @@ jobs:
board: PIMORONI_PICOLIPO_4MB
- name: picolipo_16mb
board: PIMORONI_PICOLIPO_16MB
- name: tufty2040
board: PIMORONI_TUFTY2040

env:
# MicroPython version will be contained in github.event.release.tag_name for releases
Expand Down Expand Up @@ -107,6 +109,12 @@ jobs:
with:
submodules: true
path: pimoroni-pico-${{ github.sha }}

- name: "HACK: MicroPython Board Fixups"
shell: bash
working-directory: micropython/ports/rp2
run: |
../../../pimoroni-pico-${GITHUB_SHA}/micropython/_board/board-fixup.sh ${{matrix.name}} ${{matrix.board}} ../../../pimoroni-pico-${GITHUB_SHA}/micropython/_board
- name: Configure MicroPython
shell: bash
Expand Down
1 change: 1 addition & 0 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
include(pimoroni_i2c.cmake)
include(pimoroni_bus.cmake)
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
set(LIB_NAME breakout_roundlcd)
set(LIB_NAME pimoroni_bus)
add_library(${LIB_NAME} INTERFACE)

target_sources(${LIB_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}/${LIB_NAME}.cpp
${CMAKE_CURRENT_LIST_DIR}/${LIB_NAME}.cpp
)

target_include_directories(${LIB_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR})

# Pull in pico libraries that we need
target_link_libraries(${LIB_NAME} INTERFACE pico_stdlib st7789 pico_graphics)
target_link_libraries(${LIB_NAME} INTERFACE pico_stdlib hardware_spi)
15 changes: 15 additions & 0 deletions common/pimoroni_bus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "pimoroni_bus.hpp"

namespace pimoroni {
SPIPins get_spi_pins(BG_SPI_SLOT slot) {
switch(slot) {
case PICO_EXPLORER_ONBOARD:
return {PIMORONI_SPI_DEFAULT_INSTANCE, SPI_BG_FRONT_CS, SPI_DEFAULT_SCK, SPI_DEFAULT_MOSI, PIN_UNUSED, SPI_DEFAULT_DC, PIN_UNUSED};
case BG_SPI_FRONT:
return {PIMORONI_SPI_DEFAULT_INSTANCE, SPI_BG_FRONT_CS, SPI_DEFAULT_SCK, SPI_DEFAULT_MOSI, PIN_UNUSED, SPI_DEFAULT_DC, SPI_BG_FRONT_PWM};
case BG_SPI_BACK:
return {PIMORONI_SPI_DEFAULT_INSTANCE, SPI_BG_BACK_CS, SPI_DEFAULT_SCK, SPI_DEFAULT_MOSI, PIN_UNUSED, SPI_DEFAULT_DC, SPI_BG_BACK_PWM};
}
return {PIMORONI_SPI_DEFAULT_INSTANCE, SPI_BG_FRONT_CS, SPI_DEFAULT_SCK, SPI_DEFAULT_MOSI, PIN_UNUSED, SPI_DEFAULT_DC, SPI_BG_FRONT_PWM};
};
}
27 changes: 27 additions & 0 deletions common/pimoroni_bus.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include "pimoroni_common.hpp"
#include "hardware/gpio.h"
#include "hardware/spi.h"

namespace pimoroni {
struct SPIPins {
spi_inst_t *spi;
uint cs;
uint sck;
uint mosi;
uint miso;
uint dc;
uint bl;
};

struct ParallelPins {
uint cs;
uint dc;
uint wr_sck;
uint rd_sck;
uint d0;
uint bl;
};

SPIPins get_spi_pins(BG_SPI_SLOT slot);
}
12 changes: 10 additions & 2 deletions common/pimoroni_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace pimoroni {
// SPI
static const unsigned int SPI_DEFAULT_MOSI = 19;
static const unsigned int SPI_DEFAULT_MISO = 16;
static const unsigned int SPI_DEFAULT_DC = 16;
static const unsigned int SPI_DEFAULT_SCK = 18;

static const unsigned int SPI_BG_FRONT_PWM = 20;
Expand All @@ -53,6 +54,13 @@ namespace pimoroni {
INTERSTATE_75,
SERVO_2040
};

enum Rotation {
ROTATE_0 = 0,
ROTATE_90 = 90,
ROTATE_180 = 180,
ROTATE_270 = 270
};

enum Polarity {
ACTIVE_LOW = 0,
Expand Down Expand Up @@ -100,8 +108,8 @@ namespace pimoroni {
uint8_t enable;
};

pin_pair() : first(0), second(0) {}
pin_pair(uint8_t first, uint8_t second) : first(first), second(second) {}
constexpr pin_pair() : first(0), second(0) {}
constexpr pin_pair(uint8_t first, uint8_t second) : first(first), second(second) {}
};

struct bool_pair {
Expand Down
76 changes: 39 additions & 37 deletions drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
add_subdirectory(analog)
add_subdirectory(analogmux)
add_subdirectory(esp32spi)
add_subdirectory(ioexpander)
add_subdirectory(ltp305)
add_subdirectory(ltr559)
add_subdirectory(pmw3901)
add_subdirectory(sgp30)
add_subdirectory(st7735)
add_subdirectory(st7789)
add_subdirectory(msa301)
add_subdirectory(rv3028)
add_subdirectory(trackball)
add_subdirectory(vl53l1x)
add_subdirectory(is31fl3731)
add_subdirectory(fatfs)
add_subdirectory(sdcard)
add_subdirectory(as7262)
add_subdirectory(bh1745)
add_subdirectory(bme68x)
add_subdirectory(bmp280)
add_subdirectory(bme280)
add_subdirectory(button)
add_subdirectory(pid)
add_subdirectory(plasma)
add_subdirectory(rgbled)
add_subdirectory(icp10125)
add_subdirectory(scd4x)
add_subdirectory(hub75)
add_subdirectory(uc8151)
add_subdirectory(pwm)
add_subdirectory(servo)
add_subdirectory(encoder)
add_subdirectory(motor)
add_subdirectory(vl53l5cx)
add_subdirectory(pcf85063a)
add_subdirectory(pms5003)
add_subdirectory(analog)
add_subdirectory(analogmux)
add_subdirectory(esp32spi)
add_subdirectory(ioexpander)
add_subdirectory(ltp305)
add_subdirectory(ltr559)
add_subdirectory(pmw3901)
add_subdirectory(sgp30)
add_subdirectory(st7735)
add_subdirectory(st7789)
add_subdirectory(msa301)
add_subdirectory(rv3028)
add_subdirectory(trackball)
add_subdirectory(vl53l1x)
add_subdirectory(is31fl3731)
add_subdirectory(fatfs)
add_subdirectory(sdcard)
add_subdirectory(as7262)
add_subdirectory(bh1745)
add_subdirectory(bme68x)
add_subdirectory(bmp280)
add_subdirectory(bme280)
add_subdirectory(button)
add_subdirectory(pid)
add_subdirectory(plasma)
add_subdirectory(rgbled)
add_subdirectory(icp10125)
add_subdirectory(scd4x)
add_subdirectory(hub75)
add_subdirectory(uc8151)
add_subdirectory(pwm)
add_subdirectory(servo)
add_subdirectory(encoder)
add_subdirectory(motor)
add_subdirectory(vl53l5cx)
add_subdirectory(pcf85063a)
add_subdirectory(pms5003)
add_subdirectory(sh1107)

1 change: 1 addition & 0 deletions drivers/sh1107/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include(${CMAKE_CURRENT_LIST_DIR}/sh1107.cmake)
58 changes: 58 additions & 0 deletions drivers/sh1107/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# ST7789 Display Driver for Pimoroni LCDs <!-- omit in toc -->

The ST7789 driver supports both Parallel and Serial (SPI) ST7789 displays and is intended for use with:

* Pico Display
* Pico Display 2.0
* Tufty 2040
* Pico Explorer
* 240x240 Round & Square SPI LCD Breakouts

## Setup

Construct an instance of the ST7789 driver with either Parallel or SPI pins.

Parallel:

```c++
ST7789 st7789(WIDTH, HEIGHT, ROTATE_0, {
Tufty2040::LCD_CS, // Chip-Select
Tufty2040::LCD_DC, // Data-Command
Tufty2040::LCD_WR, // Write
Tufty2040::LCD_RD, // Read
Tufty2040::LCD_D0, // Data 0 (start of a bank of 8 pins)
Tufty2040::BACKLIGHT // Backlight
});
```
SPI:
```c++
ST7789 st7789(WIDTH, HEIGHT, ROTATE_0, false, {
PIMORONI_SPI_DEFAULT_INSTANCE, // SPI instance
SPI_BG_FRONT_CS, // Chip-select
SPI_DEFAULT_SCK, // SPI Clock
SPI_DEFAULT_MOSI, // SPI Out
PIN_UNUSED, // SPI In
SPI_DEFAULT_DC, // SPI Data/Command
PIN_UNUSED // Backlight
});
```

## Reference

### Update

ST7789's `update` accepts an instance of `PicoGraphics` in any colour mode:

```c++
st7789.update(&graphics);
```

### Set Backlight

If a backlight pin has been configured, you can set the backlight from 0 to 255:

```c++
st7789.set_backlight(128)
```
12 changes: 12 additions & 0 deletions drivers/sh1107/sh1107.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set(DRIVER_NAME sh1107)
add_library(${DRIVER_NAME} INTERFACE)

target_sources(${DRIVER_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}/${DRIVER_NAME}.cpp)

target_include_directories(${DRIVER_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR})

target_include_directories(sh1107 INTERFACE ${CMAKE_CURRENT_LIST_DIR})

# Pull in pico libraries that we need
target_link_libraries(${DRIVER_NAME} INTERFACE pico_stdlib pimoroni_i2c pico_graphics)
49 changes: 49 additions & 0 deletions drivers/sh1107/sh1107.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "sh1107.hpp"

#include <cstdlib>
#include <math.h>
#include <string.h>

namespace pimoroni {

void SH1107::update(PicoGraphics *graphics) {
if(graphics->pen_type == PicoGraphics::PEN_1BIT) { // Display buffer is screen native

uint8_t *p = (uint8_t *)graphics->frame_buffer;
uint framebuffer_size = PicoGraphics_Pen1Bit::buffer_size(width, height);
uint page_size = framebuffer_size / 16;

uint8_t temp[framebuffer_size] = {0};
uint8_t *ptemp = temp;

for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
uint bo = 7 - (x & 0b111);
uint8_t color = p[(x / 8) + (y * width / 8)] & (1U << bo);
if(color) {
temp[x + (y / 8 ) * width] |= 1 << (y % 8);
}else{
temp[x + (y / 8 ) * width] &= ~(1 << (y % 8));
}
}
}

uint8_t buf[page_size + 1];

for(int i = 0; i < 16; i++) {
i2c.reg_write_uint8(0x3c, 0, 0xb0 + i);
i2c.reg_write_uint8(0x3c, 0, 0x00);
i2c.reg_write_uint8(0x3c, 0, 0x10);

memcpy(buf + 1, ptemp, page_size);
buf[0] = 0x40;

i2c.write_blocking(0x3c, buf, page_size + 1, false);

ptemp += page_size;
}

}
}

}
Loading

0 comments on commit f101ffd

Please sign in to comment.