1010//! - **async** enables async function to create a [Crazyradio] object and use the [SharedCrazyradio]
1111//! - **serde** emables [serde](https://crates.io/crates/serde) serialization/deserialization of the [Channel] struct
1212
13+ #![ deny( missing_docs) ]
14+
1315#[ cfg( feature = "shared_radio" ) ]
1416mod shared_radio;
1517#[ cfg( feature = "shared_radio" ) ]
16- pub use crate :: shared_radio:: SharedCrazyradio ;
18+ pub use crate :: shared_radio:: { SharedCrazyradio , WeakSharedCrazyradio } ;
1719
1820use core:: time:: Duration ;
1921#[ cfg( feature = "serde_support" ) ]
@@ -451,15 +453,15 @@ impl Crazyradio {
451453 }
452454
453455 /// Set inline-settings USB protocol mode
454- ///
456+ ///
455457 /// When this mode is enabled, setting channel, datarate, address and
456458 /// ack_enable will become cached operations, and these settings
457459 /// will be sent as header to the data over USB. This increases performance
458460 /// when communicating with more than one PRX.
459- ///
461+ ///
460462 /// This mode, if available, is activated by default when creating the Crazyradio
461463 /// object.
462- ///
464+ ///
463465 /// This mode is only available with Crazyradio 2.0+
464466 pub fn set_inline_mode ( & mut self , inline_mode_enable : bool ) -> Result < ( ) > {
465467 let setting = inline_mode_enable. then_some ( 1 ) . unwrap_or ( 0 ) ;
@@ -478,8 +480,12 @@ impl Crazyradio {
478480 }
479481
480482 /// Set packet loss simulation.
481- ///
482- pub fn set_packet_loss_simulation ( & mut self , packet_loss_percent : u8 , ack_loss_percent : u8 ) -> Result < ( ) > {
483+ ///
484+ pub fn set_packet_loss_simulation (
485+ & mut self ,
486+ packet_loss_percent : u8 ,
487+ ack_loss_percent : u8 ,
488+ ) -> Result < ( ) > {
483489 if self . device_desciptor . device_version ( ) < rusb:: Version :: from_bcd ( 0x0500 ) {
484490 return Err ( Error :: DongleVersionNotSupported ) ;
485491 }
@@ -511,7 +517,6 @@ impl Crazyradio {
511517 /// be truncated. The length of the ack payload is returned
512518 /// in Ack::length.
513519 pub fn send_packet ( & mut self , data : & [ u8 ] , ack_data : & mut [ u8 ] ) -> Result < Ack > {
514-
515520 if self . inline_mode {
516521 self . send_inline ( data, Some ( ack_data) )
517522 } else {
@@ -538,7 +543,6 @@ impl Crazyradio {
538543 length : received - 1 ,
539544 } )
540545 }
541-
542546 }
543547
544548 /// Send a data packet without caring for Ack (for broadcast communication).
@@ -558,7 +562,7 @@ impl Crazyradio {
558562 }
559563
560564 fn send_inline ( & mut self , data : & [ u8 ] , ack_data : Option < & mut [ u8 ] > ) -> Result < Ack > {
561- const OUT_HEADER_LENGTH : usize = 8 ;
565+ const OUT_HEADER_LENGTH : usize = 8 ;
562566 const IN_HEADER_LENGTH : usize = 2 ;
563567
564568 const OUT_FIELD2_ACK_ENABLE : u8 = 0x10 ;
@@ -582,21 +586,24 @@ impl Crazyradio {
582586 command. extend_from_slice ( & data) ;
583587
584588 let mut answer = [ 0u8 ; 64 ] ;
585- self . device_handle . write_bulk ( 0x01 , & command, Duration :: from_secs ( 1 ) ) ?;
586- self . device_handle . read_bulk ( 0x81 , & mut answer, Duration :: from_secs ( 1 ) ) ?;
589+ self . device_handle
590+ . write_bulk ( 0x01 , & command, Duration :: from_secs ( 1 ) ) ?;
591+ self . device_handle
592+ . read_bulk ( 0x81 , & mut answer, Duration :: from_secs ( 1 ) ) ?;
587593
588594 // Decode answer
589595 let payload_length = ( answer[ 0 ] as usize ) - 2 ;
590596 if let Some ( ack_data) = ack_data {
591- ack_data[ 0 ..payload_length] . copy_from_slice ( & answer[ IN_HEADER_LENGTH ..( IN_HEADER_LENGTH +payload_length) ] ) ;
597+ ack_data[ 0 ..payload_length]
598+ . copy_from_slice ( & answer[ IN_HEADER_LENGTH ..( IN_HEADER_LENGTH + payload_length) ] ) ;
592599 }
593600
594601 Ok ( Ack {
595- received : answer[ 1 ] & IN_HEADER_ACK_RECEIVED != 0 ,
596- power_detector : answer[ 1 ] & IN_HEADER_POWER_DETECTOR != 0 ,
597- retry : ( ( answer[ 1 ] & IN_HEADER_RETRY_MASK ) >> IN_HEADER_RETRY_SHIFT ) as usize ,
598- length : payload_length,
599- } )
602+ received : answer[ 1 ] & IN_HEADER_ACK_RECEIVED != 0 ,
603+ power_detector : answer[ 1 ] & IN_HEADER_POWER_DETECTOR != 0 ,
604+ retry : ( ( answer[ 1 ] & IN_HEADER_RETRY_MASK ) >> IN_HEADER_RETRY_SHIFT ) as usize ,
605+ length : payload_length,
606+ } )
600607 }
601608}
602609
@@ -611,7 +618,6 @@ impl Crazyradio {
611618#[ cfg( feature = "async" ) ]
612619#[ cfg_attr( docsrs, doc( cfg( feature = "async" ) ) ) ]
613620impl Crazyradio {
614-
615621 /// Async vesion of [Crazyradio::open_first()]
616622 pub async fn open_first_async ( ) -> Result < Self > {
617623 let ( tx, rx) = flume:: bounded ( 0 ) ;
@@ -651,14 +657,19 @@ impl Crazyradio {
651657 }
652658}
653659
660+ /// Errors returned by Crazyradio functions
654661#[ derive( thiserror:: Error , Debug , Clone ) ]
655662pub enum Error {
656- #[ error( "Usb Error: {0}:?" ) ]
663+ /// USB error returned by the underlying rusb library
664+ #[ error( "Usb Error: {0:?}" ) ]
657665 UsbError ( rusb:: Error ) ,
666+ /// Crazyradio not found
658667 #[ error( "Crazyradio not found" ) ]
659668 NotFound ,
669+ /// Invalid argument passed to function
660670 #[ error( "Invalid arguments" ) ]
661671 InvalidArgument ,
672+ /// Crazyradio version not supported
662673 #[ error( "Crazyradio version not supported" ) ]
663674 DongleVersionNotSupported ,
664675}
@@ -701,6 +712,9 @@ impl<'de> Deserialize<'de> for Channel {
701712}
702713
703714impl Channel {
715+ /// Create a Channel from its number (0-125)
716+ ///
717+ /// Returns an Error::InvalidArgument if the channel number is out of range
704718 pub fn from_number ( channel : u8 ) -> Result < Self > {
705719 if channel < 126 {
706720 Ok ( Channel ( channel) )
@@ -719,16 +733,23 @@ impl From<Channel> for u8 {
719733/// Radio datarate
720734#[ derive( Copy , Clone , PartialEq ) ]
721735pub enum Datarate {
736+ /// 250 kbps
722737 Dr250K = 0 ,
738+ /// 1 Mbps
723739 Dr1M = 1 ,
740+ /// 2 Mbps
724741 Dr2M = 2 ,
725742}
726743
727744/// Radio power
728745pub enum Power {
746+ /// -18 dBm
729747 Pm18dBm = 0 ,
748+ /// -12 dBm
730749 Pm12dBm = 1 ,
750+ /// -6 dBm
731751 Pm6dBm = 2 ,
752+ /// 0 dBm
732753 P0dBm = 3 ,
733754}
734755
0 commit comments