|
| 1 | +use embassy_executor::{Executor, Spawner}; |
| 2 | +use embassy_sync::once_lock::OnceLock; |
| 3 | +use embassy_time::Timer; |
| 4 | +use embedded_services::keyboard::{self, DeviceId, Key, KeyEvent}; |
| 5 | +use embedded_services::{comms, define_static_buffer}; |
| 6 | +use log::info; |
| 7 | +use static_cell::StaticCell; |
| 8 | + |
| 9 | +/// Mock that generates keyboard messages |
| 10 | +mod device { |
| 11 | + use std::borrow::BorrowMut; |
| 12 | + |
| 13 | + use embedded_services::buffer::OwnedRef; |
| 14 | + use embedded_services::keyboard::{self, DeviceId, Event, Key, KeyEvent, MessageData}; |
| 15 | + |
| 16 | + pub struct Device { |
| 17 | + id: DeviceId, |
| 18 | + event_buffer: OwnedRef<'static, KeyEvent>, |
| 19 | + } |
| 20 | + |
| 21 | + impl Device { |
| 22 | + pub fn new(id: DeviceId, event_buffer: OwnedRef<'static, KeyEvent>) -> Self { |
| 23 | + Self { id, event_buffer } |
| 24 | + } |
| 25 | + |
| 26 | + pub async fn key_down(&self, key: Key) { |
| 27 | + { |
| 28 | + let mut borrow = self.event_buffer.borrow_mut(); |
| 29 | + let buf: &mut [KeyEvent] = borrow.borrow_mut(); |
| 30 | + |
| 31 | + buf[0] = KeyEvent::Make(key); |
| 32 | + } |
| 33 | + |
| 34 | + keyboard::broadcast_message( |
| 35 | + self.id, |
| 36 | + MessageData::Event(Event::KeyEvent(self.id, self.event_buffer.reference().slice(0..1))), |
| 37 | + ) |
| 38 | + .await; |
| 39 | + } |
| 40 | + |
| 41 | + pub async fn key_up(&self, key: Key) { |
| 42 | + { |
| 43 | + let mut borrow = self.event_buffer.borrow_mut(); |
| 44 | + let buf: &mut [KeyEvent] = borrow.borrow_mut(); |
| 45 | + |
| 46 | + buf[0] = KeyEvent::Break(key); |
| 47 | + } |
| 48 | + |
| 49 | + keyboard::broadcast_message( |
| 50 | + self.id, |
| 51 | + MessageData::Event(Event::KeyEvent(self.id, self.event_buffer.reference().slice(0..1))), |
| 52 | + ) |
| 53 | + .await; |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// Mock host device |
| 59 | +mod host { |
| 60 | + use std::borrow::Borrow; |
| 61 | + |
| 62 | + use embedded_services::comms::{self, Endpoint, EndpointID, External, MailboxDelegate}; |
| 63 | + use embedded_services::keyboard::{Event, KeyEvent, Message, MessageData}; |
| 64 | + use log::info; |
| 65 | + |
| 66 | + pub struct Host { |
| 67 | + pub tp: Endpoint, |
| 68 | + } |
| 69 | + |
| 70 | + impl Host { |
| 71 | + pub fn new() -> Self { |
| 72 | + Self { |
| 73 | + tp: Endpoint::uninit(EndpointID::External(External::Host)), |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + impl MailboxDelegate for Host { |
| 79 | + fn receive(&self, message: &comms::Message) { |
| 80 | + if let Some(message) = message.data.get::<Message>() { |
| 81 | + match &message.data { |
| 82 | + MessageData::Event(Event::KeyEvent(id, events)) => { |
| 83 | + let borrow = events.borrow(); |
| 84 | + let buf: &[KeyEvent] = borrow.borrow(); |
| 85 | + |
| 86 | + for event in buf { |
| 87 | + info!("Host received event from device {}: {:?}", id.0, event); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +const DEVICE0_ID: DeviceId = DeviceId(0); |
| 97 | + |
| 98 | +#[embassy_executor::task] |
| 99 | +async fn device() { |
| 100 | + define_static_buffer!(buffer, KeyEvent, [KeyEvent::Break(Key(0)); 8]); |
| 101 | + |
| 102 | + info!("Device task"); |
| 103 | + static DEVICE0: OnceLock<device::Device> = OnceLock::new(); |
| 104 | + let this = DEVICE0.get_or_init(|| device::Device::new(DEVICE0_ID, buffer::get_mut().unwrap())); |
| 105 | + info!("Registering device 0 endpoint"); |
| 106 | + |
| 107 | + loop { |
| 108 | + info!("Sending message"); |
| 109 | + this.key_down(Key(0x04)).await; |
| 110 | + Timer::after_millis(250).await; |
| 111 | + this.key_up(Key(0x04)).await; |
| 112 | + Timer::after_secs(1).await; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +#[embassy_executor::task] |
| 117 | +async fn host() { |
| 118 | + info!("Host task"); |
| 119 | + static HOST: OnceLock<host::Host> = OnceLock::new(); |
| 120 | + let this = HOST.get_or_init(|| host::Host::new()); |
| 121 | + info!("Registering host endpoint"); |
| 122 | + comms::register_endpoint(this, &this.tp).await.unwrap(); |
| 123 | +} |
| 124 | + |
| 125 | +#[embassy_executor::task] |
| 126 | +async fn run(spawner: Spawner) { |
| 127 | + embedded_services::init().await; |
| 128 | + |
| 129 | + keyboard::enable_broadcast_host().await; |
| 130 | + |
| 131 | + spawner.must_spawn(host()); |
| 132 | + spawner.must_spawn(device()); |
| 133 | +} |
| 134 | + |
| 135 | +fn main() { |
| 136 | + env_logger::builder().filter_level(log::LevelFilter::Info).init(); |
| 137 | + |
| 138 | + static EXECUTOR: StaticCell<Executor> = StaticCell::new(); |
| 139 | + let executor = EXECUTOR.init(Executor::new()); |
| 140 | + executor.run(|spawner| { |
| 141 | + spawner.must_spawn(run(spawner)); |
| 142 | + }); |
| 143 | +} |
0 commit comments