all FX functions are now safe wrapped and I added the crate to the docs also it is added to the prelude

This commit is contained in:
ZennDev1337 2023-10-04 14:47:38 +02:00
parent 3f847e86a6
commit 522844499e
1272 changed files with 1371 additions and 61826 deletions

View file

@ -33,10 +33,9 @@ mod print;
#[doc(inline)]
pub extern crate heapless;
pub use crate::library::arduboy2::{self, Arduboy2, Color, FONT_SIZE, HEIGHT, WIDTH};
pub use crate::library::arduboy_tone::{self, ArduboyTones};
pub use crate::library::arduboy_tones::{self, ArduboyTones};
pub use crate::library::ardvoice::{self, ArdVoice};
pub use crate::library::eeprom::{EEPROM, EEPROMBYTE};
pub use crate::library::{arduino, c, sprites};
pub use crate::library::arduboyfx;
pub use crate::library::arduboyfx::{self};
pub mod serial_print;

View file

@ -66,6 +66,8 @@ impl Arduboy2 {
/// gives you a new instance of the [Arduboy2]
/// ## Example
/// ```
/// #![allow(non_upper_case_globals)]
/// use arduboy_rust::prelude::*;
/// const arduboy: Arduboy2 = Arduboy2::new();
/// ```
pub const fn new() -> Self {
@ -342,8 +344,11 @@ impl Arduboy2 {
///- ASCII carriage return (\r, 0x0D, musical eighth note). This character will be ignored.
///
///
///Example
/// ## Example
/// ```
/// #![allow(non_upper_case_globals)]
/// use arduboy_rust::prelude::*;
/// const arduboy:Arduboy2 =Arduboy2::new();
/// let value: i16 = 42;
///
/// arduboy.print(b"Hello World\n\0"[..]); // Prints "Hello World" and then sets the
@ -542,10 +547,14 @@ impl Arduboy2 {
///
///## Example
///If you wanted to fire a shot every 5 frames while the A button is being held down:
/// ```
///```
/// #![allow(non_upper_case_globals)]
/// use arduboy_rust::prelude::*;
/// const arduboy:Arduboy2 =Arduboy2::new();
///
/// if arduboy.everyXFrames(5) {
/// if arduboy.pressed(A_BUTTON) {
/// fireShot();
/// //fireShot(); // just some example
/// }
/// }
/// ```

View file

@ -1,39 +1,10 @@
//!This is the Module to interact in a save way with the ArduboyTones C++ library.
//!
//! You will need to uncomment the ArduboyTones_Library in the import_config.h file.
pub use crate::library::arduboy_tone_pitch;
pub mod tones_pitch;
use core::ffi::{c_uchar, c_uint, c_ulong};
extern "C" {
#[link_name = "sound_tone"]
fn sound_tone(frequency: c_uint, duration: c_ulong);
#[link_name = "sound_tone2"]
fn sound_tone2(frequency1: c_uint, duration1: c_ulong, frequency2: c_uint, duration2: c_ulong);
#[link_name = "sound_tone3"]
fn sound_tone3(
frequency1: c_uint,
duration1: c_ulong,
frequency2: c_uint,
duration2: c_ulong,
frequency3: c_uint,
duration3: c_ulong,
);
#[link_name = "sound_tones"]
fn sound_tones(tones: *const c_uint);
#[link_name = "sound_no_tone"]
fn sound_no_tone();
#[link_name = "sound_playing"]
fn sound_playing() -> bool;
#[link_name = "sound_tones_in_ram"]
fn sound_tones_in_ram(tones: *mut c_ulong);
#[link_name = "sound_volume_mode"]
fn sound_volume_mode(mode: c_uchar);
}
///This is the struct to interact in a save way with the ArduboyTones C++ library.
///
/// You will need to uncomment the ArduboyTones_Library in the import_config.h file.
@ -42,6 +13,7 @@ impl ArduboyTones {
///Get a new instance of [ArduboyTones]
/// ## Example
/// ```
/// use arduboy_rust::prelude::*;
/// const sound: ArduboyTones = ArduboyTones::new();
/// ```
pub const fn new() -> ArduboyTones {
@ -79,7 +51,11 @@ impl ArduboyTones {
frequency3: u16,
duration3: u32,
) {
unsafe { sound_tone3(frequency1, duration1, frequency2, duration2, frequency3, duration3) }
unsafe {
sound_tone3(
frequency1, duration1, frequency2, duration2, frequency3, duration3,
)
}
}
/// Play a tone sequence from frequency/duration pairs in a PROGMEM array.
///
@ -94,11 +70,13 @@ impl ArduboyTones {
///
/// Example:
/// ```
/// use arduboy_rust::prelude::*;
/// const sound:ArduboyTones=ArduboyTones::new();
/// progmem!(
/// static sound1:[u8;_]=[220,1000, 0,250, 440,500, 880,2000,TONES_END]
/// static sound1:[u8;_]=[220,1000, 0,250, 440,500, 880,2000,TONES_END];
/// );
///
/// tones(get_tones_addr!(sound1));
/// sound.tones(get_tones_addr!(sound1));
/// ```
pub fn tones(&self, tones: *const u16) {
unsafe { sound_tones(tones) }
@ -130,6 +108,8 @@ impl ArduboyTones {
/// Example:
///
/// ```
/// use arduboy_rust::prelude::*;
/// use arduboy_tones::tones_pitch::*;
/// let sound2: [u16; 9] = [220, 1000, 0, 250, 440, 500, 880, 2000, TONES_END];
/// ```
/// Using `tones()`, with the data in PROGMEM, is normally a better
@ -150,3 +130,28 @@ impl ArduboyTones {
unsafe { sound_volume_mode(mode) }
}
}
extern "C" {
#[link_name = "sound_tone"]
fn sound_tone(frequency: c_uint, duration: c_ulong);
#[link_name = "sound_tone2"]
fn sound_tone2(frequency1: c_uint, duration1: c_ulong, frequency2: c_uint, duration2: c_ulong);
#[link_name = "sound_tone3"]
fn sound_tone3(
frequency1: c_uint,
duration1: c_ulong,
frequency2: c_uint,
duration2: c_ulong,
frequency3: c_uint,
duration3: c_ulong,
);
#[link_name = "sound_tones"]
fn sound_tones(tones: *const c_uint);
#[link_name = "sound_no_tone"]
fn sound_no_tone();
#[link_name = "sound_playing"]
fn sound_playing() -> bool;
#[link_name = "sound_tones_in_ram"]
fn sound_tones_in_ram(tones: *mut c_ulong);
#[link_name = "sound_volume_mode"]
fn sound_volume_mode(mode: c_uchar);
}

View file

@ -1,45 +1,9 @@
#![allow(non_upper_case_globals)]
use core::ffi::{c_char, c_int, c_size_t, c_uchar, c_uint, c_ulong};
pub const dbmNormal: u8 = 0;
pub const dbmOverwrite: u8 = 0;
pub const dbmMasked: u8 = 1;
extern "C" {
#[link_name = "arduboyfx_begin"]
pub fn arduboyfx_begin();
#[link_name = "arduboyfx_begin_data"]
pub fn arduboyfx_begin_data(datapage: c_uint);
#[link_name = "arduboyfx_begin_data_save"]
pub fn arduboyfx_begin_data_save(datapage: c_uint, savepage: c_uint);
#[link_name = "arduboyfx_display"]
pub fn arduboyfx_display();
#[link_name = "arduboyfx_display_clear"]
pub fn arduboyfx_display_clear();
#[link_name = "arduboyfx_read_data_array"]
pub fn arduboyfx_read_data_array(
address: c_ulong,
index: c_uchar,
offset: c_uchar,
element_size: c_uchar,
buffer: *const c_uchar,
length: c_size_t,
);
#[link_name = "arduboyfx_draw_bitmap"]
pub fn arduboyfx_draw_bitmap(
x: c_int,
y: c_int,
address: c_ulong,
frame: c_uchar,
mode: c_uchar,
);
#[link_name = "arduboyfx_set_frame"]
pub fn arduboyfx_set_frame(frame: c_ulong, repeat: c_uchar);
#[link_name = "arduboyfx_draw_frame"]
pub fn arduboyfx_draw_frame(address: c_ulong) -> c_ulong;
#[link_name = "arduboyfx_draw_string"]
pub fn arduboyfx_draw_string(address: c_ulong);
#[link_name = "arduboyfx_draw_string_buffer"]
pub fn arduboyfx_draw_string_buffer(buffer: *const c_uchar);
#[link_name = "arduboyfx_set_cursor"]
pub fn arduboyfx_set_cursor(x: c_int, y: c_int);
}
//! This is the Module to interact in a save way with the ArduboyFX C++ library.
//!
//! You will need to uncomment the ArduboyFX_Library in the import_config.h file.
pub mod fx_consts;
mod drawable_number;
pub use drawable_number::DrawableNumber;
mod drawable_string;
pub use drawable_string::DrawableString;
pub mod fx;

View file

@ -0,0 +1,38 @@
use core::ffi::{c_char, c_int, c_long, c_uint, c_ulong};
pub trait DrawableNumber
where
Self: Sized,
{
fn draw(self, digits: i8);
}
impl DrawableNumber for i16 {
fn draw(self, digits: i8) {
unsafe { arduboyfx_draw_number_i16(self, digits) }
}
}
impl DrawableNumber for u16 {
fn draw(self, digits: i8) {
unsafe { arduboyfx_draw_number_u16(self, digits) }
}
}
impl DrawableNumber for i32 {
fn draw(self, digits: i8) {
unsafe { arduboyfx_draw_number_i32(self, digits) }
}
}
impl DrawableNumber for u32 {
fn draw(self, digits: i8) {
unsafe { arduboyfx_draw_number_u32(self, digits) }
}
}
extern "C" {
#[link_name = "arduboyfx_draw_number_i16"]
fn arduboyfx_draw_number_i16(n: c_int, digits: c_char);
#[link_name = "arduboyfx_draw_number_u16"]
fn arduboyfx_draw_number_u16(n: c_uint, digits: c_char);
#[link_name = "arduboyfx_draw_number_i32"]
fn arduboyfx_draw_number_i32(n: c_long, digits: c_char);
#[link_name = "arduboyfx_draw_number_u32"]
fn arduboyfx_draw_number_u32(n: c_ulong, digits: c_char);
}

View file

@ -0,0 +1,53 @@
use core::ffi::{c_char, c_uchar, c_ulong};
use crate::library::progmem::Pstring;
pub trait DrawableString
where
Self: Sized,
{
fn draw(self);
}
impl DrawableString for &[u8] {
fn draw(self) {
unsafe {
arduboyfx_draw_string(self as *const [u8] as *const i8);
}
}
}
impl DrawableString for &str {
fn draw(self) {
unsafe {
arduboyfx_draw_string(self.as_bytes() as *const [u8] as *const i8);
}
}
}
impl<const N: usize> DrawableString for crate::heapless::String<N> {
fn draw(self) {
unsafe {
arduboyfx_draw_string(self.as_bytes() as *const [u8] as *const i8);
}
}
}
impl DrawableString for Pstring {
fn draw(self) {
unsafe {
arduboyfx_draw_string_buffer(self.pointer as *const u8);
}
}
}
impl DrawableString for u32 {
fn draw(self) {
unsafe {
arduboyfx_draw_string_fx(self);
}
}
}
extern "C" {
#[link_name = "arduboyfx_draw_string_fx"]
fn arduboyfx_draw_string_fx(address: c_ulong);
#[link_name = "arduboyfx_draw_string_buffer"]
fn arduboyfx_draw_string_buffer(buffer: *const c_uchar);
#[link_name = "arduboyfx_draw_string"]
fn arduboyfx_draw_string(cstr: *const c_char);
}

View file

@ -0,0 +1,115 @@
//! Functions given by the ArduboyFX library.
//!
//! You can use the 'FX::' module to access the functions after the import of the prelude
//! ```
//! use arduboy_rust::prelude::*;
//!
//! fn setup(){
//! FX::begin()
//! }
//!
//! ```
//! You will need to uncomment the ArduboyFX_Library in the import_config.h file.
#![allow(non_upper_case_globals)]
use super::drawable_number::DrawableNumber;
use super::drawable_string::DrawableString;
use core::ffi::{c_int, c_long, c_size_t, c_uchar, c_uint, c_ulong};
pub fn begin() {
unsafe { arduboyfx_begin() }
}
pub fn begin_data(datapage: u16) {
unsafe { arduboyfx_begin_data(datapage) }
}
pub fn begin_data_save(datapage: u16, savepage: u16) {
unsafe { arduboyfx_begin_data_save(datapage, savepage) }
}
pub fn display() {
unsafe { arduboyfx_display() }
}
pub fn display_clear() {
unsafe { arduboyfx_display_clear() }
}
pub fn draw_number(n: impl DrawableNumber, digits: i8) {
n.draw(digits)
}
pub fn draw_string(string: impl DrawableString) {
string.draw()
}
pub fn draw_char(c: u8) {
unsafe { arduboyfx_draw_char(c) }
}
pub fn draw_bitmap(x: i16, y: i16, address: u32, frame: u8, mode: u8) {
unsafe { arduboyfx_draw_bitmap(x, y, address, frame, mode) }
}
pub fn draw_frame(address: u32) -> u32 {
unsafe { arduboyfx_draw_frame(address) }
}
pub fn set_frame(frame: u32, repeat: u8) {
unsafe { arduboyfx_set_frame(frame, repeat) }
}
pub fn read_data_array(
address: u32,
index: u8,
offset: u8,
element_size: u8,
buffer: *const u8,
length: usize,
) {
unsafe { arduboyfx_read_data_array(address, index, offset, element_size, buffer, length) }
}
pub fn set_cursor(x: i16, y: i16) {
unsafe { arduboyfx_set_cursor(x, y) }
}
pub fn set_cursor_x(x: i16) {
unsafe { arduboyfx_set_cursor_x(x) }
}
pub fn set_cursor_y(y: i16) {
unsafe { arduboyfx_set_cursor_y(y) }
}
pub fn set_cursor_range(left: i32, wrap: i32) {
unsafe { arduboyfx_set_cursor_range(left, wrap) }
}
pub fn set_font(address: u32, mode: u8) {
unsafe { arduboyfx_set_font(address, mode) }
}
extern "C" {
#[link_name = "arduboyfx_begin"]
fn arduboyfx_begin();
#[link_name = "arduboyfx_begin_data"]
fn arduboyfx_begin_data(datapage: c_uint);
#[link_name = "arduboyfx_begin_data_save"]
fn arduboyfx_begin_data_save(datapage: c_uint, savepage: c_uint);
#[link_name = "arduboyfx_display"]
fn arduboyfx_display();
#[link_name = "arduboyfx_display_clear"]
fn arduboyfx_display_clear();
#[link_name = "arduboyfx_read_data_array"]
fn arduboyfx_read_data_array(
address: c_ulong,
index: c_uchar,
offset: c_uchar,
element_size: c_uchar,
buffer: *const c_uchar,
length: c_size_t,
);
#[link_name = "arduboyfx_draw_bitmap"]
fn arduboyfx_draw_bitmap(x: c_int, y: c_int, address: c_ulong, frame: c_uchar, mode: c_uchar);
#[link_name = "arduboyfx_set_frame"]
fn arduboyfx_set_frame(frame: c_ulong, repeat: c_uchar);
#[link_name = "arduboyfx_draw_frame"]
fn arduboyfx_draw_frame(address: c_ulong) -> c_ulong;
#[link_name = "arduboyfx_set_cursor"]
fn arduboyfx_set_cursor(x: c_int, y: c_int);
#[link_name = "arduboyfx_set_cursor_x"]
fn arduboyfx_set_cursor_x(x: c_int);
#[link_name = "arduboyfx_set_cursor_y"]
fn arduboyfx_set_cursor_y(y: c_int);
#[link_name = "arduboyfx_set_font"]
fn arduboyfx_set_font(address: c_ulong, mode: c_uchar);
#[link_name = "arduboyfx_set_cursor_range"]
fn arduboyfx_set_cursor_range(left: c_long, wrap: c_long);
#[link_name = "arduboyfx_draw_char"]
fn arduboyfx_draw_char(c: c_uchar);
}

View file

@ -0,0 +1,61 @@
//! Consts given by the ArduboyFX library.
//!
//! You can use the `use arduboyfx::fx_consts::*;` module to access the consts.
//! ```
//! use arduboy_rust::prelude::*;
//! use arduboyfx::fx_consts::*;
//!
//! fn setup(){
//! let demo = dbmBlack;
//! }
//!
//! ```
#![allow(non_upper_case_globals)]
pub const dbfWhiteBlack: u8 = 0;
pub const dbfInvert: u8 = 0;
pub const dbfBlack: u8 = 0;
pub const dbfReverseBlack: u8 = 3;
pub const dbfMasked: u8 = 4;
pub const dbfFlip: u8 = 5;
pub const dbfExtraRow: u8 = 7;
pub const dbfEndFrame: u8 = 6;
pub const dbfLastFrame: u8 = 7;
pub const dbmBlack: u8 = (1 << dbfReverseBlack) | (1 << dbfBlack) | (1 << dbfWhiteBlack);
pub const dbmWhite: u8 = 1 << dbfWhiteBlack;
pub const dbmInvert: u8 = 1 << dbfInvert;
pub const dbmFlip: u8 = 1 << dbfFlip;
pub const dbmNormal: u8 = 0;
pub const dbmOverwrite: u8 = 0;
pub const dbmReverse: u8 = 1 << dbfReverseBlack;
pub const dbmMasked: u8 = 1 << dbfMasked;
pub const dbmEndFrame: u8 = 1 << dbfEndFrame;
pub const dbmLastFrame: u8 = 1 << dbfLastFrame;
pub const dcfWhiteBlack: u8 = 0;
pub const dcfInvert: u8 = 1;
pub const dcfBlack: u8 = 2;
pub const dcfReverseBlack: u8 = 3;
pub const dcfMasked: u8 = 4;
pub const dcfProportional: u8 = 5;
pub const dcmBlack: u8 = (1 << dcfReverseBlack) | (1 << dcfBlack) | (1 << dcfWhiteBlack);
pub const dcmWhite: u8 = 1 << dcfWhiteBlack;
pub const dcmInvert: u8 = 1 << dcfInvert;
pub const dcmNormal: u8 = 0;
pub const dcmOverwrite: u8 = 0;
pub const dcmReverse: u8 = 1 << dcfReverseBlack;
pub const dcmMasked: u8 = 1 << dcfMasked;
pub const dcmProportional: u8 = 1 << dcfProportional;
pub const FX_VECTOR_KEY_VALUE: u16 = 0x9518;
pub const FX_DATA_VECTOR_KEY_POINTER: u16 = 0x0014;
pub const FX_DATA_VECTOR_PAGE_POINTER: u16 = 0x0016;
pub const FX_SAVE_VECTOR_KEY_POINTER: u16 = 0x0018;
pub const FX_SAVE_VECTOR_PAGE_POINTER: u16 = 0x001A;
pub const SFC_JEDEC_ID: u8 = 0x9F;
pub const SFC_READSTATUS1: u8 = 0x05;
pub const SFC_READSTATUS2: u8 = 0x35;
pub const SFC_READSTATUS3: u8 = 0x15;
pub const SFC_READ: u8 = 0x03;
pub const SFC_WRITE_ENABLE: u8 = 0x06;
pub const SFC_WRITE: u8 = 0x02;
pub const SFC_ERASE: u8 = 0x20;
pub const SFC_RELEASE_POWERDOWN: u8 = 0xAB;
pub const SFC_POWERDOWN: u8 = 0xB9;

View file

@ -140,6 +140,7 @@ impl EEPROMBYTE {
}
///Use this struct to store and read single bytes to/from eeprom memory without using a check digit.
///
///Unlike the other eeprom structs, this does not need to be initialised.
pub struct EEPROMBYTECHECKLESS {
idx: i16,

View file

@ -1,6 +1,5 @@
pub mod arduboy2;
pub mod arduboy_tone;
pub mod arduboy_tone_pitch;
pub mod arduboy_tones;
pub mod arduino;
pub mod ardvoice;
pub mod c;

View file

@ -10,10 +10,13 @@ pub use crate::hardware::buttons::{self, *};
pub use crate::hardware::led::{self, *};
pub use crate::heapless::{LinearMap, String, Vec};
pub use crate::library::arduboy2::{self, *};
pub use crate::library::arduboy_tone::{self, ArduboyTones};
pub use crate::library::arduboy_tones::{self, ArduboyTones};
pub use crate::library::arduino::*;
pub use crate::library::ardvoice::{self, ArdVoice};
pub use crate::library::c::*;
#[doc(hidden)]
pub use crate::library::arduboyfx::{ fx as FX};
pub use crate::library::arduboyfx::{self};
pub use crate::library::eeprom::{EEPROM, EEPROMBYTE, EEPROMBYTECHECKLESS};
#[doc(hidden)]
pub use crate::library::progmem::Pstring;
@ -21,8 +24,10 @@ pub use crate::library::sprites;
pub use crate::print::*;
pub use crate::{
f, get_ardvoice_tone_addr, get_sprite_addr, get_string_addr, get_tones_addr, progmem,
serial_print as serial,
};
#[doc(inline)]
pub use crate::{serial_print as serial};
use core::cmp;
pub use core::ffi::{
c_char, c_double, c_float, c_int, c_long, c_longlong, c_size_t, c_uchar, c_uint, c_ulong,