2014-08-31 00:39:41 -04:00
|
|
|
// Generic Interrupt Pins.
|
|
|
|
|
|
|
|
package generic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/kidoman/embd"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
MaxGPIOInterrupt = 64
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrorPinAlreadyRegistered = errors.New("pin interrupt already registered")
|
|
|
|
|
|
|
|
type interrupt struct {
|
|
|
|
pin embd.DigitalPin
|
|
|
|
initialTrigger bool
|
|
|
|
handler func(embd.DigitalPin)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *interrupt) Signal() {
|
|
|
|
if !i.initialTrigger {
|
|
|
|
i.initialTrigger = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
i.handler(i.pin)
|
|
|
|
}
|
|
|
|
|
2015-01-15 05:35:10 +05:30
|
|
|
type epollListener struct {
|
|
|
|
mu sync.Mutex // Guards the following.
|
|
|
|
fd int
|
2014-08-31 00:39:41 -04:00
|
|
|
interruptablePins map[int]*interrupt
|
|
|
|
}
|
|
|
|
|
2015-01-15 05:35:10 +05:30
|
|
|
var epollListenerInstance *epollListener
|
2014-08-31 00:39:41 -04:00
|
|
|
|
2015-01-15 05:35:10 +05:30
|
|
|
func getEpollListenerInstance() *epollListener {
|
|
|
|
if epollListenerInstance == nil {
|
|
|
|
epollListenerInstance = initEpollListener()
|
2014-08-31 00:39:41 -04:00
|
|
|
}
|
2015-01-15 05:35:10 +05:30
|
|
|
return epollListenerInstance
|
2014-08-31 00:39:41 -04:00
|
|
|
}
|
|
|
|
|
2015-01-15 05:35:10 +05:30
|
|
|
func initEpollListener() *epollListener {
|
|
|
|
fd, err := syscall.EpollCreate1(0)
|
2014-08-31 00:39:41 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Unable to create epoll: %v", err))
|
|
|
|
}
|
2015-01-15 05:35:10 +05:30
|
|
|
listener := &epollListener{fd: fd, interruptablePins: make(map[int]*interrupt)}
|
2014-08-31 00:39:41 -04:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
var epollEvents [MaxGPIOInterrupt]syscall.EpollEvent
|
|
|
|
|
|
|
|
for {
|
2015-01-15 05:35:10 +05:30
|
|
|
n, err := syscall.EpollWait(listener.fd, epollEvents[:], -1)
|
2014-08-31 00:39:41 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("EpollWait error: %v", err))
|
|
|
|
}
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
if irq, ok := listener.interruptablePins[int(epollEvents[i].Fd)]; ok {
|
|
|
|
irq.Signal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return listener
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerInterrupt(pin *digitalPin, handler func(embd.DigitalPin)) error {
|
2015-01-15 05:35:10 +05:30
|
|
|
l := getEpollListenerInstance()
|
2014-08-31 00:39:41 -04:00
|
|
|
|
|
|
|
pinFd := int(pin.val.Fd())
|
|
|
|
|
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
|
|
|
|
if _, ok := l.interruptablePins[pinFd]; ok {
|
|
|
|
return ErrorPinAlreadyRegistered
|
|
|
|
}
|
|
|
|
|
|
|
|
var event syscall.EpollEvent
|
|
|
|
event.Events = syscall.EPOLLIN | (syscall.EPOLLET & 0xffffffff) | syscall.EPOLLPRI
|
|
|
|
|
|
|
|
if err := syscall.SetNonblock(pinFd, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
event.Fd = int32(pinFd)
|
|
|
|
|
2015-01-15 05:35:10 +05:30
|
|
|
if err := syscall.EpollCtl(l.fd, syscall.EPOLL_CTL_ADD, pinFd, &event); err != nil {
|
2014-08-31 00:39:41 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
l.interruptablePins[pinFd] = &interrupt{pin: pin, handler: handler}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unregisterInterrupt(pin *digitalPin) error {
|
2015-01-15 05:35:10 +05:30
|
|
|
l := getEpollListenerInstance()
|
2014-08-31 00:39:41 -04:00
|
|
|
|
|
|
|
pinFd := int(pin.val.Fd())
|
|
|
|
|
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
|
|
|
|
if _, ok := l.interruptablePins[pinFd]; !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-15 05:35:10 +05:30
|
|
|
if err := syscall.EpollCtl(l.fd, syscall.EPOLL_CTL_DEL, pinFd, nil); err != nil {
|
2014-08-31 00:39:41 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := syscall.SetNonblock(pinFd, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(l.interruptablePins, pinFd)
|
|
|
|
return nil
|
|
|
|
}
|