Skip to content

Commit

Permalink
Static buffer for text show
Browse files Browse the repository at this point in the history
Also fixed space assignment in font render
  • Loading branch information
graeme-winter committed Apr 17, 2021
1 parent a14c124 commit 4cafb30
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
38 changes: 26 additions & 12 deletions micropython/modules/pico_scroll/pico_scroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ mp_obj_t picoscroll_scroll_text(mp_obj_t text_obj, mp_obj_t brightness_obj,
}
}
}
scroll->update();
sleep_ms(delay_ms);
}
scroll->update();
sleep_ms(delay_ms);
}
m_free(buffer);
} else {
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
Expand All @@ -106,13 +106,11 @@ mp_obj_t picoscroll_scroll_text(mp_obj_t text_obj, mp_obj_t brightness_obj,
return mp_const_none;
}

// TODO refactor this to not need a buffer - should be able to render
// view into static 17 bytes
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;
unsigned char buffer[PicoScroll::WIDTH + 7];
int text_len, bfr_len;

mp_get_buffer_raise(text_obj, &bufinfo, MP_BUFFER_READ);
Expand All @@ -121,22 +119,38 @@ mp_obj_t picoscroll_show_text(mp_obj_t text_obj, mp_obj_t brightness_obj,
int offset = mp_obj_get_int(offset_obj);

text_len = bufinfo.len;
bfr_len = 6 * text_len;
bfr_len = PicoScroll::WIDTH + 7;

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)) {
if ((offset < -width) || (offset > 6 * text_len)) {
return mp_const_none;
}

// allocate buffer, render text, free buffer #TODO probably can do
// without the buffer here
// compute what can actually be seen, render only that...
// modify offset and bfr_len accordingly
if (offset < 0) {
int space = 1 + (width + offset) / 6;
if (space < text_len) {
text_len = space;
}
} else {
int start = offset / 6;
offset -= start * 6;
text_len = text_len - start;
if (text_len > 4) {
text_len = 4;
}
}

if (bfr_len > 6 * text_len) {
bfr_len = 6 * text_len;
}

buffer = (unsigned char *)m_malloc(sizeof(unsigned char) * bfr_len);
render(values, text_len, buffer, bfr_len);

for (int x = 0; x < width; x++) {
Expand All @@ -149,7 +163,7 @@ mp_obj_t picoscroll_show_text(mp_obj_t text_obj, mp_obj_t brightness_obj,
}
}
}
m_free(buffer);

} else {
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
}
Expand Down
2 changes: 1 addition & 1 deletion micropython/modules/pico_scroll/pico_scroll_font.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ int render(unsigned char *text, int nchr, unsigned char *buffer, int nbfr) {
for (int j = 0; j < 5; j++) {
buffer[i * 6 + j] = symbol[j];
}
buffer[i * 6 + 6] = 0x0;
buffer[i * 6 + 5] = 0x0;
}
return 0;
}

0 comments on commit 4cafb30

Please sign in to comment.