Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support log over USB serial #211

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/use_config/nrf52840_ble/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)
# target = "thumbv8m.main-none-eabihf" # Cortex-M33 (with FPU)

[env]
DEFMT_LOG = "debug"
DEFMT_LOG = "trace"
1 change: 0 additions & 1 deletion rmk-macro/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ pub(crate) fn gen_imports(config: &KeyboardConfig) -> TokenStream2 {

// TODO: remove static var?
quote! {
use defmt::*;
#imports

#keyboard_info_static_var
Expand Down
1 change: 0 additions & 1 deletion rmk-macro/src/split/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ pub(crate) fn parse_split_peripheral_mod(
quote! {
use defmt_rtt as _;
use panic_probe as _;
use defmt::*;

#main_function_sig {
::defmt::info!("RMK start!");
Expand Down
59 changes: 36 additions & 23 deletions rmk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,36 @@ resolver = "2"

[dependencies]
rmk-macro = { version = "=0.4.0", path = "../rmk-macro" }
embedded-hal = { version = "1.0.0", features = ["defmt-03"] }
embedded-hal-async = { version = "1.0.0", features = [
"defmt-03",
], optional = true }
embedded-io-async = { version = "0.6", features = ["defmt-03"] }
embedded-hal = { version = "1.0.0" }
embedded-hal-async = { version = "1.0.0", optional = true }
embedded-io-async = { version = "0.6" }
embedded-storage = "0.3"
embedded-storage-async = "0.4"
embassy-embedded-hal = { version = "0.2" }
embassy-time = { version = "0.3", features = ["defmt"] }
embassy-time = { version = "0.3" }
embassy-usb = { version = "0.3", features = [
"defmt",
"usbd-hid",
"max-interface-count-8",
"max-handler-count-8",
] }
heapless = "0.8.0"
embassy-sync = { version = "0.6", features = ["defmt"] }
embassy-futures = { version = "0.1", features = ["defmt"] }
embassy-executor = { version = "0.6", features = ["defmt"] }
embassy-sync = { version = "0.6" }
embassy-futures = { version = "0.1" }
embassy-executor = { version = "0.6" }

usbd-hid = { version = "0.8.2", features = ["defmt"] }
usbd-hid = { version = "0.8.2" }
ssmarshal = { version = "1.0", default-features = false }
defmt = "0.3"
defmt = { version = "0.3", optional = true }
log = { version = "0.4", optional = true }
static_cell = "2"
num_enum = { version = "0.7", default-features = false }
bitfield-struct = "0.9"
byteorder = { version = "1", default-features = false }
futures = { version = "0.3", default-features = false }
sequential-storage = { version = "3.0.0", features = ["defmt-03"] }
sequential-storage = { version = "3.0.0" }

serde = { version = "1", default-features = false, features = ["derive"] }
postcard = { version = "1", features = ["experimental-derive", "use-defmt"] }
postcard = { version = "1", features = ["experimental-derive"] }

# Optional dependencies
# nRF dependencies
Expand All @@ -59,7 +57,6 @@ nrf-softdevice = { version = "0.1.0", git = "https://github.com/embassy-rs/nrf-s
"ble-sec",
], optional = true }
embassy-nrf = { version = "0.2.0", features = [
"defmt",
"unstable-pac",
"time",
], optional = true }
Expand All @@ -79,10 +76,31 @@ features = ["split"]
cortex-m = { version = "0.7" }

[features]
default = ["col2row"]
default = ["col2row", "defmt"]

## If your PCB diode's direction is col2row, enable this feature. If it's row2col, disable this feature.
col2row = []

## Enable defmt feature
defmt = [
"dep:defmt",
"embedded-hal/defmt-03",
"embedded-hal-async?/defmt-03",
"embedded-io-async/defmt-03",
"embassy-time/defmt",
"embassy-usb/defmt",
"embassy-sync/defmt",
"embassy-futures/defmt",
"embassy-executor/defmt",
"usbd-hid/defmt",
"sequential-storage/defmt-03",
"embassy-nrf?/defmt",
"nrf-softdevice?/defmt",
"postcard/use-defmt",
]

log = ["dep:log"]

## Enable async matrix scan
async_matrix = ["dep:embedded-hal-async"]

Expand Down Expand Up @@ -146,15 +164,10 @@ esp32c6_ble = ["_esp_ble"]
esp32s3_ble = ["_esp_ble"]
_esp_ble = [
"_ble",
"_no_usb", # ESP doesn't have USB support right now
"_no_usb", # ESP doesn't have USB support right now
"ssmarshal/std",
"dep:esp32-nimble",
"dep:esp-idf-svc",
]
_nrf_ble = [
"_ble",
"dep:nrf-softdevice",
"dep:embassy-nrf",
"dep:once_cell",
]
_nrf_ble = ["_ble", "dep:nrf-softdevice", "dep:embassy-nrf", "dep:once_cell"]
_ble = ["_no_external_storage"]
7 changes: 4 additions & 3 deletions rmk/src/action.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::keycode::{KeyCode, ModifierCombination};
use defmt::{error, Format};

/// A KeyAction is the action at a keyboard position, stored in keymap.
/// It can be a single action like triggering a key, or a composite keyboard action like tap/hold
Expand All @@ -13,7 +12,8 @@ use defmt::{error, Format};
/// The `BasicAction` represents only a single key action of keycodes defined in HID spec. The `Action` represents all actions defined in the following `Action` enum, including modifier combination and layer switch.
///
/// The KeyActionType bits varies between different types of a KeyAction, see docs of each enum variant.
#[derive(Format, Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum KeyAction {
/// No action. Serialized as 0x0000.
No,
Expand Down Expand Up @@ -81,7 +81,8 @@ impl KeyAction {

/// A single basic action that a keyboard can execute.
/// An Action can be represented in 12 bits, aka 0x000 ~ 0xFFF
#[derive(Debug, Format, Copy, Clone, PartialEq, Eq)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Action {
/// A normal key stroke, uses for all keycodes defined in `KeyCode` enum, including mouse key, consumer/system control, etc.
///
Expand Down
2 changes: 1 addition & 1 deletion rmk/src/ble/device_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub(crate) struct PnPID {
pub(crate) product_version: u16,
}

#[derive(Debug, Default, defmt::Format)]
#[derive(Debug, Default)]
pub(crate) struct DeviceInformation {
pub(crate) manufacturer_name: Option<&'static str>,
pub(crate) model_number: Option<&'static str>,
Expand Down
1 change: 0 additions & 1 deletion rmk/src/ble/esp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use crate::{
keymap::KeyMap,
};
use core::cell::RefCell;
use defmt::{debug, info, warn};
use embassy_futures::select::{select, select4};
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel};
use embedded_hal::digital::OutputPin;
Expand Down
1 change: 0 additions & 1 deletion rmk/src/ble/esp/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
extern crate alloc;
use alloc::sync::Arc;
use defmt::{debug, error, info, warn};
use embassy_futures::block_on;
use embassy_sync::{blocking_mutex::raw::RawMutex, channel::Receiver};
use embassy_time::Timer;
Expand Down
5 changes: 2 additions & 3 deletions rmk/src/ble/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub mod esp;
#[cfg(feature = "_nrf_ble")]
pub mod nrf;

use defmt::{debug, error};
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Receiver};
use embassy_time::Timer;
#[cfg(any(feature = "nrf52840_ble", feature = "nrf52833_ble"))]
Expand Down Expand Up @@ -48,12 +47,12 @@ pub(crate) async fn ble_communication_task<
match report {
KeyboardReportMessage::KeyboardReport(report) => {
debug!(
"Send keyboard report via BLE: {=[u8]:#X}, modifier: {:b}",
"Send keyboard report via BLE: {:?}, modifier: {:b}",
report.keycodes, report.modifier
);
match ble_keyboard_writer.write_serialize(&report).await {
Ok(()) => {}
Err(e) => error!("Send keyboard report error: {}", e),
Err(e) => error!("Send keyboard report error: {:?}", e),
};
}
KeyboardReportMessage::CompositeReport(report, report_type) => {
Expand Down
11 changes: 6 additions & 5 deletions rmk/src/ble/nrf/battery_service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::config::BleBatteryConfig;
use defmt::{error, info};
use embassy_time::Timer;
use nrf_softdevice::ble::Connection;

Expand Down Expand Up @@ -56,15 +55,17 @@ impl<'a> BatteryService {
Ok(_) => info!("Battery value: {}", val),
Err(e) => match self.battery_level_set(&val) {
Ok(_) => info!("Battery value set: {}", val),
Err(e2) => error!("Battery value notify error: {}, set error: {}", e, e2),
Err(e2) => {
error!("Battery value notify error: {:?}, set error: {:?}", e, e2)
}
},
}
if val < 10 {
// The battery is low, blink the led!
if let Some(ref mut charge_led) = battery_config.charge_led_pin {
charge_led.toggle();
}
Timer::after_millis(200).await;
Timer::after_secs(200).await;
continue;
} else {
// Turn off the led
Expand All @@ -91,7 +92,7 @@ impl<'a> BatteryService {

// TODO: Make battery calculation user customizable
fn get_battery_percent(&self, val: i16, battery_config: &BleBatteryConfig<'a>) -> u8 {
info!("Detected adc value: {=i16}", val);
info!("Detected adc value: {:?}", val);
// Avoid overflow
let val = val as i32;

Expand Down Expand Up @@ -133,7 +134,7 @@ impl BleServer {
Ok(_) => info!("Battery value: {}", val),
Err(e) => match self.bas.battery_level_set(val) {
Ok(_) => info!("Battery value set: {}", val),
Err(e2) => error!("Battery value notify error: {}, set error: {}", e, e2),
Err(e2) => error!("Battery value notify error: {:?}, set error: {:?}", e, e2),
},
}
}
Expand Down
Loading
Loading