Cleaner solution from @ssokol.

pull/42/head
Christopher Young 2015-09-14 01:17:18 -04:00
rodzic 7a98518daf
commit 2b7b51e14a
1 zmienionych plików z 9 dodań i 14 usunięć

Wyświetl plik

@ -8,6 +8,7 @@ import (
"sync" "sync"
"time" "time"
"bufio"
"github.com/kidoman/embd" "github.com/kidoman/embd"
_ "github.com/kidoman/embd/host/all" _ "github.com/kidoman/embd/host/all"
"github.com/kidoman/embd/sensor/bmp180" "github.com/kidoman/embd/sensor/bmp180"
@ -185,22 +186,16 @@ func processNMEALine(l string) bool {
func gpsSerialReader() { func gpsSerialReader() {
defer serialPort.Close() defer serialPort.Close()
tmpBuf := string("")
for globalSettings.GPS_Enabled && globalStatus.GPS_connected { for globalSettings.GPS_Enabled && globalStatus.GPS_connected {
buf := make([]byte, 1024)
n, err := serialPort.Read(buf) scanner := bufio.NewScanner(serialPort)
if err != nil { for scanner.Scan() {
log.Printf("gps serial read error: %s\n", err.Error()) s := scanner.Text()
globalStatus.GPS_connected = false // log.Printf("Output: %s\n", s)
break processNMEALine(s)
} }
s := string(buf[:n]) if err := scanner.Err(); err != nil {
tmpBuf = tmpBuf + s fmt.Fprintln(os.Stderr, "reading standard input:", err)
for strings.Index(tmpBuf, "\n") != -1 {
idx := strings.Index(tmpBuf, "\n")
thisNMEA := tmpBuf[:idx]
tmpBuf = tmpBuf[idx+1:]
processNMEALine(thisNMEA)
} }
} }
globalStatus.GPS_connected = false globalStatus.GPS_connected = false