Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python] Turtle module #748

Merged
merged 59 commits into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
464c1a5
[python] Initial turtle module
boricj Oct 27, 2018
0a05b94
[python] Turtle module improvements
boricj Oct 28, 2018
ca589b9
[python] Fix turtle icon dependency
boricj Oct 28, 2018
3a7ae27
[python] Fix turtle icon dependency (for real)
boricj Oct 28, 2018
9e24370
[python] Clean up turtle code and samples
boricj Oct 28, 2018
0c94f44
[python] Return value for turtle.pensize()
boricj Oct 30, 2018
fcf7cc3
[python] Implement turtle.position()
boricj Oct 30, 2018
c89a369
[python] Add getters for turtle module
boricj Oct 30, 2018
34f5aba
[python] Add turtle toolbox
boricj Oct 30, 2018
efaa87d
Merge branch 'master' into python_turtle
Ecco Nov 16, 2018
82f0b71
[python/turtle] Use micropython_port_interruptible_msleep
Ecco Nov 16, 2018
22a39a2
[python/turtle] Use a dedicated folder
Ecco Nov 16, 2018
fe076d0
[python/turtle] Use mp_float_t
Ecco Nov 16, 2018
a9091c9
[python/turtle] Use a Turtle class
Ecco Nov 16, 2018
7fc34fe
[python/turtle] Use an init method to reset the turtle
Ecco Nov 16, 2018
5748521
[python/turtle] Try to play well with uPy's GC
Ecco Nov 16, 2018
467ae9c
[python/turtle] Fix small typos
Ecco Nov 16, 2018
05a2491
[python/turtle] Fix the heading
Ecco Nov 16, 2018
49e8a50
[python] Fix micropython_port_interruptible_msleep
LeaNumworks Dec 5, 2018
1e78f20
[python] Return after interruption in possibly long turtle methods
LeaNumworks Dec 6, 2018
4b3ce7a
[python/turtle] Fix Turtle::setSpeed
LeaNumworks Dec 6, 2018
e769255
[python/turtle] Default pen size is 1
LeaNumworks Dec 6, 2018
d530637
[python/turtle] Fix speed type
LeaNumworks Dec 6, 2018
432e259
[python/turtle] Speed max and min should produce the same behavior
LeaNumworks Dec 6, 2018
3ca1790
[python/turtle] The Y axis is oriented upwards
LeaNumworks Dec 6, 2018
9c9355d
[python/turtle] Use Escher::Metric values instead of copying them
LeaNumworks Dec 6, 2018
cc3beb6
[python/turtle] Turtle::reset() method
LeaNumworks Dec 6, 2018
c8a5972
[python/turtle] Turtle::circle(radius, angle)
LeaNumworks Dec 7, 2018
e836593
[kandinsky] In KDContext::getPixels: beware of rects crossing the screen
LeaNumworks Dec 7, 2018
c95e291
[python/turtle] First icon redesign
LeaNumworks Dec 7, 2018
3b33425
[python/turlte] Move the turtle paws to mimic crawling
LeaNumworks Dec 10, 2018
3249ccb
[python/turtle] "back()" shortcut for "backwards()"
LeaNumworks Dec 10, 2018
fa0e93e
[python/turtle] Fix Turtle::circle for negative angles
LeaNumworks Dec 10, 2018
7efb48a
[python/turtle] Turtle::circle's angle argument is optional
LeaNumworks Dec 10, 2018
d545c45
[python/turtle] When speed = 0, do not draw the turtle during "goTo"
LeaNumworks Dec 10, 2018
23e3038
[python/turtle] Remove waitForVBlank before drawings
LeaNumworks Dec 10, 2018
62cda01
[python/turtle] color and pencolor can take various arguments
LeaNumworks Dec 11, 2018
3bd3e64
[python/turtle] Order the qstr
LeaNumworks Dec 11, 2018
99d71bb
[python/turtle] Clean Turtle::setColor
LeaNumworks Dec 12, 2018
8ac6098
[python/turtle] Remove old turtle icon
LeaNumworks Dec 12, 2018
91c4fea
[python/turtle] Clean Turtle::PawPosition
LeaNumworks Dec 12, 2018
306869e
[python/turtle] Tune values of the turtle speed
LeaNumworks Dec 11, 2018
609740b
[apps/code] Order catalog.universal.i18n
LeaNumworks Dec 17, 2018
8a3aa1a
[apps/code] Add turtle functions in th python toolbox
LeaNumworks Dec 17, 2018
b058b02
[apps/code] Add turtle functions in the toolbox catalog
LeaNumworks Dec 17, 2018
b3c11f2
[apps/code] Put turtle module higher in Python toolbox
LeaNumworks Dec 17, 2018
62de89c
[apps/code] Fix 'blue', 'red', ... python command insertion
LeaNumworks Dec 17, 2018
101370c
[escher] Change ToolboxMessageTree constructors
LeaNumworks Dec 17, 2018
ad745f7
[python/turtle] Add speed() methods without args
LeaNumworks Dec 17, 2018
ed19d19
Merge branch 'master' into python_turtle
LeaNumworks Dec 17, 2018
4b30679
[python/turtle] Turtle::m_drawn is false if sandbox not displayed
LeaNumworks Dec 17, 2018
de6646a
[build/emscripten] Add turtle symbols to the whitelist
LeaNumworks Dec 18, 2018
5a941bc
[python/turtle] Clean circle code
LeaNumworks Dec 21, 2018
c2dbe9f
[python/turtle] Fix line drawing glitches
LeaNumworks Dec 21, 2018
df2b9c4
[escher/apps] Get children count via template in ToolboxMessageTree::…
LeaNumworks Dec 21, 2018
03f300a
[python/turtle] Add showturtle and hideturtle to the toolbox
LeaNumworks Dec 27, 2018
384fa04
[apps/code] Change the default scripts: squares instead of factorial
LeaNumworks Dec 27, 2018
9b07f19
[apps/code] Squares script template: start by resetting the turtle
LeaNumworks Jan 3, 2019
4f4ae4e
Merge branch 'master' into python_turtle
LeaNumworks Jan 8, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions escher/include/escher/palette.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Palette {
constexpr static KDColor Orange = KDColor::RGB24(0xfe871f);
constexpr static KDColor Green = KDColor::RGB24(0x50c102);
constexpr static KDColor GreenLight = KDColor::RGB24(0x52db8f);
constexpr static KDColor Brown = KDColor::RGB24(0x8d7350);
constexpr static KDColor Purple = KDColor::RGB24(0x6e2d79);
constexpr static KDColor DataColor[] = {Red, Blue, Green, YellowDark};
constexpr static KDColor DataColorLight[] = {RedLight, BlueLight, GreenLight, YellowLight};
};
Expand Down
2 changes: 2 additions & 0 deletions escher/src/palette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ constexpr KDColor Palette::BlueLight;
constexpr KDColor Palette::Orange;
constexpr KDColor Palette::Green;
constexpr KDColor Palette::GreenLight;
constexpr KDColor Palette::Brown;
constexpr KDColor Palette::Purple;
constexpr KDColor Palette::DataColor[];
constexpr KDColor Palette::DataColorLight[];
13 changes: 7 additions & 6 deletions python/port/genhdr/qstrdefs.in.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,21 @@ Q(set_pixel)

