Skip to content

Commit 6cb04f7

Browse files
committed
Added board config identification subroutines
1 parent 3155323 commit 6cb04f7

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

Diff for: arduino/cores/board.go

+38-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func (b *Board) GetIdentificationProperties() []*properties.Map {
165165
}
166166

167167
// IsBoardMatchingIDProperties returns true if the board match the given
168-
// identification properties
168+
// upload port identification properties
169169
func (b *Board) IsBoardMatchingIDProperties(query *properties.Map) bool {
170170
// check checks if the given set of properties p match the "query"
171171
check := func(p *properties.Map) bool {
@@ -185,3 +185,40 @@ func (b *Board) IsBoardMatchingIDProperties(query *properties.Map) bool {
185185
}
186186
return false
187187
}
188+
189+
// IdentifyBoardConfiguration returns the configuration of the board that can be
190+
// deduced from the given upload port identification properties
191+
func (b *Board) IdentifyBoardConfiguration(query *properties.Map) *properties.Map {
192+
// check checks if the given set of properties p match the "query"
193+
check := func(p *properties.Map) bool {
194+
for k, v := range p.AsMap() {
195+
if !strings.EqualFold(query.Get(k), v) {
196+
return false
197+
}
198+
}
199+
return true
200+
}
201+
checkAll := func(allP []*properties.Map) bool {
202+
for _, p := range allP {
203+
if check(p) {
204+
return true
205+
}
206+
}
207+
return false
208+
}
209+
210+
res := properties.NewMap()
211+
for _, option := range b.GetConfigOptions().Keys() {
212+
values := b.GetConfigOptionValues(option).Keys()
213+
214+
for _, value := range values {
215+
config := option + "=" + value
216+
configProps := b.configOptionProperties[config]
217+
218+
if checkAll(configProps.ExtractSubIndexSets("upload_port")) {
219+
res.Set(option, value)
220+
}
221+
}
222+
}
223+
return res
224+
}

Diff for: arduino/cores/board_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -554,3 +554,66 @@ func TestBoardMatching(t *testing.T) {
554554
"lemons": "XXX",
555555
})))
556556
}
557+
558+
func TestBoardConfigMatching(t *testing.T) {
559+
brd01 := &Board{
560+
Properties: properties.NewFromHashmap(map[string]string{
561+
"upload_port.pid": "0x0010",
562+
"upload_port.vid": "0x2341",
563+
"menu.cpu.atmega1280": "ATmega1280",
564+
"menu.cpu.atmega1280.upload_port.cpu": "atmega1280",
565+
"menu.cpu.atmega1280.build_cpu": "atmega1280",
566+
"menu.cpu.atmega2560": "ATmega2560",
567+
"menu.cpu.atmega2560.upload_port.cpu": "atmega2560",
568+
"menu.cpu.atmega2560.build_cpu": "atmega2560",
569+
"menu.mem.1k": "1KB",
570+
"menu.mem.1k.upload_port.mem": "1",
571+
"menu.mem.1k.build_mem": "1024",
572+
"menu.mem.2k": "2KB",
573+
"menu.mem.2k.upload_port.1.mem": "2",
574+
"menu.mem.2k.upload_port.2.ab": "ef",
575+
"menu.mem.2k.upload_port.2.cd": "gh",
576+
"menu.mem.2k.build_mem": "2048",
577+
}),
578+
PlatformRelease: &PlatformRelease{
579+
Platform: &Platform{
580+
Architecture: "avr",
581+
Package: &Package{
582+
Name: "arduino",
583+
},
584+
},
585+
Menus: properties.NewFromHashmap(map[string]string{
586+
"cpu": "Processor",
587+
"mem": "Memory",
588+
}),
589+
},
590+
}
591+
require.True(t, brd01.IsBoardMatchingIDProperties(properties.NewFromHashmap(map[string]string{
592+
"pid": "0x0010",
593+
"vid": "0x2341",
594+
})))
595+
res := brd01.IdentifyBoardConfiguration(properties.NewFromHashmap(map[string]string{
596+
"cpu": "atmega2560",
597+
}))
598+
require.EqualValues(t, map[string]string{"cpu": "atmega2560"}, res.AsMap())
599+
res = brd01.IdentifyBoardConfiguration(properties.NewFromHashmap(map[string]string{
600+
"cpu": "atmega1280",
601+
}))
602+
require.EqualValues(t, map[string]string{"cpu": "atmega1280"}, res.AsMap())
603+
res = brd01.IdentifyBoardConfiguration(properties.NewFromHashmap(map[string]string{
604+
"cpu": "atmega1280",
605+
"mem": "1",
606+
}))
607+
require.EqualValues(t, map[string]string{"cpu": "atmega1280", "mem": "1k"}, res.AsMap())
608+
res = brd01.IdentifyBoardConfiguration(properties.NewFromHashmap(map[string]string{
609+
"cpu": "atmega1280",
610+
"ab": "ef",
611+
}))
612+
require.EqualValues(t, map[string]string{"cpu": "atmega1280"}, res.AsMap())
613+
res = brd01.IdentifyBoardConfiguration(properties.NewFromHashmap(map[string]string{
614+
"cpu": "atmega1280",
615+
"ab": "ef",
616+
"cd": "gh",
617+
}))
618+
require.EqualValues(t, map[string]string{"cpu": "atmega1280", "mem": "2k"}, res.AsMap())
619+
}

Diff for: arduino/cores/packagemanager/identify.go

+9
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,12 @@ func (pm *PackageManager) IdentifyBoard(idProps *properties.Map) []*cores.Board
3535

3636
return foundBoards
3737
}
38+
39+
// IdentifyBoardConfiguration returns a set of identification properties for configuration options
40+
// of a board.
41+
func (pm *PackageManager) IdentifyBoardConfiguration(idProps *properties.Map, board *cores.Board) *properties.Map {
42+
if idProps.Size() == 0 {
43+
return properties.NewMap()
44+
}
45+
return board.IdentifyBoardConfiguration(idProps)
46+
}

0 commit comments

Comments
 (0)