Commit 6dc28a8a authored by Simon Wörner's avatar Simon Wörner
Browse files

created kawaii-rs c api

parents
Cargo.lock
target/
**/*.rs.bk
*.iml
\ No newline at end of file
cmake_minimum_required(VERSION 3.0)
project(kawaii-rs)
include(ExternalProject)
if (MEASURE)
set(CARGO_FEATURES "${CARGO_FEATURES} measure")
endif (MEASURE)
if (CARGO_FEATURES)
set(CARGO_ARGUMENTS --features "${CARGO_FEATURES}")
endif(CARGO_FEATURES)
file(GLOB RUST_SOURCE_FILES "${PROJECT_SOURCE_DIR}/src" *.rs)
add_custom_target(${PROJECT_NAME}
COMMAND cargo build --color=never ${CARGO_ARGUMENTS}
COMMAND cargo build --color=never ${CARGO_ARGUMENTS} --release
DEPENDS ${RUST_SOURCE_FILES}
BYPRODUCTS target/debug/libkawaii.a target/release/libkawaii.a
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
SOURCES ${RUST_SOURCE_FILES})
\ No newline at end of file
[package]
name = "kawaii-rs-api"
version = "0.1.0"
authors = ["Simon Wörner <git@simon-woerner.de>"]
[lib]
name = "kawaii"
crate-type = ["staticlib"]
[features]
measure = [
"kawaii/measure",
"emergency_stop/measure",
"ultrasonic_irq/measure" ]
[dependencies]
emergency_stop = { git = "https://git.brn.li/kawaii-robotto/emergency-stop.git" }
kawaii = { git = "https://git.brn.li/kawaii-robotto/kawaii-rs.git" }
ultrasonic_irq = { git = "https://git.brn.li/kawaii-robotto/ultrasonic-irq.git" }
\ No newline at end of file
extern crate emergency_stop;
use self::emergency_stop::EmergencyStop;
use std::mem::transmute;
use std::ptr::null_mut;
use std::sync::atomic::Ordering;
#[no_mangle]
pub extern "C" fn emergency_stop_init(stop: u8) -> *mut EmergencyStop {
match EmergencyStop::new(stop) {
Err(_) => null_mut(),
Ok(emergency_stop) => unsafe { transmute(Box::new(emergency_stop)) },
}
}
#[no_mangle]
pub extern "C" fn emergency_stop_clean(emergency_stop: *mut EmergencyStop) {
let _emergency_stop: Box<EmergencyStop> = unsafe { transmute(emergency_stop) };
}
#[no_mangle]
pub extern "C" fn emergency_stop_get_state(emergency_stop: *mut EmergencyStop) -> bool {
let emergency_stop = unsafe { &mut *emergency_stop };
emergency_stop.state.load(Ordering::Relaxed)
}
#![feature(integer_atomics)]
mod emergency_stop;
mod ultrasonic_irq;
#[cfg(feature = "measure")]
mod measure;
pub use emergency_stop::*;
pub use ultrasonic_irq::*;
#[cfg(feature = "measure")]
pub use measure::*;
extern crate kawaii;
use self::kawaii::Measure;
use std::ffi::CStr;
use std::ptr::null_mut;
use std::mem::transmute;
use std::os::raw::c_char;
#[no_mangle]
pub extern "C" fn measure_init(name: *const c_char) -> *mut Measure {
if name.is_null() {
return null_mut();
}
unsafe {
match CStr::from_ptr(name).to_str() {
Err(_) => null_mut(),
Ok(name) => transmute(Box::new(Measure::new(String::from(name)))),
}
}
}
#[no_mangle]
pub extern "C" fn measure_clean(measure: *mut Measure) {
let _measure: Box<Measure> = unsafe { transmute(measure) };
}
#[no_mangle]
pub extern "C" fn measure_start(measure: *mut Measure) {
let measure = unsafe { &mut *measure };
measure.start();
}
#[no_mangle]
pub extern "C" fn measure_pause(measure: *mut Measure) {
let measure = unsafe { &mut *measure };
measure.pause();
}
#[no_mangle]
pub extern "C" fn measure_stop(measure: *mut Measure) {
let measure = unsafe { &mut *measure };
measure.stop();
}
\ No newline at end of file
extern crate ultrasonic_irq;
use std::mem::transmute;
use std::ptr::null_mut;
use std::sync::atomic::Ordering;
use self::ultrasonic_irq::Ultrasonic;
#[no_mangle]
pub extern "C" fn ultrasonic_init(trigger: u8, echo: u8, temperature: u8) -> *mut Ultrasonic {
match Ultrasonic::new(trigger, echo, temperature) {
Err(_) => null_mut(),
Ok(ultrasonic) => unsafe { transmute(Box::new(ultrasonic)) },
}
}
#[no_mangle]
pub extern "C" fn ultrasonic_clean(ultrasonic: *mut Ultrasonic) {
let _ultrasonic: Box<Ultrasonic> = unsafe { transmute(ultrasonic) };
}
#[no_mangle]
pub extern "C" fn ultrasonic_get_distance(ultrasonic: *mut Ultrasonic) -> u32 {
let ultrasonic = unsafe { &mut *ultrasonic };
ultrasonic.distance.load(Ordering::Relaxed)
}
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