-
Notifications
You must be signed in to change notification settings - Fork 1
/
sys.go
174 lines (146 loc) · 4.7 KB
/
sys.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// The MIT License (MIT)
//
// Copyright © 2017 Sven Agneessens <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package rn2483
import (
"errors"
"fmt"
"strconv"
)
// Sleep puts the RN2483 chip to sleep for the specified number of milliseconds.
func Sleep(length uint32) bool {
if length < 100 {
WARN.Println("sys sleep called with length lower than 100:", length)
return false
}
err := serialWrite(fmt.Sprintf("sys sleep %v", length))
if err != nil {
WARN.Println("sys sleep error:", err)
return false
}
n, answer := serialRead()
if n == 0 || string(sanitize(answer)) == invalidParameter {
WARN.Println("sys sleep error: invalid parameter")
return false
}
return true
}
// Reset will reset and restart the RN2483 module.
func Reset() bool {
err := serialWrite("sys reset")
if err != nil {
WARN.Println("reset error:", err)
return false
}
serialFlush()
return true
}
// SaveByte allows the user to modify the EEPROM at the specified address
// with the specified data (one byte).
func SaveByte(address uint16, data uint8) bool {
if address < 768 || address > 1023 {
WARN.Println("sys set nvm error: address out of range [768-1023]")
return false
}
err := serialWrite(fmt.Sprintf("sys set nvm %X %X", address, data))
if err != nil {
WARN.Println("sys set nvm error:", err)
return false
}
n, answer := serialRead()
if n == 0 || string(sanitize(answer)) == invalidParameter {
WARN.Println("sys set nvm error: invalid parameter")
return false
}
return true
}
// ReadByte returns the data stored in the EEPROM at the specified address.
func ReadByte(address uint16) (byte, error) {
if address < 768 || address > 1023 {
WARN.Println("sys get nvm error: address out of range [768-1023]")
return 0, errors.New("address out of range [768-1023]")
}
err := serialWrite(fmt.Sprintf("sys get nvm %X", address))
if err != nil {
WARN.Println("sys get nvm error:", err)
return 0, err
}
n, answer := serialRead()
if n == 0 || string(sanitize(answer)) == invalidParameter {
WARN.Println("sys get nvm error: invalid parameter")
return 0, errors.New("invalid parameter")
}
value, err := strconv.ParseUint(string(sanitize(answer)), 16, 8)
if err != nil {
WARN.Println("sys get nvm error:", err)
return 0, err
}
return byte(value), nil
}
// Version returns the information related to the hardware platform,
// firmware version, release date and time stamp on firmware creation.
func Version() string {
err := serialWrite("sys get ver")
if err != nil {
WARN.Println("sys get ver error:", err)
return ""
}
n, answer := serialRead()
if n == 0 {
WARN.Println("sys get ver error: no answer")
return ""
}
return string(sanitize(answer))
}
// Voltage will return the voltage measured on Vdd in millivolts
func Voltage() (uint16, error) {
err := serialWrite("sys get vdd")
if err != nil {
WARN.Println("sys get vdd error:", err)
return 0, err
}
n, answer := serialRead()
if n == 0 || string(sanitize(answer)) == invalidParameter {
WARN.Println("sys get vdd error: invalid parameter")
return 0, errors.New("invalid parameter")
}
value, err := strconv.ParseUint(string(sanitize(answer)), 10, 16)
if err != nil {
WARN.Println("sys get vdd error:", err)
return 0, err
}
return uint16(value), nil
}
// HardwareID will return the HWEUI of the RN2483 module as a string.
// The HWEUI is actually an 8 bit hex string.
func HardwareID() string {
err := serialWrite("sys get hweui")
if err != nil {
WARN.Println("sys get hweui error:", err)
return ""
}
n, answer := serialRead()
if n == 0 || string(sanitize(answer)) == invalidParameter {
WARN.Println("sys get hweui error: invalid parameter")
return ""
}
return string(sanitize(answer))
}