| 1 |
/* |
| 2 |
* This file is part of the MicroPython project, http://micropython.org/ |
| 3 |
* |
| 4 |
* The MIT License (MIT) |
| 5 |
* |
| 6 |
* Copyright (c) 2015 Damien P. George |
| 7 |
* |
| 8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
* of this software and associated documentation files (the "Software"), to deal |
| 10 |
* in the Software without restriction, including without limitation the rights |
| 11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
* copies of the Software, and to permit persons to whom the Software is |
| 13 |
* furnished to do so, subject to the following conditions: |
| 14 |
* |
| 15 |
* The above copyright notice and this permission notice shall be included in |
| 16 |
* all copies or substantial portions of the Software. |
| 17 |
* |
| 18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
* THE SOFTWARE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <string.h> |
| 28 |
#include "py/obj.h" |
| 29 |
#include "py/runtime.h" |
| 30 |
#include "py/gc.h" |
| 31 |
#include "nrf_gpio.h" |
| 32 |
#include "microbitimage.h" |
| 33 |
#include "microbitdisplay.h" |
| 34 |
#include "iters.h" |
| 35 |
#include "ticker.h" |
| 36 |
|
| 37 |
#define min(a,b) (((a)<(b))?(a):(b)) |
| 38 |
|
| 39 |
void microbit_display_show(microbit_display_obj_t *display, microbit_image_obj_t *image) { |
| 40 |
mp_int_t w = min(imageWidth(image), 5); |
| 41 |
mp_int_t h = min(imageHeight(image), 5); |
| 42 |
mp_int_t x = 0; |
| 43 |
mp_int_t brightnesses = 0; |
| 44 |
for (; x < w; ++x) { |
| 45 |
mp_int_t y = 0; |
| 46 |
for (; y < h; ++y) { |
| 47 |
uint8_t pix = imageGetPixelValue(image, x, y); |
| 48 |
display->image_buffer[x][y] = pix; |
| 49 |
brightnesses |= (1 << pix); |
| 50 |
} |
| 51 |
for (; y < 5; ++y) { |
| 52 |
display->image_buffer[x][y] = 0; |
| 53 |
} |
| 54 |
} |
| 55 |
for (; x < 5; ++x) { |
| 56 |
for (mp_int_t y = 0; y < 5; ++y) { |
| 57 |
display->image_buffer[x][y] = 0; |
| 58 |
} |
| 59 |
} |
| 60 |
display->brightnesses = brightnesses; |
| 61 |
} |
| 62 |
|
| 63 |
#define DEFAULT_PRINT_SPEED 400 |
| 64 |
|
| 65 |
|
| 66 |
mp_obj_t microbit_display_show_func(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 67 |
|
| 68 |
// Cancel any animations. |
| 69 |
MP_STATE_PORT(async_data)[0] = NULL; |
| 70 |
MP_STATE_PORT(async_data)[1] = NULL; |
| 71 |
|
| 72 |
static const mp_arg_t show_allowed_args[] = { |
| 73 |
{ MP_QSTR_image, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 74 |
{ MP_QSTR_delay, MP_ARG_INT, {.u_int = DEFAULT_PRINT_SPEED} }, |
| 75 |
{ MP_QSTR_clear, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, |
| 76 |
{ MP_QSTR_wait, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} }, |
| 77 |
{ MP_QSTR_loop, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, |
| 78 |
}; |
| 79 |
|
| 80 |
// Parse the args. |
| 81 |
microbit_display_obj_t *self = (microbit_display_obj_t*)pos_args[0]; |
| 82 |
mp_arg_val_t args[MP_ARRAY_SIZE(show_allowed_args)]; |
| 83 |
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(show_allowed_args), show_allowed_args, args); |
| 84 |
|
| 85 |
mp_obj_t image = args[0].u_obj; |
| 86 |
mp_int_t delay = args[1].u_int; |
| 87 |
bool clear = args[2].u_bool; |
| 88 |
bool wait = args[3].u_bool; |
| 89 |
bool loop = args[4].u_bool; |
| 90 |
|
| 91 |
if (mp_obj_is_str(image)) { |
| 92 |
// arg is a string object |
| 93 |
size_t len; |
| 94 |
const char *str = mp_obj_str_get_data(image, &len); |
| 95 |
if (len == 0) { |
| 96 |
// There are no chars; do nothing. |
| 97 |
return mp_const_none; |
| 98 |
} else if (len == 1) { |
| 99 |
if (!clear && !loop) { |
| 100 |
// A single char; convert to an image and print that. |
| 101 |
image = microbit_image_for_char(str[0]); |
| 102 |
goto single_image_immediate; |
| 103 |
} |
| 104 |
} |
| 105 |
image = microbit_string_facade(image); |
| 106 |
} else if (mp_obj_get_type(image) == µbit_image_type) { |
| 107 |
if (!clear && !loop) { |
| 108 |
goto single_image_immediate; |
| 109 |
} |
| 110 |
image = mp_obj_new_tuple(1, &image); |
| 111 |
} |
| 112 |
// iterable: |
| 113 |
if (args[4].u_bool) { /*loop*/ |
| 114 |
image = microbit_repeat_iterator(image); |
| 115 |
} |
| 116 |
microbit_display_animate(self, image, delay, clear, wait); |
| 117 |
return mp_const_none; |
| 118 |
|
| 119 |
single_image_immediate: |
| 120 |
microbit_display_show(self, (microbit_image_obj_t *)image); |
| 121 |
return mp_const_none; |
| 122 |
} |
| 123 |
MP_DEFINE_CONST_FUN_OBJ_KW(microbit_display_show_obj, 1, microbit_display_show_func); |
| 124 |
|
| 125 |
static uint8_t async_mode; |
| 126 |
static mp_obj_t async_iterator = NULL; |
| 127 |
// Record if an error occurs in async animation. Unfortunately there is no way to report this. |
| 128 |
static volatile bool wakeup_event = false; |
| 129 |
static mp_uint_t async_delay = 1000; |
| 130 |
static mp_uint_t async_tick = 0; |
| 131 |
static bool async_clear = false; |
| 132 |
|
| 133 |
|
| 134 |
bool microbit_display_active_animation(void) { |
| 135 |
return async_mode == ASYNC_MODE_ANIMATION; |
| 136 |
} |
| 137 |
|
| 138 |
static void async_stop(void) { |
| 139 |
async_iterator = NULL; |
| 140 |
async_mode = ASYNC_MODE_STOPPED; |
| 141 |
async_tick = 0; |
| 142 |
async_delay = 1000; |
| 143 |
async_clear = false; |
| 144 |
MP_STATE_PORT(async_data)[0] = NULL; |
| 145 |
MP_STATE_PORT(async_data)[1] = NULL; |
| 146 |
wakeup_event = true; |
| 147 |
} |
| 148 |
|
| 149 |
static void wait_for_event(void) { |
| 150 |
while (!wakeup_event) { |
| 151 |
// allow CTRL-C to stop the animation |
| 152 |
if (MP_STATE_THREAD(mp_pending_exception) != MP_OBJ_NULL) { |
| 153 |
async_stop(); |
| 154 |
return; |
| 155 |
} |
| 156 |
__WFI(); |
| 157 |
} |
| 158 |
wakeup_event = false; |
| 159 |
} |
| 160 |
|
| 161 |
typedef struct { |
| 162 |
uint8_t x; |
| 163 |
uint8_t y; |
| 164 |
} DisplayPoint; |
| 165 |
|
| 166 |
#define NO_CONN 0 |
| 167 |
|
| 168 |
#define ROW_COUNT 3 |
| 169 |
#define COLUMN_COUNT 9 |
| 170 |
|
| 171 |
static const DisplayPoint display_map[COLUMN_COUNT][ROW_COUNT] = { |
| 172 |
{{0,0}, {4,2}, {2,4}}, |
| 173 |
{{2,0}, {0,2}, {4,4}}, |
| 174 |
{{4,0}, {2,2}, {0,4}}, |
| 175 |
{{4,3}, {1,0}, {0,1}}, |
| 176 |
{{3,3}, {3,0}, {1,1}}, |
| 177 |
{{2,3}, {3,4}, {2,1}}, |
| 178 |
{{1,3}, {1,4}, {3,1}}, |
| 179 |
{{0,3}, {NO_CONN,NO_CONN}, {4,1}}, |
| 180 |
{{1,2}, {NO_CONN,NO_CONN}, {3,2}} |
| 181 |
}; |
| 182 |
|
| 183 |
#define MIN_COLUMN_PIN 4 |
| 184 |
#define COLUMN_PINS_MASK 0x1ff0 |
| 185 |
#define MIN_ROW_PIN 13 |
| 186 |
#define MAX_ROW_PIN 15 |
| 187 |
#define ROW_PINS_MASK 0xe000 |
| 188 |
|
| 189 |
static inline void displaySetPinsForRow(microbit_display_obj_t * p_display, uint8_t brightness) { |
| 190 |
if (brightness == 0) { |
| 191 |
nrf_gpio_port_out_clear(NRF_GPIO, COLUMN_PINS_MASK & ~p_display->pins_for_brightness[brightness]); |
| 192 |
} else { |
| 193 |
nrf_gpio_pin_set(p_display->pins_for_brightness[brightness]); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/* This is the primary PWM driver/display driver. It will operate on one row |
| 198 |
* (9 pins) per invocation. It will turn on LEDs with maximum brightness, |
| 199 |
* then let the "callback" callback turn off the LEDs as appropriate for the |
| 200 |
* required brightness level. |
| 201 |
* |
| 202 |
* For each row |
| 203 |
* Turn off all the LEDs in the previous row |
| 204 |
* Set the column bits high (off) |
| 205 |
* Set the row strobe low (off) |
| 206 |
* Turn on all the LEDs in the current row that have maximum brightness |
| 207 |
* Set the row strobe high (on) |
| 208 |
* Set some/all column bits low (on) |
| 209 |
* Register the PWM callback |
| 210 |
* For each callback start with brightness 0 |
| 211 |
* If brightness 0 |
| 212 |
* Turn off the LEDs specified at this level |
| 213 |
* Else |
| 214 |
* Turn on the LEDs specified at this level |
| 215 |
* If brightness max |
| 216 |
* Disable the PWM callback |
| 217 |
* Else |
| 218 |
* Re-queue the PWM callback after the appropriate delay |
| 219 |
*/ |
| 220 |
static void displayAdvanceRow(microbit_display_obj_t * p_display) { |
| 221 |
/* Clear all of the column bits */ |
| 222 |
nrf_gpio_port_out_set(NRF_GPIO, COLUMN_PINS_MASK); |
| 223 |
/* Clear the strobe bit for this row */ |
| 224 |
nrf_gpio_pin_clear(p_display->strobe_row + MIN_ROW_PIN); |
| 225 |
|
| 226 |
/* Move to the next row. Before this, "this row" refers to the row |
| 227 |
* manipulated by the previous invocation of this function. After this, |
| 228 |
* "this row" refers to the row manipulated by the current invocation of |
| 229 |
* this function. */ |
| 230 |
p_display->strobe_row++; |
| 231 |
|
| 232 |
// Reset the row counts and bit mask when we have hit the max. |
| 233 |
if (p_display->strobe_row == ROW_COUNT) { |
| 234 |
p_display->strobe_row = 0; |
| 235 |
} |
| 236 |
|
| 237 |
// Set pin for this row. |
| 238 |
// Prepare row for rendering. |
| 239 |
for (int i = 0; i <= MAX_BRIGHTNESS; i++) { |
| 240 |
p_display->pins_for_brightness[i] = 0; |
| 241 |
} |
| 242 |
for (int i = 0; i < COLUMN_COUNT; i++) { |
| 243 |
int x = display_map[i][p_display->strobe_row].x; |
| 244 |
int y = display_map[i][p_display->strobe_row].y; |
| 245 |
int brightness = microbit_display_obj.image_buffer[x][y]; |
| 246 |
p_display->pins_for_brightness[brightness] |= (1<<(i+MIN_COLUMN_PIN)); |
| 247 |
(void)brightness; |
| 248 |
} |
| 249 |
/* Enable the strobe bit for this row */ |
| 250 |
nrf_gpio_pin_set(p_display->strobe_row + MIN_ROW_PIN); |
| 251 |
/* Enable the column bits for all pins that need to be on. */ |
| 252 |
nrf_gpio_port_out_clear(NRF_GPIO, p_display->pins_for_brightness[MAX_BRIGHTNESS]); |
| 253 |
} |
| 254 |
|
| 255 |
static const uint16_t render_timings[] = |
| 256 |
// The scale is (approximately) exponential, |
| 257 |
// each step is approx x1.9 greater than the previous. |
| 258 |
{ 0, // Bright, Ticks Duration, Relative power |
| 259 |
2, // 1, 2, 32us, inf |
| 260 |
2, // 2, 4, 64us, 200% |
| 261 |
4, // 3, 8, 128us, 200% |
| 262 |
7, // 4, 15, 240us, 187% |
| 263 |
13, // 5, 28, 448us, 187% |
| 264 |
25, // 6, 53, 848us, 189% |
| 265 |
49, // 7, 102, 1632us, 192% |
| 266 |
97, // 8, 199, 3184us, 195% |
| 267 |
// Always on 9, 375, 6000us, 188% |
| 268 |
}; |
| 269 |
|
| 270 |
#define DISPLAY_TICKER_SLOT 1 |
| 271 |
|
| 272 |
/* This is the PWM callback. It is registered by the animation callback and |
| 273 |
* will unregister itself when all of the brightness steps are complete. */ |
| 274 |
static int32_t callback(void) { |
| 275 |
microbit_display_obj_t *display = µbit_display_obj; |
| 276 |
mp_uint_t brightness = display->previous_brightness; |
| 277 |
displaySetPinsForRow(display, brightness); |
| 278 |
brightness += 1; |
| 279 |
if (brightness == MAX_BRIGHTNESS) { |
| 280 |
clear_ticker_callback(DISPLAY_TICKER_SLOT); |
| 281 |
return -1; |
| 282 |
} |
| 283 |
display->previous_brightness = brightness; |
| 284 |
// Return interval (in 16us ticks) until next callback |
| 285 |
return render_timings[brightness]; |
| 286 |
} |
| 287 |
|
| 288 |
static void draw_object(mp_obj_t obj) { |
| 289 |
microbit_display_obj_t *display = (microbit_display_obj_t*)MP_STATE_PORT(async_data)[0]; |
| 290 |
if (obj == MP_OBJ_STOP_ITERATION) { |
| 291 |
if (async_clear) { |
| 292 |
microbit_display_show(µbit_display_obj, BLANK_IMAGE); |
| 293 |
async_clear = false; |
| 294 |
} else { |
| 295 |
async_stop(); |
| 296 |
} |
| 297 |
} else if (mp_obj_get_type(obj) == µbit_image_type) { |
| 298 |
microbit_display_show(display, (microbit_image_obj_t *)obj); |
| 299 |
} else if (mp_obj_is_str(obj)) { |
| 300 |
size_t len; |
| 301 |
const char *str = mp_obj_str_get_data(obj, &len); |
| 302 |
if (len == 1) { |
| 303 |
microbit_display_show(display, microbit_image_for_char(str[0])); |
| 304 |
} else { |
| 305 |
async_stop(); |
| 306 |
} |
| 307 |
} else { |
| 308 |
mp_sched_exception(mp_obj_new_exception_msg(&mp_type_TypeError, MP_ERROR_TEXT("not an image."))); |
| 309 |
async_stop(); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
static void microbit_display_update(void) { |
| 314 |
async_tick += MILLISECONDS_PER_MACRO_TICK; |
| 315 |
if (async_tick < async_delay) { |
| 316 |
return; |
| 317 |
} |
| 318 |
async_tick = 0; |
| 319 |
switch (async_mode) { |
| 320 |
case ASYNC_MODE_ANIMATION: |
| 321 |
{ |
| 322 |
if (MP_STATE_PORT(async_data)[0] == NULL || MP_STATE_PORT(async_data)[1] == NULL) { |
| 323 |
async_stop(); |
| 324 |
break; |
| 325 |
} |
| 326 |
/* WARNING: We are executing in an interrupt handler. |
| 327 |
* If an exception is raised here then we must hand it to the VM. */ |
| 328 |
mp_obj_t obj; |
| 329 |
nlr_buf_t nlr; |
| 330 |
gc_lock(); |
| 331 |
if (nlr_push(&nlr) == 0) { |
| 332 |
obj = mp_iternext_allow_raise(async_iterator); |
| 333 |
nlr_pop(); |
| 334 |
gc_unlock(); |
| 335 |
} else { |
| 336 |
gc_unlock(); |
| 337 |
if (!mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(((mp_obj_base_t*)nlr.ret_val)->type), |
| 338 |
MP_OBJ_FROM_PTR(&mp_type_StopIteration))) { |
| 339 |
// An exception other than StopIteration, so set it for the VM to raise later |
| 340 |
// If memory error, write an appropriate message. |
| 341 |
if (mp_obj_get_type(nlr.ret_val) == &mp_type_MemoryError) { |
| 342 |
mp_printf(&mp_plat_print, "Allocation in interrupt handler"); |
| 343 |
} |
| 344 |
mp_sched_exception(MP_OBJ_FROM_PTR(nlr.ret_val)); |
| 345 |
} |
| 346 |
obj = MP_OBJ_STOP_ITERATION; |
| 347 |
} |
| 348 |
draw_object(obj); |
| 349 |
break; |
| 350 |
} |
| 351 |
case ASYNC_MODE_CLEAR: |
| 352 |
microbit_display_show(µbit_display_obj, BLANK_IMAGE); |
| 353 |
async_stop(); |
| 354 |
break; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
#define GREYSCALE_MASK ((1<<MAX_BRIGHTNESS)-2) |
| 359 |
|
| 360 |
/* This is the top-level animation/display registered callback. */ |
| 361 |
void microbit_display_tick(void) { |
| 362 |
/* Do nothing if the display is not active. */ |
| 363 |
if (!microbit_display_obj.active) { |
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
displayAdvanceRow(µbit_display_obj); |
| 368 |
|
| 369 |
microbit_display_update(); |
| 370 |
microbit_display_obj.previous_brightness = 0; |
| 371 |
if (microbit_display_obj.brightnesses & GREYSCALE_MASK) { |
| 372 |
set_ticker_callback(DISPLAY_TICKER_SLOT, callback, 1800); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
void microbit_display_animate(microbit_display_obj_t *self, mp_obj_t iterable, mp_int_t delay, bool clear, bool wait) { |
| 378 |
// Reset the repeat state. |
| 379 |
MP_STATE_PORT(async_data)[0] = NULL; |
| 380 |
MP_STATE_PORT(async_data)[1] = NULL; |
| 381 |
async_iterator = mp_getiter(iterable, NULL); |
| 382 |
async_delay = delay; |
| 383 |
async_clear = clear; |
| 384 |
MP_STATE_PORT(async_data)[0] = self; // so it doesn't get GC'd |
| 385 |
MP_STATE_PORT(async_data)[1] = async_iterator; |
| 386 |
wakeup_event = false; |
| 387 |
mp_obj_t obj = mp_iternext_allow_raise(async_iterator); |
| 388 |
draw_object(obj); |
| 389 |
async_tick = 0; |
| 390 |
async_mode = ASYNC_MODE_ANIMATION; |
| 391 |
if (wait) { |
| 392 |
wait_for_event(); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
// Delay in ms in between moving display one column to the left. |
| 398 |
#define DEFAULT_SCROLL_SPEED 150 |
| 399 |
|
| 400 |
void microbit_display_scroll(microbit_display_obj_t *self, const char* str, bool wait) { |
| 401 |
mp_obj_t iterable = scrolling_string_image_iterable(str, strlen(str), NULL, false, false); |
| 402 |
microbit_display_animate(self, iterable, DEFAULT_SCROLL_SPEED, false, wait); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
mp_obj_t microbit_display_scroll_func(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 407 |
static const mp_arg_t scroll_allowed_args[] = { |
| 408 |
{ MP_QSTR_text, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 409 |
{ MP_QSTR_delay, MP_ARG_INT, {.u_int = DEFAULT_SCROLL_SPEED} }, |
| 410 |
{ MP_QSTR_wait, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} }, |
| 411 |
{ MP_QSTR_monospace, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, |
| 412 |
{ MP_QSTR_loop, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, |
| 413 |
}; |
| 414 |
// Parse the args. |
| 415 |
microbit_display_obj_t *self = (microbit_display_obj_t*)pos_args[0]; |
| 416 |
mp_arg_val_t args[MP_ARRAY_SIZE(scroll_allowed_args)]; |
| 417 |
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(scroll_allowed_args), scroll_allowed_args, args); |
| 418 |
size_t len; |
| 419 |
const char* str = mp_obj_str_get_data(args[0].u_obj, &len); |
| 420 |
mp_obj_t iterable = scrolling_string_image_iterable(str, len, args[0].u_obj, args[3].u_bool /*monospace?*/, args[4].u_bool /*loop*/); |
| 421 |
microbit_display_animate(self, iterable, args[1].u_int /*delay*/, false/*clear*/, args[2].u_bool/*wait?*/); |
| 422 |
return mp_const_none; |
| 423 |
} |
| 424 |
MP_DEFINE_CONST_FUN_OBJ_KW(microbit_display_scroll_obj, 1, microbit_display_scroll_func); |
| 425 |
|
| 426 |
mp_obj_t microbit_display_on_func(mp_obj_t obj) { |
| 427 |
microbit_display_obj_t *self = (microbit_display_obj_t*)obj; |
| 428 |
/* Try to reclaim the pins we need */ |
| 429 |
/* |
| 430 |
microbit_obj_pin_fail_if_cant_acquire(µbit_p3_obj); |
| 431 |
microbit_obj_pin_fail_if_cant_acquire(µbit_p4_obj); |
| 432 |
microbit_obj_pin_fail_if_cant_acquire(µbit_p6_obj); |
| 433 |
microbit_obj_pin_fail_if_cant_acquire(µbit_p7_obj); |
| 434 |
microbit_obj_pin_fail_if_cant_acquire(µbit_p9_obj); |
| 435 |
microbit_obj_pin_fail_if_cant_acquire(µbit_p10_obj); |
| 436 |
microbit_obj_pin_acquire(µbit_p3_obj, microbit_pin_mode_display); |
| 437 |
microbit_obj_pin_acquire(µbit_p4_obj, microbit_pin_mode_display); |
| 438 |
microbit_obj_pin_acquire(µbit_p6_obj, microbit_pin_mode_display); |
| 439 |
microbit_obj_pin_acquire(µbit_p7_obj, microbit_pin_mode_display); |
| 440 |
microbit_obj_pin_acquire(µbit_p9_obj, microbit_pin_mode_display); |
| 441 |
microbit_obj_pin_acquire(µbit_p10_obj, microbit_pin_mode_display); |
| 442 |
*/ |
| 443 |
/* Make sure all pins are in the correct state */ |
| 444 |
microbit_display_init(); |
| 445 |
/* Re-enable the display loop. This will resume any animations in |
| 446 |
* progress and display any static image. */ |
| 447 |
self->active = true; |
| 448 |
return mp_const_none; |
| 449 |
} |
| 450 |
MP_DEFINE_CONST_FUN_OBJ_1(microbit_display_on_obj, microbit_display_on_func); |
| 451 |
|
| 452 |
mp_obj_t microbit_display_off_func(mp_obj_t obj) { |
| 453 |
microbit_display_obj_t *self = (microbit_display_obj_t*)obj; |
| 454 |
/* Disable the display loop. This will pause any animations in progress. |
| 455 |
* It will not prevent a user from attempting to modify the state, but |
| 456 |
* modifications will not appear to have any effect until the display loop |
| 457 |
* is re-enabled. */ |
| 458 |
self->active = false; |
| 459 |
/* Disable the row strobes, allowing the columns to be used freely for |
| 460 |
* GPIO. */ |
| 461 |
nrf_gpio_port_out_clear(0, ROW_PINS_MASK); |
| 462 |
/* Free pins for other uses */ |
| 463 |
/* |
| 464 |
microbit_obj_pin_free(µbit_p3_obj); |
| 465 |
microbit_obj_pin_free(µbit_p4_obj); |
| 466 |
microbit_obj_pin_free(µbit_p6_obj); |
| 467 |
microbit_obj_pin_free(µbit_p7_obj); |
| 468 |
microbit_obj_pin_free(µbit_p9_obj); |
| 469 |
microbit_obj_pin_free(µbit_p10_obj); |
| 470 |
*/ |
| 471 |
return mp_const_none; |
| 472 |
} |
| 473 |
MP_DEFINE_CONST_FUN_OBJ_1(microbit_display_off_obj, microbit_display_off_func); |
| 474 |
|
| 475 |
mp_obj_t microbit_display_is_on_func(mp_obj_t obj) { |
| 476 |
microbit_display_obj_t *self = (microbit_display_obj_t*)obj; |
| 477 |
if (self->active) { |
| 478 |
return mp_const_true; |
| 479 |
} |
| 480 |
else { |
| 481 |
return mp_const_false; |
| 482 |
} |
| 483 |
} |
| 484 |
MP_DEFINE_CONST_FUN_OBJ_1(microbit_display_is_on_obj, microbit_display_is_on_func); |
| 485 |
|
| 486 |
void microbit_display_clear(void) { |
| 487 |
// Reset repeat state, cancel animation and clear screen. |
| 488 |
wakeup_event = false; |
| 489 |
async_mode = ASYNC_MODE_CLEAR; |
| 490 |
async_tick = async_delay - MILLISECONDS_PER_MACRO_TICK; |
| 491 |
wait_for_event(); |
| 492 |
} |
| 493 |
|
| 494 |
mp_obj_t microbit_display_clear_func(mp_obj_t self_in) { |
| 495 |
microbit_display_clear(); |
| 496 |
return mp_const_none; |
| 497 |
} |
| 498 |
MP_DEFINE_CONST_FUN_OBJ_1(microbit_display_clear_obj, microbit_display_clear_func); |
| 499 |
|
| 500 |
void microbit_display_set_pixel(microbit_display_obj_t *display, mp_int_t x, mp_int_t y, mp_int_t bright) { |
| 501 |
if (x < 0 || y < 0 || x > 4 || y > 4) { |
| 502 |
mp_raise_ValueError(MP_ERROR_TEXT("index out of bounds.")); |
| 503 |
} |
| 504 |
if (bright < 0 || bright > MAX_BRIGHTNESS) { |
| 505 |
mp_raise_ValueError(MP_ERROR_TEXT("brightness out of bounds.")); |
| 506 |
} |
| 507 |
display->image_buffer[x][y] = bright; |
| 508 |
display->brightnesses |= (1 << bright); |
| 509 |
} |
| 510 |
|
| 511 |
static mp_obj_t microbit_display_set_pixel_func(mp_uint_t n_args, const mp_obj_t *args) { |
| 512 |
(void)n_args; |
| 513 |
microbit_display_obj_t *self = (microbit_display_obj_t*)args[0]; |
| 514 |
microbit_display_set_pixel(self, mp_obj_get_int(args[1]), mp_obj_get_int(args[2]), mp_obj_get_int(args[3])); |
| 515 |
return mp_const_none; |
| 516 |
} |
| 517 |
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(microbit_display_set_pixel_obj, 4, 4, microbit_display_set_pixel_func); |
| 518 |
|
| 519 |
mp_int_t microbit_display_get_pixel(microbit_display_obj_t *display, mp_int_t x, mp_int_t y) { |
| 520 |
if (x < 0 || y < 0 || x > 4 || y > 4) { |
| 521 |
mp_raise_ValueError(MP_ERROR_TEXT("index out of bounds.")); |
| 522 |
} |
| 523 |
return display->image_buffer[x][y]; |
| 524 |
} |
| 525 |
|
| 526 |
static mp_obj_t microbit_display_get_pixel_func(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in) { |
| 527 |
microbit_display_obj_t *self = (microbit_display_obj_t*)self_in; |
| 528 |
return MP_OBJ_NEW_SMALL_INT(microbit_display_get_pixel(self, mp_obj_get_int(x_in), mp_obj_get_int(y_in))); |
| 529 |
} |
| 530 |
MP_DEFINE_CONST_FUN_OBJ_3(microbit_display_get_pixel_obj, microbit_display_get_pixel_func); |
| 531 |
|
| 532 |
static const mp_rom_map_elem_t microbit_display_locals_dict_table[] = { |
| 533 |
{ MP_ROM_QSTR(MP_QSTR_get_pixel), MP_ROM_PTR(µbit_display_get_pixel_obj) }, |
| 534 |
{ MP_ROM_QSTR(MP_QSTR_set_pixel), MP_ROM_PTR(µbit_display_set_pixel_obj) }, |
| 535 |
{ MP_ROM_QSTR(MP_QSTR_show), MP_ROM_PTR(µbit_display_show_obj) }, |
| 536 |
{ MP_ROM_QSTR(MP_QSTR_scroll), MP_ROM_PTR(µbit_display_scroll_obj) }, |
| 537 |
{ MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(µbit_display_clear_obj) }, |
| 538 |
{ MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(µbit_display_on_obj) }, |
| 539 |
{ MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(µbit_display_off_obj) }, |
| 540 |
{ MP_ROM_QSTR(MP_QSTR_is_on), MP_ROM_PTR(µbit_display_is_on_obj) }, |
| 541 |
}; |
| 542 |
|
| 543 |
static MP_DEFINE_CONST_DICT(microbit_display_locals_dict, microbit_display_locals_dict_table); |
| 544 |
|
| 545 |
MP_DEFINE_CONST_OBJ_TYPE( |
| 546 |
microbit_display_type, |
| 547 |
MP_QSTR_MicroBitDisplay, |
| 548 |
MP_TYPE_FLAG_NONE, |
| 549 |
locals_dict, µbit_display_locals_dict |
| 550 |
); |
| 551 |
|
| 552 |
microbit_display_obj_t microbit_display_obj = { |
| 553 |
{µbit_display_type}, |
| 554 |
{{ 0, }}, |
| 555 |
.previous_brightness = 0, |
| 556 |
.active = 1, |
| 557 |
.strobe_row = 0, |
| 558 |
.brightnesses = 0, |
| 559 |
.pins_for_brightness = { 0 }, |
| 560 |
}; |
| 561 |
|
| 562 |
void microbit_display_init(void) { |
| 563 |
// Set pins as output. |
| 564 |
for (int i = MIN_COLUMN_PIN; i <= MAX_ROW_PIN; i++) { |
| 565 |
nrf_gpio_cfg_output(i); |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
MP_REGISTER_ROOT_POINTER(void *async_data[2]); |