Skip to content

Commit

Permalink
pbio/iodev: Set writability flag.
Browse files Browse the repository at this point in the history
This replaces the previously deleted output flag.

It is encoded in the data type. This saves yet another static byte per port, and it makes sense because this property is closely associated with that particular data.
  • Loading branch information
laurensvalk committed Jun 22, 2021
1 parent eb08bae commit f1a78f1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 28 deletions.
10 changes: 5 additions & 5 deletions lib/pbio/drv/ioport/ioport_lpf2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand All @@ -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] = {
Expand All @@ -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] = {
Expand All @@ -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] = {
Expand All @@ -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,
},
},
};
Expand Down
8 changes: 7 additions & 1 deletion lib/pbio/include/pbio/iodev.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/pbio/src/iodev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 8 additions & 8 deletions lib/pbio/src/uartdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down
22 changes: 11 additions & 11 deletions lib/pbio/test/src/uartdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions pybricks/util_pb/pb_device_stm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit f1a78f1

Please sign in to comment.