forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_driver.go
149 lines (123 loc) · 3.88 KB
/
led_driver.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
package microbit
import (
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/ble"
)
// LEDDriver is the Gobot driver for the Microbit's LED array
type LEDDriver struct {
name string
connection gobot.Connection
gobot.Eventer
}
const (
// BLE services
ledService = "e95dd91d251d470aa062fa1922dfa9a8"
// BLE characteristics
ledMatrixStateCharacteristic = "e95d7b77251d470aa062fa1922dfa9a8"
ledTextCharacteristic = "e95d93ee251d470aa062fa1922dfa9a8"
ledScrollingDelayCharacteristic = "e95d0d2d251d470aa062fa1922dfa9a8"
)
// NewLEDDriver creates a Microbit LEDDriver
func NewLEDDriver(a ble.BLEConnector) *LEDDriver {
n := &LEDDriver{
name: gobot.DefaultName("Microbit LED"),
connection: a,
Eventer: gobot.NewEventer(),
}
return n
}
// Connection returns the BLE connection
func (b *LEDDriver) Connection() gobot.Connection { return b.connection }
// Name returns the Driver Name
func (b *LEDDriver) Name() string { return b.name }
// SetName sets the Driver Name
func (b *LEDDriver) SetName(n string) { b.name = n }
// adaptor returns BLE adaptor
func (b *LEDDriver) adaptor() ble.BLEConnector {
return b.Connection().(ble.BLEConnector)
}
// Start tells driver to get ready to do work
func (b *LEDDriver) Start() (err error) {
return
}
// Halt stops LED driver (void)
func (b *LEDDriver) Halt() (err error) {
return
}
// ReadMatrix read the current LED matrix state
func (b *LEDDriver) ReadMatrix() (data []byte, err error) {
data, err = b.adaptor().ReadCharacteristic(ledMatrixStateCharacteristic)
return
}
// WriteMatrix writes an array of 5 bytes to set the LED matrix
func (b *LEDDriver) WriteMatrix(data []byte) (err error) {
err = b.adaptor().WriteCharacteristic(ledMatrixStateCharacteristic, data)
return
}
// WriteText writes a text message to the Microbit LED matrix
func (b *LEDDriver) WriteText(msg string) (err error) {
err = b.adaptor().WriteCharacteristic(ledTextCharacteristic, []byte(msg))
return err
}
func (b *LEDDriver) ReadScrollingDelay() (delay uint16, err error) {
return
}
func (b *LEDDriver) WriteScrollingDelay(delay uint16) (err error) {
buf := []byte{byte(delay)}
err = b.adaptor().WriteCharacteristic(ledScrollingDelayCharacteristic, buf)
return
}
// Blank clears the LEDs on the Microbit
func (b *LEDDriver) Blank() (err error) {
buf := []byte{0x00, 0x00, 0x00, 0x00, 0x00}
err = b.WriteMatrix(buf)
return
}
// Solid turns on all of the Microbit LEDs
func (b *LEDDriver) Solid() (err error) {
buf := []byte{0x1F, 0x1F, 0x1F, 0x1F, 0x1F}
err = b.WriteMatrix(buf)
return
}
// UpRightArrow displays an arrow pointing upwards and to the right on the Microbit LEDs
func (b *LEDDriver) UpRightArrow() (err error) {
buf := []byte{0x0F, 0x03, 0x05, 0x09, 0x10}
err = b.WriteMatrix(buf)
return
}
// UpLeftArrow displays an arrow pointing upwards and to the left on the Microbit LEDs
func (b *LEDDriver) UpLeftArrow() (err error) {
buf := []byte{0x1E, 0x18, 0x14, 0x12, 0x01}
err = b.WriteMatrix(buf)
return
}
// DownRightArrow displays an arrow pointing down and to the right on the Microbit LEDs
func (b *LEDDriver) DownRightArrow() (err error) {
buf := []byte{0x10, 0x09, 0x05, 0x03, 0x0F}
err = b.WriteMatrix(buf)
return
}
// DownLeftArrow displays an arrow pointing down and to the left on the Microbit LEDs
func (b *LEDDriver) DownLeftArrow() (err error) {
buf := []byte{0x01, 0x12, 0x14, 0x18, 0x1E}
err = b.WriteMatrix(buf)
return
}
// Dimond displays a dimond on the Microbit LEDs
func (b *LEDDriver) Dimond() (err error) {
buf := []byte{0x04, 0x0A, 0x11, 0x0A, 0x04}
err = b.WriteMatrix(buf)
return
}
// Smile displays a smile on the Microbit LEDs
func (b *LEDDriver) Smile() (err error) {
buf := []byte{0x0A, 0x0A, 0x00, 0x11, 0x0E}
err = b.WriteMatrix(buf)
return
}
// Wink displays a wink on the Microbit LEDs
func (b *LEDDriver) Wink() (err error) {
buf := []byte{0x08, 0x0B, 0x00, 0x11, 0x0E}
err = b.WriteMatrix(buf)
return
}