Commit 5b03a821 authored by Simon Wörner's avatar Simon Wörner

added documentation

parent f65e1fa6
......@@ -3,6 +3,10 @@ name = "kawaii"
version = "0.1.0"
authors = ["Simon Wörner <git@simon-woerner.de>"]
[[bin]]
name = "kawaii-test"
doc = false
[features]
measure = []
......
......@@ -133,6 +133,11 @@ impl fmt::Debug for AsyncPort {
}
impl Port {
/// Constructs a new GPIO `Port`.
///
/// # Parameter
/// - `number` GPIO Port number of pin.
/// - `direction` GPIO Port direction.
pub fn new(number: u8, direction: Direction) -> std::io::Result<Port> {
let port = Port {
number: number,
......@@ -143,6 +148,7 @@ impl Port {
Ok(port)
}
/// Init GPIO `Port`: export port and set direction
fn init(&self) -> std::io::Result<()> {
self.export().ok();
self.set_direction()?;
......@@ -150,6 +156,7 @@ impl Port {
Ok(())
}
/// Drop GPIO `Port`: unexport
pub fn drop(&mut self) {
self.unexport().ok();
}
......@@ -159,13 +166,17 @@ impl Port {
.write_all(value.as_bytes())
}
/// Export GPIO `Port`
fn export(&self) -> std::io::Result<()> {
Port::write_path("/sys/class/gpio/export", self.number.to_string().as_str())
}
/// Unexport GPIO `Port`
fn unexport(&self) -> std::io::Result<()> {
Port::write_path("/sys/class/gpio/unexport", self.number.to_string().as_str())
}
/// Set GPIO `Port` direction
fn set_direction(&self) -> std::io::Result<()> {
Port::write_path(format!("/sys/class/gpio/gpio{}/direction", self.number).as_str(),
self.direction.as_str())
......@@ -173,6 +184,11 @@ impl Port {
}
impl SyncPort {
/// Constructs a new synchronised GPIO `Port`.
///
/// # Parameter
/// - `number` GPIO Port number of pin.
/// - `direction` GPIO Port direction.
pub fn new(number: u8, direction: Direction) -> std::io::Result<SyncPort> {
Ok(SyncPort {
port: Port::new(number, direction)?,
......@@ -181,6 +197,7 @@ impl SyncPort {
})
}
/// Open GPIO `SyncPort` sysfs file
fn open(number: u8, direction: Direction) -> std::io::Result<File> {
let path = format!("/sys/class/gpio/gpio{}/value", number);
let path = Path::new(path.as_str());
......@@ -191,6 +208,7 @@ impl SyncPort {
})
}
/// Read from GPIO `SyncPort` sysfs file
pub fn read(&mut self) -> std::io::Result<Value> {
self.file.seek(SeekFrom::Start(0))?;
self.file.read_exact(&mut self.buffer)?;
......@@ -199,12 +217,18 @@ impl SyncPort {
"Unrecognized GPIO Value"))
}
/// Write to GPIO `SyncPort` sysfs file
pub fn write(&mut self, value: Value) -> std::io::Result<()> {
self.file.write_all(value.as_str().as_bytes())
}
}
impl AsyncPort {
/// Constructs a new asynchronous GPIO `Port`.
///
/// # Parameter
/// - `number` GPIO Port number of pin.
/// - `edge` GPIO Port edge detection setting.
pub fn new(number: u8, edge: Edge) -> std::io::Result<AsyncPort> {
let port = Port::new(number, Direction::In)?;
let file = AsyncPort::open(number)?;
......@@ -219,12 +243,15 @@ impl AsyncPort {
port.init()?;
Ok(port)
}
/// Init GPIO `Port`: set edge detection
fn init(&self) -> std::io::Result<()> {
self.set_edge()?;
Ok(())
}
/// Open GPIO `AsyncPort` sysfs file with posix API
fn open(number: u8) -> std::io::Result<RawFd> {
nix::fcntl::open(format!("/sys/class/gpio/gpio{}/value", number).as_str(),
nix::fcntl::O_RDONLY,
......@@ -239,10 +266,12 @@ impl AsyncPort {
}
}
/// Posix poll on sysfs file
fn nix_poll(&mut self, timeout: i32) -> nix::Result<i32> {
nix::poll::poll(&mut self.fds, timeout)
}
/// Read from GPIO `AsyncPort` sysfs file with posix poll
fn poll_read(&mut self, poll: nix::Result<i32>) -> std::io::Result<Option<Value>> {
let poll = poll.or(Err(Error::new(ErrorKind::Other, "poll failed")))?;
......@@ -261,12 +290,14 @@ impl AsyncPort {
|v| Ok(Some(v)))
}
/// Read asynchronous from GPIO `AsyncPort` sysfs file
pub fn poll(&mut self, timeout: Option<Duration>) -> std::io::Result<Option<Value>> {
let poll = self.nix_poll(Self::get_timeout(timeout));
self.poll_read(poll)
}
/// Read asynchronous from GPIO `AsyncPort` sysfs file with measure support (ignore poll time)
#[cfg(feature = "measure")]
pub fn poll_measure(&mut self,
timeout: Option<Duration>,
......@@ -281,6 +312,7 @@ impl AsyncPort {
self.poll_read(poll)
}
/// Set GPIO `Port` edge detection
fn set_edge(&self) -> std::io::Result<()> {
Port::write_path(format!("/sys/class/gpio/gpio{}/edge", self.port.number).as_str(),
self.edge.as_str())
......
......@@ -22,6 +22,10 @@ pub struct Measure {
}
impl Measure {
/// Constructs a new `Measure`.
///
/// # Parameter
/// - `name` Name of measured component.
pub fn new(name: String) -> Self {
Measure {
min: u64::max_value(),
......@@ -33,10 +37,12 @@ impl Measure {
}
}
/// Start measurement
pub fn start(&mut self) {
self.last = precise_time_ns();
}
/// Pause measurement
pub fn pause(&mut self) {
if self.last == 0 {
#[cfg(debug_assertions)]
......@@ -48,6 +54,7 @@ impl Measure {
self.time += self.time_diff();
}
/// Stop measurement and calculate time difference
pub fn stop(&mut self) {
if self.last == 0 {
#[cfg(debug_assertions)]
......@@ -98,6 +105,7 @@ impl Measure {
}
impl Drop for Measure {
/// Print measure results and write times to file
fn drop(&mut self) {
println!("{}:\n\tmin: {} ns\n\tmax: {} ns",
self.name,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment