Rust SDK wrapper for the Mystic Light SDK
mystic_light_sdk
```rust use mysticlightsdk::{Color, CommonError, DeviceLedState, MysticLightSDK}; use std::thread; use std::time::Duration;
const LIBPATH: &str = if cfg!(targetarch = "x8664") { "../sdk/MysticLightSDKx64.dll" } else { "../sdk/MysticLightSDK.dll" };
fn main() -> Result<(), CommonError> { let sdk = MysticLightSDK::new(LIB_PATH)?;
let devices: Vec<_> = sdk.devices_iter().collect();
println!("{:#?}", devices);
println!("Second Device name is {}", devices[2].name());
let keyboardleds: Vec<_> = devices[2].ledsiter().collect();
println!("{:#?}", keyboard_leds);
println!( "First led has name: {} with maxbright: {} and maxspeed: {}", keyboardleds[0].name(), keyboardleds[0].maxbright(), keyboardleds[0].max_speed() );
let state = keyboardleds[0].getstate()?;
println!("Current device state: {:#?}", state);
println!("Disable lightning!");
let new_state = DeviceLedState { color: Color { red: 0, green: 0, blue: 0, }, style: String::from("NoAnimation"), ..state };
keyboardleds[0].setstate(&new_state)?;
thread::sleep(Duration::from_secs(5));
println!("Enable lightning");
keyboardleds[0].setstate(&state)?;
Ok(()) }
```
It depends on the os architecture you are building the program to and the os architecture for the end users.
Currently, most of the PC’s are 64 bit architecture so you may just use MysticLightSDKx64.dll
Or if you are targetting both architecture you may use code below
rust
const LIB_PATH: &str = if cfg!(target_arch = "x86_64") {
"sdk/MysticLight_SDK_x64.dll" // path to the dll file that must be available in runtime
} else {
"sdk/MysticLight_SDK.dll"
};
As sdk dll is required in runtime you must provide these files somehow in the runtime.
You may use build script included in the library itself to copy directory with sdk to the output directory. To do so provide environment variable MYSTIC_LIGHT_SDK_PATH
with the path to directory with the sdk’s dll relative to the crate root e.g. MYSTIC_LIGHT_SDK_PATH=sdk
if you have dlls inside <crate_root>/sdk
directory
Underlying C++ SDK doesn’t support parallel access and trying to use sdk that way will lead to wrong data. To prevent such problems this wrapper wraps underlying library in Arc and Mutex. Arc is used to share the same library instance across wrapper structs. Mutex is used to prevent parallel access to the underlying library.
That all means you can safely use rust wrapper both in single-threaded and multi-threaded environments, but actual sdk calls will be executed in sequence anyway.
Logging is implemented with library log
- to enable actual logging just pick one of the logger implementation from the list and activate log for the module mystic_light
e.g. for env_logger
pass RUST_LOG=mystic_light_sdk
Enables serde serialization/deserialization for some of the sdk structs
Enables async-graphql support for sdk entities
When this feature is enabled you can use MysticLightGraphqlQuery as asyncgraphql::Query and MysticLightGraphqlMutation as asyncgraphql::Mutation
```rust use asyncgraphql::{EmptySubscription, Schema}; use mysticlightsdk::{buildgraphql_schema, MysticLightSDK, MysticLightGraphqlMutation, MysticLightGraphqlQuery};
pub type MysticLightSchema = Schema
pub fn createqraphqlschema(sdk: MysticLightSDK) -> MysticLightSchema { let (query, mutation) = buildgraphqlschema(sdk);
Schema::build(query, mutation, EmptySubscription).finish()
}
```
Make sure you have been fulfilled requirements and you running the result program with the admin rights
Some of the device’s styles do not support colors. In this case this kind of error will be generated.