mirror of
https://codeberg.org/ral/rwedid.git
synced 2024-08-16 09:59:49 +02:00
Some more documentation
This commit is contained in:
parent
0de6c4d001
commit
72410d3d34
5 changed files with 11 additions and 4 deletions
|
@ -4,6 +4,7 @@ fn hex_address(a: &str) -> Result<u16, std::num::ParseIntError> {
|
||||||
u16::from_str_radix(a.trim_start_matches("0x"), 16)
|
u16::from_str_radix(a.trim_start_matches("0x"), 16)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Command line argument definitions.
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(version, about, long_about = None, next_line_help = true, arg_required_else_help(true))]
|
#[command(version, about, long_about = None, next_line_help = true, arg_required_else_help(true))]
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
|
@ -15,7 +16,7 @@ pub struct Args {
|
||||||
#[arg(short, long, default_value = "0x50", value_parser = hex_address)]
|
#[arg(short, long, default_value = "0x50", value_parser = hex_address)]
|
||||||
pub address: u16,
|
pub address: u16,
|
||||||
|
|
||||||
/// Number of 128-byte blocks to read, defaults to autodetect from data
|
/// Number of 128-byte blocks to read, defaults to autodetect from data.
|
||||||
#[arg(short, long, default_value = None)]
|
#[arg(short, long, default_value = None)]
|
||||||
pub blocks: Option<usize>,
|
pub blocks: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
// Compute the checksum over the bytes of a block.
|
/// Compute the checksum over the bytes of a block.
|
||||||
#[allow(clippy::cast_possible_truncation)]
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
pub fn checksum(bytes: &[u8]) -> u8 {
|
pub fn checksum(bytes: &[u8]) -> u8 {
|
||||||
bytes.iter().fold(0u32, |x, y| x + u32::from(*y)) as u8
|
bytes.iter().fold(0u32, |x, y| x + u32::from(*y)) as u8
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate the checksum
|
/// Check if the checksum is valid.
|
||||||
pub fn valid_checksum(bytes: &[u8]) -> bool {
|
pub fn valid_checksum(bytes: &[u8]) -> bool {
|
||||||
checksum(bytes) == 0x00
|
checksum(bytes) == 0x00
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the checksum to stderr
|
/// Print the checksum to stderr.
|
||||||
pub fn print_checksum(bytes: &[u8]) {
|
pub fn print_checksum(bytes: &[u8]) {
|
||||||
let cs = checksum(bytes);
|
let cs = checksum(bytes);
|
||||||
eprintln!(" Checksum is: {cs}");
|
eprintln!(" Checksum is: {cs}");
|
||||||
|
|
|
@ -3,6 +3,7 @@ use i2cdev::linux::{LinuxI2CDevice, LinuxI2CMessage};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
/// Read a fixed-size block of binary data from an i2c device.
|
||||||
pub fn read_from_bus(
|
pub fn read_from_bus(
|
||||||
i2c_device: &str,
|
i2c_device: &str,
|
||||||
slave_address: u16,
|
slave_address: u16,
|
||||||
|
@ -25,6 +26,7 @@ pub fn read_from_bus(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Write a fixed-size block of binary data to an i2c device.
|
||||||
pub fn write_to_bus(
|
pub fn write_to_bus(
|
||||||
i2c_device: &str,
|
i2c_device: &str,
|
||||||
slave_address: u16,
|
slave_address: u16,
|
||||||
|
|
|
@ -3,6 +3,8 @@ use crate::checksum::print_checksum;
|
||||||
/// Size of an EDID block in bytes.
|
/// Size of an EDID block in bytes.
|
||||||
pub const BLOCK_SIZE: usize = 128;
|
pub const BLOCK_SIZE: usize = 128;
|
||||||
|
|
||||||
|
/// Read an EDID binary blob block by block. Check the first block for the
|
||||||
|
/// number of extension blocks that should follow and try to read them all.
|
||||||
pub fn block_by_block_read(
|
pub fn block_by_block_read(
|
||||||
blocks: Option<usize>,
|
blocks: Option<usize>,
|
||||||
read: impl Fn(u8, usize) -> Result<Vec<u8>, std::io::Error>,
|
read: impl Fn(u8, usize) -> Result<Vec<u8>, std::io::Error>,
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use std::io::{self, Read, Write};
|
use std::io::{self, Read, Write};
|
||||||
|
|
||||||
|
/// Read a fixed-size block of binary data from stdin.
|
||||||
pub fn read_from_stdin(data_length: usize) -> Result<Vec<u8>, std::io::Error> {
|
pub fn read_from_stdin(data_length: usize) -> Result<Vec<u8>, std::io::Error> {
|
||||||
// Buffer to read into
|
// Buffer to read into
|
||||||
let mut data = vec![0; data_length];
|
let mut data = vec![0; data_length];
|
||||||
|
@ -8,6 +9,7 @@ pub fn read_from_stdin(data_length: usize) -> Result<Vec<u8>, std::io::Error> {
|
||||||
Ok(data)
|
Ok(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Write a fixed-size block of binary data to stdout.
|
||||||
pub fn write_to_stdout(data: &[u8]) -> Result<(), std::io::Error> {
|
pub fn write_to_stdout(data: &[u8]) -> Result<(), std::io::Error> {
|
||||||
// Write data to stdout
|
// Write data to stdout
|
||||||
let mut stdout = io::stdout().lock();
|
let mut stdout = io::stdout().lock();
|
||||||
|
|
Loading…
Reference in a new issue