use crate::arduino_system::progmem::Pstring;
use core::ffi::{c_char, c_int, c_long, c_size_t, c_uchar, c_uint, c_ulong};
use crate::libraries::arduboy2_library::print::Base;
extern "C" {
#[link_name = "arduino_serial_begin"]
fn serial_begin(serial: c_ulong);
#[link_name = "arduino_serial_end"]
fn serial_end();
#[link_name = "arduino_serial_available"]
fn serial_available() -> c_int;
#[link_name = "arduino_serial_read"]
fn serial_read() -> c_int;
}
pub fn print(x: impl Serialprintable) {
x.print()
}
pub fn println(x: impl Serialprintlnable) {
x.println()
}
pub fn begin(baud_rates: u32) {
unsafe { serial_begin(baud_rates) }
}
pub fn end() {
unsafe { serial_end() }
}
pub fn read() -> i16 {
unsafe { serial_read() }
}
pub fn read_as_utf8_str() -> &'static str {
let incoming_byte = unsafe { serial_read() };
static mut L: [u8; 2] = [0, 0];
unsafe {
L[0] = incoming_byte as u8;
}
unsafe { core::str::from_utf8(&L).unwrap() }
}
pub fn available() -> i16 {
unsafe { serial_available() }
}
pub trait Serialprintlnable
where
Self: Sized,
{
type Parameters;
fn println_2(self, params: Self::Parameters);
fn default_parameters() -> Self::Parameters;
fn println(self) {
self.println_2(Self::default_parameters());
}
}
impl Serialprintlnable for i16 {
type Parameters = Base;
fn println_2(self, params: Self::Parameters) {
unsafe {
println_int(self, params as c_int);
}
}
fn default_parameters() -> Self::Parameters {
Base::Dec
}
}
impl Serialprintlnable for u16 {
type Parameters = Base;
fn println_2(self, params: Self::Parameters) {
unsafe {
println_unsigned_int(self, params as c_int);
}
}
fn default_parameters() -> Self::Parameters {
Base::Dec
}
}
impl Serialprintlnable for i32 {
type Parameters = Base;
fn println_2(self, params: Self::Parameters) {
unsafe {
println_long(self, params as c_int);
}
}
fn default_parameters() -> Self::Parameters {
Base::Dec
}
}
impl Serialprintlnable for u32 {
type Parameters = Base;
fn println_2(self, params: Self::Parameters) {
unsafe {
println_unsigned_long(self, params as c_int);
}
}
fn default_parameters() -> Self::Parameters {
Base::Dec
}
}
impl Serialprintlnable for &[u8] {
type Parameters = ();
fn println_2(self, _params: Self::Parameters) {
unsafe {
println_chars(self as *const [u8] as *const i8);
}
}
fn default_parameters() -> Self::Parameters {}
}
impl Serialprintlnable for &str {
type Parameters = ();
fn println_2(self, _params: Self::Parameters) {
unsafe {
println_chars(self.as_bytes() as *const [u8] as *const i8);
}
}
fn default_parameters() -> Self::Parameters {}
}
impl<const N: usize> Serialprintlnable for heapless::String<N> {
type Parameters = ();
fn println_2(self, _params: Self::Parameters) {
unsafe {
println_chars(self.as_bytes() as *const [u8] as *const i8);
}
}
fn default_parameters() -> Self::Parameters {}
}
impl Serialprintlnable for Pstring {
type Parameters = ();
fn println_2(self, _params: Self::Parameters) {
unsafe {
println_chars_progmem(self.pointer);
}
}
fn default_parameters() -> Self::Parameters {}
}
extern "C" {
#[link_name = "arduino_serial_println_chars"]
fn println_chars(cstr: *const c_char);
#[doc(hidden)]
#[link_name = "arduino_serial_println_chars_progmem"]
fn println_chars_progmem(pstring: *const c_char);
#[doc(hidden)]
#[link_name = "arduino_serial_println_int"]
fn println_int(n: c_int, base: c_int) -> c_size_t;
#[doc(hidden)]
#[link_name = "arduino_serial_println_long"]
fn println_long(n: c_long, base: c_int) -> c_size_t;
#[allow(dead_code)]
#[doc(hidden)]
#[link_name = "arduino_serial_println_unsigned_char"]
fn println_unsigned_char(n: c_uchar, base: c_int) -> c_size_t;
#[doc(hidden)]
#[link_name = "arduino_serial_println_unsigned_int"]
fn println_unsigned_int(n: c_uint, base: c_int) -> c_size_t;
#[doc(hidden)]
#[link_name = "arduino_serial_println_unsigned_long"]
fn println_unsigned_long(n: c_ulong, base: c_int) -> c_size_t;
}
pub trait Serialprintable
where
Self: Sized,
{
type Parameters;
fn print_2(self, params: Self::Parameters);
fn default_parameters() -> Self::Parameters;
fn print(self) {
self.print_2(Self::default_parameters());
}
}
impl Serialprintable for i16 {
type Parameters = Base;
fn print_2(self, params: Self::Parameters) {
unsafe {
print_int(self, params as c_int);
}
}
fn default_parameters() -> Self::Parameters {
Base::Dec
}
}
impl Serialprintable for u16 {
type Parameters = Base;
fn print_2(self, params: Self::Parameters) {
unsafe {
print_unsigned_int(self, params as c_int);
}
}
fn default_parameters() -> Self::Parameters {
Base::Dec
}
}
impl Serialprintable for i32 {
type Parameters = Base;
fn print_2(self, params: Self::Parameters) {
unsafe {
print_long(self, params as c_int);
}
}
fn default_parameters() -> Self::Parameters {
Base::Dec
}
}
impl Serialprintable for u32 {
type Parameters = Base;
fn print_2(self, params: Self::Parameters) {
unsafe {
print_unsigned_long(self, params as c_int);
}
}
fn default_parameters() -> Self::Parameters {
Base::Dec
}
}
impl Serialprintable for &[u8] {
type Parameters = ();
fn print_2(self, _params: Self::Parameters) {
unsafe {
print_chars(self as *const [u8] as *const i8);
}
}
fn default_parameters() -> Self::Parameters {}
}
impl Serialprintable for &str {
type Parameters = ();
fn print_2(self, _params: Self::Parameters) {
unsafe {
print_chars(self.as_bytes() as *const [u8] as *const i8);
}
}
fn default_parameters() -> Self::Parameters {}
}
impl<const N: usize> Serialprintable for heapless::String<N> {
type Parameters = ();
fn print_2(self, _params: Self::Parameters) {
unsafe {
print_chars(self.as_bytes() as *const [u8] as *const i8);
}
}
fn default_parameters() -> Self::Parameters {}
}
impl Serialprintable for Pstring {
type Parameters = ();
fn print_2(self, _params: Self::Parameters) {
unsafe {
print_chars_progmem(self.pointer);
}
}
fn default_parameters() -> Self::Parameters {}
}
extern "C" {
#[link_name = "arduino_serial_print_chars"]
fn print_chars(cstr: *const c_char);
#[doc(hidden)]
#[link_name = "arduino_serial_print_chars_progmem"]
fn print_chars_progmem(pstring: *const c_char);
#[doc(hidden)]
#[link_name = "arduino_serial_print_int"]
fn print_int(n: c_int, base: c_int) -> c_size_t;
#[doc(hidden)]
#[link_name = "arduino_serial_print_long"]
fn print_long(n: c_long, base: c_int) -> c_size_t;
#[allow(dead_code)]
#[doc(hidden)]
#[link_name = "arduino_serial_print_unsigned_char"]
fn print_unsigned_char(n: c_uchar, base: c_int) -> c_size_t;
#[doc(hidden)]
#[link_name = "arduino_serial_print_unsigned_int"]
fn print_unsigned_int(n: c_uint, base: c_int) -> c_size_t;
#[doc(hidden)]
#[link_name = "arduino_serial_print_unsigned_long"]
fn print_unsigned_long(n: c_ulong, base: c_int) -> c_size_t;
}