1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#![feature(associated_consts)]
#![feature(core_intrinsics)]
#![feature(duration)]
#![feature(thread_sleep)]
#![feature(convert)]
#![feature(core)]

extern crate libc;
extern crate mmap;
//extern crate mio;
extern crate spidev;
extern crate core;
extern crate nix;
#[macro_use] extern crate bitflags;

mod result;
mod map;
mod logic;
mod board;
mod cupi;

pub use cupi::{
    CuPi
};

pub use logic::{
    Logic,
    Logic3,
    DigitalLogic,
    DigitalWrite,
    DigitalRead,
    AnalogWrite,
    AnalogRead,
};

pub use result::{
    Result,
    Error
};

pub use board::{
    Board,
    Hardware,
    CPU,
    RaspberryModel,
    RaspberryRevision,
    RaspberryMaker,
    board
};

pub mod bcm270x;
pub mod sys;

pub use bcm270x::{
    PinOptions,
    PinInput,
    PinOutput
};

use std::thread;
use std::time::Duration;
use nix::sys::ioctl::libc::geteuid;

pub trait RegisterDesc {
    fn offset(&self) -> usize;
}

pub trait RegisterOperations<T> {
    fn write(&self, data: T);
    fn read(&self) -> T;
    fn bitand(&self, data: T);
    fn bitor(&self, data: T);
    fn bitxor(&self, data: T);
}

pub struct Register<R: RegisterDesc> {
    pub ptr: *mut u32,
    pub desc: R
}

impl<R: RegisterDesc> RegisterOperations<u32> for Register<R> {
    #[inline(always)]
    fn write(&self, data: u32) {
        unsafe { *self.ptr = data; }
    }

    #[inline(always)]
    fn read(&self) -> u32 {
        unsafe { *self.ptr }
    }

    #[inline(always)]
    fn bitand(&self, data: u32) {
        unsafe { *self.ptr &= data; }
    }

    #[inline(always)]
    fn bitor(&self, data: u32) {
        unsafe { *self.ptr |= data; }
    }

    #[inline(always)]
    fn bitxor(&self, data: u32) {
        unsafe { *self.ptr ^= data; }
    }
}

#[inline(always)]
pub fn delay(dur: Duration) {
    // FIXME: todo sleep hard if < 100
    thread::sleep(dur);
}

#[inline(always)]
pub fn delay_ms(ms: u64) {
    delay(Duration::from_millis(ms));
}

pub fn is_root() -> bool {
    unsafe { geteuid() == 0 }
}