-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset.go
More file actions
66 lines (51 loc) · 1.29 KB
/
reset.go
File metadata and controls
66 lines (51 loc) · 1.29 KB
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
// Copyright 2022 TrueLevel SA
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0
package nd300
import (
"errors"
"fmt"
"time"
"go.bug.st/serial"
)
const (
resetDelay = 500 * time.Millisecond // Based on manual testing.
resetChecks = 8
)
var ErrResetFailed = errors.New("machine reset failed")
func (c *Conn) ResetMachine() (err error) {
var port serial.Port
port, err = c.Open()
if err != nil {
return
}
defer func() {
err = AppendErr(err, port.Close(), closeErrMsg)
c.port = nil
}()
if c.txBuff[idxCmd] != byte(ResetDispenser) || c.txBuff[idxData] != 0x0 {
c.txBuff[idxCmd] = byte(ResetDispenser)
c.txBuff[idxData] = 0x0
c.txBuff[idxCksum] = computeChecksum(c.txBuff)
}
if err = c.write(); err != nil {
return
}
checks := 0
// Whilst reseting the machine must be adjusting the sensor and returns SensorAdjusting.
for status := SensorAdjusting; status == SensorAdjusting; checks++ {
time.Sleep(resetDelay)
status, _, err = requestStatus(c)
if err != nil {
return
}
if checks >= resetChecks {
return fmt.Errorf("%w: %s", ErrResetFailed, status)
}
}
return nil
}