Skip to content
Snippets Groups Projects
Commit fbc45bd3 authored by Glenn Ruben Bakke's avatar Glenn Ruben Bakke Committed by Damien George
Browse files

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

This aligns implementation with new style structures.
parent f3386cfc
No related branches found
No related tags found
No related merge requests found
......@@ -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
};
......
......@@ -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
);
}
......@@ -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__
......@@ -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},
......
......@@ -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);
}
}
......@@ -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__
......@@ -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,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x8c, 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,
0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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
This diff is collapsed.
......@@ -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)
......
......@@ -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,
};
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment