Crates.io | mystic_light_sdk |
lib.rs | mystic_light_sdk |
version | 0.4.2 |
source | src |
created_at | 2022-04-24 14:58:00.88397 |
updated_at | 2022-10-01 17:22:01.594666 |
description | A Rust wrapper for the MysticLight SDK (MSI hardware and peripherals) |
homepage | https://github.com/meskill/mystic-light-sdk |
repository | https://github.com/meskill/mystic-light-sdk |
max_upload_size | |
id | 573165 |
size | 88,027 |
Rust SDK wrapper for the Mystic Light SDK
Any MSI device with RGB support
Only Windows 7+
Dragon Center or Msi Center installed and running. You can download it here
Admin rights to run program with the mystic_light_sdk
use mystic_light_sdk::{Color, CommonError, DeviceLedState, MysticLightSDK};
use std::thread;
use std::time::Duration;
use tracing::{info, warn, Level};
use tracing_subscriber::{fmt, fmt::format::FmtSpan};
const LIB_PATH: &str = if cfg!(target_arch = "x86_64") {
"../sdk/MysticLight_SDK_x64.dll"
} else {
"../sdk/MysticLight_SDK.dll"
};
fn main() -> Result<(), CommonError> {
fmt()
.pretty()
.with_max_level(Level::DEBUG)
.with_span_events(FmtSpan::ACTIVE)
.init();
let sdk = MysticLightSDK::new(LIB_PATH)?;
let devices: Vec<_> = sdk.devices_iter().collect();
info!(?devices);
info!(second_device_name = devices[2].name());
let keyboard_leds: Vec<_> = devices[2].leds_iter().collect();
info!(?keyboard_leds);
info!(
"First led has name: {} with max_bright: {} and max_speed: {}",
keyboard_leds[0].name(),
keyboard_leds[0].max_bright(),
keyboard_leds[0].max_speed()
);
let state = keyboard_leds[0].get_state()?;
info!("Current device state: {:#?}", state);
warn!("Disable lightning!");
let new_state = DeviceLedState {
color: Color {
red: 0,
green: 0,
blue: 0,
},
style: String::from("NoAnimation"),
..state
};
keyboard_leds[0].set_state(&new_state)?;
thread::sleep(Duration::from_secs(5));
warn!("Enable lightning");
keyboard_leds[0].set_state(&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 MysticLight_SDK_x64.dll
Or if you are targetting both architecture you may use code below
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 absolute path to directory with the sdk’s dll e.g. MYSTIC_LIGHT_SDK_PATH=/workspaces/project/sdk
.
in case of any problems with conversion from and into WinApi types
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.
Tracing is implemented with library tracing
- to see tracing logs follow the instructions of tracing crate.
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 async_graphql::Query and MysticLightGraphqlMutation as async_graphql::Mutation
use async_graphql::{EmptySubscription, Schema};
use mystic_light_sdk::{build_graphql_schema, MysticLightSDK, MysticLightGraphqlMutation, MysticLightGraphqlQuery};
pub type MysticLightSchema = Schema<MysticLightGraphqlQuery, MysticLightGraphqlMutation, EmptySubscription>;
pub fn create_qraphql_schema(sdk: MysticLightSDK) -> MysticLightSchema {
let (query, mutation) = build_graphql_schema(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.