g29/
controller.rs

1//!
2//! This module provides functionality to run a separate thread and
3//! initiate reading from the G29 controller
4//!
5//!
6use crate::interface::G29Interface;
7use std::{
8    sync::{Arc, Mutex},
9    thread::{self, JoinHandle},
10};
11
12// ## Controller Struct
13/// The `Controller` struct represents the G29 device and provides methods for controlling and interacting with it.
14#[derive(Debug)]
15pub struct Controller {
16    pub g29: Arc<Mutex<G29Interface>>,
17    reading_thread: Option<JoinHandle<()>>,
18}
19
20impl Controller {
21    /// Creates a new G29 instance.
22    pub fn new() -> Self {
23        Self {
24            g29: Arc::new(Mutex::new(G29Interface::new())),
25            reading_thread: None,
26        }
27    }
28    /// Starts a thread to continuously read input from the G29 device.
29    pub fn start_pumping(&mut self) {
30        let local_g29 = self.g29.clone();
31        self.reading_thread = Some(thread::spawn(move || {
32            local_g29.lock().unwrap().read_loop();
33        }));
34        println!("thread_start_spawn");
35    }
36
37    /// Stops the reading thread.
38    pub fn stop_pumping(&mut self) {
39        if let Some(handle) = self.reading_thread.take() {
40            handle.join().unwrap();
41        } else {
42            println!("No Thread spawned");
43        }
44    }
45}