Skip to content

Commit

Permalink
Merge pull request #12 from pimoroni/micropython-rebase
Browse files Browse the repository at this point in the history
PicoSystem SDK port for MicroPython.
  • Loading branch information
Gadgetoid authored Oct 18, 2021
2 parents da9ce81 + 2cb78c7 commit b816b20
Show file tree
Hide file tree
Showing 30 changed files with 2,478 additions and 22 deletions.
97 changes: 97 additions & 0 deletions .github/workflows/micropython.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
name: MicroPython

on:
push:
pull_request:
release:
types: [created]

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
MICROPYTHON_VERSION: v1.17
BUILD_TYPE: Release
BOARD_TYPE: PIMORONI_PICOSYSTEM

jobs:
build:
name: ${{matrix.name}}
strategy:
matrix:
include:
- os: ubuntu-20.04
name: Linux
cache-key: linux
apt-packages: clang-tidy gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib

runs-on: ${{matrix.os}}

steps:
# Check out MicroPython
- name: Checkout MicroPython
uses: actions/checkout@v2
with:
repository: pimoroni/micropython
ref: board/rp2/pimoroni_picosystem #${{env.MICROPYTHON_VERSION}}
submodules: false # MicroPython submodules are hideously broken
path: micropython

- uses: actions/checkout@v2
with:
submodules: true
path: picosystem-${{ github.sha }}

