Projects

Peripherals, a keyboard and mouse run from the Mac.

A keyboard with a display nobody had documented, and a mouse to match. This is how we worked out what they understand, from their own firmware and software, and built a small Mac app that speaks it.

The keyboard's display playing its start-up animation: a turning double helix that settles into the deoxy lockup.
256 × 64
pixels on the keyboard’s display, in sixteen levels of grey
90 bytes
one report layout, for nearly every command on both devices
2
first bytes the app will never send to the display
11
tests that pin the wire formats to what the hardware accepted

01

Why.

A Razer BlackWidow V4 Pro 75% keyboard has per-key lighting and a small greyscale display beside its dial. A Basilisk V3 Pro mouse has lighting and a battery. Out of the box, all of it is run by the manufacturer’s own software.

We wanted them to behave like part of the Mac: dark when the Mac’s displays sleep, lit quietly the rest of the time, the battery levels in the menu bar, and whatever is playing in Music on the keyboard’s display. That meant talking to the devices directly, and for the display nobody had published how.

Peripherals is the result: a small menu bar app, and beside it a written record of every message it sends. Both are public at github.com/deoxylabs/peripherals.

02

Two sources.

The usual way to learn a device’s protocol is to watch the official software talk to it and copy what it says. That shows what was said, but not why, or what else could be. We went to where the answers are written down instead.

  • The keyboard’s firmwareThe firmware updater ships the keyboard’s main controller firmware unencrypted. That chip is the USB device, so every command the keyboard accepts is handled somewhere in it. We decompiled it and read the handlers.
  • The official softwareThe device’s support in the manufacturer’s software is delivered as a web bundle. The encoder for the display’s images and the upload sequence are both in it, in JavaScript.

A second chip in the keyboard drives the display, and its firmware is encrypted. It never needed opening: the main controller only passes display traffic through to it, so reading the main controller was enough to know what to send.

Between the two, each finding could be checked twice: what the software sends, against what the firmware does with it.

03

One report for everything.

Nearly everything both devices do is asked for with the same 90-byte report: a short header, a command class and number, up to 80 bytes of arguments, and a checksum over the middle.

Sources/HID/RazerReport.swift
static func make(_ commandClass: UInt8, _ id: UInt8, _ args: [UInt8], size: UInt8? = nil) -> [UInt8] {
    var body = [UInt8](repeating: 0, count: 90)
    body[1] = transaction
    body[5] = size ?? UInt8(min(args.count, 80))
    body[6] = commandClass
    body[7] = id
    for (i, byte) in args.prefix(80).enumerated() {
        body[8 + i] = byte
    }
    body[88] = body[2..<88].reduce(0, ^)     // XOR of everything between
    return body
}
  • One reply at a timeThe device keeps a single reply, and the next write replaces it. A reply is trusted only if its class and number match the question.
  • Share the mouseThe mouse’s control interface is also the pointer. Open it for exclusive use and the cursor freezes, so everything is opened shared.
  • Getters mirror settersFor most settings the command that reads a value is the one that sets it, plus 0x80. Finding one usually finds the other.
  • Ask the deviceThe keyboard reports its own lighting grid: six rows of eighteen keys, two more columns than its nearest documented relative.

04

Lighting.

Lighting is sent a row at a time: a row number, the first and last column, and a colour for each key between. Six rows make a frame.

The devices never acknowledge a frame, and they drop the one they are showing without saying so: after a sleep, or a moment of lost radio for the mouse. The only fix is to assume it has happened, so the app sends the current frame again every few seconds whether it has changed or not.

At rest the keys are a dim, cool white. While DuoChart is playing, they show the song’s album art across the grid and breathe on the beat.

05

The display.

The panel is 256 pixels by 64, in sixteen levels of grey. Each pixel is four bits, two pixels to a byte. The one thing neither source made obvious was which pixel goes in which half of the byte.

