blob: 643f2b2f93825e99b4574a122fbe0448274697a4 [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -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
13#include "AnalogTrigger.h"
14#include "CounterBase.h"
15#include "HAL/Counter.h"
16#include "HAL/Types.h"
17#include "LiveWindow/LiveWindowSendable.h"
18#include "SensorBase.h"
19
20namespace frc {
21
22class DigitalGlitchFilter;
23
24/**
25 * Class for counting the number of ticks on a digital input channel.
26 * This is a general purpose class for counting repetitive events. It can return
27 * the number of counts, the period of the most recent cycle, and detect when
28 * the signal being counted has stopped by supplying a maximum cycle time.
29 *
30 * All counters will immediately start counting - Reset() them if you need them
31 * to be zeroed before use.
32 */
33class Counter : public SensorBase,
34 public CounterBase,
35 public LiveWindowSendable {
36 public:
37 enum Mode {
38 kTwoPulse = 0,
39 kSemiperiod = 1,
40 kPulseLength = 2,
41 kExternalDirection = 3
42 };
43 explicit Counter(Mode mode = kTwoPulse);
44 explicit Counter(int channel);
45 explicit Counter(DigitalSource* source);
46 explicit Counter(std::shared_ptr<DigitalSource> source);
47 WPI_DEPRECATED("Use pass-by-reference instead.")
48 explicit Counter(AnalogTrigger* trigger);
49 explicit Counter(const AnalogTrigger& trigger);
50 Counter(EncodingType encodingType, DigitalSource* upSource,
51 DigitalSource* downSource, bool inverted);
52 Counter(EncodingType encodingType, std::shared_ptr<DigitalSource> upSource,
53 std::shared_ptr<DigitalSource> downSource, bool inverted);
54 virtual ~Counter();
55
56 void SetUpSource(int channel);
57 void SetUpSource(AnalogTrigger* analogTrigger, AnalogTriggerType triggerType);
58 void SetUpSource(std::shared_ptr<AnalogTrigger> analogTrigger,
59 AnalogTriggerType triggerType);
60 void SetUpSource(DigitalSource* source);
61 void SetUpSource(std::shared_ptr<DigitalSource> source);
62 void SetUpSource(DigitalSource& source);
63 void SetUpSourceEdge(bool risingEdge, bool fallingEdge);
64 void ClearUpSource();
65
66 void SetDownSource(int channel);
67 void SetDownSource(AnalogTrigger* analogTrigger,
68 AnalogTriggerType triggerType);
69 void SetDownSource(std::shared_ptr<AnalogTrigger> analogTrigger,
70 AnalogTriggerType triggerType);
71 void SetDownSource(DigitalSource* source);
72 void SetDownSource(std::shared_ptr<DigitalSource> source);
73 void SetDownSource(DigitalSource& source);
74 void SetDownSourceEdge(bool risingEdge, bool fallingEdge);
75 void ClearDownSource();
76
77 void SetUpDownCounterMode();
78 void SetExternalDirectionMode();
79 void SetSemiPeriodMode(bool highSemiPeriod);
80 void SetPulseLengthMode(double threshold);
81
82 void SetReverseDirection(bool reverseDirection);
83
84 // CounterBase interface
85 int Get() const override;
86 void Reset() override;
87 double GetPeriod() const override;
88 void SetMaxPeriod(double maxPeriod) override;
89 void SetUpdateWhenEmpty(bool enabled);
90 bool GetStopped() const override;
91 bool GetDirection() const override;
92
93 void SetSamplesToAverage(int samplesToAverage);
94 int GetSamplesToAverage() const;
95 int GetFPGAIndex() const { return m_index; }
96
97 void UpdateTable() override;
98 void StartLiveWindowMode() override;
99 void StopLiveWindowMode() override;
100 std::string GetSmartDashboardType() const override;
101 void InitTable(std::shared_ptr<ITable> subTable) override;
102 std::shared_ptr<ITable> GetTable() const override;
103
104 protected:
105 // Makes the counter count up.
106 std::shared_ptr<DigitalSource> m_upSource;
107 // Makes the counter count down.
108 std::shared_ptr<DigitalSource> m_downSource;
109 // The FPGA counter object
110 HAL_CounterHandle m_counter = HAL_kInvalidHandle;
111
112 private:
113 int m_index = 0; ///< The index of this counter.
114
115 std::shared_ptr<ITable> m_table;
116 friend class DigitalGlitchFilter;
117};
118
119} // namespace frc