Blame view

PercTeacher/Sources/ApplicationJava/ActionList.java 3.77 KB
c5abccad   pfrison   PercTeacher inter...
1
2
3
4
  import java.util.ArrayList;
  
  public class ActionList {
  	private ArrayList<Action> actions;
04949080   pfrison   PercTeacher stabl...
5
  	private SerialCom serialCom;
c5abccad   pfrison   PercTeacher inter...
6
  	
04949080   pfrison   PercTeacher stabl...
7
8
  	public ActionList(SerialCom serialCom) {
  		this.serialCom = serialCom;
c5abccad   pfrison   PercTeacher inter...
9
10
11
  		actions = new ArrayList<>();
  		actions.add(new Action());
  	}
04949080   pfrison   PercTeacher stabl...
12
  	public ActionList(int[] leftDeltaArray, int[] rightDeltaArray, long[] leftDelaiArray, long[] rightDelaiArray) {
c5abccad   pfrison   PercTeacher inter...
13
14
15
16
17
18
19
20
21
22
  		if(leftDelaiArray.length != rightDelaiArray.length
  				|| leftDelaiArray.length != leftDelaiArray.length
  				|| leftDelaiArray.length != rightDelaiArray.length)
  			throw new InvalidActionListArrays();
  		actions = new ArrayList<>();
  		for(int i=0; i<leftDeltaArray.length; i++)
  			actions.add(new Action(leftDeltaArray[i], rightDeltaArray[i], leftDelaiArray[i], rightDelaiArray[i]));
  		actions.add(new Action());
  	}
  	
04949080   pfrison   PercTeacher stabl...
23
  	public void addToActionList(int i, int j, int step) {
c5abccad   pfrison   PercTeacher inter...
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
  		switch (j * 10 + i) {
  		case 00: //ACTION_LEFT_PLUS;
  			addToLastAction(step, 0);
  			break;
  		case 02: //ACTION_LEFT_MINUS;
  			addToLastAction(-step, 0);
  			break;
  		case 20: //ACTION_RIGHT_PLUS;
  			addToLastAction(0, step);
  			break;
  		case 22: //ACTION_RIGHT_MINUS;
  			addToLastAction(0, -step);
  			break;
  		case 10: //ACTION_FORWARD;
  			addToLastAction(step, step);
  			break;
  		case 12: //ACTION_BACKWARD;
  			addToLastAction(-step, -step);
  			break;
  		case 01: //ACTION_CLOCKWISE;
  			addToLastAction(step, -step);
  			break;
  		case 21: //ACTION_ANTI_CLOCKWISE;
  			addToLastAction(-step, step);
  			break;
  		default:
  			throw new InvalidInputException();
  		}
  	}
  	
  	public void completeAction() {
  		if(!actions.get(actions.size() - 1).isEmpty())
  			actions.add(new Action());
  	}
  	
  	public void completeActionAndAddPause(long delai) {
  		if(actions.get(actions.size() - 1).isEmpty())
  			actions.set(actions.size() - 1, new Action(0, 0, delai, delai));
  		else
  			actions.add(new Action(0, 0, delai, delai));
  		completeAction();
  	}
  	
04949080   pfrison   PercTeacher stabl...
67
  	private void addToLastAction(int deltaL, int deltaR) {
c5abccad   pfrison   PercTeacher inter...
68
69
  		actions.get(actions.size() - 1).addDeltaL(deltaL);
  		actions.get(actions.size() - 1).addDeltaR(deltaR);
04949080   pfrison   PercTeacher stabl...
70
  		SerialCommands.sendDeltas(serialCom, deltaL, deltaR);
c5abccad   pfrison   PercTeacher inter...
71
72
73
74
75
  	}
  	
  	public void revertLastAction() {
  		if(actions.size() <= 0)
  			return;
04949080   pfrison   PercTeacher stabl...
76
77
  
  		Action toRevert = null;
c5abccad   pfrison   PercTeacher inter...
78
79
  		if(actions.get(actions.size() - 1).isEmpty()) {
  			actions.remove(actions.size() - 1);
04949080   pfrison   PercTeacher stabl...
80
81
  			if(actions.size() != 0) {
  				toRevert = actions.get(actions.size() - 1);
c5abccad   pfrison   PercTeacher inter...
82
  				actions.set(actions.size() - 1, new Action());
04949080   pfrison   PercTeacher stabl...
83
  			}
c5abccad   pfrison   PercTeacher inter...
84
85
86
  			else
  				actions.add(new Action());
  		} else {
04949080   pfrison   PercTeacher stabl...
87
  			toRevert = actions.get(actions.size() - 1);
c5abccad   pfrison   PercTeacher inter...
88
89
  			actions.set(actions.size() - 1, new Action());
  		}
04949080   pfrison   PercTeacher stabl...
90
91
  		if(toRevert != null)
  			SerialCommands.sendDeltas(serialCom, -toRevert.getDeltaL(), -toRevert.getDeltaR());
c5abccad   pfrison   PercTeacher inter...
92
93
94
95
96
97
  	}
  	
  	public String getLeftDeltaArray() {
  		String str = "{";
  		for(Action action : actions) {
  			if(!action.isEmpty())
04949080   pfrison   PercTeacher stabl...
98
  				str += String.valueOf(action.getDeltaL()) + ", ";
c5abccad   pfrison   PercTeacher inter...
99
100
101
102
103
104
105
106
  		}
  		str = str.substring(0, str.length() - 2) + "}";
  		return str;
  	}
  	public String getRightDeltaArray() {
  		String str = "{";
  		for(Action action : actions) {
  			if(!action.isEmpty())
04949080   pfrison   PercTeacher stabl...
107
  				str += String.valueOf(action.getDeltaR()) + ", ";
c5abccad   pfrison   PercTeacher inter...
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
  		}
  		str = str.substring(0, str.length() - 2) + "}";
  		return str;
  	}
  	public String getLeftDelaiArray() {
  		String str = "{";
  		for(Action action : actions) {
  			if(!action.isEmpty())
  				str += String.valueOf(action.getDeltaR()) + "L, ";
  		}
  		str = str.substring(0, str.length() - 2) + "}";
  		return str;
  	}
  	public String getRightDelaiArray() {
  		String str = "{";
  		for(Action action : actions) {
  			if(!action.isEmpty())
  				str += String.valueOf(action.getDeltaR()) + "L, ";
  		}
  		str = str.substring(0, str.length() - 2) + "}";
  		return str;
  	}
  	
  	@Override
  	public String toString() {
  		String str = "";
  		for(Action action : actions)
  			str += action.toString() + "\n";
cd5544f2   pfrison   PercTeacher added...
136
  		str = str.substring(0, str.length() - 1); // remove last \n
c5abccad   pfrison   PercTeacher inter...
137
138
139
  		return str;
  	}
  }