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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
|
#include "../stdafx.h"
#include "phidget_jni.h"
#include "com_phidgets_IRPhidget.h"
#include "../cphidgetir.h"
EVENT_VARS(code, Code)
EVENT_VARS(learn, Learn)
EVENT_VARS(rawData, RawData)
static jclass irCode_class; //class
static jmethodID irCode_cons; //constructor
static jmethodID irCode_getData;
static jclass irCodeInfo_class; //class
static jmethodID irCodeInfo_cons; //constructor
static jmethodID irCodeInfo_getEncoding;
static jmethodID irCodeInfo_getLength;
static jmethodID irCodeInfo_getBitCount;
static jmethodID irCodeInfo_getGap;
static jmethodID irCodeInfo_getTrail;
static jmethodID irCodeInfo_getMinRepeat;
static jmethodID irCodeInfo_getCarrierFrequency;
static jmethodID irCodeInfo_getDutyCycle;
static jmethodID irCodeInfo_getHeader;
static jmethodID irCodeInfo_getZero;
static jmethodID irCodeInfo_getOne;
static jmethodID irCodeInfo_getRepeat;
static jmethodID irCodeInfo_getToggleMask;
static jclass irLearn_class; //class
static jmethodID irLearn_cons; //constructor
JNI_LOAD(ir, IR)
EVENT_VAR_SETUP(ir, learn, Learn, Lcom/phidgets/IRLearnedCode;, V)
EVENT_VAR_SETUP(ir, rawData, RawData, [I, V)
EVENT_VAR_SETUP(ir, code, Code, Lcom/phidgets/IRCode;Z, V)
if (!(irCode_class = (*env)->FindClass(env,"com/phidgets/IRCode")))
JNI_ABORT_STDERR("Couldn't FindClass com/phidgets/IRCode");
if (!(irCode_class = (jclass)(*env)->NewGlobalRef(env, irCode_class)))
JNI_ABORT_STDERR("Couldn't create global ref irCode_class");
if (!(irCode_cons = (*env)->GetMethodID(env, irCode_class, "<init>", "([SI)V")))
JNI_ABORT_STDERR("Couldn't get method ID <init> from irCode_class");
if (!(irCode_getData = (*env)->GetMethodID(env, irCode_class, "getData", "()[S")))
JNI_ABORT_STDERR("Couldn't get method ID getData from irCode_class");
if (!(irCodeInfo_class = (*env)->FindClass(env,"com/phidgets/IRCodeInfo")))
JNI_ABORT_STDERR("Couldn't FindClass com/phidgets/IRCodeInfo");
if (!(irCodeInfo_class = (jclass)(*env)->NewGlobalRef(env, irCodeInfo_class)))
JNI_ABORT_STDERR("Couldn't create global ref irCodeInfo_class");
if (!(irCodeInfo_cons = (*env)->GetMethodID(env, irCodeInfo_class, "<init>", "(II[I[I[III[IILcom/phidgets/IRCode;III)V")))
JNI_ABORT_STDERR("Couldn't get method ID <init> from irCodeInfo_class");
if (!(irCodeInfo_getEncoding = (*env)->GetMethodID(env, irCodeInfo_class, "getEncoding", "()I")))
JNI_ABORT_STDERR("Couldn't get method ID getEncoding from irCodeInfo_class");
if (!(irCodeInfo_getLength = (*env)->GetMethodID(env, irCodeInfo_class, "getLength", "()I")))
JNI_ABORT_STDERR("Couldn't get method ID getLength from irCodeInfo_class");
if (!(irCodeInfo_getBitCount = (*env)->GetMethodID(env, irCodeInfo_class, "getBitCount", "()I")))
JNI_ABORT_STDERR("Couldn't get method ID getBitCount from irCodeInfo_class");
if (!(irCodeInfo_getGap = (*env)->GetMethodID(env, irCodeInfo_class, "getGap", "()I")))
JNI_ABORT_STDERR("Couldn't get method ID getGap from irCodeInfo_class");
if (!(irCodeInfo_getTrail = (*env)->GetMethodID(env, irCodeInfo_class, "getTrail", "()I")))
JNI_ABORT_STDERR("Couldn't get method ID getTrail from irCodeInfo_class");
if (!(irCodeInfo_getMinRepeat = (*env)->GetMethodID(env, irCodeInfo_class, "getMinRepeat", "()I")))
JNI_ABORT_STDERR("Couldn't get method ID getMinRepeat from irCodeInfo_class");
if (!(irCodeInfo_getCarrierFrequency = (*env)->GetMethodID(env, irCodeInfo_class, "getCarrierFrequency", "()I")))
JNI_ABORT_STDERR("Couldn't get method ID getCarrierFrequency from irCodeInfo_class");
if (!(irCodeInfo_getDutyCycle = (*env)->GetMethodID(env, irCodeInfo_class, "getDutyCycle", "()I")))
JNI_ABORT_STDERR("Couldn't get method ID getDutyCycle from irCodeInfo_class");
if (!(irCodeInfo_getHeader = (*env)->GetMethodID(env, irCodeInfo_class, "getHeader", "()[I")))
JNI_ABORT_STDERR("Couldn't get method ID getHeader from irCodeInfo_class");
if (!(irCodeInfo_getZero = (*env)->GetMethodID(env, irCodeInfo_class, "getZero", "()[I")))
JNI_ABORT_STDERR("Couldn't get method ID getZero from irCodeInfo_class");
if (!(irCodeInfo_getOne = (*env)->GetMethodID(env, irCodeInfo_class, "getOne", "()[I")))
JNI_ABORT_STDERR("Couldn't get method ID getOne from irCodeInfo_class");
if (!(irCodeInfo_getRepeat = (*env)->GetMethodID(env, irCodeInfo_class, "getRepeat", "()[I")))
JNI_ABORT_STDERR("Couldn't get method ID getRepeat from irCodeInfo_class");
if (!(irCodeInfo_getToggleMask = (*env)->GetMethodID(env, irCodeInfo_class, "getToggleMask", "()Lcom/phidgets/IRCode;")))
JNI_ABORT_STDERR("Couldn't get method ID getToggleMask from irCodeInfo_class");
if (!(irLearn_class = (*env)->FindClass(env,"com/phidgets/IRLearnedCode")))
JNI_ABORT_STDERR("Couldn't FindClass com/phidgets/IRLearnedCode");
if (!(irLearn_class = (jclass)(*env)->NewGlobalRef(env, irLearn_class)))
JNI_ABORT_STDERR("Couldn't create global ref irLearn_class");
if (!(irLearn_cons = (*env)->GetMethodID(env, irLearn_class, "<init>", "(Lcom/phidgets/IRCode;Lcom/phidgets/IRCodeInfo;)V")))
JNI_ABORT_STDERR("Couldn't get method ID <init> from irLearn_class");
}
//Code event
static int CCONV code_handler(CPhidgetIRHandle h, void *arg, unsigned char *data, int dataLength, int bitCount, int repeat);
JNIEXPORT void JNICALL
Java_com_phidgets_IRPhidget_enableCodeEvents(JNIEnv *env, jobject obj, jboolean b)
{
jlong gr = updateGlobalRef(env, obj, nativeCodeHandler_fid, b);
CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField(env, obj, handle_fid);
CPhidgetIR_set_OnCode_Handler(h, b ? code_handler : 0, (void *)(uintptr_t)gr);
}
static int CCONV
code_handler(CPhidgetIRHandle h, void *arg, unsigned char *data, int dataLength, int bitCount, int repeat)
{
JNIEnv *env;
jobject obj;
jobject codeEv;
jobject ircode;
jshortArray js;
jshort *datas;
int i;
if ((*ph_vm)->AttachCurrentThread(ph_vm, (JNIEnvPtr)&env, NULL))
JNI_ABORT_STDERR("Couldn't AttachCurrentThread");
obj = (jobject)arg;
//create and fill in short array
js = (*env)->NewShortArray(env, dataLength);
if (!js)
return -1;
datas = (*env)->GetShortArrayElements(env, js, 0);
if (!datas)
return -1;
for (i=0; i<dataLength; i++)
datas[i] = (jshort)data[i];
(*env)->ReleaseShortArrayElements(env, js, datas, 0);
if (!(ircode = (*env)->NewObject(env, irCode_class, irCode_cons, js, bitCount)))
{
return -1;
}
if (!(codeEv = (*env)->NewObject(env, codeEvent_class,
codeEvent_cons, obj, ircode, repeat)))
{
(*env)->DeleteLocalRef(env, ircode);
return -1;
}
(*env)->CallVoidMethod(env, obj, fireCode_mid, codeEv);
(*env)->DeleteLocalRef(env, codeEv);
(*env)->DeleteLocalRef(env, ircode);
(*ph_vm)->DetachCurrentThread(ph_vm);
return 0;
}
//Learn event
static int CCONV learn_handler(CPhidgetIRHandle h, void *arg, unsigned char *data, int dataLength, CPhidgetIR_CodeInfoHandle codeInfo);
JNIEXPORT void JNICALL
Java_com_phidgets_IRPhidget_enableLearnEvents(JNIEnv *env, jobject obj, jboolean b)
{
jlong gr = updateGlobalRef(env, obj, nativeLearnHandler_fid, b);
CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField(env, obj, handle_fid);
CPhidgetIR_set_OnLearn_Handler(h, b ? learn_handler : 0, (void *)(uintptr_t)gr);
}
static int CCONV
learn_handler(CPhidgetIRHandle h, void *arg, unsigned char *data, int dataLength, CPhidgetIR_CodeInfoHandle codeInfo)
{
JNIEnv *env;
jobject obj;
jobject learnEv;
jobject ircode, ircodeinfo, irlearn, togglemask;
jshortArray codeData;
jintArray headerArray, zeroArray, oneArray, repeatArray;
jshort *datas;
jint *datai;
int i,j;
if ((*ph_vm)->AttachCurrentThread(ph_vm, (JNIEnvPtr)&env, NULL))
JNI_ABORT_STDERR("Couldn't AttachCurrentThread");
obj = (jobject)arg;
//create and fill in short array
if(dataLength > 0){
codeData = (*env)->NewShortArray(env, dataLength);
if (!codeData)
return -1;
datas = (*env)->GetShortArrayElements(env, codeData, 0);
if (!datas)
return -1;
for (i=0; i<dataLength; i++)
datas[i] = (jshort)data[i];
(*env)->ReleaseShortArrayElements(env, codeData, datas, 0);
}
else
codeData=NULL;
//create IRCode object
if (!(ircode = (*env)->NewObject(env, irCode_class, irCode_cons, codeData, codeInfo->bitCount)))
{
return -1;
}
//Header
if(codeInfo->header[0])
{
headerArray = (*env)->NewIntArray(env, 2);
if (!headerArray)
return -1;
datai = (*env)->GetIntArrayElements(env, headerArray, 0);
if (!datai)
return -1;
for (i=0; i<2; i++)
datai[i] = (jint)codeInfo->header[i];
(*env)->ReleaseIntArrayElements(env, headerArray, datai, 0);
}
else
headerArray=NULL;
//Zero
zeroArray = (*env)->NewIntArray(env, 2);
if (!zeroArray)
return -1;
datai = (*env)->GetIntArrayElements(env, zeroArray, 0);
if (!datai)
return -1;
for (i=0; i<2; i++)
datai[i] = (jint)codeInfo->zero[i];
(*env)->ReleaseIntArrayElements(env, zeroArray, datai, 0);
//One
oneArray = (*env)->NewIntArray(env, 2);
if (!oneArray)
return -1;
datai = (*env)->GetIntArrayElements(env, oneArray, 0);
if (!datai)
return -1;
for (i=0; i<2; i++)
datai[i] = (jint)codeInfo->one[i];
(*env)->ReleaseIntArrayElements(env, oneArray, datai, 0);
//Repeat
i=0;
while(codeInfo->repeat[i])
i++;
if(i)
{
repeatArray = (*env)->NewIntArray(env, i);
if (!repeatArray)
return -1;
datai = (*env)->GetIntArrayElements(env, repeatArray, 0);
if (!datai)
return -1;
for (j=0; j<i; j++)
datai[j] = (jint)codeInfo->repeat[j];
(*env)->ReleaseIntArrayElements(env, repeatArray, datai, 0);
}
else{
repeatArray = NULL;
}
//ToggleMask
if(dataLength > 0){
codeData = (*env)->NewShortArray(env, dataLength);
if (!codeData)
return -1;
datas = (*env)->GetShortArrayElements(env, codeData, 0);
if (!datas)
return -1;
for (i=0; i<dataLength; i++)
datas[i] = (jshort)codeInfo->toggle_mask[i];
(*env)->ReleaseShortArrayElements(env, codeData, datas, 0);
if (!(togglemask = (*env)->NewObject(env, irCode_class, irCode_cons, codeData, codeInfo->bitCount)))
{
return -1;
}
}
else{
codeData=NULL;
}
//create IRCodeInfo object
if (!(ircodeinfo = (*env)->NewObject(env, irCodeInfo_class, irCodeInfo_cons,
codeInfo->encoding, codeInfo->bitCount, headerArray, zeroArray, oneArray,
codeInfo->trail, codeInfo->gap, repeatArray, codeInfo->min_repeat, togglemask,
codeInfo->length, codeInfo->carrierFrequency, codeInfo->dutyCycle)))
{
(*env)->DeleteLocalRef(env, ircode);
return -1;
}
//create IRLearn object
if (!(irlearn = (*env)->NewObject(env, irLearn_class, irLearn_cons, ircode, ircodeinfo)))
{
(*env)->DeleteLocalRef(env, ircode);
(*env)->DeleteLocalRef(env, ircodeinfo);
return -1;
}
if (!(learnEv = (*env)->NewObject(env, learnEvent_class,
learnEvent_cons, obj, irlearn)))
{
(*env)->DeleteLocalRef(env, ircode);
(*env)->DeleteLocalRef(env, ircodeinfo);
(*env)->DeleteLocalRef(env, irlearn);
return -1;
}
(*env)->CallVoidMethod(env, obj, fireLearn_mid, learnEv);
(*env)->DeleteLocalRef(env, learnEv);
(*env)->DeleteLocalRef(env, ircode);
(*env)->DeleteLocalRef(env, ircodeinfo);
(*env)->DeleteLocalRef(env, irlearn);
(*ph_vm)->DetachCurrentThread(ph_vm);
return 0;
}
//RawData event
static int CCONV rawData_handler(CPhidgetIRHandle h, void *arg, int *data, int dataLength);
JNIEXPORT void JNICALL
Java_com_phidgets_IRPhidget_enableRawDataEvents(JNIEnv *env, jobject obj, jboolean b)
{
jlong gr = updateGlobalRef(env, obj, nativeRawDataHandler_fid, b);
CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField(env, obj, handle_fid);
CPhidgetIR_set_OnRawData_Handler(h, b ? rawData_handler : 0, (void *)(uintptr_t)gr);
}
static int CCONV
rawData_handler(CPhidgetIRHandle h, void *arg, int *data, int dataLength)
{
JNIEnv *env;
jobject obj;
jobject rawDataEv;
jintArray js;
jint *datas;
int i;
if ((*ph_vm)->AttachCurrentThread(ph_vm, (JNIEnvPtr)&env, NULL))
JNI_ABORT_STDERR("Couldn't AttachCurrentThread");
obj = (jobject)arg;
//create and fill in int array
js = (*env)->NewIntArray(env, dataLength);
if (!js)
return -1;
datas = (*env)->GetIntArrayElements(env, js, 0);
if (!datas)
return -1;
for (i=0; i<dataLength; i++)
datas[i] = (jint)data[i];
(*env)->ReleaseIntArrayElements(env, js, datas, 0);
if (!(rawDataEv = (*env)->NewObject(env, rawDataEvent_class, rawDataEvent_cons, obj, js)))
return -1;
(*env)->CallVoidMethod(env, obj, fireRawData_mid, rawDataEv);
(*env)->DeleteLocalRef(env, rawDataEv);
(*ph_vm)->DetachCurrentThread(ph_vm);
return 0;
}
JNI_CREATE(IR)
JNIEXPORT jobject JNICALL
Java_com_phidgets_IRPhidget_getLastCode(JNIEnv *env, jobject obj)
{
CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField( env, obj, handle_fid);
int error;
unsigned char data[IR_MAX_CODE_DATA_LENGTH];
int dataLength=IR_MAX_CODE_DATA_LENGTH, bitCount, i;
jobject ircode;
jshortArray js;
jshort *datas;
if ((error = CPhidgetIR_getLastCode(h, data, &dataLength, &bitCount)))
{
PH_THROW(error);
return NULL;
}
//create and fill in short array
js = (*env)->NewShortArray(env, dataLength);
if (!js)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
datas = (*env)->GetShortArrayElements(env, js, 0);
if (!datas)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
for (i=0; i<dataLength; i++)
datas[i] = (jshort)data[i];
(*env)->ReleaseShortArrayElements(env, js, datas, 0);
//create and return IRCode object
if (!(ircode = (*env)->NewObject(env, irCode_class, irCode_cons, js, bitCount)))
{
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
return ircode;
}
JNIEXPORT jint JNICALL
Java_com_phidgets_IRPhidget_readRaw(JNIEnv *env, jobject obj, jintArray intArray, jint offset, jint count)
{
CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField( env, obj, handle_fid);
int error, dataLength = count;
jint *datai;
//create and fill in int array
datai = (*env)->GetIntArrayElements(env, intArray, 0);
if (!datai)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return -1;
}
if ((error = CPhidgetIR_getRawData(h, datai, &dataLength)))
{
(*env)->ReleaseIntArrayElements(env, intArray, datai, 0);
PH_THROW(error);
return -1;
}
(*env)->ReleaseIntArrayElements(env, intArray, datai, 0);
return dataLength;
}
JNIEXPORT jobject JNICALL
Java_com_phidgets_IRPhidget_getLastLearnedCode(JNIEnv *env, jobject obj)
{
CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField( env, obj, handle_fid);
int error;
unsigned char data[IR_MAX_CODE_DATA_LENGTH];
int dataLength=IR_MAX_CODE_DATA_LENGTH;
CPhidgetIR_CodeInfo codeInfoReal;
CPhidgetIR_CodeInfoHandle codeInfo = &codeInfoReal;
jobject ircode, ircodeinfo, irlearn, togglemask;
jshortArray codeData;
jintArray headerArray, zeroArray, oneArray, repeatArray;
jshort *datas;
jint *datai;
int i,j;
if ((error = CPhidgetIR_getLastLearnedCode(h, data, &dataLength, codeInfo)))
{
PH_THROW(error);
return NULL;
}
//create and fill in short array
if(dataLength > 0){
codeData = (*env)->NewShortArray(env, dataLength);
if (!codeData)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
datas = (*env)->GetShortArrayElements(env, codeData, 0);
if (!datas)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
for (i=0; i<dataLength; i++)
datas[i] = (jshort)data[i];
(*env)->ReleaseShortArrayElements(env, codeData, datas, 0);
}
else
codeData=NULL;
//create IRCode object
if (!(ircode = (*env)->NewObject(env, irCode_class, irCode_cons, codeData, codeInfo->bitCount)))
{
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
//Header
if(codeInfo->header[0])
{
headerArray = (*env)->NewIntArray(env, 2);
if (!headerArray)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
datai = (*env)->GetIntArrayElements(env, headerArray, 0);
if (!datai)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
for (i=0; i<2; i++)
datai[i] = (jint)codeInfo->header[i];
(*env)->ReleaseIntArrayElements(env, headerArray, datai, 0);
}
else
headerArray=NULL;
//Zero
zeroArray = (*env)->NewIntArray(env, 2);
if (!zeroArray)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
datai = (*env)->GetIntArrayElements(env, zeroArray, 0);
if (!datai)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
for (i=0; i<2; i++)
datai[i] = (jint)codeInfo->zero[i];
(*env)->ReleaseIntArrayElements(env, zeroArray, datai, 0);
//One
oneArray = (*env)->NewIntArray(env, 2);
if (!oneArray)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
datai = (*env)->GetIntArrayElements(env, oneArray, 0);
if (!datai)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
for (i=0; i<2; i++)
datai[i] = (jint)codeInfo->one[i];
(*env)->ReleaseIntArrayElements(env, oneArray, datai, 0);
//Repeat
i=0;
while(codeInfo->repeat[i])
i++;
if(i)
{
repeatArray = (*env)->NewIntArray(env, i);
if (!repeatArray)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
datai = (*env)->GetIntArrayElements(env, repeatArray, 0);
if (!datai)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
for (j=0; j<i; j++)
datai[j] = (jint)codeInfo->repeat[j];
(*env)->ReleaseIntArrayElements(env, repeatArray, datai, 0);
}
else{
repeatArray=NULL;
}
//ToggleMask
if(dataLength > 0){
codeData = (*env)->NewShortArray(env, dataLength);
if (!codeData)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
datas = (*env)->GetShortArrayElements(env, codeData, 0);
if (!datas)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
for (i=0; i<dataLength; i++){
datas[i] = (jshort)codeInfo->toggle_mask[i];
}
(*env)->ReleaseShortArrayElements(env, codeData, datas, 0);
}
else{
codeData=NULL;
}
if (!(togglemask = (*env)->NewObject(env, irCode_class, irCode_cons, codeData, codeInfo->bitCount)))
{
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
//create IRCodeInfo object
if (!(ircodeinfo = (*env)->NewObject(env, irCodeInfo_class, irCodeInfo_cons,
codeInfo->encoding, codeInfo->bitCount, headerArray, zeroArray, oneArray,
codeInfo->trail, codeInfo->gap, repeatArray, codeInfo->min_repeat, togglemask,
codeInfo->length, codeInfo->carrierFrequency, codeInfo->dutyCycle)))
{
(*env)->DeleteLocalRef(env, ircode);
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
//create IRLearn object
if (!(irlearn = (*env)->NewObject(env, irLearn_class, irLearn_cons, ircode, ircodeinfo)))
{
(*env)->DeleteLocalRef(env, ircode);
(*env)->DeleteLocalRef(env, ircodeinfo);
PH_THROW(EPHIDGET_UNEXPECTED);
return NULL;
}
return irlearn;
}
JNIEXPORT void JNICALL
Java_com_phidgets_IRPhidget_transmit(JNIEnv *env, jobject obj, jobject code, jobject irCodeInfo)
{
CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField( env, obj, handle_fid);
jshortArray codeData;
jintArray headerArray, zeroArray, oneArray, repeatArray;
jobject togglemask;
jshort *datas;
jint *datai;
unsigned char data[IR_MAX_CODE_DATA_LENGTH];
jsize len;
CPhidgetIR_CodeInfo codeInfoReal = {0};
CPhidgetIR_CodeInfoHandle codeInfo = &codeInfoReal;
int i;
int error;
//read in code
if (!(codeData = (*env)->CallObjectMethod(env, code, irCode_getData)))
{
PH_THROW(EPHIDGET_UNEXPECTED);
return;
}
datas = (*env)->GetShortArrayElements(env, codeData, 0);
if (!datas)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return;
}
len = (*env)->GetArrayLength(env, codeData);
for(i=0;i<len;i++)
{
data[i] = (unsigned char)datas[i];
}
(*env)->ReleaseShortArrayElements(env, codeData, datas, 0);
//read in codeInfo
//ints
codeInfo->encoding = (*env)->CallIntMethod(env, irCodeInfo, irCodeInfo_getEncoding);
codeInfo->length = (*env)->CallIntMethod(env, irCodeInfo, irCodeInfo_getLength);
codeInfo->bitCount = (*env)->CallIntMethod(env, irCodeInfo, irCodeInfo_getBitCount);
codeInfo->gap = (*env)->CallIntMethod(env, irCodeInfo, irCodeInfo_getGap);
codeInfo->trail = (*env)->CallIntMethod(env, irCodeInfo, irCodeInfo_getTrail);
codeInfo->min_repeat = (*env)->CallIntMethod(env, irCodeInfo, irCodeInfo_getMinRepeat);
codeInfo->carrierFrequency = (*env)->CallIntMethod(env, irCodeInfo, irCodeInfo_getCarrierFrequency);
codeInfo->dutyCycle = (*env)->CallIntMethod(env, irCodeInfo, irCodeInfo_getDutyCycle);
//arrays/objects
//Header
headerArray = (*env)->CallObjectMethod(env, irCodeInfo, irCodeInfo_getHeader);
if(headerArray)
{
datai = (*env)->GetIntArrayElements(env, headerArray, 0);
if (!datai)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return;
}
for (i=0; i<2; i++)
codeInfo->header[i] = datai[i];
(*env)->ReleaseIntArrayElements(env, headerArray, datai, 0);
}
else
headerArray=NULL;
//Zero
zeroArray = (*env)->CallObjectMethod(env, irCodeInfo, irCodeInfo_getZero);
datai = (*env)->GetIntArrayElements(env, zeroArray, 0);
if (!datai)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return;
}
for (i=0; i<2; i++)
codeInfo->zero[i] = datai[i];
(*env)->ReleaseIntArrayElements(env, zeroArray, datai, 0);
//One
oneArray = (*env)->CallObjectMethod(env, irCodeInfo, irCodeInfo_getOne);
datai = (*env)->GetIntArrayElements(env, oneArray, 0);
if (!datai)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return;
}
for (i=0; i<2; i++)
codeInfo->one[i] = datai[i];
(*env)->ReleaseIntArrayElements(env, oneArray, datai, 0);
//Repeat
repeatArray = (*env)->CallObjectMethod(env, irCodeInfo, irCodeInfo_getRepeat);
if(repeatArray)
{
len = (*env)->GetArrayLength(env, repeatArray);
if(len)
{
datai = (*env)->GetIntArrayElements(env, repeatArray, 0);
if (!datai)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return;
}
for (i=0; i<len; i++)
codeInfo->repeat[i] = datai[i];
(*env)->ReleaseIntArrayElements(env, repeatArray, datai, 0);
}
}
else
repeatArray=NULL;
//ToggleMask
togglemask = (*env)->CallObjectMethod(env, irCodeInfo, irCodeInfo_getToggleMask);
if(togglemask)
{
if (!(codeData = (*env)->CallObjectMethod(env, togglemask, irCode_getData)))
{
PH_THROW(EPHIDGET_UNEXPECTED);
return;
}
datas = (*env)->GetShortArrayElements(env, codeData, 0);
if (!datas)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return;
}
len = (*env)->GetArrayLength(env, codeData);
for(i=0;i<len;i++)
{
codeInfo->toggle_mask[i] = (unsigned char)datas[i];
}
(*env)->ReleaseShortArrayElements(env, codeData, datas, 0);
}
else
togglemask=NULL;
if ((error = CPhidgetIR_Transmit(h, data, codeInfo)))
PH_THROW(error);
}
JNIEXPORT void JNICALL
Java_com_phidgets_IRPhidget_transmitRepeat(JNIEnv *env, jobject obj)
{
CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField( env, obj, handle_fid);
int error;
if ((error = CPhidgetIR_TransmitRepeat(h)))
PH_THROW(error);
}
JNIEXPORT void JNICALL
Java_com_phidgets_IRPhidget_transmitRaw(JNIEnv *env, jobject obj, jintArray data, jint offset, jint count, jint gap, jint carrierFrequency, jint dutyCycle)
{
CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField( env, obj, handle_fid);
int error;
jint *datai;
datai = (*env)->GetIntArrayElements(env, data, 0);
if (!datai)
{
PH_THROW(EPHIDGET_UNEXPECTED);
return;
}
if ((error = CPhidgetIR_TransmitRaw(h, datai+offset, count, carrierFrequency, dutyCycle, gap)))
PH_THROW(error);
(*env)->ReleaseIntArrayElements(env, data, datai, 0);
}
|