summary refs log tree commit diff stats
path: root/src/keymaps/soispha/hid/hid.c
blob: 73b737096a60e9144fd4843b844de45777f867b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "hid.h"
#include "raw_hid.h"
#include <stdint.h>
#include <string.h>

void hid_send(uint32_t hex) {
  uint8_t length = 32;

  uint8_t response[length];
  memset(response, 0, length);

  uint8_t first, second, third, fourth;

  uint8_t n = 1;
  if (*(char *)&n == 1) {
    // Little endian
    first = (uint8_t)hex;
    second = (uint8_t)(hex >> 8);
    third = (uint8_t)(hex >> 16);
    fourth = (uint8_t)(hex >> 24);
  } else {
    // Big endian
    fourth = (uint8_t)hex;
    third = (uint8_t)(hex << 8);
    second = (uint8_t)(hex << 16);
    first = (uint8_t)(hex << 24);
  }

  response[0] = first;
  response[1] = second;
  response[2] = third;
  response[3] = fourth;

  raw_hid_send(response, length);
}

// `data` is a pointer to the buffer containing the received HID report
// `length` is the length of the report - always `RAW_EPSIZE`
void raw_hid_receive(uint8_t *data, uint8_t length) {
  uint8_t response[length];
  memset(response, 0, length);
  response[0] = 'B';

  if (data[0] == 'A') {
    raw_hid_send(response, length);
  }
}