-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsa818.go
132 lines (121 loc) · 3.49 KB
/
sa818.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* sa8118 golang library for controlling nicerf walkie talkie modules with go
* Copyright (C) 2018-2022, Suvir Kumar <suvir@talkkonnect.com>
*
* 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 http://mozilla.org/MPL/2.0/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* talkkonnect is the based on talkiepi and barnard by Daniel Chote and Tim Cooper
*
* The Initial Developer of the Original Code is
* Suvir Kumar <suvir@talkkonnect.com>
* Portions created by the Initial Developer are Copyright (C) Suvir Kumar. All Rights Reserved.
*
* Contributor(s):
*
* Suvir Kumar <suvir@talkkonnect.com>
*
* My Blog is at www.talkkonnect.com
* The source code is hosted at github.com/talkkonnect
*
* sa818.go -> sa818 walkie talkie module golang library
*/
package sa818
import (
"errors"
"fmt"
"io"
"log"
"regexp"
"strings"
"time"
"github.com/jacobsa/go-serial/serial"
)
type DMOSetupStruct struct {
Band int
Rxfreq float32
Txfreq float32
Ctsstone int
Squelch int
Dcstone int
Predeemph int
Highpass int
Lowpass int
Volume int
SerialOptions serial.OpenOptions
}
func Callsa818(sendCommand string, TDMOSetup DMOSetupStruct) error {
var ExpectedAnswer string
Port, err := serial.Open(TDMOSetup.SerialOptions)
if err != nil {
return fmt.Errorf("serial open failed with error %v", err)
}
defer Port.Close()
switch sendCommand {
case "InitComm":
serialWrite("AT+DMOCONNECT\r\n", Port)
ExpectedAnswer = "(DMOCONNECT:0)"
case "CheckVersion":
serialWrite("AT+VERSION\r\n", Port)
ExpectedAnswer = "(VERSION:)"
case "DMOSetupGroup":
serialWrite(fmt.Sprintf("AT+DMOSETGROUP=%d,%.4f,%.4f,%04d,%d,%04d", TDMOSetup.Band, TDMOSetup.Rxfreq, TDMOSetup.Txfreq, TDMOSetup.Ctsstone, TDMOSetup.Squelch, TDMOSetup.Dcstone)+"\r\n", Port)
ExpectedAnswer = "(DMOSETGROUP:0)"
case "DMOSetupFilter":
serialWrite(fmt.Sprintf("AT+SETFILTER=%d,%d,%d", TDMOSetup.Predeemph, TDMOSetup.Highpass, TDMOSetup.Lowpass)+"\r\n", Port)
ExpectedAnswer = "(DMOSETFILTER:0)"
case "SetVolume":
serialWrite(fmt.Sprintf("AT+DMOSETVOLUME=%d\r\n", TDMOSetup.Volume), Port)
ExpectedAnswer = "(DMOSETVOLUME:0)"
case "CheckRSSI":
serialWrite("RSSI?\r\n", Port)
ExpectedAnswer = "(RSSI)"
default:
ExpectedAnswer = ""
return errors.New("invalid command")
}
time.Sleep(1000 * time.Millisecond)
AnswerGot := serialRead(Port)
re := regexp.MustCompile(ExpectedAnswer)
matched := re.MatchString(AnswerGot)
if matched {
if sendCommand == "CheckVersion" || sendCommand == "CheckRSSI" {
return errors.New(AnswerGot)
}
return nil
} else {
return errors.New(AnswerGot)
}
}
func serialWrite(message string, port io.WriteCloser) {
b := []byte(message)
n, err := port.Write(b)
if err != nil {
log.Println("error: cannot write")
return
}
stripMessage := strings.TrimRight(message, "\r\n")
if len(stripMessage) > 0 {
log.Printf("debug: Wrote %v total of %v bytes.\n", stripMessage, n)
}
}
func serialRead(port io.ReadCloser) string {
buf := make([]byte, 1048)
var n int = 0
var err error
for {
n, err = port.Read(buf)
if err != nil {
return "error reading serial"
} else {
defer port.Close()
return string(buf[:n])
}
}
}