Bump up dependecies and added non blocking barcode reader
This commit is contained in:
parent
80f9037f42
commit
235b66d582
723
Cargo.lock
generated
723
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
55
src/main.rs
55
src/main.rs
|
|
@ -4,8 +4,10 @@ use reqwest::Client;
|
|||
use serde::Deserialize;
|
||||
use serial::prelude::*;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::io::{Cursor, copy};
|
||||
use std::str::from_utf8;
|
||||
use std::sync::mpsc::{self, Receiver, Sender};
|
||||
use std::time::Duration;
|
||||
use std::{env, fs};
|
||||
use tokio;
|
||||
|
|
@ -48,7 +50,7 @@ struct ImageLinks {
|
|||
smallThumbnail: Option<String>,
|
||||
}
|
||||
|
||||
fn main() -> eframe::Result {
|
||||
fn main() -> eframe::Result<()> {
|
||||
env_logger::init();
|
||||
let options = eframe::NativeOptions {
|
||||
viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 880.0]),
|
||||
|
|
@ -71,8 +73,7 @@ fn get_ean13_data() -> String {
|
|||
interact(&mut port).to_string()
|
||||
}
|
||||
fn interact<T: SerialPort>(port: &mut T) -> String {
|
||||
port
|
||||
.reconfigure(&|settings| {
|
||||
port.reconfigure(&|settings| {
|
||||
settings.set_baud_rate(serial::Baud9600).unwrap();
|
||||
settings.set_char_size(serial::Bits8);
|
||||
settings.set_parity(serial::ParityNone);
|
||||
|
|
@ -171,13 +172,17 @@ fn get_thumbnail(url: String, isbn: String) {
|
|||
image::convert_to_webp(isbn);
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct BarcodeScanner {}
|
||||
struct BarcodeScanner {
|
||||
barcode_rx: Receiver<String>,
|
||||
last_barcode: Option<String>,
|
||||
}
|
||||
|
||||
impl eframe::App for BarcodeScanner {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) -> () {
|
||||
egui_extras::install_image_loaders(ctx);
|
||||
|
||||
if let Ok(code) = self.barcode_rx.try_recv() {
|
||||
self.last_barcode = Some(code);
|
||||
}
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.heading("Barcode Scanner");
|
||||
ui.group(|ui| {
|
||||
|
|
@ -198,5 +203,43 @@ impl eframe::App for BarcodeScanner {
|
|||
//}
|
||||
})
|
||||
});
|
||||
ctx.request_repaint_after(Duration::from_millis(100));
|
||||
}
|
||||
}
|
||||
impl Default for BarcodeScanner {
|
||||
fn default() -> Self {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
std::thread::spawn(move || {
|
||||
let mut port = serial::open("/dev/ttyACM0").unwrap();
|
||||
port.reconfigure(&|settings| {
|
||||
settings.set_baud_rate(serial::Baud9600)?;
|
||||
settings.set_char_size(serial::Bits8);
|
||||
settings.set_parity(serial::ParityNone);
|
||||
settings.set_stop_bits(serial::Stop1);
|
||||
settings.set_flow_control(serial::FlowNone);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
port.set_timeout(Duration::from_millis(1000)).unwrap();
|
||||
|
||||
loop {
|
||||
let mut buf = [0u8; 14];
|
||||
if let Ok(_) = port.read_exact(&mut buf) {
|
||||
if let Ok(code) = std::str::from_utf8(&buf) {
|
||||
let barcode = code.trim().to_string();
|
||||
if !barcode.is_empty() {
|
||||
let _ = tx.send(barcode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Self {
|
||||
barcode_rx: rx,
|
||||
last_barcode: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue