tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/sx127x/radiocontrol_pins.go (about) 1 package sx127x 2 3 import ( 4 "machine" 5 ) 6 7 // RadioControl for boards that are connected using normal pins. 8 type RadioControl struct { 9 nssPin, dio0Pin, dio1Pin machine.Pin 10 } 11 12 func NewRadioControl(nssPin, dio0Pin, dio1Pin machine.Pin) *RadioControl { 13 return &RadioControl{ 14 nssPin: nssPin, 15 dio0Pin: dio0Pin, 16 dio1Pin: dio1Pin, 17 } 18 } 19 20 // SetNss sets the NSS line aka chip select for SPI. 21 func (rc *RadioControl) SetNss(state bool) error { 22 rc.nssPin.Set(state) 23 return nil 24 } 25 26 // Init() configures whatever needed for sx127x radio control 27 func (rc *RadioControl) Init() error { 28 rc.nssPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) 29 rc.dio0Pin.Configure(machine.PinConfig{Mode: machine.PinInputPulldown}) 30 rc.dio1Pin.Configure(machine.PinConfig{Mode: machine.PinInputPulldown}) 31 return nil 32 } 33 34 // add interrupt handlers for Radio IRQs for pins 35 func (rc *RadioControl) SetupInterrupts(handler func()) error { 36 irqHandler = handler 37 38 // Setup DIO0 interrupt Handling 39 if err := rc.dio0Pin.SetInterrupt(machine.PinRising, handleInterrupt); err != nil { 40 return err 41 } 42 43 // Setup DIO1 interrupt Handling 44 if err := rc.dio1Pin.SetInterrupt(machine.PinRising, handleInterrupt); err != nil { 45 return err 46 } 47 48 return nil 49 } 50 51 var irqHandler func() 52 53 func handleInterrupt(machine.Pin) { 54 irqHandler() 55 }