kopia lustrzana https://github.com/pimoroni/pimoroni-pico
Completed mpy wrapper for pico_display, and included example
rodzic
75560ea038
commit
84e98b7040
|
@ -131,11 +131,9 @@ namespace pimoroni {
|
|||
}
|
||||
}
|
||||
|
||||
void PicoGraphics::text(const std::string &t, const point &p, int32_t wrap) {
|
||||
void PicoGraphics::text(const std::string &t, const point &p, int32_t wrap, uint8_t scale) {
|
||||
uint32_t co = 0, lo = 0; // character and line (if wrapping) offset
|
||||
|
||||
uint8_t scale = 2;
|
||||
|
||||
size_t i = 0;
|
||||
while(i < t.length()) {
|
||||
// find length of current word
|
||||
|
|
|
@ -67,8 +67,8 @@ namespace pimoroni {
|
|||
void pixel_span(const point &p, int32_t l);
|
||||
void rectangle(const rect &r);
|
||||
void circle(const point &p, int32_t r);
|
||||
void text(const std::string &t, const point &p, int32_t wrap);
|
||||
void character(const char c, const point &p, uint8_t scale);
|
||||
void character(const char c, const point &p, uint8_t scale = 2);
|
||||
void text(const std::string &t, const point &p, int32_t wrap, uint8_t scale = 2);
|
||||
//void polygon(std::vector);
|
||||
};
|
||||
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
import time, random
|
||||
import picodisplay as display
|
||||
display.init()
|
||||
display.set_backlight(1.0)
|
||||
i = 0
|
||||
width = display.get_width()
|
||||
height = display.get_height()
|
||||
|
||||
class Ball:
|
||||
def __init__(self, x, y, r, dx, dy, pen):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.r = r
|
||||
self.dx = dx
|
||||
self.dy = dy
|
||||
self.pen = pen
|
||||
|
||||
# initialise shapes
|
||||
balls = []
|
||||
for i in range(0, 100):
|
||||
balls.append(
|
||||
Ball(
|
||||
random.randint(0, width),
|
||||
random.randint(0, height),
|
||||
random.randint(0, 10) + 3,
|
||||
random.randint(0, 255) / 128,
|
||||
random.randint(0, 255) / 128,
|
||||
display.create_pen(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
|
||||
)
|
||||
)
|
||||
|
||||
while True:
|
||||
display.set_pen(120, 40, 60)
|
||||
display.clear()
|
||||
|
||||
for ball in balls:
|
||||
ball.x += ball.dx
|
||||
ball.y += ball.dy
|
||||
|
||||
if ball.x < 0 or ball.x > width:
|
||||
ball.dx *= -1
|
||||
if ball.y < 0 or ball.y > height:
|
||||
ball.dy *= -1
|
||||
|
||||
display.set_pen(ball.pen)
|
||||
display.circle(int(ball.x), int(ball.y), int(ball.r))
|
||||
|
||||
display.update()
|
||||
time.sleep(0.01)
|
|
@ -24,21 +24,17 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(picodisplay_set_led_obj, picodisplay_set_led);
|
|||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(picodisplay_is_pressed_obj, picodisplay_is_pressed);
|
||||
|
||||
//From PicoGraphics parent class
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(picodisplay_set_pen_rgb_obj, picodisplay_set_pen_rgb);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(picodisplay_set_pen_obj, picodisplay_set_pen);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picodisplay_set_pen_obj, 1, 3, picodisplay_set_pen);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(picodisplay_create_pen_obj, picodisplay_create_pen);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(picodisplay_set_clip_obj, picodisplay_set_clip);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picodisplay_set_clip_obj, 4, 4, picodisplay_set_clip);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(picodisplay_remove_clip_obj, picodisplay_remove_clip);
|
||||
//STATIC MP_DEFINE_CONST_FUN_OBJ_1(picodisplay_ptr_obj, picodisplay_ptr);
|
||||
//STATIC MP_DEFINE_CONST_FUN_OBJ_1(picodisplay_ptr_obj, picodisplay_ptr);
|
||||
//STATIC MP_DEFINE_CONST_FUN_OBJ_2(picodisplay_ptr_obj, picodisplay_ptr);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(picodisplay_clear_obj, picodisplay_clear);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(picodisplay_pixel_obj, picodisplay_pixel);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(picodisplay_pixel_span_obj, picodisplay_pixel_span);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(picodisplay_rectangle_obj, picodisplay_rectangle);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(picodisplay_circle_obj, picodisplay_circle);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(picodisplay_text_obj, picodisplay_text);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(picodisplay_character_obj, picodisplay_character);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(picodisplay_pixel_obj, picodisplay_pixel);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(picodisplay_pixel_span_obj, picodisplay_pixel_span);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picodisplay_rectangle_obj, 4, 4, picodisplay_rectangle);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(picodisplay_circle_obj, picodisplay_circle);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picodisplay_character_obj, 3, 4, picodisplay_character);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picodisplay_text_obj, 4, 5, picodisplay_text);
|
||||
|
||||
|
||||
/***** Globals Table *****/
|
||||
|
@ -52,19 +48,17 @@ STATIC const mp_map_elem_t picodisplay_globals_table[] = {
|
|||
{ MP_ROM_QSTR(MP_QSTR_set_led), MP_ROM_PTR(&picodisplay_set_led_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_is_pressed), MP_ROM_PTR(&picodisplay_is_pressed_obj) },
|
||||
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_pen_rgb), MP_ROM_PTR(&picodisplay_set_pen_rgb_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_pen), MP_ROM_PTR(&picodisplay_set_pen_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_create_pen), MP_ROM_PTR(&picodisplay_create_pen_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_clip), MP_ROM_PTR(&picodisplay_set_clip_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_remove_clip), MP_ROM_PTR(&picodisplay_remove_clip_obj) },
|
||||
//
|
||||
{ MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&picodisplay_clear_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pixel), MP_ROM_PTR(&picodisplay_pixel_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pixel_span), MP_ROM_PTR(&picodisplay_pixel_span_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_rectangle), MP_ROM_PTR(&picodisplay_rectangle_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_circle), MP_ROM_PTR(&picodisplay_circle_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&picodisplay_text_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_character), MP_ROM_PTR(&picodisplay_character_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&picodisplay_text_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_BUTTON_A), MP_ROM_INT(BUTTON_A) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_BUTTON_B), MP_ROM_INT(BUTTON_B) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_BUTTON_X), MP_ROM_INT(BUTTON_X) },
|
||||
|
|
|
@ -88,63 +88,169 @@ mp_obj_t picodisplay_is_pressed(mp_obj_t button_obj) {
|
|||
return buttonPressed ? mp_const_true : mp_const_false;
|
||||
}
|
||||
|
||||
mp_obj_t picodisplay_set_pen_rgb(mp_obj_t r_obj, mp_obj_t g_obj, mp_obj_t b_obj) {
|
||||
return mp_const_none;
|
||||
}
|
||||
mp_obj_t picodisplay_set_pen(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
switch(n_args)
|
||||
{
|
||||
case 1: {
|
||||
int p = mp_obj_get_int(args[0]);
|
||||
|
||||
if(p < 0 || p > 0xffff)
|
||||
mp_raise_ValueError("p is not a valid pen.");
|
||||
else
|
||||
display.set_pen(p);
|
||||
} break;
|
||||
|
||||
case 3: {
|
||||
int r = mp_obj_get_int(args[0]);
|
||||
int g = mp_obj_get_int(args[1]);
|
||||
int b = mp_obj_get_int(args[2]);
|
||||
|
||||
if(r < 0 || r > 255)
|
||||
mp_raise_ValueError("r out of range. Expected 0 to 255");
|
||||
else if(g < 0 || g > 255)
|
||||
mp_raise_ValueError("g out of range. Expected 0 to 255");
|
||||
else if(b < 0 || b > 255)
|
||||
mp_raise_ValueError("b out of range. Expected 0 to 255");
|
||||
else
|
||||
display.set_pen(r, g, b);
|
||||
} break;
|
||||
|
||||
default: {
|
||||
char *buffer;
|
||||
buffer = (char*)malloc(100);
|
||||
sprintf(buffer, "function takes 1 or 3 (r,g,b) positional arguments but %d were given", n_args);
|
||||
mp_raise_TypeError(buffer);
|
||||
} break;
|
||||
}
|
||||
|
||||
mp_obj_t picodisplay_set_pen(mp_obj_t p_obj) {
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t picodisplay_create_pen(mp_obj_t r_obj, mp_obj_t g_obj, mp_obj_t b_obj) {
|
||||
return mp_const_none;
|
||||
int r = mp_obj_get_int(r_obj);
|
||||
int g = mp_obj_get_int(g_obj);
|
||||
int b = mp_obj_get_int(b_obj);
|
||||
|
||||
int pen = 0;
|
||||
if(r < 0 || r > 255)
|
||||
mp_raise_ValueError("r out of range. Expected 0 to 255");
|
||||
else if(g < 0 || g > 255)
|
||||
mp_raise_ValueError("g out of range. Expected 0 to 255");
|
||||
else if(b < 0 || b > 255)
|
||||
mp_raise_ValueError("b out of range. Expected 0 to 255");
|
||||
else
|
||||
pen = display.create_pen(r, g, b);
|
||||
|
||||
return mp_obj_new_int(pen);
|
||||
}
|
||||
|
||||
mp_obj_t picodisplay_set_clip(mp_obj_t r_obj) {
|
||||
mp_obj_t picodisplay_set_clip(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
(void)n_args; //Unused input parameter, we know it's 4
|
||||
|
||||
int x = mp_obj_get_int(args[0]);
|
||||
int y = mp_obj_get_int(args[1]);
|
||||
int w = mp_obj_get_int(args[2]);
|
||||
int h = mp_obj_get_int(args[3]);
|
||||
|
||||
rect r(x, y, w, h);
|
||||
display.set_clip(r);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t picodisplay_remove_clip() {
|
||||
display.remove_clip();
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
// mp_obj_t picodisplay_ptr(mp_obj_t p_obj) {
|
||||
// return mp_const_none;
|
||||
// }
|
||||
|
||||
// mp_obj_t picodisplay_ptr(mp_obj_t r_obj) {
|
||||
// return mp_const_none;
|
||||
// }
|
||||
|
||||
// mp_obj_t picodisplay_ptr(mp_obj_t x_obj, mp_obj_t y_obj) {
|
||||
// return mp_const_none;
|
||||
// }
|
||||
|
||||
mp_obj_t picodisplay_clear() {
|
||||
display.clear();
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t picodisplay_pixel(mp_obj_t p_obj) {
|
||||
mp_obj_t picodisplay_pixel(mp_obj_t x_obj, mp_obj_t y_obj) {
|
||||
int x = mp_obj_get_int(x_obj);
|
||||
int y = mp_obj_get_int(y_obj);
|
||||
|
||||
point p(x, y);
|
||||
display.pixel(p);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t picodisplay_pixel_span(mp_obj_t p_obj, mp_obj_t l_obj) {
|
||||
mp_obj_t picodisplay_pixel_span(mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t l_obj) {
|
||||
int x = mp_obj_get_int(x_obj);
|
||||
int y = mp_obj_get_int(y_obj);
|
||||
int l = mp_obj_get_int(l_obj);
|
||||
|
||||
point p(x, y);
|
||||
display.pixel_span(p, l);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t picodisplay_rectangle(mp_obj_t r_obj) {
|
||||
mp_obj_t picodisplay_rectangle(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
(void)n_args; //Unused input parameter, we know it's 4
|
||||
|
||||
int x = mp_obj_get_int(args[0]);
|
||||
int y = mp_obj_get_int(args[1]);
|
||||
int w = mp_obj_get_int(args[2]);
|
||||
int h = mp_obj_get_int(args[3]);
|
||||
|
||||
rect r(x, y, w, h);
|
||||
display.rectangle(r);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t picodisplay_circle(mp_obj_t p_obj, mp_obj_t r_obj) {
|
||||
mp_obj_t picodisplay_circle(mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t r_obj) {
|
||||
int x = mp_obj_get_int(x_obj);
|
||||
int y = mp_obj_get_int(y_obj);
|
||||
int r = mp_obj_get_int(r_obj);
|
||||
|
||||
point p(x, y);
|
||||
display.circle(p, r);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t picodisplay_text(mp_obj_t t_obj, mp_obj_t p_obj, mp_obj_t wrap_obj) {
|
||||
mp_obj_t picodisplay_character(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
|
||||
int c = mp_obj_get_int(args[0]);
|
||||
int x = mp_obj_get_int(args[1]);
|
||||
int y = mp_obj_get_int(args[2]);
|
||||
|
||||
point p(x, y);
|
||||
if(n_args == 4) {
|
||||
int scale = mp_obj_get_int(args[3]);
|
||||
display.character((char)c, p, scale);
|
||||
}
|
||||
else
|
||||
display.character((char)c, p);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t picodisplay_character(mp_obj_t c_obj, mp_obj_t p_obj, mp_obj_t scale_obj) {
|
||||
mp_obj_t picodisplay_text(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
(void)n_args; //Unused input parameter, we know it's 4
|
||||
|
||||
mp_check_self(mp_obj_is_str_or_bytes(args[0]));
|
||||
GET_STR_DATA_LEN(args[0], str, str_len);
|
||||
|
||||
std::string t((const char*)str);
|
||||
|
||||
int x = mp_obj_get_int(args[1]);
|
||||
int y = mp_obj_get_int(args[2]);
|
||||
int wrap = mp_obj_get_int(args[3]);
|
||||
|
||||
point p(x, y);
|
||||
if(n_args == 5) {
|
||||
int scale = mp_obj_get_int(args[4]);
|
||||
display.text(t, p, wrap, scale);
|
||||
}
|
||||
else
|
||||
display.text(t, p, wrap);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
// Include MicroPython API.
|
||||
#include "py/runtime.h"
|
||||
#include "py/objstr.h"
|
||||
|
||||
// Declare the functions we'll make available in Python
|
||||
extern mp_obj_t picodisplay_init();
|
||||
|
@ -11,21 +12,14 @@ extern mp_obj_t picodisplay_set_led(mp_obj_t r_obj, mp_obj_t g_obj, mp_obj_t b_o
|
|||
extern mp_obj_t picodisplay_is_pressed(mp_obj_t button_obj);
|
||||
|
||||
// From PicoGraphics parent class
|
||||
extern mp_obj_t picodisplay_set_pen_rgb(mp_obj_t r_obj, mp_obj_t g_obj, mp_obj_t b_obj);
|
||||
extern mp_obj_t picodisplay_set_pen(mp_obj_t p_obj);
|
||||
extern mp_obj_t picodisplay_set_pen(mp_uint_t n_args, const mp_obj_t *args);
|
||||
extern mp_obj_t picodisplay_create_pen(mp_obj_t r_obj, mp_obj_t g_obj, mp_obj_t b_obj);
|
||||
|
||||
extern mp_obj_t picodisplay_set_clip(mp_obj_t r_obj);
|
||||
extern mp_obj_t picodisplay_set_clip(mp_uint_t n_args, const mp_obj_t *args);
|
||||
extern mp_obj_t picodisplay_remove_clip();
|
||||
|
||||
//extern mp_obj_t picodisplay_ptr(mp_obj_t p_obj);
|
||||
//extern mp_obj_t picodisplay_ptr(mp_obj_t r_obj);
|
||||
//extern mp_obj_t picodisplay_ptr(mp_obj_t x_obj, mp_obj_t y_obj);
|
||||
|
||||
extern mp_obj_t picodisplay_clear();
|
||||
extern mp_obj_t picodisplay_pixel(mp_obj_t p_obj);
|
||||
extern mp_obj_t picodisplay_pixel_span(mp_obj_t p_obj, mp_obj_t l_obj);
|
||||
extern mp_obj_t picodisplay_rectangle(mp_obj_t r_obj);
|
||||
extern mp_obj_t picodisplay_circle(mp_obj_t p_obj, mp_obj_t r_obj);
|
||||
extern mp_obj_t picodisplay_text(mp_obj_t t_obj, mp_obj_t p_obj, mp_obj_t wrap_obj);
|
||||
extern mp_obj_t picodisplay_character(mp_obj_t c_obj, mp_obj_t p_obj, mp_obj_t scale_obj);
|
||||
extern mp_obj_t picodisplay_pixel(mp_obj_t x_obj, mp_obj_t y_obj);
|
||||
extern mp_obj_t picodisplay_pixel_span(mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t l_obj);
|
||||
extern mp_obj_t picodisplay_rectangle(mp_uint_t n_args, const mp_obj_t *args);
|
||||
extern mp_obj_t picodisplay_circle(mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t r_obj);
|
||||
extern mp_obj_t picodisplay_character(mp_uint_t n_args, const mp_obj_t *args);
|
||||
extern mp_obj_t picodisplay_text(mp_uint_t n_args, const mp_obj_t *args);
|
Ładowanie…
Reference in New Issue