refactor: simplify write_device_config1/2 and fix error message typo
This commit is contained in:
@@ -152,6 +152,78 @@ impl EskinDeviceInner {
|
|||||||
pub fn shared_transport(&self) -> SharedSerialTransport {
|
pub fn shared_transport(&self) -> SharedSerialTransport {
|
||||||
Arc::clone(&self.transport)
|
Arc::clone(&self.transport)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait EskinDeviceFunc {
|
||||||
|
fn read_hdw_version(&mut self) -> Result<String, SdkError>;
|
||||||
|
fn read_matrix_row(&mut self) -> Result<u8, SdkError>;
|
||||||
|
fn read_matrix_col(&mut self) -> Result<u8, SdkError>;
|
||||||
|
fn read_device_config1(&mut self) -> Result<u8, SdkError>;
|
||||||
|
fn read_device_config2(&mut self) -> Result<u8, SdkError>;
|
||||||
|
fn write_device_config1(&mut self, enable: bool) -> Result<u16, SdkError>;
|
||||||
|
fn write_device_config2(&mut self, enable: bool) -> Result<u16, SdkError>;
|
||||||
|
fn write_matrix_row(&mut self, row: u8) -> Result<u16, SdkError>;
|
||||||
|
fn write_matrix_col(&mut self, col: u8) -> Result<u16, SdkError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EskinDeviceFunc for EskinDeviceInner {
|
||||||
|
fn read_hdw_version(&mut self) -> Result<String, SdkError> {
|
||||||
|
let hdw = self.read_register(0, 2)
|
||||||
|
.map_err(|_| SdkError::FrameError("read hardware version failed".into()))?;
|
||||||
|
|
||||||
|
let version = format!("{}.{}", hdw[0], hdw[1]);
|
||||||
|
Ok(version)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_matrix_row(&mut self) -> Result<u8, SdkError> {
|
||||||
|
let row = self.read_register(0x0015, 1)
|
||||||
|
.map_err(|_| SdkError::FrameError("read matrix row failed".into()))?;
|
||||||
|
|
||||||
|
Ok(row[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_matrix_col(&mut self) -> Result<u8, SdkError> {
|
||||||
|
let col = self.read_register(0x0014, 1)
|
||||||
|
.map_err(|_| SdkError::FrameError("read matrix col failed".into()))?;
|
||||||
|
|
||||||
|
Ok(col[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_matrix_row(&mut self, row: u8) -> Result<u16, SdkError> {
|
||||||
|
let res = self.write_register(0x0015, &[row])
|
||||||
|
.map_err(|_| SdkError::FrameError("write matrix row failed".into()))?;
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_matrix_col(&mut self, col: u8) -> Result<u16, SdkError> {
|
||||||
|
let res = self.write_register(0x0015, &[col])
|
||||||
|
.map_err(|_| SdkError::FrameError("write matrix row failed".into()))?;
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_device_config1(&mut self) -> Result<u8, SdkError> {
|
||||||
|
let enabled = self.read_register(0x0017, 1)
|
||||||
|
.map_err(|_| SdkError::FrameError("read device config1 failed".into()))?;
|
||||||
|
Ok(enabled[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_device_config2(&mut self) -> Result<u8, SdkError> {
|
||||||
|
let enabled = self.read_register(0x0018, 1)
|
||||||
|
.map_err(|_| SdkError::FrameError("read device config1 failed".into()))?;
|
||||||
|
Ok(enabled[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_device_config1(&mut self, enable: bool) -> Result<u16, SdkError> {
|
||||||
|
self.write_register(0x0017, &[u8::from(enable)])
|
||||||
|
.map_err(|_| SdkError::FrameError("write device config1 failed".into()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_device_config2(&mut self, enable: bool) -> Result<u16, SdkError> {
|
||||||
|
self.write_register(0x0018, &[u8::from(enable)])
|
||||||
|
.map_err(|_| SdkError::FrameError("write device config2 failed".into()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait EskinDevice {
|
pub trait EskinDevice {
|
||||||
|
|||||||
Reference in New Issue
Block a user