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