Browse Source

nrf/boards/microbit: Attempt to get working display/images without FP.

And update the API to align with new unary/binary function callback
structures.
pull/3137/merge
Glenn Ruben Bakke 7 years ago
committed by Damien George
parent
commit
1b241be310
  1. 22
      ports/nrf/boards/microbit/modules/microbitimage.c
  2. 4
      ports/nrf/boards/microbit/modules/microbitimage.h
  3. 6
      ports/nrf/boards/microbit/modules/modmicrobit.c

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

@ -610,13 +610,21 @@ microbit_image_obj_t *microbit_image_for_char(char c) {
return (microbit_image_obj_t *)result;
}
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
microbit_image_obj_t *microbit_image_dim(microbit_image_obj_t *lhs, mp_float_t fval) {
#else // MICROPY_FLOAT_IMPL_NONE
microbit_image_obj_t *microbit_image_dim(microbit_image_obj_t *lhs, mp_int_t fval) {
#endif
if (fval < 0)
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Brightness multiplier must not be negative."));
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) {
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
int val = min((int)imageGetPixelValue(lhs, x,y)*fval+0.5, MAX_BRIGHTNESS);
#else // MICROPY_FLOAT_IMPL_NONE
int val = min((int)imageGetPixelValue(lhs, x,y)*fval, MAX_BRIGHTNESS);
#endif
greyscaleSetPixelValue(result, x, y, val);
}
}
@ -646,7 +654,7 @@ microbit_image_obj_t *microbit_image_sum(microbit_image_obj_t *lhs, microbit_ima
return (microbit_image_obj_t *)result;
}
STATIC mp_obj_t image_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
STATIC mp_obj_t image_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
if (mp_obj_get_type(lhs_in) != &microbit_image_type) {
return MP_OBJ_NULL; // op not supported
}
@ -656,9 +664,19 @@ STATIC mp_obj_t image_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in)
case MP_BINARY_OP_SUBTRACT:
break;
case MP_BINARY_OP_MULTIPLY:
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
return microbit_image_dim(lhs, mp_obj_get_float(rhs_in));
#else
return microbit_image_dim(lhs, mp_obj_get_int(rhs_in) * 10);
#endif
case MP_BINARY_OP_TRUE_DIVIDE:
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
return microbit_image_dim(lhs, 1.0/mp_obj_get_float(rhs_in));
#else
break;
case MP_BINARY_OP_FLOOR_DIVIDE:
return microbit_image_dim(lhs, (100/mp_obj_get_int(rhs_in) + 5) / 10);
#endif
default:
return MP_OBJ_NULL; // op not supported
}
@ -877,7 +895,7 @@ static mp_obj_t string_image_facade_subscr(mp_obj_t self_in, mp_obj_t index_in,
}
}
static mp_obj_t facade_unary_op(mp_uint_t op, mp_obj_t self_in) {
static mp_obj_t facade_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
string_image_facade_t *self = (string_image_facade_t *)self_in;
switch (op) {
case MP_UNARY_OP_LEN:

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

@ -89,7 +89,11 @@ extern const mp_obj_type_t microbit_image_type;
#define HEART_IMAGE (microbit_image_obj_t *)(&microbit_const_image_heart_obj)
#define HAPPY_IMAGE (microbit_image_obj_t *)(&microbit_const_image_happy_obj)
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
microbit_image_obj_t *microbit_image_dim(microbit_image_obj_t *lhs, mp_float_t fval);
#else
microbit_image_obj_t *microbit_image_dim(microbit_image_obj_t *lhs, mp_int_t val);
#endif
microbit_image_obj_t *microbit_image_sum(microbit_image_obj_t *lhs, microbit_image_obj_t *rhs, bool add);
#endif // __MICROPY_INCLUDED_MICROBIT_IMAGE_H__

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

@ -43,8 +43,10 @@ STATIC mp_obj_t microbit_sleep(mp_obj_t ms_in) {
mp_int_t ms;
if (mp_obj_is_integer(ms_in)) {
ms = mp_obj_get_int(ms_in);
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
} else {
ms = (mp_int_t)mp_obj_get_float(ms_in);
#endif
}
if (ms > 0) {
mp_hal_delay_ms(ms);
@ -98,7 +100,11 @@ STATIC mp_obj_t microbit_temperature(void) {
NRF_TEMP->EVENTS_DATARDY = 0;
temp = NRF_TEMP->TEMP;
NRF_TEMP->TASKS_STOP = 1;
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
return mp_obj_new_float(temp/4.0);
#else
return mp_obj_new_int(temp/4);
#endif
}
MP_DEFINE_CONST_FUN_OBJ_0(microbit_temperature_obj, microbit_temperature);

Loading…
Cancel
Save