Skip to content

Commit

Permalink
INFRA - Initialise Core (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: David Beechey <david@dtbeechey.dev>
  • Loading branch information
kshxtij and davidbeechey authored Sep 5, 2024
1 parent c892334 commit 0f1da38
Show file tree
Hide file tree
Showing 10 changed files with 1,084 additions and 20 deletions.
571 changes: 551 additions & 20 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"lib/*",
"config",
]
resolver = "2"
234 changes: 234 additions & 0 deletions lib/core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions lib/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "hyped_core"
version = "0.1.0"
edition = "2021"

[dependencies]
defmt = "0.3"
heapless = { version = "0.8", default-features = false, features = ["serde"] }
rust-mqtt = { version = "0.3.0", default-features = false, features = ["defmt"] }
embedded-io-async = { version = "0.6.1" }
rand_core = "0.6.3"
serde = { version = "1.0", default-features = false, features = ["derive"] }
embassy-net = { version = "0.4.0", default-features = false, features = ["defmt", "tcp", "proto-ipv4", "medium-ip"] }

[features]
std = []
55 changes: 55 additions & 0 deletions lib/core/src/format_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use core::cmp::min;
use core::fmt;

pub struct FormatString<'a> {
buffer: &'a mut [u8],
used: usize,
}

impl<'a> FormatString<'a> {
pub fn new(buffer: &'a mut [u8]) -> Self {
FormatString { buffer, used: 0 }
}

pub fn as_str(self) -> Option<&'a str> {
if self.used <= self.buffer.len() {
use core::str::from_utf8;
Some(from_utf8(&self.buffer[..self.used]).unwrap())
} else {
None
}
}
}

impl<'a> fmt::Write for FormatString<'a> {
fn write_str(&mut self, s: &str) -> fmt::Result {
if self.used > self.buffer.len() {
return Err(fmt::Error);
}
let remaining_buf = &mut self.buffer[self.used..];
let raw_s = s.as_bytes();
let write_num = min(raw_s.len(), remaining_buf.len());
remaining_buf[..write_num].copy_from_slice(&raw_s[..write_num]);
self.used += raw_s.len();
if write_num < raw_s.len() {
Err(fmt::Error)
} else {
Ok(())
}
}
}

pub fn show<'a>(buffer: &'a mut [u8; 1024], args: fmt::Arguments) -> Result<&'a str, fmt::Error> {
let mut w = FormatString::new(buffer);
fmt::write(&mut w, args)?;
w.as_str().ok_or(fmt::Error)
}

#[macro_export]
macro_rules! format {
($buffer:expr, $($arg:tt)*) => {
{
show(&mut $buffer, format_args!($($arg)*))
}
};
}
7 changes: 7 additions & 0 deletions lib/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub mod format_string;
pub mod log_types;
pub mod mqtt;
pub mod mqtt_topics;
pub mod types;
12 changes: 12 additions & 0 deletions lib/core/src/log_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[derive(PartialEq, PartialOrd)]
pub enum LogLevel {
Debug = 0,
Info = 1,
Warn = 2,
Error = 3,
}

pub enum LogTarget {
Console,
Mqtt,
}
Loading

0 comments on commit 0f1da38

Please sign in to comment.