# HACK: Copy Python module files
- name: Copy modules
run: |
cp -r picosystem-${GITHUB_SHA}/micropython/modules_py/* micropython/ports/rp2/modules/
cp -r picosystem-${GITHUB_SHA}/micropython/examples/picosystem/colour.py micropython/ports/rp2/modules/
cp -r picosystem-${GITHUB_SHA}/micropython/examples/picosystem/shapes.py micropython/ports/rp2/modules/
cp -r picosystem-${GITHUB_SHA}/micropython/examples/picosystem/launcher.py micropython/ports/rp2/modules/
# Linux deps
- name: Install deps
if: runner.os == 'Linux'
run: |
sudo apt update && sudo apt install ${{matrix.apt-packages}}
- name: Fetch base MicroPython submodules
shell: bash
working-directory: micropython
run: git submodule update --init

- name: Fetch Pico SDK submodules
shell: bash
working-directory: micropython/lib/pico-sdk
run: git submodule update --init

- name: Build mpy-cross
shell: bash
working-directory: micropython/mpy-cross
run: make

- name: Build MicroPython
shell: bash
working-directory: micropython/ports/rp2
run: make USER_C_MODULES=../../../picosystem-${GITHUB_SHA}/micropython/modules/micropython.cmake BOARD=${BOARD_TYPE} -j2

- name: Rename .uf2 for artifact
shell: bash
working-directory: micropython/ports/rp2/build-${{env.BOARD_TYPE}}
run: cp firmware.uf2 ${{github.event.repository.name}}-${{github.event.release.tag_name}}-micropython-${{env.MICROPYTHON_VERSION}}.uf2

- name: Store .uf2 as artifact
uses: actions/upload-artifact@v2
with:
name: ${{github.event.repository.name}}-${{ github.sha }}-micropython-${{env.MICROPYTHON_VERSION}}.uf2
path: micropython/ports/rp2/build-${{env.BOARD_TYPE}}/${{github.event.repository.name}}-${{github.event.release.tag_name}}-micropython-${{env.MICROPYTHON_VERSION}}.uf2

- name: Upload .uf2
if: github.event_name == 'release'
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
with:
asset_path: micropython/ports/rp2/build-${{env.BOARD_TYPE}}/firmware.uf2
upload_url: ${{github.event.release.upload_url}}
asset_name: ${{github.event.repository.name}}-${{github.event.release.tag_name}}-micropython-${{env.MICROPYTHON_VERSION}}.uf2
asset_content_type: application/octet-stream
36 changes: 36 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Python

on:
push:
pull_request:

jobs:
build:
name: ${{matrix.name}}
strategy:
matrix:
include:
- os: ubuntu-20.04
name: Linux
cache-key: linux
apt-packages: python3 python3-pip
python-packages: flake8

runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v2

# Linux deps
- name: Install deps
if: runner.os == 'Linux'
run: |
sudo apt update && sudo apt install ${{matrix.apt-packages}}
- name: Install Python Deps
run: python3 -m pip install ${{matrix.python-packages}}

- name: Lint
shell: bash
run: |
python3 -m flake8 --ignore E501,F405,F821,F403 micropython/examples
python3 -m flake8 --ignore E501,F405,F821,F403,F401 micropython/modules_py
1 change: 1 addition & 0 deletions assets/gadgetoid-raycaster.16bpp

Large diffs are not rendered by default.

Binary file added assets/s4m_ur4i-dingbads.16bpp
Binary file not shown.
Binary file added assets/s4m_ur4i-pirate-characters.16bpp
Binary file not shown.
Binary file added assets/s4m_ur4i-pirate-tilemap.16bpp
Binary file not shown.
Binary file added assets/s4m_ur4i-platformer.16bpp
Binary file not shown.
Binary file added assets/s4m_ur4i-space-shooter-backdrop.16bpp
Binary file not shown.
Binary file added assets/s4m_ur4i-space-shooter-ships.16bpp
Binary file not shown.
Binary file added assets/s4m_ur4i-top-down-shooter.16bpp
Binary file not shown.
46 changes: 32 additions & 14 deletions examples/colour/colour.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using namespace picosystem;

voice_t blip;
buffer_t *colour_picker = buffer(68, 68);

// a place to store our current selected colour along with slider attributes
struct slider_t {
Expand All @@ -30,9 +31,38 @@ void colour_from_xy(int32_t x, int32_t y, uint32_t &r, uint32_t &g, uint32_t &b)
b = x % 16;
}

void prepare_rgb_palette() {
uint32_t r, g, b;

target(colour_picker);
blend(COPY);

// clear
pen(0, 0, 0);
clear();

// draw outline
pen(8, 8, 8);
rect(0, 0, 68, 68);

// draw the full palette grid of 64 x 64 pixels, this covers every single
// colour in the picosystem 4096 colour (4 bits per channel) palette.
for(int py = 0; py < 64; py++) {
for(int px = 0; px < 64; px++) {
colour_from_xy(px, py, r, g, b);
pen(r, g, b);
pixel(px + 2, py + 2);
}
}

blend(ALPHA);
target(SCREEN);
}

void init() {
// a brief chirp for when user input is processed
blip = voice(10, 0, 80, 0, 0, 0, 0, 100);
prepare_rgb_palette();
}

void update(uint32_t tick) {
Expand All @@ -51,21 +81,8 @@ void update(uint32_t tick) {

void draw_rgb_palette(int32_t x, int32_t y) {
int32_t cx, cy;
uint32_t r, g, b;

// draw outline
pen(8, 8, 8);
rect(x, y, 68, 68);

// draw the full palette grid of 64 x 64 pixels, this covers every single
// colour in the picosystem 4096 colour (4 bits per channel) palette.
for(int py = 0; py < 64; py++) {
for(int px = 0; px < 64; px++) {
colour_from_xy(px, py, r, g, b);
pen(r, g, b);
pixel(px + x + 2, py + y + 2);
}
}
blit(colour_picker, 0, 0, 69, 69, x, y);

// calculate a brightness for the cursor that pulses over time
uint8_t cursor_pulse = (sin(time() / 100.0f) + 1.0f) * 7.5f;
Expand All @@ -78,6 +95,7 @@ void draw_rgb_palette(int32_t x, int32_t y) {
vline(sx + x + 2, sy + 2 + y + 2, 3);
}


void draw_slider(slider_t &slider, int32_t x, int32_t y) {
static uint32_t w = 10, h = 68;

Expand Down
9 changes: 6 additions & 3 deletions libraries/hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "pico/stdlib.h"
#include "pico/time.h"


#ifdef PIXEL_DOUBLE
#include "screen_double.pio.h"
#else
Expand Down Expand Up @@ -284,6 +285,10 @@ namespace picosystem {
return true;
}

void _start_audio() {
add_repeating_timer_ms(-1, _audio_update_callback, NULL, &_audio_update_timer);
}

void _init_hardware() {
// configure backlight pwm and disable backlight while setting up
pwm_config cfg = pwm_get_default_config();
Expand Down Expand Up @@ -394,7 +399,7 @@ namespace picosystem {

// initialise dma channel for transmitting pixel data to screen
// via the screen updating pio program
dma_channel = dma_claim_unused_channel(true);
dma_channel = 0; //dma_claim_unused_channel(true); // avoid MicroPython soft-reset timebomb
dma_channel_config config = dma_channel_get_default_config(dma_channel);
channel_config_set_bswap(&config, true);
channel_config_set_dreq(&config, pio_get_dreq(screen_pio, screen_sm, true));
Expand All @@ -412,8 +417,6 @@ namespace picosystem {
pwm_init(audio_pwm_slice_number, &audio_pwm_cfg, true);
gpio_set_function(AUDIO, GPIO_FUNC_PWM);
pwm_set_gpio_level(AUDIO, 0);

add_repeating_timer_ms(-1, _audio_update_callback, NULL, &_audio_update_timer);
}

}
11 changes: 10 additions & 1 deletion libraries/picosystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "picosystem.hpp"


namespace picosystem {

uint32_t _debug;
Expand All @@ -17,6 +18,7 @@ namespace picosystem {
uint32_t _io = 0, _lio = 0;
blend_func_t _bf = ALPHA;

#ifndef DYNAMIC_BUFFER
#ifdef PIXEL_DOUBLE
color_t _fb[120 * 120];
buffer_t *SCREEN = buffer(120, 120, _fb);
Expand All @@ -26,6 +28,10 @@ namespace picosystem {
buffer_t *SCREEN = buffer(240, 240, _fb);
int32_t _cx = 0, _cy = 0, _cw = 240, _ch = 240;
#endif
#else
buffer_t *SCREEN = nullptr;
int32_t _cx = 0, _cy = 0, _cw = 120, _ch = 120;
#endif

buffer_t *_dt = SCREEN;

Expand All @@ -50,8 +56,10 @@ using namespace picosystem;
// called when they implement the init(), update(), and render()
// functions in their project

#ifndef MICROPY_BUILD_TYPE
int main() {
_init_hardware();
_start_audio();

// setup lut for fast sin/cos functions
for(uint32_t i = 0; i < 256; i++) {
Expand Down Expand Up @@ -132,4 +140,5 @@ int main() {
last_ms = ms;
}

}
}
#endif
1 change: 1 addition & 0 deletions libraries/picosystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ namespace picosystem {
// internal methods - do not call directly, will change!
void _logo();
void _init_hardware();
void _start_audio();
uint32_t _gpio_get();
float _battery_voltage();
void _reset_to_dfu();
Expand Down
4 changes: 2 additions & 2 deletions libraries/primitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ namespace picosystem {
// clip source coordinates
if(sx < 0) {dx += -sx; w += sx; sx = 0;}
if(sy < 0) {dy += -sy; h += sy; sy = 0;}
if(sx + w >= src->w) {w -= (sx + w) - src->w;}
if(sy + h >= src->h) {h -= (sy + h) - src->h;}
if(sx + w >= (int32_t)src->w) {w -= (sx + w) - src->w;}
if(sy + h >= (int32_t)src->h) {h -= (sy + h) - src->h;}

// clip destination coordinates
if(dx < _cx) {sx += (_cx - dx); w += (_cx - dx); dx = _cx;}
Expand Down
4 changes: 2 additions & 2 deletions libraries/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ namespace picosystem {

std::string str(int32_t v) {
static char b[32];
snprintf(b, 32, "%d", v);
snprintf(b, 32, "%ld", v);
return b;
}

std::string str(uint32_t v) {
static char b[32];
snprintf(b, 32, "%d", v);
snprintf(b, 32, "%lu", v);
return b;
}

Expand Down
19 changes: 19 additions & 0 deletions micropython/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# PicoSystem MicroPython

## Get the latest release

## Build from source

```
git clone https://github.com/pimoroni/micropython -b board/rp2/pimoroni_picosystem
cd micropython
git submodule update --init
cd lib/pico-sdk
git submodule update --init
cd ../../micropython/mpy_cross
make
cd ../ports/rp2
cp ../../../picosystem/micropython/modules_py/boot.py modules/
make USER_C_MODULES=../../../picosystem/micropython/modules/micropython.cmake BOARD=PIMORONI_PICOSYSTEM
cp build-PIMORONI_PICOSYSTEM/firmware.uf2 /path/to/RPI-RP2/
```
Loading

0 comments on commit b816b20

Please # to comment.