Response

Struct Response 

Source
pub struct Response;

Implementations§

Source§

impl Response

Source

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 the RegisterResponse.
  • Err(Error::UnknownRegister(u8)) with the register address if it do not match a known Register.
  • Ok(ResponseType::Job(j)) with the JobResponse.
§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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.