QMKhost/src/ergodox.hpp

81 lines
1.6 KiB
C++
Raw Normal View History

2022-06-26 12:39:02 -07:00
#pragma once
#include <stdexcept>
#include <cstdint>
#include <wchar.h>
2022-07-08 16:56:32 -07:00
// For sleep
#include <chrono>
#include <thread>
2022-06-26 12:39:02 -07:00
2022-07-08 16:56:32 -07:00
#include "hidapi.h"
2022-07-08 10:41:19 -07:00
#include "config.h"
#include "commands.h"
2022-06-26 12:39:02 -07:00
/*
A singleton Ergodox interface. Wraps all hidapi methods, including
hid_init() and hid_exit().
*/
class Ergodox {
public:
2022-07-08 10:41:19 -07:00
// USB Device paramaters
const unsigned short vendor_id;
const unsigned short product_id;
const unsigned short usage;
const unsigned short usage_page;
const uint8_t packet_size = RAW_EPSIZE;
2022-06-26 12:39:02 -07:00
static Ergodox& init(
unsigned short vendor_id,
unsigned short product_id,
unsigned short usage,
unsigned short usage_page
);
~Ergodox();
2022-07-08 16:33:42 -07:00
bool try_connect();
void disconnect();
void test_connection();
2022-07-08 16:56:32 -07:00
void connect_loop();
2022-06-26 12:39:02 -07:00
2022-07-08 10:41:19 -07:00
bool read();
2022-07-08 16:33:42 -07:00
bool write(uint8_t cmd, const uint8_t* data, uint8_t data_len);
2022-07-08 10:41:19 -07:00
// Read buffer, len = packet_size.
// Filled by read().
uint8_t read_buf[RAW_EPSIZE];
2022-07-08 16:33:42 -07:00
// Getter methods
2022-07-08 10:41:19 -07:00
uint8_t get_animation_mode() const { return animation_mode; }
2022-07-08 16:33:42 -07:00
bool is_connected() const { return connected; }
2022-06-26 12:39:02 -07:00
private:
Ergodox(
unsigned short vendor_id,
unsigned short product_id,
unsigned short usage,
unsigned short usage_page
);
// Disable copy and assignment
//Ergodox(void);
//Ergodox(Ergodox& other);
//Ergodox& operator=(Ergodox& other);
// HID device.
// NULL if not opened.
hid_device* handle;
2022-07-08 10:41:19 -07:00
// Keyboard state variables.
// Updated by read().
2022-07-08 16:33:42 -07:00
// Which animation is the keyboard running right now?
// See CMD_SEND_STATE in commands.h for docs.
2022-07-08 10:41:19 -07:00
uint8_t animation_mode;
2022-07-08 16:33:42 -07:00
// Are we connected to a keyboard right now?
bool connected;
2022-06-26 12:39:02 -07:00
};