/
konashi.go
95 lines (78 loc) · 2.09 KB
/
konashi.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
package gonashi
import (
"log"
"sync"
"time"
"github.com/flemay/gatt"
)
type Konashi struct {
Peripheral gatt.Peripheral
Advertisement *gatt.Advertisement
Rssi int
T time.Time
Connected chan struct{}
Disconnected chan struct{}
mu sync.RWMutex
}
func NewKonashi(p gatt.Peripheral, a *gatt.Advertisement, rssi int) *Konashi {
return &Konashi{p, a, rssi, time.Now(),
make(chan struct{}, 1),
make(chan struct{}, 1),
sync.RWMutex{},
}
}
func (k *Konashi) Connect() {
k.Peripheral.Device().Connect(k.Peripheral)
}
func (k *Konashi) DisConnect() {
k.Peripheral.Device().CancelConnection(k.Peripheral)
}
func (k *Konashi) DiscoverCharacteristics() []*gatt.Characteristic {
s, err := k.Peripheral.DiscoverServices([]gatt.UUID{gatt.MustParseUUID("229bff0003fb40da98a7b0def65c2d4b")})
if err != nil || len(s) == 0 {
log.Println("Service Not Found")
}
cs, err := k.Peripheral.DiscoverCharacteristics(nil, s[0])
return cs
}
// func (k *Konashi) StoreCharacteristics() {
// cs := k.DiscoverCharacteristics()
// k.mu.Lock()
// defer k.mu.Unlock()
// for _, c := range cs {
// k.characteristics[strings.ToUpper(c.UUID().String())] = c
// }
// }
func (k *Konashi) SetPeripheral(p gatt.Peripheral) {
k.mu.Lock()
defer k.mu.Unlock()
k.Peripheral = p
}
func (k *Konashi) SetMTU(mtu uint16) error {
return k.Peripheral.SetMTU(mtu)
}
func (k *Konashi) Update(a *gatt.Advertisement, rssi int) {
k.mu.Lock()
defer k.mu.Unlock()
k.Advertisement = a
k.Rssi = rssi
k.T = time.Now()
}
func (k *Konashi) ReadCharacteristic(c *gatt.Characteristic) ([]byte, error) {
return k.Peripheral.ReadCharacteristic(c)
}
func (k *Konashi) WriteCharacteristic(c *gatt.Characteristic, b []byte, noRsp bool) error {
return nil
}
func (k *Konashi) SetNotifyValue(c *gatt.Characteristic) error {
//f func(*gatt.Characteristic, []byte, error)
return nil
}
func (k *Konashi) PinMode(pin DioPin, mode PinIOMode) error {
var pioSetting uint8
pioSetting |= 0x01 << pin
b := make([]byte, 1)
b[0] = byte(pioSetting)
k.WriteCharacteristic(KonashiPioSetting, b, true)
return nil
}