#![deny(unsafe_code)] #![no_main] #![no_std] use panic_halt as _; //use nb::block; use cortex_m_rt::entry; use cortex_m::asm::delay; use stm32f1xx_hal::{adc, pac, prelude::*, timer::{Channel, Tim2NoRemap}}; use stm32f1xx_hal::timer::Timer; use embedded_hal::digital::v2::InputPin; use stm32f1xx_hal::usb::{Peripheral, UsbBus}; use usb_device::prelude::*; use usbd_human_interface_device::device::consumer::MultipleConsumerReport; use usbd_human_interface_device::prelude::*; use usbd_human_interface_device::page::Consumer; #[entry] fn main() -> ! { // ====================== general setup ================= // Acquire peripherals let cp = cortex_m::Peripherals::take().unwrap(); let p = pac::Peripherals::take().unwrap(); let mut flash = p.FLASH.constrain(); let rcc = p.RCC.constrain(); // Setup GPIOA let mut gpioa = p.GPIOA.split(); // configure clock let clocks = rcc .cfgr .use_hse(16.MHz()) .sysclk(48.MHz()) .pclk1(24.MHz()) .freeze(&mut flash.acr); // ====================== USB setup ================= assert!(clocks.usbclk_valid()); let mut usb_dp = gpioa.pa12.into_push_pull_output(&mut gpioa.crh); usb_dp.set_low(); delay(clocks.sysclk().raw() / 100); let usb = Peripheral { usb: p.USB, pin_dm: gpioa.pa11, pin_dp: usb_dp.into_floating_input(&mut gpioa.crh), }; let usb_bus = UsbBus::new(usb); let mut consumer = UsbHidClassBuilder::new() .add_device(usbd_human_interface_device::device::consumer::ConsumerControlConfig::default()) .build(&usb_bus); let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd)) .manufacturer("FLC Meow") .product("Serial port") .serial_number("01189998819991197253") .build(); // ====================== ADC setup ================= // Setup ADC let mut adc1 = adc::Adc::adc1(p.ADC1, clocks); // ====================== Pin setup ================= let input_pins: [&dyn InputPin; 2] = [ &gpioa.pa1.into_analog(&mut gpioa.crl), &gpioa.pa2.into_analog(&mut gpioa.crl) ]; let mut last = get_report(&input_pins); // ====================== PWM setup ================= let mut afio = p.AFIO.constrain(); let c1 = gpioa.pa0.into_alternate_push_pull(&mut gpioa.crl); let mut pwm = p.TIM2.pwm_hz::(c1, &mut afio.mapr, 1.kHz(), &clocks); pwm.enable(Channel::C1); let pwm_max = pwm.get_max_duty() as u16; //48000 in our case // ====================== Timer setup ================= let timer = Timer::new(pwm, &mut clocks); let mut input_count_down = timer.count_down(); input_count_down.start(50.millis()); loop { //Poll the every 10ms if input_count_down.wait().is_ok() { let report = get_report(&input_pins); if report != last { match consumer.device().write_report(&report) { Err(UsbError::WouldBlock) => {} Ok(_) => { last = report; } Err(e) => { core::panic!("Failed to write consumer report: {:?}", e) } } } } if usb_dev.poll(&mut [&mut consumer]) {} // TODO get value and set PWM //pwm.set_duty(Channel::C1, pwm_duty); // // Get data from pots // let data: u16 = adc1.read(&mut ch0).unwrap(); // let mut pwm_val = data as f32 / 0xfff as f32; // // let mut outBuf = ryu::Buffer::new(); // let mut ret = outBuf.format(pwm_val).as_bytes(); } } fn get_report( pins: &[&dyn InputPin; 2], ) -> MultipleConsumerReport { #[rustfmt::skip] let pins = [ // pins[0].is_low().unwrap() { Consumer::PlayPause } else { Consumer::Unassigned }, // pins[1].is_low().unwrap() { Consumer::ScanPreviousTrack } else { Consumer::Unassigned }, ]; let mut report = MultipleConsumerReport { codes: [Consumer::Unassigned; 4], }; let mut it = pins.iter().filter(|&&c| c != Consumer::Unassigned); for c in report.codes.iter_mut() { if let Some(&code) = it.next() { *c = code; } else { break; } } report }