Skip to content

Commit 8851b43

Browse files
committed
pybricks.pupdevices.DuploTrain: Use 2-byte speed mode.
This makes speed values similar across both variants.
1 parent 012a26e commit 8851b43

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

pybricks/iodevices/pb_type_iodevices_lwp3device.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ MP_DEFINE_CONST_OBJ_TYPE(pb_type_technic_move_hub,
760760
#define DUPLO_NEW_PORT_COLOR (0x33)
761761
#define DUPLO_NEW_PORT_COLOR_MODE_TAG (0x00)
762762
#define DUPLO_NEW_PORT_SPEED (0x36)
763-
#define DUPLO_NEW_PORT_SPEED_MODE_SPEED (0)
763+
#define DUPLO_NEW_PORT_SPEED_MODE_SPEED (2)
764764

765765
static const uint8_t pb_type_duplo_train_old_activate_speaker[] = {
766766
0x0a, 0x00, 0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01,
@@ -784,19 +784,19 @@ static void pb_type_duplo_train_handle_notification(void *user, const uint8_t *v
784784

785785
uint8_t port = value[3];
786786

787-
// Store speed byte as data 0.
788-
if ((size == 6 && port == DUPLO_OLD_PORT_SPEED) || (size == 5 && port == DUPLO_NEW_PORT_SPEED)) {
789-
self->data[0] = value[4];
787+
// Get speed as two-byte value for either train.
788+
if (size == 6 && (port == DUPLO_NEW_PORT_SPEED || port == DUPLO_OLD_PORT_SPEED)) {
789+
memcpy(&self->data[0], &value[4], 2);
790790
}
791791

792792
// Store tag id bytes for new trains.
793793
if (size == 6 && port == DUPLO_NEW_PORT_COLOR) {
794-
memcpy(&self->data[1], &value[4], 2);
794+
memcpy(&self->data[2], &value[4], 2);
795795
}
796796

797797
// Store rgb bytes for old trains.
798798
if (size == 10 && port == DUPLO_OLD_PORT_COLOR) {
799-
memcpy(&self->data[1], &value[4], 6);
799+
memcpy(&self->data[2], &value[4], 6);
800800
}
801801
}
802802

@@ -983,7 +983,7 @@ static mp_obj_t pb_type_duplo_train_speed(mp_obj_t self_in) {
983983
if (!pbdrv_bluetooth_peripheral_is_connected(self->peripheral)) {
984984
pb_assert(PBIO_ERROR_NO_DEV);
985985
}
986-
return mp_obj_new_int((int8_t)self->data[0]);
986+
return mp_obj_new_int((int16_t)pbio_get_uint16_le(&self->data[0]));
987987
}
988988
static MP_DEFINE_CONST_FUN_OBJ_1(pb_type_duplo_train_speed_obj, pb_type_duplo_train_speed);
989989

@@ -1004,9 +1004,9 @@ static mp_obj_t pb_type_duplo_train_color(mp_obj_t self_in) {
10041004
// this is pretty niche, we won't introduce new color objects just for
10051005
// these tags, but just round them to the nearest available color.
10061006
const pbio_color_rgb_t rgb = {
1007-
.r = rgb_to_byte(&self->data[1]),
1008-
.g = rgb_to_byte(&self->data[3]),
1009-
.b = rgb_to_byte(&self->data[5]),
1007+
.r = rgb_to_byte(&self->data[2]),
1008+
.g = rgb_to_byte(&self->data[4]),
1009+
.b = rgb_to_byte(&self->data[6]),
10101010
};
10111011
pbio_color_hsv_t hsv;
10121012
pbio_color_rgb_to_hsv(&rgb, &hsv);
@@ -1045,7 +1045,7 @@ static mp_obj_t pb_type_duplo_train_color(mp_obj_t self_in) {
10451045
// tag. We can't know if we are currently still on that tag or not.
10461046
// There might be some logic to these tag IDs, but we've just tried
10471047
// all of them, resulting in these two byte IDs.
1048-
switch (pbio_get_uint16_le(&self->data[1])) {
1048+
switch (pbio_get_uint16_le(&self->data[2])) {
10491049
case 1:
10501050
return MP_OBJ_FROM_PTR(&pb_Color_WHITE_obj);
10511051
case 24:

0 commit comments

Comments
 (0)