1
0
Fork 0
mirror of https://github.com/kidoman/embd synced 2025-07-04 04:17:41 +02:00

OneWire bus impl

This commit is contained in:
Max Matveev 2016-02-05 22:16:51 -08:00
parent bfcd1345fe
commit 82f119fadb
72 changed files with 568 additions and 174 deletions

View file

@ -2,6 +2,6 @@
package all
import (
_ "github.com/kidoman/embd/host/bbb"
_ "github.com/kidoman/embd/host/rpi"
_ "github.com/zlowred/embd/host/bbb"
_ "github.com/zlowred/embd/host/rpi"
)

View file

@ -9,7 +9,7 @@ import (
"strconv"
"strings"
"github.com/kidoman/embd"
"github.com/zlowred/embd"
)
type analogPin struct {

View file

@ -3,7 +3,7 @@ package bbb
import (
"testing"
"github.com/kidoman/embd"
"github.com/zlowred/embd"
)
func TestAnalogPinClose(t *testing.T) {

View file

@ -16,8 +16,8 @@ import (
"strings"
"github.com/golang/glog"
"github.com/kidoman/embd"
"github.com/kidoman/embd/host/generic"
"github.com/zlowred/embd"
"github.com/zlowred/embd/host/generic"
)
var pins = embd.PinMap{

View file

@ -11,8 +11,8 @@ import (
"time"
"github.com/golang/glog"
"github.com/kidoman/embd"
"github.com/kidoman/embd/util"
"github.com/zlowred/embd"
"github.com/zlowred/embd/util"
)
const (

View file

@ -3,7 +3,7 @@ package bbb
import (
"testing"
"github.com/kidoman/embd"
"github.com/zlowred/embd"
)
func TestPWMPinClose(t *testing.T) {

View file

@ -12,7 +12,7 @@ import (
"strconv"
"time"
"github.com/kidoman/embd"
"github.com/zlowred/embd"
)
type digitalPin struct {

View file

@ -3,7 +3,7 @@ package generic
import (
"testing"
"github.com/kidoman/embd"
"github.com/zlowred/embd"
)
func TestDigitalPinClose(t *testing.T) {

View file

@ -12,7 +12,7 @@ import (
"unsafe"
"github.com/golang/glog"
"github.com/kidoman/embd"
"github.com/zlowred/embd"
)
const (

View file

@ -8,7 +8,7 @@ import (
"sync"
"syscall"
"github.com/kidoman/embd"
"github.com/zlowred/embd"
)
const (

View file

@ -8,7 +8,7 @@ import (
"os"
"strings"
"github.com/kidoman/embd"
"github.com/zlowred/embd"
)
type led struct {

View file

@ -8,7 +8,7 @@ import (
"unsafe"
"github.com/golang/glog"
"github.com/kidoman/embd"
"github.com/zlowred/embd"
)
const (

192
host/rpi/onewirebus.go Normal file
View file

@ -0,0 +1,192 @@
// 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
mu sync.Mutex
initialized bool
}
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.mu.Lock()
defer d.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.mu.Lock()
defer d.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.mu.Lock()
defer d.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.mu.Lock()
defer d.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}
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 (b *w1Device) Close() error {
b.mu.Lock()
defer b.mu.Unlock()
if !b.initialized {
return nil
}
return b.file.Close()
}

View file

@ -5,12 +5,14 @@
GPIO (digital (rw))
I²C
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
import (
"github.com/kidoman/embd"
"github.com/kidoman/embd/host/generic"
"github.com/zlowred/embd"
"github.com/zlowred/embd/host/generic"
"fmt"
)
var spiDeviceMinor = byte(0)
@ -73,6 +75,7 @@ var ledMap = embd.LEDMap{
}
func init() {
fmt.Println("registering RPI")
embd.Register(embd.HostRPi, func(rev int) *embd.Descriptor {
// Refer to http://elinux.org/RPi_HardwareHistory#Board_Revision_History
// for details.
@ -97,6 +100,10 @@ func init() {
SPIDriver: func() embd.SPIDriver {
return embd.NewSPIDriver(spiDeviceMinor, generic.NewSPIBus, nil)
},
W1Driver: func() embd.W1Driver {
return embd.NewW1Driver(NewW1Bus)
},
}
})
fmt.Println("registered")
}