Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
yesitsfebreeze committed Sep 25, 2024
1 parent cc37cf3 commit b99615f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 48 deletions.
6 changes: 4 additions & 2 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ A full list is available [here](https://github.com/orbit-firmware/orbit/tree/mas

if [gnu make](https://www.gnu.org/software/make/) is installed
```shell
cd orbit
make flash kb=MY_KEYBOARD
cd orbit # [!code focus]
make flash kb=MY_KEYBOARD # [!code focus]
# optionally pass the debug feature if you want to debug via st-link or j-link
make flash kb=MY_KEYBOARD features="debug"
```

or plain script
Expand Down
8 changes: 6 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ else
# @cd build && cargo objcopy --release -- -O ihex ../firmware.hex
endif

flash: #/ flashes the firmware [debug=true/false]
flash: #/ flashes the firmware [features="list of features"]
@make pre_compile -B args="$(kb) $(features)" || { \
echo "pre_compile failed, aborting."; \
exit 1; \
}
ifeq ($(debug),true)
cd build && cargo embed --features debug
ifneq ($(features),)
cd build && cargo embed --features $(features)
else
cd build && cargo embed
endif
else
cd build && cargo embed
endif
Expand Down
43 changes: 29 additions & 14 deletions orbit/src/orbit/hid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use core::option::Option;
use core::sync::atomic::{AtomicBool, Ordering};
use static_cell::StaticCell;

use embassy_time::Timer;
use embassy_usb::{
class::hid::{HidReader, HidReaderWriter, HidWriter, ReportId, RequestHandler, State},
control::{InResponse, OutResponse},
Expand All @@ -16,20 +17,30 @@ static USB_READY: AtomicBool = AtomicBool::new(false);

use crate::orbit::dbg::*;

use super::modifiers::s;

const READ_N: usize = 1;
const WRITE_N: usize = 8;

pub struct Hid<D: Driver<'static>> {
reader: Option<HidReader<'static, D, 1>>,
writer: Option<HidWriter<'static, D, 8>>,
hid: Option<HidReaderWriter<'static, D, READ_N, WRITE_N>>,
}

impl<D: Driver<'static>> Hid<D> {
pub async fn new(driver: D) -> Self {
let mut hid = Hid {
reader: None,
writer: None,
};
let mut hid = Hid { hid: None };

hid.configure(driver).await;

// let mut ready = async {
// while !hid.usb_ready() {
// Timer::after_millis(10).await;
// info!("Waiting for USB to be ready");
// }
// };

// ready.await;

hid
}

Expand Down Expand Up @@ -84,21 +95,25 @@ impl<D: Driver<'static>> Hid<D> {
max_packet_size: 8,
};

#[rustfmt::skip]
let hid = HidReaderWriter::<_, 1, 8>::new(
self.hid = Some(HidReaderWriter::<'static, D, READ_N, WRITE_N>::new(
&mut builder,
STATE.init(State::new()),
config
);
config,
));

let mut usb = builder.build();

let (reader, writer) = hid.split();
self.reader = Some(reader);
self.writer = Some(writer);

usb.run().await;
}

pub fn split(
self,
) -> (
HidReader<'static, D, READ_N>,
HidWriter<'static, D, WRITE_N>,
) {
self.hid.unwrap().split()
}
}

struct KeyboardRequest {}
Expand Down
43 changes: 29 additions & 14 deletions orbit/src/orbit/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ use core::cell::UnsafeCell;
use core::option::Option;
use core::sync::atomic::{AtomicBool, Ordering};
use embassy_usb::driver::Driver;
use usbd_hid::descriptor::{KeyboardReport, SerializedDescriptor};

use crate::orbit::config;
use crate::orbit::dbg::{info, warn};
use crate::orbit::hid::Hid;
use crate::orbit::key::Key;
use crate::orbit::peripherals::*;

static KEYBOARD_INITIIALIZED: AtomicBool = AtomicBool::new(false);
static mut KEYBOARD_INSTANCE: UnsafeCell<Option<Keyboard>> = UnsafeCell::new(None);

use crate::orbit::dbg::{dump, info};

pub struct Keyboard {
peripherals: Peripherals,
layer: u32,
keys: [Key; config::KEY_COUNT],
hid: Option<Hid<Driver<'static>>>,
buffer: [u8; 8],
}

impl Keyboard {
Expand All @@ -41,15 +41,10 @@ impl Keyboard {
peripherals: Peripherals::new(),
keys: populate(Key::new),
layer: 0,
hid: None,
buffer: [0; 8],
}
}

pub async fn create_hid<D: Driver<'static>>(&mut self, driver: D) {
let hid = Hid::new(driver).await;
self.hid = Some(hid);
}

pub fn set_layer(&mut self, layer: u32) {
self.layer = layer;
}
Expand All @@ -58,8 +53,28 @@ impl Keyboard {
self.layer
}

pub async fn process(&mut self) {
self.scan().await;
pub async fn process<D: Driver<'static>>(&mut self, driver: D) {
let mut hid = Hid::new(driver).await;

let proc = async {
let (reader, mut writer) = hid.split();
loop {
self.scan();
let report = KeyboardReport {
keycodes: [4, 0, 0, 0, 0, 0],
leds: 0,
modifier: 0,
reserved: 0,
};
match writer.write_serialize(&report).await {
Ok(()) => {}
Err(e) => warn!("Failed to send report: {:?}", e),
};
info!("Keyboard scanned");
}
};

proc.await;
}

pub fn key(&mut self, index: usize) -> &mut Key {
Expand All @@ -71,17 +86,17 @@ impl Keyboard {
&mut self.peripherals
}

async fn scan(&mut self) {
fn scan(&mut self) {
if config::USE_MATRIX {
self.scan_matrix().await;
self.scan_matrix();
}

if config::USE_MULTIPLEXERS {
// self.scan_multiplexers();
}
}

async fn scan_matrix(&mut self) {
fn scan_matrix(&mut self) {
let keys = &mut self.keys;
let peri = &mut self.peripherals;

Expand Down
17 changes: 1 addition & 16 deletions orbit/src/orbit/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,9 @@ mod stm32 {
use crate::orbit::hid::Hid;
use crate::orbit::peripherals::*;
use embassy_futures::join::join;
use embassy_time::Timer;

pub async fn run<D: Driver<'static>>(driver: D) {
let mut keyboard = Keyboard::instance();
keyboard.create_hid(driver);
keyboard.process().await;
// let process = async {
// loop {
// if !hid.usb_ready() {
// Timer::after_millis(100).await;
// info!("Waiting for USB to be ready");
// continue;
// }

// }
// };

// process.await;
Keyboard::instance().process(driver).await;
}
}

Expand Down

0 comments on commit b99615f

Please sign in to comment.