blob: 26ae7a20bdbb67df69e40954c4c1a79fb4353b60 [file] [log] [blame]
Parker Schuhd3b7a8872018-02-19 16:42:27 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008-2017. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#pragma once
9
10#include <memory>
11#include <string>
12
Austin Schuhf6b94632019-02-02 22:11:27 -080013#include "hal/Counter.h"
14#include "hal/Types.h"
Parker Schuhd3b7a8872018-02-19 16:42:27 -080015#include "frc971/wpilib/ahal/AnalogTrigger.h"
16#include "frc971/wpilib/ahal/CounterBase.h"
17#include "frc971/wpilib/ahal/SensorBase.h"
18
19namespace frc {
20
21class DigitalGlitchFilter;
22
23/**
24 * Class for counting the number of ticks on a digital input channel.
25 * This is a general purpose class for counting repetitive events. It can return
26 * the number of counts, the period of the most recent cycle, and detect when
27 * the signal being counted has stopped by supplying a maximum cycle time.
28 *
29 * All counters will immediately start counting - Reset() them if you need them
30 * to be zeroed before use.
31 */
32class Counter : public CounterBase {
33 public:
34 enum Mode {
35 kTwoPulse = 0,
36 kSemiperiod = 1,
37 kPulseLength = 2,
38 kExternalDirection = 3
39 };
40 explicit Counter(Mode mode = kTwoPulse);
41 explicit Counter(int channel);
42 explicit Counter(DigitalSource *source);
43 explicit Counter(std::shared_ptr<DigitalSource> source);
44 explicit Counter(const AnalogTrigger &trigger);
45 Counter(EncodingType encodingType, DigitalSource *upSource,
46 DigitalSource *downSource, bool inverted);
47 Counter(EncodingType encodingType, std::shared_ptr<DigitalSource> upSource,
48 std::shared_ptr<DigitalSource> downSource, bool inverted);
49 virtual ~Counter();
50
51 void SetUpSource(int channel);
52 void SetUpSource(AnalogTrigger *analogTrigger, AnalogTriggerType triggerType);
53 void SetUpSource(std::shared_ptr<AnalogTrigger> analogTrigger,
54 AnalogTriggerType triggerType);
55 void SetUpSource(DigitalSource *source);
56 void SetUpSource(std::shared_ptr<DigitalSource> source);
57 void SetUpSource(DigitalSource &source);
58 void SetUpSourceEdge(bool risingEdge, bool fallingEdge);
59 void ClearUpSource();
60
61 void SetDownSource(int channel);
62 void SetDownSource(AnalogTrigger *analogTrigger,
63 AnalogTriggerType triggerType);
64 void SetDownSource(std::shared_ptr<AnalogTrigger> analogTrigger,
65 AnalogTriggerType triggerType);
66 void SetDownSource(DigitalSource *source);
67 void SetDownSource(std::shared_ptr<DigitalSource> source);
68 void SetDownSource(DigitalSource &source);
69 void SetDownSourceEdge(bool risingEdge, bool fallingEdge);
70 void ClearDownSource();
71
72 void SetUpDownCounterMode();
73 void SetExternalDirectionMode();
74 void SetSemiPeriodMode(bool highSemiPeriod);
75 void SetPulseLengthMode(double threshold);
76
77 void SetReverseDirection(bool reverseDirection);
78
79 // CounterBase interface
80 int Get() const override;
81 void Reset() override;
82 double GetPeriod() const override;
83 void SetMaxPeriod(double maxPeriod) override;
84 void SetUpdateWhenEmpty(bool enabled);
85 bool GetStopped() const override;
86 bool GetDirection() const override;
87
88 void SetSamplesToAverage(int samplesToAverage);
89 int GetSamplesToAverage() const;
90 int GetFPGAIndex() const { return m_index; }
91
92 protected:
93 // Makes the counter count up.
94 std::shared_ptr<DigitalSource> m_upSource;
95 // Makes the counter count down.
96 std::shared_ptr<DigitalSource> m_downSource;
97 // The FPGA counter object
98 HAL_CounterHandle m_counter = HAL_kInvalidHandle;
99
100 private:
101 int m_index = 0; ///< The index of this counter.
102
103 friend class DigitalGlitchFilter;
104};
105
106} // namespace frc