nrf/boards/microbit: Attempt to get working display/images without FP.
And update the API to align with new unary/binary function callback structures.
This commit is contained in:
parent
9e090a8783
commit
1b241be310
@ -610,13 +610,21 @@ microbit_image_obj_t *microbit_image_for_char(char c) {
|
|||||||
return (microbit_image_obj_t *)result;
|
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) {
|
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)
|
if (fval < 0)
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Brightness multiplier must not be negative."));
|
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));
|
greyscale_t *result = greyscale_new(imageWidth(lhs), imageHeight(lhs));
|
||||||
for (int x = 0; x < imageWidth(lhs); ++x) {
|
for (int x = 0; x < imageWidth(lhs); ++x) {
|
||||||
for (int y = 0; y < imageWidth(lhs); ++y) {
|
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);
|
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);
|
greyscaleSetPixelValue(result, x, y, val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -645,8 +653,8 @@ microbit_image_obj_t *microbit_image_sum(microbit_image_obj_t *lhs, microbit_ima
|
|||||||
}
|
}
|
||||||
return (microbit_image_obj_t *)result;
|
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) != µbit_image_type) {
|
if (mp_obj_get_type(lhs_in) != µbit_image_type) {
|
||||||
return MP_OBJ_NULL; // op not supported
|
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:
|
case MP_BINARY_OP_SUBTRACT:
|
||||||
break;
|
break;
|
||||||
case MP_BINARY_OP_MULTIPLY:
|
case MP_BINARY_OP_MULTIPLY:
|
||||||
|
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
|
||||||
return microbit_image_dim(lhs, mp_obj_get_float(rhs_in));
|
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:
|
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));
|
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:
|
default:
|
||||||
return MP_OBJ_NULL; // op not supported
|
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;
|
string_image_facade_t *self = (string_image_facade_t *)self_in;
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case MP_UNARY_OP_LEN:
|
case MP_UNARY_OP_LEN:
|
||||||
|
@ -89,7 +89,11 @@ extern const mp_obj_type_t microbit_image_type;
|
|||||||
#define HEART_IMAGE (microbit_image_obj_t *)(µbit_const_image_heart_obj)
|
#define HEART_IMAGE (microbit_image_obj_t *)(µbit_const_image_heart_obj)
|
||||||
#define HAPPY_IMAGE (microbit_image_obj_t *)(µbit_const_image_happy_obj)
|
#define HAPPY_IMAGE (microbit_image_obj_t *)(µbit_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);
|
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);
|
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__
|
#endif // __MICROPY_INCLUDED_MICROBIT_IMAGE_H__
|
||||||
|
@ -43,8 +43,10 @@ STATIC mp_obj_t microbit_sleep(mp_obj_t ms_in) {
|
|||||||
mp_int_t ms;
|
mp_int_t ms;
|
||||||
if (mp_obj_is_integer(ms_in)) {
|
if (mp_obj_is_integer(ms_in)) {
|
||||||
ms = mp_obj_get_int(ms_in);
|
ms = mp_obj_get_int(ms_in);
|
||||||
|
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
|
||||||
} else {
|
} else {
|
||||||
ms = (mp_int_t)mp_obj_get_float(ms_in);
|
ms = (mp_int_t)mp_obj_get_float(ms_in);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
if (ms > 0) {
|
if (ms > 0) {
|
||||||
mp_hal_delay_ms(ms);
|
mp_hal_delay_ms(ms);
|
||||||
@ -98,7 +100,11 @@ STATIC mp_obj_t microbit_temperature(void) {
|
|||||||
NRF_TEMP->EVENTS_DATARDY = 0;
|
NRF_TEMP->EVENTS_DATARDY = 0;
|
||||||
temp = NRF_TEMP->TEMP;
|
temp = NRF_TEMP->TEMP;
|
||||||
NRF_TEMP->TASKS_STOP = 1;
|
NRF_TEMP->TASKS_STOP = 1;
|
||||||
|
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
|
||||||
return mp_obj_new_float(temp/4.0);
|
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);
|
MP_DEFINE_CONST_FUN_OBJ_0(microbit_temperature_obj, microbit_temperature);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user