-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from ajayyy/two-way-radio
Two way radio
- Loading branch information
Showing
7 changed files
with
188 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,3 +111,7 @@ data | |
build | ||
builds | ||
|
||
# Intellij | ||
.idea | ||
BaseStation.iml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters