From 9563003ff4a64d1403474b631a6e87ac137bf22a Mon Sep 17 00:00:00 2001 From: Odysseas Stavrou Date: Sat, 18 Dec 2021 23:46:21 +0200 Subject: [PATCH] Export Board With Time To Solution (tts) --- Cargo.lock | 15 +++++++-------- Cargo.toml | 3 +-- README.md | 7 ++++--- src/lib.rs | 13 ++++++++++--- src/main.rs | 17 ++++++++++------- 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6a6c034..152df4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -25,10 +25,9 @@ dependencies = [ [[package]] name = "ace411_sudoku" -version = "1.5.0" +version = "1.6.0" dependencies = [ "anyhow", - "chrono", "clap", "colored", "log", @@ -204,9 +203,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.109" +version = "0.2.112" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f98a04dce437184842841303488f70d0188c5f51437d2a834dc097eafa909a01" +checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" [[package]] name = "libudev" @@ -304,9 +303,9 @@ dependencies = [ [[package]] name = "pkg-config" -version = "0.3.23" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1a3ea4f0dd7f1f3e512cf97bf100819aa547f36a6eccac8dbaae839eb92363e" +checksum = "58893f751c9b0412871a09abd62ecd2a00298c6c83befa223ef98c52aef40cbe" [[package]] name = "ppv-lite86" @@ -340,9 +339,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.33" +version = "1.0.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb37d2df5df740e582f28f8560cf425f52bb267d872fe58358eadb554909f07a" +checksum = "2f84e92c0f7c9d58328b85a78557813e4bd845130db68d7184635344399423b1" dependencies = [ "unicode-xid", ] diff --git a/Cargo.toml b/Cargo.toml index be45b77..059622c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ace411_sudoku" -version = "1.5.0" +version = "1.6.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -18,4 +18,3 @@ structopt = "0.3.23" clap = "2.33.3" strum = "0.22" strum_macros = "0.22" -chrono = "0.4" diff --git a/README.md b/README.md index 504ed3a..b10324b 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,10 @@ The Interface has the following functionality: - Download a board to the STK-500 - Receive Board and check solution - Measure Time until receiving "Solve" signal -- Can Drop you into Interactive Shell suporting a specific Haze Command Set +- Can Drop you into Interactive Shell suporting a specific Hayes Command Set - Cross Platform (Windows / Linux) using the [serialport-rs](https://crates.io/crates/serialport) Crate - Variable UART Configurations (Data bits, Stop bits, etc) Provided by the Crate +- Export Current Board with time to solve The interface has 4 modes: @@ -31,7 +32,7 @@ The interface has 4 modes: - run (Same as above but generates a board at Runtime, drops into interactive shell afterwords) - list (List available UART Ports) -The Haze Command Set +The Hayes Command Set | Command | From | To | Response | Description | |-|-|-|-|-| @@ -183,7 +184,7 @@ The serialport-rs Crate requires `pkg-config` and `libudev` headers to be instal - [X] Await Responses Each time a write is issued -- [X] Interactive Shell Conforming to the Haze Command Set +- [X] Interactive Shell Conforming to the Hayes Command Set - [X] Replies when Necessary diff --git a/src/lib.rs b/src/lib.rs index d00ea55..811c73a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,6 @@ use colored::*; use std::thread; use sudoku::Sudoku; use std::time::Duration; -use chrono::Local; use std::fs::{ OpenOptions, create_dir }; use std::path::{ PathBuf }; use strum::IntoEnumIterator; @@ -45,6 +44,8 @@ pub struct SudokuAvr { filled: u8, dif: Difficulty, + + pub tts: u64, } #[derive(Default, Debug)] @@ -72,6 +73,7 @@ impl SudokuAvr { solution: SudokuAvr::parse_board(&solution), dif: diff.clone(), filled: 0, + tts: 0, }; board.filled = SudokuAvr::count_filled(&board.board); @@ -105,6 +107,7 @@ impl SudokuAvr { solution: SudokuAvr::parse_board(&solution), dif: diff.clone(), filled: 0, + tts: 0, }; board.filled = SudokuAvr::count_filled(&board.board); @@ -242,6 +245,10 @@ impl SudokuAvr { } pub fn export_board(&self) -> Result<()> { + if self.tts == 0 { + error!("No Solution Time Found"); + return Ok(()); + } let dir = "exports"; match create_dir(dir) { Ok(_) => (), @@ -256,7 +263,7 @@ impl SudokuAvr { } } - let filename = format!("{}_{}.txt", self.dif, Local::now().format("%d%m%Y_%H%M%S")); + let filename = format!("{}_{}s.txt", self.dif, self.tts); let path = PathBuf::from(format!("./{}", dir)).join(filename.clone()); let mut f = OpenOptions::new() @@ -329,7 +336,7 @@ pub fn generate_boards(dir: String, num: u32) -> Result<()> { for diff in Difficulty::iter() { for i in 1..=num { // let filename = format!("{}_{}.txt", diff, i); - let filename = format!("{}_{}_{}.txt", diff, i, Local::now().format("%d%m%Y_%H%M%S")); + let filename = format!("{}_{}.txt", diff, i); let path = PathBuf::from(format!("./{}/", dir)).join(filename); let sudoku = SudokuAvr::new(&diff); diff --git a/src/main.rs b/src/main.rs index 12c1444..74eaeb9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,7 +42,7 @@ enum MyParity { #[structopt( name = "ACE411 - Sudoku <=> AVR Interface", author = "Stavrou Odysseas (canopus)", - version = "1.5", + version = "1.6", )] struct Opts { /// Command to run @@ -161,7 +161,7 @@ fn get_ports() { } fn run(dif: lib::Difficulty, port: &mut Port) -> Result<()> { - let sudoku = lib::SudokuAvr::new(&dif); + let mut sudoku = lib::SudokuAvr::new(&dif); println!(); info!("Generated Board!"); @@ -171,7 +171,7 @@ fn run(dif: lib::Difficulty, port: &mut Port) -> Result<()> { sudoku.print_solved(); info!("Going Interactive!"); - go_interactive(port, &sudoku, false)?; + go_interactive(port, &mut sudoku, false)?; Ok(()) } @@ -250,7 +250,7 @@ fn main() -> Result<()> { } line.clear(); reader.read_line(&mut line)?; - let sudoku = lib::SudokuAvr::new_from_str(&line, diff); + let mut sudoku = lib::SudokuAvr::new_from_str(&line, diff); sudoku.print_solved(); lib::write_uart(&mut port, CLEAR)?; @@ -260,7 +260,7 @@ fn main() -> Result<()> { port.clear(ClearBuffer::All).with_context(|| format!("Unable to Clear Buffers"))?; if args.inter { info!("Going Interactive!"); - go_interactive(&mut port, &sudoku, true)?; + go_interactive(&mut port, &mut sudoku, true)?; } } Command::Gen(gen) => { lib::generate_boards(gen.directory, gen.number)?; } @@ -319,7 +319,7 @@ fn ct_msg(msg: &str) -> Result<()> { } -fn go_interactive(port: &mut Port, sudoku: &lib::SudokuAvr, flag: bool) -> Result<()> { +fn go_interactive(port: &mut Port, sudoku: &mut lib::SudokuAvr, flag: bool) -> Result<()> { let mut flag_send = flag; let mut user_input = String::new(); @@ -371,7 +371,10 @@ fn go_interactive(port: &mut Port, sudoku: &lib::SudokuAvr, flag: bool) -> Resul info!("Ready to Receive the Solved Board from the AVR?"); ct_msg("Receiving in ")?; match recv_and_check(port, &sudoku) { - Ok(_) => info!("{}", format!("Valid Solution!!").green().bold()), + Ok(_) => { + info!("{}", format!("Valid Solution!!").green().bold()); + sudoku.tts = time_elapsed.as_secs(); + }, Err(_) => info!("{}", format!("Invalid Solution! :( ").red().bold()), } },