pyrgbdev - A Python Library for Controlling RGB Devices | Razer Insider

pyrgbdev - A Python Library for Controlling RGB Devices

  • 28 February 2022
  • 1 reply
  • 534 views

Hello guys, it has been a long time since I have posted my PyPeripheral project in Razer forum
(The older links are : this and this). It had been almost 3 years ago that I have posted that thread and finished working on that project.

However, I have came here today with another project named pyrgbdev.
As you might have expected, this is a Python library that controls RGB devices easily.

Personally, I had lots of difficulties with Razer Chroma SDK 3 years ago, and I would like to provide a bit of help to those who are looking into Razer Chroma SDK for their future projects with a Python library.

As some might know, there 'is' a Official python library by official Razer (*omitted link), which they abandoned. Razer Chroma SDK supports REST API however they lack lots of features compared to C++ library.
Thus I built this python library using cython so that it can use C++ library functions in python.

Let's look on a example of setting a mouse color RGB to (255, 255, 0) (Yellow!)

Do you think this code is long?

code:
import requests
import json

url = "http://localhost:54235/razer/chromasdk/"
jsondata = {
"title": 'PyPheperial',
"description": 'A Wrapper for PyPheperial',
"author": {
"name": 'Gooday2die',
"contact": 'github.com/gooday2die/pypheperial'
},
"device_supported": ['keyboard', 'mouse', 'mousepad'],
"category": 'application'
}

response = requests.post(url=url, json=jsondata)
uri = json.loads(response.text)['uri']

data = {
"effect": "CHROMA_STATIC",
"param": {
"color": convert_hex(255, 0, 0)
},
}
response = requests.post(url=uri+"/mouse", data=json.dumps(data))
effect_id = json.loads(response.text)['id']

data = {
"id": str(effect_id)
}
response = requests.put(url=uri + "/effect", data=json.dumps(data))

Yes, it is even longer

code:
#include <iostream>
#include <tchar.h>
#include <windows.h>
#include <assert.h>
#include <wtypes.h>
#include <list>

#include "RzChromaSDKDefines.h"
#include "RzChromaSDKTypes.h"
#include "RzErrors.h"

#ifdef _WIN64
#define CHROMASDKDLL _T("RzChromaSDK64.dll")
#else
#define CHROMASDKDLL _T("RzChromaSDK.dll")
#endif
int main(){
HMODULE m_ChromaSDKModule = NULL;

typedef RZRESULT(*INIT)(void);
typedef RZRESULT(*UNINIT)(void);
typedef RZRESULT(*CREATEMOUSEEFFECT)(ChromaSDK::Mouse::EFFECT_TYPE Effect, PRZPARAM pParam, RZEFFECTID* pEffectId);
typedef RZRESULT(*SETEFFECT)(RZEFFECTID EffectId);

INIT Init = nullptr;
UNINIT UnInit = nullptr;
CREATEEFFECT CreateEffect = nullptr;
CREATEKEYBOARDEFFECT CreateKeyboardEffect = nullptr;
SETEFFECT SetEffect = nullptr;

m_ChromaSDKModule = LoadLibrary(CHROMASDKDLL);
Init = (INIT)GetProcAddress(m_ChromaSDKModule, "Init");
UnInit = (INIT)GetProcAddress(m_ChromaSDKModule, "UnInit");
CreateMouseEffect = reinterpret_cast<CREATEMOUSEEFFECT>(GetProcAddress(m_ChromaSDKModule,
"CreateMouseEffect"));
SetEffect = reinterpret_cast<SETEFFECT>(GetProcAddress(m_ChromaSDKModule, "SetEffect"));
Init(); // perform init

ChromaSDK::Mouse::STATIC_EFFECT_TYPE StaticEffect = {};
RZEFFECTID EffectId;

StaticEffect.Color = RGB(255, 255, 0);
StaticEffect.LEDId = ChromaSDK::Mouse::RZLED_ALL;

CreateMouseEffect(ChromaSDK::Mouse::CHROMA_STATIC, &StaticEffect, &EffectId);
SetEffect(EffectId);

return 0;
}



We are done with this. Also it is toooooo much complicated for each users to make this code every time they would like to make their project. Thus I made it simpler.

code:
from pyrgbdev import Razer
b = Razer.sdk()
b.connect()
b.set_rgb({"Mouse": (255, 255, 0)})


This code just sets Mouse color go Yellow without all that long codes. Currently my library supports Razer and Corsair.

Also I have added demo programs so that you can run scripts in a GUI environment


First, Screen reactive reacts to screen




Second, Rainbow All sets all connected devices go rainbow shifting


Third, static light sets all connected devices to a specific RGB color.
So, if anyone wants to use Razer Chroma SDK with python on their project easily, you can use pyrgbdev. I did the time consuming part, so everyone else who are willing to make a project can just focus on their projects, not on SDKs. Its 100% free and open source.

You can check my project on github (*omitted link). I constantly fixing and adding features to the library, so please stay tuned! Also any issue reporting and testing the library with your devices are always welcomed!

Also, if this thread violates the rules of forum, I would be taking this post down immediately.
Thank you.

This topic has been closed for comments

1 Reply

Userlevel 7
Badge +1
Awesome project! Keep it up 🙂