Q(turtle)

Q(reset)

Q(forward)
Q(fd)
Q(backward)
Q(bk)
Q(back)
Q(right)
Q(rt)
Q(left)
Q(lt)
Q(circle)
Q(goto)
Q(setpos)
Q(setposition)
Q(setheading)
Q(seth)
Q(circle)
Q(speed)

Q(position)
Expand All @@ -56,18 +55,20 @@ Q(down)
Q(penup)
Q(pu)
Q(up)
Q(isdown)
Q(pensize)
Q(width)
Q(isdown)

Q(pencolor)

Q(reset)

Q(showturtle)
Q(st)
Q(hideturtle)
Q(ht)
Q(isvisible)

Q(pencolor)

// utime QSTRs
Q(time)
Q(sleep)
Expand Down
47 changes: 43 additions & 4 deletions python/port/mod/turtle/modturtle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@ mp_obj_t modturtle_left(mp_obj_t angle) {
return mp_const_none;
}

mp_obj_t modturtle_circle(mp_obj_t radius, mp_obj_t angle) {
sTurtle.circle(mp_obj_get_int(radius), mp_obj_get_float(angle));
mp_obj_t modturtle_circle(size_t n_args, const mp_obj_t *args) {
mp_int_t radius = mp_obj_get_int(args[0]);

if (n_args == 1) {
sTurtle.circle(radius);
}
else {
mp_float_t angle = mp_obj_get_float(args[1]);
sTurtle.circle(radius, angle);
}
return mp_const_none;
}

Expand Down Expand Up @@ -112,8 +120,39 @@ mp_obj_t modturtle_isdown() {
return sTurtle.isPenDown() ? mp_const_true : mp_const_false;
}

mp_obj_t modturtle_color(mp_obj_t r, mp_obj_t g, mp_obj_t b) {
sTurtle.setColor(KDColor::RGB888(mp_obj_get_int(r), mp_obj_get_int(g), mp_obj_get_int(b)));
mp_obj_t modturtle_pencolor(size_t n_args, const mp_obj_t *args) {
if (n_args == 0) {
// pencolor()
KDColor c = sTurtle.color();
mp_obj_t mp_col[3];
mp_col[0] = mp_obj_new_int_from_uint(c.red());
mp_col[1] = mp_obj_new_int_from_uint(c.green());
mp_col[2] = mp_obj_new_int_from_uint(c.blue());
return mp_obj_new_tuple(3, mp_col);
}
if (n_args == 1) {
if (MP_OBJ_IS_STR(args[0])) {
// pencolor("blue")
size_t l;
sTurtle.setColor(mp_obj_str_get_data(args[0], &l));
} else {
// pencolor((r, g, b))
mp_obj_t * rgb;
mp_obj_get_array_fixed_n(args[0], 3, &rgb);
sTurtle.setColor(
KDColor::RGB888(
mp_obj_get_int(rgb[0]),
mp_obj_get_int(rgb[1]),
mp_obj_get_int(rgb[2])));
}
} else if (n_args == 3) {
// pencolor(r, g, b)
sTurtle.setColor(
KDColor::RGB888(
mp_obj_get_int(args[0]),
mp_obj_get_int(args[1]),
mp_obj_get_int(args[2])));
}
return mp_const_none;
}

Expand Down
4 changes: 2 additions & 2 deletions python/port/mod/turtle/modturtle.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mp_obj_t modturtle_forward(mp_obj_t px);
mp_obj_t modturtle_backward(mp_obj_t px);
mp_obj_t modturtle_right(mp_obj_t deg);
mp_obj_t modturtle_left(mp_obj_t deg);
mp_obj_t modturtle_circle(mp_obj_t radius, mp_obj_t angle);
mp_obj_t modturtle_circle(size_t n_args, const mp_obj_t *args);
mp_obj_t modturtle_goto(size_t n_args, const mp_obj_t *args);
mp_obj_t modturtle_setheading(mp_obj_t deg);
mp_obj_t modturtle_speed(mp_obj_t speed);
Expand All @@ -23,7 +23,7 @@ mp_obj_t modturtle_isdown();
mp_obj_t modturtle_pensize(size_t n_args, const mp_obj_t *args);
mp_obj_t modturtle_isvisible();

mp_obj_t modturtle_color(mp_obj_t r, mp_obj_t g, mp_obj_t b);
mp_obj_t modturtle_pencolor(size_t n_args, const mp_obj_t *args);

mp_obj_t modturtle_showturtle();
mp_obj_t modturtle_hideturtle();
22 changes: 12 additions & 10 deletions python/port/mod/turtle/modturtle_table.c
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#include "modturtle.h"

STATIC MP_DEFINE_CONST_FUN_OBJ_0(modturtle_reset_obj, modturtle_reset);

STATIC MP_DEFINE_CONST_FUN_OBJ_1(modturtle_forward_obj, modturtle_forward);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(modturtle_backward_obj, modturtle_backward);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(modturtle_right_obj, modturtle_right);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(modturtle_left_obj, modturtle_left);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(modturtle_circle_obj, modturtle_circle);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modturtle_goto_obj, 1, 2, modturtle_goto);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(modturtle_setheading_obj, modturtle_setheading);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modturtle_circle_obj, 1, 2, modturtle_circle);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(modturtle_speed_obj, modturtle_speed);

