Now you can download a copy of these docs so you can use them offline! Download now
Relay.cpp
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2008. All Rights Reserved. */ 00003 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00005 /*----------------------------------------------------------------------------*/ 00006 00007 #include "Relay.h" 00008 00009 #include "DigitalModule.h" 00010 #include "Resource.h" 00011 #include "WPIErrors.h" 00012 00013 // Allocate each direction separately. 00014 static Resource *relayChannels = NULL; 00015 00024 void Relay::InitRelay (UINT8 moduleNumber) 00025 { 00026 char buf[64]; 00027 Resource::CreateResourceObject(&relayChannels, tDIO::kNumSystems * kRelayChannels * 2); 00028 if (!SensorBase::CheckRelayModule(moduleNumber)) 00029 { 00030 snprintf(buf, 64, "Digital Module %d", moduleNumber); 00031 wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf); 00032 return; 00033 } 00034 if (!SensorBase::CheckRelayChannel(m_channel)) 00035 { 00036 snprintf(buf, 64, "Relay Channel %d", m_channel); 00037 wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf); 00038 return; 00039 } 00040 00041 if (m_direction == kBothDirections || m_direction == kForwardOnly) 00042 { 00043 snprintf(buf, 64, "Forward Relay %d (Module: %d)", m_channel, moduleNumber); 00044 if (relayChannels->Allocate(((moduleNumber - 1) * kRelayChannels + m_channel - 1) * 2, buf) == ~0ul) 00045 { 00046 CloneError(relayChannels); 00047 return; 00048 } 00049 } 00050 if (m_direction == kBothDirections || m_direction == kReverseOnly) 00051 { 00052 snprintf(buf, 64, "Reverse Relay %d (Module: %d)", m_channel, moduleNumber); 00053 if (relayChannels->Allocate(((moduleNumber - 1) * kRelayChannels + m_channel - 1) * 2 + 1, buf) == ~0ul) 00054 { 00055 CloneError(relayChannels); 00056 return; 00057 } 00058 } 00059 m_module = DigitalModule::GetInstance(moduleNumber); 00060 m_module->SetRelayForward(m_channel, false); 00061 m_module->SetRelayReverse(m_channel, false); 00062 } 00063 00071 Relay::Relay(UINT8 moduleNumber, UINT32 channel, Relay::Direction direction) 00072 : m_channel (channel) 00073 , m_direction (direction) 00074 { 00075 InitRelay(moduleNumber); 00076 } 00077 00083 Relay::Relay(UINT32 channel, Relay::Direction direction) 00084 : m_channel (channel) 00085 , m_direction (direction) 00086 { 00087 InitRelay(GetDefaultDigitalModule()); 00088 } 00089 00094 Relay::~Relay() 00095 { 00096 m_module->SetRelayForward(m_channel, false); 00097 m_module->SetRelayReverse(m_channel, false); 00098 00099 if (m_direction == kBothDirections || m_direction == kForwardOnly) 00100 { 00101 relayChannels->Free(((m_module->GetNumber() - 1) * kRelayChannels + m_channel - 1) * 2); 00102 } 00103 if (m_direction == kBothDirections || m_direction == kReverseOnly) 00104 { 00105 relayChannels->Free(((m_module->GetNumber() - 1) * kRelayChannels + m_channel - 1) * 2 + 1); 00106 } 00107 } 00108 00123 void Relay::Set(Relay::Value value) 00124 { 00125 if (StatusIsFatal()) return; 00126 switch (value) 00127 { 00128 case kOff: 00129 if (m_direction == kBothDirections || m_direction == kForwardOnly) 00130 { 00131 m_module->SetRelayForward(m_channel, false); 00132 } 00133 if (m_direction == kBothDirections || m_direction == kReverseOnly) 00134 { 00135 m_module->SetRelayReverse(m_channel, false); 00136 } 00137 break; 00138 case kOn: 00139 if (m_direction == kBothDirections || m_direction == kForwardOnly) 00140 { 00141 m_module->SetRelayForward(m_channel, true); 00142 } 00143 if (m_direction == kBothDirections || m_direction == kReverseOnly) 00144 { 00145 m_module->SetRelayReverse(m_channel, true); 00146 } 00147 break; 00148 case kForward: 00149 if (m_direction == kReverseOnly) 00150 { 00151 wpi_setWPIError(IncompatibleMode); 00152 break; 00153 } 00154 if (m_direction == kBothDirections || m_direction == kForwardOnly) 00155 { 00156 m_module->SetRelayForward(m_channel, true); 00157 } 00158 if (m_direction == kBothDirections) 00159 { 00160 m_module->SetRelayReverse(m_channel, false); 00161 } 00162 break; 00163 case kReverse: 00164 if (m_direction == kForwardOnly) 00165 { 00166 wpi_setWPIError(IncompatibleMode); 00167 break; 00168 } 00169 if (m_direction == kBothDirections) 00170 { 00171 m_module->SetRelayForward(m_channel, false); 00172 } 00173 if (m_direction == kBothDirections || m_direction == kReverseOnly) 00174 { 00175 m_module->SetRelayReverse(m_channel, true); 00176 } 00177 break; 00178 } 00179 }
Generated on Thu Jan 12 2012 22:35:23 for WPILibC++ by
1.7.1