tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/wifinina/pins.go (about)

     1  package wifinina
     2  
     3  import "errors"
     4  
     5  // Mimics machine package's pin control
     6  //
     7  // NB! These are NINA chip pins, not main unit pins.
     8  //
     9  // Digital pin values and modes taken from
    10  // https://github.com/arduino/nina-fw/blob/master/arduino/cores/esp32/wiring_digital.h
    11  
    12  type Pin uint8
    13  
    14  const (
    15  	PinLow uint8 = iota
    16  	PinHigh
    17  )
    18  
    19  type PinMode uint8
    20  
    21  const (
    22  	PinInput PinMode = iota
    23  	PinOutput
    24  	PinInputPullup
    25  )
    26  
    27  type PinConfig struct {
    28  	Mode PinMode
    29  }
    30  
    31  var (
    32  	ErrPinNoDevice = errors.New("wifinina pin: device not set")
    33  )
    34  
    35  var pinDevice *wifinina
    36  
    37  func pinUseDevice(w *wifinina) {
    38  	pinDevice = w
    39  }
    40  
    41  func (p Pin) Configure(config PinConfig) error {
    42  	if pinDevice == nil {
    43  		return ErrPinNoDevice
    44  	}
    45  	pinDevice.PinMode(uint8(p), uint8(config.Mode))
    46  	return nil
    47  }
    48  
    49  func (p Pin) Set(high bool) error {
    50  	if pinDevice == nil {
    51  		return ErrPinNoDevice
    52  	}
    53  	value := PinLow
    54  	if high {
    55  		value = PinHigh
    56  	}
    57  	pinDevice.DigitalWrite(uint8(p), value)
    58  	return nil
    59  }
    60  
    61  func (p Pin) High() error {
    62  	return p.Set(true)
    63  }
    64  
    65  func (p Pin) Low() error {
    66  	return p.Set(false)
    67  }