mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-19 06:03:57 +00:00
examples: lora atcmd minimal rx and tx
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
@@ -11,7 +11,7 @@ func quicktest() {
|
|||||||
|
|
||||||
// Check firmware version.
|
// Check firmware version.
|
||||||
func version() {
|
func version() {
|
||||||
writeCommandOutput("VER", currentVersion()+" "+firmwareVersion())
|
writeCommandOutput("VER", currentVersion()+" ("+firmwareVersion()+")")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use to check the ID of the LoRaWAN module, or change the ID.
|
// Use to check the ID of the LoRaWAN module, or change the ID.
|
||||||
@@ -172,7 +172,7 @@ func power(setting string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unconfirmed message repeats times.
|
// Unconfirmed message repeats times.
|
||||||
func rept(setting string) error {
|
func rept(setting string) error {
|
||||||
cmd := "REPT"
|
cmd := "REPT"
|
||||||
writeCommandOutput(cmd, setting)
|
writeCommandOutput(cmd, setting)
|
||||||
@@ -180,9 +180,9 @@ func rept(setting string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Confirmed message retry times. Valid range 0~254,
|
// Confirmed message retry times. Valid range 0~254,
|
||||||
// if retry times is less than 2, only one message will
|
// if retry times is less than 2, only one message will
|
||||||
// be sent. Random delay 3 - 10s between each retry
|
// be sent. Random delay 3 - 10s between each retry
|
||||||
// (band duty cycle limitation has the priority)
|
// (band duty cycle limitation has the priority)
|
||||||
func retry(setting string) error {
|
func retry(setting string) error {
|
||||||
cmd := "RETRY"
|
cmd := "RETRY"
|
||||||
@@ -329,12 +329,38 @@ func log(setting string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func crlf() {
|
func recv(setting string) error {
|
||||||
|
cmd := "RECV"
|
||||||
|
|
||||||
|
data, err := lorarx()
|
||||||
|
if err != nil {
|
||||||
|
writeCommandOutput(cmd, "ERROR "+err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
writeCommandOutput(cmd, string(data))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func recvhex(setting string) error {
|
||||||
|
cmd := "RECVHEX"
|
||||||
|
|
||||||
|
data, err := lorarx()
|
||||||
|
if err != nil {
|
||||||
|
writeCommandOutput(cmd, "ERROR "+err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
writeCommandOutput(cmd, string(data))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func crlf() {
|
||||||
uart.Write([]byte("\r\n"))
|
uart.Write([]byte("\r\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeCommandOutput(cmd, data string) {
|
func writeCommandOutput(cmd, data string) {
|
||||||
uart.Write([]byte("+"+cmd+": "))
|
uart.Write([]byte("+" + cmd + ": "))
|
||||||
uart.Write([]byte(data))
|
uart.Write([]byte(data))
|
||||||
crlf()
|
crlf()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,12 +15,12 @@ import (
|
|||||||
|
|
||||||
// change these to test a different UART or pins if available
|
// change these to test a different UART or pins if available
|
||||||
var (
|
var (
|
||||||
uart = machine.Serial
|
uart = machine.Serial
|
||||||
tx = machine.UART_TX_PIN
|
tx = machine.UART_TX_PIN
|
||||||
rx = machine.UART_RX_PIN
|
rx = machine.UART_RX_PIN
|
||||||
input = make([]byte, 0, 64)
|
input = make([]byte, 0, 64)
|
||||||
|
|
||||||
radio LoraRadio
|
radio LoraRadio
|
||||||
defaultTimeout uint32 = 1000
|
defaultTimeout uint32 = 1000
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -55,11 +55,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func fail(msg string) {
|
func fail(msg string) {
|
||||||
for {
|
for {
|
||||||
uart.Write([]byte(msg))
|
uart.Write([]byte(msg))
|
||||||
crlf()
|
crlf()
|
||||||
|
|
||||||
time.Sleep(time.Minute)
|
time.Sleep(time.Minute)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,6 +99,10 @@ func parseCommand(cmd, args string) error {
|
|||||||
test(args)
|
test(args)
|
||||||
case "LOG":
|
case "LOG":
|
||||||
log(args)
|
log(args)
|
||||||
|
case "RECV":
|
||||||
|
recv(args)
|
||||||
|
case "RECVHEX":
|
||||||
|
recvhex(args)
|
||||||
default:
|
default:
|
||||||
return errInvalidCommand
|
return errInvalidCommand
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
errRadioNotFound = errors.New("radio not found")
|
errRadioNotFound = errors.New("radio not found")
|
||||||
|
errRxTimeout = errors.New("radio RX timeout")
|
||||||
)
|
)
|
||||||
|
|
||||||
type LoraRadio interface {
|
type LoraRadio interface {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ package main
|
|||||||
|
|
||||||
// do simulator setup here
|
// do simulator setup here
|
||||||
func setupLora() (LoraRadio, error) {
|
func setupLora() (LoraRadio, error) {
|
||||||
return &SimLoraRadio{}, nil
|
return &SimLoraRadio{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type SimLoraRadio struct {
|
type SimLoraRadio struct {
|
||||||
@@ -21,13 +21,17 @@ func (sr *SimLoraRadio) LoraRx(timeoutMs uint32) ([]uint8, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sr *SimLoraRadio) SetLoraFrequency(freq uint32) {}
|
func (sr *SimLoraRadio) SetLoraFrequency(freq uint32) {}
|
||||||
func (sr *SimLoraRadio) SetLoraIqMode(mode uint8) {}
|
func (sr *SimLoraRadio) SetLoraIqMode(mode uint8) {}
|
||||||
func (sr *SimLoraRadio) SetLoraCodingRate(cr uint8) {}
|
func (sr *SimLoraRadio) SetLoraCodingRate(cr uint8) {}
|
||||||
func (sr *SimLoraRadio) SetLoraBandwidth(bw uint8) {}
|
func (sr *SimLoraRadio) SetLoraBandwidth(bw uint8) {}
|
||||||
func (sr *SimLoraRadio) SetLoraCrc(enable bool) {}
|
func (sr *SimLoraRadio) SetLoraCrc(enable bool) {}
|
||||||
func (sr *SimLoraRadio) SetLoraSpreadingFactor(sf uint8) {}
|
func (sr *SimLoraRadio) SetLoraSpreadingFactor(sf uint8) {}
|
||||||
|
|
||||||
func firmwareVersion() string {
|
func firmwareVersion() string {
|
||||||
return "Simulator "+currentVersion()
|
return "Simulator " + currentVersion()
|
||||||
|
}
|
||||||
|
|
||||||
|
func lorarx() ([]byte, error) {
|
||||||
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ func setupLora() (LoraRadio, error) {
|
|||||||
|
|
||||||
loraRadio.LoraConfig(loraConf)
|
loraRadio.LoraConfig(loraConf)
|
||||||
|
|
||||||
return loraRadio, nil
|
return loraRadio, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// radioIntHandler will take care of radio interrupts
|
// radioIntHandler will take care of radio interrupts
|
||||||
@@ -63,3 +63,7 @@ func radioIntHandler(intr interrupt.Interrupt) {
|
|||||||
func firmwareVersion() string {
|
func firmwareVersion() string {
|
||||||
return "sx126x"
|
return "sx126x"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func lorarx() ([]byte, error) {
|
||||||
|
return loraRadio.LoraRx(LORA_DEFAULT_RXTIMEOUT_MS)
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,15 +11,19 @@ import (
|
|||||||
"tinygo.org/x/drivers/sx127x"
|
"tinygo.org/x/drivers/sx127x"
|
||||||
)
|
)
|
||||||
|
|
||||||
const FREQ = 868100000
|
const (
|
||||||
|
FREQ = 868100000
|
||||||
|
LORA_DEFAULT_RXTIMEOUT_MS = 1000
|
||||||
|
LORA_DEFAULT_TXTIMEOUT_MS = 5000
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// We assume LoRa Featherwing module is connected to PyBadge:
|
// We assume LoRa Featherwing module is connected to PyBadge:
|
||||||
rstPin = machine.D11
|
rstPin = machine.D11
|
||||||
csPin = machine.D10
|
csPin = machine.D10
|
||||||
dio0Pin = machine.D6
|
dio0Pin = machine.D6
|
||||||
dio1Pin = machine.D9
|
dio1Pin = machine.D9
|
||||||
spi = machine.SPI0
|
spi = machine.SPI0
|
||||||
loraRadio *sx127x.Device
|
loraRadio *sx127x.Device
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -64,15 +68,18 @@ func setupLora() (LoraRadio, error) {
|
|||||||
|
|
||||||
loraRadio.LoraConfig(loraConf)
|
loraRadio.LoraConfig(loraConf)
|
||||||
|
|
||||||
return loraRadio, nil
|
return loraRadio, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func dioIrqHandler(machine.Pin) {
|
func dioIrqHandler(machine.Pin) {
|
||||||
loraRadio.HandleInterrupt()
|
loraRadio.HandleInterrupt()
|
||||||
}
|
}
|
||||||
|
|
||||||
func firmwareVersion() string {
|
func firmwareVersion() string {
|
||||||
v := loraRadio.GetVersion()
|
v := loraRadio.GetVersion()
|
||||||
return "sx127x v"+strconv.Itoa(int(v))
|
return "sx127x v" + strconv.Itoa(int(v))
|
||||||
|
}
|
||||||
|
|
||||||
|
func lorarx() ([]byte, error) {
|
||||||
|
return loraRadio.LoraRx(LORA_DEFAULT_RXTIMEOUT_MS)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,4 +4,4 @@ const VERSION = "0.0.1"
|
|||||||
|
|
||||||
func currentVersion() string {
|
func currentVersion() string {
|
||||||
return VERSION
|
return VERSION
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user