embed

module
v0.0.0-...-8a7fcd8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2021 License: MIT

README

embd Build Status GoDoc

embd is a hardware abstraction layer (HAL) for embedded systems.

It allows you to start your hardware hack on easily available hobby boards (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 sensors (like accelerometers, gyroscopes, thermometers, etc.) and controllers (PWM generators, digital-to-analog convertors) for 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.

The overall strategy used in embd is to use Linux device drivers to access gpio pins, SPI and I2C buses, as well as interrupts. This makes it easy to port from one platform to another and it enables kernel code to handle the devices as efficiently as possible. What embd then adds is first a Golang library interface on top of the various Linux devices and then another layer of user-level drivers for specific sensors and controllers that are connected to gpio pins or one of the buses.

Development supported and sponsored by SoStronk and ThoughtWorks.

Also, you might be interested in: Why Golang?

Blog post introducing EMBD

Getting Started

Install Go version 1.6 or later to make compiling for ARM easy. The set up your GOPATH, and create your first .go file. We'll call it simpleblinker.go.

package main

import (
	"time"

	"github.com/talkkonnect/embd"
	_ "github.com/talkkonnect/embd/host/rpi" // This loads the RPi driver
)

func main() {
	for {
		embd.LEDToggle("LED0")
		time.Sleep(250 * time.Millisecond)
	}
}

Then install the EMBD package:

$ go get github.com/talkkonnect/embd

Build the binary for linux/ARM:

$ 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:~

Then on the rPi run the program with sudo*:

$ sudo ./simpleblinker

You will now see the green LED (next to the always on power LED) blink every 1/4 sec.

* Notes

  • Assuming your RaspberryPi has an IP address of 192.168.2.2. Substitute as necessary
  • sudo (root) permission is required as we are controlling the hardware by writing to special files
  • This sample program is optimized for brevity and does not clean up after itself. Click here to see the full version

Getting Help

Join the slack channel

Platforms Supported

The command line tool

go get github.com/talkkonnect/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 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 folder, we will show a few choice examples here.

Use the LED driver to toggle LEDs on the BBB:

import "github.com/talkkonnect/embd"
import _ "github.com/talkkonnect/embd/host/all"
...
embd.InitLED()
defer embd.CloseLED()
...
led, err := embd.NewLED(3)
...
led.Toggle()

Even shorter when quickly trying things out:

import "github.com/talkkonnect/embd"
import _ "github.com/talkkonnect/embd/host/all"
...
embd.InitLED()
defer embd.CloseLED()
...
embd.ToggleLED(3)

3 is the same as USR3 for all intents and purposes. The driver is smart enough to figure all this out.

BBB + PWM:

import "github.com/talkkonnect/embd"
import _ "github.com/talkkonnect/embd/host/all"
...
embd.InitGPIO()
defer embd.CloseGPIO()
...
pwm, _ := embd.NewPWMPin("P9_14")
defer pwm.Close()
...
pwm.SetDuty(1000)

Control GPIO pins on the RaspberryPi / BeagleBone Black:

import "github.com/talkkonnect/embd"
import _ "github.com/talkkonnect/embd/host/all"
...
embd.InitGPIO()
defer embd.CloseGPIO()
...
embd.SetDirection(10, embd.Out)
embd.DigitalWrite(10, embd.High)

Could also do:

import "github.com/talkkonnect/embd"
import _ "github.com/talkkonnect/embd/host/all"
...
embd.InitGPIO()
defer embd.CloseGPIO()
...
pin, err := embd.NewDigitalPin(10)
...
pin.SetDirection(embd.Out)
pin.Write(embd.High)

Or read data from the Bosch BMP085 barometric sensor:

import "github.com/talkkonnect/embd"
import "github.com/talkkonnect/embd/sensor/bmp085"
import _ "github.com/talkkonnect/embd/host/all"
...
bus := embd.NewI2CBus(1)
...
baro := bmp085.New(bus)
...
temp, err := baro.Temperature()
altitude, err := baro.Altitude()

Even find out the heading from the LSM303 magnetometer:

import "github.com/talkkonnect/embd"
import "github.com/talkkonnect/embd/sensor/lsm303"
import _ "github.com/talkkonnect/embd/host/all"
...
bus := embd.NewI2CBus(1)
...
mag := lsm303.New(bus)
...
heading, err := mag.Heading()

The above two examples depend on I2C and therefore will work without change on almost all platforms.

Protocols Supported

Sensors Supported

Interfaces

Controllers

Convertors

  • MCP3008 8-channel, 10-bit ADC with SPI protocol, Datasheet

Contributing

Pull requests that follow the guidelines are very appreciated. If you find a problem but are not up to coding a fix please file an issue. Thank you!

About

EMBD is affectionately designed/developed by Karan Misra (talkkonnect), Kunal Powar (kunalpowar) and FRIENDS. We also have a list of CONTRIBUTORS.

embed

Directories

Path Synopsis
Package controller is a container for the various device controllers supported by EMBD.
Package controller is a container for the various device controllers supported by EMBD.
hd44780
Package hd44780 allows controlling an HD44780-compatible character LCD controller.
Package hd44780 allows controlling an HD44780-compatible character LCD controller.
mcp4725
Package mcp4725 allows interfacing with the MCP4725 DAC.
Package mcp4725 allows interfacing with the MCP4725 DAC.
pca9685
Package pca9685 allows interfacing with the pca9685 16-channel, 12-bit PWM Controller through I2C protocol.
Package pca9685 allows interfacing with the pca9685 16-channel, 12-bit PWM Controller through I2C protocol.
servoblaster
Package servoblaster allows interfacing with the software servoblaster driver.
Package servoblaster allows interfacing with the software servoblaster driver.
Package convertors contains the various convertor modules for use on your platform.
Package convertors contains the various convertor modules for use on your platform.
mcp3008
Package mcp3008 allows interfacing with the mcp3008 8-channel, 10-bit ADC through SPI protocol.
Package mcp3008 allows interfacing with the mcp3008 8-channel, 10-bit ADC through SPI protocol.
Package host is a container for the various hosts supported by EMBD.
Package host is a container for the various hosts supported by EMBD.
all
Package all conviniently loads all the inbuilt/supported host drivers.
Package all conviniently loads all the inbuilt/supported host drivers.
bbb
Package bbb provides BeagleBone Black support.
Package bbb provides BeagleBone Black support.
generic
Package generic provides generic (to Linux) drivers for functionalities like Digital I/O I²C LED control They are used by the hosts to satiate the HAL.
Package generic provides generic (to Linux) drivers for functionalities like Digital I/O I²C LED control They are used by the hosts to satiate the HAL.
rpi
Package rpi provides Raspberry Pi (including A+/B+) support.
Package rpi provides Raspberry Pi (including A+/B+) support.
interface
display/characterdisplay
Package characterdisplay provides an ease-of-use layer on top of a character display controller.
Package characterdisplay provides an ease-of-use layer on top of a character display controller.
keypad/matrix4x3
Package matrix4x3 allows interfacing 4x3 keypad with Raspberry pi.
Package matrix4x3 allows interfacing 4x3 keypad with Raspberry pi.
motion
servo
Package servo allows control of servos using a PWM controller.
Package servo allows control of servos using a PWM controller.
Package sensor contains the various sensors modules for use on your platform.
Package sensor contains the various sensors modules for use on your platform.
bh1750fvi
Package BH1750FVI allows interfacing with the BH1750FVI ambient light sensor through I2C.
Package BH1750FVI allows interfacing with the BH1750FVI ambient light sensor through I2C.
bmp085
Package bmp085 allows interfacing with Bosch BMP085 barometric pressure sensor.
Package bmp085 allows interfacing with Bosch BMP085 barometric pressure sensor.
bmp180
Package bmp180 allows interfacing with Bosch BMP180 barometric pressure sensor.
Package bmp180 allows interfacing with Bosch BMP180 barometric pressure sensor.
l3gd20
Package l3gd20 allows interacting with L3GD20 gyroscoping sensor.
Package l3gd20 allows interacting with L3GD20 gyroscoping sensor.
lsm303
Package lsm303 allows interfacing with the LSM303 magnetometer.
Package lsm303 allows interfacing with the LSM303 magnetometer.
tmp006
Package tmp006 allows interfacing with the TMP006 thermopile.
Package tmp006 allows interfacing with the TMP006 thermopile.
us020
Package us020 allows interfacing with the US020 ultrasonic range finder.
Package us020 allows interfacing with the US020 ultrasonic range finder.
watersensor
Package watersensor allows interfacing with the water sensor.
Package watersensor allows interfacing with the water sensor.
Package util contains utility functions.
Package util contains utility functions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL