mirror of
https://github.com/kidoman/embd
synced 2024-06-13 14:19:52 +02:00
Merge 62497f2eb9
into bfcd1345fe
This commit is contained in:
commit
77e4c7d70e
|
@ -3,7 +3,7 @@
|
||||||
This actually is really simple. A few simple guidelines and we can break for dinner:
|
This actually is really simple. A few simple guidelines and we can break for dinner:
|
||||||
|
|
||||||
* EMBD is designed with a lot of affection, with utmost importance given to the dev experience (read: the API feel and style.) So always think from that angle when creating the pull request
|
* EMBD is designed with a lot of affection, with utmost importance given to the dev experience (read: the API feel and style.) So always think from that angle when creating the pull request
|
||||||
* [Documentation](https://godoc.org/github.com/kidoman/embd) helps drive adoption. No exceptions
|
* [Documentation](https://godoc.org/github.com/zlowred/embd) helps drive adoption. No exceptions
|
||||||
|
|
||||||
When it comes to the code:
|
When it comes to the code:
|
||||||
|
|
||||||
|
|
86
README.md
86
README.md
|
@ -1,4 +1,4 @@
|
||||||
# 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)
|
# embd [![Build Status](https://travis-ci.org/kidoman/embd.svg?branch=master)](https://travis-ci.org/kidoman/embd) [![GoDoc](http://godoc.org/github.com/zlowred/embd?status.png)](http://godoc.org/github.com/zlowred/embd)
|
||||||
|
|
||||||
**embd** is a hardware abstraction layer (HAL) for embedded systems.
|
**embd** is a hardware abstraction layer (HAL) for embedded systems.
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ It allows you to start your hardware hack on easily available hobby boards (like
|
||||||
|
|
||||||
Development supported and sponsored by [**SoStronk**](https://www.sostronk.com) and [**ThoughtWorks**](http://www.thoughtworks.com/)
|
Development supported and sponsored by [**SoStronk**](https://www.sostronk.com) and [**ThoughtWorks**](http://www.thoughtworks.com/)
|
||||||
|
|
||||||
Also, you might be interested in: [Why Golang?](https://github.com/kidoman/embd/wiki/Why-Go)
|
Also, you might be interested in: [Why Golang?](https://github.com/zlowred/embd/wiki/Why-Go)
|
||||||
|
|
||||||
[Blog post introducing EMBD](http://kidoman.io/framework/embd.html)
|
[Blog post introducing EMBD](http://kidoman.io/framework/embd.html)
|
||||||
|
|
||||||
|
@ -20,8 +20,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
_ "github.com/kidoman/embd/host/rpi" // This loads the RPi driver
|
_ "github.com/zlowred/embd/host/rpi" // This loads the RPi driver
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -34,7 +34,7 @@ func main() {
|
||||||
|
|
||||||
Then install the EMBD package (go1.2 and greater is required):
|
Then install the EMBD package (go1.2 and greater is required):
|
||||||
|
|
||||||
$ go get github.com/kidoman/embd
|
$ go get github.com/zlowred/embd
|
||||||
|
|
||||||
Build the binary*:
|
Build the binary*:
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ Then run the program with ```sudo```*:
|
||||||
* We are instructing the ```go``` compiler to create a binary which will run on the RaspberryPi processor
|
* We are instructing the ```go``` compiler to create a binary which will run on the RaspberryPi processor
|
||||||
* Assuming your RaspberryPi has an IP address of ```192.168.2.2```. Substitute as necessary
|
* 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
|
* ```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](https://github.com/kidoman/embd/blob/master/samples/fullblinker.go)
|
* This sample program is optimized for brevity and does not clean up after itself. Click here to see the [full version](https://github.com/zlowred/embd/blob/master/samples/fullblinker.go)
|
||||||
|
|
||||||
## Getting Help
|
## Getting Help
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ Join the [mailing list](https://groups.google.com/forum/#!forum/go-embd)
|
||||||
|
|
||||||
## The command line tool
|
## The command line tool
|
||||||
|
|
||||||
go get github.com/kidoman/embd/embd
|
go get github.com/zlowred/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.
|
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.
|
||||||
|
|
||||||
|
@ -96,14 +96,14 @@ Package **embd** provides a hardware abstraction layer for doing embedded progra
|
||||||
on supported platforms like the Raspberry Pi and BeagleBone Black. Most of the examples below
|
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?
|
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,
|
Although samples are all present in the [samples](https://github.com/zlowred/embd/tree/master/samples) folder,
|
||||||
we will show a few choice examples here.
|
we will show a few choice examples here.
|
||||||
|
|
||||||
Use the **LED** driver to toggle LEDs on the BBB:
|
Use the **LED** driver to toggle LEDs on the BBB:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/kidoman/embd"
|
import "github.com/zlowred/embd"
|
||||||
import _ "github.com/kidoman/embd/host/all"
|
import _ "github.com/zlowred/embd/host/all"
|
||||||
...
|
...
|
||||||
embd.InitLED()
|
embd.InitLED()
|
||||||
defer embd.CloseLED()
|
defer embd.CloseLED()
|
||||||
|
@ -116,8 +116,8 @@ led.Toggle()
|
||||||
Even shorter when quickly trying things out:
|
Even shorter when quickly trying things out:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/kidoman/embd"
|
import "github.com/zlowred/embd"
|
||||||
import _ "github.com/kidoman/embd/host/all"
|
import _ "github.com/zlowred/embd/host/all"
|
||||||
...
|
...
|
||||||
embd.InitLED()
|
embd.InitLED()
|
||||||
defer embd.CloseLED()
|
defer embd.CloseLED()
|
||||||
|
@ -130,8 +130,8 @@ embd.ToggleLED(3)
|
||||||
BBB + **PWM**:
|
BBB + **PWM**:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/kidoman/embd"
|
import "github.com/zlowred/embd"
|
||||||
import _ "github.com/kidoman/embd/host/all"
|
import _ "github.com/zlowred/embd/host/all"
|
||||||
...
|
...
|
||||||
embd.InitGPIO()
|
embd.InitGPIO()
|
||||||
defer embd.CloseGPIO()
|
defer embd.CloseGPIO()
|
||||||
|
@ -145,8 +145,8 @@ pwm.SetDuty(1000)
|
||||||
Control **GPIO** pins on the RaspberryPi / BeagleBone Black:
|
Control **GPIO** pins on the RaspberryPi / BeagleBone Black:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/kidoman/embd"
|
import "github.com/zlowred/embd"
|
||||||
import _ "github.com/kidoman/embd/host/all"
|
import _ "github.com/zlowred/embd/host/all"
|
||||||
...
|
...
|
||||||
embd.InitGPIO()
|
embd.InitGPIO()
|
||||||
defer embd.CloseGPIO()
|
defer embd.CloseGPIO()
|
||||||
|
@ -158,8 +158,8 @@ embd.DigitalWrite(10, embd.High)
|
||||||
Could also do:
|
Could also do:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/kidoman/embd"
|
import "github.com/zlowred/embd"
|
||||||
import _ "github.com/kidoman/embd/host/all"
|
import _ "github.com/zlowred/embd/host/all"
|
||||||
...
|
...
|
||||||
embd.InitGPIO()
|
embd.InitGPIO()
|
||||||
defer embd.CloseGPIO()
|
defer embd.CloseGPIO()
|
||||||
|
@ -173,9 +173,9 @@ pin.Write(embd.High)
|
||||||
Or read data from the **Bosch BMP085** barometric sensor:
|
Or read data from the **Bosch BMP085** barometric sensor:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/kidoman/embd"
|
import "github.com/zlowred/embd"
|
||||||
import "github.com/kidoman/embd/sensor/bmp085"
|
import "github.com/zlowred/embd/sensor/bmp085"
|
||||||
import _ "github.com/kidoman/embd/host/all"
|
import _ "github.com/zlowred/embd/host/all"
|
||||||
...
|
...
|
||||||
bus := embd.NewI2CBus(1)
|
bus := embd.NewI2CBus(1)
|
||||||
...
|
...
|
||||||
|
@ -188,9 +188,9 @@ altitude, err := baro.Altitude()
|
||||||
Even find out the heading from the **LSM303** magnetometer:
|
Even find out the heading from the **LSM303** magnetometer:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/kidoman/embd"
|
import "github.com/zlowred/embd"
|
||||||
import "github.com/kidoman/embd/sensor/lsm303"
|
import "github.com/zlowred/embd/sensor/lsm303"
|
||||||
import _ "github.com/kidoman/embd/host/all"
|
import _ "github.com/zlowred/embd/host/all"
|
||||||
...
|
...
|
||||||
bus := embd.NewI2CBus(1)
|
bus := embd.NewI2CBus(1)
|
||||||
...
|
...
|
||||||
|
@ -204,28 +204,28 @@ platforms.
|
||||||
|
|
||||||
## Protocols Supported
|
## Protocols Supported
|
||||||
|
|
||||||
* **Digital GPIO** [Documentation](http://godoc.org/github.com/kidoman/embd#DigitalPin)
|
* **Digital GPIO** [Documentation](http://godoc.org/github.com/zlowred/embd#DigitalPin)
|
||||||
* **Analog GPIO** [Documentation](http://godoc.org/github.com/kidoman/embd#AnalogPin)
|
* **Analog GPIO** [Documentation](http://godoc.org/github.com/zlowred/embd#AnalogPin)
|
||||||
* **PWM** [Documentation](http://godoc.org/github.com/kidoman/embd#PWMPin)
|
* **PWM** [Documentation](http://godoc.org/github.com/zlowred/embd#PWMPin)
|
||||||
* **I2C** [Documentation](http://godoc.org/github.com/kidoman/embd#I2CBus)
|
* **I2C** [Documentation](http://godoc.org/github.com/zlowred/embd#I2CBus)
|
||||||
* **LED** [Documentation](http://godoc.org/github.com/kidoman/embd#LED)
|
* **LED** [Documentation](http://godoc.org/github.com/zlowred/embd#LED)
|
||||||
* **SPI** [Documentation](http://godoc.org/github.com/kidoman/embd#SPIBus)
|
* **SPI** [Documentation](http://godoc.org/github.com/zlowred/embd#SPIBus)
|
||||||
|
|
||||||
## Sensors Supported
|
## Sensors Supported
|
||||||
|
|
||||||
* **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)
|
* **TMP006** Thermopile sensor [Documentation](http://godoc.org/github.com/zlowred/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)
|
* **BMP085** Barometric pressure sensor [Documentation](http://godoc.org/github.com/zlowred/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)
|
* **BMP180** Barometric pressure sensor [Documentation](http://godoc.org/github.com/zlowred/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)
|
* **LSM303** Accelerometer and magnetometer [Documentation](http://godoc.org/github.com/zlowred/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)
|
* **L3GD20** Gyroscope [Documentation](http://godoc.org/github.com/zlowred/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)
|
* **US020** Ultrasonic proximity sensor [Documentation](http://godoc.org/github.com/zlowred/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)
|
* **BH1750FVI** Luminosity sensor [Documentation](http://godoc.org/github.com/zlowred/embd/sensor/bh1750fvi), [Datasheet](http://www.elechouse.com/elechouse/images/product/Digital%20light%20Sensor/bh1750fvi-e.pdf)
|
||||||
|
|
||||||
## Interfaces
|
## Interfaces
|
||||||
|
|
||||||
|
@ -233,11 +233,11 @@ platforms.
|
||||||
|
|
||||||
## Controllers
|
## Controllers
|
||||||
|
|
||||||
* **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)
|
* **PCA9685** 16-channel, 12-bit PWM Controller with I2C protocol [Documentation](http://godoc.org/github.com/zlowred/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)
|
* **MCP4725** 12-bit DAC [Documentation](http://godoc.org/github.com/zlowred/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)
|
* **ServoBlaster** RPi PWM/PCM based PWM controller [Documentation](http://godoc.org/github.com/zlowred/embd/controller/servoblaster), [Product Page](https://github.com/richardghirst/PiBits/tree/master/ServoBlaster)
|
||||||
|
|
||||||
## Convertors
|
## Convertors
|
||||||
|
|
||||||
|
@ -245,10 +245,10 @@ platforms.
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
We look forward to your pull requests, but contributions which abide by the [guidelines](https://github.com/kidoman/embd/blob/master/CONTRIBUTING.md) will get a free beer!
|
We look forward to your pull requests, but contributions which abide by the [guidelines](https://github.com/zlowred/embd/blob/master/CONTRIBUTING.md) will get a free beer!
|
||||||
|
|
||||||
File an [issue](https://github.com/kidoman/embd/issues), open a [pull request](https://github.com/kidoman/embd/pulls). We are waiting.
|
File an [issue](https://github.com/zlowred/embd/issues), open a [pull request](https://github.com/zlowred/embd/pulls). We are waiting.
|
||||||
|
|
||||||
## About
|
## About
|
||||||
|
|
||||||
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).
|
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/zlowred/embd/blob/master/AUTHORS). We also have a list of [CONTRIBUTORS](https://github.com/zlowred/embd/blob/master/CONTRIBUTORS).
|
||||||
|
|
|
@ -16,7 +16,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
type entryMode byte
|
type entryMode byte
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -131,6 +131,7 @@ type mockI2CBus struct {
|
||||||
closed bool
|
closed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bus *mockI2CBus) ReadBytes(addr byte, num int) ([]byte, error) { return []byte{0x00}, nil }
|
||||||
func (bus *mockI2CBus) ReadByte(addr byte) (byte, error) { return 0x00, nil }
|
func (bus *mockI2CBus) ReadByte(addr byte) (byte, error) { return 0x00, nil }
|
||||||
func (bus *mockI2CBus) WriteBytes(addr byte, value []byte) error { return nil }
|
func (bus *mockI2CBus) WriteBytes(addr byte, value []byte) error { return nil }
|
||||||
func (bus *mockI2CBus) ReadFromReg(addr, reg byte, value []byte) error { return nil }
|
func (bus *mockI2CBus) ReadFromReg(addr, reg byte, value []byte) error { return nil }
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -7,8 +7,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/util"
|
"github.com/zlowred/embd/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -3,7 +3,7 @@ package mcp3008
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MCP3008 represents a mcp3008 8bit DAC.
|
// MCP3008 represents a mcp3008 8bit DAC.
|
||||||
|
|
|
@ -15,6 +15,7 @@ type Descriptor struct {
|
||||||
I2CDriver func() I2CDriver
|
I2CDriver func() I2CDriver
|
||||||
LEDDriver func() LEDDriver
|
LEDDriver func() LEDDriver
|
||||||
SPIDriver func() SPIDriver
|
SPIDriver func() SPIDriver
|
||||||
|
W1Driver func() W1Driver
|
||||||
}
|
}
|
||||||
|
|
||||||
// The Describer type is a Descriptor provider.
|
// The Describer type is a Descriptor provider.
|
||||||
|
|
|
@ -122,6 +122,6 @@ func DetectHost() (host Host, rev int, err error) {
|
||||||
case strings.Contains(hardware, "BCM2708") || strings.Contains(hardware, "BCM2709"):
|
case strings.Contains(hardware, "BCM2708") || strings.Contains(hardware, "BCM2709"):
|
||||||
return HostRPi, rev, nil
|
return HostRPi, rev, nil
|
||||||
default:
|
default:
|
||||||
return HostNull, 0, fmt.Errorf(`embd: your host "%v:%v" is not supported at this moment. request support at https://github.com/kidoman/embd/issues`, host, model)
|
return HostNull, 0, fmt.Errorf(`embd: your host "%v:%v" is not supported at this moment. request support at https://github.com/zlowred/embd/issues`, host, model)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
18
doc.go
18
doc.go
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
Use the LED driver to toggle LEDs on the BBB:
|
Use the LED driver to toggle LEDs on the BBB:
|
||||||
|
|
||||||
import "github.com/kidoman/embd"
|
import "github.com/zlowred/embd"
|
||||||
...
|
...
|
||||||
embd.InitLED()
|
embd.InitLED()
|
||||||
defer embd.CloseLED()
|
defer embd.CloseLED()
|
||||||
|
@ -18,7 +18,7 @@
|
||||||
|
|
||||||
Even shorter while prototyping:
|
Even shorter while prototyping:
|
||||||
|
|
||||||
import "github.com/kidoman/embd"
|
import "github.com/zlowred/embd"
|
||||||
...
|
...
|
||||||
embd.InitLED()
|
embd.InitLED()
|
||||||
defer embd.CloseLED()
|
defer embd.CloseLED()
|
||||||
|
@ -27,7 +27,7 @@
|
||||||
|
|
||||||
BBB + PWM:
|
BBB + PWM:
|
||||||
|
|
||||||
import "github.com/kidoman/embd"
|
import "github.com/zlowred/embd"
|
||||||
...
|
...
|
||||||
embd.InitGPIO()
|
embd.InitGPIO()
|
||||||
defer embd.CloseGPIO()
|
defer embd.CloseGPIO()
|
||||||
|
@ -39,7 +39,7 @@
|
||||||
|
|
||||||
Control GPIO pins on the RaspberryPi / BeagleBone Black:
|
Control GPIO pins on the RaspberryPi / BeagleBone Black:
|
||||||
|
|
||||||
import "github.com/kidoman/embd"
|
import "github.com/zlowred/embd"
|
||||||
...
|
...
|
||||||
embd.InitGPIO()
|
embd.InitGPIO()
|
||||||
defer embd.CloseGPIO()
|
defer embd.CloseGPIO()
|
||||||
|
@ -49,7 +49,7 @@
|
||||||
|
|
||||||
Could also do:
|
Could also do:
|
||||||
|
|
||||||
import "github.com/kidoman/embd"
|
import "github.com/zlowred/embd"
|
||||||
...
|
...
|
||||||
embd.InitGPIO()
|
embd.InitGPIO()
|
||||||
defer embd.CloseGPIO()
|
defer embd.CloseGPIO()
|
||||||
|
@ -61,8 +61,8 @@
|
||||||
|
|
||||||
Or read data from the Bosch BMP085 barometric sensor:
|
Or read data from the Bosch BMP085 barometric sensor:
|
||||||
|
|
||||||
import "github.com/kidoman/embd"
|
import "github.com/zlowred/embd"
|
||||||
import "github.com/kidoman/embd/sensor/bmp085"
|
import "github.com/zlowred/embd/sensor/bmp085"
|
||||||
...
|
...
|
||||||
bus := embd.NewI2CBus(1)
|
bus := embd.NewI2CBus(1)
|
||||||
...
|
...
|
||||||
|
@ -73,8 +73,8 @@
|
||||||
|
|
||||||
Even find out the heading from the LSM303 magnetometer:
|
Even find out the heading from the LSM303 magnetometer:
|
||||||
|
|
||||||
import "github.com/kidoman/embd"
|
import "github.com/zlowred/embd"
|
||||||
import "github.com/kidoman/embd/sensor/lsm303"
|
import "github.com/zlowred/embd/sensor/lsm303"
|
||||||
...
|
...
|
||||||
bus := embd.NewI2CBus(1)
|
bus := embd.NewI2CBus(1)
|
||||||
...
|
...
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/codegangsta/cli"
|
"github.com/codegangsta/cli"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func detect(c *cli.Context) {
|
func detect(c *cli.Context) {
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/codegangsta/cli"
|
"github.com/codegangsta/cli"
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
var version = "0.1.0"
|
var version = "0.1.0"
|
||||||
|
|
|
@ -2,6 +2,6 @@
|
||||||
package all
|
package all
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "github.com/kidoman/embd/host/bbb"
|
_ "github.com/zlowred/embd/host/bbb"
|
||||||
_ "github.com/kidoman/embd/host/rpi"
|
_ "github.com/zlowred/embd/host/rpi"
|
||||||
)
|
)
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
type analogPin struct {
|
type analogPin struct {
|
||||||
|
|
|
@ -3,7 +3,7 @@ package bbb
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAnalogPinClose(t *testing.T) {
|
func TestAnalogPinClose(t *testing.T) {
|
||||||
|
|
|
@ -16,8 +16,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/host/generic"
|
"github.com/zlowred/embd/host/generic"
|
||||||
)
|
)
|
||||||
|
|
||||||
var pins = embd.PinMap{
|
var pins = embd.PinMap{
|
||||||
|
|
|
@ -11,8 +11,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/util"
|
"github.com/zlowred/embd/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -3,7 +3,7 @@ package bbb
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPWMPinClose(t *testing.T) {
|
func TestPWMPinClose(t *testing.T) {
|
||||||
|
|
|
@ -12,7 +12,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
type digitalPin struct {
|
type digitalPin struct {
|
||||||
|
|
|
@ -3,7 +3,7 @@ package generic
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDigitalPinClose(t *testing.T) {
|
func TestDigitalPinClose(t *testing.T) {
|
||||||
|
|
|
@ -12,7 +12,7 @@ import (
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -101,6 +101,28 @@ func (b *i2cBus) ReadByte(addr byte) (byte, error) {
|
||||||
return bytes[0], nil
|
return bytes[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *i2cBus) ReadBytes(addr byte, num int) ([]byte, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
if err := b.init(); err != nil {
|
||||||
|
return []byte{0}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := b.setAddress(addr); err != nil {
|
||||||
|
return []byte{0}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes := make([]byte, num)
|
||||||
|
n, _ := b.file.Read(bytes)
|
||||||
|
|
||||||
|
if n != num {
|
||||||
|
return []byte{0}, fmt.Errorf("i2c: Unexpected number (%v) of bytes read", n)
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (b *i2cBus) WriteByte(addr, value byte) error {
|
func (b *i2cBus) WriteByte(addr, value byte) error {
|
||||||
b.mu.Lock()
|
b.mu.Lock()
|
||||||
defer b.mu.Unlock()
|
defer b.mu.Unlock()
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
type led struct {
|
type led struct {
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
191
host/rpi/onewirebus.go
Normal file
191
host/rpi/onewirebus.go
Normal file
|
@ -0,0 +1,191 @@
|
||||||
|
// I²C support.
|
||||||
|
|
||||||
|
package rpi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/golang/glog"
|
||||||
|
"github.com/zlowred/embd"
|
||||||
|
"io/ioutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
type w1Bus struct {
|
||||||
|
l byte
|
||||||
|
busMap map[string]embd.W1Device
|
||||||
|
Mu sync.Mutex
|
||||||
|
|
||||||
|
initialized bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type w1Device struct {
|
||||||
|
file *os.File
|
||||||
|
addr string
|
||||||
|
initialized bool
|
||||||
|
bus w1Bus
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewW1Bus(l byte) embd.W1Bus {
|
||||||
|
fmt.Println("new w1 bus")
|
||||||
|
return &w1Bus{l: l, busMap: make(map[string]embd.W1Device)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *w1Bus) init() error {
|
||||||
|
if b.initialized {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
if _, err = os.Stat("/sys/bus/w1"); os.IsNotExist(err) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
glog.V(2).Infof("onewire: bus %v initialized", b.l)
|
||||||
|
|
||||||
|
b.initialized = true
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *w1Device) init() error {
|
||||||
|
if d.initialized {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
if d.file, err = os.OpenFile(fmt.Sprintf("/sys/bus/w1/devices/%s/rw", d.addr), os.O_RDWR, os.ModeExclusive); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
glog.V(2).Infof("onewire: device %s initialized", d.addr)
|
||||||
|
|
||||||
|
d.initialized = true
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *w1Device) ReadByte() (byte, error) {
|
||||||
|
d.bus.Mu.Lock()
|
||||||
|
defer d.bus.Mu.Unlock()
|
||||||
|
|
||||||
|
if err := d.init(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes := make([]byte, 1)
|
||||||
|
n, _ := d.file.Read(bytes)
|
||||||
|
|
||||||
|
if n != 1 {
|
||||||
|
return 0, fmt.Errorf("onewire: Unexpected number (%v) of bytes read in ReadByte", n)
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *w1Device) WriteByte(value byte) error {
|
||||||
|
d.bus.Mu.Lock()
|
||||||
|
defer d.bus.Mu.Unlock()
|
||||||
|
|
||||||
|
if err := d.init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err := d.file.Write([]byte{value})
|
||||||
|
|
||||||
|
if n != 1 {
|
||||||
|
err = fmt.Errorf("onewire: Unexpected number (%v) of bytes written in WriteByte", n)
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *w1Device) WriteBytes(value []byte) error {
|
||||||
|
d.bus.Mu.Lock()
|
||||||
|
defer d.bus.Mu.Unlock()
|
||||||
|
|
||||||
|
if err := d.init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range value {
|
||||||
|
n, err := d.file.Write([]byte{value[i]})
|
||||||
|
|
||||||
|
if n != 1 {
|
||||||
|
return fmt.Errorf("onewire: Unexpected number (%v) of bytes written in WriteBytes", n)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *w1Device) ReadBytes(number int) (value []byte, err error) {
|
||||||
|
d.bus.Mu.Lock()
|
||||||
|
defer d.bus.Mu.Unlock()
|
||||||
|
|
||||||
|
if err := d.init(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes := make([]byte, number)
|
||||||
|
n, _ := d.file.Read(bytes)
|
||||||
|
|
||||||
|
if n != number {
|
||||||
|
return nil, fmt.Errorf("onewire: Unexpected number (%v) of bytes read in ReadBytes", n)
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *w1Bus) ListDevices() (devices []string, err error) {
|
||||||
|
dir, err := ioutil.ReadDir("/sys/bus/w1/devices/")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
devs := make([]string, len(dir))
|
||||||
|
|
||||||
|
for index, element := range dir {
|
||||||
|
devs[index] = element.Name()
|
||||||
|
}
|
||||||
|
|
||||||
|
return devs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *w1Bus) Open(address string) (device embd.W1Device, err error) {
|
||||||
|
b.Mu.Lock()
|
||||||
|
defer b.Mu.Unlock()
|
||||||
|
|
||||||
|
if d, ok := b.busMap[address]; ok {
|
||||||
|
return d, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
d := &w1Device{addr: address, bus: b}
|
||||||
|
b.busMap[address] = d
|
||||||
|
return d, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *w1Bus) Close() error {
|
||||||
|
b.Mu.Lock()
|
||||||
|
defer b.Mu.Unlock()
|
||||||
|
|
||||||
|
for _, b := range b.busMap {
|
||||||
|
b.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *w1Device) Close() error {
|
||||||
|
d.bus.Mu.Lock()
|
||||||
|
defer d.bus.Mu.Unlock()
|
||||||
|
|
||||||
|
if !d.initialized {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return d.file.Close()
|
||||||
|
}
|
|
@ -5,12 +5,14 @@
|
||||||
GPIO (digital (rw))
|
GPIO (digital (rw))
|
||||||
I²C
|
I²C
|
||||||
LED
|
LED
|
||||||
|
W1 - make sure that w1-gpio kernel module is loaded. If you wish to use it with sensors directly (e.g. DS18B20 etc)
|
||||||
|
make sure to disable respective kernel modules (e.g. w1-therm) in /etc/modprobe.d/blacklist.conf.
|
||||||
*/
|
*/
|
||||||
package rpi
|
package rpi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/host/generic"
|
"github.com/zlowred/embd/host/generic"
|
||||||
)
|
)
|
||||||
|
|
||||||
var spiDeviceMinor = byte(0)
|
var spiDeviceMinor = byte(0)
|
||||||
|
@ -97,6 +99,9 @@ func init() {
|
||||||
SPIDriver: func() embd.SPIDriver {
|
SPIDriver: func() embd.SPIDriver {
|
||||||
return embd.NewSPIDriver(spiDeviceMinor, generic.NewSPIBus, nil)
|
return embd.NewSPIDriver(spiDeviceMinor, generic.NewSPIBus, nil)
|
||||||
},
|
},
|
||||||
|
W1Driver: func() embd.W1Driver {
|
||||||
|
return embd.NewW1Driver(NewW1Bus)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
2
i2c.go
2
i2c.go
|
@ -6,6 +6,8 @@ package embd
|
||||||
type I2CBus interface {
|
type I2CBus interface {
|
||||||
// ReadByte reads a byte from the given address.
|
// ReadByte reads a byte from the given address.
|
||||||
ReadByte(addr byte) (value byte, err error)
|
ReadByte(addr byte) (value byte, err error)
|
||||||
|
// ReadBytes reads a slice of bytes from the given address.
|
||||||
|
ReadBytes(addr byte, num int) (value []byte, err error)
|
||||||
// WriteByte writes a byte to the given address.
|
// WriteByte writes a byte to the given address.
|
||||||
WriteByte(addr, value byte) error
|
WriteByte(addr, value byte) error
|
||||||
// WriteBytes writes a slice bytes to the given address.
|
// WriteBytes writes a slice bytes to the given address.
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Key int
|
type Key int
|
||||||
|
|
|
@ -3,7 +3,7 @@ package servo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/kidoman/embd/util"
|
"github.com/zlowred/embd/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
78
onewire.go
Normal file
78
onewire.go
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
//OneWire support.
|
||||||
|
|
||||||
|
package embd
|
||||||
|
|
||||||
|
// W1Bus interface is used to interact with the OneWire bus.
|
||||||
|
type W1Bus interface {
|
||||||
|
|
||||||
|
// List devices on the bus
|
||||||
|
ListDevices() (devices []string, err error)
|
||||||
|
|
||||||
|
// Open a device
|
||||||
|
Open(address string) (device W1Device, err error)
|
||||||
|
|
||||||
|
// Close releases the resources associated with the bus.
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
|
||||||
|
// W1Device interface is user to interact with the OneWire device.
|
||||||
|
type W1Device interface {
|
||||||
|
// ReadByte reads a byte from the device.
|
||||||
|
ReadByte() (value byte, err error)
|
||||||
|
// ReadByte number of bytes from the device.
|
||||||
|
ReadBytes(number int) (value []byte, err error)
|
||||||
|
// WriteByte writes a byte to the device.
|
||||||
|
WriteByte(value byte) error
|
||||||
|
// WriteBytes writes a slice bytes to the device.
|
||||||
|
WriteBytes(value []byte) error
|
||||||
|
|
||||||
|
// Close releases the resources associated with the device.
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
|
||||||
|
// W1Driver interface interacts with the host descriptors to allow us
|
||||||
|
// control of OneWire communication.
|
||||||
|
type W1Driver interface {
|
||||||
|
Bus(l byte) W1Bus
|
||||||
|
|
||||||
|
// Close releases the resources associated with the driver.
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
|
||||||
|
var w1DriverInitialized bool
|
||||||
|
var w1DriverInstance W1Driver
|
||||||
|
|
||||||
|
// InitW1 initializes the W1 driver.
|
||||||
|
func InitW1() error {
|
||||||
|
if w1DriverInitialized {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
desc, err := DescribeHost()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if desc.W1Driver == nil {
|
||||||
|
return ErrFeatureNotSupported
|
||||||
|
}
|
||||||
|
|
||||||
|
w1DriverInstance = desc.W1Driver()
|
||||||
|
w1DriverInitialized = true
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CloseW1 releases resources associated with the OneWire driver.
|
||||||
|
func CloseW1() error {
|
||||||
|
return w1DriverInstance.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewW1Bus returns a W1Bus.
|
||||||
|
func NewW1Bus(l byte) W1Bus {
|
||||||
|
if err := InitW1(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return w1DriverInstance.Bus(l)
|
||||||
|
}
|
44
onewiredriver.go
Normal file
44
onewiredriver.go
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
// Generic OneWire driver.
|
||||||
|
|
||||||
|
package embd
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
type w1BusFactory func(byte) W1Bus
|
||||||
|
|
||||||
|
type w1Driver struct {
|
||||||
|
busMap map[byte]W1Bus
|
||||||
|
busMapLock sync.Mutex
|
||||||
|
|
||||||
|
ibf w1BusFactory
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewW1Driver returns a W1Driver interface which allows control
|
||||||
|
// over the OneWire subsystem.
|
||||||
|
func NewW1Driver(ibf w1BusFactory) W1Driver {
|
||||||
|
return &w1Driver{
|
||||||
|
busMap: make(map[byte]W1Bus),
|
||||||
|
ibf: ibf,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *w1Driver) Bus(l byte) W1Bus {
|
||||||
|
i.busMapLock.Lock()
|
||||||
|
defer i.busMapLock.Unlock()
|
||||||
|
|
||||||
|
if b, ok := i.busMap[l]; ok {
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
b := i.ibf(l)
|
||||||
|
i.busMap[l] = b
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *w1Driver) Close() error {
|
||||||
|
for _, b := range i.busMap {
|
||||||
|
b.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -18,8 +18,8 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
_ "github.com/kidoman/embd/host/rpi"
|
_ "github.com/zlowred/embd/host/rpi"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -9,9 +9,9 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -6,9 +6,9 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -7,10 +7,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/sensor/bh1750fvi"
|
"github.com/zlowred/embd/sensor/bh1750fvi"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -7,10 +7,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/sensor/bmp085"
|
"github.com/zlowred/embd/sensor/bmp085"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -7,10 +7,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/sensor/bmp180"
|
"github.com/zlowred/embd/sensor/bmp180"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -6,11 +6,11 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/controller/hd44780"
|
"github.com/zlowred/embd/controller/hd44780"
|
||||||
"github.com/kidoman/embd/interface/display/characterdisplay"
|
"github.com/zlowred/embd/interface/display/characterdisplay"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
78
samples/ds18b20.go
Normal file
78
samples/ds18b20.go
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
// +build ignore
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/zlowred/embd"
|
||||||
|
"github.com/zlowred/embd/sensor/ds18b20"
|
||||||
|
_ "github.com/zlowred/embd/host/rpi"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := embd.InitW1(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer embd.CloseW1()
|
||||||
|
|
||||||
|
w1 := embd.NewW1Bus(0)
|
||||||
|
|
||||||
|
devs, err := w1.ListDevices()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var name string = ""
|
||||||
|
for _, dev := range devs {
|
||||||
|
if strings.HasPrefix(dev, "28-") {
|
||||||
|
name = dev
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if name == "" {
|
||||||
|
fmt.Println("No DS18B20 devices found")
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Using DS18B20 device %s\n", name)
|
||||||
|
w1d, err := w1.Open(name)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sensor := ds18b20.New(w1d)
|
||||||
|
|
||||||
|
err = sensor.SetResolution(ds18b20.Resolution_12bit)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = sensor.ReadTemperature()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Measured temperature: %vC\n", sensor.Celsius())
|
||||||
|
fmt.Printf("Measured temperature: %vF\n", sensor.Fahrenheit())
|
||||||
|
|
||||||
|
err = sensor.SetResolution(ds18b20.Resolution_9bit)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = sensor.ReadTemperature()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Measured temperature: %vC\n", sensor.Celsius())
|
||||||
|
fmt.Printf("Measured temperature: %vF\n", sensor.Fahrenheit())
|
||||||
|
}
|
|
@ -11,8 +11,8 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
_ "github.com/kidoman/embd/host/rpi"
|
_ "github.com/zlowred/embd/host/rpi"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -6,9 +6,9 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -6,9 +6,9 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -6,9 +6,9 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -6,9 +6,9 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -5,9 +5,9 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -6,10 +6,10 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/controller/hd44780"
|
"github.com/zlowred/embd/controller/hd44780"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -5,8 +5,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/interface/keypad/matrix4x3"
|
"github.com/zlowred/embd/interface/keypad/matrix4x3"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -9,10 +9,10 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/sensor/l3gd20"
|
"github.com/zlowred/embd/sensor/l3gd20"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -11,9 +11,9 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/bbb"
|
_ "github.com/zlowred/embd/host/bbb"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -8,9 +8,9 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/bbb"
|
_ "github.com/zlowred/embd/host/bbb"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -7,10 +7,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/sensor/lsm303"
|
"github.com/zlowred/embd/sensor/lsm303"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -8,9 +8,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/convertors/mcp3008"
|
"github.com/zlowred/embd/convertors/mcp3008"
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -9,10 +9,10 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/controller/mcp4725"
|
"github.com/zlowred/embd/controller/mcp4725"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
35
samples/npa700b001d.go
Normal file
35
samples/npa700b001d.go
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
// +build ignore
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/zlowred/embd"
|
||||||
|
"github.com/zlowred/embd/sensor/npa700"
|
||||||
|
|
||||||
|
_ "github.com/zlowred/embd/host/all"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if err := embd.InitI2C(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer embd.CloseI2C()
|
||||||
|
|
||||||
|
bus := embd.NewI2CBus(1)
|
||||||
|
|
||||||
|
sensor := npa700.New(bus)
|
||||||
|
|
||||||
|
err := sensor.Read()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Temp is %fC\n", sensor.Celsius())
|
||||||
|
fmt.Printf("Temp is %fF\n", sensor.Fahrenheit())
|
||||||
|
fmt.Printf("Pres is %fPa\n", sensor.Pascals(0, 1638, 14745, -6894.76, 6894.76))
|
||||||
|
}
|
72
samples/onewire.go
Normal file
72
samples/onewire.go
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
// +build ignore
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/zlowred/embd"
|
||||||
|
_ "github.com/zlowred/embd/host/all"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := embd.InitW1(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer embd.CloseW1()
|
||||||
|
|
||||||
|
w1 := embd.NewW1Bus(0)
|
||||||
|
|
||||||
|
devs, err := w1.ListDevices()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, dev := range devs {
|
||||||
|
fmt.Println("OneWire device: %s", dev)
|
||||||
|
}
|
||||||
|
|
||||||
|
w1d, err := w1.Open("28-011572120bff")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("%v\n", w1d)
|
||||||
|
|
||||||
|
err = w1d.WriteByte(0x44)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for ret, err := w1d.ReadByte(); ret == 0 && err != nil; {}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = w1d.WriteByte(0xBE)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := w1d.ReadBytes(9)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Print("res: ")
|
||||||
|
for _, val := range res {
|
||||||
|
fmt.Printf("0x%02X ", val)
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
var temp float64 = float64(float64(res[1]) * 256. + float64(res[0])) / 16.
|
||||||
|
fmt.Printf("%f\n", temp)
|
||||||
|
|
||||||
|
fmt.Println("Done")
|
||||||
|
}
|
|
@ -8,10 +8,10 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/controller/pca9685"
|
"github.com/zlowred/embd/controller/pca9685"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -8,10 +8,10 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/host/bbb"
|
"github.com/zlowred/embd/host/bbb"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -8,11 +8,11 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/controller/pca9685"
|
"github.com/zlowred/embd/controller/pca9685"
|
||||||
"github.com/kidoman/embd/motion/servo"
|
"github.com/zlowred/embd/motion/servo"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -8,10 +8,10 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/motion/servo"
|
"github.com/zlowred/embd/motion/servo"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -8,10 +8,10 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd/controller/servoblaster"
|
"github.com/zlowred/embd/controller/servoblaster"
|
||||||
"github.com/kidoman/embd/motion/servo"
|
"github.com/zlowred/embd/motion/servo"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -8,8 +8,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
_ "github.com/kidoman/embd/host/rpi" // This loads the RPi driver
|
_ "github.com/zlowred/embd/host/rpi" // This loads the RPi driver
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -5,8 +5,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -7,8 +7,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -8,10 +8,10 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/sensor/tmp006"
|
"github.com/zlowred/embd/sensor/tmp006"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -10,8 +10,8 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -9,10 +9,10 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/sensor/us020"
|
"github.com/zlowred/embd/sensor/us020"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -7,10 +7,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
"github.com/kidoman/embd/sensor/watersensor"
|
"github.com/zlowred/embd/sensor/watersensor"
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/all"
|
_ "github.com/zlowred/embd/host/all"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
//accuracy = sensorValue/actualValue] (min = 0.96, typ = 1.2, max = 1.44
|
//accuracy = sensorValue/actualValue] (min = 0.96, typ = 1.2, max = 1.44
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
146
sensor/ds18b20/ds18b20.go
Normal file
146
sensor/ds18b20/ds18b20.go
Normal file
|
@ -0,0 +1,146 @@
|
||||||
|
// Support for the very popular DS18B20 1-WIre temperature sensor
|
||||||
|
package ds18b20
|
||||||
|
import (
|
||||||
|
"github.com/zlowred/embd"
|
||||||
|
"errors"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DS18B20_Resolution int
|
||||||
|
|
||||||
|
const (
|
||||||
|
Resolution_9bit DS18B20_Resolution = iota
|
||||||
|
Resolution_10bit
|
||||||
|
Resolution_11bit
|
||||||
|
Resolution_12bit
|
||||||
|
)
|
||||||
|
|
||||||
|
var ds18b20_crc_data = [...]byte{
|
||||||
|
0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
|
||||||
|
157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
|
||||||
|
35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
|
||||||
|
190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
|
||||||
|
70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
|
||||||
|
219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
|
||||||
|
101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
|
||||||
|
248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
|
||||||
|
140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,
|
||||||
|
17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
|
||||||
|
175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,
|
||||||
|
50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
|
||||||
|
202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
|
||||||
|
87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
|
||||||
|
233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
|
||||||
|
116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53,
|
||||||
|
}
|
||||||
|
// DS18B20 represents a DS18B20 temperature sensor.
|
||||||
|
type DS18B20 struct {
|
||||||
|
Device embd.W1Device
|
||||||
|
|
||||||
|
Raw int16
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns a handle to a DS18B20 sensor.
|
||||||
|
func New(device embd.W1Device) *DS18B20 {
|
||||||
|
return &DS18B20{Device: device}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ds18b20_crc(data []byte) byte {
|
||||||
|
var crc byte = 0
|
||||||
|
var x byte
|
||||||
|
for _, x := range data {
|
||||||
|
crc = ds18b20_crc_data[crc ^ x]
|
||||||
|
}
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sensor *DS18B20) ReadTemperature() error {
|
||||||
|
sensor.mu.Lock()
|
||||||
|
defer sensor.mu.Unlock()
|
||||||
|
|
||||||
|
err := sensor.Device.WriteByte(0x44)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for ret, err := sensor.Device.ReadByte(); ret == 0 && err != nil; {}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = sensor.Device.WriteByte(0xBE)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := sensor.Device.ReadBytes(9)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
crc := ds18b20_crc(res[:8])
|
||||||
|
|
||||||
|
if crc != res[8] {
|
||||||
|
errors.New("CRC error")
|
||||||
|
}
|
||||||
|
|
||||||
|
sensor.Raw = int16(res[1]) * 256 + int16(res[0])
|
||||||
|
cfg := res[4] & 0x60
|
||||||
|
|
||||||
|
switch cfg {
|
||||||
|
case 0x00:
|
||||||
|
sensor.Raw &^= 7
|
||||||
|
case 0x20:
|
||||||
|
sensor.Raw &^= 3
|
||||||
|
case 0x40:
|
||||||
|
sensor.Raw &^= 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sensor *DS18B20) Celsius() float32 {
|
||||||
|
return float32(sensor.Raw) * 0.0625
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sensor *DS18B20) Fahrenheit() float32 {
|
||||||
|
return float32(sensor.Raw) * 0.1125 + 32.
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sensor *DS18B20) SetResolution(resoultion DS18B20_Resolution) error {
|
||||||
|
sensor.mu.Lock()
|
||||||
|
defer sensor.mu.Unlock()
|
||||||
|
|
||||||
|
err := sensor.Device.WriteByte(0x4E)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = sensor.Device.WriteByte(0x00)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = sensor.Device.WriteByte(0x00)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
switch resoultion {
|
||||||
|
case Resolution_9bit:
|
||||||
|
err = sensor.Device.WriteByte(0x1F)
|
||||||
|
case Resolution_10bit:
|
||||||
|
err = sensor.Device.WriteByte(0x3F)
|
||||||
|
case Resolution_11bit:
|
||||||
|
err = sensor.Device.WriteByte(0x5F)
|
||||||
|
case Resolution_12bit:
|
||||||
|
err = sensor.Device.WriteByte(0x7F)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
50
sensor/npa700/npa700.go
Normal file
50
sensor/npa700/npa700.go
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
// Package npa700 allows interfacing with GE NPA-700 pressure sensor. This sensor
|
||||||
|
// has the ability to provide compensated temperature and pressure readings.
|
||||||
|
package npa700
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/zlowred/embd"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NPA700 represents a Bosch BMP180 barometric sensor.
|
||||||
|
type NPA700 struct {
|
||||||
|
Bus embd.I2CBus
|
||||||
|
|
||||||
|
RawTemperature int16
|
||||||
|
RawPressure int16
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns a handle to a BMP180 sensor.
|
||||||
|
func New(bus embd.I2CBus) *NPA700 {
|
||||||
|
return &NPA700{Bus: bus}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sensor *NPA700) Read() error {
|
||||||
|
sensor.mu.Lock()
|
||||||
|
defer sensor.mu.Unlock()
|
||||||
|
|
||||||
|
data, err := sensor.Bus.ReadBytes(0x28, 4)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
sensor.RawPressure = (int16(data[0]) << 8) + int16(data[1])
|
||||||
|
sensor.RawTemperature = (int16(data[2]) << 3) + (int16(data[3]) >> 5)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sensor *NPA700) Celsius() float32 {
|
||||||
|
return float32(sensor.RawTemperature) * 200. / 2048. - 50.
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sensor *NPA700) Fahrenheit() float32 {
|
||||||
|
return (float32(sensor.RawTemperature) * 200. / 2048. - 50.) * 1.8 + 32.
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sensor *NPA700) Pascals(offset float32, minValue float32, maxValue float32, minPressure float32, maxPressure float32) float32 {
|
||||||
|
return minPressure + (float32(sensor.RawPressure) + offset - minValue) / (maxValue - minValue) * (maxPressure - minPressure)
|
||||||
|
}
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/kidoman/embd"
|
"github.com/zlowred/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WaterSensor represents a water sensor.
|
// WaterSensor represents a water sensor.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user