diff --git a/lib/pbio/drv/ioport/ioport_lpf2.c b/lib/pbio/drv/ioport/ioport_lpf2.c index 8cea8ed1d..8c7e02ad8 100644 --- a/lib/pbio/drv/ioport/ioport_lpf2.c +++ b/lib/pbio/drv/ioport/ioport_lpf2.c @@ -62,7 +62,7 @@ static const basic_info_t basic_infos[] = { }, .mode = { .num_values = 1, - .data_type = PBIO_IODEV_DATA_TYPE_INT8, + .data_type = PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE, }, }, [PBIO_IODEV_TYPE_ID_LPF2_TRAIN] = { @@ -73,7 +73,7 @@ static const basic_info_t basic_infos[] = { }, .mode = { .num_values = 1, - .data_type = PBIO_IODEV_DATA_TYPE_INT8, + .data_type = PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE, }, }, [PBIO_IODEV_TYPE_ID_LPF2_LIGHT] = { @@ -84,7 +84,7 @@ static const basic_info_t basic_infos[] = { }, .mode = { .num_values = 1, - .data_type = PBIO_IODEV_DATA_TYPE_INT8, + .data_type = PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE, }, }, [PBIO_IODEV_TYPE_ID_LPF2_LIGHT1] = { @@ -95,7 +95,7 @@ static const basic_info_t basic_infos[] = { }, .mode = { .num_values = 1, - .data_type = PBIO_IODEV_DATA_TYPE_INT8, + .data_type = PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE, }, }, [PBIO_IODEV_TYPE_ID_LPF2_LIGHT2] = { @@ -106,7 +106,7 @@ static const basic_info_t basic_infos[] = { }, .mode = { .num_values = 1, - .data_type = PBIO_IODEV_DATA_TYPE_INT8, + .data_type = PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE, }, }, }; diff --git a/lib/pbio/include/pbio/iodev.h b/lib/pbio/include/pbio/iodev.h index b2f2cc8ef..5515e791d 100644 --- a/lib/pbio/include/pbio/iodev.h +++ b/lib/pbio/include/pbio/iodev.h @@ -159,6 +159,12 @@ typedef enum { PBIO_IODEV_DATA_TYPE_FLOAT = LUMP_DATA_TYPE_DATAF, } pbio_iodev_data_type_t; +// Bit mask for pbio_iodev_data_type_t +#define PBIO_IODEV_DATA_TYPE_MASK (0x03) + +// Third bit indicates whether the mode supports writing +#define PBIO_IODEV_DATA_TYPE_WRITABLE (0x04) + /** * The maximum number of modes a I/O device can have. */ @@ -259,7 +265,7 @@ typedef struct { */ uint8_t num_values; /** - * The binary format of the data for this mode. + * The binary format of the data for this mode and writability flag. */ pbio_iodev_data_type_t data_type; } pbio_iodev_mode_t; diff --git a/lib/pbio/src/iodev.c b/lib/pbio/src/iodev.c index 954ac5aad..645e260bf 100644 --- a/lib/pbio/src/iodev.c +++ b/lib/pbio/src/iodev.c @@ -14,7 +14,7 @@ * @return The size of the type or 0 if the type was not valid */ size_t pbio_iodev_size_of(pbio_iodev_data_type_t type) { - switch (type) { + switch (type & PBIO_IODEV_DATA_TYPE_MASK) { case PBIO_IODEV_DATA_TYPE_INT8: return 1; case PBIO_IODEV_DATA_TYPE_INT16: diff --git a/lib/pbio/src/uartdev.c b/lib/pbio/src/uartdev.c index bff341b01..32beb66a2 100644 --- a/lib/pbio/src/uartdev.c +++ b/lib/pbio/src/uartdev.c @@ -491,12 +491,12 @@ static void pbio_uartdev_parse_msg(uartdev_port_data_t *data) { goto err; } - // Check that mode supports writing. - if (data->rx_msg[3]) { - // TODO: Store write capability - } + // Mode supports writing if rx_msg[3] is nonzero. Store in 3rd bit of data type. + data->info->mode_info[mode].data_type = (data->rx_msg[3] != 0) << 2; debug_pr("mapping: in %02x out %02x\n", data->rx_msg[2], data->rx_msg[3]); + debug_pr("mapping: in %02x out %02x\n", data->rx_msg[2], data->rx_msg[3]); + debug_pr("Writable: %d\n", data->info->mode_info[mode].data_type & PBIO_IODEV_DATA_TYPE_WRITABLE); break; case LUMP_INFO_MODE_COMBOS: @@ -555,13 +555,14 @@ static void pbio_uartdev_parse_msg(uartdev_port_data_t *data) { DBG_ERR(data->last_err = "Did not receive all required INFO"); goto err; } - data->info->mode_info[mode].data_type = data->rx_msg[3]; + // Mode writability has been set previously. Now OR with mode type. + data->info->mode_info[mode].data_type |= data->rx_msg[3]; if (data->new_mode) { data->new_mode--; } debug_pr("num_values: %d\n", data->info->mode_info[mode].num_values); - debug_pr("data_type: %d\n", data->info->mode_info[mode].data_type); + debug_pr("data_type: %d\n", data->info->mode_info[mode].data_type & PBIO_IODEV_DATA_TYPE_MASK); break; } @@ -1093,8 +1094,7 @@ static pbio_error_t ev3_uart_set_data_begin(pbio_iodev_t *iodev, const uint8_t * uint8_t size; // not all modes support setting data - bool writable = 0; // TODO: Check write capability - if (!writable) { + if (!(mode->data_type & PBIO_IODEV_DATA_TYPE_WRITABLE)) { return PBIO_ERROR_INVALID_OP; } diff --git a/lib/pbio/test/src/uartdev.c b/lib/pbio/test/src/uartdev.c index 9a5d9366b..de56efabb 100644 --- a/lib/pbio/test/src/uartdev.c +++ b/lib/pbio/test/src/uartdev.c @@ -376,13 +376,13 @@ static PT_THREAD(test_boost_color_distance_sensor(struct pt *pt)) { tt_want_uint_op(iodev->info->mode_info[4].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8); tt_want_uint_op(iodev->info->mode_info[5].num_values, ==, 1); - tt_want_uint_op(iodev->info->mode_info[5].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8); + tt_want_uint_op(iodev->info->mode_info[5].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE); tt_want_uint_op(iodev->info->mode_info[6].num_values, ==, 3); tt_want_uint_op(iodev->info->mode_info[6].data_type, ==, PBIO_IODEV_DATA_TYPE_INT16); tt_want_uint_op(iodev->info->mode_info[7].num_values, ==, 1); - tt_want_uint_op(iodev->info->mode_info[7].data_type, ==, PBIO_IODEV_DATA_TYPE_INT16); + tt_want_uint_op(iodev->info->mode_info[7].data_type, ==, PBIO_IODEV_DATA_TYPE_INT16 | PBIO_IODEV_DATA_TYPE_WRITABLE); tt_want_uint_op(iodev->info->mode_info[8].num_values, ==, 4); tt_want_uint_op(iodev->info->mode_info[8].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8); @@ -608,7 +608,7 @@ static PT_THREAD(test_boost_interactive_motor(struct pt *pt)) { tt_want_uint_op(iodev->mode, ==, 0); tt_want_uint_op(iodev->info->mode_info[0].num_values, ==, 1); - tt_want_uint_op(iodev->info->mode_info[0].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8); + tt_want_uint_op(iodev->info->mode_info[0].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE); tt_want_uint_op(iodev->info->mode_info[1].num_values, ==, 1); tt_want_uint_op(iodev->info->mode_info[1].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8); @@ -811,16 +811,16 @@ static PT_THREAD(test_technic_large_motor(struct pt *pt)) { tt_want_uint_op(iodev->mode, ==, 0); tt_want_uint_op(iodev->info->mode_info[0].num_values, ==, 1); - tt_want_uint_op(iodev->info->mode_info[0].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8); + tt_want_uint_op(iodev->info->mode_info[0].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE); tt_want_uint_op(iodev->info->mode_info[1].num_values, ==, 1); - tt_want_uint_op(iodev->info->mode_info[1].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8); + tt_want_uint_op(iodev->info->mode_info[1].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE); tt_want_uint_op(iodev->info->mode_info[2].num_values, ==, 1); - tt_want_uint_op(iodev->info->mode_info[2].data_type, ==, PBIO_IODEV_DATA_TYPE_INT32); + tt_want_uint_op(iodev->info->mode_info[2].data_type, ==, PBIO_IODEV_DATA_TYPE_INT32 | PBIO_IODEV_DATA_TYPE_WRITABLE); tt_want_uint_op(iodev->info->mode_info[3].num_values, ==, 1); - tt_want_uint_op(iodev->info->mode_info[3].data_type, ==, PBIO_IODEV_DATA_TYPE_INT16); + tt_want_uint_op(iodev->info->mode_info[3].data_type, ==, PBIO_IODEV_DATA_TYPE_INT16 | PBIO_IODEV_DATA_TYPE_WRITABLE); tt_want_uint_op(iodev->info->mode_info[4].num_values, ==, 2); tt_want_uint_op(iodev->info->mode_info[4].data_type, ==, PBIO_IODEV_DATA_TYPE_INT16); @@ -1020,16 +1020,16 @@ static PT_THREAD(test_technic_xl_motor(struct pt *pt)) { tt_want_uint_op(iodev->mode, ==, 0); tt_want_uint_op(iodev->info->mode_info[0].num_values, ==, 1); - tt_want_uint_op(iodev->info->mode_info[0].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8); + tt_want_uint_op(iodev->info->mode_info[0].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE); tt_want_uint_op(iodev->info->mode_info[1].num_values, ==, 1); - tt_want_uint_op(iodev->info->mode_info[1].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8); + tt_want_uint_op(iodev->info->mode_info[1].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE); tt_want_uint_op(iodev->info->mode_info[2].num_values, ==, 1); - tt_want_uint_op(iodev->info->mode_info[2].data_type, ==, PBIO_IODEV_DATA_TYPE_INT32); + tt_want_uint_op(iodev->info->mode_info[2].data_type, ==, PBIO_IODEV_DATA_TYPE_INT32 | PBIO_IODEV_DATA_TYPE_WRITABLE); tt_want_uint_op(iodev->info->mode_info[3].num_values, ==, 1); - tt_want_uint_op(iodev->info->mode_info[3].data_type, ==, PBIO_IODEV_DATA_TYPE_INT16); + tt_want_uint_op(iodev->info->mode_info[3].data_type, ==, PBIO_IODEV_DATA_TYPE_INT16 | PBIO_IODEV_DATA_TYPE_WRITABLE); tt_want_uint_op(iodev->info->mode_info[4].num_values, ==, 2); tt_want_uint_op(iodev->info->mode_info[4].data_type, ==, PBIO_IODEV_DATA_TYPE_INT16); diff --git a/pybricks/util_pb/pb_device_stm32.c b/pybricks/util_pb/pb_device_stm32.c index 572a5b39b..72676f7d6 100644 --- a/pybricks/util_pb/pb_device_stm32.c +++ b/pybricks/util_pb/pb_device_stm32.c @@ -115,7 +115,7 @@ void pb_device_get_values(pb_device_t *pbdev, uint8_t mode, int32_t *values) { } for (uint8_t i = 0; i < len; i++) { - switch (type) { + switch (type & PBIO_IODEV_DATA_TYPE_MASK) { case PBIO_IODEV_DATA_TYPE_INT8: values[i] = *((int8_t *)(data + i * 1)); break; @@ -153,7 +153,7 @@ void pb_device_set_values(pb_device_t *pbdev, uint8_t mode, int32_t *values, uin } for (uint8_t i = 0; i < len; i++) { - switch (type) { + switch (type & PBIO_IODEV_DATA_TYPE_MASK) { case PBIO_IODEV_DATA_TYPE_INT8: *(int8_t *)(data + i) = values[i]; break;