Brian Silverman | 5c98997 | 2013-03-07 22:43:07 -0800 | [diff] [blame] | 1 | #ifndef AOS_CRIO_HARDWARE_DIGITAL_SOURCE_H_ |
| 2 | #define AOS_CRIO_HARDWARE_DIGITAL_SOURCE_H_ |
| 3 | |
| 4 | #include "aos/common/libstdc++/memory" |
| 5 | |
| 6 | #include "WPILib/DigitalSource.h" |
| 7 | #include "WPILib/DigitalInput.h" |
| 8 | #include "WPILib/AnalogTrigger.h" |
| 9 | #include "WPILib/AnalogTriggerOutput.h" |
| 10 | |
| 11 | #include "aos/common/macros.h" |
| 12 | |
| 13 | namespace aos { |
| 14 | namespace crio { |
| 15 | namespace hardware { |
| 16 | |
| 17 | // Wrapper for WPILib's class of the same name. Provides an actual Get() |
| 18 | // function and makes creating analog ones easier. |
| 19 | class DigitalSource { |
| 20 | public: |
| 21 | virtual ~DigitalSource() {} |
| 22 | |
| 23 | virtual bool Get() = 0; |
| 24 | // This object maintains ownership. |
| 25 | virtual ::DigitalSource *source() const = 0; |
| 26 | |
| 27 | private: |
| 28 | DISALLOW_COPY_AND_ASSIGN(DigitalSource); |
| 29 | }; |
| 30 | |
| 31 | class AnalogTriggerOutput : public DigitalSource { |
| 32 | public: |
| 33 | // Defaults for the voltages for AnalogTriggers. They work well for digital |
| 34 | // sensors connected to analog inputs. |
| 35 | static const float kDefaultLowerVoltage = 1; |
| 36 | static const float kDefaultUpperVoltage = 4; |
| 37 | |
| 38 | // Will set up the voltages on trigger. |
| 39 | AnalogTriggerOutput(const ::AnalogTrigger &trigger, |
| 40 | ::AnalogTriggerOutput::Type type, |
| 41 | float lowerVoltage = kDefaultLowerVoltage, |
| 42 | float upperVoltage = kDefaultUpperVoltage); |
| 43 | AnalogTriggerOutput(::std::unique_ptr<::AnalogTriggerOutput> output) |
| 44 | : output_(::std::move(output)) {} |
| 45 | |
| 46 | virtual bool Get() { return output_->Get(); } |
| 47 | virtual ::DigitalSource *source() const { return output_.get(); } |
| 48 | |
| 49 | private: |
| 50 | const ::std::unique_ptr<::AnalogTriggerOutput> output_; |
| 51 | }; |
| 52 | |
| 53 | class DigitalInput : public DigitalSource { |
| 54 | public: |
| 55 | DigitalInput(::std::unique_ptr<::DigitalInput> input) |
| 56 | : input_(::std::move(input)) {} |
| 57 | |
| 58 | virtual bool Get() { return input_->Get(); } |
| 59 | virtual ::DigitalSource *source() const { return input_.get(); } |
| 60 | |
| 61 | private: |
| 62 | const ::std::unique_ptr<::DigitalInput> input_; |
| 63 | }; |
| 64 | |
| 65 | } // namespace hardware |
| 66 | } // namespace crio |
| 67 | } // namespace aos |
| 68 | |
| 69 | #endif // AOS_CRIO_HARDWARE_DIGITAL_SOURCE_H_ |