blob: 2a09023596d0d850da05276747b7fddf0f5d0e22 [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#include "SensorBase.h"
8
9#include "NetworkCommunication/LoadOut.h"
10#include "WPIErrors.h"
11
12const UINT32 SensorBase::kSystemClockTicksPerMicrosecond;
13const UINT32 SensorBase::kDigitalChannels;
14const UINT32 SensorBase::kAnalogChannels;
15const UINT32 SensorBase::kAnalogModules;
16const UINT32 SensorBase::kDigitalModules;
17const UINT32 SensorBase::kSolenoidChannels;
18const UINT32 SensorBase::kSolenoidModules;
19const UINT32 SensorBase::kPwmChannels;
20const UINT32 SensorBase::kRelayChannels;
21const UINT32 SensorBase::kChassisSlots;
22SensorBase *SensorBase::m_singletonList = NULL;
23
24/**
25 * Creates an instance of the sensor base and gets an FPGA handle
26 */
27SensorBase::SensorBase()
28{
29}
30
31/**
32 * Frees the resources for a SensorBase.
33 */
34SensorBase::~SensorBase()
35{
36}
37
38/**
39 * Add sensor to the singleton list.
40 * Add this sensor to the list of singletons that need to be deleted when
41 * the robot program exits. Each of the sensors on this list are singletons,
42 * that is they aren't allocated directly with new, but instead are allocated
43 * by the static GetInstance method. As a result, they are never deleted when
44 * the program exits. Consequently these sensors may still be holding onto
45 * resources and need to have their destructors called at the end of the program.
46 */
47void SensorBase::AddToSingletonList()
48{
49 m_nextSingleton = m_singletonList;
50 m_singletonList = this;
51}
52
53/**
54 * Delete all the singleton classes on the list.
55 * All the classes that were allocated as singletons need to be deleted so
56 * their resources can be freed.
57 */
58void SensorBase::DeleteSingletons()
59{
60 for (SensorBase *next = m_singletonList; next != NULL;)
61 {
62 SensorBase *tmp = next;
63 next = next->m_nextSingleton;
64 delete tmp;
65 }
66 m_singletonList = NULL;
67}
68
69/**
70 * Check that the analog module number is valid.
71 *
72 * @return Analog module is valid and present
73 */
74bool SensorBase::CheckAnalogModule(UINT8 moduleNumber)
75{
76 if (nLoadOut::getModulePresence(nLoadOut::kModuleType_Analog, moduleNumber - 1))
77 return true;
78 return false;
79}
80
81/**
82 * Check that the digital module number is valid.
83 *
84 * @return Digital module is valid and present
85 */
86bool SensorBase::CheckDigitalModule(UINT8 moduleNumber)
87{
88 if (nLoadOut::getModulePresence(nLoadOut::kModuleType_Digital, moduleNumber - 1))
89 return true;
90 return false;
91}
92
93/**
94 * Check that the digital module number is valid.
95 *
96 * @return Digital module is valid and present
97 */
98bool SensorBase::CheckPWMModule(UINT8 moduleNumber)
99{
100 return CheckDigitalModule(moduleNumber);
101}
102
103/**
104 * Check that the digital module number is valid.
105 *
106 * @return Digital module is valid and present
107 */
108bool SensorBase::CheckRelayModule(UINT8 moduleNumber)
109{
110 return CheckDigitalModule(moduleNumber);
111}
112
113/**
114 * Check that the solenoid module number is valid.
115 *
116 * @return Solenoid module is valid and present
117 */
118bool SensorBase::CheckSolenoidModule(UINT8 moduleNumber)
119{
120 if (nLoadOut::getModulePresence(nLoadOut::kModuleType_Solenoid, moduleNumber - 1))
121 return true;
122 return false;
123}
124
125/**
126 * Check that the digital channel number is valid.
127 * Verify that the channel number is one of the legal channel numbers. Channel numbers are
128 * 1-based.
129 *
130 * @return Digital channel is valid
131 */
132bool SensorBase::CheckDigitalChannel(UINT32 channel)
133{
134 if (channel > 0 && channel <= kDigitalChannels)
135 return true;
136 return false;
137}
138
139/**
140 * Check that the digital channel number is valid.
141 * Verify that the channel number is one of the legal channel numbers. Channel numbers are
142 * 1-based.
143 *
144 * @return Relay channel is valid
145 */
146bool SensorBase::CheckRelayChannel(UINT32 channel)
147{
148 if (channel > 0 && channel <= kRelayChannels)
149 return true;
150 return false;
151}
152
153/**
154 * Check that the digital channel number is valid.
155 * Verify that the channel number is one of the legal channel numbers. Channel numbers are
156 * 1-based.
157 *
158 * @return PWM channel is valid
159 */
160bool SensorBase::CheckPWMChannel(UINT32 channel)
161{
162 if (channel > 0 && channel <= kPwmChannels)
163 return true;
164 return false;
165}
166
167/**
168 * Check that the analog channel number is value.
169 * Verify that the analog channel number is one of the legal channel numbers. Channel numbers
170 * are 1-based.
171 *
172 * @return Analog channel is valid
173 */
174bool SensorBase::CheckAnalogChannel(UINT32 channel)
175{
176 if (channel > 0 && channel <= kAnalogChannels)
177 return true;
178 return false;
179}
180
181/**
182 * Verify that the solenoid channel number is within limits.
183 *
184 * @return Solenoid channel is valid
185 */
186bool SensorBase::CheckSolenoidChannel(UINT32 channel)
187{
188 if (channel > 0 && channel <= kSolenoidChannels)
189 return true;
190 return false;
191}
192