stub base station
This commit is contained in:
@@ -1,3 +1,24 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use ferris_says::say;
|
||||
use std::io::{stdout, BufWriter};
|
||||
|
||||
mod sdr;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let stdout = stdout();
|
||||
let message = String::from("Welcome to the Cellular Revolution!");
|
||||
let width = message.chars().count();
|
||||
|
||||
let mut writer = BufWriter::new(stdout.lock());
|
||||
say(&message, width, &mut writer).unwrap();
|
||||
|
||||
// test SDR stuff
|
||||
let device_args = sdr::find_device()?;
|
||||
println!("Using device: {}", device_args);
|
||||
|
||||
let transmitter = sdr::SdrTransmitter::new(device_args, 0)?;
|
||||
|
||||
transmitter.transmit_tone(800e6, 1e6, 10e3)?;
|
||||
|
||||
Ok(())
|
||||
|
||||
}
|
||||
|
60
cellular/src/sdr.rs
Normal file
60
cellular/src/sdr.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use soapysdr::Direction::Tx;
|
||||
use soapysdr::{Device, Args};
|
||||
use num_complex::Complex;
|
||||
use std::f64::consts::PI;
|
||||
|
||||
pub struct SdrTransmitter {
|
||||
dev: Device,
|
||||
channel: usize,
|
||||
}
|
||||
|
||||
impl SdrTransmitter {
|
||||
// Create a new transmitter instance with specific device args
|
||||
pub fn new(args: Args, channel: usize) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let dev = Device::new(args)?;
|
||||
Ok(SdrTransmitter { dev, channel })
|
||||
}
|
||||
|
||||
// Transmit a tone
|
||||
pub fn transmit_tone(
|
||||
&self,
|
||||
frequency: f64,
|
||||
sample_rate: f64,
|
||||
tone_freq: f64,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
self.dev.set_frequency(Tx, self.channel, frequency, ())?;
|
||||
self.dev.set_sample_rate(Tx, self.channel, sample_rate)?;
|
||||
|
||||
let samples: Vec<Complex<f32>> = (0..1000)
|
||||
.map(|i| {
|
||||
let t = i as f64 / sample_rate;
|
||||
let phase = 2.0 * PI * tone_freq * t;
|
||||
Complex::new(phase.cos() as f32, phase.sin() as f32) * 0.5
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut tx_stream = self.dev.tx_stream::<Complex<f32>>(&[self.channel])?;
|
||||
tx_stream.activate(None)?;
|
||||
|
||||
println!(
|
||||
"Transmitting {} kHz tone at {} MHz...",
|
||||
tone_freq / 1e3,
|
||||
frequency / 1e6
|
||||
);
|
||||
for _ in 0..100 {
|
||||
tx_stream.write(&[&samples], Some(100_000), false, 5)?;
|
||||
}
|
||||
|
||||
tx_stream.deactivate(None)?;
|
||||
println!("Transmission complete!");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to create Args for a specific device
|
||||
pub fn device_args(driver: &str, serial: Option<&str>) -> Args {
|
||||
match serial {
|
||||
Some(serial) => Args::from(&format!("driver={},serial={}", driver, serial)),
|
||||
None => Args::from(&format!("driver={}", driver)),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user