123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
-
-
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.StringTokenizer;
- import java.util.Collections;
-
-
-
- public class cubeWorker {
-
- private final int framesRemaining = 2016;
- private boolean changedState = false;
- private Frame parentFrame;
- private Animation[] animations = new Animation[1];
-
-
-
- public cubeWorker(Frame parent) {
- animations[0] = new Animation();
- parentFrame = parent;
- }
-
- public cubeWorker(Animation[] anims, Frame parent) {
- animations = anims;
- parentFrame = parent;
- }
-
-
-
- public int completeNumOfFrames() {
- int c = 0;
- for (int i = 0; i < size(); i++) {
- c += getAnimation(i).size();
- }
- return c;
- }
-
-
-
-
-
-
-
- public int size() {
- return animations.length;
- }
-
-
-
- public int memoryRemaining() {
- return framesRemaining - completeNumOfFrames();
- }
-
-
-
- public int addAnimation() {
- changedState = true;
- if (memoryRemaining() <= 0) {
- return -1;
- } else {
- extendArray();
- animations[animations.length - 1] = new Animation();
- return animations.length - 1;
- }
- }
-
-
-
- public void removeAnimation(int i) {
- changedState = true;
- shiftOver(i);
- shrinkArray();
- }
-
-
-
- public void moveAnimationUp(int i) {
- if (i > 0) {
- Animation tmp = animations[i];
- animations[i] = animations[i - 1];
- animations[i - 1] = tmp;
- }
- }
-
-
-
- public void moveAnimationDown(int i) {
- if (i < (animations.length - 1)) {
- Animation tmp = animations[i];
- animations[i] = animations[i + 1];
- animations[i + 1] = tmp;
- }
- }
-
- public Animation getAnimation(int i) {
- if (i < animations.length) {
- return animations[i];
- } else {
- return null;
- }
- }
-
- public void setAnimation(Animation a, int i) {
- changedState = true;
- if (i < animations.length) {
- animations[i] = a;
- }
- }
-
-
-
- public int loadState(String path) {
- changedState = false;
- try {
- animations = AnimationUtility.readFile(path);
- } catch (Exception e) {
- System.out.println("Did not load!");
- e.printStackTrace(System.out);
- return -1;
- }
- int size = 0;
- for (int i = 0; i < animations.length; i++) {
- size += animations[i].size();
- }
- if (size > framesRemaining) {
- return -1;
- }
- return 0;
- }
-
-
-
- public int saveState(String path) {
- changedState = false;
- AnimationUtility.writeFile(path, animations);
- if (AnimationUtility.getLastError() != null) {
- System.out.println(AnimationUtility.getLastError());
- return -1;
- }
- return 0;
- }
-
-
-
- public boolean changedStateSinceSave() {
- return changedState;
- }
-
-
-
- public Animation[] getAnimationArray() {
- return animations;
- }
-
-
-
- public int cubeSendState(String port) {
- int a, f;
- short[] d;
-
- if (HelperUtility.openPort(port) == false) {
- error("Could not open Port!");
- return -1;
- }
-
-
- d = new short[1];
- d[0] = 's';
- if (HelperUtility.writeData(d, 1) == false) {
- error("Could not write to port!");
- HelperUtility.closePort();
- return -1;
- }
-
-
- d = HelperUtility.readData(1);
- if (d.length == 0) {
- error("Could not read from port!");
- HelperUtility.closePort();
- return -1;
- }
- if (d[0] != 0x42) {
- error("Cube not OK!");
- HelperUtility.closePort();
- return -1;
- }
-
- System.out.println("Command sent!");
-
-
- d = new short[1];
- if (animations.length >= 255) {
- error("Too many animations");
- return -1;
- }
- d[0] = (short)animations.length;
- if (HelperUtility.writeData(d, 1) == false) {
- error("Could not write to port!");
- HelperUtility.closePort();
- return -1;
- }
-
-
- d = HelperUtility.readData(1);
- if (d.length == 0) {
- error("Could not read from port!");
- HelperUtility.closePort();
- return -1;
- }
- if (d[0] != 0x42) {
- error("Cube not OK!");
- HelperUtility.closePort();
- return -1;
- }
-
- System.out.println("Animation count sent (" + animations.length + ")!");
-
- for (a = 0; a < animations.length; a++) {
-
- d = new short[1];
- if (animations[a].size() >= 255) {
- error("Too many frames!");
- HelperUtility.closePort();
- return -1;
- }
- d[0] = (short)animations[a].size();
- if (HelperUtility.writeData(d, 1) == false) {
- error("Could not write to port!");
- HelperUtility.closePort();
- return -1;
- }
-
-
- d = HelperUtility.readData(1);
- if (d.length == 0) {
- error("Could not read from port!");
- HelperUtility.closePort();
- return -1;
- }
- if (d[0] != 0x42) {
- error("Cube not OK!");
- HelperUtility.closePort();
- return -1;
- }
-
- System.out.println("Frame count sent (" + animations[a].size() + ")!");
-
- for (f = 0; f < animations[a].size(); f++) {
-
- d = new short[1];
- d[0] = animations[a].getFrame(f).getTime();
- if (HelperUtility.writeData(d, 1) == false) {
- error("Could not write to port!");
- HelperUtility.closePort();
- return -1;
- }
-
-
- d = HelperUtility.readData(1);
- if (d.length == 0) {
- error("Could not read from port!");
- HelperUtility.closePort();
- return -1;
- }
- if (d[0] != 0x42) {
- error("Cube not OK!");
- HelperUtility.closePort();
- return -1;
- }
-
- System.out.println("Duration sent (" + animations[a].getFrame(f).getTime() + ")!");
-
-
- d = animations[a].getFrame(f).getData();
- if (HelperUtility.writeData(d, 64) == false) {
- error("Could not write to port!");
- HelperUtility.closePort();
- return -1;
- }
-
-
- d = HelperUtility.readData(1);
- if (d.length == 0) {
- error("Could not read from port!");
- HelperUtility.closePort();
- return -1;
- }
- if (d[0] != 0x42) {
- error("Cube not OK!");
- HelperUtility.closePort();
- return -1;
- }
-
- System.out.println("Data sent");
- }
- }
-
-
- d = new short[4];
- d[0] = 0x42;
- d[1] = 0x42;
- d[2] = 0x42;
- d[3] = 0x42;
- if (HelperUtility.writeData(d, 4) == false) {
- error("Could not write to port!");
- HelperUtility.closePort();
- return -1;
- }
-
-
- d = HelperUtility.readData(1);
- if (d.length == 0) {
- error("Could not read from port!");
- HelperUtility.closePort();
- return -1;
- }
- if (d[0] != 0x42) {
- error("Cube not OK!");
- HelperUtility.closePort();
- return -1;
- }
-
- System.out.println("Uploaded animations!");
-
- return 0;
- }
-
-
-
-
- public int cubeGetState(String port) {
- return -1;
- }
-
-
-
- public boolean cubeProbeConnected(String port) {
- if (HelperUtility.openPort(port) == false) {
- error("Could not open Port!");
- return false;
- }
-
- short[] d = new short[1];
- d[0] = 0x42;
- if (HelperUtility.writeData(d, 1) == false) {
- error("Could not write to port!");
- HelperUtility.closePort();
- return false;
- }
-
- d = HelperUtility.readData(1);
- if (d.length == 0) {
- error("Could not read from port!");
- HelperUtility.closePort();
- return false;
- }
-
- HelperUtility.closePort();
-
- if (d[0] != 0x42) {
- error("Answer was not OK");
- return false;
- } else {
- System.out.println("Got probe response!");
- return true;
- }
-
- }
-
- private void error(String s) {
- System.out.println(s);
- parentFrame.errorMessage("Serial Error", s);
- }
-
- private void extendArray() {
- Animation newArray[] = new Animation[animations.length + 1];
- for (int i = 0; i < animations.length; i++) {
- newArray[i] = animations[i];
- }
- animations = newArray;
- }
-
- private void shrinkArray() {
- Animation newArray[] = new Animation[animations.length - 1];
- for (int i = 0; i < newArray.length; i++) {
- newArray[i] = animations[i];
- }
- animations = newArray;
- }
-
- private void shiftOver(int toForget) {
- for (int i = (toForget + 1); i < animations.length; i++) {
- animations[i - 1] = animations[i];
- }
- }
- }
|