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 | |
Brian Silverman | c2f62fa | 2013-03-08 15:55:43 -0800 | [diff] [blame] | 27 | protected: |
| 28 | DigitalSource() {} |
| 29 | |
Brian Silverman | 5c98997 | 2013-03-07 22:43:07 -0800 | [diff] [blame] | 30 | private: |
| 31 | DISALLOW_COPY_AND_ASSIGN(DigitalSource); |
| 32 | }; |
| 33 | |
| 34 | class AnalogTriggerOutput : public DigitalSource { |
| 35 | public: |
| 36 | // Defaults for the voltages for AnalogTriggers. They work well for digital |
| 37 | // sensors connected to analog inputs. |
Brian Silverman | 92fb221 | 2013-03-09 19:59:26 -0800 | [diff] [blame] | 38 | static const float kDefaultLowerVoltage = 1.35; |
Brian Silverman | 5c98997 | 2013-03-07 22:43:07 -0800 | [diff] [blame] | 39 | static const float kDefaultUpperVoltage = 4; |
| 40 | |
| 41 | // Will set up the voltages on trigger. |
Brian Silverman | f41afcc | 2013-03-08 21:21:17 -0800 | [diff] [blame] | 42 | // Takes ownership of trigger to make sure it stays around so that the output |
| 43 | // it creates won't blow up (because it holds on to and uses it). |
| 44 | AnalogTriggerOutput(::std::unique_ptr< ::AnalogTrigger> trigger, |
Brian Silverman | 5c98997 | 2013-03-07 22:43:07 -0800 | [diff] [blame] | 45 | ::AnalogTriggerOutput::Type type, |
| 46 | float lowerVoltage = kDefaultLowerVoltage, |
| 47 | float upperVoltage = kDefaultUpperVoltage); |
Brian Silverman | c2f62fa | 2013-03-08 15:55:43 -0800 | [diff] [blame] | 48 | explicit AnalogTriggerOutput(::std::unique_ptr< ::AnalogTriggerOutput> output) |
Brian Silverman | 5c98997 | 2013-03-07 22:43:07 -0800 | [diff] [blame] | 49 | : output_(::std::move(output)) {} |
| 50 | |
| 51 | virtual bool Get() { return output_->Get(); } |
| 52 | virtual ::DigitalSource *source() const { return output_.get(); } |
| 53 | |
| 54 | private: |
Brian Silverman | 75d7ca6 | 2013-03-08 22:00:28 -0800 | [diff] [blame] | 55 | const ::std::unique_ptr< ::AnalogTrigger> trigger_holder_; |
| 56 | |
Brian Silverman | c2f62fa | 2013-03-08 15:55:43 -0800 | [diff] [blame] | 57 | const ::std::unique_ptr< ::AnalogTriggerOutput> output_; |
Brian Silverman | 5c98997 | 2013-03-07 22:43:07 -0800 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | class DigitalInput : public DigitalSource { |
| 61 | public: |
Brian Silverman | c2f62fa | 2013-03-08 15:55:43 -0800 | [diff] [blame] | 62 | explicit DigitalInput(uint32_t channel) |
| 63 | : input_(::std::unique_ptr< ::DigitalInput>( |
| 64 | new ::DigitalInput(channel))) { |
| 65 | } |
| 66 | DigitalInput(uint8_t module, uint32_t channel) |
| 67 | : input_(::std::unique_ptr< ::DigitalInput>( |
| 68 | new ::DigitalInput(module, channel))) { |
| 69 | } |
| 70 | explicit DigitalInput(::std::unique_ptr< ::DigitalInput> input) |
Brian Silverman | 5c98997 | 2013-03-07 22:43:07 -0800 | [diff] [blame] | 71 | : input_(::std::move(input)) {} |
| 72 | |
| 73 | virtual bool Get() { return input_->Get(); } |
| 74 | virtual ::DigitalSource *source() const { return input_.get(); } |
| 75 | |
| 76 | private: |
Brian Silverman | c2f62fa | 2013-03-08 15:55:43 -0800 | [diff] [blame] | 77 | const ::std::unique_ptr< ::DigitalInput> input_; |
Brian Silverman | 5c98997 | 2013-03-07 22:43:07 -0800 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | } // namespace hardware |
| 81 | } // namespace crio |
| 82 | } // namespace aos |
| 83 | |
| 84 | #endif // AOS_CRIO_HARDWARE_DIGITAL_SOURCE_H_ |