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

@@ -3,14 +3,17 @@ use dbus::blocking::BlockingSender;
use dbus::channel::Sender;
use dbus::{blocking::Connection, Message};
use std::error::Error;
use std::sync::{Arc, Mutex};
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use std::{thread, time::Duration};
/// Simplified way to write a effect block
pub struct AuraDbusWriter {
connection: Box<Connection>,
block_time: u64,
stop: Arc<Mutex<bool>>,
stop: Arc<AtomicBool>,
}
impl AuraDbusWriter {
@@ -20,7 +23,7 @@ impl AuraDbusWriter {
Ok(AuraDbusWriter {
connection: Box::new(connection),
block_time: 10,
stop: Arc::new(Mutex::new(false)),
stop: Arc::new(AtomicBool::new(false)),
})
}
@@ -35,9 +38,7 @@ impl AuraDbusWriter {
println!("GOT {:?}", msg);
if let Ok(stop) = msg.read1::<bool>() {
if stop {
if let Ok(mut lock) = stopper.lock() {
*lock = true;
}
stopper.store(true, Ordering::Relaxed);
}
}
true
@@ -75,10 +76,8 @@ impl AuraDbusWriter {
.append1(&group[10].to_vec());
self.connection.send(msg).unwrap();
thread::sleep(Duration::from_millis(self.block_time));
if let Ok(lock) = self.stop.try_lock() {
if *lock {
panic!("Go signal to stop!");
}
if self.stop.load(Ordering::Relaxed) {
panic!("Go signal to stop!");
}
Ok(())
}

View File

@@ -60,7 +60,7 @@ impl BuiltInModeBytes {
BuiltInModeByte::WideZoomy => &self.widezoomy,
_ => return None,
};
return Some(bytes);
Some(bytes)
}
}
impl Default for BuiltInModeBytes {

View File

@@ -5,6 +5,7 @@
/// to the keyboard EC. One row controls one group of keys, these keys are not
/// necessarily all on the same row of the keyboard, with some splitting between
/// two rows.
#[derive(Clone)]
pub struct KeyColourArray([[u8; 64]; 11]);
impl Default for KeyColourArray {
fn default() -> Self {
@@ -43,15 +44,16 @@ impl KeyColourArray {
#[inline]
pub fn set(&mut self, key: Key, r: u8, g: u8, b: u8) {
let (rr, gg, bb) = self.key(key);
*rr = r;
*gg = g;
*bb = b;
if let Some((rr, gg, bb)) = self.key(key) {
*rr = r;
*gg = g;
*bb = b;
}
}
/// Indexes in to `KeyColourArray` at the correct row and column
/// to set a series of three bytes to the chosen R,G,B values
pub fn key(&mut self, key: Key) -> (&mut u8, &mut u8, &mut u8) {
pub fn key(&mut self, key: Key) -> Option<(&mut u8, &mut u8, &mut u8)> {
// Tuples are indexes in to array
let (row, col) = match key {
Key::VolDown => (0, 15),
@@ -161,14 +163,15 @@ impl KeyColourArray {
//
Key::Down => (10, 9),
Key::Right => (10, 12),
Key::None => return None,
};
// LOLOLOLOLOLOLOL! Look it's safe okay
unsafe {
(
Some((
&mut *(&mut self.0[row][col] as *mut u8),
&mut *(&mut self.0[row][col + 1] as *mut u8),
&mut *(&mut self.0[row][col + 2] as *mut u8),
)
))
}
}
@@ -178,7 +181,7 @@ impl KeyColourArray {
}
}
#[derive(Debug, Copy, Clone)]
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Key {
VolUp,
VolDown,
@@ -277,16 +280,17 @@ pub enum Key {
Left,
Right,
RFn,
None,
}
pub trait KeyLayout {
fn get_rows(&self) -> &Vec<Vec<Key>>;
fn get_rows(&self) -> &Vec<[Key; 17]>;
}
pub struct GX502Layout(Vec<Vec<Key>>);
pub struct GX502Layout(Vec<[Key; 17]>);
impl KeyLayout for GX502Layout {
fn get_rows(&self) -> &Vec<Vec<Key>> {
fn get_rows(&self) -> &Vec<[Key; 17]> {
&self.0
}
}
@@ -294,24 +298,45 @@ impl KeyLayout for GX502Layout {
impl Default for GX502Layout {
fn default() -> Self {
GX502Layout(vec![
vec![Key::VolDown, Key::VolUp, Key::MicMute, Key::ROG],
vec![
[
Key::None,
Key::None,
Key::VolDown,
Key::VolUp,
Key::MicMute,
Key::ROG,
Key::None,
Key::None,
Key::None,
Key::None,
Key::None,
Key::None,
Key::None,
Key::None,
Key::None,
Key::None,
Key::None,
],
[
Key::Esc,
Key::None,
Key::F1,
Key::F2,
Key::F3,
Key::F4,
Key::None, // not sure which key to put here
Key::F5,
Key::F6,
Key::F7,
Key::F8,
Key::F9,
Key::F9,
Key::F10,
Key::F11,
Key::F12,
Key::Del,
],
vec![
[
Key::Tilde,
Key::N1,
Key::N2,
@@ -330,7 +355,7 @@ impl Default for GX502Layout {
Key::BkSpc3,
Key::Home,
],
vec![
[
Key::Tab,
Key::Q,
Key::W,
@@ -345,9 +370,11 @@ impl Default for GX502Layout {
Key::LBracket,
Key::RBracket,
Key::BackSlash,
Key::BackSlash,
Key::BackSlash,
Key::PgUp,
],
vec![
[
Key::Caps,
Key::A,
Key::S,
@@ -360,13 +387,14 @@ impl Default for GX502Layout {
Key::L,
Key::SemiColon,
Key::Quote,
//
Key::Quote,
Key::Ret1,
Key::Ret2,
Key::Ret3,
Key::PgDn,
],
vec![
[
Key::LShift,
Key::LShift,
Key::Z,
Key::X,
@@ -378,28 +406,50 @@ impl Default for GX502Layout {
Key::Comma,
Key::Period,
Key::FwdSlash,
Key::FwdSlash,
Key::Rshift1,
Key::Rshift2,
Key::Rshift3,
Key::End,
],
vec![
[
Key::LCtrl,
Key::LFn,
//
Key::Meta,
Key::LAlt,
Key::Space1,
Key::Space2,
Key::Space3,
Key::Space4,
Key::Space4,
Key::RAlt,
Key::PrtSc,
Key::RCtrl,
Key::RCtrl,
Key::Left,
Key::Up,
Key::Right,
Key::RFn,
],
vec![Key::Left, Key::Down, Key::Right],
[
Key::None,
Key::None,
Key::None,
Key::None,
Key::None,
Key::None,
Key::None,
Key::None,
Key::None,
Key::None,
Key::None,
Key::None,
Key::None,
Key::Left,
Key::Down,
Key::Right,
Key::None,
],
])
}
}

View File

@@ -1,6 +1,6 @@
pub static DBUS_NAME: &'static str = "org.rogcore.Daemon";
pub static DBUS_PATH: &'static str = "/org/rogcore/Daemon";
pub static DBUS_IFACE: &'static str = "org.rogcore.Daemon";
pub static DBUS_NAME: &str = "org.rogcore.Daemon";
pub static DBUS_PATH: &str = "/org/rogcore/Daemon";
pub static DBUS_IFACE: &str = "org.rogcore.Daemon";
pub const LED_MSG_LEN: usize = 17;
mod builtins;