Commit 9d4a349e authored by Simon Wörner's avatar Simon Wörner

first commit

parents
target/
**/*.rs.bk
[root]
name = "kawaii"
version = "0.1.0"
[package]
name = "kawaii"
version = "0.1.0"
authors = ["Simon Wörner <git@simon-woerner.de>"]
[dependencies]
extern crate std;
use std::io::prelude::*;
use std::fs::File;
use std::path::Path;
#[derive(Debug)]
pub enum Direction {
Out,
In
}
impl Direction {
pub fn from_str(s: &str) -> Option<Direction> {
match s {
"out" => Some(Direction::Out),
"in" => Some(Direction::In),
_ => None,
}
}
pub fn as_str(&self) -> &'static str {
match *self {
Direction::Out => "out",
Direction::In => "in",
}
}
}
#[derive(Debug)]
pub enum Edge {
None,
Rising,
Falling,
Both
}
impl Edge {
pub fn from_str(s: &str) -> Option<Edge> {
match s {
"none" => Some(Edge::None),
"rising" => Some(Edge::Rising),
"falling" => Some(Edge::Falling),
"both" => Some(Edge::Both),
_ => None,
}
}
pub fn as_str(&self) -> &'static str {
match *self {
Edge::None => "none",
Edge::Rising => "rising",
Edge::Falling => "falling",
Edge::Both => "both",
}
}
}
#[derive(Debug)]
pub enum Value {
High,
Low
}
impl Value {
pub fn from_str(s: &str) -> Option<Value> {
match s {
"1" => Some(Value::High),
"0" => Some(Value::Low),
_ => None,
}
}
pub fn as_str(&self) -> &'static str {
match *self {
Value::High => "1",
Value::Low => "0",
}
}
}
#[derive(Debug)]
pub struct Port {
pub number: u8,
pub direction: Direction,
pub edge: Edge,
file: File,
}
impl Port {
pub fn new(number: u8, direction: Direction, edge: Edge) -> std::io::Result<Port> {
let path = format!("/sys/class/gpio/gpio{}/value", number);
let path = Path::new(path.as_str());
let file = match direction {
Direction::Out => File::create(path)?,
Direction::In => File::open(path)?
};
let port = Port {
number: number,
direction: direction,
edge: edge,
file: file
};
port.export().ok();
port.set_direction()?;
port.set_edge()?;
Ok(port)
}
pub fn drop(&mut self) {
self.unexport().ok();
}
fn write_path(path: &str, value: &str) -> std::io::Result<()> {
File::create(Path::new(path))?.write_all(value.as_bytes())
}
pub fn read(&mut self) -> std::io::Result<Value> {
let mut contents = String::new();
self.file.read_to_string(&mut contents)?;
Value::from_str(contents.as_str()).ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "Unrecognized GPIO Value"))
}
pub fn write(&mut self, value: Value) -> std::io::Result<()> {
self.file.write_all(value.as_str().as_bytes())
}
fn export(&self) -> std::io::Result<()> {
Port::write_path("/sys/class/gpio/export", self.number.to_string().as_str())
}
fn unexport(&self) -> std::io::Result<()> {
Port::write_path("/sys/class/gpio/unexport", self.number.to_string().as_str())
}
fn set_direction(&self) -> std::io::Result<()> {
Port::write_path(format!("/sys/class/gpio/gpio{}/direction", self.number).as_str(), self.direction.as_str())
}
fn set_edge(&self) -> std::io::Result<()> {
Port::write_path(format!("/sys/class/gpio/gpio{}/edge", self.number).as_str(), self.edge.as_str())
}
}
\ No newline at end of file
pub mod gpio;
\ No newline at end of file
extern crate kawaii;
use kawaii::gpio::{Port, Direction, Edge, Value};
fn main() {
println!("Hello, world!");
let mut trigger = Port::new(27, Direction::Out, Edge::None).expect("Create Trigger GPIO failed");
let mut echo = Port::new(28, Direction::In, Edge::Both).expect("Create Echo GPIO failed");
println!("trigger = {:?}", trigger.read());
trigger.write(Value::High);
println!("trigger = {:?}", trigger.read());
trigger.write(Value::Low);
println!("trigger = {:?}", trigger.read());
}
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