Skip to content

Commit

Permalink
Merge pull request #633 from pimoroni/feature/badger2040w
Browse files Browse the repository at this point in the history
Badger2040w support
  • Loading branch information
ZodiusInfuser authored Feb 16, 2023
2 parents 05391d2 + 5a62a79 commit 943ea13
Show file tree
Hide file tree
Showing 67 changed files with 4,631 additions and 18 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: 67fac4ebc53db6337008ba06df7932faec80f57c
MICROPYTHON_VERSION: 35524a6fda1e44692ad599a39c802c168c897de9
BOARD_TYPE: PIMORONI_BADGER2040
# 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
Expand Down
153 changes: 153 additions & 0 deletions .github/workflows/micropython-badger2040w.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
name: MicroPython for Badger2040W

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

env:
MICROPYTHON_VERSION: 67fac4ebc53db6337008ba06df7932faec80f57c
BOARD_TYPE: PIMORONI_BADGER2040W
# MicroPython version will be contained in github.event.release.tag_name for releases
RELEASE_FILE: pimoroni-badger2040w-${{github.event.release.tag_name || github.sha}}-micropython

jobs:
deps:
runs-on: ubuntu-20.04
name: Dependencies
steps:
- name: Workspace Cache
id: cache
uses: actions/cache@v3
with:
path: ${{runner.workspace}}
key: workspace-micropython-${{env.MICROPYTHON_VERSION}}
restore-keys: |
workspace-micropython-${{env.MICROPYTHON_VERSION}}
# Check out MicroPython
- name: Checkout MicroPython
if: steps.cache.outputs.cache-hit != 'true'
uses: actions/checkout@v3
with:
repository: micropython/micropython
ref: ${{env.MICROPYTHON_VERSION}}
submodules: false # MicroPython submodules are hideously broken
path: micropython

- name: Fetch base MicroPython submodules
if: steps.cache.outputs.cache-hit != 'true'
shell: bash
working-directory: micropython
run: git submodule update --init

- name: Fetch Pico SDK submodules
if: steps.cache.outputs.cache-hit != 'true'
shell: bash
working-directory: micropython/lib/pico-sdk
run: git submodule update --init

- name: Build mpy-cross
if: steps.cache.outputs.cache-hit != 'true'
shell: bash
working-directory: micropython/mpy-cross
run: make

build:
needs: deps
name: Build Badger 2040W
runs-on: ubuntu-20.04

steps:
- name: Compiler Cache
uses: actions/cache@v3
with:
path: /home/runner/.ccache
key: ccache-micropython-badger2040w-${{github.ref}}-${{github.sha}}
restore-keys: |
ccache-micropython-badger2040w-${{github.ref}}
ccache-micropython-badger2040w-
- name: Workspace Cache
uses: actions/cache@v3
with:
path: ${{runner.workspace}}
key: workspace-micropython-${{env.MICROPYTHON_VERSION}}
restore-keys: |
workspace-micropython-${{env.MICROPYTHON_VERSION}}
- uses: actions/checkout@v3
with:
submodules: true
path: pimoroni-pico-${{ github.sha }}

# Check out dir2u2f
- uses: actions/checkout@v3
with:
repository: gadgetoid/dir2uf2
ref: v0.0.1
path: dir2uf2

