mirror of
https://github.com/kidoman/embd
synced 2025-07-04 20:37:46 +02:00
Initial attempt to implemented GPIO Interrupts
Basicly imported Dave Cheney's code from http://github.com/davecheney/gpio
This commit is contained in:
parent
5c0ae1c83a
commit
a54e7dc7ff
3 changed files with 154 additions and 4 deletions
|
@ -5,6 +5,11 @@ package embd
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxGPIOInterrupt = 64
|
||||
)
|
||||
|
||||
type pin interface {
|
||||
|
@ -22,20 +27,88 @@ type gpioDriver struct {
|
|||
apf analogPinFactory
|
||||
ppf pwmPinFactory
|
||||
|
||||
initializedPins map[string]pin
|
||||
watchEventCallbacks map[int]DigitalPin
|
||||
initializedPins map[string]pin
|
||||
}
|
||||
|
||||
// NewGPIODriver returns a GPIODriver interface which allows control
|
||||
// over the GPIO subsystem.
|
||||
func NewGPIODriver(pinMap PinMap, dpf digitalPinFactory, apf analogPinFactory, ppf pwmPinFactory) GPIODriver {
|
||||
return &gpioDriver{
|
||||
driver := &gpioDriver{
|
||||
pinMap: pinMap,
|
||||
dpf: dpf,
|
||||
apf: apf,
|
||||
ppf: ppf,
|
||||
|
||||
initializedPins: map[string]pin{},
|
||||
watchEventCallbacks: map[int]DigitalPin{},
|
||||
initializedPins: map[string]pin{},
|
||||
}
|
||||
return driver
|
||||
}
|
||||
|
||||
var epollFD int
|
||||
|
||||
func (io *gpioDriver) initializeEpoll() {
|
||||
var err error
|
||||
epollFD, err = syscall.EpollCreate1(0)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Unable to create epoll FD: ", err.Error()))
|
||||
}
|
||||
|
||||
go func() {
|
||||
var epollEvents [MaxGPIOInterrupt]syscall.EpollEvent
|
||||
|
||||
for {
|
||||
numEvents, err := syscall.EpollWait(epollFD, epollEvents[:], -1)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("EpollWait error: %s", err.Error()))
|
||||
}
|
||||
for i := 0; i < numEvents; i++ {
|
||||
if eventPin, exists := io.watchEventCallbacks[int(epollEvents[i].Fd)]; exists {
|
||||
eventPin.Signal()
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (io *gpioDriver) RegisterInterrupt(fd int, p DigitalPin) error {
|
||||
|
||||
if epollFD == 0 {
|
||||
io.initializeEpoll()
|
||||
}
|
||||
|
||||
var event syscall.EpollEvent
|
||||
event.Events = syscall.EPOLLIN | (syscall.EPOLLET & 0xffffffff) | syscall.EPOLLPRI
|
||||
|
||||
io.watchEventCallbacks[fd] = p
|
||||
|
||||
if err := syscall.SetNonblock(fd, true); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
event.Fd = int32(fd)
|
||||
|
||||
if err := syscall.EpollCtl(epollFD, syscall.EPOLL_CTL_ADD, fd, &event); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (io *gpioDriver) UnregisterInterrupt(fd int) error {
|
||||
|
||||
if err := syscall.EpollCtl(epollFD, syscall.EPOLL_CTL_DEL, fd, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := syscall.SetNonblock(fd, false); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
delete(io.watchEventCallbacks, fd)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (io *gpioDriver) Unregister(id string) error {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue