Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add battery status for G930 #77

Merged
merged 2 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ talking. This differs from a simple loopback via PulseAudio as you won't have an
- Logitech G533
- Sidetone, Battery (for Wireless)
- Logitech G930
- Sidetone
- Sidetone, Battery
- Logitech G633 / G933 / G935
- Sidetone, Battery (for Wireless), LED on/off
- SteelSeries Arctis (7 and Pro)
Expand Down
55 changes: 53 additions & 2 deletions src/devices/logitech_g930.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
#include "../device.h"
#include "logitech.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <hidapi.h>
#include <string.h>

#include <errno.h>

#define BATTERY_MAX 91
#define BATTERY_MIN 44
#define MSG_SIZE 64

static struct device device_g930;

static const uint16_t PRODUCT_ID = 0x0a1f;

static int g930_send_sidetone(hid_device* device_handle, uint8_t num);
static int g930_request_battery(hid_device* device_handle);

void g930_init(struct device** device)
{
Expand All @@ -18,19 +31,57 @@ void g930_init(struct device** device)

strncpy(device_g930.device_name, "Logitech G930", sizeof(device_g930.device_name));

device_g930.capabilities = CAP_SIDETONE;
device_g930.capabilities = CAP_SIDETONE | CAP_BATTERY_STATUS;
device_g930.send_sidetone = &g930_send_sidetone;
device_g930.request_battery = &g930_request_battery;

*device = &device_g930;
}

static int g930_send_sidetone(hid_device* device_handle, uint8_t num)
{
#define MSG_SIZE 64
uint8_t data[MSG_SIZE] = { 0xFF, 0x0A, 0, 0xFF, 0xF4, 0x10, 0x05, 0xDA, 0x8F, 0xF2, 0x01, num, 0, 0, 0, 0 };

for (int i = 16; i < MSG_SIZE; i++)
data[i] = 0;

return hid_send_feature_report(device_handle, data, MSG_SIZE);
}

static int g930_request_battery(hid_device* device_handle)
{
/*
CREDIT GOES TO https://github.com/Roliga for the project
https://github.com/Roliga/g930-battery-percentage/blob/master/main.c
I've simply ported that implementation to this project!
*/

int i;
int res;
tobiaspc marked this conversation as resolved.
Show resolved Hide resolved

unsigned char buf[MSG_SIZE] = { 0xFF, 0x09, 0x00, 0xFD, 0xF4, 0x10, 0x05, 0xB1, 0xBF, 0xA0, 0x04 };
for (i = 11; i < MSG_SIZE; i++)
buf[i] = 0;

res = hid_send_feature_report(device_handle, buf, MSG_SIZE);
if (res < 0) {
fprintf(stderr, "Failed to send feature report: %ls\n", hid_error(device_handle));
return -1;
}

for (i = 0; i < 3; i++) {
res = hid_get_feature_report(device_handle, buf, MSG_SIZE);

if (res < 0) {
fprintf(stderr, "Failed to get feature report %d: %ls\n", i, hid_error(device_handle));
return -1;
tobiaspc marked this conversation as resolved.
Show resolved Hide resolved
}

if (i < 2) {
usleep(100000);
}
}
unsigned int batteryPercent = (buf[13] - BATTERY_MIN) * 100 / (BATTERY_MAX - BATTERY_MIN);

return (int)(batteryPercent);
tobiaspc marked this conversation as resolved.
Show resolved Hide resolved
}