Skip to content

Commit de16c10

Browse files
matteosuppofacchinm
authored andcommitted
Docs
1 parent b64bc92 commit de16c10

File tree

5 files changed

+201
-18
lines changed

5 files changed

+201
-18
lines changed

programmer/README.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
use 'godoc cmd/github.com/arduino/arduino-create-agent/programmer' for documentation on the github.com/arduino/arduino-create-agent/programmer command
2+
3+
Package programmer
4+
=====================
5+
6+
import "github.com/arduino/arduino-create-agent/programmer"
7+
8+
Package programmer allows to upload sketches into a board connected to the
9+
computer It can do it via serial port or via network
10+
11+
**Usage for a serial upload**
12+
13+
Make sure that you have a compiled sketch somewhere on your disk
14+
15+
```go
16+
commandline = ``"/usr/bin/avrdude" "-C/usr/bin/avrdude.conf" -v -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D "-Uflash:w:./sketch.hex:i"``
17+
err := programmer.Serial("/dev/ttyACM0", commandline, programmer.Extra{}, nil)
18+
```
19+
20+
note that the commandline contains the path of the sketch (sketch.hex)
21+
22+
**Usage for a network upload**
23+
24+
Make sure that you have a compiled sketch somewhere on your disk
25+
26+
```go
27+
err := programmer.Network("127.0.10.120", "arduino:avr:yun, "./sketch.hex", "", programmer.Auth{}, nil)
28+
```
29+
30+
The commandline can be empty or it can contain instructions (depends on the
31+
board)
32+
33+
**Resolving commandlines**
34+
35+
If you happen to have an unresolved commandline (full of {} parameters) you can
36+
resolve it
37+
38+
```go
39+
t := tools.Tools{}
40+
commandline = programmer.Resolve("/dev/ttyACM0", "arduino:avr:leonardo", "./sketch.hex", commandline, programmer.Extra{}, t)
41+
```
42+
43+
t must implement the locater interface (the Tools package does!)
44+
45+
**Logging** If you're interested in the output of the commands, you can
46+
implement the logger interface. Here's an example:
47+
48+
```go
49+
logger := logrus.New()
50+
logger.Level = logrus.DebugLevel
51+
programmer.Serial("/dev/ttyACM0", commandline, programmer.Extra{}, logger)
52+
```
53+
54+
55+
56+
Variables
57+
---------
58+
59+
60+
```go
61+
var Busy = false
62+
```
63+
Busy tells wether the programmer is doing something
64+
65+
Functions
66+
---------
67+
68+
69+
```go
70+
func Kill()
71+
```
72+
73+
Kill stops any upload process as soon as possible
74+
75+
76+
```go
77+
func Network(port, board, file, commandline string, auth Auth, l Logger) error
78+
```
79+
80+
Network performs a network upload
81+
82+
83+
```go
84+
func Resolve(port, board, file, commandline string, extra Extra, t Locater) (string, error)
85+
```
86+
87+
Resolve replaces some symbols in the commandline with the appropriate values it
88+
can return an error when looking a variable in the Locater
89+
90+
91+
```go
92+
func Serial(port, commandline string, extra Extra, l Logger) error
93+
```
94+
95+
Serial performs a serial upload
96+
97+
Types
98+
-----
99+
100+
101+
```go
102+
type Auth struct {
103+
Username string `json:"username"`
104+
Password string `json:"password"`
105+
}
106+
```
107+
Auth contains username and password used for a network upload
108+
109+
110+
```go
111+
type Extra struct {
112+
Use1200bpsTouch bool `json:"use_1200bps_touch"`
113+
WaitForUploadPort bool `json:"wait_for_upload_port"`
114+
Network bool `json:"network"`
115+
Auth Auth `json:"auth"`
116+
Verbose bool `json:"verbose"`
117+
ParamsVerbose string `json:"params_verbose"`
118+
ParamsQuiet string `json:"params_quiet"`
119+
}
120+
```
121+
Extra contains some options used during the upload
122+
123+
124+
```go
125+
type Locater interface {
126+
GetLocation(command string) (string, error)
127+
}
128+
```
129+
Locater can return the location of a tool in the system
130+
131+
132+
```go
133+
type Logger interface {
134+
Debug(args ...interface{})
135+
Info(args ...interface{})
136+
}
137+
```
138+
Logger is an interface implemented by most loggers (like logrus)
139+
140+

