Skip to content

Commit

Permalink
[python] Implement get_keys in kandinsky
Browse files Browse the repository at this point in the history
  • Loading branch information
boricj committed Oct 11, 2019
1 parent 1d57573 commit 0971d72
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
56 changes: 56 additions & 0 deletions python/port/genhdr/qstrdefs.in.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,62 @@ Q(fill_rect)
Q(get_pixel)
Q(set_pixel)
Q(wait_vblank)
Q(get_keys)

// Keys QSTRs
Q(left)
Q(up)
Q(down)
Q(right)
Q(OK)
Q(back)

Q(home)
Q(onOff)
Q(shift)
Q(alpha)
Q(xnt)
Q(var)
Q(toolbox)
Q(backspace)

Q(exp)
Q(ln)
Q(log)
Q(imaginary)
Q(comma)
Q(power)

Q(sin)
Q(cos)
Q(tan)
Q(pi)
Q(sqrt)
Q(square)

Q(7)
Q(8)
Q(9)
Q(()
Q())

Q(4)
Q(5)
Q(6)
Q(*)
Q(/)

Q(1)
Q(2)
Q(3)
Q(+)
Q(-)

Q(0)
Q(.)
Q(EE)
Q(Ans)
Q(EXE)

// Turtle QSTRs
Q(turtle)
Expand Down
79 changes: 79 additions & 0 deletions python/port/mod/kandinsky/modkandinsky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,82 @@ mp_obj_t modkandinsky_wait_vblank() {
Ion::Display::waitForVBlank();
return mp_const_none;
}

struct key2mp
{
Ion::Keyboard::Key key;
mp_obj_t string;
};

const static key2mp keyMapping[] =
{
{ Ion::Keyboard::Key::Left, MP_ROM_QSTR(MP_QSTR_left) },
{ Ion::Keyboard::Key::Right, MP_ROM_QSTR(MP_QSTR_up) },
{ Ion::Keyboard::Key::Down, MP_ROM_QSTR(MP_QSTR_down) },
{ Ion::Keyboard::Key::Up, MP_ROM_QSTR(MP_QSTR_right) },
{ Ion::Keyboard::Key::OK, MP_ROM_QSTR(MP_QSTR_OK) },
{ Ion::Keyboard::Key::Back, MP_ROM_QSTR(MP_QSTR_back) },

{ Ion::Keyboard::Key::Home, MP_ROM_QSTR(MP_QSTR_home) },
{ Ion::Keyboard::Key::OnOff, MP_ROM_QSTR(MP_QSTR_onOff) },

{ Ion::Keyboard::Key::Shift, MP_ROM_QSTR(MP_QSTR_shift) },
{ Ion::Keyboard::Key::Alpha, MP_ROM_QSTR(MP_QSTR_alpha) },
{ Ion::Keyboard::Key::XNT, MP_ROM_QSTR(MP_QSTR_xnt) },
{ Ion::Keyboard::Key::Var, MP_ROM_QSTR(MP_QSTR_var) },
{ Ion::Keyboard::Key::Toolbox, MP_ROM_QSTR(MP_QSTR_toolbox) },
{ Ion::Keyboard::Key::Backspace, MP_ROM_QSTR(MP_QSTR_backspace) },

{ Ion::Keyboard::Key::Exp, MP_ROM_QSTR(MP_QSTR_exp) },
{ Ion::Keyboard::Key::Ln, MP_ROM_QSTR(MP_QSTR_ln) },
{ Ion::Keyboard::Key::Log, MP_ROM_QSTR(MP_QSTR_log) },
{ Ion::Keyboard::Key::Imaginary, MP_ROM_QSTR(MP_QSTR_imaginary) },
{ Ion::Keyboard::Key::Comma, MP_ROM_QSTR(MP_QSTR_comma) },
{ Ion::Keyboard::Key::Power, MP_ROM_QSTR(MP_QSTR_power) },

{ Ion::Keyboard::Key::Sine, MP_ROM_QSTR(MP_QSTR_sin) },
{ Ion::Keyboard::Key::Cosine, MP_ROM_QSTR(MP_QSTR_cos) },
{ Ion::Keyboard::Key::Tangent, MP_ROM_QSTR(MP_QSTR_tan) },
{ Ion::Keyboard::Key::Pi, MP_ROM_QSTR(MP_QSTR_pi) },
{ Ion::Keyboard::Key::Sqrt, MP_ROM_QSTR(MP_QSTR_sqrt) },
{ Ion::Keyboard::Key::Square, MP_ROM_QSTR(MP_QSTR_square) },

{ Ion::Keyboard::Key::Seven, MP_ROM_QSTR(MP_QSTR_7) },
{ Ion::Keyboard::Key::Eight, MP_ROM_QSTR(MP_QSTR_8) },
{ Ion::Keyboard::Key::Nine, MP_ROM_QSTR(MP_QSTR_9) },
{ Ion::Keyboard::Key::RightParenthesis, MP_ROM_QSTR(MP_QSTR__paren_open_) },
{ Ion::Keyboard::Key::LeftParenthesis, MP_ROM_QSTR(MP_QSTR__paren_close_) },

{ Ion::Keyboard::Key::Four, MP_ROM_QSTR(MP_QSTR_4) },
{ Ion::Keyboard::Key::Five, MP_ROM_QSTR(MP_QSTR_5) },
{ Ion::Keyboard::Key::Six, MP_ROM_QSTR(MP_QSTR_6) },
{ Ion::Keyboard::Key::Multiplication, MP_ROM_QSTR(MP_QSTR__star_) },
{ Ion::Keyboard::Key::Division, MP_ROM_QSTR(MP_QSTR__slash_) },

{ Ion::Keyboard::Key::One, MP_ROM_QSTR(MP_QSTR_1) },
{ Ion::Keyboard::Key::Two, MP_ROM_QSTR(MP_QSTR_2) },
{ Ion::Keyboard::Key::Three, MP_ROM_QSTR(MP_QSTR_3) },
{ Ion::Keyboard::Key::Plus, MP_ROM_QSTR(MP_QSTR__plus_) },
{ Ion::Keyboard::Key::Minus, MP_ROM_QSTR(MP_QSTR__hyphen_) },

{ Ion::Keyboard::Key::Zero, MP_ROM_QSTR(MP_QSTR_0) },
{ Ion::Keyboard::Key::Dot, MP_ROM_QSTR(MP_QSTR__dot_) },
{ Ion::Keyboard::Key::EE, MP_ROM_QSTR(MP_QSTR_EE) },
{ Ion::Keyboard::Key::Ans, MP_ROM_QSTR(MP_QSTR_Ans) },
{ Ion::Keyboard::Key::EXE, MP_ROM_QSTR(MP_QSTR_EXE) },
};

