summaryrefslogtreecommitdiff
path: root/src/endpoints.c
blob: f46d42dffd47fad9daf987d8cfe10733d96858a5 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

#include <zmk/endpoints.h>
#include <zmk/hid.h>
#include <zmk/usb_hid.h>
#include <zmk/hog.h>

#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

int zmk_endpoints_init()
{
    int err;

    LOG_DBG("");

#ifdef CONFIG_ZMK_USB
    err = zmk_usb_hid_init();
    if (err)
    {
        LOG_ERR("USB HID Init Failed\n");
        return err;
    }
#endif /* CONFIG_ZMK_USB */

#ifdef CONFIG_ZMK_BLE
    err = zmk_hog_init();
    if (err)
    {
        LOG_ERR("HOG Init Failed\n");
        return err;
    }

#endif /* CONFIG_ZMK_BLE */

    return 0;
}

int zmk_endpoints_send_report(enum zmk_hid_report_changes report_type)
{
    int err;
    struct zmk_hid_keypad_report *keypad_report;
    struct zmk_hid_consumer_report *consumer_report;
    switch (report_type)
    {
    case Keypad:
        keypad_report = zmk_hid_get_keypad_report();
#ifdef CONFIG_ZMK_USB
        if (zmk_usb_hid_send_report((u8_t *)keypad_report, sizeof(struct zmk_hid_keypad_report)) != 0)
        {
            LOG_DBG("USB Send Failed");
        }
#endif /* CONFIG_ZMK_USB */

#ifdef CONFIG_ZMK_BLE
        err = zmk_hog_send_keypad_report(&keypad_report->body);
        if (err)
        {
            LOG_ERR("FAILED TO SEND OVER HOG: %d", err);
        }
#endif /* CONFIG_ZMK_BLE */

        break;
    case Consumer:
        consumer_report = zmk_hid_get_consumer_report();
#ifdef CONFIG_ZMK_USB
        if (zmk_usb_hid_send_report((u8_t *)consumer_report, sizeof(struct zmk_hid_consumer_report)) != 0)
        {
            LOG_DBG("USB Send Failed");
        }
#endif /* CONFIG_ZMK_USB */

#ifdef CONFIG_ZMK_BLE
        err = zmk_hog_send_consumer_report(&consumer_report->body);
        if (err)
        {
            LOG_ERR("FAILED TO SEND OVER HOG: %d", err);
        }
#endif /* CONFIG_ZMK_BLE */

        break;
    default:
        LOG_ERR("Unknown report change type %d", report_type);
        return -EINVAL;
    }

    return 0;
}

int zmk_endpoints_send_key_event(struct zmk_key_event key_event)
{
    enum zmk_hid_report_changes changes;

    LOG_DBG("key %d, state %d\n", key_event.key, key_event.pressed);

    if (key_event.pressed)
    {
        changes = zmk_hid_press_key(key_event.key);
    }
    else
    {
        changes = zmk_hid_release_key(key_event.key);
    }

    return zmk_endpoints_send_report(changes);
}