Skip to content

Commit

Permalink
use clock_nanosleep() in bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Nov 10, 2024
1 parent 6cf77ac commit b285a70
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions bindings/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ napi-build = "2.0.1"

[target.'cfg(target_os = "linux")'.dependencies]
linux-embedded-hal = "0.4.0"
nix = { version = "0.29.0", features = ["time"]}
rf24-rs = { path = "../../crates/rf24-rs", features = ["std"] }
21 changes: 19 additions & 2 deletions bindings/node/src/radio.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
#![cfg(target_os = "linux")]

use std::time::Duration;

use crate::config::RadioConfig;
use crate::types::{
coerce_to_bool, AvailablePipe, CrcLength, DataRate, FifoState, HardwareConfig, PaLevel,
StatusFlags, WriteConfig,
};

use embedded_hal::digital::OutputPin;
use embedded_hal::{delay::DelayNs, digital::OutputPin};
use linux_embedded_hal::{
gpio_cdev::{chips, LineRequestFlags},
spidev::{SpiModeFlags, SpidevOptions},
CdevPin, Delay, SpidevDevice,
CdevPin, SpidevDevice,
};
use nix::sys::time::TimeSpec;
use nix::time::{clock_nanosleep, ClockId, ClockNanosleepFlags};

use napi::{bindgen_prelude::Buffer, Error, JsNumber, Result, Status};

use rf24::radio::prelude::*;

struct Delay;

impl DelayNs for Delay {
fn delay_ns(&mut self, ns: u32) {
clock_nanosleep(
ClockId::CLOCK_REALTIME,
ClockNanosleepFlags::empty(),
&TimeSpec::from_duration(Duration::from_nanos(ns as u64)),
)
.unwrap_or_else(|e| panic!("delay_ns({ns}) failed. {e:?}"));
}
}

/// This class provides the user facing API to interact with a nRF24L01 transceiver.
#[napi(js_name = "RF24")]
pub struct RF24 {
Expand Down
1 change: 1 addition & 0 deletions bindings/python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ pyo3 = {version = "0.22.5", features = ["extension-module"]}

[target.'cfg(target_os = "linux")'.dependencies]
linux-embedded-hal = "0.4.0"
nix = { version = "0.29.0", features = ["time"]}
rf24-rs = { path = "../../crates/rf24-rs", features = ["std"]}
24 changes: 20 additions & 4 deletions bindings/python/src/radio.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
#![cfg(target_os = "linux")]
use std::borrow::Cow;
use std::time::Duration;

use crate::config::RadioConfig;
use crate::types::{CrcLength, DataRate, FifoState, PaLevel, StatusFlags};
use embedded_hal::digital::OutputPin;
use embedded_hal::{delay::DelayNs, digital::OutputPin};
use linux_embedded_hal::{
gpio_cdev::{chips, LineRequestFlags},
spidev::{SpiModeFlags, SpidevOptions},
CdevPin, Delay, SpidevDevice,
CdevPin, SpidevDevice,
};
use nix::sys::time::TimeSpec;
use nix::time::{clock_nanosleep, ClockId, ClockNanosleepFlags};

use pyo3::{
exceptions::{PyOSError, PyRuntimeError, PyValueError},
prelude::*,
};
use rf24::radio::prelude::*;

struct Delay;

impl DelayNs for Delay {
fn delay_ns(&mut self, ns: u32) {
clock_nanosleep(
ClockId::CLOCK_REALTIME,
ClockNanosleepFlags::empty(),
&TimeSpec::from_duration(Duration::from_nanos(ns as u64)),
)
.unwrap_or_else(|e| panic!("delay_ns({ns}) failed. {e:?}"));
}
}

/// Construct an object to control the radio.
///
/// Parameters:
Expand Down Expand Up @@ -91,12 +107,12 @@ impl RF24 {
)
)
})?;
let config = SpidevOptions::new()
let spi_config = SpidevOptions::new()
.max_speed_hz(spi_speed)
.mode(SpiModeFlags::SPI_MODE_0)
.bits_per_word(8)
.build();
spi.configure(&config)
spi.configure(&spi_config)
.map_err(|e| PyOSError::new_err(format!("{e:?}")))?;

Ok(Self {
Expand Down
3 changes: 3 additions & 0 deletions cspell.config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ words:
- fontawesome
- gnueabihf
- gpio
- gpiocdev
- gpiochip
- gpiod
- HLINE
Expand All @@ -42,6 +43,8 @@ words:
- mosi
- msvc
- musleabihf
- nanos
- nanosleep
- napi
- nocbreak
- noecho
Expand Down
2 changes: 1 addition & 1 deletion examples/node/ts/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class App {
channel = 0;
sweeps += 1;
}
if (sweeps > 15) {
if (sweeps >= 15) {
endl = true;
sweeps = 0;
// reset total signal counts for all channels
Expand Down

0 comments on commit b285a70

Please sign in to comment.