-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·322 lines (259 loc) · 8.87 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <iostream>
#include <libusb-1.0/libusb.h>
#include <map>
#include <vector>
#include <dirent.h>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <thread>
#include <cstring>
#define Byte unsigned char
// will need this for ramps
#define SByte char
#define UInt16 uint16_t
using namespace std;
void InvokeHapticFeedback(char **argv);
void ConfigureLED(char **args);
void ConfigureOLED(char **args);
void delay (unsigned int msecs);
libusb_device_handle* SetupDevice(libusb_context *ctx);
void PlayAnimation();
void ShutdownDevice(libusb_context *ctx);
struct Colour {
Byte R;
Byte G;
Byte B;
Colour(Byte R, Byte G, Byte B) {
this->R = R;
this->G = G;
this->B = B;
}
Colour() = default;
};
Colour ParseColour(char *arg);
libusb_device_handle* deviceHandle;
map<string, Byte> buzzes = {
{"Strong" , 0b000001},
{"Soft" , 0b000010},
{"Sharp" , 0b000100},
{"Ping" , 0b001000},
{"Bump" , 0b000111},
{"Double" , 0b001010},
{"QuickDouble" , 0b011011},
{"QuickDoubleSoft" , 0b100000},
{"QuickTriple" , 0b001100},
{"Buzz" , 0b101111},
{"LongBuzz" , 0b001111},
{"Ring" , 0b010000},
{"LongButLight" , 0b111111},
{"LightBuzz" , 0b110011},
{"Tick" , 0b011000},
{"Pulse" , 0b110101},
{"StrongPulse" , 0b110100},
};
map<string, Byte> leds = {
{ "Logo", 0 },
{ "Wheel", 1 },
};
map<string, Byte> ledModes // numbers directly match the "mode" passed in the message content
{
{ "Trigger", 8 },
{ "Steady", 1 },
{ "Shift", 0 }
};
map<string, Colour> colours = {
{"White" , Colour(0xff,0xff,0xff)},
{"Red" , Colour(0xff,0x00,0x00)},
{"Green" , Colour(0x00,0xff,0x00)},
{"LightGreen", Colour(0x80,0xff,0x80)},
{"Lime" , Colour(0x00,0xff,0x00)},
{"Blue" , Colour(0x00,0x00,0xff)},
{"LightBlue" , Colour(0x80,0x80,0xff)},
{"Yellow" , Colour(0xff,0xff,0x00)},
{"Aqua" , Colour(0x00,0xff,0xff)},
{"Teal" , Colour(0x00,0xff,0xff)},
{"Turquoise" , Colour(0x00,0xff,0xff)},
{"Fuchsia" , Colour(0xff,0x00,0xff)},
{"Pink" , Colour(0xff,0x00,0xff)},
{"Purple" , Colour(0x80,0x00,0x80)},
};
int main(int argc, char *argv[]) {
libusb_context *ctx;
deviceHandle = SetupDevice(ctx);
if (argc < 2){
//print_usage();
return 0;
}
string command = string(argv[1]);
if (command == "Tactile" ||
command == "Haptic" ||
command == "Buzz" ) {
InvokeHapticFeedback(argv);
}
if (command == "Light" ||
command == "Lamp" ||
command == "Colour" ||
command == "Color" ||
command == "LED"){
ConfigureLED(argv);
}
if (command == "Image" ||
command == "Picture" ||
command == "OLED"){
ConfigureOLED(argv);
}
if (command == "Animation"){
PlayAnimation();
}
ShutdownDevice(ctx);
return 0;
}
void ShutdownDevice(libusb_context *ctx) {
if(deviceHandle) {
int attachResult = libusb_attach_kernel_driver(deviceHandle, 0); // if there is one I guess?
if (attachResult!=0)
cout<<"Could not attach kernel driver on interface 0" << endl;
libusb_close(deviceHandle);
}
libusb_exit(ctx);
}
void PlayAnimation() {
vector<string> files{};
DIR *dir;
struct dirent *entry;
if ((dir = opendir(".")) != nullptr) {
while ((entry = readdir(dir)) != nullptr) {
string filename = string(entry->d_name);
if (filename.length() > 5 && filename.substr(filename.length() - 5) == ".bits")
files.insert(files.end(), filename);
}
closedir(dir);
}
sort(files.begin(), files.end());
Byte command = 0x50;
for (const auto &i : files) {
ifstream file;
cout<<"Uploading " + i<<endl;
delay(70);
file.open(i, ios::binary);
file >> std::noskipws;
vector<Byte> data {command,0x00};
data.insert(data.end(),istream_iterator<Byte>(file),istream_iterator<Byte>());
file.close();
libusb_control_transfer(deviceHandle, 0x21 , 9, 0x0300, 0, data.data(), static_cast<uint16_t>(data.size()), 60);
}
}
void delay (unsigned int msecs) {
clock_t clock1 = clock();
clock_t goal = msecs * CLOCKS_PER_SEC / 1000 + clock1; //convert msecs to clock count
while ( goal > clock() ){
};
}
void ConfigureOLED(char **args) {
string i = string(args[2]);
ifstream file;
cout<<"Uploading " + i<<endl;
delay(70);
file.open(i, ios::binary);
file >> std::noskipws;
vector<Byte> data {0x51, 0x00};
data.insert(data.end(),istream_iterator<Byte>(file),istream_iterator<Byte>());
file.close();
uint16_t wValue = 0x0300;
libusb_control_transfer(deviceHandle, 0x21 , 9, wValue, 0, data.data(), data.size(), 60);
}
void ConfigureLED(char **args) {
// args[2] = LED
Byte LED = leds[args[2]];
// args[3] = Mode
Byte Mode = ledModes[args[3]];
Colour Colour1 = ParseColour(args[4]);
Colour Colour2 = Colour(0,0,0);
UInt16 Time = 0;
// if Mode == Trigger
if (Mode == 8) {
// args[4] = Colour 1 (done)
// args[5] = Colour 2
Colour2 = ParseColour(args[5]);
// args[6] = Duration in ms. (We'll round to the nearest 10)
Time = static_cast<UInt16>(strtol(args[6], nullptr, 10) / 10);
}
if (Mode == 1) {
// if Mode == Steady
// args[4] = Colour - which is already done
}
// not even going to try Shift yet
// I'd like to be able to momentarily change the colour, then change back to what it was.
// To support:
// Fading to and from a different colour - or not
// Flashing two colours, e.g. red/blue, with fading into and/or out of the flash and/or the transition between colours
// need to see if we can query current colour
// need to store local data
auto timeLowByte = static_cast<Byte>(Time & 0xff);
auto timeHighByte = static_cast<Byte>((Time >> 8) & 0xff);
vector<Byte> data = {05, 00, LED, Colour1.R, Colour1.G, Colour1.B, Colour2.R, Colour2.G, Colour2.B, timeLowByte, timeHighByte};
uint16_t wValue = 0x0200;
libusb_control_transfer(deviceHandle, 0x21 , 9, wValue, 0, data.data(), static_cast<UInt16>(data.size()), 60);
}
Colour ParseColour(char *arg) {
Colour result = colours[arg];
if (result.R == 0 && result.G == 0 && result.B == 0){
int adj = 0;
if (arg[0] == '#')
adj = 1;
if (strlen(arg) - adj == 6){
char buf[2];
memcpy(buf, arg, 0 + adj);
result.R = static_cast<Byte>(std::strtol(buf, nullptr, 16));
memcpy(buf, arg, 2 + adj);
result.G = static_cast<Byte>(std::strtol(buf, nullptr, 16));
memcpy(buf, arg, 4 + adj);
result.B = static_cast<Byte>(std::strtol(buf, nullptr, 16));
} else if(strlen(arg) - adj == 3){
char buf[1];
memcpy(buf, arg, 0 + adj);
result.R = static_cast<Byte>(std::strtol(buf, nullptr, 16) * 0x11);
memcpy(buf, arg, 1 + adj);
result.G = static_cast<Byte>(std::strtol(buf, nullptr, 16) * 0x11);
memcpy(buf, arg, 2 + adj);
result.B = static_cast<Byte>(std::strtol(buf, nullptr, 16) * 0x11);
}
}
return result;
}
void InvokeHapticFeedback(char **argv) {
string buzzName = string(argv[2]);
Byte buzzType;
if (buzzes[buzzName])
buzzType = buzzes[buzzName];
else
buzzType = static_cast<Byte>(stoi(buzzName));
int wValue = 0x0200;
std::vector<Byte> data = {0x59,0x01, 0x00, buzzType};
libusb_control_transfer(deviceHandle, 0x21 , 9, wValue, 0, data.data(), data.size(), 60);
}
libusb_device_handle* SetupDevice(libusb_context *ctx) {
ctx = nullptr;
int r = libusb_init(&ctx);
libusb_device_handle *deviceHandle;
if (r != 0) {
cout << "Init error " << r << endl;
return deviceHandle;
}
libusb_set_option(ctx, libusb_option::LIBUSB_OPTION_LOG_LEVEL, 3);
int detachResult;
int claimResult;
deviceHandle = libusb_open_device_with_vid_pid(ctx, 0x1038, 0x1700);
if (!deviceHandle) {
cout<<"Could not open device" << endl;
return deviceHandle;
}
detachResult = libusb_detach_kernel_driver(deviceHandle, 0);
if (detachResult!=0)
cout<<"Could not detach kernel driver on interface 0" << endl;
claimResult = libusb_claim_interface(deviceHandle, 0);
if (claimResult!=0)
cout<<"Could not claim interface 0" << endl;
return deviceHandle;
}