sx127x_lora

The sx127xlora_ crate provides an interface to Semtech SX1276/77/78/79 based boards. Due to its dependency on the rppal crate, the library currently only works on Raspberry Pi. It requires that both the SPI and GPIO interfaces are enabled on the Pi. This can accomplished with sudo raspi-config and a reboot. This library is under heavy development is subject to changes

Examples

Basic send

rust extern crate sx127x_lora; const LORA_CS_PIN: u8 = 8; const DIO_0_PIN: u8 = 20; const LORA_RESET_PIN: u8 = 21; const FREQUENCY: i64 = 915; fn main(){ let mut lora = sx127x_lora::LoRa::new(LORA_CS_PIN,LORA_RESET_PIN, DIO_0_PIN,FREQUENCY).unwrap(); lora.set_tx_power(17,1); //Using PA_BOOST. See your board for correct pin. let transmit = lora.transmit_string("Hello World!".to_string()); match transmit { Ok(packet_size) => println!("Sent packet with size: {}", packet_size), Err(e) => println!("Error: {}", e), } }

Asynchronous send and receive

Utilizes a Arc<Mutex<sx127x_lora::LoRa>> to allow for an asynchronous interrupt to be handled when a new packet is received. The lazy_static crate is used to create a global static ref. The example is utilizes the rppal crate directly to setup the interrupt pin. This will change in the future. ```rust

[macro_use]

extern crate lazystatic; extern crate sx127xlora;

use rppal::gpio::{Level,Trigger}; use std::time::Duration; use std::{thread}; use std::sync::{Arc, Mutex};

const LORACSPIN: u8 = 8; const DIO0PIN: u8 = 20; const LORARESETPIN: u8 = 21; const FREQUENCY: i64 = 915;

lazystatic! { static ref LORA: Arc> = Arc::new(Mutex::new(sx127xlora::LoRa:: new(LORACSPIN,LORARESETPIN, DIO0PIN,FREQUENCY).unwrap())); }

fn main() { let loraclone = LORA.clone(); let mut lora = loraclone.lock().unwrap(); lora.settxpower(17,1); lora.gpio.setasyncinterrupt(DIO0PIN,Trigger::RisingEdge,handlenewpacket); lora.setmode(sx127xlora::RadioMode::RxContinuous); drop(lora); loop{ let mut lora = loraclone.lock().unwrap(); let transmit = lora.transmitstring("Hello world!".tostring()); match transmit { Ok(s) => println!("Sent packet with size: {}", s), Err(e) => println!("Error: {}",e), } lora.setmode(sx127xlora::RadioMode::RxContinuous); drop(lora); thread::sleep(Duration::frommillis(5000)); } }

fn handlenewpacket(level: Level){ let loraclone = LORA.clone(); let mut lora = loraclone.lock().unwrap(); println!("New Packet with rssi: {} and SNR: {}",lora.getpacketrssi(), lora.getpacketsnr()); let buffer = lora.readpacket(); print!("Payload: "); for i in buffer.iter() { print!("{}",*i as char); } println!(); lora.setmode(sx127xlora::RadioMode::RxContinuous); drop(lora); } ```