finished up the fitpc pc side joystick reading code
This meant writing a nice OO api for reading joysticks.
It also involved redoing the wire format to fix byte-order problems and
get rid of the need for packing structs.
diff --git a/aos/common/input/driver_station_data.cc b/aos/common/input/driver_station_data.cc
new file mode 100644
index 0000000..803d46a
--- /dev/null
+++ b/aos/common/input/driver_station_data.cc
@@ -0,0 +1,74 @@
+#include "aos/common/input/driver_station_data.h"
+
+namespace aos {
+namespace input {
+namespace driver_station {
+
+Data::Data() : current_values_(), old_values_() {}
+
+void Data::Update(const NetworkRobotJoysticks &new_values) {
+ old_values_ = current_values_;
+ current_values_ = new_values;
+}
+
+namespace {
+
+bool GetButton(const ButtonLocation location,
+ const NetworkRobotJoysticks &values) {
+ return values.joysticks[location.joystick() - 1].buttons &
+ (1 << (location.number() - 1));
+}
+
+bool GetControlBitValue(const ControlBit bit,
+ const NetworkRobotJoysticks &values) {
+ switch (bit) {
+ case ControlBit::kTestMode:
+ return values.control.test_mode();
+ case ControlBit::kFmsAttached:
+ return values.control.fms_attached();
+ case ControlBit::kAutonomous:
+ return values.control.autonomous();
+ case ControlBit::kEnabled:
+ return values.control.enabled();
+ }
+}
+
+} // namespace
+
+bool Data::IsPressed(const ButtonLocation location) const {
+ return GetButton(location, current_values_);
+}
+
+bool Data::PosEdge(const ButtonLocation location) const {
+ return !GetButton(location, old_values_) &&
+ GetButton(location, current_values_);
+}
+
+bool Data::NegEdge(const ButtonLocation location) const {
+ return GetButton(location, old_values_) &&
+ !GetButton(location, current_values_);
+}
+
+bool Data::GetControlBit(const ControlBit bit) const {
+ return GetControlBitValue(bit, current_values_);
+}
+
+bool Data::PosEdge(const ControlBit bit) const {
+ return !GetControlBitValue(bit, old_values_) &&
+ GetControlBitValue(bit, current_values_);
+}
+
+bool Data::NegEdge(const ControlBit bit) const {
+ return GetControlBitValue(bit, old_values_) &&
+ !GetControlBitValue(bit, current_values_);
+}
+
+float Data::GetAxis(JoystickAxis axis) const {
+ // TODO(brians): check this math against what our joysticks report as their
+ // logical minimums and maximums
+ return current_values_.joysticks[axis.joystick()].axes[axis.number()] / 127.0;
+}
+
+} // namespace driver_station
+} // namespace input
+} // namespace aos
diff --git a/aos/common/input/driver_station_data.h b/aos/common/input/driver_station_data.h
new file mode 100644
index 0000000..41609ed
--- /dev/null
+++ b/aos/common/input/driver_station_data.h
@@ -0,0 +1,83 @@
+#ifndef AOS_COMMON_INPUT_DRIVER_STATION_DATA_H_
+#define AOS_COMMON_INPUT_DRIVER_STATION_DATA_H_
+
+// This file defines several types to support nicely looking at the data
+// received from the driver's station.
+
+#include <memory>
+
+#include "aos/externals/WPILib/WPILib/NetworkRobot/NetworkRobotValues.h"
+
+namespace aos {
+namespace input {
+namespace driver_station {
+
+// Represents a feature of a joystick (a button or an axis).
+// All indices are 1-based.
+class JoystickFeature {
+ public:
+ JoystickFeature(int joystick, int number)
+ : joystick_(joystick), number_(number) {}
+
+ // Which joystick number this is (1-based).
+ int joystick() const { return joystick_; }
+ // Which feature on joystick() this is (1-based).
+ int number() const { return number_; }
+
+ private:
+ const int joystick_, number_;
+};
+
+// Represents the location of a button.
+// Use Data to actually get the value.
+// Safe for static initialization.
+class ButtonLocation : public JoystickFeature {
+ public:
+ ButtonLocation(int joystick, int number)
+ : JoystickFeature(joystick, number) {}
+};
+
+// Represents various bits of control information that the DS sends.
+// Use Data to actually get the value.
+enum class ControlBit {
+ kTestMode, kFmsAttached, kAutonomous, kEnabled
+};
+
+// Represents a single axis of a joystick.
+// Use Data to actually get the value.
+// Safe for static initialization.
+class JoystickAxis : public JoystickFeature {
+ public:
+ JoystickAxis(int joystick, int number)
+ : JoystickFeature(joystick, number) {}
+};
+
+class Data {
+ public:
+ // Initializes the data to all buttons and control bits off and all joysticks
+ // at 0.
+ Data();
+
+ // Updates the current information with a new set of values.
+ void Update(const NetworkRobotJoysticks &new_values);
+
+ bool IsPressed(ButtonLocation location) const;
+ bool PosEdge(ButtonLocation location) const;
+ bool NegEdge(ButtonLocation location) const;
+
+ bool GetControlBit(ControlBit bit) const;
+ bool PosEdge(ControlBit bit) const;
+ bool NegEdge(ControlBit bit) const;
+
+ // Returns the value in the range [-1.0, 1.0].
+ float GetAxis(JoystickAxis axis) const;
+
+ private:
+ NetworkRobotJoysticks current_values_, old_values_;
+};
+
+} // namespace driver_station
+} // namespace input
+} // namespace aos
+
+#endif // AOS_COMMON_INPUT_DRIVER_STATION_DATA_H_
diff --git a/aos/common/input/input.gyp b/aos/common/input/input.gyp
new file mode 100644
index 0000000..e5c964c
--- /dev/null
+++ b/aos/common/input/input.gyp
@@ -0,0 +1,17 @@
+{
+ 'targets': [
+ {
+ 'target_name': 'driver_station_data',
+ 'type': 'static_library',
+ 'sources': [
+ 'driver_station_data.cc',
+ ],
+ 'dependencies': [
+ '<(EXTERNALS):WPILib-NetworkRobotValues',
+ ],
+ 'export_dependent_settings': [
+ '<(EXTERNALS):WPILib-NetworkRobotValues',
+ ],
+ },
+ ],
+}