blob: 965b8c5966d1bcf97b3ce4f14358362cc48fa771 [file] [log] [blame]
jerrymf1579332013-02-07 01:56:28 +00001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
5/*----------------------------------------------------------------------------*/
6
7#ifndef CPPCOUNTER_H_
8#define CPPCOUNTER_H_
9
10#include "AnalogTriggerOutput.h"
11#include "CounterBase.h"
12#include "SensorBase.h"
13#include "LiveWindow/LiveWindowSendable.h"
14
15/**
16 * Class for counting the number of ticks on a digital input channel.
17 * This is a general purpose class for counting repetitive events. It can return the number
18 * of counts, the period of the most recent cycle, and detect when the signal being counted
19 * has stopped by supplying a maximum cycle time.
20 */
21class Counter : public SensorBase, public CounterBase, public LiveWindowSendable
22{
23public:
24 typedef enum {kTwoPulse=0, kSemiperiod=1, kPulseLength=2, kExternalDirection=3} Mode;
25
26 Counter();
27 explicit Counter(UINT32 channel);
28 Counter(UINT8 moduleNumber, UINT32 channel);
29 explicit Counter(DigitalSource *source);
30 explicit Counter(DigitalSource &source);
31 explicit Counter(AnalogTrigger *trigger);
32 explicit Counter(AnalogTrigger &trigger);
33 Counter(EncodingType encodingType, DigitalSource *upSource, DigitalSource *downSource, bool inverted);
34 virtual ~Counter();
35
36 void SetUpSource(UINT32 channel);
37 void SetUpSource(UINT8 moduleNumber, UINT32 channel);
38 void SetUpSource(AnalogTrigger *analogTrigger, AnalogTriggerOutput::Type triggerType);
39 void SetUpSource(AnalogTrigger &analogTrigger, AnalogTriggerOutput::Type triggerType);
40 void SetUpSource(DigitalSource *source);
41 void SetUpSource(DigitalSource &source);
42 void SetUpSourceEdge(bool risingEdge, bool fallingEdge);
43 void ClearUpSource();
44
45 void SetDownSource(UINT32 channel);
46 void SetDownSource(UINT8 moduleNumber, UINT32 channel);
47 void SetDownSource(AnalogTrigger *analogTrigger, AnalogTriggerOutput::Type triggerType);
48 void SetDownSource(AnalogTrigger &analogTrigger, AnalogTriggerOutput::Type triggerType);
49 void SetDownSource(DigitalSource *source);
50 void SetDownSource(DigitalSource &source);
51 void SetDownSourceEdge(bool risingEdge, bool fallingEdge);
52 void ClearDownSource();
53
54 void SetUpDownCounterMode();
55 void SetExternalDirectionMode();
56 void SetSemiPeriodMode(bool highSemiPeriod);
57 void SetPulseLengthMode(float threshold);
58
59 void SetReverseDirection(bool reverseDirection);
60
61 // CounterBase interface
62 void Start();
63 INT32 Get();
64 void Reset();
65 void Stop();
66 double GetPeriod();
67 void SetMaxPeriod(double maxPeriod);
68 void SetUpdateWhenEmpty(bool enabled);
69 bool GetStopped();
70 bool GetDirection();
71 UINT32 GetIndex() {return m_index;}
72
73
74 void UpdateTable();
75 void StartLiveWindowMode();
76 void StopLiveWindowMode();
77 virtual std::string GetSmartDashboardType();
78 void InitTable(ITable *subTable);
79 ITable * GetTable();
80
81private:
82 void InitCounter(Mode mode = kTwoPulse);
83
84 DigitalSource *m_upSource; ///< What makes the counter count up.
85 DigitalSource *m_downSource; ///< What makes the counter count down.
86 bool m_allocatedUpSource; ///< Was the upSource allocated locally?
87 bool m_allocatedDownSource; ///< Was the downSource allocated locally?
88 tCounter *m_counter; ///< The FPGA counter object.
89 UINT32 m_index; ///< The index of this counter.
90
91 ITable *m_table;
92};
93
94#endif