Skip to content

Commit f1a78f1

Browse files
committed
pbio/iodev: Set writability flag.
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.
1 parent eb08bae commit f1a78f1

File tree

6 files changed

+34
-28
lines changed

6 files changed

+34
-28
lines changed

lib/pbio/drv/ioport/ioport_lpf2.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static const basic_info_t basic_infos[] = {
6262
},
6363
.mode = {
6464
.num_values = 1,
65-
.data_type = PBIO_IODEV_DATA_TYPE_INT8,
65+
.data_type = PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE,
6666
},
6767
},
6868
[PBIO_IODEV_TYPE_ID_LPF2_TRAIN] = {
@@ -73,7 +73,7 @@ static const basic_info_t basic_infos[] = {
7373
},
7474
.mode = {
7575
.num_values = 1,
76-
.data_type = PBIO_IODEV_DATA_TYPE_INT8,
76+
.data_type = PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE,
7777
},
7878
},
7979
[PBIO_IODEV_TYPE_ID_LPF2_LIGHT] = {
@@ -84,7 +84,7 @@ static const basic_info_t basic_infos[] = {
8484
},
8585
.mode = {
8686
.num_values = 1,
87-
.data_type = PBIO_IODEV_DATA_TYPE_INT8,
87+
.data_type = PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE,
8888
},
8989
},
9090
[PBIO_IODEV_TYPE_ID_LPF2_LIGHT1] = {
@@ -95,7 +95,7 @@ static const basic_info_t basic_infos[] = {
9595
},
9696
.mode = {
9797
.num_values = 1,
98-
.data_type = PBIO_IODEV_DATA_TYPE_INT8,
98+
.data_type = PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE,
9999
},
100100
},
101101
[PBIO_IODEV_TYPE_ID_LPF2_LIGHT2] = {
@@ -106,7 +106,7 @@ static const basic_info_t basic_infos[] = {
106106
},
107107
.mode = {
108108
.num_values = 1,
109-
.data_type = PBIO_IODEV_DATA_TYPE_INT8,
109+
.data_type = PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE,
110110
},
111111
},
112112
};

lib/pbio/include/pbio/iodev.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ typedef enum {
159159
PBIO_IODEV_DATA_TYPE_FLOAT = LUMP_DATA_TYPE_DATAF,
160160
} pbio_iodev_data_type_t;
161161

162+
// Bit mask for pbio_iodev_data_type_t
163+
#define PBIO_IODEV_DATA_TYPE_MASK (0x03)
164+
165+
// Third bit indicates whether the mode supports writing
166+
#define PBIO_IODEV_DATA_TYPE_WRITABLE (0x04)
167+
162168
/**
163169
* The maximum number of modes a I/O device can have.
164170
*/
@@ -259,7 +265,7 @@ typedef struct {
259265
*/
260266
uint8_t num_values;
261267
/**
262-
* The binary format of the data for this mode.
268+
* The binary format of the data for this mode and writability flag.
263269
*/
264270
pbio_iodev_data_type_t data_type;
265271
} pbio_iodev_mode_t;

