pub struct Response;Implementations§
Source§impl Response
impl Response
Sourcepub fn parse(data: &[u8; 9]) -> Result<ResponseType, Error>
pub fn parse(data: &[u8; 9]) -> Result<ResponseType, Error>
§Parse Response
Parse raw bytes from RO signal of BM1397.
The packet must have a lenght of 9 bytes.
§Return
Err(Error::InvalidPreamble)if it first 2 bytes are not[0xAA, 0x55].Err(Error::InvalidCrc)if the CRC5 is not valid.Ok(ResponseType::Reg(r))with theRegisterResponse.Err(Error::UnknownRegister(u8))with the register address if it do not match a knownRegister.Ok(ResponseType::Job(j))with theJobResponse.
§Example
use bm1397_protocol::{Error, Register, Response, ResponseType};
// Error::InvalidPreamble
let resp = Response::parse(&[0x00,0x55,0x13,0x97,0x18,0x00,0x00,0x00,0x06]);
assert!(resp.is_err());
assert_eq!(resp.unwrap_err(), Error::InvalidPreamble);
let resp = Response::parse(&[0xAA,0x00,0x13,0x97,0x18,0x00,0x00,0x00,0x06]);
assert!(resp.is_err());
assert_eq!(resp.unwrap_err(), Error::InvalidPreamble);
let resp = Response::parse(&[0x00,0x00,0x13,0x97,0x18,0x00,0x00,0x00,0x06]);
assert!(resp.is_err());
assert_eq!(resp.unwrap_err(), Error::InvalidPreamble);
// Error::InvalidCrc
let resp = Response::parse(&[0xAA,0x55,0x13,0x97,0x18,0x00,0x00,0x00,0x00]);
assert!(resp.is_err());
assert_eq!(resp.unwrap_err(), Error::InvalidCrc);
// Register::ChipAddress == 0x13971800
let resp = Response::parse(&[0xAA,0x55,0x13,0x97,0x18,0x00,0x00,0x00,0x06]);
assert!(resp.is_ok());
match resp.unwrap() {
ResponseType::Reg(r) => {
assert_eq!(r.value, 0x13971800);
assert_eq!(r.chip_addr, 0);
assert_eq!(r.register, Register::ChipAddress);
},
_ => panic!(),
};
// Error::UnknownRegister(0xF0)
let resp = Response::parse(&[0xAA,0x55,0x00,0x00,0x00,0x00,0x04,0xF0,0x03]);
assert!(resp.is_err());
assert_eq!(resp.unwrap_err(), Error::UnknownRegister(0xF0));
// Nonce == 0x97C328B6
let resp = Response::parse(&[0xAA,0x55,0x97,0xC3,0x28,0xB6,0x01,0x63,0x9C]);
assert!(resp.is_ok());
match resp.unwrap() {
ResponseType::Job(j) => {
assert_eq!(j.nonce, 0x97C328B6);
assert_eq!(j.midstate_id, 1);
assert_eq!(j.job_id, 0x63);
},
_ => panic!(),
};Auto Trait Implementations§
impl Freeze for Response
impl RefUnwindSafe for Response
impl Send for Response
impl Sync for Response
impl Unpin for Response
impl UnwindSafe for Response
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more