forked from vangdfang/libcutter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
LibCutterPlugin.cpp
621 lines (521 loc) · 14 KB
/
LibCutterPlugin.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#ifdef WIN32
#include "stdafx.h"
#include <setupapi.h>
#include <Cfgmgr32.h>
#include <tchar.h>
#else
#include <dlfcn.h>
#include <CoreFoundation/CoreFoundation.h>
#endif
#include <iostream>
#include "LibCutterPlugin.h"
LIBCUTTERPLUGIN_API void *SCP_AllocatePlugin(char *dllFolderPath)
{
LibCutter *ptr = new LibCutter;
ptr->sDllFolderPath = dllFolderPath;
return ptr;
}
LIBCUTTERPLUGIN_API void SCP_DeallocatePlugin(void *pluginPtr)
{
if (pluginPtr) {
delete (LibCutter*)pluginPtr;
}
}
LIBCUTTERPLUGIN_API long SCP_GetCapabilities(void *pluginPtr)
{
return ((LibCutter*)pluginPtr)->GetCapabilities();
}
LIBCUTTERPLUGIN_API bool SCP_FindDevice(void *pluginPtr)
{
return ((LibCutter*)pluginPtr)->FindDevice();
}
LIBCUTTERPLUGIN_API bool SCP_OpenDevice(void *pluginPtr)
{
return ((LibCutter*)pluginPtr)->OpenDevice();
}
LIBCUTTERPLUGIN_API bool SCP_CloseDevice(void *pluginPtr)
{
return ((LibCutter*)pluginPtr)->CloseDevice();
}
LIBCUTTERPLUGIN_API bool SCP_Cutting_SetSpeed(void *pluginPtr, int speedUp, int speedDown)
{
return true;
}
LIBCUTTERPLUGIN_API bool SCP_Cutting_SetPressure(void *pluginPtr, int pressure)
{
return true;
}
LIBCUTTERPLUGIN_API bool SCP_Cutting_SetMatSize(void *pluginPtr, double widthInch, double heightInch)
{
return true;
}
LIBCUTTERPLUGIN_API bool SCP_Cutting_Begin(void *pluginPtr)
{
return ((LibCutter*)pluginPtr)->Cutting_Begin();
}
LIBCUTTERPLUGIN_API bool SCP_Cutting_MoveTo(void *pluginPtr, double xInch, double yInch)
{
return ((LibCutter*)pluginPtr)->Cutting_MoveTo(xInch, yInch);
}
LIBCUTTERPLUGIN_API bool SCP_Cutting_LineTo(void *pluginPtr, double xInch, double yInch)
{
return ((LibCutter*)pluginPtr)->Cutting_LineTo(xInch, yInch);
}
LIBCUTTERPLUGIN_API bool SCP_Cutting_CurveTo(void *pluginPtr, double cx1Inch, double cy1Inch, double cx2Inch, double cy2Inch, double xInch, double yInch)
{
return ((LibCutter*)pluginPtr)->Cutting_CurveTo(cx1Inch, cy1Inch, cx2Inch, cy2Inch, xInch, yInch);
}
LIBCUTTERPLUGIN_API bool SCP_Cutting_End(void *pluginPtr)
{
return ((LibCutter*)pluginPtr)->Cutting_End();
}
LibCutter::LibCutter(void) :
pLibCutterDevice(NULL)
{
}
LibCutter::~LibCutter(void)
{
if (pLibCutterDevice) {
delete pLibCutterDevice;
}
}
int GetBase(char *str)
{
if (strstr(str, "0x")) {
return 16;
}
return 10;
}
bool LibCutter::LoadKeys(void)
{
long key_index = 0;
std::string key_path = sDllFolderPath;
#ifdef WIN32
key_path += "\\keys.h";
#else
key_path += "/keys.h";
#endif
FILE *fp = fopen(key_path.c_str(), "r");
if (fp) {
char str[256];
while (fgets(str, 256, fp)) {
char *pch = strtok(str, "\t ");
int key_type = 0;
while (pch) {
if (key_type > 0) {
switch (key_type) {
case 1:
moveKey0 = strtol(pch, NULL, GetBase(pch));
break;
case 2:
moveKey1 = strtol(pch, NULL, GetBase(pch));
break;
case 3:
moveKey2 = strtol(pch, NULL, GetBase(pch));
break;
case 4:
moveKey3 = strtol(pch, NULL, GetBase(pch));
break;
case 5:
lineKey0 = strtol(pch, NULL, GetBase(pch));
break;
case 6:
lineKey1 = strtol(pch, NULL, GetBase(pch));
break;
case 7:
lineKey2 = strtol(pch, NULL, GetBase(pch));
break;
case 8:
lineKey3 = strtol(pch, NULL, GetBase(pch));
break;
case 9:
curveKey0 = strtol(pch, NULL, GetBase(pch));
break;
case 10:
curveKey1 = strtol(pch, NULL, GetBase(pch));
break;
case 11:
curveKey2 = strtol(pch, NULL, GetBase(pch));
break;
case 12:
curveKey3 = strtol(pch, NULL, GetBase(pch));
break;
case 13:
if (GetBase(pch) == 16) {
switch (key_index) {
case 0:
curveKey0 = strtol(pch, NULL, 16); break;
case 1:
curveKey1 = strtol(pch, NULL, 16); break;
case 2:
curveKey2 = strtol(pch, NULL, 16); break;
case 3:
curveKey3 = strtol(pch, NULL, 16); break;
}
key_index++;
}
break;
case 14:
if (GetBase(pch) == 16) {
switch (key_index) {
case 0:
moveKey0 = strtol(pch, NULL, 16); break;
case 1:
moveKey1 = strtol(pch, NULL, 16); break;
case 2:
moveKey2 = strtol(pch, NULL, 16); break;
case 3:
moveKey3 = strtol(pch, NULL, 16); break;
}
key_index++;
}
break;
case 15:
if (GetBase(pch) == 16) {
switch (key_index) {
case 0:
lineKey0 = strtol(pch, NULL, 16); break;
case 1:
lineKey1 = strtol(pch, NULL, 16); break;
case 2:
lineKey2 = strtol(pch, NULL, 16); break;
case 3:
lineKey3 = strtol(pch, NULL, 16); break;
}
key_index++;
}
break;
}
}
else {
if (0 == strcmp(pch, "MOVE_KEY_0")) {
key_type = 1;
}
else if (0 == strcmp(pch, "MOVE_KEY_1")) {
key_type = 2;
}
else if (0 == strcmp(pch, "MOVE_KEY_2")) {
key_type = 3;
}
else if (0 == strcmp(pch, "MOVE_KEY_3")) {
key_type = 4;
}
else if (0 == strcmp(pch, "LINE_KEY_0")) {
key_type = 5;
}
else if (0 == strcmp(pch, "LINE_KEY_1")) {
key_type = 6;
}
else if (0 == strcmp(pch, "LINE_KEY_2")) {
key_type = 7;
}
else if (0 == strcmp(pch, "LINE_KEY_3")) {
key_type = 8;
}
else if (0 == strcmp(pch, "CURVE_KEY_0")) {
key_type = 9;
}
else if (0 == strcmp(pch, "CURVE_KEY_1")) {
key_type = 10;
}
else if (0 == strcmp(pch, "CURVE_KEY_2")) {
key_type = 11;
}
else if (0 == strcmp(pch, "CURVE_KEY_3")) {
key_type = 12;
}
//
else if (0 == strcmp(pch, "KEY1")) { // curve
key_type = 13;
key_index = 0;
}
else if (0 == strcmp(pch, "KEY2")) { // move
key_type = 14;
key_index = 0;
}
else if (0 == strcmp(pch, "KEY3")) { // line
key_type = 15;
key_index = 0;
}
}
pch = strtok(NULL, "\t ");
}
}
fclose(fp);
return true;
}
return false;
}
long LibCutter::GetCapabilities(void)
{
return 0;
}
#ifdef WIN32
BOOL IsNumeric(LPCSTR pszString, BOOL bIgnoreColon)
{
long len = strlen(pszString);
if (len == 0) {
return FALSE;
}
BOOL b_numeric = TRUE;
for (long i = 0; i<len && b_numeric; i++) {
b_numeric = (isdigit((int)(pszString[i])) != 0);
if (bIgnoreColon && (pszString[i] == ':')) {
b_numeric = TRUE;
}
}
return b_numeric;
}
BOOL IsNumeric(LPCWSTR pszString, BOOL bIgnoreColon)
{
long len = wcslen(pszString);
if (len == 0) {
return FALSE;
}
BOOL b_numeric = TRUE;
for (long i = 0; i<len && b_numeric; i++) {
b_numeric = (iswdigit((int)(pszString[i])) != 0);
if (bIgnoreColon && (pszString[i] == L':')) {
b_numeric = TRUE;
}
}
return b_numeric;
}
#endif
bool LibCutter::FindDevice(void)
{
sDevPath.clear();
long vid = 0x0403;
long pid = 0x6001;
#ifdef WIN32
DWORD dw_guids = 0;
::SetupDiClassGuidsFromName(_T("Ports"), NULL, 0, &dw_guids);
if (dw_guids == 0)
{
return false;
}
GUID *p_guids = new GUID[dw_guids];
if (!::SetupDiClassGuidsFromName(_T("Ports"), p_guids, dw_guids, &dw_guids))
{
delete[] p_guids;
return false;
}
HDEVINFO h_dev_info_set = SetupDiGetClassDevs(p_guids, NULL, NULL, DIGCF_PRESENT);
if (h_dev_info_set == INVALID_HANDLE_VALUE)
{
delete[] p_guids;
return false;
}
TCHAR vid_pid_str[64];
_sntprintf(vid_pid_str, 64, _T("vid_%04x&pid_%04x"), vid, pid);
TCHAR vid_pid_str2[64];
_sntprintf(vid_pid_str2, 64, _T("vid_%04x+pid_%04x"), vid, pid);
long com_port_number = 0;
BOOL b_more_items = TRUE;
long index = 0;
SP_DEVINFO_DATA dev_info;
while (b_more_items)
{
dev_info.cbSize = sizeof(SP_DEVINFO_DATA);
b_more_items = SetupDiEnumDeviceInfo(h_dev_info_set, index, &dev_info);
if (b_more_items)
{
BOOL b_added = FALSE;
HKEY h_device_key = SetupDiOpenDevRegKey(h_dev_info_set, &dev_info, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE);
if (h_device_key)
{
TCHAR sz_port_name[256];
sz_port_name[0] = 0;
DWORD dw_size = sizeof(sz_port_name);
DWORD dw_type = 0;
if ((RegQueryValueEx(h_device_key, _T("PortName"), NULL, &dw_type, (LPBYTE)(sz_port_name), &dw_size) == ERROR_SUCCESS) && (dw_type == REG_SZ))
{
long len = _tcslen(sz_port_name);
if (len > 3) {
if ((_tcsnicmp(sz_port_name, _T("COM"), 3) == 0) && IsNumeric(&(sz_port_name[3]), FALSE))
{
long n_port = _ttoi(&(sz_port_name[3]));
com_port_number = n_port;
b_added = TRUE;
}
}
}
RegCloseKey(h_device_key);
}
if (b_added) {
DWORD required_sz = 0;
required_sz = 512;
TCHAR dev_instance_str[512];
dev_instance_str[0] = 0;
if (SetupDiGetDeviceInstanceId(h_dev_info_set, &dev_info, dev_instance_str, required_sz, NULL))
{
long l = _tcslen(dev_instance_str);
TCHAR *lowercase_str = new TCHAR[l + 1];
TCHAR *p_dst = lowercase_str;
TCHAR *p_src = dev_instance_str;
while (l-- > 0)
{
TCHAR c = *p_src++;
if (_istascii(c) && _istupper(c))
{
*p_dst = _tolower(c);
}
else {
*p_dst = c;
}
++p_dst;
}
*p_dst = 0;
bool b_found = (_tcsstr(lowercase_str, vid_pid_str)) != NULL;
if (!b_found)
{
b_found = (_tcsstr(lowercase_str, vid_pid_str2)) != NULL;
}
delete[] lowercase_str;
if (b_found)
{
char com_str[32];
if (com_port_number > 9) {
_sntprintf(com_str, 32, _T("\\\\.\\COM%d"), com_port_number);
}
else {
_sntprintf(com_str, 32, _T("COM%d"), com_port_number);
}
sDevPath = com_str;
SetupDiDestroyDeviceInfoList(h_dev_info_set);
return true;
}
}
}
}
++index;
}
SetupDiDestroyDeviceInfoList(h_dev_info_set);
return false;
#else
io_iterator_t serial_port_iterator;
char bsd_path[512];
kern_return_t kern_result;
CFMutableDictionaryRef classes_to_match = IOServiceMatching(kIOSerialBSDServiceValue);
kern_result = IOServiceGetMatchingServices(kIOMasterPortDefault, classes_to_match, &serial_port_iterator);
if (KERN_SUCCESS != kern_result)
{
return false;
}
io_object_t service;
long product_id = 0;
long vendor_id = 0;
while ((service = IOIteratorNext(serial_port_iterator)))
{
bsd_path[0] = 0;
CFTypeRef bsd_path_as_cfstring = IORegistryEntryCreateCFProperty(service, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
if (bsd_path_as_cfstring) {
CFStringGetCString((CFStringRef)bsd_path_as_cfstring, bsd_path, 512, kCFStringEncodingUTF8);
CFRelease(bsd_path_as_cfstring);
io_registry_entry_t parent;
kern_result = IORegistryEntryGetParentEntry(service, kIOServicePlane, &parent);
CFTypeRef vendor_id_as_cfnumber = NULL;
CFTypeRef product_id_as_cfnumber = NULL;
while (kern_result == KERN_SUCCESS && !vendor_id_as_cfnumber && !product_id_as_cfnumber) {
vendor_id_as_cfnumber = IORegistryEntrySearchCFProperty(parent, kIOServicePlane, CFSTR(kUSBVendorID), kCFAllocatorDefault, 0);
product_id_as_cfnumber = IORegistryEntrySearchCFProperty(parent, kIOServicePlane, CFSTR(kUSBProductID), kCFAllocatorDefault, 0);
io_registry_entry_t old_parent = parent;
kern_result = IORegistryEntryGetParentEntry(parent, kIOServicePlane, &parent);
IOObjectRelease(old_parent);
}
if (vendor_id_as_cfnumber)
{
SInt32 vid;
if (CFNumberGetValue((CFNumberRef)vendor_id_as_cfnumber, kCFNumberSInt32Type, &vid)) {
vendor_id = vid;
}
CFRelease(vendor_id_as_cfnumber);
}
if (product_id_as_cfnumber)
{
SInt32 pid;
if (CFNumberGetValue((CFNumberRef)product_id_as_cfnumber, kCFNumberSInt32Type, &pid)) {
product_id = pid;
}
CFRelease(product_id_as_cfnumber);
}
if (vid == vendor_id && pid == product_id)
{
sDevPath = bsd_path;
IOObjectRelease(serial_port_iterator);
return true;
}
}
}
IOObjectRelease(serial_port_iterator);
#endif
return false;
}
bool LibCutter::OpenDevice(void)
{
if (!LoadKeys()) {
return false;
}
pLibCutterDevice = new Device::C(sDevPath.c_str());
return pLibCutterDevice->is_open();
}
bool LibCutter::CloseDevice(void)
{
return true;
}
bool LibCutter::Cutting_Begin(void)
{
pLibCutterDevice->stop();
pLibCutterDevice->start();
ckey_type move_key = { moveKey0, moveKey1, moveKey2, moveKey3 };
pLibCutterDevice->set_move_key(move_key);
ckey_type line_key = { lineKey0, lineKey1, lineKey2, lineKey3 };
pLibCutterDevice->set_line_key(line_key);
ckey_type curve_key = { curveKey0, curveKey1, curveKey2, curveKey3 };
pLibCutterDevice->set_curve_key(curve_key);
return true;
}
bool LibCutter::Cutting_End(void)
{
pLibCutterDevice->stop();
return true;
}
bool LibCutter::Cutting_MoveTo(double xInch, double yInch)
{
xy pt;
pt.x = xInch;
pt.y = yInch;
pLibCutterDevice->move_to(pt);
lastX = xInch;
lastY = yInch;
return true;
}
bool LibCutter::Cutting_LineTo(double xInch, double yInch)
{
xy pt;
pt.x = xInch;
pt.y = yInch;
pLibCutterDevice->cut_to(pt);
lastX = xInch;
lastY = yInch;
return true;
}
bool LibCutter::Cutting_CurveTo(double cx1Inch, double cy1Inch, double cx2Inch, double cy2Inch, double xInch, double yInch)
{
xy pt;
xy cpt1;
xy cpt2;
xy pt2;
pt.x = lastX;
pt.y = lastY;
cpt1.x = cx1Inch;
cpt1.y = cy1Inch;
cpt2.x = cx2Inch;
cpt2.y = cy2Inch;
pt2.x = xInch;
pt2.y = yInch;
pLibCutterDevice->curve_to(pt, cpt1, cpt2, pt2);
lastX = xInch;
lastY = yInch;
return true;
}