g29/
controller.rs

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
//!
//! This module provides functionality to run a separate thread and
//! initiate reading from the G29 controller
//!
//!
use crate::interface::G29Interface;
use std::{
    sync::{Arc, Mutex},
    thread::{self, JoinHandle},
};

// ## Controller Struct
/// The `Controller` struct represents the G29 device and provides methods for controlling and interacting with it.
#[derive(Debug)]
pub struct Controller {
    pub g29: Arc<Mutex<G29Interface>>,
    reading_thread: Option<JoinHandle<()>>,
}

impl Controller {
    /// Creates a new G29 instance.
    pub fn new() -> Self {
        Self {
            g29: Arc::new(Mutex::new(G29Interface::new())),
            reading_thread: None,
        }
    }
    /// Starts a thread to continuously read input from the G29 device.
    pub fn start_pumping(&mut self) {
        let local_g29 = self.g29.clone();
        self.reading_thread = Some(thread::spawn(move || {
            local_g29.lock().unwrap().read_loop();
        }));
        println!("thread_start_spawn");
    }

    /// Stops the reading thread.
    pub fn stop_pumping(&mut self) {
        if let Some(handle) = self.reading_thread.take() {
            handle.join().unwrap();
        } else {
            println!("No Thread spawned");
        }
    }
}