lib/pbio/src/iodev.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @return The size of the type or 0 if the type was not valid
1515
*/
1616
size_t pbio_iodev_size_of(pbio_iodev_data_type_t type) {
17-
switch (type) {
17+
switch (type & PBIO_IODEV_DATA_TYPE_MASK) {
1818
case PBIO_IODEV_DATA_TYPE_INT8:
1919
return 1;
2020
case PBIO_IODEV_DATA_TYPE_INT16:

lib/pbio/src/uartdev.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,12 @@ static void pbio_uartdev_parse_msg(uartdev_port_data_t *data) {
491491
goto err;
492492
}
493493

494-
// Check that mode supports writing.
495-
if (data->rx_msg[3]) {
496-
// TODO: Store write capability
497-
}
494+
// Mode supports writing if rx_msg[3] is nonzero. Store in 3rd bit of data type.
495+
data->info->mode_info[mode].data_type = (data->rx_msg[3] != 0) << 2;
498496

499497
debug_pr("mapping: in %02x out %02x\n", data->rx_msg[2], data->rx_msg[3]);
498+
debug_pr("mapping: in %02x out %02x\n", data->rx_msg[2], data->rx_msg[3]);
499+
debug_pr("Writable: %d\n", data->info->mode_info[mode].data_type & PBIO_IODEV_DATA_TYPE_WRITABLE);
500500

501501
break;
502502
case LUMP_INFO_MODE_COMBOS:
@@ -555,13 +555,14 @@ static void pbio_uartdev_parse_msg(uartdev_port_data_t *data) {
555555
DBG_ERR(data->last_err = "Did not receive all required INFO");
556556
goto err;
557557
}
558-
data->info->mode_info[mode].data_type = data->rx_msg[3];
558+
// Mode writability has been set previously. Now OR with mode type.
559+
data->info->mode_info[mode].data_type |= data->rx_msg[3];
559560
if (data->new_mode) {
560561
data->new_mode--;
561562
}
562563

563564
debug_pr("num_values: %d\n", data->info->mode_info[mode].num_values);
564-
debug_pr("data_type: %d\n", data->info->mode_info[mode].data_type);
565+
debug_pr("data_type: %d\n", data->info->mode_info[mode].data_type & PBIO_IODEV_DATA_TYPE_MASK);
565566

566567
break;
567568
}
@@ -1093,8 +1094,7 @@ static pbio_error_t ev3_uart_set_data_begin(pbio_iodev_t *iodev, const uint8_t *
10931094
uint8_t size;
10941095

10951096
// not all modes support setting data
1096-
bool writable = 0; // TODO: Check write capability
1097-
if (!writable) {
1097+
if (!(mode->data_type & PBIO_IODEV_DATA_TYPE_WRITABLE)) {
10981098
return PBIO_ERROR_INVALID_OP;
10991099
}
11001100

lib/pbio/test/src/uartdev.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,13 @@ static PT_THREAD(test_boost_color_distance_sensor(struct pt *pt)) {
376376
tt_want_uint_op(iodev->info->mode_info[4].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8);
377377

378378
tt_want_uint_op(iodev->info->mode_info[5].num_values, ==, 1);
379-
tt_want_uint_op(iodev->info->mode_info[5].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8);
379+
tt_want_uint_op(iodev->info->mode_info[5].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE);
380380

381381
tt_want_uint_op(iodev->info->mode_info[6].num_values, ==, 3);
382382
tt_want_uint_op(iodev->info->mode_info[6].data_type, ==, PBIO_IODEV_DATA_TYPE_INT16);
383383

384384
tt_want_uint_op(iodev->info->mode_info[7].num_values, ==, 1);
385-
tt_want_uint_op(iodev->info->mode_info[7].data_type, ==, PBIO_IODEV_DATA_TYPE_INT16);
385+
tt_want_uint_op(iodev->info->mode_info[7].data_type, ==, PBIO_IODEV_DATA_TYPE_INT16 | PBIO_IODEV_DATA_TYPE_WRITABLE);
386386

387387
tt_want_uint_op(iodev->info->mode_info[8].num_values, ==, 4);
388388
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)) {
608608
tt_want_uint_op(iodev->mode, ==, 0);
609609

610610
tt_want_uint_op(iodev->info->mode_info[0].num_values, ==, 1);
611-
tt_want_uint_op(iodev->info->mode_info[0].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8);
611+
tt_want_uint_op(iodev->info->mode_info[0].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE);
612612

613613
tt_want_uint_op(iodev->info->mode_info[1].num_values, ==, 1);
614614
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)) {
811811
tt_want_uint_op(iodev->mode, ==, 0);
812812

813813
tt_want_uint_op(iodev->info->mode_info[0].num_values, ==, 1);
814-
tt_want_uint_op(iodev->info->mode_info[0].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8);
814+
tt_want_uint_op(iodev->info->mode_info[0].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE);
815815

816816
tt_want_uint_op(iodev->info->mode_info[1].num_values, ==, 1);
817-
tt_want_uint_op(iodev->info->mode_info[1].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8);
817+
tt_want_uint_op(iodev->info->mode_info[1].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE);
818818

819819
tt_want_uint_op(iodev->info->mode_info[2].num_values, ==, 1);
820-
tt_want_uint_op(iodev->info->mode_info[2].data_type, ==, PBIO_IODEV_DATA_TYPE_INT32);
820+
tt_want_uint_op(iodev->info->mode_info[2].data_type, ==, PBIO_IODEV_DATA_TYPE_INT32 | PBIO_IODEV_DATA_TYPE_WRITABLE);
821821

822822
tt_want_uint_op(iodev->info->mode_info[3].num_values, ==, 1);
823-
tt_want_uint_op(iodev->info->mode_info[3].data_type, ==, PBIO_IODEV_DATA_TYPE_INT16);
823+
tt_want_uint_op(iodev->info->mode_info[3].data_type, ==, PBIO_IODEV_DATA_TYPE_INT16 | PBIO_IODEV_DATA_TYPE_WRITABLE);
824824

825825
tt_want_uint_op(iodev->info->mode_info[4].num_values, ==, 2);
826826
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)) {
10201020
tt_want_uint_op(iodev->mode, ==, 0);
10211021

10221022
tt_want_uint_op(iodev->info->mode_info[0].num_values, ==, 1);
1023-
tt_want_uint_op(iodev->info->mode_info[0].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8);
1023+
tt_want_uint_op(iodev->info->mode_info[0].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE);
10241024

10251025
tt_want_uint_op(iodev->info->mode_info[1].num_values, ==, 1);
1026-
tt_want_uint_op(iodev->info->mode_info[1].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8);
1026+
tt_want_uint_op(iodev->info->mode_info[1].data_type, ==, PBIO_IODEV_DATA_TYPE_INT8 | PBIO_IODEV_DATA_TYPE_WRITABLE);
10271027

10281028
tt_want_uint_op(iodev->info->mode_info[2].num_values, ==, 1);
1029-
tt_want_uint_op(iodev->info->mode_info[2].data_type, ==, PBIO_IODEV_DATA_TYPE_INT32);
1029+
tt_want_uint_op(iodev->info->mode_info[2].data_type, ==, PBIO_IODEV_DATA_TYPE_INT32 | PBIO_IODEV_DATA_TYPE_WRITABLE);
10301030

10311031
tt_want_uint_op(iodev->info->mode_info[3].num_values, ==, 1);
1032-
tt_want_uint_op(iodev->info->mode_info[3].data_type, ==, PBIO_IODEV_DATA_TYPE_INT16);
1032+
tt_want_uint_op(iodev->info->mode_info[3].data_type, ==, PBIO_IODEV_DATA_TYPE_INT16 | PBIO_IODEV_DATA_TYPE_WRITABLE);
10331033

10341034
tt_want_uint_op(iodev->info->mode_info[4].num_values, ==, 2);
10351035
tt_want_uint_op(iodev->info->mode_info[4].data_type, ==, PBIO_IODEV_DATA_TYPE_INT16);

pybricks/util_pb/pb_device_stm32.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void pb_device_get_values(pb_device_t *pbdev, uint8_t mode, int32_t *values) {
115115
}
116116

117117
for (uint8_t i = 0; i < len; i++) {
118-
switch (type) {
118+
switch (type & PBIO_IODEV_DATA_TYPE_MASK) {
119119
case PBIO_IODEV_DATA_TYPE_INT8:
120120
values[i] = *((int8_t *)(data + i * 1));
121121
break;
@@ -153,7 +153,7 @@ void pb_device_set_values(pb_device_t *pbdev, uint8_t mode, int32_t *values, uin
153153
}
154154

155155
for (uint8_t i = 0; i < len; i++) {
156-
switch (type) {
156+
switch (type & PBIO_IODEV_DATA_TYPE_MASK) {
157157
case PBIO_IODEV_DATA_TYPE_INT8:
158158
*(int8_t *)(data + i) = values[i];
159159
break;

0 commit comments

Comments
 (0)