-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.go
67 lines (57 loc) · 1.42 KB
/
upload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package core
import (
"time"
"github.com/robgonnella/ardi/v2/rpc"
log "github.com/sirupsen/logrus"
)
// UploadCore represents core module for ardi upload commands
type UploadCore struct {
logger *log.Logger
client rpc.Client
uploading bool
}
// NewUploadCore returns new ardi upload core
func NewUploadCore(client rpc.Client, logger *log.Logger) *UploadCore {
return &UploadCore{
client: client,
logger: logger,
uploading: false,
}
}
// Upload compiled sketches to the specified board
func (u *UploadCore) Upload(board *rpc.Board, buildDir string) error {
fqbn := board.FQBN
device := board.Port
u.waitForUploadsToFinish()
u.uploading = true
if err := u.client.Upload(fqbn, buildDir, device); err != nil {
u.logger.WithError(err).Error("Failed to upload sketch")
u.uploading = false
return err
}
u.uploading = false
return nil
}
// Attach attaches to the associated board port and prints logs
func (u *UploadCore) Attach(device string, baud int, port SerialPort) {
if port == nil {
port = NewArdiSerialPort(device, baud, u.logger)
} else {
port.Stop()
}
port.Watch()
}
// IsUploading returns whether or not core is currently uploading
func (u *UploadCore) IsUploading() bool {
return u.uploading
}
// private
func (u *UploadCore) waitForUploadsToFinish() {
for {
if !u.IsUploading() {
break
}
u.logger.Info("Waiting for previous upload to finish...")
time.Sleep(time.Second)
}
}