blob: ea875055e733af1d3f6a43e83aff7a2261cabe62 [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 "SolenoidBase.h"
8
9// Needs to be global since the protected resource spans all Solenoid objects.
10ReentrantSemaphore SolenoidBase::m_semaphore;
11Resource *SolenoidBase::m_allocated = NULL;
12
13tSolenoid *SolenoidBase::m_fpgaSolenoidModule = NULL;
14UINT32 SolenoidBase::m_refCount = 0;
15
16
17/**
18 * Constructor
19 *
20 * @param moduleNumber The solenoid module (1 or 2).
21 */
22SolenoidBase::SolenoidBase(UINT8 moduleNumber)
23 : m_moduleNumber (moduleNumber)
24{
25 Synchronized sync(m_semaphore);
26 m_refCount++;
27 if (m_refCount == 1)
28 {
29 tRioStatusCode localStatus = NiFpga_Status_Success;
30 m_fpgaSolenoidModule = tSolenoid::create(&localStatus);
31 wpi_setError(localStatus);
32 }
33}
34
35/**
36 * Destructor.
37 */
38SolenoidBase::~SolenoidBase()
39{
40 Synchronized sync(m_semaphore);
41 if (CheckSolenoidModule(m_moduleNumber))
42 {
43 if (m_refCount == 1)
44 {
45 delete m_fpgaSolenoidModule;
46 m_fpgaSolenoidModule = NULL;
47 }
48 m_refCount--;
49 }
50}
51
52/**
53 * Set the value of a solenoid.
54 *
55 * @param value The value you want to set on the module.
56 * @param mask The channels you want to be affected.
57 */
58void SolenoidBase::Set(UINT8 value, UINT8 mask)
59{
60 tRioStatusCode localStatus = NiFpga_Status_Success;
61 if (CheckSolenoidModule(m_moduleNumber))
62 {
63 Synchronized sync(m_semaphore);
64 UINT8 currentValue = m_fpgaSolenoidModule->readDO7_0(m_moduleNumber - 1, &localStatus);
65 // Zero out the values to change
66 currentValue = currentValue & ~mask;
67 currentValue = currentValue | (value & mask);
68 m_fpgaSolenoidModule->writeDO7_0(m_moduleNumber - 1, currentValue, &localStatus);
69 }
70 wpi_setError(localStatus);
71}
72
73/**
74 * Read all 8 solenoids as a single byte
75 *
76 * @return The current value of all 8 solenoids on the module.
77 */
78UINT8 SolenoidBase::GetAll()
79{
80 if (CheckSolenoidModule(m_moduleNumber))
81 {
82 tRioStatusCode localStatus = NiFpga_Status_Success;
83 UINT8 solenoids = m_fpgaSolenoidModule->readDO7_0(m_moduleNumber - 1, &localStatus);
84 wpi_setError(localStatus);
85 return solenoids;
86 }
87 return 0;
88}