mp_obj_t modkandinsky_get_keys() {
micropython_port_interrupt_if_needed();

Ion::Keyboard::State keys = Ion::Keyboard::scan();
mp_obj_t result = mp_obj_new_set(0, nullptr);

for (unsigned i = 0; i < sizeof(keyMapping)/sizeof(key2mp); i++) {
if (keys.keyDown(keyMapping[i].key)) {
mp_obj_set_store(result, keyMapping[i].string);
}
}

return result;
}
1 change: 1 addition & 0 deletions python/port/mod/kandinsky/modkandinsky.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ mp_obj_t modkandinsky_set_pixel(mp_obj_t x, mp_obj_t y, mp_obj_t color);
mp_obj_t modkandinsky_draw_string(size_t n_args, const mp_obj_t *args);
mp_obj_t modkandinsky_fill_rect(size_t n_args, const mp_obj_t *args);
mp_obj_t modkandinsky_wait_vblank();
mp_obj_t modkandinsky_get_keys();
2 changes: 2 additions & 0 deletions python/port/mod/kandinsky/modkandinsky_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(modkandinsky_set_pixel_obj, modkandinsky_set_pi
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modkandinsky_draw_string_obj, 3, 5, modkandinsky_draw_string);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modkandinsky_fill_rect_obj, 5, 5, modkandinsky_fill_rect);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(modkandinsky_wait_vblank_obj, modkandinsky_wait_vblank);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(modkandinsky_get_keys_obj, modkandinsky_get_keys);

STATIC const mp_rom_map_elem_t modkandinsky_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_kandinsky) },
Expand All @@ -15,6 +16,7 @@ STATIC const mp_rom_map_elem_t modkandinsky_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_draw_string), (mp_obj_t)&modkandinsky_draw_string_obj },
{ MP_ROM_QSTR(MP_QSTR_fill_rect), (mp_obj_t)&modkandinsky_fill_rect_obj },
{ MP_ROM_QSTR(MP_QSTR_wait_vblank), (mp_obj_t)&modkandinsky_wait_vblank_obj },
{ MP_ROM_QSTR(MP_QSTR_get_keys), (mp_obj_t)&modkandinsky_get_keys_obj },
};

STATIC MP_DEFINE_CONST_DICT(modkandinsky_module_globals, modkandinsky_module_globals_table);
Expand Down

0 comments on commit 0971d72

Please sign in to comment.