aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2018-09-04 09:20:59 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2018-09-04 09:20:59 +0200
commit359a6ebaf59aa19699cbd61b07406999e6732373 (patch)
treed758f8dad49094ac6a7aa15dd0e817b2cf3c1cc5 /src/main.rs
parent4c9f3c8dbfc67d9e1f8479538ba9c9b6dc3be5c4 (diff)
downloadraspi-rfm95-kiss-master.tar.gz
raspi-rfm95-kiss-master.tar.bz2
raspi-rfm95-kiss-master.zip
Rework rfm95 and kissHEADmaster
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs70
1 files changed, 54 insertions, 16 deletions
diff --git a/src/main.rs b/src/main.rs
index 134ee11..0a562ee 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,12 +3,13 @@
extern crate libc;
extern crate raspi_rfm95_kiss;
-use raspi_rfm95_kiss::rfm95::{RF95, Bandwidth, CodingRate, SpreadingFactor};
+use raspi_rfm95_kiss::rfm95::{RF95EventType, RF95, Bandwidth, CodingRate, SpreadingFactor, MAX_MTU};
+use raspi_rfm95_kiss::kiss::{KissDecoder, KissEncoder};
use std::fs::{OpenOptions, File};
use std::ffi::{CStr, CString};
use std::os::unix::io::AsRawFd;
-
-const MAX_MTU: usize = 251;
+use std::thread;
+use std::sync::{Arc, Mutex};
extern {
fn kissattach(
@@ -41,7 +42,7 @@ fn create_pts_pair() -> std::io::Result<File> {
fn main() {
eprintln!("Creating PTY pair");
- let master_file = match create_pts_pair() {
+ let mut master_file = match create_pts_pair() {
Ok(fd) => fd,
Err(e) => panic!("create_pts_pair failed: {}", e)
};
@@ -61,7 +62,7 @@ fn main() {
eprintln!("PTS slave: {:?}", slice);
}
- match RF95::new(Bandwidth::Bw250, CodingRate::Cr8, SpreadingFactor::Sf10) {
+ let radio = match RF95::new(Bandwidth::Bw250, CodingRate::Cr8, SpreadingFactor::Sf10) {
Ok(mut radio) => {
radio.reset().expect("radio reset");
@@ -71,13 +72,14 @@ fn main() {
else {
eprintln!("Cannot read device version");
}
+ radio
},
Err(e) => panic!("Cannot create lora radio object, {}", e)
};
let callsign = CString::new("HB9EGM-1").expect("Failed to convert callsign to CString");
let speed : i32 = 9600;
- let mtu : i32 = 251;
+ let mtu = MAX_MTU as i32;
let allow_broadcast : i32 = 1;
let success = unsafe {
@@ -93,21 +95,57 @@ fn main() {
panic!("kissattach failed");
}
- let todo = "Read/Write from/to TTY and RFM95";
- /*
- let master_tty = Box::new(master_file);
+ let radio = Arc::new(Mutex::new(radio));
+
+ let mut pty_tx_side = master_file.try_clone()
+ .expect("Cannot clone PTY file");
+
+ let radio_tx = radio.clone();
+
+ let writer = thread::spawn(move || {
+ let mut kiss_decoder = KissDecoder::new(&mut pty_tx_side)
+ .expect("KissDecoder creation failed");
- let writer = thread::spawn(|| {
loop {
- let mut buffer = [0; MAX_MTU];
- match *master_tty.read(&mut buffer[..]) {
- Ok(n) => radio.write_buffer(buffer),
- Err(e) => panic!("TTY Read error {}", e);
- }
+ let frame_to_tx = match kiss_decoder.receive_frame() {
+ Ok(f) => f,
+ Err(e) => {
+ println!("Error decoding KISS: {}", e);
+ continue
+ }
+ };
+
+ radio_tx.lock().unwrap().send(frame_to_tx)
+ .expect("Failed to send frame");
}
});
+ let mut kiss_encoder = KissEncoder::new(&mut master_file)
+ .expect("KissEncoder creation failed");
+
+ loop {
+ let timeout = std::time::Duration::from_millis(100);
+ match radio.lock().unwrap().wait_for_event(timeout) {
+ Ok(msg) => match msg {
+ RF95EventType::None => println!("radio: None"),
+ RF95EventType::DataReceived(buf) => {
+ println!("radio: Data received {:?}", buf);
+
+ kiss_encoder.send_frame(buf.as_slice()).expect("Failed to encode frame");
+ },
+ RF95EventType::DataSent => println!("radio: Data sent"),
+ RF95EventType::ErrorPinConfig => println!("radio: Error pin config"),
+ RF95EventType::ErrorTimedOut => println!("radio: Timeout"),
+ RF95EventType::ErrorWrongCrc => println!("radio: Wrong CRC"),
+ RF95EventType::ErrorCommBus => println!("radio: Comm Bus"),
+ },
+ Err(e) => {
+ println!("Receiver channel closing: {}", e);
+ break;
+ },
+ }
+ }
+
writer.join().unwrap();
- */
}