- name: "HACK: MicroPython Board Fixups"
shell: bash
working-directory: micropython/ports/rp2
run: |
../../../pimoroni-pico-${GITHUB_SHA}/micropython/_board/board-fixup.sh badger2040w ${{env.BOARD_TYPE}} ../../../pimoroni-pico-${GITHUB_SHA}/micropython/_board
# Linux deps
- name: Install Compiler & CCache
if: runner.os == 'Linux'
run: |
sudo apt update && sudo apt install ccache gcc-arm-none-eabi
python3 -m pip install pillow
# Build firmware
- name: Configure MicroPython
shell: bash
working-directory: micropython/ports/rp2
run: |
cmake -S . -B build-${{env.BOARD_TYPE}} -DPICO_BUILD_DOCS=0 -DUSER_C_MODULES=../../../pimoroni-pico-${GITHUB_SHA}/micropython/modules/micropython-badger2040w.cmake -DMICROPY_BOARD=${{env.BOARD_TYPE}} -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
- name: Build MicroPython
shell: bash
working-directory: micropython/ports/rp2
run: |
ccache --zero-stats || true
cmake --build build-${{env.BOARD_TYPE}} -j 2
ccache --show-stats || true
- name: Rename .uf2 for artifact
shell: bash
working-directory: micropython/ports/rp2/build-${{env.BOARD_TYPE}}
run: |
cp firmware.uf2 ${{env.RELEASE_FILE}}.uf2
- name: Append Filesystem
shell: bash
run: |
python3 -m pip install littlefs-python
./dir2uf2/dir2uf2 --append-to micropython/ports/rp2/build-${{env.BOARD_TYPE}}/${{env.RELEASE_FILE}}.uf2 --manifest pimoroni-pico-${{ github.sha }}/micropython/examples/badger2040w/uf2-manifest.txt --filename with-examples.uf2 pimoroni-pico-${{ github.sha }}/micropython/examples/badger2040w/
- name: Store .uf2 as artifact
uses: actions/upload-artifact@v3
with:
name: ${{env.RELEASE_FILE}}.uf2
path: micropython/ports/rp2/build-${{env.BOARD_TYPE}}/${{env.RELEASE_FILE}}.uf2

- name: Store .uf2 + examples as artifact
uses: actions/upload-artifact@v3
with:
name: ${{env.RELEASE_FILE}}-with-examples.uf2
path: ${{env.RELEASE_FILE}}-with-examples.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}}/${{env.RELEASE_FILE}}.uf2
upload_url: ${{github.event.release.upload_url}}
asset_name: ${{env.RELEASE_FILE}}.uf2
asset_content_type: application/octet-stream
16 changes: 8 additions & 8 deletions drivers/icp10125/icp10125.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ namespace pimoroni {
}

void ICP10125::reset() {
uint16_t command = __bswap16(SOFT_RESET);
uint16_t command = __builtin_bswap16(SOFT_RESET);
i2c->write_blocking(address, (uint8_t *)&command, 2, false);
sleep_ms(10); // Soft reset time is 170us but you can never be too sure...
}

