Skip to content

Commit 388090a

Browse files
committed
Added host_cmd queue
1 parent 8632e38 commit 388090a

File tree

2 files changed

+67
-17
lines changed

2 files changed

+67
-17
lines changed

macro_hid/macro_hid.c

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,60 @@
88
* using the HID protocol.
99
* @see macro_hid.h
1010
*/
11-
1211
#include "macro_hid.h"
13-
#include "../macro_uart/macro_uart.h"
14-
#include "macro_custom_report.h"
15-
#include "usb_descriptors.h"
1612

17-
int tx_ready = 0; /**< USB ready flag: non-zero if a new HID report can be sent (previous transfer completed). */
13+
uint8_t b_tx_ready_flag = 0; /**< USB ready flag: non-zero if a new HID report can be sent (previous transfer completed). */
14+
uint8_t programming_cmd_flag = 0; /**< Programming command flag: a new programming command has been received from the host.*/
15+
16+
hid_report_queue_t hid_report_queue = {0};
17+
host_cmd_queue_t host_cmd_queue = {0};
18+
19+
bool is_hid_report_queue_full()
20+
{
21+
return count == HID_QUEUE_SIZE;
22+
}
23+
24+
bool is_hid_report_queue_empty()
25+
{
26+
return count == 0;
27+
}
28+
29+
bool enqueue_hid_report(hid_macro_report_t *report)
30+
{
31+
if (is_hid_report_queue_full())
32+
{
33+
return false;
34+
}
35+
hid_queue[head] = *report;
36+
head = (head + 1) % HID_QUEUE_SIZE; // Circular buffer
37+
count++;
38+
return true;
39+
}
40+
41+
bool dequeue_hid_report(hid_macro_report_t *out_report)
42+
{
43+
if (is_hid_report_queue_empty())
44+
return false;
1845

19-
hid_macro_report_t hid_queue[HID_QUEUE_SIZE];
20-
int head = 0; // Where to insert next
21-
int tail = 0; // Where to remove from
22-
int count = 0; // Number of items in queueAZSXWasx
46+
*out_report = hid_queue[tail];
47+
tail = (tail + 1) % HID_QUEUE_SIZE;
48+
count--;
49+
return true;
50+
}
2351

24-
bool is_queue_full()
52+
bool is_host_cmd_queue_full()
2553
{
2654
return count == HID_QUEUE_SIZE;
2755
}
2856

29-
bool is_queue_empty()
57+
bool is_hostt_queue_empty()
3058
{
3159
return count == 0;
3260
}
3361

3462
bool enqueue_hid_report(hid_macro_report_t *report)
3563
{
36-
if (is_queue_full())
64+
if (is_hid_report_queue_full())
3765
{
3866
return false;
3967
}
@@ -45,7 +73,7 @@ bool enqueue_hid_report(hid_macro_report_t *report)
4573

4674
bool dequeue_hid_report(hid_macro_report_t *out_report)
4775
{
48-
if (is_queue_empty())
76+
if (is_hid_report_queue_empty())
4977
return false;
5078

5179
*out_report = hid_queue[tail];
@@ -99,9 +127,9 @@ void hid_task(void)
99127
return;
100128

101129
hid_macro_report_t report;
102-
if (tx_ready && dequeue_hid_report(&report))
130+
if (usb_tx_ready_flag && dequeue_hid_report(&report))
103131
{
104-
tx_ready = 0;
132+
usb_tx_ready_flag = 0;
105133
tud_hid_keyboard_report(REPORT_ID_KEYBOARD, report.modifier, report.keycode);
106134
}
107135
}
@@ -112,7 +140,7 @@ void tud_hid_report_complete_cb(uint8_t instance, uint8_t const *report, uint16_
112140
(void)instance;
113141
(void)len;
114142

115-
tx_ready = 1;
143+
usb_tx_ready_flag = 1;
116144
}
117145

118146
uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t *buffer, uint16_t reqlen)

macro_hid/macro_hid.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,30 @@
2222
#include "usb_descriptors.h"
2323
#include "tusb_config.h"
2424

25+
#include "macro_hid.h"
26+
#include "../macro_uart/macro_uart.h"
27+
#include "macro_custom_report.h"
28+
#include "usb_descriptors.h"
29+
2530
// TODO Dynamic allocation with linked list?
26-
#define HID_QUEUE_SIZE 1024 /**< Maximum number of HID reports that can be queued. */
31+
#define HID_QUEUE_SIZE 1024 /**< Maximum number of HID reports that can be queued. */
32+
#define HOST_CMD_QUEUE_SIZE 256 /**< Host command queue buffer size. */
33+
34+
typedef struct
35+
{
36+
hid_macro_report_t queue[HID_QUEUE_SIZE];
37+
int16_t head;
38+
int16_t tail;
39+
int16_t count;
40+
} hid_report_queue_t;
41+
42+
typedef struct
43+
{
44+
host queue[HID_QUEUE_SIZE];
45+
int16_t head;
46+
int16_t tail;
47+
int16_t count;
48+
} host_cmd_queue_t;
2749

2850
/**
2951
* @brief HID keyboard report structure (Report ID and reserved byte removed).

0 commit comments

Comments
 (0)