Many small fixes

- Flip writing order of effect colour blocks every other block write to try
  and even out. Previously the bottom rows were always last to be written.
- Add more examples: ball, comet, pulser.
- Refine the keyboard layout grid for GX502.
- Use tokio to delay main loop, not thread::sleep
- Minor tweaks to error handling. Needs refactor of errors.
This commit is contained in:
Luke Jones
2020-05-22 15:05:49 +12:00
committed by Luke
parent 2381a64b71
commit 0e6f6f3289
9 changed files with 45 additions and 41 deletions

View File

@@ -31,6 +31,7 @@ impl Config {
let mut c = Config::default();
c.current_mode[0] = 0x5d;
c.current_mode[1] = 0xb3;
// Should be okay to unwrap this as is since it is a Default
let toml = toml::to_string(&c).unwrap();
file.write_all(toml.as_bytes())
.expect("Writing default config failed");

View File

@@ -72,8 +72,8 @@ impl RogCore {
vendor: u16,
product: u16,
) -> Result<DeviceHandle<rusb::GlobalContext>, rusb::Error> {
for device in rusb::devices().unwrap().iter() {
let device_desc = device.device_descriptor().unwrap();
for device in rusb::devices()?.iter() {
let device_desc = device.device_descriptor()?;
if device_desc.vendor_id() == vendor && device_desc.product_id() == product {
return device.open();
}

View File

@@ -56,8 +56,7 @@ pub async fn start_daemon() -> Result<(), Box<dyn Error>> {
rogcore
.fan_mode_reload(&mut config)
.await
.map_err(|err| warn!("Fan mode: {}", err))
.unwrap();
.unwrap_or_else(|err| warn!("Fan mode: {}", err));
let mut led_writer = LedWriter::new(
rogcore.get_raw_device_handle(),
laptop.led_endpoint(),
@@ -104,8 +103,7 @@ pub async fn start_daemon() -> Result<(), Box<dyn Error>> {
laptop
.run(&mut rogcore, &config1, bytes, acs)
.await
.map_err(|err| warn!("{:?}", err))
.unwrap();
.unwrap_or_else(|err| warn!("{:?}", err));
}
}
});
@@ -122,8 +120,7 @@ pub async fn start_daemon() -> Result<(), Box<dyn Error>> {
led_writer
.do_command(command, &mut config)
.await
.map_err(|err| warn!("{:?}", err))
.unwrap();
.unwrap_or_else(|err| warn!("{:?}", err));
connection
.send(
@@ -131,7 +128,7 @@ pub async fn start_daemon() -> Result<(), Box<dyn Error>> {
.msg(&DBUS_PATH.into(), &DBUS_IFACE.into())
.append1(true),
)
.unwrap();
.unwrap_or_else(|_| 0);
// Clear any possible queued effect
let mut effect = effect.lock().await;
*effect = None;
@@ -144,8 +141,7 @@ pub async fn start_daemon() -> Result<(), Box<dyn Error>> {
led_writer
.do_command(AuraCommand::WriteBytes(bytes), &mut config)
.await
.map_err(|err| warn!("{:?}", err))
.unwrap();
.unwrap_or_else(|err| warn!("{:?}", err));
// Also cancel any effect client
connection
.send(
@@ -164,8 +160,7 @@ pub async fn start_daemon() -> Result<(), Box<dyn Error>> {
led_writer
.do_command(AuraCommand::WriteEffect(effect), &mut config)
.await
.map_err(|err| warn!("{:?}", err))
.unwrap();
.unwrap_or_else(|err| warn!("{:?}", err));
time_mark = Instant::now();
}
}

View File

@@ -108,30 +108,32 @@ where
AuraCommand::BuiltinNext => {
// TODO: different path for multi-zone (byte 2 controlled, non-zero)
let mode_curr = config.current_mode[3];
let idx = self
.supported_modes
.binary_search(&mode_curr.into())
.unwrap();
let idx_next = if idx < self.supported_modes.len() - 1 {
idx + 1
if let Ok(idx) = self.supported_modes.binary_search(&mode_curr.into()) {
let idx_next = if idx < self.supported_modes.len() - 1 {
idx + 1
} else {
0
};
self.set_builtin(config, idx_next).await?;
} else {
0
};
self.set_builtin(config, idx_next).await?;
warn!("Tried to step to next LED mode while in non-supported mode");
self.set_builtin(config, 0).await?;
}
}
AuraCommand::BuiltinPrev => {
// TODO: different path for multi-zone (byte 2 controlled, non-zero)
let mode_curr = config.current_mode[3];
let idx = self
.supported_modes
.binary_search(&mode_curr.into())
.unwrap();
let idx_next = if idx > 0 {
idx - 1
if let Ok(idx) = self.supported_modes.binary_search(&mode_curr.into()) {
let idx_next = if idx > 0 {
idx - 1
} else {
self.supported_modes.len() - 1
};
self.set_builtin(config, idx_next).await?;
} else {
self.supported_modes.len() - 1
};
self.set_builtin(config, idx_next).await?;
warn!("Tried to step to next LED mode while in non-supported mode");
self.set_builtin(config, 0).await?;
}
}
AuraCommand::WriteBytes(bytes) => self.set_and_save(&bytes, config).await?,
AuraCommand::WriteEffect(effect) => self.write_effect(effect).await?,
@@ -183,7 +185,6 @@ where
}
}
self.flip_effect_write = !self.flip_effect_write;
let now = std::time::Instant::now();
Ok(())
}
@@ -206,14 +207,13 @@ where
Err(AuraError::NotSupported)
}
/// 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
.builtin_modes
.get_field_from(mode_curr)
.unwrap()
.ok_or(AuraError::NotSupported)?
.to_owned();
self.write_bytes(&mode).await?;
// Reload brightness too
@@ -224,15 +224,12 @@ where
Ok(())
}
/// 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
.get_field_from(self.supported_modes[index].into())
.unwrap()
.ok_or(AuraError::NotSupported)?
.to_owned();
println!("{:X?}", &mode_next);
self.set_and_save(&mode_next, config).await?;

View File

@@ -1,3 +1,4 @@
use log::error;
use uhid_virt::{Bus, CreateParams, UHIDDevice};
/// Create a virtual device to emit key-presses
@@ -91,7 +92,8 @@ impl VirtKeys {
]
.to_vec(),
})
.unwrap(),
.map_err(|err| error!("Could not create virtual device: {:?}", err))
.expect("Could not create virtual device"),
}
}