13个不稳定版本 (3个重大更新)
0.4.2 | 2022年10月1日 |
---|---|
0.4.1 | 2022年8月20日 |
0.3.0 | 2022年7月3日 |
0.2.4 | 2022年5月26日 |
0.1.5 | 2022年4月26日 |
#557 in HTTP服务器
每月26次下载
53KB
993 行
mystic_light_sdk

Rust语言的Mystic Light SDK包装器
要求
- 任何支持RGB的MSI设备
- 仅限Windows 7+
- 已安装并运行Dragon Center或Msi Center。您可以在此下载
- 运行程序需要具有
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(())
}
传递正确的dll文件
这取决于您构建程序时的操作系统架构和最终用户的操作系统架构。
目前,大多数PC都是64位架构,所以您可能只需要使用MysticLight_SDK_x64.dll
或者,如果您针对的是两种架构,您可以使用以下代码
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"
};
将dll文件复制到输出目录
由于SDK dll在运行时是必需的,您必须在运行时以某种方式提供这些文件。
您可以使用库本身包含的构建脚本将包含SDK的目录复制到输出目录。要做到这一点,请提供环境变量MYSTIC_LIGHT_SDK_PATH
,其包含SDK dll的绝对路径,例如MYSTIC_LIGHT_SDK_PATH=/workspaces/project/sdk
。
恐慌
- 在从和到WinApi类型转换出现问题的情况下
它是如何工作的
并行
底层C++ SDK不支持并行访问,尝试以这种方式使用SDK会导致数据错误。为了防止此类问题,此包装器使用Arc和Mutex包装底层库。Arc用于在包装器结构体之间共享相同的库实例。Mutex用于防止对底层库的并行访问。
这意味着您可以在单线程和多线程环境中安全地使用Rust包装器,但实际的SDK调用仍然会顺序执行。
用法
跟踪
跟踪是通过库 tracing
实现的 - 要查看跟踪日志,请遵循 tracing crate的说明。
功能
serde
启用了serde对某些SDK结构的序列化/反序列化支持
async-graphql
为SDK实体启用了async-graphql支持
当此功能启用时,您可以使用MysticLightGraphqlQuery作为async_graphql::Query,并使用MysticLightGraphqlMutation作为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()
}
故障排除
初始化时的超时错误
请确保您已满足要求,并且您正在使用管理员权限运行结果程序
尝试设置颜色时出现NotSupported错误
某些设备的样式不支持颜色。在这种情况下,将生成此类错误。
依赖关系
~2–14MB
~176K SLoC