forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_port.go
77 lines (63 loc) · 1.71 KB
/
serial_port.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
package ble
import "sync"
// SerialPort is a implementation of serial over Bluetooth LE
// Inspired by https://github.com/monteslu/ble-serial by @monteslu
type SerialPort struct {
address string
rid string
tid string
client *ClientAdaptor
// buffer of responseData and mutex to protect it
responseData []byte
responseMutex sync.Mutex
}
// NewSerialPort returns a new serial over Bluetooth LE connection
func NewSerialPort(address string, rid string, tid string) *SerialPort {
return &SerialPort{address: address, rid: rid, tid: tid}
}
// Open opens a connection to a BLE serial device
func (p *SerialPort) Open() (err error) {
p.client = NewClientAdaptor(p.address)
err = p.client.Connect()
// subscribe to response notifications
p.client.Subscribe(p.rid, func(data []byte, e error) {
p.responseMutex.Lock()
p.responseData = append(p.responseData, data...)
p.responseMutex.Unlock()
})
return
}
// Read reads bytes from BLE serial port connection
func (p *SerialPort) Read(b []byte) (n int, err error) {
if len(p.responseData) == 0 {
return
}
p.responseMutex.Lock()
n = len(b)
if len(p.responseData) < n {
n = len(p.responseData)
}
copy(b, p.responseData[:n])
if len(p.responseData) > n {
p.responseData = p.responseData[n:]
} else {
p.responseData = nil
}
p.responseMutex.Unlock()
return
}
// Write writes to the BLE serial port connection
func (p *SerialPort) Write(b []byte) (n int, err error) {
err = p.client.WriteCharacteristic(p.tid, b)
n = len(b)
return
}
// Close closes the BLE serial port connection
func (p *SerialPort) Close() (err error) {
p.client.Disconnect()
return
}
// Address returns the BLE address
func (p *SerialPort) Address() string {
return p.address
}