Skip to content
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
34 changes: 34 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ derivative = "2.2.0"
clap = { version = "4.5.60", features = ["derive"] }
async-stream = "0.3.6"
rand = "0.10.0"
tracing-appender = "0.2.4"

[dev-dependencies]
mockall = "0.14.0"
2 changes: 2 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ runner := if x'${env:-local}' == "docker" {
"docker exec -it suffice-dev-1"
}

all: build test lint

docker-build:
docker compose build dev

Expand Down
95 changes: 60 additions & 35 deletions src/bin/suffice/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ use suffice::trainer::Command;

use crate::stats::Stat;

#[derive(Debug, Derivative)]
#[derivative(Default)]
struct RideState {
mode: Mode,
#[derivative(Default(value = "true"))]
mode_dirty: bool,
#[derivative(Default(value = "true"))]
level_dirty: bool,

#[derivative(Default(value = "false"))]
is_recording: bool,

resistance: i16,
power: i16,
}

#[derive(Debug, Default)]
enum Mode {
Power,
Expand All @@ -40,17 +56,12 @@ struct Stats {
#[derivative(Default)]
/// The main application data bundle
pub struct App {
resistance: i16,
power: i16,
exit: bool,
mode: Mode,
#[derivative(Default(value = "true"))]
mode_dirty: bool,
#[derivative(Default(value = "true"))]
level_dirty: bool,
to_trainer: Option<mpsc::UnboundedSender<Command>>,
from_trainer: Option<broadcast::Receiver<BikeData>>,

stats: Stats,
ride: RideState,
}

impl App {
Expand Down Expand Up @@ -105,18 +116,18 @@ impl App {
_ => {}
};
} else {
if self.mode_dirty {
if self.ride.mode_dirty {
let _ = self
.to_trainer
.as_ref()
.expect("send should always work")
.send(Command::Reset);
ev!(Level::INFO, "sent reset command");
self.mode_dirty = false;
self.ride.mode_dirty = false;
}

if self.level_dirty {
match self.mode {
if self.ride.level_dirty {
match self.ride.mode {
Mode::Power => {
let _ = self
.to_trainer
Expand All @@ -127,23 +138,23 @@ impl App {
.to_trainer
.as_ref()
.expect("")
.send(Command::Power(self.power));
.send(Command::Power(self.ride.power));
}
Mode::Resistance => {
let _ = self.to_trainer.as_ref().expect("").send(Command::Reset);
let _ = self
.to_trainer
.as_ref()
.expect("")
.send(Command::Resist((self.resistance as u8).into()));
.send(Command::Resist((self.ride.resistance as u8).into()));
}
}
ev!(
Level::INFO,
"sent level change command for {:?} mode",
self.mode
self.ride.mode
);
self.level_dirty = false;
self.ride.level_dirty = false;
}
}

Expand All @@ -163,6 +174,7 @@ impl App {
.as_ref()
.expect("")
.send(Command::ToggleRecording);
self.ride.is_recording = !self.ride.is_recording;
}
_ => {}
}
Expand All @@ -173,27 +185,27 @@ impl App {
}

fn more(&mut self) {
match self.mode {
Mode::Power => self.power = (self.power + 10).clamp(0, 1000),
Mode::Resistance => self.resistance = (self.resistance + 1).clamp(0, 100),
match self.ride.mode {
Mode::Power => self.ride.power = (self.ride.power + 10).clamp(0, 1000),
Mode::Resistance => self.ride.resistance = (self.ride.resistance + 1).clamp(0, 100),
}
self.level_dirty = true;
self.ride.level_dirty = true;
}

fn less(&mut self) {
match self.mode {
Mode::Power => self.power = (self.power - 10).clamp(0, 1000),
Mode::Resistance => self.resistance = (self.resistance - 1).clamp(0, 100),
match self.ride.mode {
Mode::Power => self.ride.power = (self.ride.power - 10).clamp(0, 1000),
Mode::Resistance => self.ride.resistance = (self.ride.resistance - 1).clamp(0, 100),
}
self.level_dirty = true;
self.ride.level_dirty = true;
}

fn change_mode(&mut self, _change: i8) {
match self.mode {
Mode::Power => self.mode = Mode::Resistance,
Mode::Resistance => self.mode = Mode::Power,
match self.ride.mode {
Mode::Power => self.ride.mode = Mode::Resistance,
Mode::Resistance => self.ride.mode = Mode::Power,
}
self.mode_dirty = true;
self.ride.mode_dirty = true;
}
}

Expand All @@ -206,7 +218,11 @@ impl Widget for &App {
" Less ".into(),
"<Down>".blue().bold(),
" Record ".into(),
"<R>".blue().bold(),
if self.ride.is_recording {
"<R>".red().slow_blink().bold()
} else {
"<R>".blue().bold()
},
" Quit ".into(),
"<Q> ".blue().bold(),
]);
Expand All @@ -222,10 +238,13 @@ impl Widget for &App {
let dist_total = self.stats.distance.total();

let counter = Text::from(vec![
Line::from(match self.mode {
Mode::Power => vec!["Power: ".into(), self.power.to_string().yellow()],
Line::from(match self.ride.mode {
Mode::Power => vec!["Power: ".into(), self.ride.power.to_string().yellow()],
Mode::Resistance => {
vec!["Resistance: ".into(), self.resistance.to_string().yellow()]
vec![
"Resistance: ".into(),
self.ride.resistance.to_string().yellow(),
]
}
}),
Line::from(vec![]),
Expand Down Expand Up @@ -266,8 +285,11 @@ mod tests {
#[test]
fn render_no_data() {
let mut app = App {
level_dirty: false,
mode_dirty: false,
ride: RideState {
level_dirty: false,
mode_dirty: false,
..Default::default()
},
..Default::default()
};
let mut buf = Buffer::empty(Rect::new(0, 0, 50, 10));
Expand Down Expand Up @@ -312,8 +334,11 @@ mod tests {
#[test]
fn render_with_data() {
let mut app = App {
level_dirty: false,
mode_dirty: false,
ride: RideState {
level_dirty: false,
mode_dirty: false,
..Default::default()
},
..Default::default()
};
let mut buf = Buffer::empty(Rect::new(0, 0, 50, 10));
Expand Down
7 changes: 6 additions & 1 deletion src/bin/suffice/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::error::Error;
use tokio::sync::{broadcast, mpsc};

use suffice::bluetooth::BluetoothDevice;
use suffice::config::Config;
use suffice::ftms::{BikeData, FitnessDevice, SampleDevice};
use suffice::trainer::{Command, Trainer};

Expand Down Expand Up @@ -62,10 +63,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
Level::INFO
};

let config = Config::default().get_dir();
println!("logs and FIT files will be written to {:?}", config.clone());
let file_appender = tracing_appender::rolling::daily(config, "suffice.log");
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
let subscriber = tracing_subscriber::fmt()
.compact()
.with_max_level(level)
.with_writer(std::io::stderr)
.with_writer(non_blocking)
.finish();
tracing::subscriber::set_global_default(subscriber)?;

Expand Down
35 changes: 35 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#[cfg(not(test))]
use chrono::Local;
use std::path::PathBuf;

#[derive(Default, Debug)]
pub struct Config {}

#[cfg(test)]
impl Config {
pub fn get_dir(&self) -> PathBuf {
PathBuf::new()
}

pub fn get_recording_name(&self) -> PathBuf {
let mut fout = self.get_dir();
fout.push("./output.fit");
fout
}
}

#[cfg(not(test))]
impl Config {
pub fn get_dir(&self) -> std::path::PathBuf {
let mut dir = std::env::home_dir().unwrap_or_default();
dir.push(".config/suffice/");
dir
}

pub fn get_recording_name(&self) -> PathBuf {
let mut fout = self.get_dir();
let now = Local::now();
fout.push(format!("recording-{}.fit", now.to_rfc3339()));
fout
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// Suffice is a TUI app for controlling a bike trainer.
pub mod bluetooth;
pub mod config;
pub mod ftms;
mod record;
pub mod trainer;
9 changes: 3 additions & 6 deletions src/record.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::config::Config;
use crate::ftms::BikeData;

#[allow(unused_imports)]
use chrono::TimeZone as _;
use chrono::{DateTime, Local};
Expand All @@ -20,12 +22,7 @@ use tracing::{Level, event, instrument};
#[instrument(skip(data))]
/// Saves a collection of BikeData to a FIT file on disk.
pub(crate) fn save_file(data: &mut [BikeData]) -> Result<(), Error> {
// NOTE: adapted from the example in the documentation
// https://crates.io/crates/rustyfit#encode-using-mesgdef-module

// FIXME: make this a random/timestamped name and/or with a name specified by user
let fout_name = "output.fit";
let fout = File::create(fout_name)?;
let fout = File::create(Config::default().get_recording_name())?;
let mut bw = BufWriter::new(fout);
let mut enc = Encoder::new(&mut bw);

Expand Down
Loading