WIP but nearly there: need to resolve string vs. buffer protocol

pull/121/head
Graeme Winter 2021-04-15 20:01:37 +01:00
rodzic f984754b2a
commit 39b88182a8
5 zmienionych plików z 57 dodań i 0 usunięć

Wyświetl plik

@ -2,6 +2,7 @@ add_library(usermod_pico_scroll INTERFACE)
target_sources(usermod_pico_scroll INTERFACE
${CMAKE_CURRENT_LIST_DIR}/pico_scroll.c
${CMAKE_CURRENT_LIST_DIR}/pico_scroll_font.c
${CMAKE_CURRENT_LIST_DIR}/pico_scroll.cpp
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/pico_scroll/pico_scroll.cpp
)

Wyświetl plik

@ -21,6 +21,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(picoscroll_get_height_obj, picoscroll_get_heigh
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_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);
@ -34,6 +35,7 @@ STATIC const mp_map_elem_t picoscroll_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&picoscroll_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_pixel), MP_ROM_PTR(&picoscroll_set_pixel_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_pixels), MP_ROM_PTR(&picoscroll_set_pixels_obj) },
{ MP_ROM_QSTR(MP_QSTR_show_text), MP_ROM_PTR(&picoscroll_show_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) },

Wyświetl plik

@ -11,6 +11,7 @@ PicoScroll *scroll = nullptr;
extern "C" {
#include "pico_scroll.h"
#include "pico_scroll_font.h"
#define NOT_INITIALISED_MSG "Cannot call this function, as picoscroll is not initialised. Call picoscroll.init() first."
#define BUFFER_TOO_SMALL_MSG "bytearray too small: len(image) < width * height."
@ -60,6 +61,57 @@ mp_obj_t picoscroll_set_pixel(mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t v_obj) {
return mp_const_none;
}
mp_obj_t picoscroll_show_text(mp_obj_t text_obj, mp_obj_t brightness_obj, mp_obj_t offset_obj) {
if(scroll != nullptr) {
mp_buffer_info_t bufinfo;
unsigned char * buffer;
int text_len, bfr_len;
mp_get_buffer_raise(text_obj, &bufinfo, MP_BUFFER_RW);
unsigned char * values = (unsigned char *) bufinfo.buf;
int brightness = mp_obj_get_int(brightness_obj);
int offset = mp_obj_get_int(offset_obj);
text_len = bufinfo.len;
bfr_len = 6 * text_len;
int width = PicoScroll::WIDTH;
int height = PicoScroll::HEIGHT;
// clear the scroll, so only need to write visible bytes
scroll->clear();
if ((offset < -width) || (offset > bfr_len)) {
return mp_const_none;
}
// allocate buffer, render text, free buffer #TODO probably can do
// without the buffer here
buffer = (unsigned char *) m_malloc (sizeof(unsigned char) * bfr_len);
render(values, text_len, buffer, bfr_len);
for (int x = 0; x < width; x++) {
int k = offset + x;
if ((k >= 0) && (k < bfr_len)) {
unsigned char col = buffer[k];
for (int y = 0; y < height; y++) {
int val = brightness * ((col >> y) & 1);
scroll->set_pixel(x, y, val);
}
}
}
m_free(buffer);
} else {
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
}
return mp_const_none;
}
mp_obj_t picoscroll_set_pixels(mp_obj_t image_obj) {
if(scroll != nullptr) {
mp_buffer_info_t bufinfo;

Wyświetl plik

@ -8,6 +8,7 @@ extern mp_obj_t picoscroll_get_height();
extern mp_obj_t picoscroll_update();
extern mp_obj_t picoscroll_set_pixel(mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t v_obj);
extern mp_obj_t picoscroll_set_pixels(mp_obj_t image_obj);
extern mp_obj_t picoscroll_show_text(mp_obj_t text_obj, mp_obj_t brightness, mp_obj_t offset);
extern mp_obj_t picoscroll_show_bitmap_1d(mp_obj_t bitmap_obj, mp_obj_t brightness, mp_obj_t offset);
extern mp_obj_t picoscroll_clear();
extern mp_obj_t picoscroll_is_pressed(mp_obj_t button_obj);

Wyświetl plik

@ -143,4 +143,5 @@ int render(unsigned char *text, int nchr, unsigned char *buffer, int nbfr) {
}
buffer[i * 6 + 6] = 0x0;
}
return 0;
}