Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 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 "WPIErrors.h" |
| 10 | |
| 11 | const uint32_t SensorBase::kDigitalChannels; |
| 12 | const uint32_t SensorBase::kAnalogInputs; |
| 13 | const uint32_t SensorBase::kSolenoidChannels; |
| 14 | const uint32_t SensorBase::kSolenoidModules; |
| 15 | const uint32_t SensorBase::kPwmChannels; |
| 16 | const uint32_t SensorBase::kRelayChannels; |
| 17 | const uint32_t SensorBase::kPDPChannels; |
| 18 | const uint32_t SensorBase::kChassisSlots; |
| 19 | SensorBase *SensorBase::m_singletonList = nullptr; |
| 20 | |
| 21 | /** |
| 22 | * Creates an instance of the sensor base and gets an FPGA handle |
| 23 | */ |
| 24 | SensorBase::SensorBase() |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Add sensor to the singleton list. |
| 30 | * Add this sensor to the list of singletons that need to be deleted when |
| 31 | * the robot program exits. Each of the sensors on this list are singletons, |
| 32 | * that is they aren't allocated directly with new, but instead are allocated |
| 33 | * by the static GetInstance method. As a result, they are never deleted when |
| 34 | * the program exits. Consequently these sensors may still be holding onto |
| 35 | * resources and need to have their destructors called at the end of the program. |
| 36 | */ |
| 37 | void SensorBase::AddToSingletonList() |
| 38 | { |
| 39 | m_nextSingleton = m_singletonList; |
| 40 | m_singletonList = this; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Delete all the singleton classes on the list. |
| 45 | * All the classes that were allocated as singletons need to be deleted so |
| 46 | * their resources can be freed. |
| 47 | */ |
| 48 | void SensorBase::DeleteSingletons() |
| 49 | { |
| 50 | for (SensorBase *next = m_singletonList; next != nullptr;) |
| 51 | { |
| 52 | SensorBase *tmp = next; |
| 53 | next = next->m_nextSingleton; |
| 54 | delete tmp; |
| 55 | } |
| 56 | m_singletonList = nullptr; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Check that the solenoid module number is valid. |
| 61 | * |
| 62 | * @return Solenoid module is valid and present |
| 63 | */ |
| 64 | bool SensorBase::CheckSolenoidModule(uint8_t moduleNumber) |
| 65 | { |
| 66 | return 1 <= moduleNumber && moduleNumber <= 2; // TODO: Fix for Athena |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Check that the digital channel number is valid. |
| 71 | * Verify that the channel number is one of the legal channel numbers. Channel numbers are |
| 72 | * 1-based. |
| 73 | * |
| 74 | * @return Digital channel is valid |
| 75 | */ |
| 76 | bool SensorBase::CheckDigitalChannel(uint32_t channel) |
| 77 | { |
| 78 | if (channel > 0 && channel <= kDigitalChannels) |
| 79 | return true; |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Check that the digital channel number is valid. |
| 85 | * Verify that the channel number is one of the legal channel numbers. Channel numbers are |
| 86 | * 1-based. |
| 87 | * |
| 88 | * @return Relay channel is valid |
| 89 | */ |
| 90 | bool SensorBase::CheckRelayChannel(uint32_t channel) |
| 91 | { |
| 92 | if (channel > 0 && channel <= kRelayChannels) |
| 93 | return true; |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Check that the digital channel number is valid. |
| 99 | * Verify that the channel number is one of the legal channel numbers. Channel numbers are |
| 100 | * 1-based. |
| 101 | * |
| 102 | * @return PWM channel is valid |
| 103 | */ |
| 104 | bool SensorBase::CheckPWMChannel(uint32_t channel) |
| 105 | { |
| 106 | if (channel > 0 && channel <= kPwmChannels) |
| 107 | return true; |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Check that the analog input number is valid. |
| 113 | * Verify that the analog input number is one of the legal channel numbers. Channel numbers |
| 114 | * are 1-based. |
| 115 | * |
| 116 | * @return Analog channel is valid |
| 117 | */ |
| 118 | bool SensorBase::CheckAnalogInput(uint32_t channel) |
| 119 | { |
| 120 | if (channel > 0 && channel <= kAnalogInputs) |
| 121 | return true; |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Check that the analog output number is valid. |
| 127 | * Verify that the analog output number is one of the legal channel numbers. Channel numbers |
| 128 | * are 1-based. |
| 129 | * |
| 130 | * @return Analog channel is valid |
| 131 | */ |
| 132 | bool SensorBase::CheckAnalogOutput(uint32_t channel) |
| 133 | { |
| 134 | if (channel > 0 && channel <= kAnalogOutputs) |
| 135 | return true; |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Verify that the solenoid channel number is within limits. |
| 141 | * |
| 142 | * @return Solenoid channel is valid |
| 143 | */ |
| 144 | bool SensorBase::CheckSolenoidChannel(uint32_t channel) |
| 145 | { |
| 146 | if (channel > 0 && channel <= kSolenoidChannels) |
| 147 | return true; |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Verify that the power distribution channel number is within limits. |
| 153 | * |
| 154 | * @return PDP channel is valid |
| 155 | */ |
| 156 | bool SensorBase::CheckPDPChannel(uint32_t channel) |
| 157 | { |
| 158 | if (channel > 0 && channel <= kPDPChannels) |
| 159 | return true; |
| 160 | return false; |
| 161 | } |