programmer/doc.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Package programmer allows to upload sketches into a board connected to the computer
2+
// It can do it via serial port or via network
3+
//
4+
// **Usage for a serial upload**
5+
//
6+
// Make sure that you have a compiled sketch somewhere on your disk
7+
//
8+
// ```go
9+
// commandline = ``"/usr/bin/avrdude" "-C/usr/bin/avrdude.conf" -v -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D "-Uflash:w:./sketch.hex:i"``
10+
// err := programmer.Serial("/dev/ttyACM0", commandline, programmer.Extra{}, nil)
11+
// ```
12+
//
13+
// note that the commandline contains the path of the sketch (sketch.hex)
14+
//
15+
// **Usage for a network upload**
16+
//
17+
// Make sure that you have a compiled sketch somewhere on your disk
18+
//
19+
// ```go
20+
// err := programmer.Network("127.0.10.120", "arduino:avr:yun, "./sketch.hex", "", programmer.Auth{}, nil)
21+
// ```
22+
//
23+
// The commandline can be empty or it can contain instructions (depends on the board)
24+
//
25+
// **Resolving commandlines**
26+
//
27+
// If you happen to have an unresolved commandline (full of {} parameters) you can resolve it
28+
//
29+
// ```go
30+
// t := tools.Tools{}
31+
// commandline = programmer.Resolve("/dev/ttyACM0", "arduino:avr:leonardo", "./sketch.hex", commandline, programmer.Extra{}, t)
32+
// ```
33+
//
34+
// t must implement the locater interface (the Tools package does!)
35+
//
36+
// **Logging**
37+
// If you're interested in the output of the commands, you can implement the logger interface. Here's an example:
38+
//
39+
// ```go
40+
// logger := logrus.New()
41+
// logger.Level = logrus.DebugLevel
42+
// programmer.Serial("/dev/ttyACM0", commandline, programmer.Extra{}, logger)
43+
// ```
44+
package programmer

programmer/programmer.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ type Extra struct {
4444
}
4545

