Skip to content

Commit 4542b5a

Browse files
author
Martin Crossley
committed
add simple bounds checking
1 parent 447dcff commit 4542b5a

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

pio/seven_segment/pio_7_segment_library/7_segment_lib.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ static const uint8_t segments[] = {
3030
};
3131

3232

33-
// converts an integer into a 32-bit word representing up to four 7-segment digits
33+
// Simple example of how to convert a positive integer into a 32-bit word
34+
// representing up to four 7-segment digits. Could be extended to handle
35+
// negative numbers.
3436
//
3537
uint32_t int_to_seven_segment (int num) {
3638
uint32_t word = 0;
37-
if (num > 9999) {
38-
// argument exceeds 4 digits: display an 'E' symbol
39+
if (num > 9999 || num < 0) {
40+
// number out of range, display 'E' symbol
41+
// EDBGACF.
3942
word = 0b11011010
4043
} else {
4144
if (num == 0) {

0 commit comments

Comments
 (0)