ICP10125::reading ICP10125::measure(meas_command cmd) {
uint16_t command = __bswap16(cmd);
uint16_t command = __builtin_bswap16(cmd);
reading result = {0.0f, 0.0f, OK};
uint16_result results[3];

Expand Down Expand Up @@ -74,17 +74,17 @@ namespace pimoroni {
if(results[1].crc8 != crc8((uint8_t *)&results[1].data, 2)) {result.status = CRC_FAIL; return result;};
if(results[2].crc8 != crc8((uint8_t *)&results[2].data, 2)) {result.status = CRC_FAIL; return result;};

int temperature = __bswap16(results[0].data);
int temperature = __builtin_bswap16(results[0].data);
// Due to all the byte swapping nonsense I'm not sure if I've discarded the LLSB or LMSB here...
int pressure = ((int32_t)__bswap16(results[1].data) << 8) | (__bswap16(results[2].data >> 8)); // LLSB is discarded
int pressure = ((int32_t)__builtin_bswap16(results[1].data) << 8) | (__builtin_bswap16(results[2].data >> 8)); // LLSB is discarded

process_data(pressure, temperature, &result.pressure, &result.temperature);
return result;
}

int ICP10125::chip_id() {
uint16_result result;
uint16_t command = __bswap16(READ_ID);
uint16_t command = __builtin_bswap16(READ_ID);

i2c->write_blocking(address, (uint8_t *)&command, 2, true);
i2c->read_blocking(address, (uint8_t *)&result, 3, false);
Expand All @@ -93,12 +93,12 @@ namespace pimoroni {
return -1;
}

return __bswap16(result.data) & 0x3f;
return __builtin_bswap16(result.data) & 0x3f;
}

bool ICP10125::read_otp() {
uint16_result result[4];
uint16_t command = __bswap16(READ_OTP);
uint16_t command = __builtin_bswap16(READ_OTP);
uint8_t move_address_ptr[] = {
MOVE_ADDRESS_PTR >> 8, MOVE_ADDRESS_PTR & 0xff,
0x00,
Expand All @@ -114,7 +114,7 @@ namespace pimoroni {
if(result[x].crc8 != crc8((uint8_t *)&result[x].data, 2)) {
return false;
}
sensor_constants[x] = (float)__bswap16(result[x].data);
sensor_constants[x] = (float)__builtin_bswap16(result[x].data);
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion drivers/vl53l1x/vl53l1x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ namespace pimoroni {
i2c->read_blocking(address, (uint8_t *)&value, 2, false);

// TODO do we need to bswap this return value?
return __bswap16(value);
return __builtin_bswap16(value);
}

// Read a 32-bit register
Expand Down
48 changes: 45 additions & 3 deletions libraries/pico_graphics/pico_graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,15 @@ namespace pimoroni {
}

if (hershey_font) {
hershey::text(hershey_font, [this](int32_t x1, int32_t y1, int32_t x2, int32_t y2) {
line(Point(x1, y1), Point(x2, y2));
}, t, p.x, p.y, s, a);
if(thickness == 1) {
hershey::text(hershey_font, [this](int32_t x1, int32_t y1, int32_t x2, int32_t y2) {
line(Point(x1, y1), Point(x2, y2));
}, t, p.x, p.y, s, a);
} else {
hershey::text(hershey_font, [this](int32_t x1, int32_t y1, int32_t x2, int32_t y2) {
thick_line(Point(x1, y1), Point(x2, y2), thickness);
}, t, p.x, p.y, s, a);
}
return;
}
}
Expand Down Expand Up @@ -292,6 +298,42 @@ namespace pimoroni {
}
}

void PicoGraphics::thick_line(Point p1, Point p2, uint thickness) {
// general purpose line
// lines are either "shallow" or "steep" based on whether the x delta
// is greater than the y delta
int32_t dx = p2.x - p1.x;
int32_t dy = p2.y - p1.y;
bool shallow = std::abs(dx) > std::abs(dy);
if(shallow) {
// shallow version
int32_t s = std::abs(dx); // number of steps
int32_t sx = dx < 0 ? -1 : 1; // x step value
int32_t sy = (dy << 16) / s; // y step value in fixed 16:16
int32_t x = p1.x;
int32_t y = p1.y << 16;
while(s--) {
int32_t ht = thickness / 2;
rectangle({x - ht, (y >> 16) - ht, ht * 2, ht * 2});
y += sy;
x += sx;
}
}else{
// steep version
int32_t s = std::abs(dy); // number of steps
int32_t sy = dy < 0 ? -1 : 1; // y step value
int32_t sx = (dx << 16) / s; // x step value in fixed 16:16
int32_t y = p1.y;
int32_t x = p1.x << 16;
while(s--) {
int32_t ht = thickness / 2;
rectangle({(x >> 16) - ht, y - ht, ht * 2, ht * 2});
y += sy;
x += sx;
}
}
}

void PicoGraphics::line(Point p1, Point p2) {
// fast horizontal line
if(p1.y == p2.y) {
Expand Down
11 changes: 11 additions & 0 deletions libraries/pico_graphics/pico_graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ namespace pimoroni {
PenType pen_type;
Rect bounds;
Rect clip;
uint thickness = 1;



Expand Down Expand Up @@ -228,6 +229,7 @@ namespace pimoroni {
virtual void set_pen(uint8_t r, uint8_t g, uint8_t b) = 0;
virtual void set_pixel(const Point &p) = 0;
virtual void set_pixel_span(const Point &p, uint l) = 0;
virtual void set_thickness(uint t) = 0;

virtual int create_pen(uint8_t r, uint8_t g, uint8_t b);
virtual int create_pen_hsv(float h, float s, float v);
Expand Down Expand Up @@ -264,6 +266,7 @@ namespace pimoroni {
void triangle(Point p1, Point p2, Point p3);
void line(Point p1, Point p2);
void from_hsv(float h, float s, float v, uint8_t &r, uint8_t &g, uint8_t &b);
void thick_line(Point p1, Point p2, uint thickness);

protected:
void frame_convert_rgb565(conversion_callback_func callback, next_pixel_func get_next_pixel);
Expand All @@ -276,6 +279,7 @@ namespace pimoroni {
PicoGraphics_Pen1Bit(uint16_t width, uint16_t height, void *frame_buffer);
void set_pen(uint c) override;
void set_pen(uint8_t r, uint8_t g, uint8_t b) override;
void set_thickness(uint t) override;

void set_pixel(const Point &p) override;
void set_pixel_span(const Point &p, uint l) override;
Expand All @@ -292,6 +296,7 @@ namespace pimoroni {
PicoGraphics_Pen1BitY(uint16_t width, uint16_t height, void *frame_buffer);
void set_pen(uint c) override;
void set_pen(uint8_t r, uint8_t g, uint8_t b) override;
void set_thickness(uint t) override;

void set_pixel(const Point &p) override;
void set_pixel_span(const Point &p, uint l) override;
Expand Down Expand Up @@ -334,6 +339,7 @@ namespace pimoroni {

void set_pen(uint c) override;
void set_pen(uint8_t r, uint8_t g, uint8_t b) override;
void set_thickness(uint t) override {};

void set_pixel(const Point &p) override;
void set_pixel_span(const Point &p, uint l) override;
Expand All @@ -360,6 +366,7 @@ namespace pimoroni {
PicoGraphics_PenP4(uint16_t width, uint16_t height, void *frame_buffer);
void set_pen(uint c) override;
void set_pen(uint8_t r, uint8_t g, uint8_t b) override;
void set_thickness(uint t) override {};
int update_pen(uint8_t i, uint8_t r, uint8_t g, uint8_t b) override;
int create_pen(uint8_t r, uint8_t g, uint8_t b) override;
int reset_pen(uint8_t i) override;
Expand Down Expand Up @@ -389,6 +396,7 @@ namespace pimoroni {
PicoGraphics_PenP8(uint16_t width, uint16_t height, void *frame_buffer);
void set_pen(uint c) override;
void set_pen(uint8_t r, uint8_t g, uint8_t b) override;
void set_thickness(uint t) override {};
int update_pen(uint8_t i, uint8_t r, uint8_t g, uint8_t b) override;
int create_pen(uint8_t r, uint8_t g, uint8_t b) override;
int reset_pen(uint8_t i) override;
Expand All @@ -410,6 +418,7 @@ namespace pimoroni {
PicoGraphics_PenRGB332(uint16_t width, uint16_t height, void *frame_buffer);
void set_pen(uint c) override;
void set_pen(uint8_t r, uint8_t g, uint8_t b) override;
void set_thickness(uint t) override {};
int create_pen(uint8_t r, uint8_t g, uint8_t b) override;
void set_pixel(const Point &p) override;
void set_pixel_span(const Point &p, uint l) override;
Expand All @@ -431,6 +440,7 @@ namespace pimoroni {
PicoGraphics_PenRGB565(uint16_t width, uint16_t height, void *frame_buffer);
void set_pen(uint c) override;
void set_pen(uint8_t r, uint8_t g, uint8_t b) override;
void set_thickness(uint t) override {};
int create_pen(uint8_t r, uint8_t g, uint8_t b) override;
int create_pen_hsv(float h, float s, float v) override;
void set_pixel(const Point &p) override;
Expand All @@ -447,6 +457,7 @@ namespace pimoroni {
PicoGraphics_PenRGB888(uint16_t width, uint16_t height, void *frame_buffer);
void set_pen(uint c) override;
void set_pen(uint8_t r, uint8_t g, uint8_t b) override;
void set_thickness(uint t) override {};
int create_pen(uint8_t r, uint8_t g, uint8_t b) override;
int create_pen_hsv(float h, float s, float v) override;
void set_pixel(const Point &p) override;
Expand Down
4 changes: 4 additions & 0 deletions libraries/pico_graphics/pico_graphics_pen_1bit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ namespace pimoroni {
color = std::max(r, std::max(g, b)) >> 4;
}

void PicoGraphics_Pen1Bit::set_thickness(uint t) {
thickness = t;
}

void PicoGraphics_Pen1Bit::set_pixel(const Point &p) {
// pointer to byte in framebuffer that contains this pixel
uint8_t *buf = (uint8_t *)frame_buffer;
Expand Down
Loading

0 comments on commit 943ea13

Please sign in to comment.