STATIC MP_DEFINE_CONST_FUN_OBJ_0(modturtle_position_obj, modturtle_position);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(modturtle_heading_obj, modturtle_heading);

STATIC MP_DEFINE_CONST_FUN_OBJ_0(modturtle_pendown_obj, modturtle_pendown);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(modturtle_penup_obj, modturtle_penup);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(modturtle_isdown_obj, modturtle_isdown);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modturtle_pensize_obj, 0, 1, modturtle_pensize);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(modturtle_isdown_obj, modturtle_isdown);

STATIC MP_DEFINE_CONST_FUN_OBJ_3(modturtle_color_obj, modturtle_color);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modturtle_pencolor_obj, 0, 3, modturtle_pencolor);

STATIC MP_DEFINE_CONST_FUN_OBJ_0(modturtle_reset_obj, modturtle_reset);

STATIC MP_DEFINE_CONST_FUN_OBJ_0(modturtle_showturtle_obj, modturtle_showturtle);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(modturtle_hideturtle_obj, modturtle_hideturtle);
Expand All @@ -31,22 +31,21 @@ STATIC const mp_rom_map_elem_t modturtle_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_turtle) },
{ MP_ROM_QSTR(MP_QSTR___init__), (mp_obj_t)&modturtle___init___obj },

{ MP_ROM_QSTR(MP_QSTR_reset), (mp_obj_t)&modturtle_reset_obj },

