Now you can download a copy of these docs so you can use them offline! Download now
DigitalOutput.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 "DigitalOutput.h" 00008 #include "DigitalModule.h" 00009 #include "Resource.h" 00010 #include "WPIErrors.h" 00011 00012 extern Resource *interruptsResource; 00013 00019 void DigitalOutput::InitDigitalOutput(UINT8 moduleNumber, UINT32 channel) 00020 { 00021 char buf[64]; 00022 if (!CheckDigitalModule(moduleNumber)) 00023 { 00024 snprintf(buf, 64, "Digital Module %d", moduleNumber); 00025 wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf); 00026 return; 00027 } 00028 if (!CheckDigitalChannel(channel)) 00029 { 00030 snprintf(buf, 64, "Digital Channel %d", channel); 00031 wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf); 00032 return; 00033 } 00034 m_channel = channel; 00035 m_pwmGenerator = ~0ul; 00036 m_module = DigitalModule::GetInstance(moduleNumber); 00037 m_module->AllocateDIO(m_channel, false); 00038 } 00039 00046 DigitalOutput::DigitalOutput(UINT32 channel) 00047 { 00048 InitDigitalOutput(GetDefaultDigitalModule(), channel); 00049 } 00050 00058 DigitalOutput::DigitalOutput(UINT8 moduleNumber, UINT32 channel) 00059 { 00060 InitDigitalOutput(moduleNumber, channel); 00061 } 00062 00066 DigitalOutput::~DigitalOutput() 00067 { 00068 if (StatusIsFatal()) return; 00069 // Disable the PWM in case it was running. 00070 DisablePWM(); 00071 m_module->FreeDIO(m_channel); 00072 } 00073 00078 void DigitalOutput::Set(UINT32 value) 00079 { 00080 if (StatusIsFatal()) return; 00081 m_module->SetDIO(m_channel, value); 00082 } 00083 00087 UINT32 DigitalOutput::GetChannel() 00088 { 00089 return m_channel; 00090 } 00091 00098 void DigitalOutput::Pulse(float length) 00099 { 00100 if (StatusIsFatal()) return; 00101 m_module->Pulse(m_channel, length); 00102 } 00103 00108 bool DigitalOutput::IsPulsing() 00109 { 00110 if (StatusIsFatal()) return false; 00111 return m_module->IsPulsing(m_channel); 00112 } 00113 00123 void DigitalOutput::SetPWMRate(float rate) 00124 { 00125 if (StatusIsFatal()) return; 00126 m_module->SetDO_PWMRate(rate); 00127 } 00128 00141 void DigitalOutput::EnablePWM(float initialDutyCycle) 00142 { 00143 if (StatusIsFatal()) return; 00144 if (m_pwmGenerator != ~0ul) return; 00145 m_pwmGenerator = m_module->AllocateDO_PWM(); 00146 m_module->SetDO_PWMDutyCycle(m_pwmGenerator, initialDutyCycle); 00147 m_module->SetDO_PWMOutputChannel(m_pwmGenerator, m_channel); 00148 } 00149 00155 void DigitalOutput::DisablePWM() 00156 { 00157 if (StatusIsFatal()) return; 00158 // Disable the output by routing to a dead bit. 00159 m_module->SetDO_PWMOutputChannel(m_pwmGenerator, kDigitalChannels); 00160 m_module->FreeDO_PWM(m_pwmGenerator); 00161 m_pwmGenerator = ~0ul; 00162 } 00163 00172 void DigitalOutput::UpdateDutyCycle(float dutyCycle) 00173 { 00174 if (StatusIsFatal()) return; 00175 m_module->SetDO_PWMDutyCycle(m_pwmGenerator, dutyCycle); 00176 } 00177 00181 UINT32 DigitalOutput::GetChannelForRouting() 00182 { 00183 return DigitalModule::RemapDigitalChannel(GetChannel() - 1); 00184 } 00185 00189 UINT32 DigitalOutput::GetModuleForRouting() 00190 { 00191 if (StatusIsFatal()) return 0; 00192 return m_module->GetNumber() - 1; 00193 } 00194 00198 bool DigitalOutput::GetAnalogTriggerForRouting() 00199 { 00200 return false; 00201 } 00202 00211 void DigitalOutput::RequestInterrupts(tInterruptHandler handler, void *param) 00212 { 00213 if (StatusIsFatal()) return; 00214 UINT32 index = interruptsResource->Allocate("Sync Interrupt"); 00215 if (index == ~0ul) 00216 { 00217 CloneError(interruptsResource); 00218 return; 00219 } 00220 m_interruptIndex = index; 00221 00222 // Creates a manager too 00223 AllocateInterrupts(false); 00224 00225 tRioStatusCode localStatus = NiFpga_Status_Success; 00226 m_interrupt->writeConfig_WaitForAck(false, &localStatus); 00227 m_interrupt->writeConfig_Source_AnalogTrigger(GetAnalogTriggerForRouting(), &localStatus); 00228 m_interrupt->writeConfig_Source_Channel(GetChannelForRouting(), &localStatus); 00229 m_interrupt->writeConfig_Source_Module(GetModuleForRouting(), &localStatus); 00230 SetUpSourceEdge(true, false); 00231 00232 m_manager->registerHandler(handler, param, &localStatus); 00233 wpi_setError(localStatus); 00234 } 00235 00242 void DigitalOutput::RequestInterrupts() 00243 { 00244 if (StatusIsFatal()) return; 00245 UINT32 index = interruptsResource->Allocate("Sync Interrupt"); 00246 if (index == ~0ul) 00247 { 00248 CloneError(interruptsResource); 00249 return; 00250 } 00251 m_interruptIndex = index; 00252 00253 AllocateInterrupts(true); 00254 00255 tRioStatusCode localStatus = NiFpga_Status_Success; 00256 m_interrupt->writeConfig_Source_AnalogTrigger(GetAnalogTriggerForRouting(), &localStatus); 00257 m_interrupt->writeConfig_Source_Channel(GetChannelForRouting(), &localStatus); 00258 m_interrupt->writeConfig_Source_Module(GetModuleForRouting(), &localStatus); 00259 SetUpSourceEdge(true, false); 00260 wpi_setError(localStatus); 00261 } 00262 00263 void DigitalOutput::SetUpSourceEdge(bool risingEdge, bool fallingEdge) 00264 { 00265 if (StatusIsFatal()) return; 00266 if (m_interrupt == NULL) 00267 { 00268 wpi_setWPIErrorWithContext(NullParameter, "You must call RequestInterrupts before SetUpSourceEdge"); 00269 return; 00270 } 00271 tRioStatusCode localStatus = NiFpga_Status_Success; 00272 if (m_interrupt != NULL) 00273 { 00274 m_interrupt->writeConfig_RisingEdge(risingEdge, &localStatus); 00275 m_interrupt->writeConfig_FallingEdge(fallingEdge, &localStatus); 00276 } 00277 wpi_setError(localStatus); 00278 } 00279
Generated on Thu Jan 12 2012 22:35:19 for WPILibC++ by
1.7.1