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