@@ -25,9 +25,9 @@ type Device struct {
2525 rstPin machine.Pin // GPIO for reset
2626 radioEventChan chan lora.RadioEvent // Channel for Receiving events
2727 loraConf lora.Config // Current Lora configuration
28- controller RadioController // to manage interactions with the radio
28+ controller RadioController // to manage interrupts with the radio
2929 deepSleep bool // Internal Sleep state
30- deviceType int // sx1261,sx1262,sx1268 (defaults sx1261 )
30+ deviceType int // sx1272, sx1273, sx1276, sx1279 (defaults sx1276 )
3131 spiTxBuf []byte // global Tx buffer to avoid heap allocations in interrupt
3232 spiRxBuf []byte // global Rx buffer to avoid heap allocations in interrupt
3333}
@@ -65,6 +65,11 @@ func (d *Device) SetRadioController(rc RadioController) error {
6565 return nil
6666}
6767
68+ // Specify device type (sx1272, sx1273, sx1276, sx1279)
69+ func (d * Device ) SetDeviceType (devType int ) {
70+ d .deviceType = devType
71+ }
72+
6873// Reset re-initialize the sx127x device
6974func (d * Device ) Reset () {
7075 d .rstPin .Low ()
@@ -81,23 +86,31 @@ func (d *Device) DetectDevice() bool {
8186
8287// ReadRegister reads register value
8388func (d * Device ) ReadRegister (reg uint8 ) uint8 {
84- d .controller .SetNss (false )
89+ if d .controller != nil {
90+ d .controller .SetNss (false )
91+ }
92+
8593 // Send register
86- //d.spiTxBuf = []byte{reg & 0x7f}
8794 d .spiTxBuf = d .spiTxBuf [:0 ]
8895 d .spiTxBuf = append (d .spiTxBuf , byte (reg & 0x7f ))
8996 d .spi .Tx (d .spiTxBuf , nil )
9097 // Read value
9198 d .spiRxBuf = d .spiRxBuf [:0 ]
9299 d .spiRxBuf = append (d .spiRxBuf , 0 )
93100 d .spi .Tx (nil , d .spiRxBuf )
94- d .controller .SetNss (true )
101+ if d .controller != nil {
102+ d .controller .SetNss (true )
103+ }
104+
95105 return d .spiRxBuf [0 ]
96106}
97107
98108// WriteRegister writes value to register
99109func (d * Device ) WriteRegister (reg uint8 , value uint8 ) uint8 {
100- d .controller .SetNss (false )
110+ if d .controller != nil {
111+ d .controller .SetNss (false )
112+ }
113+
101114 // Send register
102115 d .spiTxBuf = d .spiTxBuf [:0 ]
103116 d .spiTxBuf = append (d .spiTxBuf , byte (reg | 0x80 ))
@@ -108,7 +121,10 @@ func (d *Device) WriteRegister(reg uint8, value uint8) uint8 {
108121 d .spiRxBuf = d .spiRxBuf [:0 ]
109122 d .spiRxBuf = append (d .spiRxBuf , 0 )
110123 d .spi .Tx (d .spiTxBuf , d .spiRxBuf )
111- d .controller .SetNss (true )
124+ if d .controller != nil {
125+ d .controller .SetNss (true )
126+ }
127+
112128 return d .spiRxBuf [0 ]
113129}
114130
@@ -119,9 +135,20 @@ func (d *Device) SetOpMode(mode uint8) {
119135 d .WriteRegister (SX127X_REG_OP_MODE , new )
120136}
121137
122- // SetOpMode changes the sx1276 mode
138+ // SetOpModeLora changes the sx1276 mode to lora.
123139func (d * Device ) SetOpModeLora () {
124- d .WriteRegister (SX127X_REG_OP_MODE , SX127X_OPMODE_LORA )
140+ d .WriteRegister (SX127X_REG_OP_MODE , d .ReadRegister (SX127X_REG_OP_MODE )| SX127X_OPMODE_LORA )
141+ }
142+
143+ // SetOpModeFsk changes the sx1276 mode to fsk/ook.
144+ func (d * Device ) SetOpModeFsk () {
145+ d .WriteRegister (SX127X_REG_OP_MODE , d .ReadRegister (SX127X_REG_OP_MODE )&^SX127X_OPMODE_LORA )
146+ }
147+
148+ // SetModulationType changes the modulation type (SX127X_OPMODE_MODULATION_FSK, SX127X_OPMODE_MODULATION_OOK)
149+ func (d * Device ) SetModulationType (typ uint8 ) {
150+ cleared := d .ReadRegister (SX127X_REG_OP_MODE ) &^ SX127X_OPMODE_MODULATION_MASK
151+ d .WriteRegister (SX127X_REG_OP_MODE , cleared | typ )
125152}
126153
127154// GetVersion returns hardware version of sx1276 chipset
@@ -244,9 +271,9 @@ func (d *Device) SetLowDataRateOptim(val uint8) {
244271// SetLowFrequencyModeOn enables Low Data Rate Optimization
245272func (d * Device ) SetLowFrequencyModeOn (val bool ) {
246273 if val {
247- d .WriteRegister (SX127X_REG_OP_MODE , d .ReadRegister (SX127X_REG_OP_MODE )| 0x04 )
274+ d .WriteRegister (SX127X_REG_OP_MODE , d .ReadRegister (SX127X_REG_OP_MODE )| SX127X_OPMODE_LOW_FREQUENCY )
248275 } else {
249- d .WriteRegister (SX127X_REG_OP_MODE , d .ReadRegister (SX127X_REG_OP_MODE )& 0xfb )
276+ d .WriteRegister (SX127X_REG_OP_MODE , d .ReadRegister (SX127X_REG_OP_MODE )&^ SX127X_OPMODE_LOW_FREQUENCY )
250277 }
251278}
252279
0 commit comments