Skip to content

Commit

Permalink
add: 完善一些内容
Browse files Browse the repository at this point in the history
  • Loading branch information
HalfSweet committed Dec 17, 2023
1 parent 9b6412d commit 526ac1f
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 27 deletions.
82 changes: 82 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ rust-i18n = "2.3.0"
colored = "2.1.0"
hex = "0.4.3"
tokio = { version = "1.35.0", features = ["full"] }
crossterm = "0.27.0"
time = "0.3.30"
32 changes: 28 additions & 4 deletions src/AirISP.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::peripheral::Pp;
use std::path::Path;
use std::string::String;

#[derive(PartialEq)]
pub enum Progress {
None,
Bar,
Expand Down Expand Up @@ -84,6 +85,13 @@ pub fn air_isp() -> Command
.value_parser(["Uart", "swd"])
.default_value("Uart");

let language = Arg::new("language")
.global(true)
.long("language")
.help(t!("language_help"))
.value_parser(["auto", "en", "zh-CN", "ja"])
.default_value("auto");

let styles = styling::Styles::styled()
.header(styling::AnsiColor::Green.on_default() | styling::Effects::BOLD)
.usage(styling::AnsiColor::Green.on_default() | styling::Effects::BOLD)
Expand All @@ -106,6 +114,7 @@ pub fn air_isp() -> Command
.arg(before)
.arg(after)
.arg(peripheral)
.arg(language)
.subcommand(write_flash::command())
.subcommand(get::chip_id_command())
}
Expand All @@ -118,6 +127,7 @@ pub struct AirISP {
connect_attempts: u32,
before: String,
after: String,
language: String,

peripheral: Peripheral,
}
Expand All @@ -137,6 +147,7 @@ impl AirISP
connect_attempts: *matches.get_one::<u32>("connect_attempts").unwrap(),
before: matches.get_one::<String>("before").unwrap().to_string(),
after: matches.get_one::<String>("after").unwrap().to_string(),
language: matches.get_one::<String>("language").unwrap().to_string(),
peripheral: pp,
}
}
Expand Down Expand Up @@ -174,7 +185,12 @@ impl AirISP
self.after.clone()
}

pub fn read_file(&self, file_path: &str, bin: &mut Vec<u8>) -> Result<(), Box<dyn Error>>
pub fn get_language(&self) -> String
{
self.language.clone()
}

pub fn read_file(&self, file_path: &str, address: &mut u32, bin: &mut Vec<u8>) -> Result<(), Box<dyn Error>>
{
// 判断文件后缀是.hex还是.bin
let mut file = std::fs::File::open(file_path).unwrap();
Expand All @@ -190,7 +206,7 @@ impl AirISP
"hex" => {
let mut hex = String::from("");
file.read_to_string(&mut hex).unwrap();
self.hex_to_bin(hex, &mut b).unwrap();
self.hex_to_bin(hex, address, &mut b).unwrap();
}
"bin" | _ => {
file.read_to_end(&mut b).unwrap();
Expand All @@ -200,16 +216,24 @@ impl AirISP
Ok(())
}

/// 如果是
pub fn read_real_address(&mut self, address: &mut u32) -> Result<(), Box<dyn Error>>
{

Ok(())
}


/// 将 Intel Hex 格式的字符串转换为二进制数据
///
/// # 参数
/// * `hex` - Intel Hex 格式的字符串
/// * `address` - 起始地址
/// * `bin` - 要填充的二进制数据向量
///
/// # 返回值
/// 如果成功,返回 Ok(());如果失败,返回错误信息
pub fn hex_to_bin(&self, hex: String, bin: &mut Vec<u8>) -> Result<(), Box<dyn Error>> {
pub fn hex_to_bin(&self, hex: String,address: &mut u32, bin: &mut Vec<u8>) -> Result<(), Box<dyn Error>> {
let mut start_address = String::from("-1");
let mut current_address = 0;

Expand Down Expand Up @@ -243,14 +267,14 @@ impl AirISP
// 获取高16位地址并更新起始地址
let high_address = &hex_line[8..12];
start_address = format!("0x{}0000", high_address);
*address = u32::from_str_radix(&start_address[2..], 16)?;
current_address = 0;
},
_ => { // 其他记录类型
// 忽略不处理
}
}
}

Ok(())
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ use clap::error::Error;
use clap::{Arg, ArgAction, ArgMatches, Args, ColorChoice, Command, command, FromArgMatches, Parser, value_parser};
use clap::builder::{styling, ValueParser};

fn set_language() {
fn set_language(air_isp: &AirISP::AirISP) {
let language = whoami::lang().collect::<Vec<String>>();
let language = language[0].as_str().to_owned();
let i18n_list = rust_i18n::available_locales!();

if air_isp.get_language() != "auto" {
rust_i18n::set_locale(&air_isp.get_language());
return;
}
// 不支持的语言默认使用英语
if !i18n_list.contains(&language.as_str()) {
println!("{}","Language not supported");
Expand All @@ -28,11 +32,11 @@ fn set_language() {
}

fn main() {
set_language();

let matches = AirISP::air_isp().get_matches();

let air_isp = AirISP::AirISP::new(&matches);

set_language(&air_isp);
// 打印版本号
println!("{}", format!("AirISP version: {}", env!("CARGO_PKG_VERSION")).blue());

Expand Down
Loading

0 comments on commit 526ac1f

Please sign in to comment.