Skip to content

Instantly share code, notes, and snippets.

@ndrscodes
Last active April 25, 2024 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndrscodes/e8ef0f61b0c0b4cdf4ddf6cc00c9cc37 to your computer and use it in GitHub Desktop.
Save ndrscodes/e8ef0f61b0c0b4cdf4ddf6cc00c9cc37 to your computer and use it in GitHub Desktop.
//#![no_std]
use std::usize;
use embassy_sync::{blocking_mutex::raw::{CriticalSectionRawMutex, CriticalSectionRawMutex}, channel::{Channel, Receiver, Sender}};
use embedded_can::{Frame};
trait Can<C> where C: Frame {
async fn read() -> Result<C, ()>;
async fn write(frame: C) -> Result<(), ()>;
}
struct Communicator<'a, F, const CAP: usize>
where F: Frame + Clone {
sender: Sender<'a, CriticalSectionRawMutex, F, CAP>,
receiver: Receiver<'a, CriticalSectionRawMutex, F, CAP>
}
impl<'a, F, const CAP: usize> Communicator<'a, F, CAP>
where F: Frame + Clone {
async fn send(&mut self, data: F) {
self.sender.send(data).await
}
async fn receive(&self) -> F {
self.receiver.receive().await
}
}
struct CanRouter<'a, C, F, const CAP: usize, const SUBS: usize> where C: Can<F>, F: GenericFrame {
can: C,
recv_chan: Channel<CriticalSectionRawMutex, F, CAP>,
receiver: Receiver<'a, CriticalSectionRawMutex, F, CAP>,
signal_map: heapless::FnvIndexMap<u32, heapless::Vec<Sender<'a, CriticalSectionRawMutex, F, CAP>, SUBS>, 128>
}
trait GenericFrame: Frame + Clone {}
impl<'a, C, F, const CAP: usize, const TASKS: usize> CanRouter<'a, C, F, CAP, TASKS>
where C: Can<F>, F: GenericFrame {
fn new(can: C) -> CanRouter<'a, C, F, CAP, TASKS> {
let c: Channel<CriticalSectionRawMutex, F, CAP> = Channel::new();
CanRouter {
can,
receiver: c.receiver(),
recv_chan: c,
signal_map: heapless::FnvIndexMap::new()
}
}
fn register(&'static mut self, ids: &heapless::Vec<u32, 128>, chan: &'a Channel<CriticalSectionRawMutex, F, CAP>) -> Communicator<'a, F, CAP> {
for i in ids {
if !self.signal_map.contains_key(i) {
let _ = self.signal_map.insert(*i, heapless::Vec::new());
}
let _ = self.signal_map.get_mut(i).unwrap().push(chan.sender());
}
let c: Communicator<'a, F, CAP> = Communicator {
receiver: chan.receiver(),
sender: self.recv_chan.sender()
};
c
}
}
#[derive(Clone)]
struct GF {}
impl Frame for GF {
fn is_standard(&self) -> bool {
!self.is_extended()
}
fn is_data_frame(&self) -> bool {
!self.is_remote_frame()
}
fn new(id: impl Into<embedded_can::Id>, data: &[u8]) -> Option<Self> {
todo!()
}
fn new_remote(id: impl Into<embedded_can::Id>, dlc: usize) -> Option<Self> {
todo!()
}
fn is_extended(&self) -> bool {
todo!()
}
fn is_remote_frame(&self) -> bool {
todo!()
}
fn id(&self) -> embedded_can::Id {
todo!()
}
fn dlc(&self) -> usize {
todo!()
}
fn data(&self) -> &[u8] {
todo!()
}
}
impl GenericFrame for GF {
}
struct GenericCan {}
impl Can<GF> for GenericCan {
async fn read() -> Result<GF, ()> {
todo!()
}
async fn write(frame: GF) -> Result<(), ()> {
todo!()
}
}
fn main() {
let mut r: CanRouter<'_, GenericCan, GF, 5, 10> = CanRouter::new(GenericCan{});
let c: Channel<CriticalSectionRawMutex, GF, 5> = Channel::new();
let mut comm = r.register(&heapless::Vec::new(), &c);
comm.send(!todo!());
println!("Hello, world!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment