Working LED control!
Signed-off-by: fly <merspieler@alwaysdata.com>
This commit is contained in:
parent
0bdb9ab4ec
commit
43bccaf380
1 changed files with 98 additions and 99 deletions
|
@ -11,11 +11,11 @@ use panic_halt as _;
|
||||||
use cortex_m::asm::delay;
|
use cortex_m::asm::delay;
|
||||||
use cortex_m_rt::entry;
|
use cortex_m_rt::entry;
|
||||||
use stm32f1xx_hal::{
|
use stm32f1xx_hal::{
|
||||||
adc,
|
adc,
|
||||||
gpio::{Analog, Pin},
|
gpio::{Analog, Pin},
|
||||||
pac,
|
pac,
|
||||||
prelude::*,
|
prelude::*,
|
||||||
timer::{Channel, Tim2NoRemap},
|
timer::{Channel, Tim2NoRemap},
|
||||||
};
|
};
|
||||||
|
|
||||||
use stm32f1xx_hal::usb::{Peripheral, UsbBus};
|
use stm32f1xx_hal::usb::{Peripheral, UsbBus};
|
||||||
|
@ -25,120 +25,119 @@ use usbd_human_interface_device::prelude::*;
|
||||||
use crate::device::{CustomConfig, CustomInputReport, CustomOutputReport};
|
use crate::device::{CustomConfig, CustomInputReport, CustomOutputReport};
|
||||||
|
|
||||||
struct MyPins {
|
struct MyPins {
|
||||||
pa1: Pin<'A', 1, Analog>,
|
pa1: Pin<'A', 1, Analog>,
|
||||||
pa2: Pin<'A', 2, Analog>,
|
pa2: Pin<'A', 2, Analog>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[entry]
|
#[entry]
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
// ====================== general setup =================
|
// ====================== general setup =================
|
||||||
// Acquire peripherals
|
// Acquire peripherals
|
||||||
// let cp = cortex_m::Peripherals::take().unwrap();
|
// let cp = cortex_m::Peripherals::take().unwrap();
|
||||||
let p = pac::Peripherals::take().unwrap();
|
let p = pac::Peripherals::take().unwrap();
|
||||||
let mut flash = p.FLASH.constrain();
|
let mut flash = p.FLASH.constrain();
|
||||||
let rcc = p.RCC.constrain();
|
let rcc = p.RCC.constrain();
|
||||||
|
|
||||||
// Setup GPIOA
|
// Setup GPIOA
|
||||||
let mut gpioa = p.GPIOA.split();
|
let mut gpioa = p.GPIOA.split();
|
||||||
|
|
||||||
// configure clock
|
// configure clock
|
||||||
let clocks = rcc
|
let clocks = rcc
|
||||||
.cfgr
|
.cfgr
|
||||||
.use_hse(16.MHz())
|
.use_hse(16.MHz())
|
||||||
.sysclk(48.MHz())
|
.sysclk(48.MHz())
|
||||||
.pclk1(24.MHz())
|
.pclk1(24.MHz())
|
||||||
.freeze(&mut flash.acr);
|
.freeze(&mut flash.acr);
|
||||||
|
|
||||||
// ====================== USB setup =================
|
// ====================== USB setup =================
|
||||||
assert!(clocks.usbclk_valid());
|
assert!(clocks.usbclk_valid());
|
||||||
let mut usb_dp = gpioa.pa12.into_push_pull_output(&mut gpioa.crh);
|
let mut usb_dp = gpioa.pa12.into_push_pull_output(&mut gpioa.crh);
|
||||||
usb_dp.set_low();
|
usb_dp.set_low();
|
||||||
delay(clocks.sysclk().raw() / 100);
|
delay(clocks.sysclk().raw() / 100);
|
||||||
|
|
||||||
let usb = Peripheral {
|
let usb = Peripheral {
|
||||||
usb: p.USB,
|
usb: p.USB,
|
||||||
pin_dm: gpioa.pa11,
|
pin_dm: gpioa.pa11,
|
||||||
pin_dp: usb_dp.into_floating_input(&mut gpioa.crh),
|
pin_dp: usb_dp.into_floating_input(&mut gpioa.crh),
|
||||||
};
|
};
|
||||||
let usb_bus = UsbBus::new(usb);
|
let usb_bus = UsbBus::new(usb);
|
||||||
|
|
||||||
let mut consumer = UsbHidClassBuilder::new()
|
let mut consumer = UsbHidClassBuilder::new()
|
||||||
.add_device(CustomConfig::default())
|
.add_device(CustomConfig::default())
|
||||||
.build(&usb_bus);
|
.build(&usb_bus);
|
||||||
|
|
||||||
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
|
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
|
||||||
.manufacturer("FLC Meow")
|
.manufacturer("FLC Meow")
|
||||||
.product("Pedestal box")
|
.product("Pedestal box")
|
||||||
.serial_number("01189998819991197253")
|
.serial_number("01189998819991197253")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// ====================== ADC setup =================
|
// ====================== ADC setup =================
|
||||||
// Setup ADC
|
// Setup ADC
|
||||||
let mut adc1 = adc::Adc::adc1(p.ADC1, clocks);
|
let mut adc1 = adc::Adc::adc1(p.ADC1, clocks);
|
||||||
|
|
||||||
// ====================== Pin setup =================
|
// ====================== Pin setup =================
|
||||||
let mut input_pins = MyPins {
|
let mut input_pins = MyPins {
|
||||||
pa1: gpioa.pa1.into_analog(&mut gpioa.crl),
|
pa1: gpioa.pa1.into_analog(&mut gpioa.crl),
|
||||||
pa2: gpioa.pa2.into_analog(&mut gpioa.crl),
|
pa2: gpioa.pa2.into_analog(&mut gpioa.crl),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut last = get_report(&mut input_pins, &mut adc1);
|
let mut last = get_report(&mut input_pins, &mut adc1);
|
||||||
let mut last_output = CustomOutputReport { int: 0 };
|
let mut last_output = CustomOutputReport { int: 0 };
|
||||||
|
|
||||||
// ====================== PWM setup =================
|
// ====================== PWM setup =================
|
||||||
let mut afio = p.AFIO.constrain();
|
let mut afio = p.AFIO.constrain();
|
||||||
let c1 = gpioa.pa0.into_alternate_push_pull(&mut gpioa.crl);
|
let c1 = gpioa.pa0.into_alternate_push_pull(&mut gpioa.crl);
|
||||||
let mut pwm = p
|
let mut pwm = p
|
||||||
.TIM2
|
.TIM2
|
||||||
.pwm_hz::<Tim2NoRemap, _, _>(c1, &mut afio.mapr, 1.kHz(), &clocks);
|
.pwm_hz::<Tim2NoRemap, _, _>(c1, &mut afio.mapr, 1.kHz(), &clocks);
|
||||||
pwm.enable(Channel::C1);
|
pwm.enable(Channel::C1);
|
||||||
let pwm_max = pwm.get_max_duty() as u16; //48000 in our case
|
let pwm_max = pwm.get_max_duty() as u16; //48000 in our case
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let report = get_report(&mut input_pins, &mut adc1);
|
let report = get_report(&mut input_pins, &mut adc1);
|
||||||
if report != last {
|
if report != last {
|
||||||
match consumer.device().write_report(&report) {
|
match consumer.device().write_report(&report) {
|
||||||
Err(UsbHidError::WouldBlock) => {}
|
Err(UsbHidError::WouldBlock) => {}
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
last = report;
|
last = report;
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
core::panic!("Failed to write consumer report: {:?}", e)
|
core::panic!("Failed to write consumer report: {:?}", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if usb_dev.poll(&mut [&mut consumer]) {
|
if usb_dev.poll(&mut [&mut consumer]) {
|
||||||
match consumer.device().read_report() {
|
match consumer.device().read_report() {
|
||||||
Err(UsbHidError::WouldBlock) => {}
|
Err(UsbHidError::WouldBlock) => {}
|
||||||
Ok(output) => {
|
Ok(output) => {
|
||||||
last_output = output;
|
let pwm_val = output.int as u16 * 188;
|
||||||
}
|
pwm.set_duty(Channel::C1, pwm_val);
|
||||||
Err(e) => {
|
}
|
||||||
core::panic!("Failed to write consumer report: {:?}", e)
|
Err(e) => {
|
||||||
}
|
core::panic!("Failed to write consumer report: {:?}", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Get data from pots
|
// Get data from pots
|
||||||
let data: u16 = adc1.read(&mut input_pins.pa2).unwrap();
|
// let data: u16 = adc1.read(&mut input_pins.pa2).unwrap();
|
||||||
let mut pwm_val = data as f32 / 0xfff as f32;
|
// let mut pwm_val = data as f32 / 0xfff as f32;
|
||||||
let pwm_duty = (pwm_max as f32 * pwm_val as f32) as u16;
|
// let pwm_duty = (pwm_max as f32 * pwm_val as f32) as u16;
|
||||||
pwm.set_duty(Channel::C1, pwm_duty);
|
// pwm.set_duty(Channel::C1, pwm_duty);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn get_report(pins: &mut MyPins, adc1: &mut adc::Adc<pac::ADC1>) -> CustomInputReport {
|
fn get_report(pins: &mut MyPins, adc1: &mut adc::Adc<pac::ADC1>) -> CustomInputReport {
|
||||||
let integLT: u16 = adc1.read(&mut pins.pa1).unwrap();
|
let integLT: u16 = adc1.read(&mut pins.pa1).unwrap();
|
||||||
let floodLT: u16 = adc1.read(&mut pins.pa2).unwrap();
|
let floodLT: u16 = adc1.read(&mut pins.pa2).unwrap();
|
||||||
let retX = integLT * 16;
|
let buttons: u16 = 0;
|
||||||
let retY = floodLT * 16;
|
|
||||||
let buttons: u16 = 0;
|
|
||||||
|
|
||||||
|
|
||||||
CustomInputReport {
|
CustomInputReport {
|
||||||
x: integLT.into(),
|
x: integLT.into(),
|
||||||
y: floodLT.into(),
|
y: floodLT.into(),
|
||||||
buttons,
|
buttons,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue