2021-01-19 18:46:53 +00:00
|
|
|
#include "hardware/spi.h"
|
|
|
|
#include "hardware/sync.h"
|
|
|
|
#include "pico/binary_info.h"
|
|
|
|
|
2021-05-13 11:06:01 +00:00
|
|
|
#include "libraries/pico_unicorn/pico_unicorn.hpp"
|
2021-01-19 18:46:53 +00:00
|
|
|
|
|
|
|
using namespace pimoroni;
|
|
|
|
|
2021-02-11 15:45:39 +00:00
|
|
|
PicoUnicorn *unicorn = nullptr;
|
2021-01-19 18:46:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "pico_unicorn.h"
|
|
|
|
|
2021-02-11 15:45:39 +00:00
|
|
|
#define NOT_INITIALISED_MSG "Cannot call this function, as picounicorn is not initialised. Call picounicorn.init() first."
|
|
|
|
|
2021-01-19 18:46:53 +00:00
|
|
|
mp_obj_t picounicorn_init() {
|
2021-02-11 15:45:39 +00:00
|
|
|
if(unicorn == nullptr)
|
|
|
|
unicorn = new PicoUnicorn();
|
|
|
|
unicorn->init();
|
2021-01-19 18:46:53 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t picounicorn_get_width() {
|
|
|
|
return mp_obj_new_int(PicoUnicorn::WIDTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t picounicorn_get_height() {
|
|
|
|
return mp_obj_new_int(PicoUnicorn::HEIGHT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// mp_obj_t picounicorn_update() {
|
|
|
|
// unicorn.update();
|
|
|
|
// return mp_const_none;
|
|
|
|
// }
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-02-11 15:45:39 +00:00
|
|
|
if(unicorn != nullptr) {
|
|
|
|
int x = mp_obj_get_int(args[0]);
|
|
|
|
int y = mp_obj_get_int(args[1]);
|
|
|
|
int r = mp_obj_get_int(args[2]);
|
|
|
|
int g = mp_obj_get_int(args[3]);
|
|
|
|
int b = mp_obj_get_int(args[4]);
|
2021-01-19 18:46:53 +00:00
|
|
|
|
2021-02-11 15:45:39 +00:00
|
|
|
if(x < 0 || x >= PicoUnicorn::WIDTH || y < 0 || y >= PicoUnicorn::HEIGHT)
|
|
|
|
mp_raise_ValueError("x or y out of range.");
|
2021-01-19 18:46:53 +00:00
|
|
|
else
|
2021-02-11 15:45:39 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2021-01-19 18:46:53 +00:00
|
|
|
}
|
2021-02-11 15:45:39 +00:00
|
|
|
else
|
|
|
|
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
|
2021-01-19 18:46:53 +00:00
|
|
|
|
|
|
|
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) {
|
2021-02-11 15:45:39 +00:00
|
|
|
if(unicorn != nullptr) {
|
|
|
|
int x = mp_obj_get_int(x_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)
|
|
|
|
mp_raise_ValueError("x or y out of range.");
|
|
|
|
else {
|
|
|
|
if(val < 0 || val > 255)
|
|
|
|
mp_raise_ValueError("val out of range. Expected 0 to 255");
|
|
|
|
else
|
|
|
|
unicorn->set_pixel(x, y, val);
|
|
|
|
}
|
2021-01-19 18:46:53 +00:00
|
|
|
}
|
2021-02-11 15:45:39 +00:00
|
|
|
else
|
|
|
|
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
|
2021-01-19 18:46:53 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t picounicorn_clear() {
|
2021-02-11 15:45:39 +00:00
|
|
|
if(unicorn != nullptr)
|
|
|
|
unicorn->clear();
|
|
|
|
else
|
|
|
|
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
|
2021-01-19 18:46:53 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t picounicorn_is_pressed(mp_obj_t button_obj) {
|
|
|
|
bool buttonPressed = false;
|
2021-02-11 15:45:39 +00:00
|
|
|
|
|
|
|
if(unicorn != nullptr) {
|
|
|
|
int buttonID = mp_obj_get_int(button_obj);
|
|
|
|
switch(buttonID) {
|
|
|
|
case 0:
|
|
|
|
buttonPressed = unicorn->is_pressed(PicoUnicorn::A);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
buttonPressed = unicorn->is_pressed(PicoUnicorn::B);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
buttonPressed = unicorn->is_pressed(PicoUnicorn::X);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
buttonPressed = unicorn->is_pressed(PicoUnicorn::Y);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
mp_raise_ValueError("button not valid. Expected 0 to 3");
|
|
|
|
break;
|
|
|
|
}
|
2021-01-19 18:46:53 +00:00
|
|
|
}
|
2021-02-11 15:45:39 +00:00
|
|
|
else
|
|
|
|
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
|
2021-01-19 18:46:53 +00:00
|
|
|
|
|
|
|
return buttonPressed ? mp_const_true : mp_const_false;
|
|
|
|
}
|
|
|
|
}
|