mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-25 17:09:01 +00:00
espat: change Response() method to use a passed-in timeout value instead of fixed pauses.
Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
+13
-10
@@ -50,7 +50,7 @@ func (d *Device) Connected() bool {
|
||||
d.Execute(Test)
|
||||
|
||||
// handle response here, should include "OK"
|
||||
r := d.Response()
|
||||
r := d.Response(100)
|
||||
if strings.Contains(string(r), "OK") {
|
||||
return true
|
||||
}
|
||||
@@ -93,7 +93,7 @@ func (d Device) Set(cmd, params string) error {
|
||||
// Version returns the ESP8266/ESP32 firmware version info.
|
||||
func (d Device) Version() []byte {
|
||||
d.Execute(Version)
|
||||
return d.Response()
|
||||
return d.Response(100)
|
||||
}
|
||||
|
||||
// Echo sets the ESP8266/ESP32 echo setting.
|
||||
@@ -104,7 +104,7 @@ func (d Device) Echo(set bool) {
|
||||
d.Execute(EchoConfigOff)
|
||||
}
|
||||
// TODO: check for success
|
||||
d.Response()
|
||||
d.Response(100)
|
||||
}
|
||||
|
||||
// Reset restarts the ESP8266/ESP32 firmware. Due to how the baud rate changes,
|
||||
@@ -112,13 +112,13 @@ func (d Device) Echo(set bool) {
|
||||
// what you are doing when you call this.
|
||||
func (d Device) Reset() {
|
||||
d.Execute(Restart)
|
||||
d.Response()
|
||||
d.Response(100)
|
||||
}
|
||||
|
||||
// ReadSocket returns the data that has already been read in from the responses.
|
||||
func (d *Device) ReadSocket(b []byte) (n int, err error) {
|
||||
// make sure no data in buffer
|
||||
d.Response()
|
||||
d.Response(100)
|
||||
|
||||
count := len(b)
|
||||
if len(b) >= len(d.socketdata) {
|
||||
@@ -137,8 +137,11 @@ func (d *Device) ReadSocket(b []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
// Response gets the next response bytes from the ESP8266/ESP32.
|
||||
func (d *Device) Response() []byte {
|
||||
var i, retries int
|
||||
// The call will retry for up to timeout milliseconds before returning nothing.
|
||||
func (d *Device) Response(timeout int) []byte {
|
||||
var i int
|
||||
pause := 10 // pause to wait for 10 ms
|
||||
retries := timeout / pause
|
||||
|
||||
header := make([]byte, 2)
|
||||
for {
|
||||
@@ -174,13 +177,13 @@ func (d *Device) Response() []byte {
|
||||
i++
|
||||
}
|
||||
}
|
||||
retries++
|
||||
if retries > 2 {
|
||||
retries--
|
||||
if retries == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
// pause to make sure is no more data to be read
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
time.Sleep(time.Duration(pause) * time.Millisecond)
|
||||
}
|
||||
return d.response[:i]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user