Skip to content

Commit

Permalink
MicroPython: Bind new text API, cleanup bindings.
Browse files Browse the repository at this point in the history
Remove dead code and deprecated bindings to keep things tidy.

Overhaul all state functions: pen, alpha, clip, blend, spritesheet, target, cursor, camera.

These now reset to default state when called with no args.

Removed:
* tick
* _reset

These are replaced by "start" and "quit".

Added:
* flip() - flip the screen, can be used in REPL to see your experiments.
* w, h = measure("Text String") - measure text

Changed:
* text
  • Loading branch information
Gadgetoid committed Oct 21, 2021
1 parent 86cb27d commit 9656e33
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 308 deletions.
7 changes: 3 additions & 4 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ builtins =
Buffer,
Voice,
pen,
tick,
flip,
init,
start,
quit,
_reset,
_logo,
play,
clip,
Expand All @@ -35,7 +34,7 @@ builtins =
blit,
sprite,
text,
text_width,
measure,
rgb,
hsv,
intersects,
Expand Down Expand Up @@ -213,4 +212,4 @@ builtins =
SWORD9,
SWORD10,
SWORD11,
CROWN2,
CROWN2,
72 changes: 0 additions & 72 deletions libraries/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,78 +107,6 @@ namespace picosystem {
int32_t cx, int32_t cy, int32_t cw, int32_t ch) {
return x >= cx && y >= cy && x + w <= cx + cw && y + h <= cy + ch;
}
/*
uint32_t text_width(std::string &t) {
// add up length of characters
uint32_t l = 0;
if(_tlw == -1) {
for(auto c : t) {
l += _font[(c - 32) * 9];
}
}else{
l = t.size() * _tlw;
}
// add letter spacing
l += _tls * (t.size() - 1);
return l;
}
uint32_t _word_length(std::string &t, std::size_t &i) {
// skip past any spaces if present
i = t.find_first_not_of(' ', i);
if(i == std::string::npos) {
return std::string::npos;
}
// find next space after current word
std::size_t n = t.find(' ', i);
if(n == std::string::npos) {
n = t.length();
}
// add up length of characters
uint32_t l = 0;
while(i < n) {
if(_tlw == -1) {
l += _font[(t[i] - 32) * 9] + _tls;
}else{
l += _tlw + _tls;
}
i++;
};
// remove last letter spacing
l -= _tls;
return l;
}
uint32_t wrap(std::string &t, uint32_t w) {
std::size_t i = 0, si = 0, ll = 0;
while(true) {
uint32_t wl = _word_length(t, i);
if(i == std::string::npos) {
break;
}
if(ll + wl >= w) {
// if we would overflow the line then replace space with a newline
t[si] = '\n';
// next line starts with current word
ll = 0;
}
ll += wl + (_tlw == -1 ? _font[(t[i] - 32) * 9] : _tlw) + _tls; // 2 == space length, should be a configurable?
si = i;
}
return 0;
}*/

std::vector<std::string> split(const std::string& t, char d) {
std::vector<std::string> l;
Expand Down
4 changes: 2 additions & 2 deletions micropython/examples/picosystem/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def draw(tick):
pen(15, 15, 15)
_logo()
label = "".join(["", "Pi", "co", "Sys", "tem"][0:min(note_idx, 5)])
label_width = text_width(label)
label_width, _ = measure(label)
text(label, int(60 - (label_width / 2)), 90)
return

Expand Down Expand Up @@ -128,7 +128,7 @@ def draw(tick):
label = files[selected]
if label == "__quit__":
label = "quit"
label_width = text_width(label)
label_width, _ = measure(label)
pen(11, 11, 8)
frect(int(60 - label_width / 2 - 3), 102 - 3, label_width + 6, 13)
pen(0, 0, 0)
Expand Down
2 changes: 1 addition & 1 deletion micropython/examples/picosystem/sprites.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def draw(tick):
)

