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 Jun 6, 2019
1 parent 529c51b commit 256b67e
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
55 changes: 55 additions & 0 deletions python/port/genhdr/qstrdefs.in.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,61 @@ 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(shift)
Q(alpha)
Q(xnt)
Q(var)
Q(toolbox)
Q(backspace)

Q(exp)
Q(ln)
Q(log)
Q(imaginary)
Q(,)
Q(^)

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

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

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

key2mp keyMapping[] =
{
{ Ion::Keyboard::Key::A1, MP_ROM_QSTR(MP_QSTR_left) },
{ Ion::Keyboard::Key::A2, MP_ROM_QSTR(MP_QSTR_up) },
{ Ion::Keyboard::Key::A3, MP_ROM_QSTR(MP_QSTR_down) },
{ Ion::Keyboard::Key::A4, MP_ROM_QSTR(MP_QSTR_right) },
{ Ion::Keyboard::Key::A5, MP_ROM_QSTR(MP_QSTR_OK) },
{ Ion::Keyboard::Key::A6, MP_ROM_QSTR(MP_QSTR_back) },

{ Ion::Keyboard::Key::C1, MP_ROM_QSTR(MP_QSTR_shift) },
{ Ion::Keyboard::Key::C2, MP_ROM_QSTR(MP_QSTR_alpha) },
{ Ion::Keyboard::Key::C3, MP_ROM_QSTR(MP_QSTR_xnt) },
{ Ion::Keyboard::Key::C4, MP_ROM_QSTR(MP_QSTR_var) },
{ Ion::Keyboard::Key::C5, MP_ROM_QSTR(MP_QSTR_toolbox) },
{ Ion::Keyboard::Key::C6, MP_ROM_QSTR(MP_QSTR_backspace) },

{ Ion::Keyboard::Key::D1, MP_ROM_QSTR(MP_QSTR_exp) },
{ Ion::Keyboard::Key::D2, MP_ROM_QSTR(MP_QSTR_ln) },
{ Ion::Keyboard::Key::D3, MP_ROM_QSTR(MP_QSTR_log) },
{ Ion::Keyboard::Key::D4, MP_ROM_QSTR(MP_QSTR_imaginary) },
{ Ion::Keyboard::Key::D5, MP_ROM_QSTR(MP_QSTR_toolbox) },
{ Ion::Keyboard::Key::D6, MP_ROM_QSTR(MP_QSTR_backspace) },

{ Ion::Keyboard::Key::E1, MP_ROM_QSTR(MP_QSTR_sin) },
{ Ion::Keyboard::Key::E2, MP_ROM_QSTR(MP_QSTR_cos) },
{ Ion::Keyboard::Key::E3, MP_ROM_QSTR(MP_QSTR_tan) },
{ Ion::Keyboard::Key::E4, MP_ROM_QSTR(MP_QSTR_pi) },
{ Ion::Keyboard::Key::E5, MP_ROM_QSTR(MP_QSTR_sqrt) },
{ Ion::Keyboard::Key::E6, MP_ROM_QSTR(MP_QSTR_square) },

{ Ion::Keyboard::Key::F1, MP_ROM_QSTR(MP_QSTR_7) },
{ Ion::Keyboard::Key::F2, MP_ROM_QSTR(MP_QSTR_8) },
{ Ion::Keyboard::Key::F3, MP_ROM_QSTR(MP_QSTR_9) },
{ Ion::Keyboard::Key::F4, MP_ROM_QSTR(MP_QSTR__paren_open_) },
{ Ion::Keyboard::Key::F5, MP_ROM_QSTR(MP_QSTR__paren_close_) },

{ Ion::Keyboard::Key::G1, MP_ROM_QSTR(MP_QSTR_4) },
{ Ion::Keyboard::Key::G2, MP_ROM_QSTR(MP_QSTR_5) },
{ Ion::Keyboard::Key::G3, MP_ROM_QSTR(MP_QSTR_6) },
{ Ion::Keyboard::Key::G4, MP_ROM_QSTR(MP_QSTR__star_) },
{ Ion::Keyboard::Key::G5, MP_ROM_QSTR(MP_QSTR__slash_) },

{ Ion::Keyboard::Key::H1, MP_ROM_QSTR(MP_QSTR_1) },
{ Ion::Keyboard::Key::H2, MP_ROM_QSTR(MP_QSTR_2) },
{ Ion::Keyboard::Key::H3, MP_ROM_QSTR(MP_QSTR_3) },
{ Ion::Keyboard::Key::H4, MP_ROM_QSTR(MP_QSTR__plus_) },
{ Ion::Keyboard::Key::H5, MP_ROM_QSTR(MP_QSTR__hyphen_) },

{ Ion::Keyboard::Key::I1, MP_ROM_QSTR(MP_QSTR_0) },
{ Ion::Keyboard::Key::I2, MP_ROM_QSTR(MP_QSTR__dot_) },
{ Ion::Keyboard::Key::I3, MP_ROM_QSTR(MP_QSTR_EE) },
{ Ion::Keyboard::Key::I4, MP_ROM_QSTR(MP_QSTR_Ans) },
{ Ion::Keyboard::Key::I5, 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 256b67e

Please sign in to comment.