Skip to content

Commit 097e4e6

Browse files
lucarin91giulio93
andauthored
feat: add monza support (#8)
* feat: add monza support * Update board/board.go Co-authored-by: Giulio <pilotto.giulio@gmail.com> * Apply suggestions from code review Co-authored-by: Giulio <pilotto.giulio@gmail.com> * fix after code review --------- Co-authored-by: Giulio <pilotto.giulio@gmail.com>
1 parent 3cf11b1 commit 097e4e6

1 file changed

Lines changed: 33 additions & 5 deletions

File tree

board/board.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
package board
1717

1818
import (
19+
"bytes"
1920
"context"
21+
"io"
2022
"os"
21-
"slices"
2223
"strconv"
2324
"strings"
2425
"sync"
@@ -30,12 +31,39 @@ type Boarder interface {
3031
MkDirAll(ctx context.Context, path string) error
3132
}
3233

34+
var knownBoards = []string{"arduino,imola", "arduino,monza", "arduino"}
35+
3336
var OnBoard = sync.OnceValue(func() bool {
34-
var boardNames = []string{"UNO Q\n", "Imola\n", "Inc. Robotics RB1\n"}
35-
buf, err := os.ReadFile("/sys/class/dmi/id/product_name")
36-
if err == nil && slices.Contains(boardNames, string(buf)) {
37-
return true
37+
trimAndLower := func(s []byte) []byte {
38+
return bytes.ToLower(bytes.Trim(s, " \n\t\r\x00"))
39+
}
40+
41+
readFile := func(path string) ([]byte, error) {
42+
f, err := os.Open(path)
43+
if err != nil {
44+
return nil, err
45+
}
46+
defer f.Close()
47+
return io.ReadAll(f)
48+
}
49+
50+
// legacy check for imola
51+
if buf, err := readFile("/sys/class/dmi/id/product_name"); err == nil {
52+
return string(trimAndLower(buf)) == "imola"
3853
}
54+
55+
if buf, err := readFile("/sys/firmware/devicetree/base/compatible"); err == nil {
56+
for _, raw := range bytes.Split(buf, []byte{'\x00'}) {
57+
compatible := string(trimAndLower(raw))
58+
59+
for _, knownBoard := range knownBoards {
60+
if strings.HasPrefix(compatible, knownBoard) {
61+
return true
62+
}
63+
}
64+
}
65+
}
66+
3967
return false
4068
})()
4169

0 commit comments

Comments
 (0)