2022-11-21 02:22:26 +08:00
|
|
|
package system
|
2014-10-30 08:51:47 +08:00
|
|
|
|
|
|
|
import (
|
2016-02-19 04:08:48 +08:00
|
|
|
"errors"
|
2014-10-30 08:51:47 +08:00
|
|
|
"fmt"
|
2023-01-28 19:22:32 +08:00
|
|
|
"log"
|
2014-10-30 08:51:47 +08:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2017-04-02 04:07:47 +08:00
|
|
|
"time"
|
2022-11-27 23:06:09 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
2014-10-30 08:51:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-01-28 19:22:32 +08:00
|
|
|
systemSysfsDebug = false
|
2022-11-27 23:06:09 +08:00
|
|
|
// gpioPath default linux sysfs gpio path
|
2022-11-21 02:22:26 +08:00
|
|
|
gpioPath = "/sys/class/gpio"
|
2014-10-30 08:51:47 +08:00
|
|
|
)
|
|
|
|
|
2017-04-28 18:50:36 +08:00
|
|
|
var errNotExported = errors.New("pin has not been exported")
|
|
|
|
|
2022-11-27 23:06:09 +08:00
|
|
|
// digitalPin represents a digital pin
|
|
|
|
type digitalPinSysfs struct {
|
|
|
|
pin string
|
|
|
|
*digitalPinConfig
|
2023-11-27 02:47:28 +08:00
|
|
|
sfa *sysfsFileAccess
|
2016-02-19 04:08:48 +08:00
|
|
|
|
2023-11-27 02:47:28 +08:00
|
|
|
dirFile *sysfsFile
|
|
|
|
valFile *sysfsFile
|
|
|
|
activeLowFile *sysfsFile
|
2022-11-27 23:06:09 +08:00
|
|
|
}
|
2022-11-05 14:42:28 +08:00
|
|
|
|
2022-11-27 23:06:09 +08:00
|
|
|
// newDigitalPinSysfs returns a digital pin using for the given number. The name of the sysfs file will prepend "gpio"
|
|
|
|
// to the pin number, eg. a pin number of 10 will have a name of "gpio10". The pin is handled by the sysfs Kernel ABI.
|
2023-11-27 02:47:28 +08:00
|
|
|
func newDigitalPinSysfs(
|
|
|
|
sfa *sysfsFileAccess,
|
|
|
|
pin string,
|
|
|
|
options ...func(gobot.DigitalPinOptioner) bool,
|
|
|
|
) *digitalPinSysfs {
|
2022-11-27 23:06:09 +08:00
|
|
|
cfg := newDigitalPinConfig("gpio"+pin, options...)
|
|
|
|
d := &digitalPinSysfs{
|
|
|
|
pin: pin,
|
|
|
|
digitalPinConfig: cfg,
|
2023-11-27 02:47:28 +08:00
|
|
|
sfa: sfa,
|
2022-11-27 23:06:09 +08:00
|
|
|
}
|
|
|
|
return d
|
2014-10-30 08:51:47 +08:00
|
|
|
}
|
|
|
|
|
2022-11-27 23:06:09 +08:00
|
|
|
// ApplyOptions apply all given options to the pin immediately. Implements interface gobot.DigitalPinOptionApplier.
|
|
|
|
func (d *digitalPinSysfs) ApplyOptions(options ...func(gobot.DigitalPinOptioner) bool) error {
|
|
|
|
anyChange := false
|
|
|
|
for _, option := range options {
|
2023-10-27 02:41:41 +08:00
|
|
|
anyChange = option(d) || anyChange
|
2022-11-27 23:06:09 +08:00
|
|
|
}
|
|
|
|
if anyChange {
|
|
|
|
return d.reconfigure()
|
2022-11-05 14:42:28 +08:00
|
|
|
}
|
2022-11-27 23:06:09 +08:00
|
|
|
return nil
|
|
|
|
}
|
2014-10-31 06:26:31 +08:00
|
|
|
|
2022-11-27 23:06:09 +08:00
|
|
|
// DirectionBehavior gets the direction behavior when the pin is used the next time. This means its possibly not in
|
|
|
|
// this direction type at the moment. Implements the interface gobot.DigitalPinValuer, but should be rarely used.
|
|
|
|
func (d *digitalPinSysfs) DirectionBehavior() string {
|
|
|
|
return d.direction
|
2014-10-30 08:51:47 +08:00
|
|
|
}
|
2014-10-31 04:41:27 +08:00
|
|
|
|
2022-11-27 23:06:09 +08:00
|
|
|
// Export sets the pin as exported with the configured direction
|
|
|
|
func (d *digitalPinSysfs) Export() error {
|
2023-06-13 01:51:25 +08:00
|
|
|
return d.reconfigure()
|
2014-10-30 08:51:47 +08:00
|
|
|
}
|
|
|
|
|
2022-11-27 23:06:09 +08:00
|
|
|
// Unexport release the pin
|
|
|
|
func (d *digitalPinSysfs) Unexport() error {
|
2023-11-27 02:47:28 +08:00
|
|
|
unexport, err := d.sfa.openWrite(gpioPath + "/unexport")
|
2022-11-27 23:06:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-27 02:47:28 +08:00
|
|
|
defer unexport.close()
|
2022-11-27 23:06:09 +08:00
|
|
|
|
|
|
|
if d.dirFile != nil {
|
2023-11-27 02:47:28 +08:00
|
|
|
d.dirFile.close()
|
2022-11-27 23:06:09 +08:00
|
|
|
d.dirFile = nil
|
|
|
|
}
|
|
|
|
if d.valFile != nil {
|
2023-11-27 02:47:28 +08:00
|
|
|
d.valFile.close()
|
2022-11-27 23:06:09 +08:00
|
|
|
d.valFile = nil
|
|
|
|
}
|
2023-01-28 19:22:32 +08:00
|
|
|
if d.activeLowFile != nil {
|
2023-11-27 02:47:28 +08:00
|
|
|
d.activeLowFile.close()
|
2023-01-28 19:22:32 +08:00
|
|
|
d.activeLowFile = nil
|
|
|
|
}
|
2022-11-27 23:06:09 +08:00
|
|
|
|
2023-11-27 02:47:28 +08:00
|
|
|
err = unexport.write([]byte(d.pin))
|
2022-11-27 23:06:09 +08:00
|
|
|
if err != nil {
|
2023-11-16 03:51:52 +08:00
|
|
|
// If EINVAL then the pin is reserved in the system and can't be unexported, we suppress the error
|
|
|
|
var pathError *os.PathError
|
|
|
|
if !(errors.As(err, &pathError) && errors.Is(err, Syscall_EINVAL)) {
|
2022-11-27 23:06:09 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-05 14:42:28 +08:00
|
|
|
// Write writes the given value to the character device
|
2022-11-27 23:06:09 +08:00
|
|
|
func (d *digitalPinSysfs) Write(b int) error {
|
2023-11-27 02:47:28 +08:00
|
|
|
if d.valFile == nil {
|
|
|
|
return errNotExported
|
|
|
|
}
|
|
|
|
err := d.valFile.write([]byte(strconv.Itoa(b)))
|
2014-10-31 04:41:27 +08:00
|
|
|
return err
|
2014-10-30 08:51:47 +08:00
|
|
|
}
|
|
|
|
|
2023-11-27 02:47:28 +08:00
|
|
|
// Read reads a value from character device
|
2022-11-27 23:06:09 +08:00
|
|
|
func (d *digitalPinSysfs) Read() (int, error) {
|
2023-11-27 02:47:28 +08:00
|
|
|
if d.valFile == nil {
|
|
|
|
return 0, errNotExported
|
|
|
|
}
|
|
|
|
buf, err := d.valFile.read()
|
2014-10-31 04:41:27 +08:00
|
|
|
if err != nil {
|
2014-11-08 08:21:39 +08:00
|
|
|
return 0, err
|
2014-10-31 04:41:27 +08:00
|
|
|
}
|
|
|
|
return strconv.Atoi(string(buf[0]))
|
2014-10-30 08:51:47 +08:00
|
|
|
}
|
|
|
|
|
2022-11-27 23:06:09 +08:00
|
|
|
func (d *digitalPinSysfs) reconfigure() error {
|
2023-11-27 02:47:28 +08:00
|
|
|
exportFile, err := d.sfa.openWrite(gpioPath + "/export")
|
2016-02-19 04:08:48 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-27 02:47:28 +08:00
|
|
|
defer exportFile.close()
|
2016-02-19 04:08:48 +08:00
|
|
|
|
2023-11-27 02:47:28 +08:00
|
|
|
err = exportFile.write([]byte(d.pin))
|
2016-02-19 04:08:48 +08:00
|
|
|
if err != nil {
|
2023-11-16 03:51:52 +08:00
|
|
|
// If EBUSY then the pin has already been exported, we suppress the error
|
|
|
|
var pathError *os.PathError
|
|
|
|
if !(errors.As(err, &pathError) && errors.Is(err, Syscall_EBUSY)) {
|
2014-12-24 04:43:55 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-02-19 04:08:48 +08:00
|
|
|
|
2022-11-27 23:06:09 +08:00
|
|
|
if d.dirFile != nil {
|
2023-11-27 02:47:28 +08:00
|
|
|
d.dirFile.close()
|
2016-02-19 04:08:48 +08:00
|
|
|
}
|
|
|
|
|
2017-04-02 04:07:47 +08:00
|
|
|
attempt := 0
|
|
|
|
for {
|
|
|
|
attempt++
|
2023-11-27 02:47:28 +08:00
|
|
|
d.dirFile, err = d.sfa.openReadWrite(fmt.Sprintf("%s/%s/direction", gpioPath, d.label))
|
2017-04-02 04:07:47 +08:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if attempt > 10 {
|
2023-10-27 02:41:41 +08:00
|
|
|
break
|
2017-04-02 04:07:47 +08:00
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
2016-02-19 04:08:48 +08:00
|
|
|
|
2022-11-27 23:06:09 +08:00
|
|
|
if d.valFile != nil {
|
2023-11-27 02:47:28 +08:00
|
|
|
d.valFile.close()
|
2016-02-19 04:08:48 +08:00
|
|
|
}
|
|
|
|
if err == nil {
|
2023-11-27 02:47:28 +08:00
|
|
|
d.valFile, err = d.sfa.openReadWrite(fmt.Sprintf("%s/%s/value", gpioPath, d.label))
|
2022-11-27 23:06:09 +08:00
|
|
|
}
|
|
|
|
|
2023-01-28 19:22:32 +08:00
|
|
|
// configure direction
|
2022-11-27 23:06:09 +08:00
|
|
|
if err == nil {
|
|
|
|
err = d.writeDirectionWithInitialOutput()
|
2016-02-19 04:08:48 +08:00
|
|
|
}
|
|
|
|
|
2023-01-28 19:22:32 +08:00
|
|
|
// configure inverse logic
|
|
|
|
if err == nil {
|
|
|
|
if d.activeLow {
|
2023-11-27 02:47:28 +08:00
|
|
|
d.activeLowFile, err = d.sfa.openReadWrite(fmt.Sprintf("%s/%s/active_low", gpioPath, d.label))
|
2023-01-28 19:22:32 +08:00
|
|
|
if err == nil {
|
2023-11-27 02:47:28 +08:00
|
|
|
err = d.activeLowFile.write([]byte("1"))
|
2023-01-28 19:22:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-27 02:41:41 +08:00
|
|
|
// configure bias (inputs and outputs, unsupported)
|
2023-01-28 19:22:32 +08:00
|
|
|
if err == nil {
|
|
|
|
if d.bias != digitalPinBiasDefault && systemSysfsDebug {
|
|
|
|
log.Printf("bias options (%d) are not supported by sysfs, please use hardware resistors instead\n", d.bias)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-27 02:41:41 +08:00
|
|
|
// configure debounce period (inputs only), edge detection (inputs only) and drive (outputs only)
|
|
|
|
if d.direction == IN {
|
|
|
|
// configure debounce (unsupported)
|
|
|
|
if d.debouncePeriod != 0 && systemSysfsDebug {
|
|
|
|
log.Printf("debounce period option (%d) is not supported by sysfs\n", d.debouncePeriod)
|
|
|
|
}
|
2023-01-28 19:22:32 +08:00
|
|
|
|
2023-10-27 02:41:41 +08:00
|
|
|
// configure edge detection
|
|
|
|
if err == nil {
|
|
|
|
if d.edge != 0 && d.pollInterval <= 0 {
|
|
|
|
err = fmt.Errorf("edge detect option (%d) is not implemented for sysfs without discrete polling", d.edge)
|
|
|
|
}
|
|
|
|
}
|
2023-01-28 19:22:32 +08:00
|
|
|
|
2023-10-27 02:41:41 +08:00
|
|
|
// start discrete polling function and wait for first read is done
|
|
|
|
if err == nil {
|
|
|
|
if d.pollInterval > 0 {
|
|
|
|
err = startEdgePolling(d.label, d.Read, d.pollInterval, d.edge, d.edgeEventHandler, d.pollQuitChan)
|
|
|
|
}
|
|
|
|
}
|
2023-11-16 03:51:52 +08:00
|
|
|
} else if d.drive != digitalPinDrivePushPull && systemSysfsDebug {
|
2023-10-27 02:41:41 +08:00
|
|
|
// configure drive (unsupported)
|
2023-11-16 03:51:52 +08:00
|
|
|
log.Printf("drive options (%d) are not supported by sysfs\n", d.drive)
|
2023-01-28 19:22:32 +08:00
|
|
|
}
|
|
|
|
|
2016-02-19 04:08:48 +08:00
|
|
|
if err != nil {
|
2023-10-27 02:41:41 +08:00
|
|
|
if e := d.Unexport(); e != nil {
|
|
|
|
err = fmt.Errorf("unexport error '%v' after '%v'", e, err)
|
|
|
|
}
|
2016-02-19 04:08:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2014-10-30 08:51:47 +08:00
|
|
|
}
|
|
|
|
|
2022-11-27 23:06:09 +08:00
|
|
|
func (d *digitalPinSysfs) writeDirectionWithInitialOutput() error {
|
2023-11-27 02:47:28 +08:00
|
|
|
if d.dirFile == nil {
|
|
|
|
return errNotExported
|
|
|
|
}
|
|
|
|
if err := d.dirFile.write([]byte(d.direction)); err != nil || d.direction == IN {
|
2016-02-19 04:08:48 +08:00
|
|
|
return err
|
|
|
|
}
|
2014-10-31 04:41:27 +08:00
|
|
|
|
2023-11-27 02:47:28 +08:00
|
|
|
if d.valFile == nil {
|
2023-10-27 02:41:41 +08:00
|
|
|
return errNotExported
|
2014-10-31 04:41:27 +08:00
|
|
|
}
|
|
|
|
|
2023-11-27 02:47:28 +08:00
|
|
|
return d.valFile.write([]byte(strconv.Itoa(d.outInitialState)))
|
2014-10-31 07:06:04 +08:00
|
|
|
}
|