Skip to content

meskill/mystic-light-sdk

Repository files navigation

mystic_light_sdk License: Apache-2.0 mystic_light_sdk on crates.io mystic_light_sdk on docs.rs Source Code Repository mystic_light_sdk on deps.rs

Rust SDK wrapper for the Mystic Light SDK

Requirements

  1. Any MSI device with RGB support
  2. Only Windows 7+
  3. Dragon Center or Msi Center installed and running. You can download it here
  4. Admin rights to run program with the mystic_light_sdk

Examples

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(())
}

Pass right dll file

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"
};

Copy dll files to the output dir

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.

Panics

  • in case of any problems with conversion from and into WinApi types

How does it work

Parallelism

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.

Usage

tracing

Tracing is implemented with library tracing - to see tracing logs follow the instructions of tracing crate.

Features

serde

Enables serde serialization/deserialization for some of the sdk structs

async-graphql

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()
}

Troubleshooting

Timeout error on initialization

Make sure you have been fulfilled requirements and you running the result program with the admin rights

NotSupported error when trying to set color

Some of the device’s styles do not support colors. In this case this kind of error will be generated.