Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add programmer field to sketch profile and --profile flag to debug command #2505

Merged
merged 13 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions commands/sketch/set_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func SetSketchDefaults(ctx context.Context, req *rpc.SetSketchDefaultsRequest) (
oldAddress, oldProtocol := sk.GetDefaultPortAddressAndProtocol()
res := &rpc.SetSketchDefaultsResponse{
DefaultFqbn: sk.GetDefaultFQBN(),
DefaultProgrammer: sk.GetDefaultProgrammer(),
DefaultPortAddress: oldAddress,
DefaultPortProtocol: oldProtocol,
}
Expand All @@ -45,6 +46,12 @@ func SetSketchDefaults(ctx context.Context, req *rpc.SetSketchDefaultsRequest) (
}
res.DefaultFqbn = fqbn
}
if programmer := req.GetDefaultProgrammer(); programmer != "" {
if err := sk.SetDefaultProgrammer(programmer); err != nil {
return nil, &cmderrors.CantUpdateSketchError{Cause: err}
}
res.DefaultProgrammer = programmer
}
if newAddress, newProtocol := req.GetDefaultPortAddress(), req.GetDefaultPortProtocol(); newAddress != "" {
if err := sk.SetDefaultPort(newAddress, newProtocol); err != nil {
return nil, &cmderrors.CantUpdateSketchError{Cause: err}
Expand Down
10 changes: 9 additions & 1 deletion commands/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,22 @@ func Upload(ctx context.Context, req *rpc.UploadRequest, outStream io.Writer, er
fqbn = pme.GetProfile().FQBN
}

programmer := req.GetProgrammer()
if programmer == "" && pme.GetProfile() != nil {
programmer = pme.GetProfile().Programmer
}
if programmer == "" {
programmer = sk.GetDefaultProgrammer()
}

updatedPort, err := runProgramAction(
pme,
sk,
req.GetImportFile(),
req.GetImportDir(),
fqbn,
req.GetPort(),
req.GetProgrammer(),
programmer,
req.GetVerbose(),
req.GetVerify(),
false, // burnBootloader
Expand Down
14 changes: 10 additions & 4 deletions docs/sketch-project-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ multiple profiles.
Each profile will define:

- The board FQBN
- The programmer to use
- The target core platform name and version (with the 3rd party platform index URL if needed)
- A possible core platform name and version, that is a dependency of the target core platform (with the 3rd party
platform index URL if needed)
Expand All @@ -22,6 +23,7 @@ profiles:
<PROFILE_NAME>:
notes: <USER_NOTES>
fqbn: <FQBN>
programmer: <PROGRAMMER>
platforms:
- platform: <PLATFORM> (<PLATFORM_VERSION>)
platform_index_url: <3RD_PARTY_PLATFORM_URL>
Expand Down Expand Up @@ -50,6 +52,7 @@ otherwise below). The available fields are:
- `libraries:` is a section where the required libraries to build the project are defined. This section is optional.
- `<LIB_VERSION>` is the version required for the library, for example, `1.0.0`.
- `<USER_NOTES>` is a free text string available to the developer to add comments. This field is optional.
- `<PROGRAMMER>` is the programmer that will be used. This field is optional.

A complete example of a sketch project file may be the following:

Expand Down Expand Up @@ -134,19 +137,22 @@ The sketch project file may be used to set the default value for some command li
particular:

- The `default_fqbn` key sets the default value for the `--fqbn` flag
- The `default_programmer` key sets the default value for the `--programmer` flag
- The `default_port` key sets the default value for the `--port` flag
- The `default_protocol` key sets the default value for the `--protocol` flag
- The `default_profile` key sets the default value for the `--profile` flag

For example:

```
default_fqbn: arduino:avr:uno
default_fqbn: arduino:samd:mkr1000
default_programmer: atmel_ice
default_port: /dev/ttyACM0
default_protocol: serial
default_profile: myprofile
```

With this configuration set, it is not necessary to specify the `--fqbn`, `--port`, `--protocol` or `--profile` flags to
the [`arduino-cli compile`](commands/arduino-cli_compile.md) or [`arduino-cli upload`](commands/arduino-cli_upload.md)
commands when compiling or uploading the sketch.
With this configuration set, it is not necessary to specify the `--fqbn`, `--programmer`, `--port`, `--protocol` or
`--profile` flags to the [`arduino-cli compile`](commands/arduino-cli_compile.md),
[`arduino-cli upload`](commands/arduino-cli_upload.md) or [`arduino-cli debug`](commands/arduino-cli_debug.md) commands
when compiling, uploading or debugging the sketch.
55 changes: 33 additions & 22 deletions internal/arduino/sketch/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@ import (

// projectRaw is a support struct used only to unmarshal the yaml
type projectRaw struct {
ProfilesRaw yaml.Node `yaml:"profiles"`
DefaultProfile string `yaml:"default_profile"`
DefaultFqbn string `yaml:"default_fqbn"`
DefaultPort string `yaml:"default_port,omitempty"`
DefaultProtocol string `yaml:"default_protocol,omitempty"`
ProfilesRaw yaml.Node `yaml:"profiles"`
DefaultProfile string `yaml:"default_profile"`
DefaultFqbn string `yaml:"default_fqbn"`
DefaultPort string `yaml:"default_port,omitempty"`
DefaultProtocol string `yaml:"default_protocol,omitempty"`
DefaultProgrammer string `yaml:"default_programmer,omitempty"`
}

// Project represents the sketch project file
type Project struct {
Profiles []*Profile
DefaultProfile string
DefaultFqbn string
DefaultPort string
DefaultProtocol string
Profiles []*Profile
DefaultProfile string
DefaultFqbn string
DefaultPort string
DefaultProtocol string
DefaultProgrammer string
}

// AsYaml outputs the sketch project file as YAML
Expand All @@ -69,6 +71,9 @@ func (p *Project) AsYaml() string {
if p.DefaultProtocol != "" {
res += fmt.Sprintf("default_protocol: %s\n", p.DefaultProtocol)
}
if p.DefaultProgrammer != "" {
res += fmt.Sprintf("default_programmer: %s\n", p.DefaultProgrammer)
}
return res
}

Expand All @@ -93,18 +98,20 @@ func (p *projectRaw) getProfiles() ([]*Profile, error) {
// Profile is a sketch profile, it contains a reference to all the resources
// needed to build and upload a sketch
type Profile struct {
Name string
Notes string `yaml:"notes"`
FQBN string `yaml:"fqbn"`
Platforms ProfileRequiredPlatforms `yaml:"platforms"`
Libraries ProfileRequiredLibraries `yaml:"libraries"`
Name string
Notes string `yaml:"notes"`
FQBN string `yaml:"fqbn"`
Programmer string `yaml:"programmer"`
Platforms ProfileRequiredPlatforms `yaml:"platforms"`
Libraries ProfileRequiredLibraries `yaml:"libraries"`
}

// ToRpc converts this Profile to an rpc.SketchProfile
func (p *Profile) ToRpc() *rpc.SketchProfile {
return &rpc.SketchProfile{
Name: p.Name,
Fqbn: p.FQBN,
Name: p.Name,
Fqbn: p.FQBN,
Programmer: p.Programmer,
}
}

Expand All @@ -115,6 +122,9 @@ func (p *Profile) AsYaml() string {
res += fmt.Sprintf(" notes: %s\n", p.Notes)
}
res += fmt.Sprintf(" fqbn: %s\n", p.FQBN)
if p.Programmer != "" {
res += fmt.Sprintf(" programmer: %s\n", p.Programmer)
}
res += p.Platforms.AsYaml()
res += p.Libraries.AsYaml()
return res
Expand Down Expand Up @@ -275,10 +285,11 @@ func LoadProjectFile(file *paths.Path) (*Project, error) {
return nil, err
}
return &Project{
Profiles: profiles,
DefaultProfile: raw.DefaultProfile,
DefaultFqbn: raw.DefaultFqbn,
DefaultPort: raw.DefaultPort,
DefaultProtocol: raw.DefaultProtocol,
Profiles: profiles,
DefaultProfile: raw.DefaultProfile,
DefaultFqbn: raw.DefaultFqbn,
DefaultPort: raw.DefaultPort,
DefaultProtocol: raw.DefaultProtocol,
DefaultProgrammer: raw.DefaultProgrammer,
}, nil
}
31 changes: 22 additions & 9 deletions internal/arduino/sketch/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ func (s *Sketch) GetDefaultPortAddressAndProtocol() (string, string) {
return s.Project.DefaultPort, s.Project.DefaultProtocol
}

// GetDefaultProgrammer return the default Programmer for the sketch (from the sketch.yaml project file),
// ore the empty string if not set.
func (s *Sketch) GetDefaultProgrammer() string {
return s.Project.DefaultProgrammer
}

// SetDefaultFQBN sets the default FQBN for the sketch and saves it in the sketch.yaml project file.
func (s *Sketch) SetDefaultFQBN(fqbn string) error {
s.Project.DefaultFqbn = fqbn
Expand All @@ -263,6 +269,12 @@ func (s *Sketch) SetDefaultPort(address, protocol string) error {
return updateOrAddYamlRootEntry(s.GetProjectPath(), "default_protocol", protocol)
}

// SetDefaultFQBN sets the default programmer for the sketch and saves it in the sketch.yaml project file.
func (s *Sketch) SetDefaultProgrammer(programmer string) error {
s.Project.DefaultProgrammer = programmer
return updateOrAddYamlRootEntry(s.GetProjectPath(), "default_programmer", programmer)
}

// InvalidSketchFolderNameError is returned when the sketch directory doesn't match the sketch name
type InvalidSketchFolderNameError struct {
SketchFolder *paths.Path
Expand Down Expand Up @@ -290,15 +302,16 @@ func (s *Sketch) Hash() string {
func (s *Sketch) ToRpc() *rpc.Sketch {
defaultPort, defaultProtocol := s.GetDefaultPortAddressAndProtocol()
res := &rpc.Sketch{
MainFile: s.MainFile.String(),
LocationPath: s.FullPath.String(),
OtherSketchFiles: s.OtherSketchFiles.AsStrings(),
AdditionalFiles: s.AdditionalFiles.AsStrings(),
RootFolderFiles: s.RootFolderFiles.AsStrings(),
DefaultFqbn: s.GetDefaultFQBN(),
DefaultPort: defaultPort,
DefaultProtocol: defaultProtocol,
Profiles: f.Map(s.Project.Profiles, (*Profile).ToRpc),
MainFile: s.MainFile.String(),
LocationPath: s.FullPath.String(),
OtherSketchFiles: s.OtherSketchFiles.AsStrings(),
AdditionalFiles: s.AdditionalFiles.AsStrings(),
RootFolderFiles: s.RootFolderFiles.AsStrings(),
DefaultFqbn: s.GetDefaultFQBN(),
DefaultPort: defaultPort,
DefaultProtocol: defaultProtocol,
DefaultProgrammer: s.GetDefaultProgrammer(),
Profiles: f.Map(s.Project.Profiles, (*Profile).ToRpc),
}
if defaultProfile, err := s.GetProfile(s.Project.DefaultProfile); err == nil {
res.DefaultProfile = defaultProfile.ToRpc()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
profiles:
nanorp:
fqbn: arduino:mbed_nano:nanorp2040connect
programmer: p1
platforms:
- platform: arduino:mbed_nano (2.1.0)
libraries:
Expand Down
5 changes: 5 additions & 0 deletions internal/cli/arguments/programmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ func (p *Programmer) String(inst *commands.Instance, fqbn string) string {
}
return details.GetDefaultProgrammerId()
}

// GetProgrammer returns the programmer specified by the user
func (p *Programmer) GetProgrammer() string {
return p.programmer
}
27 changes: 17 additions & 10 deletions internal/cli/board/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,39 @@ import (
func initAttachCommand() *cobra.Command {
var port arguments.Port
var fqbn arguments.Fqbn
var programmer arguments.Programmer
attachCommand := &cobra.Command{
Use: fmt.Sprintf("attach [-p <%s>] [-b <%s>] [%s]", tr("port"), tr("FQBN"), tr("sketchPath")),
Use: fmt.Sprintf("attach [-p <%s>] [-b <%s>] [-P <%s>] [%s]", tr("port"), tr("FQBN"), tr("programmer"), tr("sketchPath")),
Short: tr("Attaches a sketch to a board."),
Long: tr("Sets the default values for port and FQBN. If no port or FQBN are specified, the current default port and FQBN are displayed."),
Long: tr("Sets the default values for port and FQBN. If no port, FQBN or programmer are specified, the current default port, FQBN and programmer are displayed."),
Example: " " + os.Args[0] + " board attach -p /dev/ttyACM0\n" +
" " + os.Args[0] + " board attach -p /dev/ttyACM0 HelloWorld\n" +
" " + os.Args[0] + " board attach -b arduino:samd:mkr1000",
" " + os.Args[0] + " board attach -b arduino:samd:mkr1000" +
" " + os.Args[0] + " board attach -P atmel_ice",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
sketchPath := ""
if len(args) > 0 {
sketchPath = args[0]
}
runAttachCommand(sketchPath, &port, fqbn.String())
runAttachCommand(sketchPath, &port, fqbn.String(), &programmer)
},
}
fqbn.AddToCommand(attachCommand)
port.AddToCommand(attachCommand)
programmer.AddToCommand(attachCommand)

return attachCommand
}

func runAttachCommand(path string, port *arguments.Port, fqbn string) {
func runAttachCommand(path string, port *arguments.Port, fqbn string, programmer *arguments.Programmer) {
sketchPath := arguments.InitSketchPath(path)

portAddress, portProtocol, _ := port.GetPortAddressAndProtocol(nil, "", "")
newDefaults, err := sketch.SetSketchDefaults(context.Background(), &rpc.SetSketchDefaultsRequest{
SketchPath: sketchPath.String(),
DefaultFqbn: fqbn,
DefaultProgrammer: programmer.GetProgrammer(),
DefaultPortAddress: portAddress,
DefaultPortProtocol: portProtocol,
})
Expand All @@ -67,7 +71,8 @@ func runAttachCommand(path string, port *arguments.Port, fqbn string) {
}

res := &boardAttachResult{
Fqbn: newDefaults.GetDefaultFqbn(),
Fqbn: newDefaults.GetDefaultFqbn(),
Programmer: newDefaults.GetDefaultProgrammer(),
}
if newDefaults.GetDefaultPortAddress() != "" {
res.Port = &boardAttachPortResult{
Expand All @@ -92,19 +97,21 @@ func (b *boardAttachPortResult) String() string {
}

type boardAttachResult struct {
Fqbn string `json:"fqbn,omitempty"`
Port *boardAttachPortResult `json:"port,omitempty"`
Fqbn string `json:"fqbn,omitempty"`
Programmer string `json:"programmer,omitempty"`
Port *boardAttachPortResult `json:"port,omitempty"`
}

func (b *boardAttachResult) Data() interface{} {
return b
}

func (b *boardAttachResult) String() string {
if b.Port == nil && b.Fqbn == "" {
return tr("No default port or FQBN set")
if b.Port == nil && b.Fqbn == "" && b.Programmer == "" {
return tr("No default port, FQBN or programmer set")
}
res := fmt.Sprintf("%s: %s\n", tr("Default port set to"), b.Port)
res += fmt.Sprintf("%s: %s\n", tr("Default FQBN set to"), b.Fqbn)
res += fmt.Sprintf("%s: %s\n", tr("Default programmer set to"), b.Programmer)
return res
}
10 changes: 9 additions & 1 deletion internal/cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,14 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
}
}

prog := profile.GetProgrammer()
if prog == "" || programmer.GetProgrammer() != "" {
prog = programmer.String(inst, fqbn)
}
if prog == "" {
prog = sk.GetDefaultProgrammer()
}

uploadRequest := &rpc.UploadRequest{
Instance: inst,
Fqbn: fqbn,
Expand All @@ -276,7 +284,7 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
Verbose: verbose,
Verify: verify,
ImportDir: buildPath,
Programmer: programmer.String(inst, fqbn),
Programmer: prog,
UserFields: fields,
}

Expand Down
Loading
Loading