2022-06-07 16:37:06 +01:00
|
|
|
#include "drivers/st7789/st7789.hpp"
|
|
|
|
#include "drivers/st7735/st7735.hpp"
|
|
|
|
#include "libraries/pico_graphics/pico_graphics.hpp"
|
2022-05-28 01:00:56 +01:00
|
|
|
#include "common/pimoroni_common.hpp"
|
|
|
|
#include "common/pimoroni_bus.hpp"
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-05-20 15:45:25 +01:00
|
|
|
#include "micropython/modules/util.hpp"
|
2022-03-21 17:02:46 +00:00
|
|
|
|
|
|
|
using namespace pimoroni;
|
|
|
|
|
|
|
|
extern "C" {
|
2022-06-07 16:37:06 +01:00
|
|
|
#include "picographics.h"
|
2022-05-28 23:00:43 +01:00
|
|
|
#include "micropython/modules/pimoroni_bus/pimoroni_bus.h"
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 20:19:16 +01:00
|
|
|
std::string mp_obj_to_string_r(const mp_obj_t &obj) {
|
|
|
|
if(mp_obj_is_str_or_bytes(obj)) {
|
|
|
|
GET_STR_DATA_LEN(obj, str, str_len);
|
|
|
|
return (const char*)str;
|
|
|
|
}
|
|
|
|
mp_raise_TypeError("can't convert object to str implicitly");
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
typedef struct _ModPicoGraphics_obj_t {
|
2022-03-21 17:02:46 +00:00
|
|
|
mp_obj_base_t base;
|
2022-06-07 16:37:06 +01:00
|
|
|
PicoGraphics *graphics;
|
|
|
|
DisplayDriver *display;
|
2022-05-26 11:51:15 +01:00
|
|
|
void *buffer;
|
2022-06-07 16:37:06 +01:00
|
|
|
} ModPicoGraphics_obj_t;
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
|
|
|
ModPicoGraphics_obj_t *self = nullptr;
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
enum { ARG_display, ARG_rotate, ARG_bus, ARG_buffer, ARG_pen_type };
|
2022-05-28 01:00:56 +01:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_display, MP_ARG_INT | MP_ARG_REQUIRED },
|
|
|
|
{ MP_QSTR_rotate, MP_ARG_INT, { .u_int = Rotation::ROTATE_0 } },
|
2022-05-28 23:00:43 +01:00
|
|
|
{ MP_QSTR_bus, MP_ARG_OBJ, { .u_obj = mp_const_none } },
|
2022-05-28 01:00:56 +01:00
|
|
|
{ MP_QSTR_buffer, MP_ARG_OBJ, { .u_obj = mp_const_none } },
|
2022-06-07 16:37:06 +01:00
|
|
|
{ MP_QSTR_pen_type, MP_ARG_INT, { .u_int = PEN_RGB332 } },
|
2022-05-28 01:00:56 +01:00
|
|
|
};
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-05-28 01:00:56 +01:00
|
|
|
// Parse args.
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
self = m_new_obj(ModPicoGraphics_obj_t);
|
|
|
|
self->base.type = &ModPicoGraphics_type;
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-05-28 01:00:56 +01:00
|
|
|
Rotation rotate = (Rotation)args[ARG_rotate].u_int;
|
|
|
|
bool round = false;
|
|
|
|
int width = 0;
|
|
|
|
int height = 0;
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
PicoGraphicsPenType pen_type = (PicoGraphicsPenType)args[ARG_pen_type].u_int;
|
|
|
|
PicoGraphicsDisplay display = (PicoGraphicsDisplay)args[ARG_display].u_int;
|
2022-05-28 01:00:56 +01:00
|
|
|
|
|
|
|
switch(display) {
|
|
|
|
case DISPLAY_PICO_DISPLAY:
|
|
|
|
width = 240;
|
|
|
|
height = 135;
|
|
|
|
break;
|
|
|
|
case DISPLAY_PICO_DISPLAY_2:
|
|
|
|
case DISPLAY_TUFTY_2040:
|
|
|
|
width = 320;
|
|
|
|
height = 240;
|
|
|
|
break;
|
|
|
|
case DISPLAY_PICO_EXPLORER:
|
|
|
|
case DISPLAY_LCD_240X240:
|
|
|
|
case DISPLAY_ENVIRO_PLUS:
|
|
|
|
width = 240;
|
|
|
|
height = 240;
|
|
|
|
break;
|
|
|
|
case DISPLAY_ROUND_LCD_240X240:
|
|
|
|
width = 240;
|
|
|
|
height = 240;
|
|
|
|
round = true;
|
|
|
|
break;
|
2022-06-07 16:37:06 +01:00
|
|
|
case DISPLAY_LCD_160X80:
|
|
|
|
width = 160;
|
|
|
|
height = 80;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mp_raise_ValueError("Unsupported display!");
|
|
|
|
break; // unreachable
|
2022-05-28 01:00:56 +01:00
|
|
|
}
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
// Try to create an appropriate display driver
|
2022-05-28 01:00:56 +01:00
|
|
|
if (display == DISPLAY_TUFTY_2040) {
|
2022-05-28 23:00:43 +01:00
|
|
|
if (args[ARG_bus].u_obj == mp_const_none) {
|
2022-06-07 16:37:06 +01:00
|
|
|
self->display = m_new_class(ST7789, width, height, rotate, {10, 11, 12, 13, 14, 2});
|
2022-05-28 23:00:43 +01:00
|
|
|
} else if (mp_obj_is_type(args[ARG_bus].u_obj, &ParallelPins_type)) {
|
|
|
|
_PimoroniBus_obj_t *bus = (_PimoroniBus_obj_t *)MP_OBJ_TO_PTR(args[ARG_bus].u_obj);
|
2022-06-07 16:37:06 +01:00
|
|
|
self->display = m_new_class(ST7789, width, height, rotate, *(ParallelPins *)(bus->pins));
|
2022-05-28 23:00:43 +01:00
|
|
|
} else {
|
|
|
|
mp_raise_ValueError("ParallelBus expected!");
|
2022-03-21 17:02:46 +00:00
|
|
|
}
|
2022-06-07 16:37:06 +01:00
|
|
|
} else if (display == DISPLAY_LCD_160X80) {
|
|
|
|
if (args[ARG_bus].u_obj == mp_const_none) {
|
|
|
|
self->display = m_new_class(ST7735, width, height, get_spi_pins(BG_SPI_FRONT));
|
|
|
|
} else if (mp_obj_is_type(args[ARG_bus].u_obj, &SPIPins_type)) {
|
|
|
|
_PimoroniBus_obj_t *bus = (_PimoroniBus_obj_t *)MP_OBJ_TO_PTR(args[ARG_bus].u_obj);
|
|
|
|
self->display = m_new_class(ST7735, width, height, *(SPIPins *)(bus->pins));
|
|
|
|
} else {
|
|
|
|
mp_raise_ValueError("SPIBus expected!");
|
|
|
|
}
|
2022-03-21 17:02:46 +00:00
|
|
|
} else {
|
2022-05-28 23:00:43 +01:00
|
|
|
if (args[ARG_bus].u_obj == mp_const_none) {
|
2022-06-07 16:37:06 +01:00
|
|
|
self->display = m_new_class(ST7789, width, height, rotate, round, get_spi_pins(BG_SPI_FRONT));
|
2022-05-28 23:00:43 +01:00
|
|
|
} else if (mp_obj_is_type(args[ARG_bus].u_obj, &SPIPins_type)) {
|
|
|
|
_PimoroniBus_obj_t *bus = (_PimoroniBus_obj_t *)MP_OBJ_TO_PTR(args[ARG_bus].u_obj);
|
2022-06-07 16:37:06 +01:00
|
|
|
self->display = m_new_class(ST7789, width, height, rotate, round, *(SPIPins *)(bus->pins));
|
2022-05-28 23:00:43 +01:00
|
|
|
} else {
|
|
|
|
mp_raise_ValueError("SPIBus expected!");
|
2022-05-25 12:25:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
// Create or fetch buffer
|
|
|
|
size_t required_size = 0;
|
|
|
|
switch(pen_type) {
|
|
|
|
case PEN_P4:
|
|
|
|
required_size = PicoGraphics_PenP4::buffer_size(width, height);
|
|
|
|
break;
|
|
|
|
case PEN_P8:
|
|
|
|
required_size = PicoGraphics_PenP8::buffer_size(width, height);
|
|
|
|
break;
|
|
|
|
case PEN_RGB332:
|
|
|
|
required_size = PicoGraphics_PenRGB332::buffer_size(width, height);
|
|
|
|
break;
|
|
|
|
case PEN_RGB565:
|
|
|
|
required_size = PicoGraphics_PenRGB565::buffer_size(width, height);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mp_raise_ValueError("Unsupported display!");
|
|
|
|
break; // unreachable
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args[ARG_buffer].u_obj != mp_const_none) {
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_RW);
|
|
|
|
self->buffer = bufinfo.buf;
|
|
|
|
if(bufinfo.len < (size_t)(required_size)) {
|
|
|
|
mp_raise_ValueError("Supplied buffer is too small!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self->buffer = m_new(uint8_t, required_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an instance of the graphics library
|
|
|
|
// use the *driver* width/height because they may have been swapped due to rotation
|
|
|
|
switch(pen_type) {
|
|
|
|
case PEN_P4:
|
|
|
|
self->graphics = m_new_class(PicoGraphics_PenP4, self->display->width, self->display->height, self->buffer);
|
|
|
|
break;
|
|
|
|
case PEN_P8:
|
|
|
|
self->graphics = m_new_class(PicoGraphics_PenP8, self->display->width, self->display->height, self->buffer);
|
|
|
|
break;
|
|
|
|
case PEN_RGB332:
|
|
|
|
self->graphics = m_new_class(PicoGraphics_PenRGB332, self->display->width, self->display->height, self->buffer);
|
|
|
|
break;
|
|
|
|
case PEN_RGB565:
|
|
|
|
self->graphics = m_new_class(PicoGraphics_PenRGB565, self->display->width, self->display->height, self->buffer);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the buffer
|
|
|
|
self->graphics->set_pen(0);
|
|
|
|
self->graphics->clear();
|
|
|
|
|
|
|
|
// Update the LCD from the graphics library
|
|
|
|
self->display->update(self->graphics);
|
|
|
|
|
2022-05-25 12:25:47 +01:00
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
2022-06-07 20:19:16 +01:00
|
|
|
mp_obj_t ModPicoGraphics_set_font(mp_obj_t self_in, mp_obj_t font) {
|
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(self_in, ModPicoGraphics_obj_t);
|
|
|
|
self->graphics->set_font(mp_obj_to_string_r(font));
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_set_framebuffer(mp_obj_t self_in, mp_obj_t framebuffer) {
|
|
|
|
(void)self_in;
|
|
|
|
(void)framebuffer;
|
|
|
|
/*
|
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(self_in, ModPicoGraphics_obj_t);
|
2022-05-27 16:52:43 +01:00
|
|
|
|
|
|
|
if (framebuffer == mp_const_none) {
|
2022-06-07 16:37:06 +01:00
|
|
|
m_del(uint8_t, self->buffer, self->graphics->bounds.w * self->graphics->bounds.h);
|
2022-05-27 16:52:43 +01:00
|
|
|
self->buffer = nullptr;
|
2022-06-07 16:37:06 +01:00
|
|
|
self->graphics->set_framebuffer(nullptr);
|
2022-05-27 16:52:43 +01:00
|
|
|
} else {
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(framebuffer, &bufinfo, MP_BUFFER_RW);
|
|
|
|
self->buffer = bufinfo.buf;
|
2022-06-07 16:37:06 +01:00
|
|
|
self->graphics->set_framebuffer(self->buffer);
|
2022-05-27 16:52:43 +01:00
|
|
|
}
|
2022-06-07 16:37:06 +01:00
|
|
|
*/
|
2022-03-21 17:02:46 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_get_bounds(mp_obj_t self_in) {
|
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(self_in, ModPicoGraphics_obj_t);
|
2022-06-06 12:05:44 +01:00
|
|
|
mp_obj_t tuple[2] = {
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_new_int(self->graphics->bounds.w),
|
|
|
|
mp_obj_new_int(self->graphics->bounds.h)
|
2022-06-06 12:05:44 +01:00
|
|
|
};
|
2022-05-28 01:00:56 +01:00
|
|
|
return mp_obj_new_tuple(2, tuple);
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_update(mp_obj_t self_in) {
|
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(self_in, ModPicoGraphics_obj_t);
|
|
|
|
self->display->update(self->graphics);
|
2022-05-26 11:51:15 +01:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_set_backlight(mp_obj_t self_in, mp_obj_t brightness) {
|
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(self_in, ModPicoGraphics_obj_t);
|
2022-05-27 16:52:43 +01:00
|
|
|
|
|
|
|
float b = mp_obj_get_float(brightness);
|
|
|
|
|
2022-06-06 12:05:44 +01:00
|
|
|
if(b < 0 || b > 1.0f) mp_raise_ValueError("brightness out of range. Expected 0.0 to 1.0");
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
self->display->set_backlight((uint8_t)(b * 255.0f));
|
2022-05-26 11:51:15 +01:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_module_RGB332(mp_obj_t r, mp_obj_t g, mp_obj_t b) {
|
|
|
|
return mp_obj_new_int(PicoGraphics::rgb_to_rgb332(
|
2022-05-27 16:52:43 +01:00
|
|
|
mp_obj_get_int(r),
|
|
|
|
mp_obj_get_int(g),
|
|
|
|
mp_obj_get_int(b)
|
|
|
|
));
|
|
|
|
}
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_module_RGB565(mp_obj_t r, mp_obj_t g, mp_obj_t b) {
|
|
|
|
return mp_obj_new_int(PicoGraphics::rgb_to_rgb565(
|
2022-05-27 16:52:43 +01:00
|
|
|
mp_obj_get_int(r),
|
|
|
|
mp_obj_get_int(g),
|
|
|
|
mp_obj_get_int(b)
|
|
|
|
));
|
|
|
|
}
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_set_pen(mp_obj_t self_in, mp_obj_t pen) {
|
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(self_in, ModPicoGraphics_obj_t);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
self->graphics->set_pen(mp_obj_get_int(pen));
|
2022-03-21 17:02:46 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_reset_pen(mp_obj_t self_in, mp_obj_t pen) {
|
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(self_in, ModPicoGraphics_obj_t);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
self->graphics->reset_pen(mp_obj_get_int(pen));
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-05-27 16:52:43 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_update_pen(size_t n_args, const mp_obj_t *args) {
|
2022-06-06 17:54:15 +01:00
|
|
|
enum { ARG_self, ARG_i, ARG_r, ARG_g, ARG_b };
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self], ModPicoGraphics_obj_t);
|
2022-05-28 18:08:47 +01:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
self->graphics->update_pen(
|
2022-06-07 11:03:38 +01:00
|
|
|
mp_obj_get_int(args[ARG_i]) & 0xff,
|
|
|
|
mp_obj_get_int(args[ARG_r]) & 0xff,
|
|
|
|
mp_obj_get_int(args[ARG_g]) & 0xff,
|
|
|
|
mp_obj_get_int(args[ARG_b]) & 0xff
|
2022-06-06 17:54:15 +01:00
|
|
|
);
|
2022-05-28 18:08:47 +01:00
|
|
|
|
2022-06-06 17:54:15 +01:00
|
|
|
return mp_const_none;
|
2022-05-28 18:08:47 +01:00
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_create_pen(size_t n_args, const mp_obj_t *args) {
|
2022-05-27 16:52:43 +01:00
|
|
|
enum { ARG_self, ARG_r, ARG_g, ARG_b };
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self], ModPicoGraphics_obj_t);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
int result = self->graphics->create_pen(
|
2022-06-07 11:03:38 +01:00
|
|
|
mp_obj_get_int(args[ARG_r]) & 0xff,
|
|
|
|
mp_obj_get_int(args[ARG_g]) & 0xff,
|
|
|
|
mp_obj_get_int(args[ARG_b]) & 0xff
|
2022-05-27 16:52:43 +01:00
|
|
|
);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-06 12:05:44 +01:00
|
|
|
if (result == -1) mp_raise_ValueError("create_pen failed. No matching colour or space in palette!");
|
2022-05-27 16:52:43 +01:00
|
|
|
|
|
|
|
return mp_obj_new_int(result);
|
2022-03-21 17:02:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 20:19:16 +01:00
|
|
|
mp_obj_t ModPicoGraphics_set_palette(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
size_t num_tuples = n_args - 1;
|
|
|
|
const mp_obj_t *tuples = pos_args + 1;
|
|
|
|
|
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(pos_args[0], ModPicoGraphics_obj_t);
|
|
|
|
|
|
|
|
// Check if there is only one argument, which might be a list
|
|
|
|
if(n_args == 2) {
|
|
|
|
if(mp_obj_is_type(pos_args[1], &mp_type_list)) {
|
|
|
|
mp_obj_list_t *points = MP_OBJ_TO_PTR2(pos_args[1], mp_obj_list_t);
|
|
|
|
|
|
|
|
if(points->len <= 0) mp_raise_ValueError("set_palette(): cannot provide an empty list");
|
|
|
|
|
|
|
|
num_tuples = points->len;
|
|
|
|
tuples = points->items;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mp_raise_TypeError("set_palette(): can't convert object to list");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(size_t i = 0; i < num_tuples; i++) {
|
|
|
|
mp_obj_t obj = tuples[i];
|
|
|
|
if(!mp_obj_is_type(obj, &mp_type_tuple)) mp_raise_ValueError("set_palette(): can't convert object to tuple");
|
|
|
|
|
|
|
|
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR2(obj, mp_obj_tuple_t);
|
|
|
|
|
|
|
|
if(tuple->len != 3) mp_raise_ValueError("set_palette(): tuple must contain R, G, B values");
|
|
|
|
|
|
|
|
self->graphics->update_pen(
|
|
|
|
i,
|
|
|
|
mp_obj_get_int(tuple->items[0]),
|
|
|
|
mp_obj_get_int(tuple->items[1]),
|
|
|
|
mp_obj_get_int(tuple->items[2])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_set_clip(size_t n_args, const mp_obj_t *args) {
|
2022-03-21 17:02:46 +00:00
|
|
|
enum { ARG_self, ARG_x, ARG_y, ARG_w, ARG_h };
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self], ModPicoGraphics_obj_t);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
self->graphics->set_clip({
|
2022-06-07 11:03:38 +01:00
|
|
|
mp_obj_get_int(args[ARG_x]),
|
|
|
|
mp_obj_get_int(args[ARG_y]),
|
|
|
|
mp_obj_get_int(args[ARG_w]),
|
|
|
|
mp_obj_get_int(args[ARG_h])
|
|
|
|
});
|
2022-03-21 17:02:46 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_remove_clip(mp_obj_t self_in) {
|
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(self_in, ModPicoGraphics_obj_t);
|
2022-06-07 11:03:38 +01:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
self->graphics->remove_clip();
|
2022-03-21 17:02:46 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_clear(mp_obj_t self_in) {
|
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(self_in, ModPicoGraphics_obj_t);
|
2022-06-07 11:03:38 +01:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
self->graphics->clear();
|
2022-03-21 17:02:46 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_pixel(mp_obj_t self_in, mp_obj_t x, mp_obj_t y) {
|
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(self_in, ModPicoGraphics_obj_t);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
self->graphics->pixel({
|
2022-05-27 16:52:43 +01:00
|
|
|
mp_obj_get_int(x),
|
|
|
|
mp_obj_get_int(y)
|
2022-06-06 12:05:44 +01:00
|
|
|
});
|
2022-03-21 17:02:46 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_pixel_span(size_t n_args, const mp_obj_t *args) {
|
2022-03-21 17:02:46 +00:00
|
|
|
enum { ARG_self, ARG_x, ARG_y, ARG_l };
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self], ModPicoGraphics_obj_t);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
self->graphics->pixel_span({
|
2022-06-07 11:03:38 +01:00
|
|
|
mp_obj_get_int(args[ARG_x]),
|
|
|
|
mp_obj_get_int(args[ARG_y])
|
|
|
|
}, mp_obj_get_int(args[ARG_l]));
|
2022-03-21 17:02:46 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_rectangle(size_t n_args, const mp_obj_t *args) {
|
2022-03-21 17:02:46 +00:00
|
|
|
enum { ARG_self, ARG_x, ARG_y, ARG_w, ARG_h };
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self], ModPicoGraphics_obj_t);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
self->graphics->rectangle({
|
2022-06-07 11:03:38 +01:00
|
|
|
mp_obj_get_int(args[ARG_x]),
|
|
|
|
mp_obj_get_int(args[ARG_y]),
|
|
|
|
mp_obj_get_int(args[ARG_w]),
|
|
|
|
mp_obj_get_int(args[ARG_h])
|
|
|
|
});
|
2022-03-21 17:02:46 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_circle(size_t n_args, const mp_obj_t *args) {
|
2022-03-21 17:02:46 +00:00
|
|
|
enum { ARG_self, ARG_x, ARG_y, ARG_r };
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self], ModPicoGraphics_obj_t);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
self->graphics->circle({
|
2022-06-07 11:03:38 +01:00
|
|
|
mp_obj_get_int(args[ARG_x]),
|
|
|
|
mp_obj_get_int(args[ARG_y])
|
|
|
|
}, mp_obj_get_int(args[ARG_r]));
|
2022-03-21 17:02:46 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_character(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2022-03-21 17:02:46 +00:00
|
|
|
enum { ARG_self, ARG_char, ARG_x, ARG_y, ARG_scale };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_char, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
2022-05-23 13:54:42 +01:00
|
|
|
{ MP_QSTR_x, MP_ARG_REQUIRED | MP_ARG_INT },
|
|
|
|
{ MP_QSTR_y, MP_ARG_REQUIRED | MP_ARG_INT },
|
2022-03-21 17:02:46 +00:00
|
|
|
{ MP_QSTR_scale, MP_ARG_INT, {.u_int = 2} },
|
|
|
|
};
|
|
|
|
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, ModPicoGraphics_obj_t);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
|
|
|
int c = mp_obj_get_int(args[ARG_char].u_obj);
|
|
|
|
int x = args[ARG_x].u_int;
|
|
|
|
int y = args[ARG_y].u_int;
|
|
|
|
int scale = args[ARG_scale].u_int;
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
self->graphics->character((char)c, Point(x, y), scale);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_text(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2022-03-21 17:02:46 +00:00
|
|
|
enum { ARG_self, ARG_text, ARG_x, ARG_y, ARG_wrap, ARG_scale };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_text, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_x1, MP_ARG_REQUIRED | MP_ARG_INT },
|
|
|
|
{ MP_QSTR_y1, MP_ARG_REQUIRED | MP_ARG_INT },
|
|
|
|
{ MP_QSTR_wordwrap, MP_ARG_REQUIRED | MP_ARG_INT },
|
|
|
|
{ MP_QSTR_scale, MP_ARG_INT, {.u_int = 2} },
|
|
|
|
};
|
|
|
|
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, ModPicoGraphics_obj_t);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
|
|
|
mp_obj_t text_obj = args[ARG_text].u_obj;
|
|
|
|
|
2022-05-27 16:52:43 +01:00
|
|
|
if(!mp_obj_is_str_or_bytes(text_obj)) mp_raise_TypeError("text: string required");
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-05-27 16:52:43 +01:00
|
|
|
GET_STR_DATA_LEN(text_obj, str, str_len);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-05-27 16:52:43 +01:00
|
|
|
std::string t((const char*)str);
|
|
|
|
|
|
|
|
int x = args[ARG_x].u_int;
|
|
|
|
int y = args[ARG_y].u_int;
|
|
|
|
int wrap = args[ARG_wrap].u_int;
|
|
|
|
int scale = args[ARG_scale].u_int;
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
self->graphics->text(t, Point(x, y), wrap, scale);
|
2022-05-26 12:47:06 +01:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_measure_text(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2022-05-26 12:47:06 +01:00
|
|
|
enum { ARG_self, ARG_text, ARG_scale };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_text, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_scale, MP_ARG_INT, {.u_int = 2} },
|
|
|
|
};
|
|
|
|
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, ModPicoGraphics_obj_t);
|
2022-05-26 12:47:06 +01:00
|
|
|
|
|
|
|
mp_obj_t text_obj = args[ARG_text].u_obj;
|
|
|
|
|
2022-05-27 16:52:43 +01:00
|
|
|
if(!mp_obj_is_str_or_bytes(text_obj)) mp_raise_TypeError("text: string required");
|
2022-05-26 12:47:06 +01:00
|
|
|
|
2022-05-27 16:52:43 +01:00
|
|
|
GET_STR_DATA_LEN(text_obj, str, str_len);
|
2022-05-26 12:47:06 +01:00
|
|
|
|
2022-05-27 16:52:43 +01:00
|
|
|
std::string t((const char*)str);
|
2022-05-26 12:47:06 +01:00
|
|
|
|
2022-05-27 16:52:43 +01:00
|
|
|
int scale = args[ARG_scale].u_int;
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
int width = self->graphics->measure_text(t, scale);
|
2022-05-27 16:52:43 +01:00
|
|
|
|
|
|
|
return mp_obj_new_int(width);
|
2022-03-21 17:02:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_polygon(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2022-03-21 17:02:46 +00:00
|
|
|
size_t num_tuples = n_args - 1;
|
|
|
|
const mp_obj_t *tuples = pos_args + 1;
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(pos_args[0], ModPicoGraphics_obj_t);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
|
|
|
// Check if there is only one argument, which might be a list
|
|
|
|
if(n_args == 2) {
|
|
|
|
if(mp_obj_is_type(pos_args[1], &mp_type_list)) {
|
|
|
|
mp_obj_list_t *points = MP_OBJ_TO_PTR2(pos_args[1], mp_obj_list_t);
|
2022-06-06 12:05:44 +01:00
|
|
|
|
|
|
|
if(points->len <= 0) mp_raise_ValueError("poly(): cannot provide an empty list");
|
|
|
|
|
|
|
|
num_tuples = points->len;
|
|
|
|
tuples = points->items;
|
2022-03-21 17:02:46 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mp_raise_TypeError("poly(): can't convert object to list");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(num_tuples > 0) {
|
|
|
|
std::vector<Point> points;
|
|
|
|
for(size_t i = 0; i < num_tuples; i++) {
|
|
|
|
mp_obj_t obj = tuples[i];
|
2022-06-06 12:05:44 +01:00
|
|
|
if(!mp_obj_is_type(obj, &mp_type_tuple)) mp_raise_ValueError("poly(): can't convert object to tuple");
|
|
|
|
|
|
|
|
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR2(obj, mp_obj_tuple_t);
|
|
|
|
|
|
|
|
if(tuple->len != 2) mp_raise_ValueError("poly(): tuple must only contain two numbers");
|
|
|
|
|
|
|
|
points.push_back({
|
|
|
|
mp_obj_get_int(tuple->items[0]),
|
|
|
|
mp_obj_get_int(tuple->items[1])
|
|
|
|
});
|
2022-03-21 17:02:46 +00:00
|
|
|
}
|
2022-06-07 16:37:06 +01:00
|
|
|
self->graphics->polygon(points);
|
2022-03-21 17:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_triangle(size_t n_args, const mp_obj_t *args) {
|
2022-03-21 17:02:46 +00:00
|
|
|
enum { ARG_self, ARG_x1, ARG_y1, ARG_x2, ARG_y2, ARG_x3, ARG_y3 };
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self], ModPicoGraphics_obj_t);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
self->graphics->triangle(
|
2022-06-07 11:03:38 +01:00
|
|
|
{mp_obj_get_int(args[ARG_x1]),
|
|
|
|
mp_obj_get_int(args[ARG_y1])},
|
|
|
|
{mp_obj_get_int(args[ARG_x2]),
|
|
|
|
mp_obj_get_int(args[ARG_y2])},
|
|
|
|
{mp_obj_get_int(args[ARG_x3]),
|
|
|
|
mp_obj_get_int(args[ARG_y3])}
|
|
|
|
);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
mp_obj_t ModPicoGraphics_line(size_t n_args, const mp_obj_t *args) {
|
2022-03-21 17:02:46 +00:00
|
|
|
enum { ARG_self, ARG_x1, ARG_y1, ARG_x2, ARG_y2 };
|
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self], ModPicoGraphics_obj_t);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
2022-06-07 16:37:06 +01:00
|
|
|
self->graphics->line(
|
2022-06-07 11:03:38 +01:00
|
|
|
{mp_obj_get_int(args[ARG_x1]),
|
|
|
|
mp_obj_get_int(args[ARG_y1])},
|
|
|
|
{mp_obj_get_int(args[ARG_x2]),
|
|
|
|
mp_obj_get_int(args[ARG_y2])}
|
|
|
|
);
|
2022-03-21 17:02:46 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|