updated docs and tests
This commit is contained in:
7 files changed
+110
-6
No files matched your search
@@ -251,6 +251,7 @@ impl Keymap {
|
||||
}
|
||||
|
||||
impl Keymap {
|
||||
/// Returns an array of colors in the placement that the keyboard supports
|
||||
pub fn map(&self) -> [[u8; 3]; 144] {
|
||||
self.map
|
||||
}
|
||||
|
||||
@@ -29,6 +29,10 @@ impl RGBController {
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns a boolean to indicate if a device is connected
|
||||
///
|
||||
/// /// # Errors
|
||||
/// ```HidWrapperError::*```
|
||||
pub fn is_device_connected(&mut self) -> Result<bool, HidWrapperError> {
|
||||
self.hid_wrapper.is_device_connected()
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
use crate::Color;
|
||||
use crate::EFFECTS;
|
||||
use crate::EffectInfo;
|
||||
use crate::Effects;
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
@@ -33,8 +35,26 @@ impl Preset {
|
||||
}
|
||||
}
|
||||
|
||||
/// Validates if the preset is a valid preset that can be applied
|
||||
pub fn is_valid(&self) -> bool {
|
||||
self.brightness <= 16 && self.direction <= 3 && 1 <= self.speed && self.speed <= 16
|
||||
if self.brightness > 16 || self.direction > 3 || 1 > self.speed || self.speed > 16 {
|
||||
return false;
|
||||
}
|
||||
let effect_info: &EffectInfo = &EFFECTS[usize::from(self.effect.to_u8() - 1)];
|
||||
if effect_info.requires_keymap && self.keymap.is_none() {
|
||||
return false;
|
||||
}
|
||||
// These are uneded and cause issues
|
||||
// if effect_info.can_set_full_color && self.full_color{
|
||||
// return false;
|
||||
// }
|
||||
// if effect_info.can_set_direction == DirectionPolicy::None && self.direction != 0{
|
||||
// return false;
|
||||
// }
|
||||
// if effect_info.can_set_color && self.color != Color::default(){
|
||||
// return false;
|
||||
// }
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+50
-2
@@ -23,6 +23,18 @@ const MAGIC_NUMBER_2: u8 = 0b00010000;
|
||||
const VERSION: u8 = 1;
|
||||
|
||||
impl Preset {
|
||||
/// Method to save a preset to a file.
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// let preset = gk1000_controller::Preset::default();
|
||||
/// let mut file = std::fs::File::create("/tmp/file.gk1k").expect("Failed to create");
|
||||
/// preset.save_to_file(&mut file).expect("Failed to Save");
|
||||
/// ```
|
||||
/// Make sure you use a function to create a writable, overridable file.
|
||||
///
|
||||
/// # Errors
|
||||
/// If this function fails a `SavePresetError` will be returned
|
||||
pub fn save_to_file(&self, _file: &mut File) -> Result<(), SavePresetError> {
|
||||
if !self.is_valid() {
|
||||
return Err(SavePresetError::InvalidPreset);
|
||||
@@ -33,7 +45,21 @@ impl Preset {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn _encode_to_buffer(&self) -> Result<Vec<u8>, SavePresetError> {
|
||||
/// Method to save a preset to a buffer.
|
||||
///
|
||||
/// This method is not supposed to be used to save presets to a file
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// let preset = gk1000_controller::Preset::default();
|
||||
/// if preset.is_valid(){
|
||||
/// let buffer = preset._encode_to_buffer().expect("Failed to Encode");
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// # Errors
|
||||
/// If this function fails a `SavePresetError` will be returned
|
||||
pub fn _encode_to_buffer(&self) -> Result<Vec<u8>, SavePresetError> {
|
||||
let effect_info: EffectInfo = EFFECTS[usize::from(self.effect.to_u8() - 1)].clone();
|
||||
let mut buffer: Vec<u8> = vec![MAGIC_NUMBER_1, MAGIC_NUMBER_2, VERSION];
|
||||
|
||||
@@ -59,6 +85,16 @@ impl Preset {
|
||||
Ok(buffer)
|
||||
}
|
||||
|
||||
/// Function to load a preset from a file.
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// let mut file = std::fs::File::open("/tmp/file.gk1k").expect("Failed to open");
|
||||
/// let preset = gk1000_controller::Preset::load_from_file(&mut file).expect("Failed to load");
|
||||
/// ```
|
||||
///
|
||||
/// # Errors
|
||||
/// If this function fails a `LoadPresetError` will be returned
|
||||
pub fn load_from_file(_file: &mut File) -> Result<Self, LoadPresetError> {
|
||||
let mut buffer: Vec<u8> = vec![];
|
||||
match _file.read_to_end(&mut buffer) {
|
||||
@@ -71,7 +107,19 @@ impl Preset {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn _decode_from_buffer(buffer: &mut VecDeque<u8>) -> Result<Self, LoadPresetError> {
|
||||
/// Function to load a preset from a buffer.
|
||||
///
|
||||
/// This function is not supposed to be used to load presets from a file
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// let mut buffer: std::collections::VecDeque<u8> = gk1000_controller::Preset::default()._encode_to_buffer().expect("Failed to Save").into();
|
||||
/// let preset = gk1000_controller::Preset::_decode_from_buffer(&mut buffer).expect("Failed to decode");
|
||||
/// ```
|
||||
///
|
||||
/// # Errors
|
||||
/// If this function fails a `LoadPresetError` will be returned
|
||||
pub fn _decode_from_buffer(buffer: &mut VecDeque<u8>) -> Result<Self, LoadPresetError> {
|
||||
let mut save: Preset = Default::default();
|
||||
|
||||
if buffer.pop_front() != Some(MAGIC_NUMBER_1) || buffer.pop_front() != Some(MAGIC_NUMBER_2)
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
#[cfg(feature = "preset_file_saving")]
|
||||
mod preset_saving;
|
||||
|
||||
mod keymap;
|
||||
@@ -0,0 +1,30 @@
|
||||
mod tests {
|
||||
use crate::Keymap;
|
||||
|
||||
#[test]
|
||||
fn test_set_all_keys() {
|
||||
let mut keymap = Keymap::default();
|
||||
keymap.set_all_keys(crate::Color {
|
||||
red: 255,
|
||||
green: 35,
|
||||
blue: 45,
|
||||
});
|
||||
assert_eq!(keymap.map(), [[255, 35, 45]; 144])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_key() {
|
||||
let mut keymap = Keymap::default();
|
||||
keymap.set_key(
|
||||
crate::KeymapKeys::Tab,
|
||||
crate::Color {
|
||||
red: 89,
|
||||
green: 49,
|
||||
blue: 67,
|
||||
},
|
||||
);
|
||||
let mut array = [[0, 0, 0]; 144];
|
||||
array[crate::KeymapKeys::Tab.to_usize()] = [89, 49, 67];
|
||||
assert_eq!(keymap.map(), array)
|
||||
}
|
||||
}
|
||||
@@ -108,8 +108,8 @@ mod tests {
|
||||
#[cfg(target_os = "linux")]
|
||||
#[test]
|
||||
fn test_save_load() {
|
||||
use std::fs::{File};
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::{env, fs};
|
||||
|
||||
let preset = Preset::new(
|
||||
crate::Effects::Static,
|
||||
@@ -231,6 +231,5 @@ mod tests {
|
||||
|
||||
#[allow(unused_must_use)]
|
||||
fs::remove_file("/tmp/test_file.gk1k");
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user