Bouncy ball example

This commit is contained in:
Luke
2020-05-06 12:31:58 +12:00
parent 497ba61d22
commit e09dd345c0
14 changed files with 277 additions and 85 deletions

View File

@@ -35,7 +35,7 @@ impl RogCore {
let mut dev_handle = RogCore::get_device(vendor, product)?;
dev_handle.set_active_configuration(0).unwrap_or(());
let dev_config = dev_handle.device().config_descriptor(0).unwrap();
let dev_config = dev_handle.device().config_descriptor(0)?;
// Interface with outputs
let mut interface = 0;
for iface in dev_config.interfaces() {
@@ -54,7 +54,7 @@ impl RogCore {
}
}
dev_handle.set_auto_detach_kernel_driver(true).unwrap();
dev_handle.set_auto_detach_kernel_driver(true)?;
dev_handle.claim_interface(interface)?;
Ok(RogCore {
@@ -98,12 +98,9 @@ impl RogCore {
let path = RogCore::get_fan_path()?;
let mut file = OpenOptions::new().write(true).open(path)?;
file.write_all(format!("{:?}\n", config.fan_mode).as_bytes())
.map_err(|err| {
error!("Could not write fan mode: {:?}", err);
})
.unwrap();
.unwrap_or_else(|err| error!("Could not write to {}, {:?}", path, err));
self.set_pstate_for_fan_mode(FanLevel::from(config.fan_mode), config)?;
info!("Reloaded last saved settings");
info!("Reloaded fan mode: {:?}", FanLevel::from(config.fan_mode));
Ok(())
}
@@ -122,7 +119,9 @@ impl RogCore {
n = 0;
}
info!("Fan mode stepped to: {:#?}", FanLevel::from(n));
fan_ctrl.write_all(format!("{:?}\n", n).as_bytes())?;
fan_ctrl
.write_all(format!("{:?}\n", config.fan_mode).as_bytes())
.unwrap_or_else(|err| error!("Could not write to {}, {:?}", path, err));
self.set_pstate_for_fan_mode(FanLevel::from(n), config)?;
config.fan_mode = n;
config.write();

View File

@@ -157,18 +157,16 @@ pub async fn start_daemon() -> Result<(), Box<dyn Error>> {
}
}
// Write a colour block
if let Ok(mut effect_lock) = effect.try_lock() {
// Spawn a writer
if let Some(effect) = effect_lock.take() {
if effect.len() == 11 {
let mut config = config.lock().await;
led_writer
.do_command(AuraCommand::WriteEffect(effect), &mut config)
.await
.map_err(|err| warn!("{:?}", err))
.unwrap();
time_mark = Instant::now();
}
let mut effect_lock = effect.lock().await;
if let Some(effect) = effect_lock.take() {
if effect.len() == 11 {
let mut config = config.lock().await;
led_writer
.do_command(AuraCommand::WriteEffect(effect), &mut config)
.await
.map_err(|err| warn!("{:?}", err))
.unwrap();
time_mark = Instant::now();
}
}
}

View File

@@ -52,6 +52,7 @@ impl<'d, C> LedWriter<'d, C>
where
C: rusb::UsbContext,
{
#[inline]
pub fn new(
device_handle: NonNull<DeviceHandle<C>>,
led_endpoint: u8,
@@ -140,11 +141,12 @@ where
}
/// Should only be used if the bytes you are writing are verified correct
#[inline]
async fn write_bytes(&self, message: &[u8]) -> Result<(), AuraError> {
match unsafe { self.handle.as_ref() }.write_interrupt(
self.led_endpoint,
message,
Duration::from_millis(10),
Duration::from_millis(2),
) {
Ok(_) => {}
Err(err) => match err {
@@ -155,6 +157,7 @@ where
Ok(())
}
#[inline]
async fn write_array_of_bytes(&self, messages: &[&[u8]]) -> Result<(), AuraError> {
for message in messages {
self.write_bytes(*message).await?;
@@ -168,6 +171,7 @@ where
/// Write an effect block
///
/// `aura_effect_init` must be called any effect routine, and called only once.
#[inline]
async fn write_effect(&mut self, effect: Vec<Vec<u8>>) -> Result<(), AuraError> {
if self.flip_effect_write {
for row in effect.iter().rev() {
@@ -179,10 +183,12 @@ where
}
}
self.flip_effect_write = !self.flip_effect_write;
let now = std::time::Instant::now();
Ok(())
}
/// Used to set a builtin mode and save the settings for it
#[inline]
async fn set_and_save(&self, bytes: &[u8], config: &mut Config) -> Result<(), AuraError> {
let mode = BuiltInModeByte::from(bytes[3]);
// safety pass-through of possible effect write
@@ -201,6 +207,7 @@ where
}
/// Used to set a builtin mode and save the settings for it
#[inline]
async fn reload_last_builtin(&self, config: &Config) -> Result<(), AuraError> {
let mode_curr = config.current_mode[3];
let mode = config
@@ -220,6 +227,7 @@ where
/// Select next Aura effect
///
/// If the current effect is the last one then the effect selected wraps around to the first.
#[inline]
async fn set_builtin(&self, config: &mut Config, index: usize) -> Result<(), AuraError> {
let mode_next = config
.builtin_modes

View File

@@ -1,3 +1,4 @@
#![deny(unused_must_use)]
/// Configuration loading, saving
mod config;
/// The core module which allows writing to LEDs or polling the