Browse Source

nrf/boards/microbit: Update board modules from C++ to C-code.

This aligns implementation with new style structures.
pull/3137/merge
Glenn Ruben Bakke 7 years ago
committed by Damien George
parent
commit
fbc45bd3f3
  1. 6
      ports/nrf/boards/microbit/modules/iters.c
  2. 7
      ports/nrf/boards/microbit/modules/microbitconstimage.c
  3. 142
      ports/nrf/boards/microbit/modules/microbitconstimage.h
  4. 2
      ports/nrf/boards/microbit/modules/microbitconstimagetuples.c
  5. 100
      ports/nrf/boards/microbit/modules/microbitdisplay.c
  6. 11
      ports/nrf/boards/microbit/modules/microbitdisplay.h
  7. 154
      ports/nrf/boards/microbit/modules/microbitfont.h
  8. 345
      ports/nrf/boards/microbit/modules/microbitimage.c
  9. 41
      ports/nrf/boards/microbit/modules/microbitimage.h
  10. 31
      ports/nrf/boards/microbit/modules/modmicrobit.c

6
ports/nrf/boards/microbit/modules/iters.c

@ -25,7 +25,7 @@
*/
#include "py/runtime.h"
#include "lib/iters.h"
#include "iters.h"
typedef struct _repeat_iterator_t {
@ -53,11 +53,9 @@ const mp_obj_type_t microbit_repeat_iterator_type = {
.binary_op = NULL,
.attr = NULL,
.subscr = NULL,
.getiter = mp_identity,
.getiter = mp_identity_getiter,
.iternext = microbit_repeat_iter_next,
.buffer_p = {NULL},
.stream_p = NULL,
.bases_tuple = MP_OBJ_NULL,
MP_OBJ_NULL
};

7
ports/nrf/boards/microbit/modules/microbitconstimage.c

@ -24,12 +24,7 @@
* THE SOFTWARE.
*/
#include "microbitobj.h"
extern "C" {
#include "py/runtime.h"
#include "modmicrobit.h"
#include "microbitimage.h"
@ -558,5 +553,3 @@ IMAGE_T microbit_const_image_snake_obj = SMALL_IMAGE(
0,1,1,1,0,
0,0,0,0,0
);
}

142
ports/nrf/boards/microbit/modules/microbitconstimage.h

@ -23,34 +23,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_MICROBIT_MODMICROBIT_H__
#define __MICROPY_INCLUDED_MICROBIT_MODMICROBIT_H__
#include "py/objtuple.h"
#ifndef __MICROPY_INCLUDED_MICROBIT_CONSTIMAGE_H__
#define __MICROPY_INCLUDED_MICROBIT_CONSTIMAGE_H__
extern const mp_obj_type_t microbit_ad_pin_type;
extern const mp_obj_type_t microbit_dig_pin_type;
extern const mp_obj_type_t microbit_touch_pin_type;
extern const struct _microbit_pin_obj_t microbit_p0_obj;
extern const struct _microbit_pin_obj_t microbit_p1_obj;
extern const struct _microbit_pin_obj_t microbit_p2_obj;
extern const struct _microbit_pin_obj_t microbit_p3_obj;
extern const struct _microbit_pin_obj_t microbit_p4_obj;
extern const struct _microbit_pin_obj_t microbit_p5_obj;
extern const struct _microbit_pin_obj_t microbit_p6_obj;
extern const struct _microbit_pin_obj_t microbit_p7_obj;
extern const struct _microbit_pin_obj_t microbit_p8_obj;
extern const struct _microbit_pin_obj_t microbit_p9_obj;
extern const struct _microbit_pin_obj_t microbit_p10_obj;
extern const struct _microbit_pin_obj_t microbit_p11_obj;
extern const struct _microbit_pin_obj_t microbit_p12_obj;
extern const struct _microbit_pin_obj_t microbit_p13_obj;
extern const struct _microbit_pin_obj_t microbit_p14_obj;
extern const struct _microbit_pin_obj_t microbit_p15_obj;
extern const struct _microbit_pin_obj_t microbit_p16_obj;
extern const struct _microbit_pin_obj_t microbit_p19_obj;
extern const struct _microbit_pin_obj_t microbit_p20_obj;
extern const mp_obj_type_t microbit_const_image_type;
extern const struct _monochrome_5by5_t microbit_const_image_heart_obj;
@ -119,116 +95,4 @@ extern const struct _monochrome_5by5_t microbit_const_image_skull_obj;
extern const struct _monochrome_5by5_t microbit_const_image_umbrella_obj;
extern const struct _monochrome_5by5_t microbit_const_image_snake_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_dadadadum_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_entertainer_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_prelude_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_ode_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_nyan_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_ringtone_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_funk_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_blues_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_birthday_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_wedding_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_funeral_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_punchline_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_python_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_baddy_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_chase_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_ba_ding_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_wawawawaa_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_jump_up_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_jump_down_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_power_up_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_power_down_obj;
extern const mp_obj_type_t microbit_image_type;
extern const mp_obj_type_t microbit_accelerometer_type;
extern const struct _microbit_accelerometer_obj_t microbit_accelerometer_obj;
extern struct _microbit_display_obj_t microbit_display_obj;
extern const struct _microbit_button_obj_t microbit_button_a_obj;
extern const struct _microbit_button_obj_t microbit_button_b_obj;
extern const struct _microbit_compass_obj_t microbit_compass_obj;
extern const struct _microbit_i2c_obj_t microbit_i2c_obj;
extern struct _microbit_uart_obj_t microbit_uart_obj;
extern struct _microbit_spi_obj_t microbit_spi_obj;
MP_DECLARE_CONST_FUN_OBJ(microbit_reset_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_sleep_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_random_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_running_time_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_temperature_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_panic_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_accelerometer_get_x_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_accelerometer_get_y_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_accelerometer_get_z_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_button_is_pressed_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_button_was_pressed_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_button_get_presses_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_compass_is_calibrated_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_compass_heading_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_compass_calibrate_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_compass_is_calibrating_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_compass_clear_calibration_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_compass_get_x_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_compass_get_y_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_compass_get_z_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_compass_get_field_strength_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_display_show_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_display_scroll_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_display_clear_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_display_get_pixel_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_display_set_pixel_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_display_on_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_display_off_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_display_is_on_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_pin_read_digital_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_pin_write_digital_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_pin_read_analog_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_pin_write_analog_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_pin_is_touched_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_pin_set_analog_period_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_pin_set_analog_period_microseconds_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_i2c_init_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_i2c_read_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_i2c_write_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_image_width_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_image_height_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_image_get_pixel_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_image_set_pixel_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_image_shift_left_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_image_shift_right_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_image_shift_up_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_image_shift_down_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_image_copy_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_image_crop_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_image_invert_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_image_slice_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_uart_init_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_uart_any_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_stream_read_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_stream_readall_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_stream_unbuffered_readline_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_stream_readinto_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_stream_write_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_spi_init_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_spi_write_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_spi_read_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_spi_write_readinto_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_music_set_tempo_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_music_pitch_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_music_play_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_music_get_tempo_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_music_stop_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_music_reset_obj);
MP_DECLARE_CONST_FUN_OBJ(love_badaboom_obj);
MP_DECLARE_CONST_FUN_OBJ(this_authors_obj);
extern const mp_obj_module_t microbit_module;
extern const mp_obj_module_t music_module;
extern const mp_obj_module_t love_module;
extern const mp_obj_module_t antigravity_module;
extern const mp_obj_module_t this_module;
#endif // __MICROPY_INCLUDED_MICROBIT_MODMICROBIT_H__
#endif // __MICROPY_INCLUDED_MICROBIT_CONSTIMAGE_H__

2
ports/nrf/boards/microbit/modules/microbitconstimagetuples.c

@ -23,7 +23,7 @@
*/
#include "py/runtime.h"
#include "modmicrobit.h"
#include "microbitconstimage.h"
const mp_obj_tuple_t microbit_const_image_all_clocks_tuple_obj = {
{&mp_type_tuple},

100
ports/nrf/boards/microbit/modules/microbitdisplay.c

@ -25,30 +25,26 @@
*/
#include <string.h>
#include "microbitobj.h"
#include "nrf_gpio.h"
extern "C" {
#include "hal_gpio.h"
#include "py/obj.h"
#include "py/runtime.h"
#include "py/gc.h"
#include "modmicrobit.h"
#include "microbitimage.h"
#include "microbitdisplay.h"
#include "microbitpin.h"
#include "lib/iters.h"
#include "lib/ticker.h"
#include "iters.h"
#include "ticker.h"
#define min(a,b) (((a)<(b))?(a):(b))
void microbit_display_show(microbit_display_obj_t *display, microbit_image_obj_t *image) {
mp_int_t w = min(image->width(), 5);
mp_int_t h = min(image->height(), 5);
mp_int_t w = min(imageWidth(image), 5);
mp_int_t h = min(imageHeight(image), 5);
mp_int_t x = 0;
mp_int_t brightnesses = 0;
for (; x < w; ++x) {
mp_int_t y = 0;
for (; y < h; ++y) {
uint8_t pix = image->getPixelValue(x, y);
uint8_t pix = imageGetPixelValue(image, x, y);
display->image_buffer[x][y] = pix;
brightnesses |= (1 << pix);
}
@ -162,10 +158,10 @@ STATIC void wait_for_event() {
wakeup_event = false;
}
struct DisplayPoint {
typedef struct {
uint8_t x;
uint8_t y;
};
} DisplayPoint;
#define NO_CONN 0
@ -190,11 +186,11 @@ static const DisplayPoint display_map[COLUMN_COUNT][ROW_COUNT] = {
#define MAX_ROW_PIN 15
#define ROW_PINS_MASK 0xe000
inline void microbit_display_obj_t::setPinsForRow(uint8_t brightness) {
static inline void displaySetPinsForRow(microbit_display_obj_t * p_display, uint8_t brightness) {
if (brightness == 0) {
nrf_gpio_pins_clear(COLUMN_PINS_MASK & ~this->pins_for_brightness[brightness]);
hal_gpio_out_clear(0, COLUMN_PINS_MASK & ~p_display->pins_for_brightness[brightness]);
} else {
nrf_gpio_pins_set(this->pins_for_brightness[brightness]);
hal_gpio_out_set(0, p_display->pins_for_brightness[brightness]);
}
}
@ -221,38 +217,39 @@ inline void microbit_display_obj_t::setPinsForRow(uint8_t brightness) {
* Else
* Re-queue the PWM callback after the appropriate delay
*/
void microbit_display_obj_t::advanceRow() {
static void displayAdvanceRow(microbit_display_obj_t * p_display) {
/* Clear all of the column bits */
nrf_gpio_pins_set(COLUMN_PINS_MASK);
hal_gpio_out_set(0, COLUMN_PINS_MASK);
/* Clear the strobe bit for this row */
nrf_gpio_pin_clear(strobe_row+MIN_ROW_PIN);
hal_gpio_pin_clear(0, p_display->strobe_row + MIN_ROW_PIN);
/* Move to the next row. Before this, "this row" refers to the row
* manipulated by the previous invocation of this function. After this,
* "this row" refers to the row manipulated by the current invocation of
* this function. */
strobe_row++;
p_display->strobe_row++;
// Reset the row counts and bit mask when we have hit the max.
if (strobe_row == ROW_COUNT) {
strobe_row = 0;
if (p_display->strobe_row == ROW_COUNT) {
p_display->strobe_row = 0;
}
// Set pin for this row.
// Prepare row for rendering.
for (int i = 0; i <= MAX_BRIGHTNESS; i++) {
pins_for_brightness[i] = 0;
p_display->pins_for_brightness[i] = 0;
}
for (int i = 0; i < COLUMN_COUNT; i++) {
int x = display_map[i][strobe_row].x;
int y = display_map[i][strobe_row].y;
uint8_t brightness = microbit_display_obj.image_buffer[x][y];
pins_for_brightness[brightness] |= (1<<(i+MIN_COLUMN_PIN));
int x = display_map[i][p_display->strobe_row].x;
int y = display_map[i][p_display->strobe_row].y;
int brightness = microbit_display_obj.image_buffer[x][y];
p_display->pins_for_brightness[brightness] |= (1<<(i+MIN_COLUMN_PIN));
(void)brightness;
}
/* Enable the strobe bit for this row */
nrf_gpio_pin_set(strobe_row+MIN_ROW_PIN);
hal_gpio_pin_set(0, p_display->strobe_row + MIN_ROW_PIN);
/* Enable the column bits for all pins that need to be on. */
nrf_gpio_pins_clear(pins_for_brightness[MAX_BRIGHTNESS]);
hal_gpio_out_clear(0, p_display->pins_for_brightness[MAX_BRIGHTNESS]);
}
static const uint16_t render_timings[] =
@ -277,7 +274,7 @@ static const uint16_t render_timings[] =
static int32_t callback(void) {
microbit_display_obj_t *display = &microbit_display_obj;
mp_uint_t brightness = display->previous_brightness;
display->setPinsForRow(brightness);
displaySetPinsForRow(display, brightness);
brightness += 1;
if (brightness == MAX_BRIGHTNESS) {
clear_ticker_callback(DISPLAY_TICKER_SLOT);
@ -368,7 +365,7 @@ void microbit_display_tick(void) {
return;
}
microbit_display_obj.advanceRow();
displayAdvanceRow(&microbit_display_obj);
microbit_display_update();
microbit_display_obj.previous_brightness = 0;
@ -382,7 +379,7 @@ void microbit_display_animate(microbit_display_obj_t *self, mp_obj_t iterable, m
// Reset the repeat state.
MP_STATE_PORT(async_data)[0] = NULL;
MP_STATE_PORT(async_data)[1] = NULL;
async_iterator = mp_getiter(iterable);
async_iterator = mp_getiter(iterable, NULL);
async_delay = delay;
async_clear = clear;
MP_STATE_PORT(async_data)[0] = self; // so it doesn't get GC'd
@ -430,6 +427,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(microbit_display_scroll_obj, 1, microbit_display_scro
mp_obj_t microbit_display_on_func(mp_obj_t obj) {
microbit_display_obj_t *self = (microbit_display_obj_t*)obj;
/* Try to reclaim the pins we need */
/*
microbit_obj_pin_fail_if_cant_acquire(&microbit_p3_obj);
microbit_obj_pin_fail_if_cant_acquire(&microbit_p4_obj);
microbit_obj_pin_fail_if_cant_acquire(&microbit_p6_obj);
@ -442,6 +440,7 @@ mp_obj_t microbit_display_on_func(mp_obj_t obj) {
microbit_obj_pin_acquire(&microbit_p7_obj, microbit_pin_mode_display);
microbit_obj_pin_acquire(&microbit_p9_obj, microbit_pin_mode_display);
microbit_obj_pin_acquire(&microbit_p10_obj, microbit_pin_mode_display);
*/
/* Make sure all pins are in the correct state */
microbit_display_init();
/* Re-enable the display loop. This will resume any animations in
@ -460,14 +459,16 @@ mp_obj_t microbit_display_off_func(mp_obj_t obj) {
self->active = false;
/* Disable the row strobes, allowing the columns to be used freely for
* GPIO. */
nrf_gpio_pins_clear(ROW_PINS_MASK);
hal_gpio_out_clear(0, ROW_PINS_MASK);
/* Free pins for other uses */
/*
microbit_obj_pin_free(&microbit_p3_obj);
microbit_obj_pin_free(&microbit_p4_obj);
microbit_obj_pin_free(&microbit_p6_obj);
microbit_obj_pin_free(&microbit_p7_obj);
microbit_obj_pin_free(&microbit_p9_obj);
microbit_obj_pin_free(&microbit_p10_obj);
*/
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_display_off_obj, microbit_display_off_func);
@ -491,7 +492,7 @@ void microbit_display_clear(void) {
wait_for_event();
}
mp_obj_t microbit_display_clear_func(void) {
mp_obj_t microbit_display_clear_func(mp_obj_t self_in) {
microbit_display_clear();
return mp_const_none;
}
@ -529,21 +530,20 @@ STATIC mp_obj_t microbit_display_get_pixel_func(mp_obj_t self_in, mp_obj_t x_in,
}
MP_DEFINE_CONST_FUN_OBJ_3(microbit_display_get_pixel_obj, microbit_display_get_pixel_func);
STATIC const mp_map_elem_t microbit_display_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_pixel), (mp_obj_t)&microbit_display_get_pixel_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_pixel), (mp_obj_t)&microbit_display_set_pixel_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_show), (mp_obj_t)&microbit_display_show_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_scroll), (mp_obj_t)&microbit_display_scroll_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&microbit_display_clear_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_on), (mp_obj_t)&microbit_display_on_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_off), (mp_obj_t)&microbit_display_off_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_is_on), (mp_obj_t)&microbit_display_is_on_obj },
STATIC const mp_rom_map_elem_t microbit_display_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_get_pixel), MP_ROM_PTR(&microbit_display_get_pixel_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_pixel), MP_ROM_PTR(&microbit_display_set_pixel_obj) },
{ MP_ROM_QSTR(MP_QSTR_show), MP_ROM_PTR(&microbit_display_show_obj) },
{ MP_ROM_QSTR(MP_QSTR_scroll), MP_ROM_PTR(&microbit_display_scroll_obj) },
{ MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&microbit_display_clear_obj) },
{ MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&microbit_display_on_obj) },
{ MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&microbit_display_off_obj) },
{ MP_ROM_QSTR(MP_QSTR_is_on), MP_ROM_PTR(&microbit_display_is_on_obj) },
};
STATIC MP_DEFINE_CONST_DICT(microbit_display_locals_dict, microbit_display_locals_dict_table);
STATIC const mp_obj_type_t microbit_display_type = {
const mp_obj_type_t microbit_display_type = {
{ &mp_type_type },
.name = MP_QSTR_MicroBitDisplay,
.print = NULL,
@ -556,14 +556,12 @@ STATIC const mp_obj_type_t microbit_display_type = {
.getiter = NULL,
.iternext = NULL,
.buffer_p = {NULL},
.stream_p = NULL,
.bases_tuple = NULL,
.locals_dict = (mp_obj_dict_t*)&microbit_display_locals_dict,
};
microbit_display_obj_t microbit_display_obj = {
{&microbit_display_type},
{ 0 },
{{ 0, }},
.previous_brightness = 0,
.active = 1,
.strobe_row = 0,
@ -573,7 +571,7 @@ microbit_display_obj_t microbit_display_obj = {
void microbit_display_init(void) {
// Set pins as output.
nrf_gpio_range_cfg_output(MIN_COLUMN_PIN, MIN_COLUMN_PIN + COLUMN_COUNT + ROW_COUNT);
}
for (int i = MIN_COLUMN_PIN; i <= MAX_ROW_PIN; i++) {
hal_gpio_cfg_pin(0, i, HAL_GPIO_MODE_OUTPUT, HAL_GPIO_PULL_DOWN);
}
}

11
ports/nrf/boards/microbit/modules/microbitdisplay.h

@ -15,11 +15,6 @@ typedef struct _microbit_display_obj_t {
/* boolean histogram of brightness in buffer */
uint16_t brightnesses;
uint16_t pins_for_brightness[MAX_BRIGHTNESS+1];
void advanceRow();
inline void setPinsForRow(uint8_t brightness);
} microbit_display_obj_t;
#define ASYNC_MODE_STOPPED 0
@ -27,9 +22,7 @@ typedef struct _microbit_display_obj_t {
#define ASYNC_MODE_CLEAR 2
extern microbit_display_obj_t microbit_display_obj;
extern "C" {
extern const mp_obj_type_t microbit_image_type;
void microbit_display_show(microbit_display_obj_t *display, microbit_image_obj_t *image);
@ -49,6 +42,4 @@ void microbit_display_tick(void);
bool microbit_display_active_animation(void);
}
#endif // __MICROPY_INCLUDED_MICROBIT_DISPLAY_H__

154
ports/nrf/boards/microbit/modules/microbitfont.h

@ -24,9 +24,6 @@ DEALINGS IN THE SOFTWARE.
*/
/**
* Class definition for a MicrobitFont
* This class represents a font that can be used by the display to render text.
*
* A MicroBitFont is 5x5.
* Each Row is represented by a byte in the array.
*
@ -44,56 +41,105 @@ DEALINGS IN THE SOFTWARE.
* We could compress further, but the complexity of decode would likely outweigh the gains.
*/
#include "MicroBitConfig.h"
#include "MicroBitFont.h"
const unsigned char pendolino3[475] = {
0x0, 0x0, 0x0, 0x0, 0x0, 0x8, 0x8, 0x8, 0x0, 0x8, 0xa, 0x4a, 0x40, 0x0, 0x0, 0xa, 0x5f, 0xea, 0x5f, 0xea, 0xe, 0xd9, 0x2e, 0xd3, 0x6e, 0x19, 0x32, 0x44, 0x89, 0x33, 0xc, 0x92, 0x4c, 0x92, 0x4d, 0x8, 0x8, 0x0, 0x0, 0x0, 0x4, 0x88, 0x8, 0x8, 0x4, 0x8, 0x4, 0x84, 0x84, 0x88, 0x0, 0xa, 0x44, 0x8a, 0x40, 0x0, 0x4, 0x8e, 0xc4, 0x80, 0x0, 0x0, 0x0, 0x4, 0x88, 0x0, 0x0, 0xe, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x8, 0x0, 0x1, 0x22, 0x44, 0x88, 0x10, 0xc, 0x92, 0x52, 0x52, 0x4c, 0x4, 0x8c, 0x84, 0x84, 0x8e, 0x1c, 0x82, 0x4c, 0x90, 0x1e, 0x1e, 0xc2, 0x44, 0x92, 0x4c, 0x6, 0xca, 0x52, 0x5f, 0xe2, 0x1f, 0xf0, 0x1e, 0xc1, 0x3e, 0x2, 0x44, 0x8e, 0xd1, 0x2e, 0x1f, 0xe2, 0x44, 0x88, 0x10, 0xe, 0xd1, 0x2e, 0xd1, 0x2e, 0xe, 0xd1, 0x2e, 0xc4, 0x88, 0x0, 0x8, 0x0, 0x8, 0x0, 0x0, 0x4, 0x80, 0x4, 0x88, 0x2, 0x44, 0x88, 0x4, 0x82, 0x0, 0xe, 0xc0, 0xe, 0xc0, 0x8, 0x4, 0x82, 0x44, 0x88, 0xe, 0xd1, 0x26, 0xc0, 0x4, 0xe, 0xd1, 0x35, 0xb3, 0x6c, 0xc, 0x92, 0x5e, 0xd2, 0x52, 0x1c, 0x92, 0x5c, 0x92, 0x5c, 0xe, 0xd0, 0x10, 0x10, 0xe, 0x1c, 0x92, 0x52, 0x52, 0x5c, 0x1e, 0xd0, 0x1c, 0x90, 0x1e, 0x1e, 0xd0, 0x1c, 0x90, 0x10, 0xe, 0xd0, 0x13, 0x71, 0x2e, 0x12, 0x52, 0x5e, 0xd2, 0x52, 0x1c, 0x88, 0x8, 0x8, 0x1c, 0x1f, 0xe2, 0x42, 0x52, 0x4c, 0x12, 0x54, 0x98, 0x14, 0x92, 0x10, 0x10, 0x10, 0x10, 0x1e, 0x11, 0x3b, 0x75, 0xb1, 0x31, 0x11, 0x39, 0x35, 0xb3, 0x71, 0xc, 0x92, 0x52, 0x52, 0x4c, 0x1c, 0x92, 0x5c, 0x90, 0x10, 0xc, 0x92, 0x52, 0x4c, 0x86, 0x1c, 0x92, 0x5c, 0x92, 0x51, 0xe, 0xd0, 0xc, 0x82, 0x5c, 0x1f, 0xe4, 0x84, 0x84, 0x84, 0x12, 0x52, 0x52, 0x52, 0x4c, 0x11, 0x31, 0x31, 0x2a, 0x44, 0x11, 0x31, 0x35, 0xbb, 0x71, 0x12, 0x52, 0x4c, 0x92, 0x52, 0x11, 0x2a, 0x44, 0x84, 0x84, 0x1e, 0xc4, 0x88, 0x10, 0x1e, 0xe, 0xc8, 0x8, 0x8, 0xe, 0x10, 0x8, 0x4, 0x82, 0x41, 0xe, 0xc2, 0x42, 0x42, 0x4e, 0x4, 0x8a, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1f, 0x8, 0x4, 0x80, 0x0, 0x0, 0x0, 0xe, 0xd2, 0x52, 0x4f, 0x10, 0x10, 0x1c, 0x92, 0x5c, 0x0, 0xe, 0xd0, 0x10, 0xe, 0x2, 0x42, 0x4e, 0xd2, 0x4e, 0xc, 0x92, 0x5c, 0x90, 0xe, 0x6, 0xc8, 0x1c, 0x88, 0x8, 0xe, 0xd2, 0x4e, 0xc2, 0x4c, 0x10, 0x10, 0x1c, 0x92, 0x52, 0x8, 0x0, 0x8, 0x8, 0x8, 0x2, 0x40, 0x2, 0x42, 0x4c, 0x10, 0x14, 0x98, 0x14, 0x92, 0x8, 0x8, 0x8, 0x8, 0x6, 0x0, 0x1b, 0x75, 0xb1, 0x31, 0x0, 0x1c, 0x92, 0x52, 0x52, 0x0, 0xc, 0x92, 0x52, 0x4c, 0x0, 0x1c, 0x92, 0x5c, 0x90, 0x0, 0xe, 0xd2, 0x4e, 0xc2, 0x0, 0xe, 0xd0, 0x10, 0x10, 0x0, 0x6, 0xc8, 0x4, 0x98, 0x8, 0x8, 0xe, 0xc8, 0x7, 0x0, 0x12, 0x52, 0x52, 0x4f, 0x0, 0x11, 0x31, 0x2a, 0x44, 0x0, 0x11, 0x31, 0x35, 0xbb, 0x0, 0x12, 0x4c, 0x8c, 0x92, 0x0, 0x11, 0x2a, 0x44, 0x98, 0x0, 0x1e, 0xc4, 0x88, 0x1e, 0x6, 0xc4, 0x8c, 0x84, 0x86, 0x8, 0x8, 0x8, 0x8, 0x8, 0x18, 0x8, 0xc, 0x88, 0x18, 0x0, 0x0, 0xc, 0x83, 0x60};
#ifndef MICROPY_INCLUDED_NRF_BOARD_MICROBIT_MICROBITFONT_H
#define MICROPY_INCLUDED_NRF_BOARD_MICROBIT_MICROBITFONT_H
const unsigned char* MicroBitFont::defaultFont = pendolino3;
MicroBitFont MicroBitFont::systemFont = MicroBitFont(defaultFont, MICROBIT_FONT_ASCII_END);
/**
* Constructor.
*
* Sets the font represented by this font object.
*
* @param font A pointer to the beginning of the new font.
*
* @param asciiEnd the char value at which this font finishes.
*/
MicroBitFont::MicroBitFont(const unsigned char* characters, int asciiEnd)
{
this->characters = characters;
this->asciiEnd = asciiEnd;
}
const unsigned char font_pendolino3_5x5_pad3msb[475] = {
0x0, 0x0, 0x0, 0x0, 0x0,
0x8, 0x8, 0x8, 0x0, 0x8,
0xa, 0x4a, 0x40, 0x0, 0x0,
0xa, 0x5f, 0xea, 0x5f, 0xea,
0xe, 0xd9, 0x2e, 0xd3, 0x6e,
0x19, 0x32, 0x44, 0x89, 0x33,
0xc, 0x92, 0x4c, 0x92, 0x4d,
0x8, 0x8, 0x0, 0x0, 0x0,
0x4, 0x88, 0x8, 0x8, 0x4,
0x8, 0x4, 0x84, 0x84, 0x88,
0x0, 0xa, 0x44, 0x8a, 0x40,
0x0, 0x4, 0x8e, 0xc4, 0x80,
0x0, 0x0, 0x0, 0x4, 0x88,
0x0, 0x0, 0xe, 0xc0, 0x0,
0x0, 0x0, 0x0, 0x8, 0x0,
0x1, 0x22, 0x44, 0x88, 0x10,
0xc, 0x92, 0x52, 0x52, 0x4c,
0x4, 0x8c, 0x84, 0x84, 0x8e,
0x1c, 0x82, 0x4c, 0x90, 0x1e,
0x1e, 0xc2, 0x44, 0x92, 0x4c,
0x6, 0xca, 0x52, 0x5f, 0xe2,
0x1f, 0xf0, 0x1e, 0xc1, 0x3e,
0x2, 0x44, 0x8e, 0xd1, 0x2e,
0x1f, 0xe2, 0x44, 0x88, 0x10,
0xe, 0xd1, 0x2e, 0xd1, 0x2e,
0xe, 0xd1, 0x2e, 0xc4, 0x88,
0x0, 0x8, 0x0, 0x8, 0x0,
0x0, 0x4, 0x80, 0x4, 0x88,
0x2, 0x44, 0x88, 0x4, 0x82,
0x0, 0xe, 0xc0, 0xe, 0xc0,
0x8, 0x4, 0x82, 0x44, 0x88,
0xe, 0xd1, 0x26, 0xc0, 0x4,
0xe, 0xd1, 0x35, 0xb3, 0x6c,
0xc, 0x92, 0x5e, 0xd2, 0x52,
0x1c, 0x92, 0x5c, 0x92, 0x5c,
0xe, 0xd0, 0x10, 0x10, 0xe,
0x1c, 0x92, 0x52, 0x52, 0x5c,
0x1e, 0xd0, 0x1c, 0x90, 0x1e,
0x1e, 0xd0, 0x1c, 0x90, 0x10,
0xe, 0xd0, 0x13, 0x71, 0x2e,
0x12, 0x52, 0x5e, 0xd2, 0x52,
0x1c, 0x88, 0x8, 0x8, 0x1c,
0x1f, 0xe2, 0x42, 0x52, 0x4c,
0x12, 0x54, 0x98, 0x14, 0x92,
0x10, 0x10, 0x10, 0x10, 0x1e,
0x11, 0x3b, 0x75, 0xb1, 0x31,
0x11, 0x39, 0x35, 0xb3, 0x71,
0xc, 0x92, 0x52, 0x52, 0x4c,
0x1c, 0x92, 0x5c, 0x90, 0x10,
0xc, 0x92, 0x52, 0x4c, 0x86,
0x1c, 0x92, 0x5c, 0x92, 0x51,
0xe, 0xd0, 0xc, 0x82, 0x5c,
0x1f, 0xe4, 0x84, 0x84, 0x84,
0x12, 0x52, 0x52, 0x52, 0x4c,
0x11, 0x31, 0x31, 0x2a, 0x44,
0x11, 0x31, 0x35, 0xbb, 0x71,
0x12, 0x52, 0x4c, 0x92, 0x52,
0x11, 0x2a, 0x44, 0x84, 0x84,
0x1e, 0xc4, 0x88, 0x10, 0x1e,
0xe, 0xc8, 0x8, 0x8, 0xe,
0x10, 0x8, 0x4, 0x82, 0x41,
0xe, 0xc2, 0x42, 0x42, 0x4e,
0x4, 0x8a, 0x40, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x1f,
0x8, 0x4, 0x80, 0x0, 0x0,
0x0, 0xe, 0xd2, 0x52, 0x4f,
0x10, 0x10, 0x1c, 0x92, 0x5c,
0x0, 0xe, 0xd0, 0x10, 0xe,
0x2, 0x42, 0x4e, 0xd2, 0x4e,
0xc, 0x92, 0x5c, 0x90, 0xe,
0x6, 0xc8, 0x1c, 0x88, 0x8,
0xe, 0xd2, 0x4e, 0xc2, 0x4c,
0x10, 0x10, 0x1c, 0x92, 0x52,
0x8, 0x0, 0x8, 0x8, 0x8,
0x2, 0x40, 0x2, 0x42, 0x4c,
0x10, 0x14, 0x98, 0x14, 0x92,
0x8, 0x8, 0x8, 0x8, 0x6,
0x0, 0x1b, 0x75, 0xb1, 0x31,
0x0, 0x1c, 0x92, 0x52, 0x52,
0x0, 0xc, 0x92, 0x52, 0x4c,
0x0, 0x1c, 0x92, 0x5c, 0x90,
0x0, 0xe, 0xd2, 0x4e, 0xc2,
0x0, 0xe, 0xd0, 0x10, 0x10,
0x0, 0x6, 0xc8, 0x4, 0x98,
0x8, 0x8, 0xe, 0xc8, 0x7,
0x0, 0x12, 0x52, 0x52, 0x4f,
0x0, 0x11, 0x31, 0x2a, 0x44,
0x0, 0x11, 0x31, 0x35, 0xbb,
0x0, 0x12, 0x4c, 0x8c, 0x92,
0x0, 0x11, 0x2a, 0x44, 0x98,
0x0, 0x1e, 0xc4, 0x88, 0x1e,
0x6, 0xc4, 0x8c, 0x84, 0x86,
0x8, 0x8, 0x8, 0x8, 0x8,
0x18, 0x8, 0xc, 0x88, 0x18,
0x0, 0x0, 0xc, 0x83, 0x60
};
/**
* Default Constructor.
*
* Configures the default font for the display to use.
*/
MicroBitFont::MicroBitFont()
{
this->characters = defaultFont;
this->asciiEnd = MICROBIT_FONT_ASCII_END;
}
/**
* Modifies the current system font to the given instance of MicroBitFont.
*
* @param font the new font that will be used to render characters on the display.
*/
void MicroBitFont::setSystemFont(MicroBitFont font)
{
MicroBitFont::systemFont = font;
}
/**
* Retreives the font object used for rendering characters on the display.
*/
MicroBitFont MicroBitFont::getSystemFont()
{
return MicroBitFont::systemFont;
}
#endif // MICROPY_INCLUDED_NRF_BOARD_MICROBIT_MICROBITFONT_H

345
ports/nrf/boards/microbit/modules/microbitimage.c

@ -25,15 +25,11 @@
*/
#include <string.h>
#include "microbitobj.h"
#include "MicroBitFont.h"
extern "C" {
#include "py/runtime.h"
#include "modmicrobit.h"
#include "microbitimage.h"
#include "microbitconstimage.h"
#include "py/runtime0.h"
#include "microbitfont.h"
#define min(a,b) (((a)<(b))?(a):(b))
#define max(a,b) (((a)>(b))?(a):(b))
@ -50,12 +46,12 @@ STATIC void microbit_image_print(const mp_print_t *print, mp_obj_t self_in, mp_p
if (kind == PRINT_STR)
mp_printf(print, "\n ");
mp_printf(print, "'");
for (int y = 0; y < self->height(); ++y) {
for (int x = 0; x < self->width(); ++x) {
mp_printf(print, "%c", "0123456789"[self->getPixelValue(x, y)]);
for (int y = 0; y < imageHeight(self); ++y) {
for (int x = 0; x < imageWidth(self); ++x) {
mp_printf(print, "%c", "0123456789"[imageGetPixelValue(self, x, y)]);
}
mp_printf(print, ":");
if (kind == PRINT_STR && y < self->height()-1)
if (kind == PRINT_STR && y < imageHeight(self)-1)
mp_printf(print, "'\n '");
}
mp_printf(print, "'");
@ -64,56 +60,56 @@ STATIC void microbit_image_print(const mp_print_t *print, mp_obj_t self_in, mp_p
mp_printf(print, ")");
}
uint8_t monochrome_5by5_t::getPixelValue(mp_int_t x, mp_int_t y) {
uint8_t monochromeGetPixelValue(monochrome_5by5_t * p_mono, mp_int_t x, mp_int_t y) {
unsigned int index = y*5+x;
if (index == 24)
return this->pixel44;
return (this->bits24[index>>3] >> (index&7))&1;
return p_mono->pixel44;
return (p_mono->bits24[index>>3] >> (index&7))&1;
}
uint8_t greyscale_t::getPixelValue(mp_int_t x, mp_int_t y) {
unsigned int index = y*this->width+x;
uint8_t greyscaleGetPixelValue(greyscale_t * p_greyscale, mp_int_t x, mp_int_t y) {
unsigned int index = y*p_greyscale->width+x;
unsigned int shift = ((index<<2)&4);
return (this->byte_data[index>>1] >> shift)&15;
return (p_greyscale->byte_data[index>>1] >> shift)&15;
}
void greyscale_t::setPixelValue(mp_int_t x, mp_int_t y, mp_int_t val) {
unsigned int index = y*this->width+x;
void greyscaleSetPixelValue(greyscale_t * p_greyscale, mp_int_t x, mp_int_t y, mp_int_t val) {
unsigned int index = y*p_greyscale->width+x;
unsigned int shift = ((index<<2)&4);
uint8_t mask = 240 >> shift;
this->byte_data[index>>1] = (this->byte_data[index>>1] & mask) | (val << shift);
p_greyscale->byte_data[index>>1] = (p_greyscale->byte_data[index>>1] & mask) | (val << shift);
}
void greyscale_t::fill(mp_int_t val) {
void greyscaleFill(greyscale_t * p_greyscale, mp_int_t val) {
mp_int_t byte = (val<<4) | val;
for (int i = 0; i < ((this->width*this->height+1)>>1); i++) {
this->byte_data[i] = byte;
for (int i = 0; i < ((p_greyscale->width*p_greyscale->height+1)>>1); i++) {
p_greyscale->byte_data[i] = byte;
}
}
void greyscale_t::clear() {
memset(&this->byte_data, 0, (this->width*this->height+1)>>1);
void greyscaleClear(greyscale_t * p_greyscale) {
memset(&p_greyscale->byte_data, 0, (p_greyscale->width*p_greyscale->height+1)>>1);
}
uint8_t microbit_image_obj_t::getPixelValue(mp_int_t x, mp_int_t y) {
if (this->base.five)
return this->monochrome_5by5.getPixelValue(x, y)*MAX_BRIGHTNESS;
uint8_t imageGetPixelValue(microbit_image_obj_t * p_image, mp_int_t x, mp_int_t y) {
if (p_image->base.five)
return monochromeGetPixelValue(&p_image->monochrome_5by5, x, y)*MAX_BRIGHTNESS;
else
return this->greyscale.getPixelValue(x, y);
return greyscaleGetPixelValue(&p_image->greyscale, x, y);
}
mp_int_t microbit_image_obj_t::width() {
if (this->base.five)
mp_int_t imageWidth(microbit_image_obj_t * p_image) {
if (p_image->base.five)
return 5;
else
return this->greyscale.width;
return p_image->greyscale.width;
}
mp_int_t microbit_image_obj_t::height() {
if (this->base.five)
mp_int_t imageHeight(microbit_image_obj_t * p_image) {
if (p_image->base.five)
return 5;
else
return this->greyscale.height;
return p_image->greyscale.height;
}
STATIC greyscale_t *greyscale_new(mp_int_t w, mp_int_t h) {
@ -125,25 +121,25 @@ STATIC greyscale_t *greyscale_new(mp_int_t w, mp_int_t h) {
return result;
}
greyscale_t *microbit_image_obj_t::copy() {
mp_int_t w = this->width();
mp_int_t h = this->height();
greyscale_t * imageCopy(microbit_image_obj_t * p_image) {
mp_int_t w = imageWidth(p_image);
mp_int_t h = imageHeight(p_image);
greyscale_t *result = greyscale_new(w, h);
for (mp_int_t y = 0; y < h; y++) {
for (mp_int_t x = 0; x < w; ++x) {
result->setPixelValue(x,y, this->getPixelValue(x,y));
greyscaleSetPixelValue(result, x,y, imageGetPixelValue(p_image, x,y));
}
}
return result;
}
greyscale_t *microbit_image_obj_t::invert() {
mp_int_t w = this->width();
mp_int_t h = this->height();
greyscale_t * imageInvert(microbit_image_obj_t * p_image) {
mp_int_t w = imageWidth(p_image);
mp_int_t h = imageHeight(p_image);
greyscale_t *result = greyscale_new(w, h);
for (mp_int_t y = 0; y < h; y++) {
for (mp_int_t x = 0; x < w; ++x) {
result->setPixelValue(x,y, MAX_BRIGHTNESS - this->getPixelValue(x,y));
greyscaleSetPixelValue(result, x,y, MAX_BRIGHTNESS - imageGetPixelValue(p_image, x,y));
}
}
return result;
@ -183,23 +179,23 @@ STATIC microbit_image_obj_t *image_from_parsed_str(const char *s, mp_int_t len)
char c = s[i];
if (c == '\n' || c == ':') {
while (x < w) {
result->setPixelValue(x, y, 0);
greyscaleSetPixelValue(result, x, y, 0);
x++;
}
++y;
x = 0;
} else if (c == ' ') {
/* Treat spaces as 0 */
result->setPixelValue(x, y, 0);
greyscaleSetPixelValue(result, x, y, 0);
++x;
} else if ('c' >= '0' && c <= '9') {
result->setPixelValue(x, y, c - '0');
greyscaleSetPixelValue(result, x, y, c - '0');
++x;
}
}
if (y < h) {
while (x < w) {
result->setPixelValue(x, y, 0);
greyscaleSetPixelValue(result, x, y, 0);
x++;
}
}
@ -214,7 +210,7 @@ STATIC mp_obj_t microbit_image_make_new(const mp_obj_type_t *type_in, mp_uint_t
switch (n_args) {
case 0: {
greyscale_t *image = greyscale_new(5, 5);
image->clear();
greyscaleClear(image);
return image;
}
@ -243,7 +239,7 @@ STATIC mp_obj_t microbit_image_make_new(const mp_obj_type_t *type_in, mp_uint_t
mp_int_t h = mp_obj_get_int(args[1]);
greyscale_t *image = greyscale_new(w, h);
if (n_args == 2) {
image->clear();
greyscaleClear(image);
} else {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
@ -256,7 +252,7 @@ STATIC mp_obj_t microbit_image_make_new(const mp_obj_type_t *type_in, mp_uint_t
for (mp_int_t y = 0; y < h; y++) {
for (mp_int_t x = 0; x < w; ++x) {
uint8_t val = min(((const uint8_t*)bufinfo.buf)[i], MAX_BRIGHTNESS);
image->setPixelValue(x, y, val);
greyscaleSetPixelValue(image, x, y, val);
++i;
}
}
@ -274,7 +270,7 @@ STATIC mp_obj_t microbit_image_make_new(const mp_obj_type_t *type_in, mp_uint_t
static void clear_rect(greyscale_t *img, mp_int_t x0, mp_int_t y0,mp_int_t x1, mp_int_t y1) {
for (int i = x0; i < x1; ++i) {
for (int j = y0; j < y1; ++j) {
img->setPixelValue(i, j, 0);
greyscaleSetPixelValue(img, i, j, 0);
}
}
}
@ -286,8 +282,8 @@ STATIC void image_blit(microbit_image_obj_t *src, greyscale_t *dest, mp_int_t x,
h = 0;
mp_int_t intersect_x0 = max(max(0, x), -xdest);
mp_int_t intersect_y0 = max(max(0, y), -ydest);
mp_int_t intersect_x1 = min(min(dest->width+x-xdest, src->width()), x+w);
mp_int_t intersect_y1 = min(min(dest->height+y-ydest, src->height()), y+h);
mp_int_t intersect_x1 = min(min(dest->width+x-xdest, imageWidth(src)), x+w);
mp_int_t intersect_y1 = min(min(dest->height+y-ydest, imageHeight(src)), y+h);
mp_int_t xstart, xend, ystart, yend, xdel, ydel;
mp_int_t clear_x0 = max(0, xdest);
mp_int_t clear_y0 = max(0, ydest);
@ -310,8 +306,8 @@ STATIC void image_blit(microbit_image_obj_t *src, greyscale_t *dest, mp_int_t x,
}
for (int i = xstart; i != xend; i += xdel) {
for (int j = ystart; j != yend; j += ydel) {
int val = src->getPixelValue(i, j);
dest->setPixelValue(i+xdest-x, j+ydest-y, val);
int val = imageGetPixelValue(src, i, j);
greyscaleSetPixelValue(dest, i+xdest-x, j+ydest-y, val);
}
}
// Adjust intersection rectange to dest
@ -327,8 +323,8 @@ STATIC void image_blit(microbit_image_obj_t *src, greyscale_t *dest, mp_int_t x,
}
greyscale_t *image_shift(microbit_image_obj_t *self, mp_int_t x, mp_int_t y) {
greyscale_t *result = greyscale_new(self->width(), self->width());
image_blit(self, result, x, y, self->width(), self->width(), 0, 0);
greyscale_t *result = greyscale_new(imageWidth(self), imageWidth(self));
image_blit(self, result, x, y, imageWidth(self), imageWidth(self), 0, 0);
return result;
}
@ -344,13 +340,13 @@ STATIC microbit_image_obj_t *image_crop(microbit_image_obj_t *img, mp_int_t x, m
mp_obj_t microbit_image_width(mp_obj_t self_in) {
microbit_image_obj_t *self = (microbit_image_obj_t*)self_in;
return MP_OBJ_NEW_SMALL_INT(self->width());
return MP_OBJ_NEW_SMALL_INT(imageWidth(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_image_width_obj, microbit_image_width);
mp_obj_t microbit_image_height(mp_obj_t self_in) {
microbit_image_obj_t *self = (microbit_image_obj_t*)self_in;
return MP_OBJ_NEW_SMALL_INT(self->height());
return MP_OBJ_NEW_SMALL_INT(imageHeight(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_image_height_obj, microbit_image_height);
@ -362,8 +358,8 @@ mp_obj_t microbit_image_get_pixel(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"index cannot be negative"));
}
if (x < self->width() && y < self->height()) {
return MP_OBJ_NEW_SMALL_INT(self->getPixelValue(x, y));
if (x < imageWidth(self) && y < imageHeight(self)) {
return MP_OBJ_NEW_SMALL_INT(imageGetPixelValue(self, x, y));
}
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "index too large"));
}
@ -390,8 +386,8 @@ mp_obj_t microbit_image_set_pixel(mp_uint_t n_args, const mp_obj_t *args) {
mp_int_t bright = mp_obj_get_int(args[3]);
if (bright < 0 || bright > MAX_BRIGHTNESS)
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "brightness out of bounds."));
if (x < self->width() && y < self->height()) {
self->greyscale.setPixelValue(x, y, bright);
if (x < imageWidth(self) && y < imageHeight(self)) {
greyscaleSetPixelValue(&(self->greyscale), x, y, bright);
return mp_const_none;
}
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "index too large"));
@ -405,7 +401,7 @@ mp_obj_t microbit_image_fill(mp_obj_t self_in, mp_obj_t n_in) {
if (n < 0 || n > MAX_BRIGHTNESS) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "brightness out of bounds."));
}
self->greyscale.fill(n);
greyscaleFill(&self->greyscale, n);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(microbit_image_fill_obj, microbit_image_fill);
@ -485,102 +481,102 @@ MP_DEFINE_CONST_FUN_OBJ_2(microbit_image_shift_down_obj, microbit_image_shift_do
mp_obj_t microbit_image_copy(mp_obj_t self_in) {
microbit_image_obj_t *self = (microbit_image_obj_t*)self_in;
return self->copy();
return imageCopy(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_image_copy_obj, microbit_image_copy);
mp_obj_t microbit_image_invert(mp_obj_t self_in) {
microbit_image_obj_t *self = (microbit_image_obj_t*)self_in;
return self->invert();
return imageInvert(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_image_invert_obj, microbit_image_invert);
STATIC const mp_map_elem_t microbit_image_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_width), (mp_obj_t)&microbit_image_width_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_height), (mp_obj_t)&microbit_image_height_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_pixel), (mp_obj_t)&microbit_image_get_pixel_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_pixel), (mp_obj_t)&microbit_image_set_pixel_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_shift_left), (mp_obj_t)&microbit_image_shift_left_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_shift_right), (mp_obj_t)&microbit_image_shift_right_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_shift_up), (mp_obj_t)&microbit_image_shift_up_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_shift_down), (mp_obj_t)&microbit_image_shift_down_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&microbit_image_copy_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_crop), (mp_obj_t)&microbit_image_crop_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_invert), (mp_obj_t)&microbit_image_invert_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_fill), (mp_obj_t)&microbit_image_fill_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_blit), (mp_obj_t)&microbit_image_blit_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_HEART), (mp_obj_t)&microbit_const_image_heart_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_HEART_SMALL), (mp_obj_t)&microbit_const_image_heart_small_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_HAPPY), (mp_obj_t)&microbit_const_image_happy_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_SMILE), (mp_obj_t)&microbit_const_image_smile_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_SAD), (mp_obj_t)&microbit_const_image_sad_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_CONFUSED), (mp_obj_t)&microbit_const_image_confused_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ANGRY), (mp_obj_t)&microbit_const_image_angry_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ASLEEP), (mp_obj_t)&microbit_const_image_asleep_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_SURPRISED), (mp_obj_t)&microbit_const_image_surprised_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_SILLY), (mp_obj_t)&microbit_const_image_silly_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_FABULOUS), (mp_obj_t)&microbit_const_image_fabulous_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_MEH), (mp_obj_t)&microbit_const_image_meh_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_YES), (mp_obj_t)&microbit_const_image_yes_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_NO), (mp_obj_t)&microbit_const_image_no_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_CLOCK12), (mp_obj_t)&microbit_const_image_clock12_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_CLOCK1), (mp_obj_t)&microbit_const_image_clock1_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_CLOCK2), (mp_obj_t)&microbit_const_image_clock2_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_CLOCK3), (mp_obj_t)&microbit_const_image_clock3_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_CLOCK4), (mp_obj_t)&microbit_const_image_clock4_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_CLOCK5), (mp_obj_t)&microbit_const_image_clock5_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_CLOCK6), (mp_obj_t)&microbit_const_image_clock6_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_CLOCK7), (mp_obj_t)&microbit_const_image_clock7_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_CLOCK8), (mp_obj_t)&microbit_const_image_clock8_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_CLOCK9), (mp_obj_t)&microbit_const_image_clock9_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_CLOCK10), (mp_obj_t)&microbit_const_image_clock10_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_CLOCK11), (mp_obj_t)&microbit_const_image_clock11_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ARROW_N), (mp_obj_t)&microbit_const_image_arrow_n_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ARROW_NE), (mp_obj_t)&microbit_const_image_arrow_ne_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ARROW_E), (mp_obj_t)&microbit_const_image_arrow_e_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ARROW_SE), (mp_obj_t)&microbit_const_image_arrow_se_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ARROW_S), (mp_obj_t)&microbit_const_image_arrow_s_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ARROW_SW), (mp_obj_t)&microbit_const_image_arrow_sw_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ARROW_W), (mp_obj_t)&microbit_const_image_arrow_w_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ARROW_NW), (mp_obj_t)&microbit_const_image_arrow_nw_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_TRIANGLE), (mp_obj_t)&microbit_const_image_triangle_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_TRIANGLE_LEFT), (mp_obj_t)&microbit_const_image_triangle_left_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_CHESSBOARD), (mp_obj_t)&microbit_const_image_chessboard_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_DIAMOND), (mp_obj_t)&microbit_const_image_diamond_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_DIAMOND_SMALL), (mp_obj_t)&microbit_const_image_diamond_small_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_SQUARE), (mp_obj_t)&microbit_const_image_square_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_SQUARE_SMALL), (mp_obj_t)&microbit_const_image_square_small_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_RABBIT), (mp_obj_t)&microbit_const_image_rabbit },
{ MP_OBJ_NEW_QSTR(MP_QSTR_COW), (mp_obj_t)&microbit_const_image_cow },
{ MP_OBJ_NEW_QSTR(MP_QSTR_MUSIC_CROTCHET), (mp_obj_t)&microbit_const_image_music_crotchet_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_MUSIC_QUAVER), (mp_obj_t)&microbit_const_image_music_quaver_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_MUSIC_QUAVERS), (mp_obj_t)&microbit_const_image_music_quavers_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PITCHFORK), (mp_obj_t)&microbit_const_image_pitchfork_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_XMAS), (mp_obj_t)&microbit_const_image_xmas_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PACMAN), (mp_obj_t)&microbit_const_image_pacman_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_TARGET), (mp_obj_t)&microbit_const_image_target_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ALL_CLOCKS), (mp_obj_t)&microbit_const_image_all_clocks_tuple_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ALL_ARROWS), (mp_obj_t)&microbit_const_image_all_arrows_tuple_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_TSHIRT), (mp_obj_t)&microbit_const_image_tshirt_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ROLLERSKATE), (mp_obj_t)&microbit_const_image_rollerskate_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_DUCK), (mp_obj_t)&microbit_const_image_duck_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_HOUSE), (mp_obj_t)&microbit_const_image_house_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_TORTOISE), (mp_obj_t)&microbit_const_image_tortoise_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_BUTTERFLY), (mp_obj_t)&microbit_const_image_butterfly_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_STICKFIGURE), (mp_obj_t)&microbit_const_image_stickfigure_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_GHOST), (mp_obj_t)&microbit_const_image_ghost_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_SWORD), (mp_obj_t)&microbit_const_image_sword_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_GIRAFFE), (mp_obj_t)&microbit_const_image_giraffe_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_SKULL), (mp_obj_t)&microbit_const_image_skull_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_UMBRELLA), (mp_obj_t)&microbit_const_image_umbrella_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_SNAKE), (mp_obj_t)&microbit_const_image_snake_obj },
STATIC const mp_rom_map_elem_t microbit_image_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&microbit_image_width_obj) },
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&microbit_image_height_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_pixel), MP_ROM_PTR(&microbit_image_get_pixel_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_pixel), MP_ROM_PTR(&microbit_image_set_pixel_obj) },
{ MP_ROM_QSTR(MP_QSTR_shift_left), MP_ROM_PTR(&microbit_image_shift_left_obj) },
{ MP_ROM_QSTR(MP_QSTR_shift_right), MP_ROM_PTR(&microbit_image_shift_right_obj) },
{ MP_ROM_QSTR(MP_QSTR_shift_up), MP_ROM_PTR(&microbit_image_shift_up_obj) },
{ MP_ROM_QSTR(MP_QSTR_shift_down), MP_ROM_PTR(&microbit_image_shift_down_obj) },
{ MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&microbit_image_copy_obj) },
{ MP_ROM_QSTR(MP_QSTR_crop), MP_ROM_PTR(&microbit_image_crop_obj) },
{ MP_ROM_QSTR(MP_QSTR_invert), MP_ROM_PTR(&microbit_image_invert_obj) },
{ MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&microbit_image_fill_obj) },
{ MP_ROM_QSTR(MP_QSTR_blit), MP_ROM_PTR(&microbit_image_blit_obj) },
{ MP_ROM_QSTR(MP_QSTR_HEART), MP_ROM_PTR(&microbit_const_image_heart_obj) },
{ MP_ROM_QSTR(MP_QSTR_HEART_SMALL), MP_ROM_PTR(&microbit_const_image_heart_small_obj) },
{ MP_ROM_QSTR(MP_QSTR_HAPPY), MP_ROM_PTR(&microbit_const_image_happy_obj) },
{ MP_ROM_QSTR(MP_QSTR_SMILE), MP_ROM_PTR(&microbit_const_image_smile_obj) },
{ MP_ROM_QSTR(MP_QSTR_SAD), MP_ROM_PTR(&microbit_const_image_sad_obj) },
{ MP_ROM_QSTR(MP_QSTR_CONFUSED), MP_ROM_PTR(&microbit_const_image_confused_obj) },
{ MP_ROM_QSTR(MP_QSTR_ANGRY), MP_ROM_PTR(&microbit_const_image_angry_obj) },
{ MP_ROM_QSTR(MP_QSTR_ASLEEP), MP_ROM_PTR(&microbit_const_image_asleep_obj) },
{ MP_ROM_QSTR(MP_QSTR_SURPRISED), MP_ROM_PTR(&microbit_const_image_surprised_obj) },
{ MP_ROM_QSTR(MP_QSTR_SILLY), MP_ROM_PTR(&microbit_const_image_silly_obj) },
{ MP_ROM_QSTR(MP_QSTR_FABULOUS), MP_ROM_PTR(&microbit_const_image_fabulous_obj) },
{ MP_ROM_QSTR(MP_QSTR_MEH), MP_ROM_PTR(&microbit_const_image_meh_obj) },
{ MP_ROM_QSTR(MP_QSTR_YES), MP_ROM_PTR(&microbit_const_image_yes_obj) },
{ MP_ROM_QSTR(MP_QSTR_NO), MP_ROM_PTR(&microbit_const_image_no_obj) },
{ MP_ROM_QSTR(MP_QSTR_CLOCK12), MP_ROM_PTR(&microbit_const_image_clock12_obj) },
{ MP_ROM_QSTR(MP_QSTR_CLOCK1), MP_ROM_PTR(&microbit_const_image_clock1_obj) },
{ MP_ROM_QSTR(MP_QSTR_CLOCK2), MP_ROM_PTR(&microbit_const_image_clock2_obj) },
{ MP_ROM_QSTR(MP_QSTR_CLOCK3), MP_ROM_PTR(&microbit_const_image_clock3_obj) },
{ MP_ROM_QSTR(MP_QSTR_CLOCK4), MP_ROM_PTR(&microbit_const_image_clock4_obj) },
{ MP_ROM_QSTR(MP_QSTR_CLOCK5), MP_ROM_PTR(&microbit_const_image_clock5_obj) },
{ MP_ROM_QSTR(MP_QSTR_CLOCK6), MP_ROM_PTR(&microbit_const_image_clock6_obj) },
{ MP_ROM_QSTR(MP_QSTR_CLOCK7), MP_ROM_PTR(&microbit_const_image_clock7_obj) },
{ MP_ROM_QSTR(MP_QSTR_CLOCK8), MP_ROM_PTR(&microbit_const_image_clock8_obj) },
{ MP_ROM_QSTR(MP_QSTR_CLOCK9), MP_ROM_PTR(&microbit_const_image_clock9_obj) },
{ MP_ROM_QSTR(MP_QSTR_CLOCK10), MP_ROM_PTR(&microbit_const_image_clock10_obj) },
{ MP_ROM_QSTR(MP_QSTR_CLOCK11), MP_ROM_PTR(&microbit_const_image_clock11_obj) },
{ MP_ROM_QSTR(MP_QSTR_ARROW_N), MP_ROM_PTR(&microbit_const_image_arrow_n_obj) },
{ MP_ROM_QSTR(MP_QSTR_ARROW_NE), MP_ROM_PTR(&microbit_const_image_arrow_ne_obj) },
{ MP_ROM_QSTR(MP_QSTR_ARROW_E), MP_ROM_PTR(&microbit_const_image_arrow_e_obj) },
{ MP_ROM_QSTR(MP_QSTR_ARROW_SE), MP_ROM_PTR(&microbit_const_image_arrow_se_obj) },
{ MP_ROM_QSTR(MP_QSTR_ARROW_S), MP_ROM_PTR(&microbit_const_image_arrow_s_obj) },
{ MP_ROM_QSTR(MP_QSTR_ARROW_SW), MP_ROM_PTR(&microbit_const_image_arrow_sw_obj) },
{ MP_ROM_QSTR(MP_QSTR_ARROW_W), MP_ROM_PTR(&microbit_const_image_arrow_w_obj) },
{ MP_ROM_QSTR(MP_QSTR_ARROW_NW), MP_ROM_PTR(&microbit_const_image_arrow_nw_obj) },
{ MP_ROM_QSTR(MP_QSTR_TRIANGLE), MP_ROM_PTR(&microbit_const_image_triangle_obj) },
{ MP_ROM_QSTR(MP_QSTR_TRIANGLE_LEFT), MP_ROM_PTR(&microbit_const_image_triangle_left_obj) },
{ MP_ROM_QSTR(MP_QSTR_CHESSBOARD), MP_ROM_PTR(&microbit_const_image_chessboard_obj) },
{ MP_ROM_QSTR(MP_QSTR_DIAMOND), MP_ROM_PTR(&microbit_const_image_diamond_obj) },
{ MP_ROM_QSTR(MP_QSTR_DIAMOND_SMALL), MP_ROM_PTR(&microbit_const_image_diamond_small_obj) },
{ MP_ROM_QSTR(MP_QSTR_SQUARE), MP_ROM_PTR(&microbit_const_image_square_obj) },
{ MP_ROM_QSTR(MP_QSTR_SQUARE_SMALL), MP_ROM_PTR(&microbit_const_image_square_small_obj) },
{ MP_ROM_QSTR(MP_QSTR_RABBIT), MP_ROM_PTR(&microbit_const_image_rabbit) },
{ MP_ROM_QSTR(MP_QSTR_COW), MP_ROM_PTR(&microbit_const_image_cow) },
{ MP_ROM_QSTR(MP_QSTR_MUSIC_CROTCHET), MP_ROM_PTR(&microbit_const_image_music_crotchet_obj) },
{ MP_ROM_QSTR(MP_QSTR_MUSIC_QUAVER), MP_ROM_PTR(&microbit_const_image_music_quaver_obj) },
{ MP_ROM_QSTR(MP_QSTR_MUSIC_QUAVERS), MP_ROM_PTR(&microbit_const_image_music_quavers_obj) },
{ MP_ROM_QSTR(MP_QSTR_PITCHFORK), MP_ROM_PTR(&microbit_const_image_pitchfork_obj) },
{ MP_ROM_QSTR(MP_QSTR_XMAS), MP_ROM_PTR(&microbit_const_image_xmas_obj) },
{ MP_ROM_QSTR(MP_QSTR_PACMAN), MP_ROM_PTR(&microbit_const_image_pacman_obj) },
{ MP_ROM_QSTR(MP_QSTR_TARGET), MP_ROM_PTR(&microbit_const_image_target_obj) },
{ MP_ROM_QSTR(MP_QSTR_ALL_CLOCKS), MP_ROM_PTR(&microbit_const_image_all_clocks_tuple_obj) },
{ MP_ROM_QSTR(MP_QSTR_ALL_ARROWS), MP_ROM_PTR(&microbit_const_image_all_arrows_tuple_obj) },
{ MP_ROM_QSTR(MP_QSTR_TSHIRT), MP_ROM_PTR(&microbit_const_image_tshirt_obj) },
{ MP_ROM_QSTR(MP_QSTR_ROLLERSKATE), MP_ROM_PTR(&microbit_const_image_rollerskate_obj) },
{ MP_ROM_QSTR(MP_QSTR_DUCK), MP_ROM_PTR(&microbit_const_image_duck_obj) },
{ MP_ROM_QSTR(MP_QSTR_HOUSE), MP_ROM_PTR(&microbit_const_image_house_obj) },
{ MP_ROM_QSTR(MP_QSTR_TORTOISE), MP_ROM_PTR(&microbit_const_image_tortoise_obj) },
{ MP_ROM_QSTR(MP_QSTR_BUTTERFLY), MP_ROM_PTR(&microbit_const_image_butterfly_obj) },
{ MP_ROM_QSTR(MP_QSTR_STICKFIGURE), MP_ROM_PTR(&microbit_const_image_stickfigure_obj) },
{ MP_ROM_QSTR(MP_QSTR_GHOST), MP_ROM_PTR(&microbit_const_image_ghost_obj) },
{ MP_ROM_QSTR(MP_QSTR_SWORD), MP_ROM_PTR(&microbit_const_image_sword_obj) },
{ MP_ROM_QSTR(MP_QSTR_GIRAFFE), MP_ROM_PTR(&microbit_const_image_giraffe_obj) },
{ MP_ROM_QSTR(MP_QSTR_SKULL), MP_ROM_PTR(&microbit_const_image_skull_obj) },
{ MP_ROM_QSTR(MP_QSTR_UMBRELLA), MP_ROM_PTR(&microbit_const_image_umbrella_obj) },
{ MP_ROM_QSTR(MP_QSTR_SNAKE), MP_ROM_PTR(&microbit_const_image_snake_obj) },
};
STATIC MP_DEFINE_CONST_DICT(microbit_image_locals_dict, microbit_image_locals_dict_table);
#define THE_FONT MicroBitFont::defaultFont
#define THE_FONT font_pendolino3_5x5_pad3msb
#define ASCII_START 32
#define ASCII_END 126
@ -602,7 +598,7 @@ void microbit_image_set_from_char(greyscale_t *img, char c) {
const unsigned char *data = get_font_data_from_char(c);
for (int x = 0; x < 5; ++x) {
for (int y = 0; y < 5; ++y) {
img->setPixelValue(x, y, get_pixel_from_font_data(data, x, y)*MAX_BRIGHTNESS);
greyscaleSetPixelValue(img, x, y, get_pixel_from_font_data(data, x, y)*MAX_BRIGHTNESS);
}
}
}
@ -617,33 +613,34 @@ microbit_image_obj_t *microbit_image_for_char(char c) {
microbit_image_obj_t *microbit_image_dim(microbit_image_obj_t *lhs, mp_float_t fval) {
if (fval < 0)
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Brightness multiplier must not be negative."));
greyscale_t *result = greyscale_new(lhs->width(), lhs->height());
for (int x = 0; x < lhs->width(); ++x) {
for (int y = 0; y < lhs->width(); ++y) {
int val = min((int)lhs->getPixelValue(x,y)*fval+0.5, MAX_BRIGHTNESS);
result->setPixelValue(x, y, val);
greyscale_t *result = greyscale_new(imageWidth(lhs), imageHeight(lhs));
for (int x = 0; x < imageWidth(lhs); ++x) {
for (int y = 0; y < imageWidth(lhs); ++y) {
int val = min((int)imageGetPixelValue(lhs, x,y)*fval+0.5, MAX_BRIGHTNESS);
greyscaleSetPixelValue(result, x, y, val);
}
}
return (microbit_image_obj_t *)result;
}
microbit_image_obj_t *microbit_image_sum(microbit_image_obj_t *lhs, microbit_image_obj_t *rhs, bool add) {
mp_int_t h = lhs->height();
mp_int_t w = lhs->width();
if (rhs->height() != h || lhs->width() != w) {
mp_int_t h = imageHeight(lhs);
mp_int_t w = imageWidth(lhs);
if (imageHeight(rhs) != h || imageWidth(lhs) != w) {
// TODO: verify that image width in test above should really test (lhs != w)
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Images must be the same size."));
}
greyscale_t *result = greyscale_new(w, h);
for (int x = 0; x < w; ++x) {
for (int y = 0; y < h; ++y) {
int val;
int lval = lhs->getPixelValue(x,y);
int rval = rhs->getPixelValue(x,y);
int lval = imageGetPixelValue(lhs, x,y);
int rval = imageGetPixelValue(rhs, x,y);
if (add)
val = min(lval + rval, MAX_BRIGHTNESS);
else
val = max(0, lval - rval);
result->setPixelValue(x, y, val);
greyscaleSetPixelValue(result, x, y, val);
}
}
return (microbit_image_obj_t *)result;
@ -685,8 +682,6 @@ const mp_obj_type_t microbit_image_type = {
.getiter = NULL,
.iternext = NULL,
.buffer_p = {NULL},
.stream_p = NULL,
.bases_tuple = NULL,
.locals_dict = (mp_obj_dict_t*)&microbit_image_locals_dict,
};
@ -763,7 +758,8 @@ static void restart(scrolling_string_iterator_t *iter) {
}
}
STATIC mp_obj_t get_microbit_scrolling_string_iter(mp_obj_t o_in) {
STATIC mp_obj_t get_microbit_scrolling_string_iter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
(void)iter_buf;
scrolling_string_t *str = (scrolling_string_t *)o_in;
scrolling_string_iterator_t *result = m_new_obj(scrolling_string_iterator_t);
result->base.type = &microbit_scrolling_string_iterator_type;
@ -782,25 +778,25 @@ STATIC mp_obj_t microbit_scrolling_string_iter_next(mp_obj_t o_in) {
if (iter->next_char == iter->end && iter->offset == 5) {
if (iter->repeat) {
restart(iter);
iter->img->clear();
greyscaleClear(iter->img);
} else {
return MP_OBJ_STOP_ITERATION;
}
}
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 5; y++) {
iter->img->setPixelValue(x, y, iter->img->getPixelValue(x+1, y));
greyscaleSetPixelValue(iter->img, x, y, greyscaleGetPixelValue(iter->img, x+1, y));
}
}
for (int y = 0; y < 5; y++) {
iter->img->setPixelValue(4, y, 0);
greyscaleSetPixelValue(iter->img, 4, y, 0);
}
const unsigned char *font_data;
if (iter->offset < iter->offset_limit) {
font_data = get_font_data_from_char(iter->right);
for (int y = 0; y < 5; ++y) {
int pix = get_pixel_from_font_data(font_data, iter->offset, y)*MAX_BRIGHTNESS;
iter->img->setPixelValue(4, y, pix);
greyscaleSetPixelValue(iter->img, 4, y, pix);
}
} else if (iter->offset == iter->offset_limit) {
++iter->next_char;
@ -837,8 +833,6 @@ const mp_obj_type_t microbit_scrolling_string_type = {
.getiter = get_microbit_scrolling_string_iter,
.iternext = NULL,
.buffer_p = {NULL},
.stream_p = NULL,
.bases_tuple = NULL,
.locals_dict = NULL,
};
@ -852,11 +846,9 @@ const mp_obj_type_t microbit_scrolling_string_iterator_type = {
.binary_op = NULL,
.attr = NULL,
.subscr = NULL,
.getiter = mp_identity,
.getiter = mp_identity_getiter,
.iternext = microbit_scrolling_string_iter_next,
.buffer_p = {NULL},
.stream_p = NULL,
.bases_tuple = NULL,
.locals_dict = NULL,
};
@ -894,7 +886,7 @@ static mp_obj_t facade_unary_op(mp_uint_t op, mp_obj_t self_in) {
}
}
static mp_obj_t microbit_facade_iterator(mp_obj_t iterable);
static mp_obj_t microbit_facade_iterator(mp_obj_t iterable_in, mp_obj_iter_buf_t *iter_buf);
const mp_obj_type_t string_image_facade_type = {
{ &mp_type_type },
@ -909,8 +901,6 @@ const mp_obj_type_t string_image_facade_type = {
.getiter = microbit_facade_iterator,
.iternext = NULL,
.buffer_p = {NULL},
.stream_p = NULL,
.bases_tuple = NULL,
NULL
};
@ -952,15 +942,14 @@ const mp_obj_type_t microbit_facade_iterator_type = {
.binary_op = NULL,
.attr = NULL,
.subscr = NULL,
.getiter = mp_identity,
.getiter = mp_identity_getiter,
.iternext = microbit_facade_iter_next,
.buffer_p = {NULL},
.stream_p = NULL,
.bases_tuple = NULL,
NULL
};
mp_obj_t microbit_facade_iterator(mp_obj_t iterable_in) {
mp_obj_t microbit_facade_iterator(mp_obj_t iterable_in, mp_obj_iter_buf_t *iter_buf) {
(void)iter_buf;
facade_iterator_t *result = m_new_obj(facade_iterator_t);
string_image_facade_t *iterable = (string_image_facade_t *)iterable_in;
result->base.type = &microbit_facade_iterator_type;
@ -969,5 +958,3 @@ mp_obj_t microbit_facade_iterator(mp_obj_t iterable_in) {
result->index = 0;
return result;
}
}

41
ports/nrf/boards/microbit/modules/microbitimage.h

@ -25,40 +25,42 @@ typedef struct _monochrome_5by5_t {
TYPE_AND_FLAGS;
uint8_t pixel44: 1;
uint8_t bits24[3];
/* This is an internal method it is up to the caller to validate the inputs */
uint8_t getPixelValue(mp_int_t x, mp_int_t y);
} monochrome_5by5_t;
/* This is an internal method it is up to the caller to validate the inputs */
uint8_t monochromeGetPixelValue(monochrome_5by5_t * p_mono, mp_int_t x, mp_int_t y);
typedef struct _greyscale_t {
TYPE_AND_FLAGS;
uint8_t height;
uint8_t width;
uint8_t byte_data[]; /* Static initializer for this will have to be C, not C++ */
void clear();
/* Thiese are internal methods and it is up to the caller to validate the inputs */
uint8_t getPixelValue(mp_int_t x, mp_int_t y);
void setPixelValue(mp_int_t x, mp_int_t y, mp_int_t val);
void fill(mp_int_t val);
} greyscale_t;
#if 1
void clear(greyscale_t * p_greyscale);
/* Thiese are internal methods and it is up to the caller to validate the inputs */
uint8_t greyscaleGetPixelValue(greyscale_t * p_greyscale, mp_int_t x, mp_int_t y);
void greyscaleSetPixelValue(greyscale_t * p_greyscale, mp_int_t x, mp_int_t y, mp_int_t val);
void greyscaleFill(greyscale_t * p_greyscale, mp_int_t val);
#endif
typedef union _microbit_image_obj_t {
image_base_t base;
monochrome_5by5_t monochrome_5by5;
greyscale_t greyscale;
mp_int_t height();
mp_int_t width();
greyscale_t *copy();
greyscale_t *invert();
/* This is an internal method it is up to the caller to validate the inputs */
uint8_t getPixelValue(mp_int_t x, mp_int_t y);
} microbit_image_obj_t;
#if 1
mp_int_t imageHeight(microbit_image_obj_t * p_image_obj);
mp_int_t imageWidth(microbit_image_obj_t * p_image_obj);
greyscale_t * imageCopy(microbit_image_obj_t * p_image_obj);
greyscale_t * imageInvert(microbit_image_obj_t * p_image_obj);
/* This is an internal method it is up to the caller to validate the inputs */
uint8_t imageGetPixelValue(microbit_image_obj_t * p_image_obj, mp_int_t x, mp_int_t y);
#endif
/** Return a facade object that presents the string as a sequence of images */
mp_obj_t microbit_string_facade(mp_obj_t string);
@ -81,6 +83,7 @@ mp_obj_t scrolling_string_image_iterable(const char* str, mp_uint_t len, mp_obj_
extern const monochrome_5by5_t microbit_blank_image;
extern const monochrome_5by5_t microbit_const_image_heart_obj;
extern const mp_obj_type_t microbit_image_type;
#define BLANK_IMAGE (microbit_image_obj_t *)(&microbit_blank_image)
#define HEART_IMAGE (microbit_image_obj_t *)(&microbit_const_image_heart_obj)

31
ports/nrf/boards/microbit/modules/modmicrobit.c

@ -24,17 +24,13 @@
* THE SOFTWARE.
*/
#include "mbed.h"
extern "C" {
#include "py/nlr.h"
#include "py/obj.h"
#include "py/mphal.h"
#include "modmicrobit.h"
#include "microbitdisplay.h"
#include "microbitimage.h"
#include "softpwm.h"
#include "ticker.h"
extern uint32_t ticks;
STATIC mp_obj_t microbit_reset_(void) {
@ -106,13 +102,22 @@ STATIC mp_obj_t microbit_temperature(void) {
}
MP_DEFINE_CONST_FUN_OBJ_0(microbit_temperature_obj, microbit_temperature);
STATIC const mp_map_elem_t microbit_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_microbit) },
static mp_obj_t microbit_module_init(void) {
softpwm_init();
ticker_init(microbit_display_tick);
ticker_start();
pwm_start();
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(microbit_module___init___obj, microbit_module_init);
STATIC const mp_rom_map_elem_t microbit_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&microbit_module___init___obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_Image), (mp_obj_t)&microbit_image_type },
{ MP_ROM_QSTR(MP_QSTR_Image), MP_ROM_PTR(&microbit_image_type) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_display), (mp_obj_t)&microbit_display_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_button_a), (mp_obj_t)&microbit_button_a_obj },
{ MP_ROM_QSTR(MP_QSTR_display), MP_ROM_PTR(&microbit_display_obj) },
/* { MP_OBJ_NEW_QSTR(MP_QSTR_button_a), (mp_obj_t)&microbit_button_a_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_button_b), (mp_obj_t)&microbit_button_b_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_accelerometer), (mp_obj_t)&microbit_accelerometer_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_compass), (mp_obj_t)&microbit_compass_obj },
@ -145,14 +150,12 @@ STATIC const mp_map_elem_t microbit_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_pin16), (mp_obj_t)&microbit_p16_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_pin19), (mp_obj_t)&microbit_p19_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_pin20), (mp_obj_t)&microbit_p20_obj },
*/
};
STATIC MP_DEFINE_CONST_DICT(microbit_module_globals, microbit_module_globals_table);
const mp_obj_module_t microbit_module = {
.base = { &mp_type_module },
.name = MP_QSTR_microbit,
.globals = (mp_obj_dict_t*)&microbit_module_globals,
};
}

Loading…
Cancel
Save