4646
// Resolve replaces some symbols in the commandline with the appropriate values
47-
// it can return an error when looking a variable in the locater
48-
func Resolve(port, board, file, commandline string, extra Extra, t locater) (string, error) {
47+
// it can return an error when looking a variable in the Locater
48+
func Resolve(port, board, file, commandline string, extra Extra, t Locater) (string, error) {
4949
commandline = strings.Replace(commandline, "{build.path}", filepath.ToSlash(filepath.Dir(file)), -1)
5050
commandline = strings.Replace(commandline, "{build.project_name}", strings.TrimSuffix(filepath.Base(file), filepath.Ext(filepath.Base(file))), -1)
5151
commandline = strings.Replace(commandline, "{serial.port}", port, -1)
@@ -57,7 +57,7 @@ func Resolve(port, board, file, commandline string, extra Extra, t locater) (str
5757
commandline = strings.Replace(commandline, "{upload.verbose}", extra.ParamsQuiet, -1)
5858
}
5959

60-
// search for runtime variables and replace with values from locater
60+
// search for runtime variables and replace with values from Locater
6161
var runtimeRe = regexp.MustCompile("\\{(.*?)\\}")
6262
runtimeVars := runtimeRe.FindAllString(commandline, -1)
6363

@@ -74,7 +74,7 @@ func Resolve(port, board, file, commandline string, extra Extra, t locater) (str
7474
}
7575

7676
// Network performs a network upload
77-
func Network(port, board, file, commandline string, auth Auth, l logger) error {
77+
func Network(port, board, file, commandline string, auth Auth, l Logger) error {
7878
Busy = true
7979

8080
// Defaults
@@ -97,7 +97,7 @@ func Network(port, board, file, commandline string, auth Auth, l logger) error {
9797
}
9898

9999
// Serial performs a serial upload
100-
func Serial(port, commandline string, extra Extra, l logger) error {
100+
func Serial(port, commandline string, extra Extra, l Logger) error {
101101
Busy = true
102102
defer func() { Busy = false }()
103103

@@ -128,7 +128,7 @@ func Kill() {
128128

129129
// reset opens the port at 1200bps. It returns the new port name (which could change
130130
// sometimes) and an error (usually because the port listing failed)
131-
func reset(port string, wait bool, l logger) (string, error) {
131+
func reset(port string, wait bool, l Logger) (string, error) {
132132
info(l, "Restarting in bootloader mode")
133133

134134
// Get port list before reset
@@ -169,7 +169,7 @@ func reset(port string, wait bool, l logger) (string, error) {
169169
// waitReset is meant to be called just after a reset. It watches the ports connected
170170
// to the machine until a port disappears and reappears. The port name could be different
171171
// so it returns the name of the new port.
172-
func waitReset(beforeReset []string, l logger) string {
172+
func waitReset(beforeReset []string, l Logger) string {
173173
var port string
174174
timeout := false
175175

@@ -220,8 +220,8 @@ func waitReset(beforeReset []string, l logger) string {
220220
var cmd *exec.Cmd
221221

222222
// program spawns the given binary with the given args, logging the sdtout and stderr
223-
// through the logger
224-
func program(binary string, args []string, l logger) error {
223+
// through the Logger
224+
func program(binary string, args []string, l Logger) error {
225225
defer func() { cmd = nil }()
226226

227227
// remove quotes form binary command and args
@@ -280,7 +280,7 @@ func program(binary string, args []string, l logger) error {
280280
return nil
281281
}
282282

283-
func form(port, board, file string, auth Auth, l logger) error {
283+
func form(port, board, file string, auth Auth, l Logger) error {
284284
// Prepare a form that you will submit to that URL.
285285
_url := "http://" + port + "/data/upload_sketch_silent"
286286
var b bytes.Buffer
@@ -342,7 +342,7 @@ func form(port, board, file string, auth Auth, l logger) error {
342342
return nil
343343
}
344344

345-
func ssh(port, file, commandline string, auth Auth, l logger) error {
345+
func ssh(port, file, commandline string, auth Auth, l Logger) error {
346346
// Connect via ssh
347347
client, err := simplessh.ConnectWithPassword(port+":22", auth.Username, auth.Password)
348348
debug(l, "Connect via ssh ", client, err)

programmer/programmer_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ func TestSerial(t *testing.T) {
3939
err := programmer.Do(test.Port, commandline, test.Extra, logger)
4040
log.Println(err)
4141
}
42-
t.Fail()
4342
}
4443

4544
var TestNetworkData = []struct {
@@ -64,7 +63,6 @@ func TestNetwork(t *testing.T) {
6463
err := programmer.Do(test.Port, commandline, test.Extra, logger)
6564
log.Println(err)
6665
}
67-
t.Fail()
6866
}
6967

7068
var TestResolveData = []struct {

programmer/utils.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
package programmer
22

3-
type logger interface {
3+
// Logger is an interface implemented by most loggers (like logrus)
4+
type Logger interface {
45
Debug(args ...interface{})
56
Info(args ...interface{})
67
}
78

8-
func debug(l logger, args ...interface{}) {
9+
func debug(l Logger, args ...interface{}) {
910
if l != nil {
1011
l.Debug(args...)
1112
}
1213
}
1314

14-
func info(l logger, args ...interface{}) {
15+
func info(l Logger, args ...interface{}) {
1516
if l != nil {
1617
l.Info(args...)
1718
}
1819
}
1920

20-
// locater can return the location of a tool in the system
21-
type locater interface {
21+
// Locater can return the location of a tool in the system
22+
type Locater interface {
2223
GetLocation(command string) (string, error)
2324
}
2425

0 commit comments

Comments
 (0)