blob: 37b2c6f3f6422ecc459a0b1d296dec75c8185ace [file] [log] [blame]
Brian Silverman5c989972013-03-07 22:43:07 -08001#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
13namespace aos {
14namespace crio {
15namespace hardware {
16
17// Wrapper for WPILib's class of the same name. Provides an actual Get()
18// function and makes creating analog ones easier.
19class 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 Silvermanc2f62fa2013-03-08 15:55:43 -080027 protected:
28 DigitalSource() {}
29
Brian Silverman5c989972013-03-07 22:43:07 -080030 private:
31 DISALLOW_COPY_AND_ASSIGN(DigitalSource);
32};
33
34class AnalogTriggerOutput : public DigitalSource {
35 public:
36 // Defaults for the voltages for AnalogTriggers. They work well for digital
37 // sensors connected to analog inputs.
Brian Silverman92fb2212013-03-09 19:59:26 -080038 static const float kDefaultLowerVoltage = 1.35;
Brian Silverman5c989972013-03-07 22:43:07 -080039 static const float kDefaultUpperVoltage = 4;
40
41 // Will set up the voltages on trigger.
Brian Silvermanf41afcc2013-03-08 21:21:17 -080042 // 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 Silverman5c989972013-03-07 22:43:07 -080045 ::AnalogTriggerOutput::Type type,
46 float lowerVoltage = kDefaultLowerVoltage,
47 float upperVoltage = kDefaultUpperVoltage);
Brian Silvermanc2f62fa2013-03-08 15:55:43 -080048 explicit AnalogTriggerOutput(::std::unique_ptr< ::AnalogTriggerOutput> output)
Brian Silverman5c989972013-03-07 22:43:07 -080049 : output_(::std::move(output)) {}
50
51 virtual bool Get() { return output_->Get(); }
52 virtual ::DigitalSource *source() const { return output_.get(); }
53
54 private:
Brian Silverman75d7ca62013-03-08 22:00:28 -080055 const ::std::unique_ptr< ::AnalogTrigger> trigger_holder_;
56
Brian Silvermanc2f62fa2013-03-08 15:55:43 -080057 const ::std::unique_ptr< ::AnalogTriggerOutput> output_;
Brian Silverman5c989972013-03-07 22:43:07 -080058};
59
60class DigitalInput : public DigitalSource {
61 public:
Brian Silvermanc2f62fa2013-03-08 15:55:43 -080062 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 Silverman5c989972013-03-07 22:43:07 -080071 : input_(::std::move(input)) {}
72
73 virtual bool Get() { return input_->Get(); }
74 virtual ::DigitalSource *source() const { return input_.get(); }
75
76 private:
Brian Silvermanc2f62fa2013-03-08 15:55:43 -080077 const ::std::unique_ptr< ::DigitalInput> input_;
Brian Silverman5c989972013-03-07 22:43:07 -080078};
79
80} // namespace hardware
81} // namespace crio
82} // namespace aos
83
84#endif // AOS_CRIO_HARDWARE_DIGITAL_SOURCE_H_