forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
microbit: initial implementation for LEDs
Signed-off-by: deadprogram <[email protected]>
- Loading branch information
1 parent
fc23d52
commit 44de801
Showing
6 changed files
with
267 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"gobot.io/x/gobot" | ||
"gobot.io/x/gobot/platforms/ble" | ||
"gobot.io/x/gobot/platforms/microbit" | ||
) | ||
|
||
func main() { | ||
bleAdaptor := ble.NewClientAdaptor(os.Args[1]) | ||
ubit := microbit.NewLEDDriver(bleAdaptor) | ||
|
||
work := func() { | ||
ubit.Blank() | ||
gobot.After(1*time.Second, func() { | ||
ubit.WriteText("Hello") | ||
}) | ||
gobot.After(7*time.Second, func() { | ||
ubit.Smile() | ||
}) | ||
} | ||
|
||
robot := gobot.NewRobot("blinkBot", | ||
[]gobot.Connection{bleAdaptor}, | ||
[]gobot.Device{ubit}, | ||
work, | ||
) | ||
|
||
robot.Start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Copyright (c) 2014-2017 The Hybrid Group | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Microbit | ||
|
||
The Microbit is a tiny computer with built-in Bluetooth LE aka Bluetooth 4.0. | ||
|
||
## How to Install | ||
``` | ||
go get -d -u gobot.io/x/gobot/... && go install gobot.io/x/gobot/platforms/microbit | ||
``` | ||
|
||
## How to Use | ||
```go | ||
// code here... | ||
``` | ||
|
||
## How to Connect | ||
|
||
The Microbit is a Bluetooth LE device. | ||
|
||
You need to know the BLE ID of the Microbit that you want to connect to. | ||
|
||
### OSX | ||
|
||
To run any of the Gobot BLE code you must use the `GODEBUG=cgocheck=0` flag in order to get around some of the issues in the CGo-based implementation. | ||
|
||
For example: | ||
|
||
GODEBUG=cgocheck=0 go run examples/microbit_blink.go "BBC micro:bit" | ||
|
||
OSX uses its own Bluetooth ID system which is different from the IDs used on Linux. The code calls thru the XPC interfaces provided by OSX, so as a result does not need to run under sudo. | ||
|
||
### Ubuntu | ||
|
||
On Linux the BLE code will need to run as a root user account. The easiest way to accomplish this is probably to use `go build` to build your program, and then to run the requesting executable using `sudo`. | ||
|
||
For example: | ||
|
||
go build examples/microbit_blink.go | ||
sudo ./microbit_blink "BBC micro:bit" | ||
|
||
### Windows | ||
|
||
Hopefully coming soon... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
Package microbit contains the Gobot driver for the Microbit. | ||
For more information refer to the microbit README: | ||
https://github.com/hybridgroup/gobot/blob/master/platforms/microbit/README.md | ||
*/ | ||
package microbit // import "gobot.io/x/gobot/platforms/microbit" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package microbit | ||
|
||
import ( | ||
"gobot.io/x/gobot" | ||
"gobot.io/x/gobot/platforms/ble" | ||
) | ||
|
||
// LEDDriver is the Gobot interface to the Microbit LED | ||
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.ClientAdaptor) *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.ClientAdaptor { | ||
return b.Connection().(*ble.ClientAdaptor) | ||
} | ||
|
||
// 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package microbit | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"gobot.io/x/gobot" | ||
"gobot.io/x/gobot/gobottest" | ||
|
||
"gobot.io/x/gobot/platforms/ble" | ||
) | ||
|
||
var _ gobot.Driver = (*LEDDriver)(nil) | ||
|
||
func initTestLEDDriver() *LEDDriver { | ||
d := NewLEDDriver(ble.NewClientAdaptor("D7:99:5A:26:EC:38")) | ||
return d | ||
} | ||
|
||
func TestLEDDriver(t *testing.T) { | ||
d := initTestLEDDriver() | ||
gobottest.Assert(t, strings.HasPrefix(d.Name(), "Microbit LED"), true) | ||
} |