So we asked the panel. A test pattern with text, evenly spaced marks and every grey level showed how rows and levels were laid out. Then the same diagonal lines were drawn twice, once in each order, and the keyboard was photographed. One half drew cleanly and the other broke into a comb.

A test pattern of text, evenly spaced lines and a sixteen-step grey ramp.
A test pattern: text, spacing marks and every grey level.
Sources/Oled/Render.swift
/// Four bits a pixel, two to a byte, the LEFT pixel in the LOW nibble.
/// The other way round looks almost right and is horrible.
func packed() -> [UInt8] {
    var out = [UInt8]()
    for y in 0..<height {
        let row = y * width
        for x in stride(from: 0, to: width, by: 2) {
            let left = pixels[row + x] >> 4
            let right = x + 1 < width ? pixels[row + x + 1] >> 4 : 0
            out.append(right << 4 | left)
        }
    }
    return out
}

06

The two bytes it never sends.

Images and animations go to the display over a second interface, in packets of 64 bytes. The first byte of each packet says what it is.

Reading the firmware showed that two values of that first byte are not image commands at all. They switch the keyboard’s main controller into a passthrough to the display chip’s bootloader, the mode used to replace its firmware. A stray packet there could leave the display unable to start.

No amount of watching the official software would have revealed that, because the official software never makes the mistake. The app refuses to build such a packet, and the rule is written at the top of the protocol record.

Sources/HID/OledLink.swift
// 0x30 and 0x31 open a passthrough to the display MCU's bootloader.
precondition(packet[0] != 0x30 && packet[0] != 0x31)

The firmware also showed that every image upload is written to the display’s permanent storage, not its memory. So the app uploads only when a picture really changes, and anything that moves, like the clock or an equaliser, goes through the commands that live in memory.

07

Drawing for sixteen greys.

With the format known, the app could draw its own screens. While Music plays, the keyboard shows the track: the cover on the left over a soft glow of itself, then the title, artist and album. When the music stops it shows the time and date, with live processor and memory figures.

A panel this small and this coarse punishes anything fussy. Album art is reduced to shapes that survive sixteen levels, and the type is set large enough to read at a glance from typing distance. Several layouts were drawn and looked at on the keyboard before one was kept.

Five now-playing layouts for the keyboard display: the current layout, a blurred-art backdrop, a full-panel poster, a fade with an equaliser glyph, and a large type-led title.
Five layouts explored for the screen.

08

Living with the firmware.

The keyboard has opinions of its own. It dims its lighting, blanks its display and starts a screensaver on timers that count keypresses only, so an hour of mouse work looks to it like an hour away. And a full battery reports as not charging, so it runs its battery timers on the cable.

  • Borrowed, then returnedWhile the app runs it switches those timers off, having first read and remembered what each was set to. When it quits, it puts them back.
  • Dim is blackOn this panel a dimmed display is unreadable, so the keys dim after ten minutes without input and the display stays at full brightness.
  • The keyboard draws the clockThe firmware has its own digits. The app feeds it the time every second and lets it draw, which costs no uploads at all.
  • A date it cannot drawThis firmware has no date. The app draws the date into the clock’s background picture and uploads it once a day.

09

Tests that hold the bytes.

A protocol learned this way is only as good as the last time it was checked on the hardware. So the app’s tests pin the wire formats to values that were seen to work on the real devices: the report checksum, the pixel order, the clock’s parameters, the layout of the live values, the colour handling.

If a later change alters a single byte of any of them, the tests fail before the keyboard ever sees it.

10

What it is not.

  • Two devicesIt runs the keyboard and mouse it was written for. The protocol record should help with their relatives, but nothing else has been tested.
  • Wired keyboardThe keyboard is supported over USB. Its wireless dongle is not, yet.
  • Not the official softwareIt does lighting, the display and batteries. Key remapping, macros and firmware updates stay with the manufacturer’s software.
  • OpenThe app and the protocol record are public under the MIT licence at github.com/deoxylabs/peripherals.