Skip to content

Commit edf74a9

Browse files
authored
pybricks.pupdevices.ColorSensor: Fix ambient HSV overflow.
This caused the S and V values to become negative on very bright TV screens. Applications that used only the Hue value are unaffected.
1 parent 0298b17 commit edf74a9

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
## [Unreleased]
66

7+
### Fixed
8+
9+
- Fixed overflow in saturation and value for ambient color measurement. This
10+
can occur with very bright screens.
11+
712
## [3.6.0] - 2024-03-02
813

914
### Changed

pybricks/pupdevices/pb_type_pupdevices_colorsensor.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "py/mphal.h"
99

10+
#include <pbio/int_math.h>
11+
1012
#include <pybricks/common.h>
1113
#include <pybricks/parameters.h>
1214
#include <pybricks/pupdevices.h>
@@ -76,14 +78,8 @@ static void get_hsv_reflected(mp_obj_t self_in, pbio_color_hsv_t *hsv) {
7678
static void get_hsv_ambient(mp_obj_t self_in, pbio_color_hsv_t *hsv) {
7779
int16_t *data = pb_type_device_get_data(self_in, PBDRV_LEGODEV_MODE_PUP_COLOR_SENSOR__SHSV);
7880
hsv->h = data[0];
79-
hsv->s = data[1] / 10;
80-
if (hsv->s > 100) {
81-
hsv->s = 100;
82-
}
83-
hsv->v = data[2] / 10;
84-
if (hsv->v > 100) {
85-
hsv->v = 100;
86-
}
81+
hsv->s = pbio_int_math_min(data[1] / 10, 100);
82+
hsv->v = pbio_int_math_min(data[2] / 10, 100);
8783
}
8884

8985
// pybricks.pupdevices.ColorSensor.hsv(surface=True)

0 commit comments

Comments
 (0)