123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
-
-
-
-
- import java.util.Date;
- import java.util.ArrayList;
- import java.util.List;
-
- public class SerialHelper {
-
- private final short OK = 0x42;
- private final short ERROR = 0x23;
-
- private Frame frame;
-
-
-
- public SerialHelper(String serialPort, Frame frame) throws Exception {
- if (HelperUtility.openPort(serialPort) == false) {
- printErrorMessage("Could not open serial port \"" + serialPort + "\"");
- throw new Exception("Could not open serial port \"" + serialPort + "\"");
- }
- this.frame = frame;
- }
-
-
-
- public boolean probeForCube() {
- short[] data = new short[1];
- data[0] = OK;
- if (!writeData(data)) {
- printErrorMessage("Timeout Probing for Cube");
- return false;
- }
- data = readData(1);
- if ((data == null) || (data[0] != OK)) {
- printErrorMessage("No response from cube!");
- return false;
- }
- return true;
- }
-
-
-
- public cubeWorker getAnimationsFromCube() {
- Animation[] animations;
- int animationCount, frameCount;
- short[] data, tmp = new short[1];
-
-
- tmp[0] = 'g';
- if (!writeData(tmp)) {
- printErrorMessage("Timout Command");
- return null;
- }
- data = readData(1);
- if ((data == null) || (data[0] != OK)) {
- printErrorMessage("Response Error");
- return null;
- }
-
-
- data = readData(1);
- if (data == null) {
- printErrorMessage("Response Error");
- return null;
- } else {
- animationCount = data[0];
- }
- tmp[0] = OK;
- if (!writeData(tmp)) {
- printErrorMessage("Timout Response");
- return null;
- }
-
- animations = new Animation[animationCount];
-
-
- for (int a = 0; a < animationCount; a++) {
- Animation currentAnim = new Animation();
-
-
- data = readData(1);
- if (data == null) {
- printErrorMessage("Response Error");
- return null;
- } else {
- frameCount = data[0];
- }
- tmp[0] = OK;
- if (!writeData(tmp)) {
- printErrorMessage("Timout Response");
- return null;
- }
-
-
- for (int f = 0; f < frameCount; f++) {
- AFrame currentFrame = new AFrame();
-
-
- data = readData(1);
- if (data == null) {
- printErrorMessage("Response Error");
- return null;
- } else {
- currentFrame.setTime(data[0]);
- }
- tmp[0] = OK;
- if (!writeData(tmp)) {
- printErrorMessage("Timout Response");
- return null;
- }
-
-
- data = readData(64);
- if (data == null) {
- printErrorMessage("Response Error");
- return null;
- } else {
- currentFrame.setData(data);
- }
- tmp[0] = OK;
- if (!writeData(tmp)) {
- printErrorMessage("Timout Response");
- return null;
- }
-
-
- currentAnim.setFrame(currentFrame, f);
- }
-
-
- animations[a] = currentAnim;
- }
-
- return new cubeWorker(animations, frame);
- }
-
-
-
- public int sendAnimationsToCube(cubeWorker worker) {
- short[] data, tmp = new short[1];
-
-
- tmp[0] = 's';
- if (!writeData(tmp)) {
- printErrorMessage("Timout Command");
- return -1;
- }
- data = readData(1);
- if ((data == null) || (data[0] != OK)) {
- printErrorMessage("Response Error");
- return -1;
- }
-
-
- tmp[0] = (short)worker.size();
- if (!writeData(tmp)) {
- printErrorMessage("Timeout numOfAnimations");
- return -1;
- }
- data = readData(1);
- if ((data == null) || (data[0] != OK)) {
- printErrorMessage("Response Error");
- return -1;
- }
-
-
- for (int a = 0; a < worker.size(); a++) {
-
- tmp[0] = (short)worker.getAnimation(a).size();
- if (!writeData(tmp)) {
- printErrorMessage("Timeout numOfFrames");
- return -1;
- }
- data = readData(1);
- if ((data == null) || (data[0] != OK)) {
- printErrorMessage("Response Error");
- return -1;
- }
-
-
- for (int f = 0; f < worker.getAnimation(a).size(); f++) {
-
- tmp[0] = worker.getAnimation(a).getFrame(f).getTime();
- if (!writeData(tmp)) {
- printErrorMessage("Timeout Frame duration");
- return -1;
- }
- data = readData(1);
- if ((data == null) || (data[0] != OK)) {
- printErrorMessage("Response Error");
- return -1;
- }
-
-
- if (!writeData(worker.getAnimation(a).getFrame(f).getData())) {
- printErrorMessage("Timeout Frame");
- return -1;
- }
- data = readData(1);
- if ((data == null) || (data[0] != OK)) {
- printErrorMessage("Response Error");
- return -1;
- }
- }
- }
-
-
- tmp = new short[4];
- tmp[0] = OK;
- tmp[1] = OK;
- tmp[2] = OK;
- tmp[3] = OK;
- if (!writeData(tmp)) {
- printErrorMessage("Timeout Finish");
- return -1;
- }
- data = readData(1);
- if ((data == null) || (data[0] != OK)) {
- printErrorMessage("Response Error");
- return -1;
- }
- return 0;
- }
-
-
-
- public void closeSerialPort() {
- HelperUtility.closePort();
- }
-
- private void printErrorMessage(String s) {
- System.out.println("SerialHelper: " + s);
- frame.errorMessage("Serial Error", s);
- }
-
- private boolean writeData(short[] data) {
-
- long startdate = (new Date()).getTime();
-
- SerialWriteThread t = new SerialWriteThread(data);
- t.start();
- while (!t.dataWasSent()) {
- if ((new Date()).getTime() >= (startdate + (data.length * 1000))) {
-
- return false;
- }
- }
- return true;
- }
-
- private short[] readData(int length) {
-
- long startdate = (new Date()).getTime();
-
- SerialReadThread t = new SerialReadThread(length);
- t.start();
- while (!t.dataIsReady()) {
- if ((new Date()).getTime() >= (startdate + (length * 1000))) {
-
- return null;
- }
- }
- return t.getSerialData();
- }
- }
|