Skip to content

Commit

Permalink
ble: introduce in drivers folder
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2thomas committed Feb 4, 2024
1 parent 474d553 commit d1abeb7
Show file tree
Hide file tree
Showing 146 changed files with 3,087 additions and 3,329 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ including_except := $(shell go list ./... | grep -v platforms/opencv)

# Run tests on nearly all directories without test cache, with race detection
test_race:
go test -failfast -count=1 -v -race $(including_except)
go test -failfast -count=1 -race $(including_except)

# Run tests on nearly all directories without test cache
test:
Expand Down
83 changes: 54 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ import (
"time"

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/platforms/sphero"
"gobot.io/x/gobot/v2/drivers/serial"
"gobot.io/x/gobot/v2/platforms/serialport"
)

func main() {
adaptor := sphero.NewAdaptor("/dev/rfcomm0")
driver := sphero.NewSpheroDriver(adaptor)
adaptor := serialport.NewAdaptor("/dev/rfcomm0")
driver := serial.NewSpheroDriver(adaptor)

work := func() {
gobot.Every(3*time.Second, func() {
Expand Down Expand Up @@ -166,18 +167,20 @@ import (

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/api"
"gobot.io/x/gobot/v2/platforms/sphero"
"gobot.io/x/gobot/v2/drivers/common/sphero"
"gobot.io/x/gobot/v2/drivers/serial"
"gobot.io/x/gobot/v2/platforms/serialport"
)

func NewSwarmBot(port string) *gobot.Robot {
spheroAdaptor := sphero.NewAdaptor(port)
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor)
spheroAdaptor := serialport.NewAdaptor(port)
spheroDriver := serial.NewSpheroDriver(spheroAdaptor)
spheroDriver.SetName("Sphero" + port)

work := func() {
spheroDriver.Stop()

spheroDriver.On(sphero.Collision, func(data interface{}) {
spheroDriver.On(sphero.CollisionEvent, func(data interface{}) {
fmt.Println("Collision Detected!")
})

Expand Down Expand Up @@ -229,7 +232,7 @@ platforms are currently supported:
- Audio <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/audio)
- [Beaglebone Black](http://beagleboard.org/boards) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/beaglebone)
- [Beaglebone PocketBeagle](http://beagleboard.org/pocket/) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/beaglebone)
- [Bluetooth LE](https://www.bluetooth.com/what-is-bluetooth-technology/bluetooth-technology-basics/low-energy) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/ble)
- [Bluetooth LE](https://www.bluetooth.com/what-is-bluetooth-technology/bluetooth-technology-basics/low-energy) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/bleclient)
- [C.H.I.P](http://www.nextthing.co/pages/chip) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/chip)
- [C.H.I.P Pro](https://docs.getchip.com/chip_pro.html) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/chip)
- [Digispark](http://digistump.com/products/1) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/digispark)
Expand Down Expand Up @@ -259,15 +262,47 @@ platforms are currently supported:
- [Pebble](https://www.getpebble.com/) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/pebble)
- [Radxa Rock Pi 4](https://wiki.radxa.com/Rock4/) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/rockpi)
- [Raspberry Pi](http://www.raspberrypi.org/) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/raspi)
- [Sphero](http://www.sphero.com/) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/sphero)
- [Serial Port](https://en.wikipedia.org/wiki/Serial_port) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/serialport)
- [Sphero](http://www.sphero.com/) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/sphero/sphero)
- [Sphero BB-8](http://www.sphero.com/bb8) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/sphero/bb8)
- [Sphero Ollie](http://www.sphero.com/ollie) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/sphero/ollie)
- [Sphero SPRK+](http://www.sphero.com/sprk-plus) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/sphero/sprkplus)
- [Tinker Board](https://www.asus.com/us/Single-Board-Computer/Tinker-Board/) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/tinkerboard)
- [UP2](http://www.up-board.org/upsquared/) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/upboard/up2)

Support for many devices that use General Purpose Input/Output (GPIO) have
a shared set of drivers provided using the `gobot/drivers/gpio` package:
Support for many devices that use Analog Input/Output (AIO) have a shared set of drivers provided using
the `gobot/drivers/aio` package:

- [AIO](https://en.wikipedia.org/wiki/Analog-to-digital_converter) <=> [Drivers](https://github.com/hybridgroup/gobot/tree/master/drivers/aio)
- Analog Actuator
- Analog Sensor
- Grove Light Sensor
- Grove Piezo Vibration Sensor
- Grove Rotary Dial
- Grove Sound Sensor
- Grove Temperature Sensor
- Temperature Sensor (supports linear and NTC thermistor in normal and inverse mode)
- Thermal Zone Temperature Sensor

Support for many devices that use Bluetooth LE (BLE) have a shared set of drivers provided using
the `gobot/drivers/ble` package:

- [BLE](http://en.wikipedia.org/wiki/Bluetooth_low_energy) <=> [Drivers](https://github.com/hybridgroup/gobot/tree/master/drivers/ble)
- Battery Service
- Device Information Service
- Generic Access Service
- Microbit: AccelerometerDriver
- Microbit: ButtonDriver
- Microbit: IOPinDriver
- Microbit: LEDDriver
- Microbit: MagnetometerDriver
- Microbit: TemperatureDriver
- Sphero: BB8
- Sphero: Ollie
- Sphero: SPRK+

Support for many devices that use General Purpose Input/Output (GPIO) have a shared set of drivers provided using
the `gobot/drivers/gpio` package:

- [GPIO](https://en.wikipedia.org/wiki/General_Purpose_Input/Output) <=> [Drivers](https://github.com/hybridgroup/gobot/tree/master/drivers/gpio)
- AIP1640 LED Dot Matrix/7 Segment Controller
Expand All @@ -294,22 +329,8 @@ a shared set of drivers provided using the `gobot/drivers/gpio` package:
- Stepper Motor
- TM1638 LED Controller

Support for many devices that use Analog Input/Output (AIO) have
a shared set of drivers provided using the `gobot/drivers/aio` package:

- [AIO](https://en.wikipedia.org/wiki/Analog-to-digital_converter) <=> [Drivers](https://github.com/hybridgroup/gobot/tree/master/drivers/aio)
- Analog Actuator
- Analog Sensor
- Grove Light Sensor
- Grove Piezo Vibration Sensor
- Grove Rotary Dial
- Grove Sound Sensor
- Grove Temperature Sensor
- Temperature Sensor (supports linear and NTC thermistor in normal and inverse mode)
- Thermal Zone Temperature Sensor

Support for devices that use Inter-Integrated Circuit (I2C) have a shared set of
drivers provided using the `gobot/drivers/i2c` package:
Support for devices that use Inter-Integrated Circuit (I2C) have a shared set of drivers provided using
the `gobot/drivers/i2c` package:

- [I2C](https://en.wikipedia.org/wiki/I%C2%B2C) <=> [Drivers](https://github.com/hybridgroup/gobot/tree/master/drivers/i2c)
- Adafruit 1109 2x16 RGB-LCD with 5 keys
Expand Down Expand Up @@ -351,6 +372,12 @@ drivers provided using the `gobot/drivers/i2c` package:
- Wii Nunchuck Controller
- YL-40 Brightness/Temperature sensor, Potentiometer, analog input, analog output Driver

Support for many devices that use Serial communication (UART) have a shared set of drivers provided using
the `gobot/drivers/serial` package:

- [UART](https://en.wikipedia.org/wiki/Serial_port) <=> [Drivers](https://github.com/hybridgroup/gobot/tree/master/drivers/serial)
- Sphero: Sphero

Support for devices that use Serial Peripheral Interface (SPI) have
a shared set of drivers provided using the `gobot/drivers/spi` package:

Expand All @@ -366,8 +393,6 @@ a shared set of drivers provided using the `gobot/drivers/spi` package:
- MFRC522 RFID Card Reader
- SSD1306 OLED Display Controller

More platforms and drivers are coming soon...

## API

Gobot includes a RESTful API to query the status of any robot running within a group, including the connection and
Expand Down
14 changes: 14 additions & 0 deletions adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,20 @@ type Adaptor interface {
Finalize() error
}

// BLEConnector is the interface that a BLE ClientAdaptor must implement
type BLEConnector interface {
Adaptor

Reconnect() error
Disconnect() error
Address() string

ReadCharacteristic(cUUID string) ([]byte, error)
WriteCharacteristic(cUUID string, data []byte) error
Subscribe(cUUID string, f func([]byte, error)) error
WithoutResponses(use bool)
}

// Porter is the interface that describes an adaptor's port
type Porter interface {
Port() string
Expand Down
8 changes: 6 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ before_test:
build_script:
- go test -v -cpu=2 .
- go test -v -cpu=2 ./drivers/aio/...
- go test -v -cpu=2 ./platforms/ble/...
- go test -v -cpu=2 ./drivers/ble/.
- go test -v -cpu=2 ./drivers/ble/parrot/.
- go test -v -cpu=2 ./drivers/ble/sphero/.
- go test -v -cpu=2 ./drivers/serial/...
- go test -v -cpu=2 ./platforms/bleclient/...
- go test -v -cpu=2 ./platforms/dji/...
- go test -v -cpu=2 ./platforms/firmata/...
- go test -v -cpu=2 ./platforms/joystick/...
- go test -v -cpu=2 ./platforms/parrot/...
- go test -v -cpu=2 ./platforms/sphero/...
- go test -v -cpu=2 ./platforms/serialport/...
- cd ..
6 changes: 3 additions & 3 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ Finally, you can use Master Gobot to add the complete Gobot API or control swarm
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/api"
"gobot.io/x/gobot/v2/platforms/sphero"
"gobot.io/x/gobot/v2/platforms/serialport"
)
func NewSwarmBot(port string) *gobot.Robot {
spheroAdaptor := sphero.NewAdaptor(port)
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor)
spheroAdaptor := serialport.NewAdaptor(port)
spheroDriver := serial.NewSpheroDriver(spheroAdaptor)
spheroDriver.SetName("Sphero" + port)
work := func() {
Expand Down
13 changes: 13 additions & 0 deletions drivers/ble/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2014-2018 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.
27 changes: 27 additions & 0 deletions drivers/ble/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# BLE

This package provides drivers for [Bluetooth LE (BLE)](http://en.wikipedia.org/wiki/Bluetooth_low_energy)
devices. It is normally used by connecting an [BLE client adaptor](https://github.com/hybridgroup/gobot/tree/release/platforms/bleclient)
that supports the needed interfaces for BLE devices.

## Getting Started

Please refer to the main [README.md](https://github.com/hybridgroup/gobot/blob/release/README.md) and the README files
in the subfolders.

## Hardware Support

Gobot has a extensible system for connecting to hardware devices. The following BLE devices are currently supported:

- Battery Service
- Device Information Service
- Generic Access Service
- Microbit: AccelerometerDriver
- Microbit: ButtonDriver
- Microbit: IOPinDriver
- Microbit: LEDDriver
- Microbit: MagnetometerDriver
- Microbit: TemperatureDriver
- Sphero: BB8
- Sphero: Ollie
- Sphero: SPRK+
39 changes: 39 additions & 0 deletions drivers/ble/battery_driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ble

import (
"bytes"
"log"

"gobot.io/x/gobot/v2"
)

const batteryCharaShort = "2a19"

// BatteryDriver represents the battery service for a BLE peripheral
type BatteryDriver struct {
*Driver
gobot.Eventer
}

// NewBatteryDriver creates a new driver
func NewBatteryDriver(a gobot.BLEConnector) *BatteryDriver {
d := &BatteryDriver{
Driver: NewDriver(a, "Battery", nil, nil),
Eventer: gobot.NewEventer(),
}

return d
}

// GetBatteryLevel reads and returns the current battery level
func (d *BatteryDriver) GetBatteryLevel() uint8 {
c, err := d.Adaptor().ReadCharacteristic(batteryCharaShort)
if err != nil {
log.Println(err)
return 0
}
buf := bytes.NewBuffer(c)
val, _ := buf.ReadByte()
level := val
return level
}
33 changes: 33 additions & 0 deletions drivers/ble/battery_driver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ble

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"

"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/ble/testutil"
)

var _ gobot.Driver = (*BatteryDriver)(nil)

func TestNewBatteryDriver(t *testing.T) {
d := NewBatteryDriver(testutil.NewBleTestAdaptor())
assert.True(t, strings.HasPrefix(d.Name(), "Battery"))
assert.NotNil(t, d.Eventer)
}

func TestBatteryDriverRead(t *testing.T) {
a := testutil.NewBleTestAdaptor()
d := NewBatteryDriver(a)
a.SetReadCharacteristicTestFunc(func(cUUID string) ([]byte, error) {
if cUUID == "2a19" {
return []byte{20}, nil
}

return nil, nil
})

assert.Equal(t, uint8(20), d.GetBatteryLevel())
}
Loading

0 comments on commit d1abeb7

Please sign in to comment.