Skip to content

Commit 8160eba

Browse files
committed
Added board config identification subroutines
1 parent 1266878 commit 8160eba

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

arduino/cores/board.go

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

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

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+
}

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)