wifinina: control nina pins, for example leds

This commit is contained in:
Yurii Soldak
2021-10-26 21:26:47 +02:00
committed by Ron Evans
parent 3637033fec
commit 334ed57373
3 changed files with 194 additions and 0 deletions
+65
View File
@@ -0,0 +1,65 @@
package wifinina
import "errors"
// Mimics machine package's pin control
//
// NB! These are NINA chip pins, not main unit pins.
//
// Digital pin values and modes taken from
// https://github.com/arduino/nina-fw/blob/master/arduino/cores/esp32/wiring_digital.h
type Pin uint8
const (
PinLow uint8 = iota
PinHigh
)
type PinMode uint8
const (
PinInput PinMode = iota
PinOutput
PinInputPullup
)
type PinConfig struct {
Mode PinMode
}
var (
ErrPinNoDevice = errors.New("wifinina pin: device not set")
)
var pinDevice *Device
func pinUseDevice(d *Device) {
pinDevice = d
}
func (p Pin) Configure(config PinConfig) error {
if pinDevice == nil {
return ErrPinNoDevice
}
return pinDevice.PinMode(uint8(p), uint8(config.Mode))
}
func (p Pin) Set(high bool) error {
if pinDevice == nil {
return ErrPinNoDevice
}
value := PinLow
if high {
value = PinHigh
}
return pinDevice.DigitalWrite(uint8(p), value)
}
func (p Pin) High() error {
return p.Set(true)
}
func (p Pin) Low() error {
return p.Set(false)
}