Fix for hardlock on calling a module function prior to calling init()

pull/54/head
ZodiusInfuser 2021-02-11 15:45:39 +00:00
rodzic e086a0d1af
commit 85906b1059
5 zmienionych plików z 578 dodań i 384 usunięć

Wyświetl plik

@ -6,19 +6,22 @@
using namespace pimoroni; using namespace pimoroni;
PicoDisplay *display; PicoDisplay *display = nullptr;
extern "C" { extern "C" {
#include "pico_display.h" #include "pico_display.h"
#define NOT_INITIALISED_MSG "Cannot call this function, as picodisplay is not initialised. Call picodisplay.init(<bytearray>) first."
mp_obj_t picodisplay_buf_obj; mp_obj_t picodisplay_buf_obj;
mp_obj_t picodisplay_init(mp_obj_t buf_obj) { mp_obj_t picodisplay_init(mp_obj_t buf_obj) {
mp_buffer_info_t bufinfo; mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf_obj, &bufinfo, MP_BUFFER_RW); mp_get_buffer_raise(buf_obj, &bufinfo, MP_BUFFER_RW);
picodisplay_buf_obj = buf_obj; picodisplay_buf_obj = buf_obj;
display = new PicoDisplay((uint16_t *)bufinfo.buf); if(display == nullptr)
display = new PicoDisplay((uint16_t *)bufinfo.buf);
display->init(); display->init();
return mp_const_none; return mp_const_none;
} }
@ -32,120 +35,143 @@ mp_obj_t picodisplay_get_height() {
} }
mp_obj_t picodisplay_update() { mp_obj_t picodisplay_update() {
display->update(); if(display != nullptr)
display->update();
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picodisplay_set_backlight(mp_obj_t brightness_obj) { mp_obj_t picodisplay_set_backlight(mp_obj_t brightness_obj) {
float brightness = mp_obj_get_float(brightness_obj); if(display != nullptr) {
float brightness = mp_obj_get_float(brightness_obj);
if(brightness < 0 || brightness > 1.0f) if(brightness < 0 || brightness > 1.0f)
mp_raise_ValueError("brightness out of range. Expected 0.0 to 1.0"); mp_raise_ValueError("brightness out of range. Expected 0.0 to 1.0");
else
display->set_backlight((uint8_t)(brightness * 255.0f));
}
else else
display->set_backlight((uint8_t)(brightness * 255.0f)); mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picodisplay_set_led(mp_obj_t r_obj, mp_obj_t g_obj, mp_obj_t b_obj) { mp_obj_t picodisplay_set_led(mp_obj_t r_obj, mp_obj_t g_obj, mp_obj_t b_obj) {
int r = mp_obj_get_int(r_obj); if(display != nullptr) {
int g = mp_obj_get_int(g_obj); int r = mp_obj_get_int(r_obj);
int b = mp_obj_get_int(b_obj); int g = mp_obj_get_int(g_obj);
int b = mp_obj_get_int(b_obj);
if(r < 0 || r > 255) if(r < 0 || r > 255)
mp_raise_ValueError("r out of range. Expected 0 to 255"); mp_raise_ValueError("r out of range. Expected 0 to 255");
else if(g < 0 || g > 255) else if(g < 0 || g > 255)
mp_raise_ValueError("g out of range. Expected 0 to 255"); mp_raise_ValueError("g out of range. Expected 0 to 255");
else if(b < 0 || b > 255) else if(b < 0 || b > 255)
mp_raise_ValueError("b out of range. Expected 0 to 255"); mp_raise_ValueError("b out of range. Expected 0 to 255");
else
display->set_led(r, g, b);
}
else else
display->set_led(r, g, b); mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picodisplay_is_pressed(mp_obj_t button_obj) { mp_obj_t picodisplay_is_pressed(mp_obj_t button_obj) {
int buttonID = mp_obj_get_int(button_obj);
bool buttonPressed = false; bool buttonPressed = false;
switch(buttonID)
{ if(display != nullptr) {
case 0: int buttonID = mp_obj_get_int(button_obj);
buttonPressed = display->is_pressed(PicoDisplay::A); switch(buttonID) {
break; case 0:
buttonPressed = display->is_pressed(PicoDisplay::A);
break;
case 1: case 1:
buttonPressed = display->is_pressed(PicoDisplay::B); buttonPressed = display->is_pressed(PicoDisplay::B);
break; break;
case 2: case 2:
buttonPressed = display->is_pressed(PicoDisplay::X); buttonPressed = display->is_pressed(PicoDisplay::X);
break; break;
case 3: case 3:
buttonPressed = display->is_pressed(PicoDisplay::Y); buttonPressed = display->is_pressed(PicoDisplay::Y);
break; break;
default: default:
mp_raise_ValueError("button not valid. Expected 0 to 3"); mp_raise_ValueError("button not valid. Expected 0 to 3");
break; break;
}
} }
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return buttonPressed ? mp_const_true : mp_const_false; return buttonPressed ? mp_const_true : mp_const_false;
} }
mp_obj_t picodisplay_set_pen(mp_uint_t n_args, const mp_obj_t *args) { mp_obj_t picodisplay_set_pen(mp_uint_t n_args, const mp_obj_t *args) {
switch(n_args) if(display != nullptr) {
{ switch(n_args) {
case 1: { case 1: {
int p = mp_obj_get_int(args[0]); int p = mp_obj_get_int(args[0]);
if(p < 0 || p > 0xffff) if(p < 0 || p > 0xffff)
mp_raise_ValueError("p is not a valid pen."); mp_raise_ValueError("p is not a valid pen.");
else else
display->set_pen(p); display->set_pen(p);
} break; } break;
case 3: { case 3: {
int r = mp_obj_get_int(args[0]); int r = mp_obj_get_int(args[0]);
int g = mp_obj_get_int(args[1]); int g = mp_obj_get_int(args[1]);
int b = mp_obj_get_int(args[2]); int b = mp_obj_get_int(args[2]);
if(r < 0 || r > 255) if(r < 0 || r > 255)
mp_raise_ValueError("r out of range. Expected 0 to 255"); mp_raise_ValueError("r out of range. Expected 0 to 255");
else if(g < 0 || g > 255) else if(g < 0 || g > 255)
mp_raise_ValueError("g out of range. Expected 0 to 255"); mp_raise_ValueError("g out of range. Expected 0 to 255");
else if(b < 0 || b > 255) else if(b < 0 || b > 255)
mp_raise_ValueError("b out of range. Expected 0 to 255"); mp_raise_ValueError("b out of range. Expected 0 to 255");
else else
display->set_pen(r, g, b); display->set_pen(r, g, b);
} break; } break;
default: { default: {
char *buffer; char *buffer;
buffer = (char*)malloc(100); buffer = (char*)malloc(100);
sprintf(buffer, "function takes 1 or 3 (r,g,b) positional arguments but %d were given", n_args); sprintf(buffer, "function takes 1 or 3 (r,g,b) positional arguments but %d were given", n_args);
mp_raise_TypeError(buffer); mp_raise_TypeError(buffer);
} break; } break;
}
} }
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; 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) { mp_obj_t picodisplay_create_pen(mp_obj_t r_obj, mp_obj_t g_obj, mp_obj_t b_obj) {
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; int pen = 0;
if(r < 0 || r > 255)
mp_raise_ValueError("r out of range. Expected 0 to 255"); if(display != nullptr) {
else if(g < 0 || g > 255) int r = mp_obj_get_int(r_obj);
mp_raise_ValueError("g out of range. Expected 0 to 255"); int g = mp_obj_get_int(g_obj);
else if(b < 0 || b > 255) int b = mp_obj_get_int(b_obj);
mp_raise_ValueError("b out of range. Expected 0 to 255");
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);
}
else else
pen = display->create_pen(r, g, b); mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_obj_new_int(pen); return mp_obj_new_int(pen);
} }
@ -153,44 +179,64 @@ mp_obj_t picodisplay_create_pen(mp_obj_t r_obj, mp_obj_t g_obj, mp_obj_t b_obj)
mp_obj_t picodisplay_set_clip(mp_uint_t n_args, const mp_obj_t *args) { 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 (void)n_args; //Unused input parameter, we know it's 4
int x = mp_obj_get_int(args[0]); if(display != nullptr) {
int y = mp_obj_get_int(args[1]); int x = mp_obj_get_int(args[0]);
int w = mp_obj_get_int(args[2]); int y = mp_obj_get_int(args[1]);
int h = mp_obj_get_int(args[3]); int w = mp_obj_get_int(args[2]);
int h = mp_obj_get_int(args[3]);
Rect r(x, y, w, h); Rect r(x, y, w, h);
display->set_clip(r); display->set_clip(r);
}
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picodisplay_remove_clip() { mp_obj_t picodisplay_remove_clip() {
display->remove_clip(); if(display != nullptr)
display->remove_clip();
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picodisplay_clear() { mp_obj_t picodisplay_clear() {
display->clear(); if(display != nullptr)
display->clear();
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picodisplay_pixel(mp_obj_t x_obj, mp_obj_t y_obj) { mp_obj_t picodisplay_pixel(mp_obj_t x_obj, mp_obj_t y_obj) {
int x = mp_obj_get_int(x_obj); if(display != nullptr) {
int y = mp_obj_get_int(y_obj); int x = mp_obj_get_int(x_obj);
int y = mp_obj_get_int(y_obj);
Point p(x, y); Point p(x, y);
display->pixel(p); display->pixel(p);
}
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picodisplay_pixel_span(mp_obj_t x_obj, mp_obj_t y_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); if(display != nullptr) {
int y = mp_obj_get_int(y_obj); int x = mp_obj_get_int(x_obj);
int l = mp_obj_get_int(l_obj); int y = mp_obj_get_int(y_obj);
int l = mp_obj_get_int(l_obj);
Point p(x, y); Point p(x, y);
display->pixel_span(p, l); display->pixel_span(p, l);
}
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
@ -198,61 +244,75 @@ mp_obj_t picodisplay_pixel_span(mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t l_obj)
mp_obj_t picodisplay_rectangle(mp_uint_t n_args, const mp_obj_t *args) { 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 (void)n_args; //Unused input parameter, we know it's 4
int x = mp_obj_get_int(args[0]); if(display != nullptr) {
int y = mp_obj_get_int(args[1]); int x = mp_obj_get_int(args[0]);
int w = mp_obj_get_int(args[2]); int y = mp_obj_get_int(args[1]);
int h = mp_obj_get_int(args[3]); int w = mp_obj_get_int(args[2]);
int h = mp_obj_get_int(args[3]);
Rect r(x, y, w, h); Rect r(x, y, w, h);
display->rectangle(r); display->rectangle(r);
}
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picodisplay_circle(mp_obj_t x_obj, mp_obj_t y_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); if(display != nullptr) {
int y = mp_obj_get_int(y_obj); int x = mp_obj_get_int(x_obj);
int r = mp_obj_get_int(r_obj); int y = mp_obj_get_int(y_obj);
int r = mp_obj_get_int(r_obj);
Point p(x, y); Point p(x, y);
display->circle(p, r); display->circle(p, r);
}
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picodisplay_character(mp_uint_t n_args, const mp_obj_t *args) { mp_obj_t picodisplay_character(mp_uint_t n_args, const mp_obj_t *args) {
int c = mp_obj_get_int(args[0]); if(display != nullptr) {
int x = mp_obj_get_int(args[1]); int c = mp_obj_get_int(args[0]);
int y = mp_obj_get_int(args[2]); int x = mp_obj_get_int(args[1]);
int y = mp_obj_get_int(args[2]);
Point p(x, y); Point p(x, y);
if(n_args == 4) { if(n_args == 4) {
int scale = mp_obj_get_int(args[3]); int scale = mp_obj_get_int(args[3]);
display->character((char)c, p, scale); display->character((char)c, p, scale);
}
else
display->character((char)c, p);
} }
else
display->character((char)c, p);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picodisplay_text(mp_uint_t n_args, const mp_obj_t *args) { mp_obj_t picodisplay_text(mp_uint_t n_args, const mp_obj_t *args) {
mp_check_self(mp_obj_is_str_or_bytes(args[0])); if(display != nullptr) {
GET_STR_DATA_LEN(args[0], str, str_len); 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); std::string t((const char*)str);
int x = mp_obj_get_int(args[1]); int x = mp_obj_get_int(args[1]);
int y = mp_obj_get_int(args[2]); int y = mp_obj_get_int(args[2]);
int wrap = mp_obj_get_int(args[3]); int wrap = mp_obj_get_int(args[3]);
Point p(x, y); Point p(x, y);
if(n_args == 5) { if(n_args == 5) {
int scale = mp_obj_get_int(args[4]); int scale = mp_obj_get_int(args[4]);
display->text(t, p, wrap, scale); display->text(t, p, wrap, scale);
}
else
display->text(t, p, wrap);
} }
else else
display->text(t, p, wrap); mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }

Wyświetl plik

@ -6,19 +6,22 @@
using namespace pimoroni; using namespace pimoroni;
PicoExplorer *explorer; PicoExplorer *explorer = nullptr;
extern "C" { extern "C" {
#include "pico_explorer.h" #include "pico_explorer.h"
#define NOT_INITIALISED_MSG "Cannot call this function, as picoexplorer is not initialised. Call picoexplorer.init(<bytearray>) first."
mp_obj_t picoexplorer_buf_obj; mp_obj_t picoexplorer_buf_obj;
mp_obj_t picoexplorer_init(mp_obj_t buf_obj) { mp_obj_t picoexplorer_init(mp_obj_t buf_obj) {
mp_buffer_info_t bufinfo; mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf_obj, &bufinfo, MP_BUFFER_RW); mp_get_buffer_raise(buf_obj, &bufinfo, MP_BUFFER_RW);
picoexplorer_buf_obj = buf_obj; picoexplorer_buf_obj = buf_obj;
explorer = new PicoExplorer((uint16_t *)bufinfo.buf); if(explorer == nullptr)
explorer = new PicoExplorer((uint16_t *)bufinfo.buf);
explorer->init(); explorer->init();
return mp_const_none; return mp_const_none;
} }
@ -32,154 +35,190 @@ mp_obj_t picoexplorer_get_height() {
} }
mp_obj_t picoexplorer_update() { mp_obj_t picoexplorer_update() {
explorer->update(); if(explorer != nullptr)
explorer->update();
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picoexplorer_set_backlight(mp_obj_t brightness_obj) { mp_obj_t picoexplorer_set_backlight(mp_obj_t brightness_obj) {
float brightness = mp_obj_get_float(brightness_obj); if(explorer != nullptr) {
float brightness = mp_obj_get_float(brightness_obj);
if(brightness < 0 || brightness > 1.0f) if(brightness < 0 || brightness > 1.0f)
mp_raise_ValueError("brightness out of range. Expected 0.0 to 1.0"); mp_raise_ValueError("brightness out of range. Expected 0.0 to 1.0");
else
explorer->set_backlight((uint8_t)(brightness * 255.0f));
}
else else
explorer->set_backlight((uint8_t)(brightness * 255.0f)); mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picoexplorer_is_pressed(mp_obj_t button_obj) { mp_obj_t picoexplorer_is_pressed(mp_obj_t button_obj) {
int buttonID = mp_obj_get_int(button_obj);
bool buttonPressed = false; bool buttonPressed = false;
switch(buttonID)
{
case 0:
buttonPressed = explorer->is_pressed(PicoExplorer::A);
break;
case 1: if(explorer != nullptr) {
buttonPressed = explorer->is_pressed(PicoExplorer::B); int buttonID = mp_obj_get_int(button_obj);
break; switch(buttonID) {
case 0:
buttonPressed = explorer->is_pressed(PicoExplorer::A);
break;
case 2: case 1:
buttonPressed = explorer->is_pressed(PicoExplorer::X); buttonPressed = explorer->is_pressed(PicoExplorer::B);
break; break;
case 3: case 2:
buttonPressed = explorer->is_pressed(PicoExplorer::Y); buttonPressed = explorer->is_pressed(PicoExplorer::X);
break; break;
default: case 3:
mp_raise_ValueError("button not valid. Expected 0 to 3"); buttonPressed = explorer->is_pressed(PicoExplorer::Y);
break; break;
default:
mp_raise_ValueError("button not valid. Expected 0 to 3");
break;
}
} }
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return buttonPressed ? mp_const_true : mp_const_false; return buttonPressed ? mp_const_true : mp_const_false;
} }
extern mp_obj_t picoexplorer_get_adc(mp_obj_t channel_obj) { extern mp_obj_t picoexplorer_get_adc(mp_obj_t channel_obj) {
int channel = mp_obj_get_int(channel_obj);
float reading = 0.0f; float reading = 0.0f;
if(channel < 0 || channel > 2)
mp_raise_ValueError("adc channel not valid. Expected 0 to 2"); if(explorer != nullptr) {
int channel = mp_obj_get_int(channel_obj);
if(channel < 0 || channel > 2)
mp_raise_ValueError("adc channel not valid. Expected 0 to 2");
else
reading = explorer->get_adc(channel);
}
else else
reading = explorer->get_adc(channel); mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_obj_new_float(reading); return mp_obj_new_float(reading);
} }
extern mp_obj_t picoexplorer_set_motor(mp_uint_t n_args, const mp_obj_t *args) { extern mp_obj_t picoexplorer_set_motor(mp_uint_t n_args, const mp_obj_t *args) {
int channel = mp_obj_get_int(args[0]); if(explorer != nullptr) {
int action = mp_obj_get_int(args[1]); int channel = mp_obj_get_int(args[0]);
int action = mp_obj_get_int(args[1]);
if(channel < 0 || channel > 1) if(channel < 0 || channel > 1)
mp_raise_ValueError("motor channel not valid. Expected 0 to 1"); mp_raise_ValueError("motor channel not valid. Expected 0 to 1");
else if(action < 0 || action > 2) else if(action < 0 || action > 2)
mp_raise_ValueError("motor action not valid. Expected 0 to 2"); mp_raise_ValueError("motor action not valid. Expected 0 to 2");
else { else {
if(n_args == 3) { if(n_args == 3) {
float speed = mp_obj_get_float(args[2]); float speed = mp_obj_get_float(args[2]);
explorer->set_motor(channel, action, speed); explorer->set_motor(channel, action, speed);
}
else
explorer->set_motor(channel, action);
} }
else
explorer->set_motor(channel, action);
} }
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
extern mp_obj_t picoexplorer_set_audio_pin(mp_obj_t pin_obj) { extern mp_obj_t picoexplorer_set_audio_pin(mp_obj_t pin_obj) {
int pin = mp_obj_get_int(pin_obj); if(explorer != nullptr) {
explorer->set_audio_pin(pin); int pin = mp_obj_get_int(pin_obj);
explorer->set_audio_pin(pin);
}
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
extern mp_obj_t picoexplorer_set_tone(mp_uint_t n_args, const mp_obj_t *args) { extern mp_obj_t picoexplorer_set_tone(mp_uint_t n_args, const mp_obj_t *args) {
int frequency = mp_obj_get_int(args[0]); if(explorer != nullptr) {
int frequency = mp_obj_get_int(args[0]);
if(n_args == 2) { if(n_args == 2) {
float duty = mp_obj_get_int(args[1]); float duty = mp_obj_get_int(args[1]);
explorer->set_tone(frequency, duty); explorer->set_tone(frequency, duty);
}
else
explorer->set_tone(frequency);
} }
else else
explorer->set_tone(frequency); mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picoexplorer_set_pen(mp_uint_t n_args, const mp_obj_t *args) { mp_obj_t picoexplorer_set_pen(mp_uint_t n_args, const mp_obj_t *args) {
switch(n_args) if(explorer != nullptr) {
{ switch(n_args) {
case 1: { case 1: {
int p = mp_obj_get_int(args[0]); int p = mp_obj_get_int(args[0]);
if(p < 0 || p > 0xffff) if(p < 0 || p > 0xffff)
mp_raise_ValueError("p is not a valid pen."); mp_raise_ValueError("p is not a valid pen.");
else else
explorer->set_pen(p); explorer->set_pen(p);
} break; } break;
case 3: { case 3: {
int r = mp_obj_get_int(args[0]); int r = mp_obj_get_int(args[0]);
int g = mp_obj_get_int(args[1]); int g = mp_obj_get_int(args[1]);
int b = mp_obj_get_int(args[2]); int b = mp_obj_get_int(args[2]);
if(r < 0 || r > 255) if(r < 0 || r > 255)
mp_raise_ValueError("r out of range. Expected 0 to 255"); mp_raise_ValueError("r out of range. Expected 0 to 255");
else if(g < 0 || g > 255) else if(g < 0 || g > 255)
mp_raise_ValueError("g out of range. Expected 0 to 255"); mp_raise_ValueError("g out of range. Expected 0 to 255");
else if(b < 0 || b > 255) else if(b < 0 || b > 255)
mp_raise_ValueError("b out of range. Expected 0 to 255"); mp_raise_ValueError("b out of range. Expected 0 to 255");
else else
explorer->set_pen(r, g, b); explorer->set_pen(r, g, b);
} break; } break;
default: { default: {
char *buffer; char *buffer;
buffer = (char*)malloc(100); buffer = (char*)malloc(100);
sprintf(buffer, "function takes 1 or 3 (r,g,b) positional arguments but %d were given", n_args); sprintf(buffer, "function takes 1 or 3 (r,g,b) positional arguments but %d were given", n_args);
mp_raise_TypeError(buffer); mp_raise_TypeError(buffer);
} break; free(buffer);
} break;
}
} }
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picoexplorer_create_pen(mp_obj_t r_obj, mp_obj_t g_obj, mp_obj_t b_obj) { mp_obj_t picoexplorer_create_pen(mp_obj_t r_obj, mp_obj_t g_obj, mp_obj_t b_obj) {
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; int pen = 0;
if(r < 0 || r > 255)
mp_raise_ValueError("r out of range. Expected 0 to 255"); if(explorer != nullptr) {
else if(g < 0 || g > 255) int r = mp_obj_get_int(r_obj);
mp_raise_ValueError("g out of range. Expected 0 to 255"); int g = mp_obj_get_int(g_obj);
else if(b < 0 || b > 255) int b = mp_obj_get_int(b_obj);
mp_raise_ValueError("b out of range. Expected 0 to 255");
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 = explorer->create_pen(r, g, b);
}
else else
pen = explorer->create_pen(r, g, b); mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_obj_new_int(pen); return mp_obj_new_int(pen);
} }
@ -187,44 +226,62 @@ mp_obj_t picoexplorer_create_pen(mp_obj_t r_obj, mp_obj_t g_obj, mp_obj_t b_obj)
mp_obj_t picoexplorer_set_clip(mp_uint_t n_args, const mp_obj_t *args) { mp_obj_t picoexplorer_set_clip(mp_uint_t n_args, const mp_obj_t *args) {
(void)n_args; //Unused input parameter, we know it's 4 (void)n_args; //Unused input parameter, we know it's 4
int x = mp_obj_get_int(args[0]); if(explorer != nullptr) {
int y = mp_obj_get_int(args[1]); int x = mp_obj_get_int(args[0]);
int w = mp_obj_get_int(args[2]); int y = mp_obj_get_int(args[1]);
int h = mp_obj_get_int(args[3]); int w = mp_obj_get_int(args[2]);
int h = mp_obj_get_int(args[3]);
Rect r(x, y, w, h); Rect r(x, y, w, h);
explorer->set_clip(r); explorer->set_clip(r);
}
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picoexplorer_remove_clip() { mp_obj_t picoexplorer_remove_clip() {
explorer->remove_clip(); if(explorer != nullptr)
explorer->remove_clip();
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picoexplorer_clear() { mp_obj_t picoexplorer_clear() {
explorer->clear(); if(explorer != nullptr)
explorer->clear();
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picoexplorer_pixel(mp_obj_t x_obj, mp_obj_t y_obj) { mp_obj_t picoexplorer_pixel(mp_obj_t x_obj, mp_obj_t y_obj) {
int x = mp_obj_get_int(x_obj); if(explorer != nullptr) {
int y = mp_obj_get_int(y_obj); int x = mp_obj_get_int(x_obj);
int y = mp_obj_get_int(y_obj);
Point p(x, y); Point p(x, y);
explorer->pixel(p); explorer->pixel(p);
}
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picoexplorer_pixel_span(mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t l_obj) { mp_obj_t picoexplorer_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); if(explorer != nullptr) {
int y = mp_obj_get_int(y_obj); int x = mp_obj_get_int(x_obj);
int l = mp_obj_get_int(l_obj); int y = mp_obj_get_int(y_obj);
int l = mp_obj_get_int(l_obj);
Point p(x, y); Point p(x, y);
explorer->pixel_span(p, l); explorer->pixel_span(p, l);
}
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
@ -232,61 +289,77 @@ mp_obj_t picoexplorer_pixel_span(mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t l_obj)
mp_obj_t picoexplorer_rectangle(mp_uint_t n_args, const mp_obj_t *args) { mp_obj_t picoexplorer_rectangle(mp_uint_t n_args, const mp_obj_t *args) {
(void)n_args; //Unused input parameter, we know it's 4 (void)n_args; //Unused input parameter, we know it's 4
int x = mp_obj_get_int(args[0]); if(explorer != nullptr) {
int y = mp_obj_get_int(args[1]); int x = mp_obj_get_int(args[0]);
int w = mp_obj_get_int(args[2]); int y = mp_obj_get_int(args[1]);
int h = mp_obj_get_int(args[3]); int w = mp_obj_get_int(args[2]);
int h = mp_obj_get_int(args[3]);
Rect r(x, y, w, h); Rect r(x, y, w, h);
explorer->rectangle(r); explorer->rectangle(r);
}
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picoexplorer_circle(mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t r_obj) { mp_obj_t picoexplorer_circle(mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t r_obj) {
int x = mp_obj_get_int(x_obj); if(explorer != nullptr) {
int y = mp_obj_get_int(y_obj); int x = mp_obj_get_int(x_obj);
int r = mp_obj_get_int(r_obj); int y = mp_obj_get_int(y_obj);
int r = mp_obj_get_int(r_obj);
Point p(x, y); Point p(x, y);
explorer->circle(p, r); explorer->circle(p, r);
}
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picoexplorer_character(mp_uint_t n_args, const mp_obj_t *args) { mp_obj_t picoexplorer_character(mp_uint_t n_args, const mp_obj_t *args) {
int c = mp_obj_get_int(args[0]); if(explorer != nullptr) {
int x = mp_obj_get_int(args[1]); int c = mp_obj_get_int(args[0]);
int y = mp_obj_get_int(args[2]); int x = mp_obj_get_int(args[1]);
int y = mp_obj_get_int(args[2]);
Point p(x, y); Point p(x, y);
if(n_args == 4) { if(n_args == 4) {
int scale = mp_obj_get_int(args[3]); int scale = mp_obj_get_int(args[3]);
explorer->character((char)c, p, scale); explorer->character((char)c, p, scale);
}
else
explorer->character((char)c, p);
} }
else else
explorer->character((char)c, p); mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picoexplorer_text(mp_uint_t n_args, const mp_obj_t *args) { mp_obj_t picoexplorer_text(mp_uint_t n_args, const mp_obj_t *args) {
mp_check_self(mp_obj_is_str_or_bytes(args[0])); if(explorer != nullptr) {
GET_STR_DATA_LEN(args[0], str, str_len); 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); std::string t((const char*)str);
int x = mp_obj_get_int(args[1]); int x = mp_obj_get_int(args[1]);
int y = mp_obj_get_int(args[2]); int y = mp_obj_get_int(args[2]);
int wrap = mp_obj_get_int(args[3]); int wrap = mp_obj_get_int(args[3]);
Point p(x, y); Point p(x, y);
if(n_args == 5) { if(n_args == 5) {
int scale = mp_obj_get_int(args[4]); int scale = mp_obj_get_int(args[4]);
explorer->text(t, p, wrap, scale); explorer->text(t, p, wrap, scale);
}
else
explorer->text(t, p, wrap);
} }
else else
explorer->text(t, p, wrap); mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }

Wyświetl plik

@ -6,14 +6,19 @@
using namespace pimoroni; using namespace pimoroni;
PicoRGBKeypad keypad; PicoRGBKeypad *keypad = nullptr;
extern "C" { extern "C" {
#include "pico_rgb_keypad.h" #include "pico_rgb_keypad.h"
#define NOT_INITIALISED_MSG "Cannot call this function, as picokeypad is not initialised. Call picokeypad.init() first."
mp_obj_t picokeypad_init() { mp_obj_t picokeypad_init() {
keypad.init(); if(keypad == nullptr) {
keypad = new PicoRGBKeypad();
keypad->init();
}
return mp_const_none; return mp_const_none;
} }
@ -30,17 +35,24 @@ mp_obj_t picokeypad_get_num_pads() {
} }
mp_obj_t picokeypad_update() { mp_obj_t picokeypad_update() {
keypad.update(); if(keypad != nullptr)
keypad->update();
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picokeypad_set_brightness(mp_obj_t brightness_obj) { mp_obj_t picokeypad_set_brightness(mp_obj_t brightness_obj) {
float brightness = mp_obj_get_float(brightness_obj); if(keypad != nullptr) {
float brightness = mp_obj_get_float(brightness_obj);
if(brightness < 0 || brightness > 1.0f) if(brightness < 0 || brightness > 1.0f)
mp_raise_ValueError("brightness out of range. Expected 0.0 to 1.0"); mp_raise_ValueError("brightness out of range. Expected 0.0 to 1.0");
else
keypad->set_brightness(brightness);
}
else else
keypad.set_brightness(brightness); mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
@ -48,25 +60,28 @@ mp_obj_t picokeypad_set_brightness(mp_obj_t brightness_obj) {
mp_obj_t picokeypad_illuminate_xy(mp_uint_t n_args, const mp_obj_t *args) { mp_obj_t picokeypad_illuminate_xy(mp_uint_t n_args, const mp_obj_t *args) {
(void)n_args; //Unused input parameter, we know it's 5 (void)n_args; //Unused input parameter, we know it's 5
int x = mp_obj_get_int(args[0]); if(keypad != nullptr) {
int y = mp_obj_get_int(args[1]); int x = mp_obj_get_int(args[0]);
int r = mp_obj_get_int(args[2]); int y = mp_obj_get_int(args[1]);
int g = mp_obj_get_int(args[3]); int r = mp_obj_get_int(args[2]);
int b = mp_obj_get_int(args[4]); int g = mp_obj_get_int(args[3]);
int b = mp_obj_get_int(args[4]);
if(x < 0 || x >= PicoRGBKeypad::WIDTH || y < 0 || y >= PicoRGBKeypad::HEIGHT) if(x < 0 || x >= PicoRGBKeypad::WIDTH || y < 0 || y >= PicoRGBKeypad::HEIGHT)
mp_raise_ValueError("x or y out of range."); mp_raise_ValueError("x or y out of range.");
else else {
{ if(r < 0 || r > 255)
if(r < 0 || r > 255) mp_raise_ValueError("r out of range. Expected 0 to 255");
mp_raise_ValueError("r out of range. Expected 0 to 255"); else if(g < 0 || g > 255)
else if(g < 0 || g > 255) mp_raise_ValueError("g out of range. Expected 0 to 255");
mp_raise_ValueError("g out of range. Expected 0 to 255"); else if(b < 0 || b > 255)
else if(b < 0 || b > 255) mp_raise_ValueError("b out of range. Expected 0 to 255");
mp_raise_ValueError("b out of range. Expected 0 to 255"); else
else keypad->illuminate(x, y, r, g, b);
keypad.illuminate(x, y, r, g, b); }
} }
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
@ -74,34 +89,47 @@ mp_obj_t picokeypad_illuminate_xy(mp_uint_t n_args, const mp_obj_t *args) {
mp_obj_t picokeypad_illuminate(mp_uint_t n_args, const mp_obj_t *args) { mp_obj_t picokeypad_illuminate(mp_uint_t n_args, const mp_obj_t *args) {
(void)n_args; //Unused input parameter, we know it's 5 (void)n_args; //Unused input parameter, we know it's 5
int i = mp_obj_get_int(args[0]); if(keypad != nullptr) {
int r = mp_obj_get_int(args[1]); int i = mp_obj_get_int(args[0]);
int g = mp_obj_get_int(args[2]); int r = mp_obj_get_int(args[1]);
int b = mp_obj_get_int(args[3]); int g = mp_obj_get_int(args[2]);
int b = mp_obj_get_int(args[3]);
if(i < 0 || i >= PicoRGBKeypad::NUM_PADS) if(i < 0 || i >= PicoRGBKeypad::NUM_PADS)
mp_raise_ValueError("x or y out of range."); mp_raise_ValueError("x or y out of range.");
else else {
{ if(r < 0 || r > 255)
if(r < 0 || r > 255) mp_raise_ValueError("r out of range. Expected 0 to 255");
mp_raise_ValueError("r out of range. Expected 0 to 255"); else if(g < 0 || g > 255)
else if(g < 0 || g > 255) mp_raise_ValueError("g out of range. Expected 0 to 255");
mp_raise_ValueError("g out of range. Expected 0 to 255"); else if(b < 0 || b > 255)
else if(b < 0 || b > 255) mp_raise_ValueError("b out of range. Expected 0 to 255");
mp_raise_ValueError("b out of range. Expected 0 to 255"); else
else keypad->illuminate(i, r, g, b);
keypad.illuminate(i, r, g, b); }
} }
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picokeypad_clear() { mp_obj_t picokeypad_clear() {
keypad.clear(); if(keypad != nullptr)
keypad->clear();
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picokeypad_get_button_states() { mp_obj_t picokeypad_get_button_states() {
return mp_obj_new_int(keypad.get_button_states()); uint16_t states = 0;
if(keypad != nullptr)
states = keypad->get_button_states();
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_obj_new_int(states);
} }
} }

Wyświetl plik

@ -6,14 +6,18 @@
using namespace pimoroni; using namespace pimoroni;
PicoScroll scroll; PicoScroll *scroll = nullptr;
extern "C" { extern "C" {
#include "pico_scroll.h" #include "pico_scroll.h"
#define NOT_INITIALISED_MSG "Cannot call this function, as picoscroll is not initialised. Call picoscroll.init() first."
mp_obj_t picoscroll_init() { mp_obj_t picoscroll_init() {
scroll.init(); if(scroll == nullptr)
scroll = new PicoScroll();
scroll->init();
return mp_const_none; return mp_const_none;
} }
@ -26,59 +30,71 @@ mp_obj_t picoscroll_get_height() {
} }
mp_obj_t picoscroll_update() { mp_obj_t picoscroll_update() {
scroll.update(); if(scroll != nullptr)
scroll->update();
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picoscroll_set_pixel(mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t v_obj) { mp_obj_t picoscroll_set_pixel(mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t v_obj) {
int x = mp_obj_get_int(x_obj); if(scroll != nullptr) {
int y = mp_obj_get_int(y_obj); int x = mp_obj_get_int(x_obj);
int val = mp_obj_get_int(v_obj); int y = mp_obj_get_int(y_obj);
int val = mp_obj_get_int(v_obj);
if(x < 0 || x >= PicoScroll::WIDTH || y < 0 || y >= PicoScroll::HEIGHT) if(x < 0 || x >= PicoScroll::WIDTH || y < 0 || y >= PicoScroll::HEIGHT)
mp_raise_ValueError("x or y out of range."); mp_raise_ValueError("x or y out of range.");
else else {
{ if(val < 0 || val > 255)
if(val < 0 || val > 255) mp_raise_ValueError("val out of range. Expected 0 to 255");
mp_raise_ValueError("val out of range. Expected 0 to 255"); else
else scroll->set_pixel(x, y, val);
scroll.set_pixel(x, y, val); }
} }
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picoscroll_clear() { mp_obj_t picoscroll_clear() {
scroll.clear(); if(scroll != nullptr)
scroll->clear();
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picoscroll_is_pressed(mp_obj_t button_obj) { mp_obj_t picoscroll_is_pressed(mp_obj_t button_obj) {
int buttonID = mp_obj_get_int(button_obj);
bool buttonPressed = false; bool buttonPressed = false;
switch(buttonID)
{ if(scroll != nullptr) {
case 0: int buttonID = mp_obj_get_int(button_obj);
buttonPressed = scroll.is_pressed(PicoScroll::A); switch(buttonID) {
break; case 0:
buttonPressed = scroll->is_pressed(PicoScroll::A);
break;
case 1: case 1:
buttonPressed = scroll.is_pressed(PicoScroll::B); buttonPressed = scroll->is_pressed(PicoScroll::B);
break; break;
case 2: case 2:
buttonPressed = scroll.is_pressed(PicoScroll::X); buttonPressed = scroll->is_pressed(PicoScroll::X);
break; break;
case 3: case 3:
buttonPressed = scroll.is_pressed(PicoScroll::Y); buttonPressed = scroll->is_pressed(PicoScroll::Y);
break; break;
default: default:
mp_raise_ValueError("button not valid. Expected 0 to 3"); mp_raise_ValueError("button not valid. Expected 0 to 3");
break; break;
}
} }
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return buttonPressed ? mp_const_true : mp_const_false; return buttonPressed ? mp_const_true : mp_const_false;
} }

Wyświetl plik

@ -6,14 +6,18 @@
using namespace pimoroni; using namespace pimoroni;
PicoUnicorn unicorn; PicoUnicorn *unicorn = nullptr;
extern "C" { extern "C" {
#include "pico_unicorn.h" #include "pico_unicorn.h"
#define NOT_INITIALISED_MSG "Cannot call this function, as picounicorn is not initialised. Call picounicorn.init() first."
mp_obj_t picounicorn_init() { mp_obj_t picounicorn_init() {
unicorn.init(); if(unicorn == nullptr)
unicorn = new PicoUnicorn();
unicorn->init();
return mp_const_none; return mp_const_none;
} }
@ -33,78 +37,91 @@ mp_obj_t picounicorn_get_height() {
mp_obj_t picounicorn_set_pixel(mp_uint_t n_args, const mp_obj_t *args) { mp_obj_t picounicorn_set_pixel(mp_uint_t n_args, const mp_obj_t *args) {
(void)n_args; //Unused input parameter, we know it's 5 (void)n_args; //Unused input parameter, we know it's 5
int x = mp_obj_get_int(args[0]); if(unicorn != nullptr) {
int y = mp_obj_get_int(args[1]); int x = mp_obj_get_int(args[0]);
int r = mp_obj_get_int(args[2]); int y = mp_obj_get_int(args[1]);
int g = mp_obj_get_int(args[3]); int r = mp_obj_get_int(args[2]);
int b = mp_obj_get_int(args[4]); int g = mp_obj_get_int(args[3]);
int b = mp_obj_get_int(args[4]);
if(x < 0 || x >= PicoUnicorn::WIDTH || y < 0 || y >= PicoUnicorn::HEIGHT) if(x < 0 || x >= PicoUnicorn::WIDTH || y < 0 || y >= PicoUnicorn::HEIGHT)
mp_raise_ValueError("x or y out of range."); mp_raise_ValueError("x or y out of range.");
else
{
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 else
unicorn.set_pixel(x, y, r, g, b); {
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
unicorn->set_pixel(x, y, r, g, b);
}
} }
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picounicorn_set_pixel_value(mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t v_obj) { mp_obj_t picounicorn_set_pixel_value(mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t v_obj) {
int x = mp_obj_get_int(x_obj); if(unicorn != nullptr) {
int y = mp_obj_get_int(y_obj); int x = mp_obj_get_int(x_obj);
int val = mp_obj_get_int(v_obj); int y = mp_obj_get_int(y_obj);
int val = mp_obj_get_int(v_obj);
if(x < 0 || x >= PicoUnicorn::WIDTH || y < 0 || y >= PicoUnicorn::HEIGHT) if(x < 0 || x >= PicoUnicorn::WIDTH || y < 0 || y >= PicoUnicorn::HEIGHT)
mp_raise_ValueError("x or y out of range."); mp_raise_ValueError("x or y out of range.");
else else {
{ if(val < 0 || val > 255)
if(val < 0 || val > 255) mp_raise_ValueError("val out of range. Expected 0 to 255");
mp_raise_ValueError("val out of range. Expected 0 to 255"); else
else unicorn->set_pixel(x, y, val);
unicorn.set_pixel(x, y, val); }
} }
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picounicorn_clear() { mp_obj_t picounicorn_clear() {
unicorn.clear(); if(unicorn != nullptr)
unicorn->clear();
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none; return mp_const_none;
} }
mp_obj_t picounicorn_is_pressed(mp_obj_t button_obj) { mp_obj_t picounicorn_is_pressed(mp_obj_t button_obj) {
int buttonID = mp_obj_get_int(button_obj);
bool buttonPressed = false; bool buttonPressed = false;
switch(buttonID)
{ if(unicorn != nullptr) {
case 0: int buttonID = mp_obj_get_int(button_obj);
buttonPressed = unicorn.is_pressed(PicoUnicorn::A); switch(buttonID) {
break; case 0:
buttonPressed = unicorn->is_pressed(PicoUnicorn::A);
break;
case 1: case 1:
buttonPressed = unicorn.is_pressed(PicoUnicorn::B); buttonPressed = unicorn->is_pressed(PicoUnicorn::B);
break; break;
case 2: case 2:
buttonPressed = unicorn.is_pressed(PicoUnicorn::X); buttonPressed = unicorn->is_pressed(PicoUnicorn::X);
break; break;
case 3: case 3:
buttonPressed = unicorn.is_pressed(PicoUnicorn::Y); buttonPressed = unicorn->is_pressed(PicoUnicorn::Y);
break; break;
default: default:
mp_raise_ValueError("button not valid. Expected 0 to 3"); mp_raise_ValueError("button not valid. Expected 0 to 3");
break; break;
}
} }
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return buttonPressed ? mp_const_true : mp_const_false; return buttonPressed ? mp_const_true : mp_const_false;
} }