Skip to content

Commit 0b75526

Browse files
committed
codal_port/microbit_soundeffect: Raise exception if value doesn't fit.
Fixes issue #120. Signed-off-by: Damien George <damien@micropython.org>
1 parent 6ea476f commit 0b75526

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/codal_port/microbit_soundeffect.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ STATIC void sound_expr_encode(microbit_soundeffect_obj_t *self, size_t offset, s
129129
self->sound_expr[offset + i - 1] = '0' + value % 10;
130130
value /= 10;
131131
}
132+
if (value != 0) {
133+
if (length == 1) {
134+
mp_raise_ValueError(MP_ERROR_TEXT("maximum value is 9"));
135+
} else if (length == 2) {
136+
mp_raise_ValueError(MP_ERROR_TEXT("maximum value is 99"));
137+
} else {
138+
mp_raise_ValueError(MP_ERROR_TEXT("maximum value is 9999"));
139+
}
140+
}
132141
}
133142

134143
STATIC unsigned int sound_expr_decode(const microbit_soundeffect_obj_t *self, size_t offset, size_t length) {

0 commit comments

Comments
 (0)