      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212

    #![feature(integer_atomics)]

    extern crate time;
    extern crate shuteye;
    extern crate kawaii;

    use std::time::Duration;
    use std::thread;
    use std::thread::JoinHandle;
    use std::sync::mpsc::{Sender, Receiver};
    use std::sync::mpsc;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicU32, Ordering};

    use self::kawaii::gpio::{SyncPort, AsyncPort, Direction, Edge, Value};

    #[cfg(feature = "measure")]
    use self::kawaii::Measure;

    use self::time::precise_time_ns;
    use self::shuteye::sleep;

    #[derive(Debug)]
    struct ThreadData<T> {
        thread: JoinHandle<T>,
        tx: Sender<()>,
    }

    #[derive(Debug)]
    pub struct UltrasonicEcho {
        echo: AsyncPort,
        temperature: u8,
        timestamp: u64,
        distance: Arc<AtomicU32>,
    }

    #[derive(Debug)]
    pub struct UltrasonicTrigger {
        trigger: SyncPort,
    }

    #[derive(Debug)]
    pub struct Ultrasonic {
        trigger: Option<ThreadData<()>>,
        echo: Option<ThreadData<()>>,
        pub distance: Arc<AtomicU32>,
    }

    impl Ultrasonic {
        /// Constructs a new `Ultrasonic` and start threads.
        ///
        /// # Threads
        /// - `UltrasonicTrigger`
        /// - `UltrasonicEcho`
        ///
        /// # Parameter
        /// - `trigger_port` GPIO Port number of trigger pin.
        /// - `echo_port` GPIO Port number of echo pin.
        /// - `temperature` Room temperature in °C.
        pub fn new(trigger_port: u8, echo_port: u8, temperature: u8) -> std::io::Result<Ultrasonic> {
            let distance = Arc::new(AtomicU32::new(u32::max_value()));

            /// Create `UltrasonicEcho` thread with sync channel.
            let echo = UltrasonicEcho {
                echo: AsyncPort::new(echo_port, Edge::Both)?,
                temperature: temperature,
                timestamp: 0,
                distance: distance.clone(),
            };
            let (tx, rx): (Sender<()>, Receiver<()>) = mpsc::channel();
            let name = format!("Ultrasonic::echo(port = {})", echo_port);
            let echo = ThreadData::<()> {
                thread: thread::Builder::new()
                    .name(name)
                    .spawn(move || echo.thread(rx))?,
                tx: tx,
            };

            /// Create `UltrasonicTrigger` thread with sync channel.
            let trigger = UltrasonicTrigger { trigger: SyncPort::new(trigger_port, Direction::Out)? };
            let (tx, rx): (Sender<()>, Receiver<()>) = mpsc::channel();
            let name = format!("Ultrasonic::trigger(port = {})", trigger_port);
            let trigger = ThreadData::<()> {
                thread: thread::Builder::new()
                    .name(name)
                    .spawn(move || trigger.thread(rx))?,
                tx: tx,
            };

            Ok(Ultrasonic {
                   trigger: Some(trigger),
                   echo: Some(echo),
                   distance: distance,
               })
        }
    }

    impl Drop for Ultrasonic {
        /// Drop sync channels and join threads.
        fn drop(&mut self) {
            if let Some(echo) = self.echo.take() {
                echo.tx.send(()).is_ok();
                echo.thread.join().is_ok();
            }

            if let Some(trigger) = self.trigger.take() {
                trigger.tx.send(()).is_ok();
                trigger.thread.join().is_ok();
            }
        }
    }

    impl UltrasonicTrigger {
        /// `UltrasonicTrigger` thread function.
        fn thread(mut self, stop_rx: Receiver<()>) {
            #[cfg(feature = "measure")]
            let mut measure = Measure::new(format!("Ultrasonic::trigger(port = {})", self.trigger.port.number));

            while stop_rx.try_recv().is_err() {
                #[cfg(feature = "measure")]
                measure.start();
                self.trigger.write(Value::Low).is_ok();
                #[cfg(feature = "measure")]
                measure.pause();

                // sleep 10us (min length to trigger)
                sleep(Duration::new(0, 10_000));

                #[cfg(feature = "measure")]
                measure.start();
                self.trigger.write(Value::High).is_ok();
                #[cfg(feature = "measure")]
                measure.stop();

                // sleep 20ms (max trigger frequency)
                sleep(Duration::new(0, 20_000_000));
            }
        }
    }

    impl UltrasonicEcho {
        /// `UltrasonicEcho` thread function.
        fn thread(mut self, stop_rx: Receiver<()>) {
            #[cfg(feature = "measure")]
            let mut measure = Measure::new(format!("Ultrasonic::echo(port = {})", self.echo.port.number));

            while stop_rx.try_recv().is_err() {
                #[cfg(feature = "measure")]
                measure.start();

                #[cfg(not(feature = "measure"))]
                let value = self.echo.poll(Some(Duration::new(1, 0)));

                #[cfg(feature = "measure")]
                let value = self.echo.poll_measure(Some(Duration::new(1, 0)), &mut measure);

                match value {
                    Ok(value) => {
                        if let Some(value) = value {
                            self.on_value_changed(value);
                        }
                    }
                    Err(_e) => {
                        #[cfg(debug_assertions)]
                        println!("POLL failed: e = {:?}", _e);
                    }
                }

                #[cfg(feature = "measure")]
                measure.stop();
            }
        }

        /// Start time measure or calculates distance based on port value
        fn on_value_changed(&mut self, value: Value) {
            match value {
                Value::High => {
                    // Switched from Value::High to Value::High
                    // possibly because of trigger after timeout and slow reading
                    if self.timestamp > 0 {
                        #[cfg(debug_assertions)]
                        println!("{:?}", self);

                        self.calc_distance();
                    }

                    self.timestamp = precise_time_ns();
                }
                Value::Low => {
                    // Switched from Value::Low to Value::Low
                    if self.timestamp == 0 {
                        #[cfg(debug_assertions)]
                        println!("{:?}", self);
                    }

                    self.calc_distance();
                }
            }
        }

        /// Calculates distance based on time measurement and `temperature` and stores it in `distance`
        fn calc_distance(&mut self) {
            let time_diff = precise_time_ns() - self.timestamp;
            let distance = (3315 + self.temperature as u64 * 6) * time_diff / 20_000_000;
            self.distance.store(distance as u32, Ordering::Relaxed);

            #[cfg(debug_assertions)]
            println!("time diff: {}\tdistance: {}mm", time_diff, distance);

            self.timestamp = 0u64;
        }
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)