Skip to content

Commit

Permalink
Merge pull request #16 from ajayyy/two-way-radio
Browse files Browse the repository at this point in the history
Two way radio
  • Loading branch information
ajayyy authored Apr 17, 2021
2 parents 693e917 + 6b46ca5 commit 88abcf1
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 50 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,7 @@ data
build
builds

# Intellij
.idea
BaseStation.iml

4 changes: 2 additions & 2 deletions src/uorocketry/basestation/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ public float getDecimalValue() {
}
}

public long getDecimalLong() {
public long getLongValue() {
switch (type) {
case LONG:
return dataLong;
default:
return getDecimalLong();
return (long) getDecimalValue();
}
}

Expand Down
44 changes: 9 additions & 35 deletions src/uorocketry/basestation/DataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ public class DataHandler {
*/
int tableIndex = 0;

public DataHandler(int tableIndex) {
private JSONObject datasetConfig;

public DataHandler(int tableIndex, JSONObject datasetConfig) {
this.tableIndex = tableIndex;
this.datasetConfig = datasetConfig;

this.data = new Data[Main.dataLength.get(tableIndex)];

Expand All @@ -59,52 +62,23 @@ public void updateTableUIWithData(JTable table, String[] labels) {
String dataText = data[i].getFormattedString();
if (hiddenDataTypes.contains(types[i])) dataText = "Hidden Data";

// Hardcode for now TODO: Move this into config
if (i == data.length - 1 && labels[i].toLowerCase().contains("state")) {
switch (dataText) {
case "0":
dataText = "Init";
break;
case "1":
dataText = "Wait For Init";
break;
case "2":
dataText = "Wait For Launch";
break;
case "3":
dataText = "Powered Flight";
break;
case "4":
dataText = "Coast";
break;
case "5":
dataText = "Descent Phase 1";
break;
case "6":
dataText = "Descent Phase 2";
break;
case "7":
dataText = "Ground";
break;
case "8":
dataText = "Max States";
break;
}
if (i == data.length - 1 && datasetConfig.getInt("stateIndex") == i) {
dataText = datasetConfig.getJSONArray("states").getString((int) data[i].getDecimalValue());
}

// Set data
tableModel.setValueAt(dataText, i, 1);
}
}

public boolean set(int index, String currentData, JSONObject dataset) {
public boolean set(int index, String currentData) {
// Check for special cases first
boolean isFormattedCoordinate = false;
boolean isTimestamp = false;
try {
isTimestamp = dataset.getInt("timestampIndex") == index;
isTimestamp = datasetConfig.getInt("timestampIndex") == index;

JSONObject coordinateIndexes = dataset.getJSONObject("coordinateIndexes");
JSONObject coordinateIndexes = datasetConfig.getJSONObject("coordinateIndexes");
isFormattedCoordinate = coordinateIndexes.has("formattedCoordinates")
&& coordinateIndexes.getBoolean("formattedCoordinates")
&& (coordinateIndexes.getInt("latitude") == index || coordinateIndexes.getInt("longitude") == index);
Expand Down
29 changes: 29 additions & 0 deletions src/uorocketry/basestation/Helper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package uorocketry.basestation;

import org.json.JSONArray;

public class Helper {

/**
*
* @param array
* @return
*/
public static int[] toIntArray(JSONArray array) {
int[] result = new int[array.length()];

for (int i = 0; i < array.length(); i++) {
result[i] = array.getInt(i);
}

return result;
}

public static boolean arrayIncludes(int[] array, int value) {
for (int i = 0; i < array.length; i++) {
if (array[i] == value) return true;
}

return false;
}
}
19 changes: 13 additions & 6 deletions src/uorocketry/basestation/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -437,6 +436,15 @@ private void updateUIInternal() {
} else {
setTableToError(i, window.dataTables.get(i));
}

if (window.stateButtons.size() > i) {
try {
int stateIndex = config.getJSONArray("datasets").getJSONObject(i).getInt("stateIndex");
for (StateButton stateButton: window.stateButtons.get(i)) {
stateButton.stateChanged((int) currentDataHandler.data[stateIndex].getDecimalValue());
}
} catch (JSONException e) {}
}
}

if (googleEarth) {
Expand Down Expand Up @@ -633,7 +641,7 @@ public void loadSimulationData(int index, String fileName) {
}

public DataHandler parseData(String data, int tableIndex) {
DataHandler dataHandler = new DataHandler(tableIndex);
DataHandler dataHandler = new DataHandler(tableIndex, config.getJSONArray("datasets").getJSONObject(tableIndex));

// Clear out the b' ' stuff added that is only meant for the radio to see
data = data.replaceAll("b'|\\\\r\\\\n'", "");
Expand All @@ -658,9 +666,8 @@ public DataHandler parseData(String data, int tableIndex) {
}
} catch (NumberFormatException | JSONException e) {}

JSONArray dataSets = config.getJSONArray("datasets");
for (int i = 0; i < splitData.length; i++) {
if (!dataHandler.set(i, splitData[i], dataSets.getJSONObject(tableIndex))) {
if (!dataHandler.set(i, splitData[i])) {
System.err.println("Failed to set data handler");

// Parsing failed
Expand Down Expand Up @@ -824,9 +831,9 @@ public void actionPerformed(ActionEvent e) {
updateUI();
}
} else if (e.getSource() == window.hideComSelectorButton) {
window.sidePanel.setVisible(!window.sidePanel.isVisible());
window.comPanelParent.setVisible(!window.comPanelParent.isVisible());

if (window.sidePanel.isVisible()) {
if (window.comPanelParent.isVisible()) {
window.hideComSelectorButton.setText("Hide Com Selector");
} else {
window.hideComSelectorButton.setText("Show Com Selector");
Expand Down
92 changes: 92 additions & 0 deletions src/uorocketry/basestation/StateButton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package uorocketry.basestation;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.Border;

import org.json.JSONArray;

import com.fazecast.jSerialComm.SerialPort;

/**
* DOES NOT support multiple data sources
* (Hardcoded, fixable)
*
* @author Ajay
*
*/
public class StateButton implements ActionListener {

// Always zero for now
private static final int TABLE_INDEX = 0;

private static final Color AVAILABLE_COLOR = new Color(0, 33, 115);
private static final Color SUCCESS_COLOR = new Color(3, 176, 0);
private static final Color INACTIVE_COLOR = new Color(79, 79, 79);

String name;
/** What data to send */
byte[] data;
/** Which states can this action be completed from */
int[] availableStates;
/** Which states means this button has been complete */
int[] successStates;

private JButton button;
private JPanel borderPanel;
private Border defaultButtonBorder;

private List<SerialPort> activeSerialPorts;

public StateButton(List<SerialPort> activeSerialPorts, String name, String data, JSONArray successStates, JSONArray availableStates) {
this.activeSerialPorts = activeSerialPorts;

this.name = name;
this.data = data.getBytes();
this.successStates = Helper.toIntArray(successStates);
this.availableStates = Helper.toIntArray(availableStates);

button = new JButton(name);
button.addActionListener(this);
button.setFont(new Font("Arial", Font.PLAIN, 20));
defaultButtonBorder = button.getBorder();

borderPanel = new JPanel();
borderPanel.setBorder(BorderFactory.createTitledBorder(name));
borderPanel.add(button);
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
SerialPort serialPort = activeSerialPorts.get(TABLE_INDEX);
if (serialPort != null) {
serialPort.writeBytes(data, data.length);
}
}
}

public void stateChanged(int newState) {
if (Helper.arrayIncludes(availableStates, newState)) {
button.setForeground(AVAILABLE_COLOR);
} else if (Helper.arrayIncludes(successStates, newState)) {
button.setForeground(SUCCESS_COLOR);

} else {
button.setForeground(INACTIVE_COLOR);

}
}

public JPanel getPanel() {
return borderPanel;
}

}
46 changes: 39 additions & 7 deletions src/uorocketry/basestation/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import javax.swing.border.TitledBorder;
import javax.swing.JSplitPane;
Expand Down Expand Up @@ -72,12 +74,14 @@ public class Window extends JFrame {
JButton latestButton;
JLabel savingToLabel;

private List<JPanel> comPanels = new ArrayList<>();
List<JList<String>> comSelectors = new ArrayList<>();
List<JLabel> comConnectionSuccessLabels = new ArrayList<>();

Vector<String> comSelectorData = new Vector<String>();
public JPanel sidePanel;
public JPanel comPanelParent;
private List<JPanel> comPanels = new ArrayList<>();
public List<JList<String>> comSelectors = new ArrayList<>();
public List<JLabel> comConnectionSuccessLabels = new ArrayList<>();

public JPanel stateSendingPanel;
public List<List<StateButton>> stateButtons = new ArrayList<>();

JPanel centerChartPanel;

Expand Down Expand Up @@ -235,12 +239,40 @@ public Window(Main main) {

sidePanel = new JPanel();
getContentPane().add(sidePanel, BorderLayout.EAST);
sidePanel.setLayout(new GridLayout(2, 1, 0, 0));
sidePanel.setLayout(new BorderLayout());

comPanelParent = new JPanel();
sidePanel.add(comPanelParent, BorderLayout.SOUTH);
comPanelParent.setLayout(new GridLayout(2, 1, 0, 0));

for (int i = 0; i < Main.dataSourceCount; i++) {
addComSelectorPanel();
}

try {
JSONArray array = main.config.getJSONArray("stateEvents");

if (array.length() > 0) {
stateSendingPanel = new JPanel();
sidePanel.add(stateSendingPanel, BorderLayout.NORTH);
stateSendingPanel.setLayout(new BoxLayout(stateSendingPanel, BoxLayout.Y_AXIS));
}

List<StateButton> buttons = new ArrayList<StateButton>(array.length());
stateButtons.add(buttons);

for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
StateButton stateButton = new StateButton(main.activeSerialPorts, object.getString("name"), object.getString("data"), object.getJSONArray("successStates"), object.getJSONArray("availableStates"));

stateSendingPanel.add(stateButton.getPanel());
buttons.add(stateButton);
}
} catch (JSONException e) {
// No states then
stateSendingPanel.setVisible(false);
}

centerChartPanel = new JPanel();

splitPane.setRightComponent(centerChartPanel);
Expand Down Expand Up @@ -318,7 +350,7 @@ public void addComSelectorPanel() {
comConnectionSuccessLabel.setOpaque(true);
comPanel.add(comConnectionSuccessLabel);

sidePanel.add(comPanel);
comPanelParent.add(comPanel);

comPanels.add(comPanel);
comSelectors.add(comSelector);
Expand Down

0 comments on commit 88abcf1

Please sign in to comment.