Implement sending text and numbers

This commit is contained in:
hodasemi 2023-04-16 19:58:10 +02:00
parent 66baaf38f0
commit 4e0b3845fb
4 changed files with 35 additions and 12 deletions

View file

@ -12,6 +12,8 @@ embassy-usb = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy"
embassy-time = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = ["defmt", "defmt-timestamp-uptime"] } embassy-time = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = ["defmt", "defmt-timestamp-uptime"] }
embassy-futures = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy" } embassy-futures = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy" }
numtoa = "0.2.4"
usbd-hid = "0.6.1" usbd-hid = "0.6.1"
defmt = "0.3" defmt = "0.3"
@ -23,4 +25,4 @@ panic-probe = { version = "0.3", features = ["print-defmt"] }
[features] [features]
serial = [] serial = []
# default = ["serial"] default = ["serial"]

View file

@ -25,7 +25,7 @@ use usbd_hid::descriptor::MouseReport;
use crate::{mouse_hid::mouse_config::MouseConfig, mouse_hid::MouseHID}; use crate::{mouse_hid::mouse_config::MouseConfig, mouse_hid::MouseHID};
#[cfg(feature = "serial")] #[cfg(feature = "serial")]
use crate::usb_serial::Serial; use crate::usb_serial::{Serial, SerialConfig};
use crate::mouse_sensor::{MouseSensor, MouseSensorPins}; use crate::mouse_sensor::{MouseSensor, MouseSensorPins};
@ -88,7 +88,7 @@ where
T: Instance, T: Instance,
CSP: CsPin<T> + 'd, CSP: CsPin<T> + 'd,
{ {
let mut serial = Serial::new(usb); let mut serial = Serial::new(usb, SerialConfig::new());
let usb_future = Serial::run_usb().await; let usb_future = Serial::run_usb().await;
@ -104,7 +104,8 @@ where
// pan: 0, // pan: 0,
// }; // };
serial.send_msg("test").await; serial.send_number(x, 10).await;
serial.send_number(y, 10).await;
} }
} }
}; };

View file

@ -66,7 +66,7 @@ where
T: Instance, T: Instance,
CSP: CsPin<T> + 'd, CSP: CsPin<T> + 'd,
{ {
spi_device: PAW3212DB_TJDT<'d, L, CSP, T>, pub spi_device: PAW3212DB_TJDT<'d, L, CSP, T>,
led: Output<'d, PIN_25>, led: Output<'d, PIN_25>,
} }

View file

@ -1,3 +1,7 @@
mod serial_config;
use numtoa::NumToA;
use core::future::Future; use core::future::Future;
use embassy_rp::usb::Driver; use embassy_rp::usb::Driver;
@ -6,6 +10,8 @@ use embassy_rp::{interrupt, peripherals::USB};
use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
use embassy_usb::{Builder, Config, UsbDevice}; use embassy_usb::{Builder, Config, UsbDevice};
pub use serial_config::SerialConfig;
static mut DEVICE_DESCRIPTOR_BUFFER: [u8; 256] = [0; 256]; static mut DEVICE_DESCRIPTOR_BUFFER: [u8; 256] = [0; 256];
static mut CONFIG_DESCRIPTOR_BUFFER: [u8; 256] = [0; 256]; static mut CONFIG_DESCRIPTOR_BUFFER: [u8; 256] = [0; 256];
static mut BOS_DESCRIPTOR_BUFFER: [u8; 256] = [0; 256]; static mut BOS_DESCRIPTOR_BUFFER: [u8; 256] = [0; 256];
@ -19,16 +25,19 @@ pub struct Serial<'a> {
} }
impl Serial<'static> { impl Serial<'static> {
pub fn new(usb: USB) -> Serial<'static> { pub fn new(usb: USB, serial_config: SerialConfig<'static>) -> Serial<'static> {
// Create the driver, from the HAL. // Create the driver, from the HAL.
let irq = interrupt::take!(USBCTRL_IRQ); let irq = interrupt::take!(USBCTRL_IRQ);
let driver = Driver::new(usb, irq); let driver = Driver::new(usb, irq);
// Create embassy-usb Config // Create embassy-usb Config
let mut config = Config::new(0xc0de, 0xcafe); let mut config = Config::new(
config.manufacturer = Some("Embassy"); serial_config.vendor_id as u16,
config.product = Some("USB-serial example"); serial_config.product_id as u16,
config.serial_number = Some("12345678"); );
config.manufacturer = Some(serial_config.manufacturer);
config.product = Some(serial_config.product);
config.serial_number = Some(serial_config.serial_number);
config.max_power = 100; config.max_power = 100;
config.max_packet_size_0 = 64; config.max_packet_size_0 = 64;
@ -68,7 +77,18 @@ impl Serial<'static> {
unsafe { USB.as_mut().unwrap().run() } unsafe { USB.as_mut().unwrap().run() }
} }
pub async fn send_msg(&mut self, msg: &str) { pub async fn send_msg(&mut self, s: &str) {
self.class.write_packet(msg.as_bytes()).await.unwrap_or(()); self.class.write_packet(s.as_bytes()).await.unwrap_or(());
}
pub async fn send_number<T, I>(&mut self, i: T, base: I)
where
T: NumToA<I>,
{
let mut buf = [0; 256];
let s = i.numtoa_str(base, &mut buf);
self.send_msg(s).await;
} }
} }