# centre name of weapon at bottom of screen
label_width = text_width(weapons[selected][1])
label_width, _ = measure(weapons[selected][1])
pen(11, 11, 8)
frect(int(60 - label_width / 2 - 3), 102 - 3, label_width + 6, 13)
pen(0, 0, 0)
Expand Down
7 changes: 7 additions & 0 deletions micropython/modules/picosystem/micropython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ target_sources(usermod_${MOD_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}/buffer.cpp
${CMAKE_CURRENT_LIST_DIR}/state.cpp
${CMAKE_CURRENT_LIST_DIR}/primitives.cpp
${CMAKE_CURRENT_LIST_DIR}/text.cpp
${CMAKE_CURRENT_LIST_DIR}/utility.cpp
${CMAKE_CURRENT_LIST_DIR}/hardware.cpp
${CMAKE_CURRENT_LIST_DIR}/stats.cpp
Expand All @@ -17,6 +18,7 @@ target_sources(usermod_${MOD_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/blend.cpp
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/state.cpp
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/primitives.cpp
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/text.cpp
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/utility.cpp
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/hardware.cpp
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/assets.cpp
Expand Down Expand Up @@ -53,4 +55,9 @@ set_source_files_properties(
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/primitives.cpp
PROPERTIES COMPILE_FLAGS
"-Wno-error=sign-compare"
)
set_source_files_properties(
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/text.cpp
PROPERTIES COMPILE_FLAGS
"-Wno-error=sign-compare"
)
25 changes: 15 additions & 10 deletions micropython/modules/picosystem/picosystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,10 @@ const mp_obj_type_t PicosystemVoice_type = {
// picosystem.cpp

MP_DEFINE_CONST_FUN_OBJ_0(picosystem_init_obj, picosystem_init);
MP_DEFINE_CONST_FUN_OBJ_0(picosystem_reset_obj, picosystem_reset);
MP_DEFINE_CONST_FUN_OBJ_0(picosystem_tick_obj, picosystem_tick);
MP_DEFINE_CONST_FUN_OBJ_0(picosystem_logo_obj, picosystem_logo);
MP_DEFINE_CONST_FUN_OBJ_0(picosystem_run_obj, picosystem_run);
MP_DEFINE_CONST_FUN_OBJ_0(picosystem_start_obj, picosystem_start);
MP_DEFINE_CONST_FUN_OBJ_0(picosystem_quit_obj, picosystem_quit);
MP_DEFINE_CONST_FUN_OBJ_0(picosystem_flip_obj, picosystem_flip);

// stats.cpp

Expand All @@ -82,12 +81,15 @@ MP_DEFINE_CONST_FUN_OBJ_KW(picosystem_play_obj, 2, picosystem_audio_play);

// state.cpp

MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picosystem_pen_obj, 1, 4, picosystem_pen);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picosystem_clip_obj, 4, 4, picosystem_clip);
MP_DEFINE_CONST_FUN_OBJ_1(picosystem_blend_obj, picosystem_blend);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picosystem_pen_obj, 0, 4, picosystem_pen);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picosystem_alpha_obj, 0, 4, picosystem_alpha);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picosystem_clip_obj, 0, 4, picosystem_clip);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picosystem_blend_obj, 0, 1, picosystem_blend);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picosystem_target_obj, 0, 1, picosystem_target);
MP_DEFINE_CONST_FUN_OBJ_2(picosystem_camera_obj, picosystem_camera);
MP_DEFINE_CONST_FUN_OBJ_1(picosystem_spritesheet_obj, picosystem_spritesheet);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picosystem_camera_obj, 0, 2, picosystem_camera);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picosystem_cursor_obj, 0, 2, picosystem_cursor);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picosystem_spritesheet_obj, 0, 1, picosystem_spritesheet);
// TODO font?

// primitives.cpp

Expand All @@ -112,8 +114,11 @@ MP_DEFINE_CONST_FUN_OBJ_VAR(picosystem_fpoly_obj, 1, picosystem_fpoly);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picosystem_line_obj, 4, 4, picosystem_line);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picosystem_blit_obj, 7, 7, picosystem_blit);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picosystem_sprite_obj, 3, 7, picosystem_sprite);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picosystem_text_obj, 1, 3, picosystem_text);
MP_DEFINE_CONST_FUN_OBJ_1(picosystem_text_width_obj, picosystem_text_width);

// text.cpp

MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picosystem_text_obj, 1, 4, picosystem_text);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picosystem_measure_obj, 1, 2, picosystem_measure);

// utility.cpp

Expand Down
Loading

0 comments on commit 9656e33

Please sign in to comment.