Skip to content

Commit

Permalink
Pico Scroll: Refactor to class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Mar 13, 2023
1 parent e7a99e5 commit 6e7878a
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 170 deletions.
5 changes: 5 additions & 0 deletions libraries/pico_scroll/pico_scroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ enum reg {

namespace pimoroni {

PicoScroll::~PicoScroll() {
clear();
update();
}

void PicoScroll::init() {
// setup i2c interface
i2c_init(i2c0, 400000);
Expand Down
1 change: 1 addition & 0 deletions libraries/pico_scroll/pico_scroll.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace pimoroni {
uint8_t __fb[BUFFER_SIZE];

public:
~PicoScroll();
void init();
void update();
void set_pixels(const char *pixels);
Expand Down
7 changes: 5 additions & 2 deletions libraries/pico_scroll/pico_scroll_font.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "pico_scroll_font.hpp"
#include <string.h>

/* static font data */
static const unsigned char __bitmap[256][5] = {
Expand Down Expand Up @@ -135,13 +136,15 @@ static const unsigned char __bitmap[256][5] = {
/* render a text string to a pre-allocated buffer - strlen(text) * 6 bytes */
int render_text(const char *text, unsigned int nchr, unsigned char *buffer, unsigned int nbfr) {
// TODO check nbfr >= 6 * nchr
memset(buffer, 0, nbfr);

for (unsigned int i = 0; i < nchr; i++) {
const unsigned char *symbol = __bitmap[(unsigned int)text[i]];
for (unsigned int j = 0; j < 5; j++) {
buffer[i * 6 + j] = symbol[j];
unsigned int offset = i * 6 + j;
if (offset >= nbfr) return -1;
buffer[offset] = symbol[j];
}
buffer[i * 6 + 5] = 0x0;
}
return 0;
}
18 changes: 8 additions & 10 deletions micropython/examples/pico_scroll/demo.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
import time
import picoscroll as scroll
from picoscroll import PicoScroll, WIDTH, HEIGHT

scroll = PicoScroll()

scroll.init()
i = 0
loop = 18
br_mult = 1
br_pressed = 32
tail = 12

width = scroll.get_width()
height = scroll.get_height()

while True:
scroll.clear()
for y in range(0, height):
for x in range(0, width):
for y in range(0, HEIGHT):
for x in range(0, WIDTH):
if x < 3 and y < 3 and scroll.is_pressed(scroll.BUTTON_A):
scroll.set_pixel(x, y, br_pressed)
elif x < 3 and y > 3 and scroll.is_pressed(scroll.BUTTON_B):
scroll.set_pixel(x, y, br_pressed)
elif x > width - 4 and y < 3 and scroll.is_pressed(scroll.BUTTON_X):
elif x > WIDTH - 4 and y < 3 and scroll.is_pressed(scroll.BUTTON_X):
scroll.set_pixel(x, y, br_pressed)
elif x > width - 4 and y > 3 and scroll.is_pressed(scroll.BUTTON_Y):
elif x > WIDTH - 4 and y > 3 and scroll.is_pressed(scroll.BUTTON_Y):
scroll.set_pixel(x, y, br_pressed)
else:
m = (x + (y * width)) % loop
m = (x + (y * WIDTH)) % loop
for b in range(0, loop):
if m == (i + (loop - b)) % loop and b < tail:
scroll.set_pixel(x, y, br_mult * (tail - b))
Expand Down
4 changes: 2 additions & 2 deletions micropython/examples/pico_scroll/scroll_text.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
import picoscroll as scroll
from picoscroll import PicoScroll

scroll.init()
scroll = PicoScroll()

while True:
scroll.scroll_text("Hello World", 128, 80)
Expand Down
31 changes: 11 additions & 20 deletions micropython/modules/pico_scroll/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,27 @@ Our Pico Scroll Pack offers a 17x7 white LED matrix for your Raspberry Pi Pico.
We've included helper functions to handle every aspect of drawing to the matrix and interfacing with the buttons. See the [function reference](#function-reference) for details.

- [Example Program](#example-program)
- [Function Reference](#function-reference)
- [init](#init)
- [get_width](#get_width)
- [get_height](#get_height)
- [set_pixel](#set_pixel)
- [set_pixels](#set_pixels)
- [show_text](#show_text)
- [scroll_text](#scroll_text)
- [show_bitmap_1d](#show_bitmap_1d)
- [Function reference](#function-reference)
- [get\_width](#get_width)
- [get\_height](#get_height)
- [set\_pixel](#set_pixel)
- [set\_pixels](#set_pixels)
- [show\_text](#show_text)
- [scroll\_text](#scroll_text)
- [show\_bitmap\_1d](#show_bitmap_1d)
- [update](#update)
- [clear](#clear)
- [is_pressed](#is_pressed)
- [is\_pressed](#is_pressed)

## Example Program

The following example sets up the matrix, sets each pixel to an increasing brightnesses level, and then clears the matrix only after button A is pressed.

```python
import picoscroll
from picoscroll import PicoScroll

# Initialise the board
picoscroll.init()
picoscroll = PicoScroll()

brightness = 0

Expand All @@ -51,14 +50,6 @@ picoscroll.update()

## Function reference

### init

Sets up the Pico Scroll Pack. The `init` function must be called before any other functions as it configures the required pins on the Pico board.

```python
picoscroll.init()
```

### get_width
### get_height

Expand Down
64 changes: 46 additions & 18 deletions micropython/modules/pico_scroll/pico_scroll.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,21 @@ enum buttons
////////////////////////////////////////////////////////////////////////////////////////////////////

/***** Module Functions *****/
STATIC MP_DEFINE_CONST_FUN_OBJ_0(picoscroll_init_obj, picoscroll_init);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(picoscroll_get_width_obj, picoscroll_get_width);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(picoscroll_get_height_obj, picoscroll_get_height);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(picoscroll_update_obj, picoscroll_update);
STATIC MP_DEFINE_CONST_FUN_OBJ_3(picoscroll_set_pixel_obj, picoscroll_set_pixel);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(picoscroll_set_pixels_obj, picoscroll_set_pixels);
STATIC MP_DEFINE_CONST_FUN_OBJ_3(picoscroll_show_text_obj, picoscroll_show_text);
STATIC MP_DEFINE_CONST_FUN_OBJ_3(picoscroll_scroll_text_obj, picoscroll_scroll_text);
STATIC MP_DEFINE_CONST_FUN_OBJ_3(picoscroll_show_bitmap_1d_obj, picoscroll_show_bitmap_1d);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(picoscroll_clear_obj, picoscroll_clear);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(picoscroll_is_pressed_obj, picoscroll_is_pressed);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(picoscroll___del___obj, picoscroll___del__);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(picoscroll_get_width_obj, picoscroll_get_width);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(picoscroll_get_height_obj, picoscroll_get_height);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(picoscroll_update_obj, picoscroll_update);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picoscroll_set_pixel_obj, 4, 4, picoscroll_set_pixel);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(picoscroll_set_pixels_obj, picoscroll_set_pixels);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picoscroll_show_text_obj, 4, 4, picoscroll_show_text);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picoscroll_scroll_text_obj, 4, 4, picoscroll_scroll_text);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picoscroll_show_bitmap_1d_obj, 4, 4, picoscroll_show_bitmap_1d);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(picoscroll_clear_obj, picoscroll_clear);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(picoscroll_is_pressed_obj, picoscroll_is_pressed);

/***** Globals Table *****/
STATIC const mp_map_elem_t picoscroll_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_picoscroll) },
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&picoscroll_init_obj) },
/* Class Methods */
STATIC const mp_rom_map_elem_t picoscroll_locals[] = {
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&picoscroll___del___obj) },
{ MP_ROM_QSTR(MP_QSTR_get_width), MP_ROM_PTR(&picoscroll_get_width_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_height), MP_ROM_PTR(&picoscroll_get_height_obj) },
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&picoscroll_update_obj) },
Expand All @@ -40,15 +39,44 @@ STATIC const mp_map_elem_t picoscroll_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_scroll_text), MP_ROM_PTR(&picoscroll_scroll_text_obj) },
{ MP_ROM_QSTR(MP_QSTR_show_bitmap_1d), MP_ROM_PTR(&picoscroll_show_bitmap_1d_obj) },
{ MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&picoscroll_clear_obj) },
{ MP_ROM_QSTR(MP_QSTR_is_pressed), MP_ROM_PTR(&picoscroll_is_pressed_obj) },
{ MP_ROM_QSTR(MP_QSTR_is_pressed), MP_ROM_PTR(&picoscroll_is_pressed_obj) },
{ MP_ROM_QSTR(MP_QSTR_BUTTON_A), MP_ROM_INT(BUTTON_A) },
{ MP_ROM_QSTR(MP_QSTR_BUTTON_B), MP_ROM_INT(BUTTON_B) },
{ MP_ROM_QSTR(MP_QSTR_BUTTON_X), MP_ROM_INT(BUTTON_X) },
{ MP_ROM_QSTR(MP_QSTR_BUTTON_Y), MP_ROM_INT(BUTTON_Y) },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_picoscroll_globals, picoscroll_globals_table);
STATIC MP_DEFINE_CONST_DICT(mp_module_picoscroll_locals, picoscroll_locals);

#ifdef MP_DEFINE_CONST_OBJ_TYPE
MP_DEFINE_CONST_OBJ_TYPE(
PicoScroll_type,
MP_QSTR_PicoScroll,
MP_TYPE_FLAG_NONE,
make_new, picoscroll_make_new,
locals_dict, (mp_obj_dict_t*)&mp_module_picoscroll_locals
);
#else
const mp_obj_type_t PicoScroll_type = {
{ &mp_type_type },
.name = MP_QSTR_PicoScroll,
.make_new = picoscroll_make_new,
.locals_dict = (mp_obj_dict_t*)&mp_module_picoscroll_locals,
};
#endif

/* Module Globals */
STATIC const mp_map_elem_t picoscroll_globals[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_picoscroll) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PicoScroll), (mp_obj_t)&PicoScroll_type },
{ MP_ROM_QSTR(MP_QSTR_WIDTH), MP_ROM_INT(17) },
{ MP_ROM_QSTR(MP_QSTR_HEIGHT), MP_ROM_INT(7) },
{ MP_ROM_QSTR(MP_QSTR_BUTTON_A), MP_ROM_INT(BUTTON_A) },
{ MP_ROM_QSTR(MP_QSTR_BUTTON_B), MP_ROM_INT(BUTTON_B) },
{ MP_ROM_QSTR(MP_QSTR_BUTTON_X), MP_ROM_INT(BUTTON_X) },
{ MP_ROM_QSTR(MP_QSTR_BUTTON_Y), MP_ROM_INT(BUTTON_Y) },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_picoscroll_globals, picoscroll_globals);

/***** Module Definition *****/
const mp_obj_module_t picoscroll_user_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_picoscroll_globals,
Expand Down
Loading

0 comments on commit 6e7878a

Please sign in to comment.