-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpacket_helpers.cpp
114 lines (98 loc) · 3.23 KB
/
packet_helpers.cpp
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
106
107
108
109
110
111
112
113
114
#include "packet_helpers.h"
#include <string.h>
output_t outputForNote(const uint8_t ¬e) {
switch (note) {
#define MIDI_MAP(midi_note, rb_out) \
case midi_note: \
return rb_out;
#include "MidiMap.h"
#undef MIDI_MAP
default:
return NO_OUT;
}
}
uint8_t getSequence() {
static uint8_t sequence = 0;
if (sequence == 0x00) {
sequence = 0x01;
}
return sequence++;
}
void fillInputPacketFromControllerData(const uint8_t *data, const uint8_t &ndata,
XBPACKET *packet) {
if (ndata > ADAPTER_OUT_SIZE)
return;
// Refuse to fill non-handled packet
if (!packet->header.handled) {
return;
}
memset(packet->buf.buffer, 0, sizeof(packet->buf.buffer));
packet->header.length = sizeof(DrumInputData);
packet->header.handled = false;
auto cast_data = (const ControllerInputData *)data;
auto drum_input_d = &packet->buf.drum_input;
drum_input_d->command = cast_data->command;
drum_input_d->deviceId = cast_data->deviceId;
drum_input_d->type = cast_data->deviceId;
drum_input_d->sequence = getSequence();
drum_input_d->length = sizeof(DrumInputData) - sizeof(Frame);
drum_input_d->a = cast_data->buttons.a;
drum_input_d->b = cast_data->buttons.b;
drum_input_d->x = cast_data->buttons.x;
drum_input_d->y = cast_data->buttons.y;
drum_input_d->dpadDown = cast_data->buttons.dpadDown;
drum_input_d->dpadUp = cast_data->buttons.dpadUp;
drum_input_d->dpadLeft = cast_data->buttons.dpadLeft;
drum_input_d->dpadRight = cast_data->buttons.dpadRight;
drum_input_d->select = cast_data->buttons.select;
drum_input_d->start = cast_data->buttons.start;
return;
}
bool fillPacket(const uint8_t *input, const uint8_t &length, XBPACKET *packet) {
if (length > ADAPTER_OUT_SIZE)
return false;
// Refuse to fill non-handled packet
if (!packet->header.handled) {
return false;
}
// set size and copy data
memset(packet->buf.buffer, 0, sizeof(packet->buf.buffer));
packet->header.length = length;
packet->header.handled = false;
memcpy(packet->buf.buffer, input, length);
// if (packet->buf.frame.command != CMD_ACKNOWLEDGE)
// packet->buf.frame.sequence = getSequence();
return true;
}
void updateDrumStateWithDrumInput(const output_t &out, const uint8_t &state,
DrumInputData *drum_input) {
switch (out) {
case OUT_KICK:
drum_input->kick = state;
break;
case OUT_PAD_RED:
drum_input->padRed = state;
break;
case OUT_PAD_BLUE:
drum_input->padBlue = state;
break;
case OUT_PAD_GREEN:
drum_input->padGreen = state;
break;
case OUT_PAD_YELLOW:
drum_input->padYellow = state;
break;
case OUT_CYM_YELLOW:
drum_input->cymbalYellow = state;
break;
case OUT_CYM_BLUE:
drum_input->cymbalBlue = state;
break;
case OUT_CYM_GREEN:
drum_input->cymbalGreen = state;
break;
default:
break;
}
return;
}