-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathread_uid.rs
66 lines (51 loc) · 1.64 KB
/
read_uid.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/// This example needs features: spidev self_test
extern crate spidev;
extern crate mfrc522;
extern crate env_logger;
mod bus_spidev;
use bus_spidev::spi_open;
use std::io;
use std::thread;
use std::time::Duration;
use mfrc522::MFRC522;
use mfrc522::pcd::Reg;
use mfrc522::picc;
fn example() -> io::Result<()> {
let mut bus = try!(spi_open("/dev/spidev0.0"));
let mut mfrc522 = MFRC522::init(&mut bus).expect("MFRC522 Initialization failed");
let version = mfrc522.register_read(Reg::Version).expect("Could not read version");
println!("Version: {:X}.", version);
println!("Starting self-test.");
let test_status = mfrc522.self_test();
if test_status.is_ok() {
println!("Self-test: PASSED.");
} else {
println!("Self-test: FAILED: {:?}", test_status);
}
// Re-init after self-test.
mfrc522.pcd_soft_reset().expect("Failed to reset MFRC522");
mfrc522.pcd_init().expect("Failed to re-init MFRC522");
let mut uid = picc::UID::default();
loop {
let new_card = mfrc522.picc_is_new_card_present();
if let Some(atqa) = new_card {
println!("New card detected. ATQA: {:04x}", atqa.bits());
let status = mfrc522.picc_select(&mut uid);
println!("Select: {:?} {:?}", status, uid);
if status.is_ok() {
println!("Card UID: {:?} | Type: {:?}", uid.as_bytes(), uid.picc_type());
let halt_status = mfrc522.picc_hlta();
println!("Halt: {:?}", halt_status);
}
uid.clear();
}
thread::sleep(Duration::from_millis(500));
}
}
pub fn main() {
env_logger::init().unwrap_or_else(|e| println!("Could not init env_logger: {:?}.", e));
match example() {
Ok(_) => println!("Done."),
Err(e) => println!("Error: {:?}", e),
}
}