tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/sx126x/lora_continuous/lora_continuous.go (about)

     1  package main
     2  
     3  // This example will periodically enable Continuous "Preamble" and "Wave" modes  on 868.1 Mhz
     4  import (
     5  	"machine"
     6  	"time"
     7  
     8  	"tinygo.org/x/drivers/lora"
     9  	"tinygo.org/x/drivers/sx126x"
    10  )
    11  
    12  const FREQ = 868100000
    13  
    14  var (
    15  	loraRadio *sx126x.Device
    16  )
    17  
    18  func main() {
    19  	println("\n# TinyGo Lora continuous Wave/Preamble test")
    20  	println("# -----------------------------------------")
    21  
    22  	machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput})
    23  
    24  	// Create the driver
    25  	loraRadio = sx126x.New(spi)
    26  	loraRadio.SetDeviceType(sx126x.DEVICE_TYPE_SX1262)
    27  
    28  	// Create radio controller for target
    29  	loraRadio.SetRadioController(newRadioControl())
    30  
    31  	state := loraRadio.DetectDevice()
    32  	if !state {
    33  		panic("sx126x not detected. ")
    34  	}
    35  
    36  	// Prepare for Lora operation
    37  	loraConf := lora.Config{
    38  		Freq:           lora.MHz_868_1,
    39  		Bw:             lora.Bandwidth_125_0,
    40  		Sf:             lora.SpreadingFactor9,
    41  		Cr:             lora.CodingRate4_7,
    42  		HeaderType:     lora.HeaderExplicit,
    43  		Preamble:       12,
    44  		Ldr:            lora.LowDataRateOptimizeOff,
    45  		Iq:             lora.IQStandard,
    46  		Crc:            lora.CRCOn,
    47  		SyncWord:       lora.SyncPrivate,
    48  		LoraTxPowerDBm: 20,
    49  	}
    50  	loraRadio.LoraConfig(loraConf)
    51  
    52  	// Although LoraConfig has already configured most of Lora settings,
    53  	// the following lines are still required to enable Continuous Preamble/Wave
    54  	loraRadio.SetPacketType(sx126x.SX126X_PACKET_TYPE_LORA)
    55  	loraRadio.SetRfFrequency(loraConf.Freq)
    56  	loraRadio.SetModulationParams(loraConf.Sf, loraConf.Bw, loraConf.Cr, loraConf.Ldr)
    57  	loraRadio.SetTxParams(loraConf.LoraTxPowerDBm, sx126x.SX126X_PA_RAMP_200U)
    58  
    59  	for {
    60  		println("2 seconds in Continuous Preamble")
    61  		loraRadio.SetStandby()
    62  		loraRadio.SetTxContinuousPreamble()
    63  		time.Sleep(2 * time.Second)
    64  		println("Continuous Preamble Stopped")
    65  
    66  		loraRadio.SetStandby()
    67  		time.Sleep(10 * time.Second)
    68  
    69  		println("2 seconds in Continuous Wave")
    70  		loraRadio.SetTxContinuousWave()
    71  		time.Sleep(2 * time.Second)
    72  		println(" Continuous Wave Stopped")
    73  
    74  		loraRadio.SetStandby()
    75  		time.Sleep(60 * time.Second)
    76  	}
    77  }