From: Tilman Sauerbeck Date: Sat, 28 Dec 2019 22:11:09 +0000 (+0100) Subject: common: Add Rust glue for the ringbuf module. X-Git-Url: http://git.code-monkey.de/?a=commitdiff_plain;h=00f7e3a2c957f387745dbd4ed3cf7834e5006513;p=gps-watch.git common: Add Rust glue for the ringbuf module. For now this only allows us to access a ringbuffer from Rust after it has been defined in C code. --- diff --git a/SConscript.libcommon b/SConscript.libcommon index e1d378c..e52b43a 100644 --- a/SConscript.libcommon +++ b/SConscript.libcommon @@ -13,6 +13,7 @@ source_files_rs = [ 'src/common/uart.rs', 'src/common/watchdog.rs', 'src/common/crc32.rs', + 'src/common/ringbuf.rs', 'src/common/buffer.rs', 'src/common/usb_serial.rs', 'src/common/display.rs', diff --git a/src/common/lib.rs b/src/common/lib.rs index 61d8f79..2f7d894 100644 --- a/src/common/lib.rs +++ b/src/common/lib.rs @@ -35,6 +35,7 @@ pub mod i2c; pub mod uart; pub mod watchdog; pub mod crc32; +pub mod ringbuf; pub mod buffer; pub mod usb_serial; pub mod screen; diff --git a/src/common/ringbuf.rs b/src/common/ringbuf.rs new file mode 100644 index 0000000..2599353 --- /dev/null +++ b/src/common/ringbuf.rs @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2019 Tilman Sauerbeck (tilman at code-monkey de) + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +pub enum Ringbuf { +} + +extern { + fn ringbuf_is_empty(ringbuf: *const Ringbuf) -> bool; + fn ringbuf_is_full(ringbuf: *const Ringbuf) -> bool; + fn ringbuf_write(ringbuf: *mut Ringbuf, value: u8); + fn ringbuf_read(ringbuf: *const Ringbuf) -> u8; +} + +impl Ringbuf { + pub fn is_empty(&self) -> bool { + unsafe { ringbuf_is_empty(self) } + } + + pub fn is_full(&self) -> bool { + unsafe { ringbuf_is_full(self) } + } + + pub fn write(&mut self, value: u8) { + unsafe { ringbuf_write(self, value) } + } + + pub fn read(&self) -> u8 { + unsafe { ringbuf_read(self) } + } +}