{ MP_ROM_QSTR(MP_QSTR_forward), (mp_obj_t)&modturtle_forward_obj },
{ MP_ROM_QSTR(MP_QSTR_fd), (mp_obj_t)&modturtle_forward_obj },
{ MP_ROM_QSTR(MP_QSTR_backward), (mp_obj_t)&modturtle_backward_obj },
{ MP_ROM_QSTR(MP_QSTR_bk), (mp_obj_t)&modturtle_backward_obj },
{ MP_ROM_QSTR(MP_QSTR_back), (mp_obj_t)&modturtle_backward_obj },
{ MP_ROM_QSTR(MP_QSTR_right), (mp_obj_t)&modturtle_right_obj },
{ MP_ROM_QSTR(MP_QSTR_rt), (mp_obj_t)&modturtle_right_obj },
{ MP_ROM_QSTR(MP_QSTR_left), (mp_obj_t)&modturtle_left_obj },
{ MP_ROM_QSTR(MP_QSTR_lt), (mp_obj_t)&modturtle_left_obj },
{ MP_ROM_QSTR(MP_QSTR_circle), (mp_obj_t)&modturtle_circle_obj },
{ MP_ROM_QSTR(MP_QSTR_goto), (mp_obj_t)&modturtle_goto_obj },
{ MP_ROM_QSTR(MP_QSTR_setpos), (mp_obj_t)&modturtle_goto_obj },
{ MP_ROM_QSTR(MP_QSTR_setposition), (mp_obj_t)&modturtle_goto_obj },
{ MP_ROM_QSTR(MP_QSTR_setheading), (mp_obj_t)&modturtle_setheading_obj },
{ MP_ROM_QSTR(MP_QSTR_seth), (mp_obj_t)&modturtle_setheading_obj },
{ MP_ROM_QSTR(MP_QSTR_circle), (mp_obj_t)&modturtle_circle_obj },
{ MP_ROM_QSTR(MP_QSTR_speed), (mp_obj_t)&modturtle_speed_obj },

{ MP_ROM_QSTR(MP_QSTR_position), (mp_obj_t)&modturtle_position_obj },
Expand All @@ -59,11 +58,14 @@ STATIC const mp_rom_map_elem_t modturtle_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_penup), (mp_obj_t)&modturtle_penup_obj },
{ MP_ROM_QSTR(MP_QSTR_pu), (mp_obj_t)&modturtle_penup_obj },
{ MP_ROM_QSTR(MP_QSTR_up), (mp_obj_t)&modturtle_penup_obj },
{ MP_ROM_QSTR(MP_QSTR_isdown), (mp_obj_t)&modturtle_isdown_obj },
{ MP_ROM_QSTR(MP_QSTR_pensize), (mp_obj_t)&modturtle_pensize_obj },
{ MP_ROM_QSTR(MP_QSTR_width), (mp_obj_t)&modturtle_pensize_obj },
{ MP_ROM_QSTR(MP_QSTR_isdown), (mp_obj_t)&modturtle_isdown_obj },

{ MP_ROM_QSTR(MP_QSTR_color), (mp_obj_t)&modturtle_pencolor_obj },
{ MP_ROM_QSTR(MP_QSTR_pencolor), (mp_obj_t)&modturtle_pencolor_obj },

{ MP_ROM_QSTR(MP_QSTR_pencolor), (mp_obj_t)&modturtle_color_obj },
{ MP_ROM_QSTR(MP_QSTR_reset), (mp_obj_t)&modturtle_reset_obj },

{ MP_ROM_QSTR(MP_QSTR_showturtle), (mp_obj_t)&modturtle_showturtle_obj },
{ MP_ROM_QSTR(MP_QSTR_st), (mp_obj_t)&modturtle_showturtle_obj },
Expand Down
Loading