Skip to content

Commit e59e88a

Browse files
committed
add Rust PWM support
1 parent 5f56b5c commit e59e88a

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

clients/jabi-rs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod peripherals {
66
pub mod gpio;
77
pub mod i2c;
88
pub mod metadata;
9+
pub mod pwm;
910
}
1011

1112
pub use crate::device::Device;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crate::interfaces::InterfaceRequest;
2+
use crate::Error;
3+
use deku::{DekuContainerWrite, DekuUpdate, DekuWrite};
4+
5+
enum Func {
6+
Write = 0,
7+
}
8+
9+
fn gen_req(f: Func, idx: usize, payload: Vec<u8>) -> Result<InterfaceRequest, Error> {
10+
let mut r = InterfaceRequest {
11+
periph_id: crate::InstID::PWM as u16,
12+
periph_idx: idx as u16,
13+
periph_fn: f as u16,
14+
payload_len: 0,
15+
payload: payload,
16+
};
17+
r.update().map_or(Err(Error::PacketFormat), |_| Ok(r))
18+
}
19+
20+
impl crate::Device {
21+
pub fn pwm_write(
22+
&self,
23+
idx: usize,
24+
pulsewidth: std::time::Duration,
25+
period: std::time::Duration,
26+
) -> Result<(), Error> {
27+
if pulsewidth.as_nanos() > u32::MAX.into() || period.as_nanos() > u32::MAX.into() {
28+
return Err(Error::InvalidArgs);
29+
}
30+
#[derive(DekuWrite)]
31+
#[deku(endian = "little")]
32+
struct WriteRequest {
33+
pulsewidth: u32, // ns
34+
period: u32,
35+
}
36+
let req = WriteRequest {
37+
pulsewidth: pulsewidth.as_nanos() as u32,
38+
period: period.as_nanos() as u32,
39+
};
40+
let resp = self.send(&gen_req(Func::Write, idx, req.to_bytes().unwrap())?)?;
41+
if resp.len() == 0 {
42+
Ok(())
43+
} else {
44+
Err(Error::PacketFormat)
45+
}
46+
}
47+
}

examples/jabi-rs/src/main.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::time::Duration;
2+
13
fn test_device(d: &jabi::Device) -> Result<(), jabi::Error> {
24
// Metadata
35
println!(
@@ -21,7 +23,7 @@ fn test_device(d: &jabi::Device) -> Result<(), jabi::Error> {
2123
d.can_write(i, &jabi::CANMessage::new_rtr(0x69420, 2, true))?;
2224
println!("\tSent some messages");
2325

24-
std::thread::sleep(std::time::Duration::from_millis(500));
26+
std::thread::sleep(Duration::from_millis(500));
2527
println!("\tPrinting received messages");
2628
while let Some(msg) = d.can_read(i)? {
2729
println!("\t{msg}");
@@ -41,21 +43,32 @@ fn test_device(d: &jabi::Device) -> Result<(), jabi::Error> {
4143
}
4244
println!();
4345

46+
// PWM (GPIO overrides it until reset)
47+
for i in 0..d.num_inst(jabi::InstID::PWM)? {
48+
println!("\tFlashing PWM {i} at 1Hz");
49+
d.pwm_write(i, Duration::from_millis(500), Duration::from_millis(1000))?;
50+
std::thread::sleep(Duration::from_millis(100));
51+
}
52+
if d.num_inst(jabi::InstID::PWM)? > 0 {
53+
std::thread::sleep(Duration::from_secs(3));
54+
}
55+
4456
// GPIO
4557
for i in 0..d.num_inst(jabi::InstID::GPIO)? {
4658
println!("\tFlashing GPIO {i}");
4759
d.gpio_set_mode(i, jabi::GPIODir::Output, jabi::GPIOPull::None, false)?;
4860
for _ in 0..6 {
4961
d.gpio_write(i, false)?;
50-
std::thread::sleep(std::time::Duration::from_millis(25));
62+
std::thread::sleep(Duration::from_millis(25));
5163
d.gpio_write(i, true)?;
52-
std::thread::sleep(std::time::Duration::from_millis(25));
64+
std::thread::sleep(Duration::from_millis(25));
5365
}
5466
}
5567
for i in 0..d.num_inst(jabi::InstID::GPIO)? {
5668
d.gpio_set_mode(i, jabi::GPIODir::Input, jabi::GPIOPull::Up, false)?;
5769
println!("\tRead GPIO {i} w/ pullups: {}", d.gpio_read(i)?);
5870
}
71+
println!();
5972

6073
Ok(())
6174
}

0 commit comments

Comments
 (0)