#![no_std]
use bl602_macros::safe_wrap;
use result::*;
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
pub mod adc;
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
pub mod dma;
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
pub mod gpio;
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
pub mod i2c;
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
pub mod pwm;
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
pub mod spi;
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
pub mod uart;
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
pub mod wifi;
use core::{
str::FromStr, };
pub fn puts(s: &str) -> i32 {
extern "C" { fn puts(s: *const u8) -> i32;
}
let mut s_with_null = String::from_str(s) .expect("puts conversion failed");
s_with_null.push('\0')
.expect("puts overflow");
let p = s_with_null.as_str().as_ptr();
unsafe { puts(p)
}
}
pub fn time_ms_to_ticks32(
ms: u32 ) -> u32 { extern "C" { fn ble_npl_time_ms_to_ticks32(ms: u32) -> u32;
}
unsafe { ble_npl_time_ms_to_ticks32(ms)
}
}
pub fn time_delay(
ticks: u32 ) {
extern "C" { fn ble_npl_time_delay(ticks: u32);
}
unsafe { ble_npl_time_delay(ticks);
}
}
pub type String = heapless::String::<64>;
pub mod result {
pub type BlResult<T> = ::core::result::Result<T, BlError>;
#[repr(i32)]
#[derive(PartialEq)]
#[allow(non_camel_case_types)] pub enum BlError {
SYS_EOK = 0,
SYS_UNKNOWN = -1,
SYS_NULLPOINTER = -2,
}
impl From<BlError> for i32 {
fn from(err: BlError) -> Self {
err as i32
}
}
impl From<i32> for BlError {
fn from(num: i32) -> Self {
unsafe {
::core::mem::transmute::
<i32, BlError>
(num)
}
}
}
impl From<()> for BlError {
fn from(_: ()) -> Self {
BlError::SYS_UNKNOWN
}
}
impl core::fmt::Debug for BlError {
fn fmt(&self, _fmt: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
Ok(())
}
}
}
#[derive(Clone, Copy)] pub struct Strn<'a> {
pub rep: StrnRep<'a>
}
#[derive(Clone, Copy)] #[repr(u8)]
pub enum StrnRep<'a> {
ByteStr(&'a [u8]),
CStr(*const u8),
}
impl<'a> Strn<'a> {
pub fn new(bs: &[u8]) -> Strn {
assert_eq!(bs.last(), Some(&0u8), "no null"); Strn {
rep: StrnRep::ByteStr(bs)
}
}
pub fn from_cstr(cstr: *const u8) -> Strn<'a> {
Strn {
rep: StrnRep::CStr(cstr)
}
}
pub fn as_ptr(&self) -> *const u8 {
match self.rep {
StrnRep::ByteStr(bs) => { bs.as_ptr() }
StrnRep::CStr(cstr) => { cstr }
}
}
pub fn len(&self) -> usize {
match self.rep {
StrnRep::ByteStr(bs) => {
assert_eq!(bs.last(), Some(&0u8), "no null"); bs.len() - 1 }
StrnRep::CStr(cstr) => {
if cstr.is_null() { return 0; }
for len in 0..127 {
let ptr: *const u8 = ((cstr as u32) + len) as *const u8;
if unsafe { *ptr } == 0 { return len as usize; }
}
assert!(false, "big strn"); return 128 as usize;
}
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn as_cstr(&self) -> *const u8 {
match self.rep {
StrnRep::ByteStr(bs) => {
assert_eq!(bs.last(), Some(&0u8), "no null"); bs.as_ptr() as *const u8
}
StrnRep::CStr(cstr) => { cstr }
}
}
pub fn as_bytestr(&self) -> &'a [u8] {
match self.rep {
StrnRep::ByteStr(bs) => {
assert_eq!(bs.last(), Some(&0u8), "no null"); &bs
}
StrnRep::CStr(_cstr) => {
assert!(false, "strn cstr"); b"\0"
}
}
}
pub fn validate(&self) {
match self.rep {
StrnRep::ByteStr(bs) => {
assert_eq!(bs.last(), Some(&0u8), "no null"); }
StrnRep::CStr(_cstr) => {}
}
}
pub fn validate_bytestr(bs: &'static [u8]) {
assert_eq!(bs.last(), Some(&0u8), "no null"); }
}
unsafe impl<'a> Send for Strn<'a> {}
unsafe impl<'a> Sync for Strn<'a> {}
pub type Ptr = *mut ::cty::c_void;
#[no_mangle] extern "C" fn test_rust( _result: *mut u8, _len: i32, _argc: i32, _argv: *const *const u8 ) {
puts("Hello from Rust!");
const LED_GPIO: u8 = 11;
gpio::enable_output(LED_GPIO, 0, 0) .expect("GPIO enable output failed");
for i in 0..10 {
gpio::output_set( LED_GPIO, i % 2 ).expect("GPIO output failed");
time_delay( time_ms_to_ticks32(1000) );
}
}