Skip to content

Commit

Permalink
Export Board With Time To Solution (tts)
Browse files Browse the repository at this point in the history
  • Loading branch information
the-rectifier committed Dec 18, 2021
1 parent d19e361 commit 9563003
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
15 changes: 7 additions & 8 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,4 +18,3 @@ structopt = "0.3.23"
clap = "2.33.3"
strum = "0.22"
strum_macros = "0.22"
chrono = "0.4"
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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 |
|-|-|-|-|-|
Expand Down Expand Up @@ -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

Expand Down
13 changes: 10 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,6 +44,8 @@ pub struct SudokuAvr {
filled: u8,

dif: Difficulty,

pub tts: u64,
}

#[derive(Default, Debug)]
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(_) => (),
Expand All @@ -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()
Expand Down Expand Up @@ -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);

Expand Down
17 changes: 10 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!");
Expand All @@ -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(())
}
Expand Down Expand Up @@ -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)?;
Expand All @@ -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)?; }
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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()),
}
},
Expand Down

0 comments on commit 9563003

Please sign in to comment.