Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/without tether #7

Merged
merged 9 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tether-artnet-controller"
version = "0.3.3"
version = "0.4.0"
edition = "2021"
repository = "https://github.com/RandomStudio/tether-artnet-controller"
authors = ["Stephen Buchanan"]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ If you have Tether Egui installed (`cargo install tether-egui`) then the easiest
- [ ] Project JSON should save ArtNet configuration (but can override via CLI args)
- [x] Colour conversions should be possible manually, e.g. RGB -> CMY
- [x] With macros, add some visual indicators of state, e.g. Colour, Brightness and Pan/Tilt
- [ ] Allow the app to launch just fine without Tether
- [x] Allow the app to launch just fine without Tether
- [ ] Allow the app to launch without any project file at all
- [ ] Add 16-bit control, at least for macros (single slider adjusts the two channels as split between first and second 8-bit digits)
- [ ] ArtNet on separate thread, with more precise timing; this might require some messaging back and forth and/or mutex
28 changes: 3 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use std::{net::SocketAddr, sync::mpsc, time::Duration};

use env_logger::Env;
use log::{debug, error, info};
use log::{debug, info};

use clap::Parser;

use crate::{
artnet::{ArtNetInterface, ArtNetMode},
model::Model,
settings::Cli,
tether_interface::start_tether_thread,
ui::SIMPLE_WIN_SIZE,
};

Expand All @@ -33,14 +32,6 @@ fn main() {

debug!("Started with settings: {:?}", cli);

let mut handles = Vec::new();

let (tether_tx, tether_rx) = mpsc::channel();
let (quit_tether_tx, quit_tether_rx) = mpsc::channel();
let tether_handle = start_tether_thread(tether_tx.clone(), quit_tether_rx);

handles.push(tether_handle);

let artnet = {
if cli.artnet_broadcast {
ArtNetInterface::new(ArtNetMode::Broadcast, cli.artnet_update_frequency)
Expand All @@ -55,7 +46,7 @@ fn main() {
}
};

let mut model = Model::new(tether_rx, cli.clone(), artnet);
let mut model = Model::new(cli.clone(), artnet);

if cli.headless_mode {
info!("Running in headless mode; Ctrl+C to quit");
Expand All @@ -77,7 +68,6 @@ fn main() {
std::thread::sleep(Duration::from_millis(1));
model.update();
}
model.reset_before_quit();
} else {
info!("Running graphics mode; close the window to quit");
let options = eframe::NativeOptions {
Expand All @@ -93,19 +83,7 @@ fn main() {
.expect("Failed to launch GUI");
info!("GUI ended; exit soon...");
}
quit_tether_tx
.send(())
.expect("failed to send quit message via channel");
for h in handles {
match h.join() {
Ok(()) => {
debug!("Thread join OK");
}
Err(e) => {
error!("Thread joined with error, {:?}", e);
}
}
}

std::thread::sleep(Duration::from_secs(1));
info!("...Exit now");
std::process::exit(0);
Expand Down
43 changes: 32 additions & 11 deletions src/model.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
sync::mpsc::Receiver,
sync::{Arc, Mutex},
thread::JoinHandle,
time::{Duration, SystemTime},
};

Expand All @@ -14,9 +15,9 @@ use crate::{
settings::{Cli, CHANNELS_PER_UNIVERSE},
tether_interface::{
RemoteControlMessage, RemoteMacroMessage, RemoteMacroValue, RemoteSceneMessage,
TetherControlChangePayload, TetherMidiMessage, TetherNotePayload,
TetherControlChangePayload, TetherInterface, TetherMidiMessage, TetherNotePayload,
},
ui::{render_gui, ViewMode},
ui::{attempt_connection, render_gui, ViewMode},
};

#[derive(PartialEq)]
Expand All @@ -26,10 +27,18 @@ pub enum BehaviourOnExit {
Zero,
}

pub enum TetherStatus {
NotConnected,
Connected,
Errored(String),
}

pub struct Model {
pub handles: Vec<JoinHandle<()>>,
pub channels_state: Vec<u8>,
pub channels_assigned: Vec<bool>,
pub tether_rx: Receiver<RemoteControlMessage>,
pub tether_interface: TetherInterface,
pub tether_status: TetherStatus,
pub settings: Cli,
pub artnet: ArtNetInterface,
pub project: Project,
Expand All @@ -43,6 +52,7 @@ pub struct Model {
pub save_on_exit: bool,
pub show_confirm_exit: bool,
pub allowed_to_close: bool,
pub should_quit: Arc<Mutex<bool>>,
}

impl eframe::App for Model {
Expand All @@ -57,11 +67,7 @@ impl eframe::App for Model {
}

impl Model {
pub fn new(
tether_rx: Receiver<RemoteControlMessage>,
settings: Cli,
artnet: ArtNetInterface,
) -> Model {
pub fn new(settings: Cli, artnet: ArtNetInterface) -> Model {
let mut current_project_path = None;

let project = match Project::load(&settings.project_path) {
Expand Down Expand Up @@ -90,8 +96,16 @@ impl Model {
}
}

let should_quit = Arc::new(Mutex::new(false));

let tether_interface = TetherInterface::new();

let should_auto_connect = !settings.tether_disable_autoconnect;

let mut model = Model {
tether_rx,
tether_status: TetherStatus::NotConnected,
handles: Vec::new(),
tether_interface,
channels_state: Vec::new(),
channels_assigned,
settings,
Expand All @@ -105,8 +119,14 @@ impl Model {
save_on_exit: true,
show_confirm_exit: false,
allowed_to_close: false,
should_quit,
};

if should_auto_connect {
info!("Auto connect Tether enabled; will attempt to connect now...");
attempt_connection(&mut model)
}

model.apply_home_values();

model
Expand All @@ -115,7 +135,7 @@ impl Model {
pub fn update(&mut self) {
let mut work_done = false;

while let Ok(m) = self.tether_rx.try_recv() {
while let Ok(m) = self.tether_interface.message_rx.try_recv() {
work_done = true;
self.apply_macros = true;
match m {
Expand Down Expand Up @@ -513,6 +533,7 @@ impl Model {
}

pub fn reset_before_quit(&mut self) {
*self.should_quit.lock().unwrap() = true;
if self.save_on_exit {
info!("Save-on-exit enabled; will save current project if loaded...");
if let Some(existing_project_path) = &self.current_project_path {
Expand Down
8 changes: 8 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ pub struct Cli {

#[arg(long = "auto.random")]
pub auto_random: bool,

/// Flag to disable Tether connect on start (GUI only)
#[arg(long = "tether.noAutoConnect")]
pub tether_disable_autoconnect: bool,

/// Host/IP for Tether MQTT Broker
#[arg(long = "tether.host")]
pub tether_host: Option<String>,
}
Loading
Loading