mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-07-26 10:38:41 +00:00
wifinina: control nina pins, for example leds
This commit is contained in:
@@ -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)
|
||||
}
|
||||
@@ -297,6 +297,7 @@ func New(bus drivers.SPI, csPin, ackPin, gpio0Pin, resetPin machine.Pin) *Device
|
||||
func (d *Device) Configure() {
|
||||
|
||||
net.UseDriver(d.NewDriver())
|
||||
pinUseDevice(d)
|
||||
|
||||
d.CS.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
d.ACK.Configure(machine.PinConfig{Mode: machine.PinInput})
|
||||
@@ -689,6 +690,23 @@ func (d *Device) StartScanNetworks() (uint8, error) {
|
||||
return d.getUint8(d.req0(CmdStartScanNetworks))
|
||||
}
|
||||
|
||||
func (d *Device) PinMode(pin uint8, mode uint8) error {
|
||||
_, err := d.req2Uint8(CmdSetPinMode, pin, mode)
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *Device) DigitalWrite(pin uint8, value uint8) error {
|
||||
_, err := d.req2Uint8(CmdSetDigitalWrite, pin, value)
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *Device) AnalogWrite(pin uint8, value uint8) error {
|
||||
_, err := d.req2Uint8(CmdSetAnalogWrite, pin, value)
|
||||
return err
|
||||
}
|
||||
|
||||
// ------------- End of public device interface ----------------------------
|
||||
|
||||
func (d *Device) getString(l uint8, err error) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -773,6 +791,14 @@ func (d *Device) reqUint8(cmd uint8, data uint8) (l uint8, err error) {
|
||||
return d.waitRspCmd1(cmd)
|
||||
}
|
||||
|
||||
// req2Uint8 sends a command to the device with two uint8 parameters
|
||||
func (d *Device) req2Uint8(cmd, p1, p2 uint8) (l uint8, err error) {
|
||||
if err := d.sendCmdPadded2(cmd, p1, p2); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return d.waitRspCmd1(cmd)
|
||||
}
|
||||
|
||||
// reqStr sends a command to the device with a single string parameter
|
||||
func (d *Device) reqStr(cmd uint8, p1 string) (uint8, error) {
|
||||
if err := d.sendCmdStr(cmd, p1); err != nil {
|
||||
@@ -834,6 +860,18 @@ func (d *Device) sendCmdPadded1(cmd uint8, data uint8) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Device) sendCmdPadded2(cmd, data1, data2 uint8) error {
|
||||
defer d.spiChipDeselect()
|
||||
if err := d.waitForChipSelect(); err != nil {
|
||||
return err
|
||||
}
|
||||
l := d.sendCmd(cmd, 1)
|
||||
l += d.sendParam8(data1, false)
|
||||
l += d.sendParam8(data2, true)
|
||||
d.SPI.Transfer(dummyData)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Device) sendCmdStr(cmd uint8, p1 string) (err error) {
|
||||
defer d.spiChipDeselect()
|
||||
if err := d.waitForChipSelect(); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user