embd/README.md

253 lines
9.2 KiB
Markdown
Raw Normal View History

2016-09-07 06:07:08 +02:00
# embd [![Build Status](https://travis-ci.org/kidoman/embd.svg?branch=master)](https://travis-ci.org/kidoman/embd) [![GoDoc](http://godoc.org/github.com/kidoman/embd?status.png)](http://godoc.org/github.com/kidoman/embd)
2013-12-07 18:41:06 +01:00
**embd** is a hardware abstraction layer (HAL) for embedded systems.
2016-09-06 08:11:12 +02:00
It allows you to start your hardware hack on easily available hobby boards
2016-09-07 06:12:17 +02:00
(like the Raspberry Pi, BeagleBone Black, C.H.I.P., etc.) by giving you
straight-forward access to the board's capabilities as well as a plethora of
2016-09-06 08:11:12 +02:00
**sensors** (like accelerometers, gyroscopes, thermometers, etc.) and
**controllers** (PWM generators, digital-to-analog convertors) for
2016-09-09 07:50:29 +02:00
which it includes drivers. If you move to custom designed boards
you have to throw away your code: you carry forward the effort
where the HAL abstraction of EMBD will save you precious time.
2016-09-06 08:11:12 +02:00
2016-09-07 06:12:17 +02:00
Development supported and sponsored by [**SoStronk**](https://www.sostronk.com) and
2016-09-06 08:11:12 +02:00
[**ThoughtWorks**](http://www.thoughtworks.com/).
2014-03-26 06:18:05 +01:00
Also, you might be interested in: [Why Golang?](https://github.com/kidoman/embd/wiki/Why-Go)
2014-05-11 18:16:24 +02:00
[Blog post introducing EMBD](http://kidoman.io/framework/embd.html)
2014-05-08 01:01:43 +02:00
2014-04-11 07:34:03 +02:00
## Getting Started
2016-09-09 07:50:29 +02:00
Install Go version 1.6 or later to make compiling for ARM easy.
The set up your [GOPATH](http://golang.org/doc/code.html#GOPATH),
and create your first .go file. We'll call it `simpleblinker.go`.
2014-04-11 07:34:03 +02:00
```go
2014-04-11 07:34:03 +02:00
package main
import (
"time"
"github.com/kidoman/embd"
_ "github.com/kidoman/embd/host/rpi" // This loads the RPi driver
2014-04-11 07:34:03 +02:00
)
func main() {
for {
embd.LEDToggle("LED0")
2014-04-11 07:34:03 +02:00
time.Sleep(250 * time.Millisecond)
}
}
```
2016-09-09 07:50:29 +02:00
Then install the EMBD package:
2014-04-11 07:34:03 +02:00
2016-09-07 06:13:16 +02:00
$ go get github.com/kidoman/embd
2014-04-11 07:34:03 +02:00
2016-09-09 07:50:29 +02:00
Build the binary for linux/ARM:
2014-04-11 07:34:03 +02:00
$ export GOOS=linux
$ export GOARCH=arm
$ go build simpleblinker.go
Copy the cross-compiled binary to your RaspberryPi*:
$ scp simpleblinker pi@192.168.2.2:~
2016-09-09 07:50:29 +02:00
Then on the rPi run the program with ```sudo```*:
2014-04-11 07:34:03 +02:00
$ sudo ./simpleblinker
**You will now see the green LED (next to the always on power LED) blink every 1/4 sec.**
2014-04-11 07:34:03 +02:00
**<nowiki>*</nowiki> Notes**
* Assuming your RaspberryPi has an IP address of ```192.168.2.2```. Substitute as necessary
2016-09-09 07:50:29 +02:00
* `sudo` (root) permission is required as we are controlling the hardware by writing to special files
2016-09-06 08:11:12 +02:00
* This sample program is optimized for brevity and does not clean up after itself. Click here to
see the [full version](https://github.com/kidoman/embd/blob/master/samples/fullblinker.go)
2014-04-11 07:34:03 +02:00
2014-04-11 04:35:15 +02:00
## Getting Help
2016-09-09 07:50:29 +02:00
Join the [slack channel](https://gophers.slack.com/archives/embd)
2014-04-11 04:35:15 +02:00
## Platforms Supported
2015-01-15 02:25:55 +01:00
* [RaspberryPi](http://www.raspberrypi.org/) (including [A+](http://www.raspberrypi.org/products/model-a-plus/) and [B+](http://www.raspberrypi.org/products/model-b-plus/))
2015-02-17 12:35:10 +01:00
* [RaspberryPi 2](http://www.raspberrypi.org/)
2016-09-06 08:11:12 +02:00
* [NextThing C.H.I.P](https://www.nextthing.co/pages/chip)
* [BeagleBone Black](http://beagleboard.org/Products/BeagleBone%20Black)
2014-03-23 11:17:58 +01:00
## The command line tool
2014-03-23 11:17:58 +01:00
go get github.com/kidoman/embd/embd
will install a command line utility ```embd``` which will allow you to quickly get started with prototyping. The binary should be available in your ```$GOPATH/bin```. However, to be able to run this on a ARM based device, you will need to build it with ```GOOS=linux``` and ```GOARCH=arm``` environment variables set.
For example, if you run ```embd detect``` on a **BeagleBone Black**:
root@beaglebone:~# embd detect
detected host BeagleBone Black (rev 0)
Run ```embd``` without any arguments to discover the various commands supported by the utility.
## How to use the framework
Package **embd** provides a hardware abstraction layer for doing embedded programming
2014-03-23 11:17:58 +01:00
on supported platforms like the Raspberry Pi and BeagleBone Black. Most of the examples below
will work without change (i.e. the same binary) on all supported platforms. How cool is that?
Although samples are all present in the [samples](https://github.com/kidoman/embd/tree/master/samples) folder,
we will show a few choice examples here.
2014-03-26 05:40:57 +01:00
Use the **LED** driver to toggle LEDs on the BBB:
2014-03-23 11:17:58 +01:00
2014-05-19 07:57:57 +02:00
```go
import "github.com/kidoman/embd"
2014-05-19 07:59:16 +02:00
import _ "github.com/kidoman/embd/host/all"
2014-05-19 07:57:57 +02:00
...
embd.InitLED()
defer embd.CloseLED()
...
led, err := embd.NewLED(3)
...
led.Toggle()
```
2014-03-23 11:17:58 +01:00
Even shorter when quickly trying things out:
2014-03-23 11:17:58 +01:00
2014-05-19 07:57:57 +02:00
```go
import "github.com/kidoman/embd"
2014-05-19 07:59:16 +02:00
import _ "github.com/kidoman/embd/host/all"
2014-05-19 07:57:57 +02:00
...
embd.InitLED()
defer embd.CloseLED()
...
embd.ToggleLED(3)
```
2014-03-23 11:17:58 +01:00
2014-04-12 06:46:35 +02:00
**3** is the same as **USR3** for all intents and purposes. The driver is smart enough to figure all this out.
2014-03-29 05:17:22 +01:00
2014-03-28 04:09:36 +01:00
BBB + **PWM**:
2014-05-19 07:57:57 +02:00
```go
import "github.com/kidoman/embd"
2014-05-19 07:59:16 +02:00
import _ "github.com/kidoman/embd/host/all"
2014-05-19 07:57:57 +02:00
...
embd.InitGPIO()
defer embd.CloseGPIO()
...
pwm, _ := embd.NewPWMPin("P9_14")
defer pwm.Close()
...
pwm.SetDuty(1000)
```
2014-03-28 04:09:36 +01:00
2014-03-26 05:40:57 +01:00
Control **GPIO** pins on the RaspberryPi / BeagleBone Black:
2014-03-23 11:17:58 +01:00
2014-05-19 07:57:57 +02:00
```go
import "github.com/kidoman/embd"
2014-05-19 07:59:16 +02:00
import _ "github.com/kidoman/embd/host/all"
2014-05-19 07:57:57 +02:00
...
embd.InitGPIO()
defer embd.CloseGPIO()
...
embd.SetDirection(10, embd.Out)
embd.DigitalWrite(10, embd.High)
```
2014-03-23 11:17:58 +01:00
Could also do:
2014-05-19 07:57:57 +02:00
```go
import "github.com/kidoman/embd"
2014-05-19 07:59:16 +02:00
import _ "github.com/kidoman/embd/host/all"
2014-05-19 07:57:57 +02:00
...
embd.InitGPIO()
defer embd.CloseGPIO()
...
pin, err := embd.NewDigitalPin(10)
...
pin.SetDirection(embd.Out)
pin.Write(embd.High)
```
2014-03-23 11:17:58 +01:00
2014-03-26 05:40:57 +01:00
Or read data from the **Bosch BMP085** barometric sensor:
2014-03-23 11:17:58 +01:00
2014-05-19 07:57:57 +02:00
```go
import "github.com/kidoman/embd"
import "github.com/kidoman/embd/sensor/bmp085"
2014-05-19 07:59:16 +02:00
import _ "github.com/kidoman/embd/host/all"
2014-05-19 07:57:57 +02:00
...
bus := embd.NewI2CBus(1)
...
baro := bmp085.New(bus)
...
temp, err := baro.Temperature()
altitude, err := baro.Altitude()
```
2014-03-23 11:17:58 +01:00
2014-03-26 05:40:57 +01:00
Even find out the heading from the **LSM303** magnetometer:
2014-03-23 11:17:58 +01:00
2014-05-19 07:57:57 +02:00
```go
import "github.com/kidoman/embd"
import "github.com/kidoman/embd/sensor/lsm303"
2014-05-19 07:59:16 +02:00
import _ "github.com/kidoman/embd/host/all"
2014-05-19 07:57:57 +02:00
...
bus := embd.NewI2CBus(1)
...
mag := lsm303.New(bus)
...
heading, err := mag.Heading()
```
2014-03-23 11:17:58 +01:00
2014-03-26 05:40:57 +01:00
The above two examples depend on **I2C** and therefore will work without change on almost all
2014-03-23 11:17:58 +01:00
platforms.
## Protocols Supported
2013-12-08 20:47:31 +01:00
2014-03-23 11:17:58 +01:00
* **Digital GPIO** [Documentation](http://godoc.org/github.com/kidoman/embd#DigitalPin)
* **Analog GPIO** [Documentation](http://godoc.org/github.com/kidoman/embd#AnalogPin)
2014-03-29 05:17:22 +01:00
* **PWM** [Documentation](http://godoc.org/github.com/kidoman/embd#PWMPin)
2014-03-23 11:17:58 +01:00
* **I2C** [Documentation](http://godoc.org/github.com/kidoman/embd#I2CBus)
* **LED** [Documentation](http://godoc.org/github.com/kidoman/embd#LED)
2014-05-22 08:40:14 +02:00
* **SPI** [Documentation](http://godoc.org/github.com/kidoman/embd#SPIBus)
2013-12-08 20:47:31 +01:00
## Sensors Supported
2013-12-08 20:47:31 +01:00
2014-02-10 00:35:41 +01:00
* **TMP006** Thermopile sensor [Documentation](http://godoc.org/github.com/kidoman/embd/sensor/tmp006), [Datasheet](http://www.adafruit.com/datasheets/tmp006.pdf), [Userguide](http://www.adafruit.com/datasheets/tmp006ug.pdf)
* **BMP085** Barometric pressure sensor [Documentation](http://godoc.org/github.com/kidoman/embd/sensor/bmp085), [Datasheet](https://www.sparkfun.com/datasheets/Components/General/BST-BMP085-DS000-05.pdf)
* **BMP180** Barometric pressure sensor [Documentation](http://godoc.org/github.com/kidoman/embd/sensor/bmp180), [Datasheet](http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf)
* **LSM303** Accelerometer and magnetometer [Documentation](http://godoc.org/github.com/kidoman/embd/sensor/lsm303), [Datasheet](https://www.sparkfun.com/datasheets/Sensors/Magneto/LSM303%20Datasheet.pdf)
* **L3GD20** Gyroscope [Documentation](http://godoc.org/github.com/kidoman/embd/sensor/l3gd20), [Datasheet](http://www.adafruit.com/datasheets/L3GD20.pdf)
* **US020** Ultrasonic proximity sensor [Documentation](http://godoc.org/github.com/kidoman/embd/sensor/us020), [Product Page](http://www.digibay.in/sensor/object-detection-and-proximity?product_id=239)
* **BH1750FVI** Luminosity sensor [Documentation](http://godoc.org/github.com/kidoman/embd/sensor/bh1750fvi), [Datasheet](http://www.elechouse.com/elechouse/images/product/Digital%20light%20Sensor/bh1750fvi-e.pdf)
2014-01-05 09:24:24 +01:00
## Interfaces
2014-01-05 22:17:25 +01:00
* **Keypad(4x3)** [Product Page](http://www.adafruit.com/products/419#Learn)
2014-01-05 09:24:24 +01:00
## Controllers
2014-02-10 00:35:41 +01:00
* **PCA9685** 16-channel, 12-bit PWM Controller with I2C protocol [Documentation](http://godoc.org/github.com/kidoman/embd/controller/pca9685), [Datasheet](http://www.adafruit.com/datasheets/PCA9685.pdf), [Product Page](http://www.adafruit.com/products/815)
* **MCP4725** 12-bit DAC [Documentation](http://godoc.org/github.com/kidoman/embd/controller/mcp4725), [Datasheet](http://www.adafruit.com/datasheets/mcp4725.pdf), [Product Page](http://www.adafruit.com/products/935)
* **ServoBlaster** RPi PWM/PCM based PWM controller [Documentation](http://godoc.org/github.com/kidoman/embd/controller/servoblaster), [Product Page](https://github.com/richardghirst/PiBits/tree/master/ServoBlaster)
2014-04-11 04:07:34 +02:00
2014-05-22 08:40:14 +02:00
## Convertors
* **MCP3008** 8-channel, 10-bit ADC with SPI protocol, [Datasheet](https://www.adafruit.com/datasheets/MCP3008.pdf)
2014-04-11 04:28:26 +02:00
## Contributing
2016-09-09 07:50:29 +02:00
[Pull requests](https://github.com/kidoman/embd/pulls) that follow the
[guidelines](https://github.com/kidoman/embd/blob/master/CONTRIBUTING.md) are very appreciated.
If you find a problem but are not up to coding a fix please file an
[issue](https://github.com/kidoman/embd/issues).
Thank you!
2014-04-11 04:28:26 +02:00
2014-04-11 04:07:34 +02:00
## About
2014-05-19 07:50:28 +02:00
EMBD is affectionately designed/developed by Karan Misra ([kidoman](https://github.com/kidoman)), Kunal Powar ([kunalpowar](https://github.com/kunalpowar)) and [FRIENDS](https://github.com/kidoman/embd/blob/master/AUTHORS). We also have a list of [CONTRIBUTORS](https://github.com/kidoman/embd/blob/master/CONTRIBUTORS).