/
Msg.java
2475 lines (2218 loc) · 66.8 KB
/
Msg.java
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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.myrobotlab.arduino;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import org.myrobotlab.logging.Level;
import org.myrobotlab.arduino.virtual.MrlComm;
/**
* <pre>
*
Welcome to Msg.java
Its created by running ArduinoMsgGenerator
which combines the MrlComm message schema (src/resource/Arduino/arduinoMsg.schema)
with the cpp template (src/resource/Arduino/generate/Msg.java.template)
Schema Type Conversions
Schema ARDUINO Java Range
none byte/unsigned char int (cuz Java byte bites) 1 byte - 0 to 255
boolean boolean boolean 0 1
b16 int int (short) 2 bytes -32,768 to 32,767
b32 long int 4 bytes -2,147,483,648 to 2,147,483, 647
bu32 unsigned long long 0 to 4,294,967,295
str char*, size String variable length
[] byte[], size int[] variable length
All message editing should be done in the arduinoMsg.schema
The binary wire format of an Arduino is:
MAGIC_NUMBER|MSG_SIZE|METHOD_NUMBER|PARAM0|PARAM1 ...
</pre>
*/
import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.logging.LoggingFactory;
import org.myrobotlab.service.VirtualArduino;
import java.io.FileOutputStream;
import java.util.Arrays;
import org.myrobotlab.service.Arduino;
import org.myrobotlab.service.Runtime;
import org.myrobotlab.service.Servo;
import org.myrobotlab.service.interfaces.PinArrayControl;
import org.myrobotlab.service.interfaces.SerialDevice;
import org.slf4j.Logger;
/**
* Singlton messaging interface to an Arduino
*
* @author GroG
*
*/
public class Msg {
public static final int MAX_MSG_SIZE = 64;
public static final int MAGIC_NUMBER = 170; // 10101010
public static final int MRLCOMM_VERSION = 60;
// send buffer
int sendBufferSize = 0;
int sendBuffer[] = new int[MAX_MSG_SIZE];
// recv buffer
int ioCmd[] = new int[MAX_MSG_SIZE];
int byteCount = 0;
int msgSize = 0;
// ------ device type mapping constants
int method = -1;
public boolean debug = false;
boolean invoke = true;
boolean ackEnabled = true;
public static class AckLock {
// first is always true - since there
// is no msg to be acknowledged...
volatile boolean acknowledged = true;
}
transient AckLock ackRecievedLock = new AckLock();
// recording related
transient FileOutputStream record = null;
transient StringBuilder rxBuffer = new StringBuilder();
transient StringBuilder txBuffer = new StringBuilder();
public static final int DEVICE_TYPE_UNKNOWN = 0;
public static final int DEVICE_TYPE_ARDUINO = 1;
public static final int DEVICE_TYPE_ULTRASONICSENSOR = 2;
public static final int DEVICE_TYPE_STEPPER = 3;
public static final int DEVICE_TYPE_MOTOR = 4;
public static final int DEVICE_TYPE_SERVO = 5;
public static final int DEVICE_TYPE_SERIAL = 6;
public static final int DEVICE_TYPE_I2C = 7;
public static final int DEVICE_TYPE_NEOPIXEL = 8;
public static final int DEVICE_TYPE_ENCODER = 9;
// < publishMRLCommError/str errorMsg
public final static int PUBLISH_MRLCOMM_ERROR = 1;
// > getBoardInfo
public final static int GET_BOARD_INFO = 2;
// < publishBoardInfo/version/boardType/b16 microsPerLoop/b16 sram/activePins/[] deviceSummary
public final static int PUBLISH_BOARD_INFO = 3;
// > enablePin/address/type/b16 rate
public final static int ENABLE_PIN = 4;
// > setDebug/bool enabled
public final static int SET_DEBUG = 5;
// > setSerialRate/b32 rate
public final static int SET_SERIAL_RATE = 6;
// > softReset
public final static int SOFT_RESET = 7;
// > enableAck/bool enabled
public final static int ENABLE_ACK = 8;
// < publishAck/function
public final static int PUBLISH_ACK = 9;
// > echo/f32 myFloat/myByte/f32 secondFloat
public final static int ECHO = 10;
// < publishEcho/f32 myFloat/myByte/f32 secondFloat
public final static int PUBLISH_ECHO = 11;
// > customMsg/[] msg
public final static int CUSTOM_MSG = 12;
// < publishCustomMsg/[] msg
public final static int PUBLISH_CUSTOM_MSG = 13;
// > deviceDetach/deviceId
public final static int DEVICE_DETACH = 14;
// > i2cBusAttach/deviceId/i2cBus
public final static int I2C_BUS_ATTACH = 15;
// > i2cRead/deviceId/deviceAddress/size
public final static int I2C_READ = 16;
// > i2cWrite/deviceId/deviceAddress/[] data
public final static int I2C_WRITE = 17;
// > i2cWriteRead/deviceId/deviceAddress/readSize/writeValue
public final static int I2C_WRITE_READ = 18;
// < publishI2cData/deviceId/[] data
public final static int PUBLISH_I2C_DATA = 19;
// > neoPixelAttach/deviceId/pin/b32 numPixels
public final static int NEO_PIXEL_ATTACH = 20;
// > neoPixelSetAnimation/deviceId/animation/red/green/blue/b16 speed
public final static int NEO_PIXEL_SET_ANIMATION = 21;
// > neoPixelWriteMatrix/deviceId/[] buffer
public final static int NEO_PIXEL_WRITE_MATRIX = 22;
// > analogWrite/pin/value
public final static int ANALOG_WRITE = 23;
// > digitalWrite/pin/value
public final static int DIGITAL_WRITE = 24;
// > disablePin/pin
public final static int DISABLE_PIN = 25;
// > disablePins
public final static int DISABLE_PINS = 26;
// > pinMode/pin/mode
public final static int PIN_MODE = 27;
// < publishDebug/str debugMsg
public final static int PUBLISH_DEBUG = 28;
// < publishPinArray/[] data
public final static int PUBLISH_PIN_ARRAY = 29;
// > setTrigger/pin/triggerValue
public final static int SET_TRIGGER = 30;
// > setDebounce/pin/delay
public final static int SET_DEBOUNCE = 31;
// > servoAttach/deviceId/pin/b16 initPos/b16 initVelocity/str name
public final static int SERVO_ATTACH = 32;
// > servoAttachPin/deviceId/pin
public final static int SERVO_ATTACH_PIN = 33;
// > servoDetachPin/deviceId
public final static int SERVO_DETACH_PIN = 34;
// > servoSetVelocity/deviceId/b16 velocity
public final static int SERVO_SET_VELOCITY = 35;
// > servoSweepStart/deviceId/min/max/step
public final static int SERVO_SWEEP_START = 36;
// > servoSweepStop/deviceId
public final static int SERVO_SWEEP_STOP = 37;
// > servoMoveToMicroseconds/deviceId/b16 target
public final static int SERVO_MOVE_TO_MICROSECONDS = 38;
// > servoSetAcceleration/deviceId/b16 acceleration
public final static int SERVO_SET_ACCELERATION = 39;
// < publishServoEvent/deviceId/eventType/b16 currentPos/b16 targetPos
public final static int PUBLISH_SERVO_EVENT = 40;
// > serialAttach/deviceId/relayPin
public final static int SERIAL_ATTACH = 41;
// > serialRelay/deviceId/[] data
public final static int SERIAL_RELAY = 42;
// < publishSerialData/deviceId/[] data
public final static int PUBLISH_SERIAL_DATA = 43;
// > ultrasonicSensorAttach/deviceId/triggerPin/echoPin
public final static int ULTRASONIC_SENSOR_ATTACH = 44;
// > ultrasonicSensorStartRanging/deviceId
public final static int ULTRASONIC_SENSOR_START_RANGING = 45;
// > ultrasonicSensorStopRanging/deviceId
public final static int ULTRASONIC_SENSOR_STOP_RANGING = 46;
// < publishUltrasonicSensorData/deviceId/b16 echoTime
public final static int PUBLISH_ULTRASONIC_SENSOR_DATA = 47;
// > setAref/b16 type
public final static int SET_AREF = 48;
// > motorAttach/deviceId/type/[] pins
public final static int MOTOR_ATTACH = 49;
// > motorMove/deviceId/pwr
public final static int MOTOR_MOVE = 50;
// > motorMoveTo/deviceId/pos
public final static int MOTOR_MOVE_TO = 51;
// > encoderAttach/deviceId/type/pin
public final static int ENCODER_ATTACH = 52;
// > setZeroPoint/deviceId
public final static int SET_ZERO_POINT = 53;
// < publishEncoderData/deviceId/b16 position
public final static int PUBLISH_ENCODER_DATA = 54;
/**
* These methods will be invoked from the Msg class as callbacks from MrlComm.
*/
// public void publishMRLCommError(String errorMsg/*str*/){}
// public void publishBoardInfo(Integer version/*byte*/, Integer boardType/*byte*/, Integer microsPerLoop/*b16*/, Integer sram/*b16*/, Integer activePins/*byte*/, int[] deviceSummary/*[]*/){}
// public void publishAck(Integer function/*byte*/){}
// public void publishEcho(Float myFloat/*f32*/, Integer myByte/*byte*/, Float secondFloat/*f32*/){}
// public void publishCustomMsg(int[] msg/*[]*/){}
// public void publishI2cData(Integer deviceId/*byte*/, int[] data/*[]*/){}
// public void publishDebug(String debugMsg/*str*/){}
// public void publishPinArray(int[] data/*[]*/){}
// public void publishServoEvent(Integer deviceId/*byte*/, Integer eventType/*byte*/, Integer currentPos/*b16*/, Integer targetPos/*b16*/){}
// public void publishSerialData(Integer deviceId/*byte*/, int[] data/*[]*/){}
// public void publishUltrasonicSensorData(Integer deviceId/*byte*/, Integer echoTime/*b16*/){}
// public void publishEncoderData(Integer deviceId/*byte*/, Integer position/*b16*/){}
public transient final static Logger log = LoggerFactory.getLogger(Msg.class);
public Msg(Arduino arduino, SerialDevice serial) {
this.arduino = arduino;
this.serial = serial;
}
public void begin(SerialDevice serial){
this.serial = serial;
}
// transient private Msg instance;
// ArduinoSerialCallBacks - TODO - extract interface
transient private Arduino arduino;
transient private SerialDevice serial;
/**
* want to grab it when SerialDevice is created
*
* @param serial
* @return
*/
/*
static public synchronized Msg getInstance(Arduino arduino, SerialDevice serial) {
if (instance == null) {
instance = new Msg();
}
instance.arduino = arduino;
instance.serial = serial;
return instance;
}
*/
public void setInvoke(boolean b){
invoke = b;
}
public void processCommand(){
processCommand(ioCmd);
}
public void processCommand(int[] ioCmd) {
int startPos = 0;
method = ioCmd[startPos];
switch (method) {
case PUBLISH_MRLCOMM_ERROR: {
String errorMsg = str(ioCmd, startPos+2, ioCmd[startPos+1]);
startPos += 1 + ioCmd[startPos+1];
if(invoke){
arduino.invoke("publishMRLCommError", errorMsg);
} else {
arduino.publishMRLCommError( errorMsg);
}
if(record != null){
rxBuffer.append("< publishMRLCommError");
rxBuffer.append("/");
rxBuffer.append(errorMsg);
rxBuffer.append("\n");
try{
record.write(rxBuffer.toString().getBytes());
rxBuffer.setLength(0);
}catch(IOException e){}
}
break;
}
case PUBLISH_BOARD_INFO: {
Integer version = ioCmd[startPos+1]; // bu8
startPos += 1;
Integer boardType = ioCmd[startPos+1]; // bu8
startPos += 1;
Integer microsPerLoop = b16(ioCmd, startPos+1);
startPos += 2; //b16
Integer sram = b16(ioCmd, startPos+1);
startPos += 2; //b16
Integer activePins = ioCmd[startPos+1]; // bu8
startPos += 1;
int[] deviceSummary = subArray(ioCmd, startPos+2, ioCmd[startPos+1]);
startPos += 1 + ioCmd[startPos+1];
if(invoke){
arduino.invoke("publishBoardInfo", version, boardType, microsPerLoop, sram, activePins, deviceSummary);
} else {
arduino.publishBoardInfo( version, boardType, microsPerLoop, sram, activePins, deviceSummary);
}
if(record != null){
rxBuffer.append("< publishBoardInfo");
rxBuffer.append("/");
rxBuffer.append(version);
rxBuffer.append("/");
rxBuffer.append(boardType);
rxBuffer.append("/");
rxBuffer.append(microsPerLoop);
rxBuffer.append("/");
rxBuffer.append(sram);
rxBuffer.append("/");
rxBuffer.append(activePins);
rxBuffer.append("/");
rxBuffer.append(Arrays.toString(deviceSummary));
rxBuffer.append("\n");
try{
record.write(rxBuffer.toString().getBytes());
rxBuffer.setLength(0);
}catch(IOException e){}
}
break;
}
case PUBLISH_ACK: {
Integer function = ioCmd[startPos+1]; // bu8
startPos += 1;
if(invoke){
arduino.invoke("publishAck", function);
} else {
arduino.publishAck( function);
}
if(record != null){
rxBuffer.append("< publishAck");
rxBuffer.append("/");
rxBuffer.append(function);
rxBuffer.append("\n");
try{
record.write(rxBuffer.toString().getBytes());
rxBuffer.setLength(0);
}catch(IOException e){}
}
break;
}
case PUBLISH_ECHO: {
Float myFloat = f32(ioCmd, startPos+1);
startPos += 4; //f32
Integer myByte = ioCmd[startPos+1]; // bu8
startPos += 1;
Float secondFloat = f32(ioCmd, startPos+1);
startPos += 4; //f32
if(invoke){
arduino.invoke("publishEcho", myFloat, myByte, secondFloat);
} else {
arduino.publishEcho( myFloat, myByte, secondFloat);
}
if(record != null){
rxBuffer.append("< publishEcho");
rxBuffer.append("/");
rxBuffer.append(myFloat);
rxBuffer.append("/");
rxBuffer.append(myByte);
rxBuffer.append("/");
rxBuffer.append(secondFloat);
rxBuffer.append("\n");
try{
record.write(rxBuffer.toString().getBytes());
rxBuffer.setLength(0);
}catch(IOException e){}
}
break;
}
case PUBLISH_CUSTOM_MSG: {
int[] msg = subArray(ioCmd, startPos+2, ioCmd[startPos+1]);
startPos += 1 + ioCmd[startPos+1];
if(invoke){
arduino.invoke("publishCustomMsg", msg);
} else {
arduino.publishCustomMsg( msg);
}
if(record != null){
rxBuffer.append("< publishCustomMsg");
rxBuffer.append("/");
rxBuffer.append(Arrays.toString(msg));
rxBuffer.append("\n");
try{
record.write(rxBuffer.toString().getBytes());
rxBuffer.setLength(0);
}catch(IOException e){}
}
break;
}
case PUBLISH_I2C_DATA: {
Integer deviceId = ioCmd[startPos+1]; // bu8
startPos += 1;
int[] data = subArray(ioCmd, startPos+2, ioCmd[startPos+1]);
startPos += 1 + ioCmd[startPos+1];
if(invoke){
arduino.invoke("publishI2cData", deviceId, data);
} else {
arduino.publishI2cData( deviceId, data);
}
if(record != null){
rxBuffer.append("< publishI2cData");
rxBuffer.append("/");
rxBuffer.append(deviceId);
rxBuffer.append("/");
rxBuffer.append(Arrays.toString(data));
rxBuffer.append("\n");
try{
record.write(rxBuffer.toString().getBytes());
rxBuffer.setLength(0);
}catch(IOException e){}
}
break;
}
case PUBLISH_DEBUG: {
String debugMsg = str(ioCmd, startPos+2, ioCmd[startPos+1]);
startPos += 1 + ioCmd[startPos+1];
if(invoke){
arduino.invoke("publishDebug", debugMsg);
} else {
arduino.publishDebug( debugMsg);
}
if(record != null){
rxBuffer.append("< publishDebug");
rxBuffer.append("/");
rxBuffer.append(debugMsg);
rxBuffer.append("\n");
try{
record.write(rxBuffer.toString().getBytes());
rxBuffer.setLength(0);
}catch(IOException e){}
}
break;
}
case PUBLISH_PIN_ARRAY: {
int[] data = subArray(ioCmd, startPos+2, ioCmd[startPos+1]);
startPos += 1 + ioCmd[startPos+1];
if(invoke){
arduino.invoke("publishPinArray", data);
} else {
arduino.publishPinArray( data);
}
if(record != null){
rxBuffer.append("< publishPinArray");
rxBuffer.append("/");
rxBuffer.append(Arrays.toString(data));
rxBuffer.append("\n");
try{
record.write(rxBuffer.toString().getBytes());
rxBuffer.setLength(0);
}catch(IOException e){}
}
break;
}
case PUBLISH_SERVO_EVENT: {
Integer deviceId = ioCmd[startPos+1]; // bu8
startPos += 1;
Integer eventType = ioCmd[startPos+1]; // bu8
startPos += 1;
Integer currentPos = b16(ioCmd, startPos+1);
startPos += 2; //b16
Integer targetPos = b16(ioCmd, startPos+1);
startPos += 2; //b16
if(invoke){
arduino.invoke("publishServoEvent", deviceId, eventType, currentPos, targetPos);
} else {
arduino.publishServoEvent( deviceId, eventType, currentPos, targetPos);
}
if(record != null){
rxBuffer.append("< publishServoEvent");
rxBuffer.append("/");
rxBuffer.append(deviceId);
rxBuffer.append("/");
rxBuffer.append(eventType);
rxBuffer.append("/");
rxBuffer.append(currentPos);
rxBuffer.append("/");
rxBuffer.append(targetPos);
rxBuffer.append("\n");
try{
record.write(rxBuffer.toString().getBytes());
rxBuffer.setLength(0);
}catch(IOException e){}
}
break;
}
case PUBLISH_SERIAL_DATA: {
Integer deviceId = ioCmd[startPos+1]; // bu8
startPos += 1;
int[] data = subArray(ioCmd, startPos+2, ioCmd[startPos+1]);
startPos += 1 + ioCmd[startPos+1];
if(invoke){
arduino.invoke("publishSerialData", deviceId, data);
} else {
arduino.publishSerialData( deviceId, data);
}
if(record != null){
rxBuffer.append("< publishSerialData");
rxBuffer.append("/");
rxBuffer.append(deviceId);
rxBuffer.append("/");
rxBuffer.append(Arrays.toString(data));
rxBuffer.append("\n");
try{
record.write(rxBuffer.toString().getBytes());
rxBuffer.setLength(0);
}catch(IOException e){}
}
break;
}
case PUBLISH_ULTRASONIC_SENSOR_DATA: {
Integer deviceId = ioCmd[startPos+1]; // bu8
startPos += 1;
Integer echoTime = b16(ioCmd, startPos+1);
startPos += 2; //b16
if(invoke){
arduino.invoke("publishUltrasonicSensorData", deviceId, echoTime);
} else {
arduino.publishUltrasonicSensorData( deviceId, echoTime);
}
if(record != null){
rxBuffer.append("< publishUltrasonicSensorData");
rxBuffer.append("/");
rxBuffer.append(deviceId);
rxBuffer.append("/");
rxBuffer.append(echoTime);
rxBuffer.append("\n");
try{
record.write(rxBuffer.toString().getBytes());
rxBuffer.setLength(0);
}catch(IOException e){}
}
break;
}
case PUBLISH_ENCODER_DATA: {
Integer deviceId = ioCmd[startPos+1]; // bu8
startPos += 1;
Integer position = b16(ioCmd, startPos+1);
startPos += 2; //b16
if(invoke){
arduino.invoke("publishEncoderData", deviceId, position);
} else {
arduino.publishEncoderData( deviceId, position);
}
if(record != null){
rxBuffer.append("< publishEncoderData");
rxBuffer.append("/");
rxBuffer.append(deviceId);
rxBuffer.append("/");
rxBuffer.append(position);
rxBuffer.append("\n");
try{
record.write(rxBuffer.toString().getBytes());
rxBuffer.setLength(0);
}catch(IOException e){}
}
break;
}
}
}
// Java-land --to--> MrlComm
public synchronized void getBoardInfo() {
try {
if (ackEnabled){
waitForAck();
}
write(MAGIC_NUMBER);
write(1); // size
write(GET_BOARD_INFO); // msgType = 2
if (ackEnabled){
// we just wrote - block threads sending
// until they get an ack
ackRecievedLock.acknowledged = false;
}
if(record != null){
txBuffer.append("> getBoardInfo");
txBuffer.append("\n");
record.write(txBuffer.toString().getBytes());
txBuffer.setLength(0);
}
} catch (Exception e) {
log.error("getBoardInfo threw",e);
}
}
public synchronized void enablePin(Integer address/*byte*/, Integer type/*byte*/, Integer rate/*b16*/) {
try {
if (ackEnabled){
waitForAck();
}
write(MAGIC_NUMBER);
write(1 + 1 + 1 + 2); // size
write(ENABLE_PIN); // msgType = 4
write(address);
write(type);
writeb16(rate);
if (ackEnabled){
// we just wrote - block threads sending
// until they get an ack
ackRecievedLock.acknowledged = false;
}
if(record != null){
txBuffer.append("> enablePin");
txBuffer.append("/");
txBuffer.append(address);
txBuffer.append("/");
txBuffer.append(type);
txBuffer.append("/");
txBuffer.append(rate);
txBuffer.append("\n");
record.write(txBuffer.toString().getBytes());
txBuffer.setLength(0);
}
} catch (Exception e) {
log.error("enablePin threw",e);
}
}
public synchronized void setDebug(Boolean enabled/*bool*/) {
try {
if (ackEnabled){
waitForAck();
}
write(MAGIC_NUMBER);
write(1 + 1); // size
write(SET_DEBUG); // msgType = 5
writebool(enabled);
if (ackEnabled){
// we just wrote - block threads sending
// until they get an ack
ackRecievedLock.acknowledged = false;
}
if(record != null){
txBuffer.append("> setDebug");
txBuffer.append("/");
txBuffer.append(enabled);
txBuffer.append("\n");
record.write(txBuffer.toString().getBytes());
txBuffer.setLength(0);
}
} catch (Exception e) {
log.error("setDebug threw",e);
}
}
public synchronized void setSerialRate(Integer rate/*b32*/) {
try {
if (ackEnabled){
waitForAck();
}
write(MAGIC_NUMBER);
write(1 + 4); // size
write(SET_SERIAL_RATE); // msgType = 6
writeb32(rate);
if (ackEnabled){
// we just wrote - block threads sending
// until they get an ack
ackRecievedLock.acknowledged = false;
}
if(record != null){
txBuffer.append("> setSerialRate");
txBuffer.append("/");
txBuffer.append(rate);
txBuffer.append("\n");
record.write(txBuffer.toString().getBytes());
txBuffer.setLength(0);
}
} catch (Exception e) {
log.error("setSerialRate threw",e);
}
}
public synchronized void softReset() {
try {
if (ackEnabled){
waitForAck();
}
write(MAGIC_NUMBER);
write(1); // size
write(SOFT_RESET); // msgType = 7
if (ackEnabled){
// we just wrote - block threads sending
// until they get an ack
ackRecievedLock.acknowledged = false;
}
if(record != null){
txBuffer.append("> softReset");
txBuffer.append("\n");
record.write(txBuffer.toString().getBytes());
txBuffer.setLength(0);
}
} catch (Exception e) {
log.error("softReset threw",e);
}
}
public synchronized void enableAck(Boolean enabled/*bool*/) {
try {
if (ackEnabled){
waitForAck();
}
write(MAGIC_NUMBER);
write(1 + 1); // size
write(ENABLE_ACK); // msgType = 8
writebool(enabled);
if (ackEnabled){
// we just wrote - block threads sending
// until they get an ack
ackRecievedLock.acknowledged = false;
}
if(record != null){
txBuffer.append("> enableAck");
txBuffer.append("/");
txBuffer.append(enabled);
txBuffer.append("\n");
record.write(txBuffer.toString().getBytes());
txBuffer.setLength(0);
}
} catch (Exception e) {
log.error("enableAck threw",e);
}
}
public synchronized void echo(Float myFloat/*f32*/, Integer myByte/*byte*/, Float secondFloat/*f32*/) {
try {
if (ackEnabled){
waitForAck();
}
write(MAGIC_NUMBER);
write(1 + 4 + 1 + 4); // size
write(ECHO); // msgType = 10
writef32(myFloat);
write(myByte);
writef32(secondFloat);
if (ackEnabled){
// we just wrote - block threads sending
// until they get an ack
ackRecievedLock.acknowledged = false;
}
if(record != null){
txBuffer.append("> echo");
txBuffer.append("/");
txBuffer.append(myFloat);
txBuffer.append("/");
txBuffer.append(myByte);
txBuffer.append("/");
txBuffer.append(secondFloat);
txBuffer.append("\n");
record.write(txBuffer.toString().getBytes());
txBuffer.setLength(0);
}
} catch (Exception e) {
log.error("echo threw",e);
}
}
public synchronized void customMsg(int[] msg/*[]*/) {
try {
if (ackEnabled){
waitForAck();
}
write(MAGIC_NUMBER);
write(1 + (1 + msg.length)); // size
write(CUSTOM_MSG); // msgType = 12
write(msg);
if (ackEnabled){
// we just wrote - block threads sending
// until they get an ack
ackRecievedLock.acknowledged = false;
}
if(record != null){
txBuffer.append("> customMsg");
txBuffer.append("/");
txBuffer.append(Arrays.toString(msg));
txBuffer.append("\n");
record.write(txBuffer.toString().getBytes());
txBuffer.setLength(0);
}
} catch (Exception e) {
log.error("customMsg threw",e);
}
}
public synchronized void deviceDetach(Integer deviceId/*byte*/) {
try {
if (ackEnabled){
waitForAck();
}
write(MAGIC_NUMBER);
write(1 + 1); // size
write(DEVICE_DETACH); // msgType = 14
write(deviceId);
if (ackEnabled){
// we just wrote - block threads sending
// until they get an ack
ackRecievedLock.acknowledged = false;
}
if(record != null){
txBuffer.append("> deviceDetach");
txBuffer.append("/");
txBuffer.append(deviceId);
txBuffer.append("\n");
record.write(txBuffer.toString().getBytes());
txBuffer.setLength(0);
}
} catch (Exception e) {
log.error("deviceDetach threw",e);
}
}
public synchronized void i2cBusAttach(Integer deviceId/*byte*/, Integer i2cBus/*byte*/) {
try {
if (ackEnabled){
waitForAck();
}
write(MAGIC_NUMBER);
write(1 + 1 + 1); // size
write(I2C_BUS_ATTACH); // msgType = 15
write(deviceId);
write(i2cBus);
if (ackEnabled){
// we just wrote - block threads sending
// until they get an ack
ackRecievedLock.acknowledged = false;
}
if(record != null){
txBuffer.append("> i2cBusAttach");
txBuffer.append("/");
txBuffer.append(deviceId);
txBuffer.append("/");
txBuffer.append(i2cBus);
txBuffer.append("\n");
record.write(txBuffer.toString().getBytes());
txBuffer.setLength(0);
}
} catch (Exception e) {
log.error("i2cBusAttach threw",e);
}
}
public synchronized void i2cRead(Integer deviceId/*byte*/, Integer deviceAddress/*byte*/, Integer size/*byte*/) {
try {
if (ackEnabled){
waitForAck();
}
write(MAGIC_NUMBER);
write(1 + 1 + 1 + 1); // size
write(I2C_READ); // msgType = 16
write(deviceId);
write(deviceAddress);
write(size);
if (ackEnabled){
// we just wrote - block threads sending
// until they get an ack
ackRecievedLock.acknowledged = false;
}
if(record != null){
txBuffer.append("> i2cRead");
txBuffer.append("/");
txBuffer.append(deviceId);
txBuffer.append("/");
txBuffer.append(deviceAddress);
txBuffer.append("/");
txBuffer.append(size);
txBuffer.append("\n");
record.write(txBuffer.toString().getBytes());
txBuffer.setLength(0);
}
} catch (Exception e) {
log.error("i2cRead threw",e);
}
}
public synchronized void i2cWrite(Integer deviceId/*byte*/, Integer deviceAddress/*byte*/, int[] data/*[]*/) {
try {
if (ackEnabled){
waitForAck();
}
write(MAGIC_NUMBER);
write(1 + 1 + 1 + (1 + data.length)); // size
write(I2C_WRITE); // msgType = 17
write(deviceId);
write(deviceAddress);
write(data);
if (ackEnabled){
// we just wrote - block threads sending
// until they get an ack
ackRecievedLock.acknowledged = false;
}
if(record != null){
txBuffer.append("> i2cWrite");
txBuffer.append("/");
txBuffer.append(deviceId);
txBuffer.append("/");
txBuffer.append(deviceAddress);
txBuffer.append("/");
txBuffer.append(Arrays.toString(data));
txBuffer.append("\n");
record.write(txBuffer.toString().getBytes());
txBuffer.setLength(0);
}
} catch (Exception e) {
log.error("i2cWrite threw",e);
}
}
public synchronized void i2cWriteRead(Integer deviceId/*byte*/, Integer deviceAddress/*byte*/, Integer readSize/*byte*/, Integer writeValue/*byte*/) {
try {
if (ackEnabled){
waitForAck();
}
write(MAGIC_NUMBER);
write(1 + 1 + 1 + 1 + 1); // size
write(I2C_WRITE_READ); // msgType = 18
write(deviceId);
write(deviceAddress);
write(readSize);
write(writeValue);
if (ackEnabled){
// we just wrote - block threads sending
// until they get an ack
ackRecievedLock.acknowledged = false;
}
if(record != null){
txBuffer.append("> i2cWriteRead");
txBuffer.append("/");
txBuffer.append(deviceId);
txBuffer.append("/");
txBuffer.append(deviceAddress);
txBuffer.append("/");
txBuffer.append(readSize);
txBuffer.append("/");
txBuffer.append(writeValue);
txBuffer.append("\n");