Now you can download a copy of these docs so you can use them offline! Download now
DoubleSolenoid.cpp
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 "DoubleSolenoid.h"
8 #include "NetworkCommunication/UsageReporting.h"
9 #include "WPIErrors.h"
10 #include <string.h>
11 #include "LiveWindow/LiveWindow.h"
12 
16 void DoubleSolenoid::InitSolenoid()
17 {
18  m_table = NULL;
19  char buf[64];
21  {
22  snprintf(buf, 64, "Solenoid Module %d", m_moduleNumber);
23  wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf);
24  return;
25  }
26  if (!CheckSolenoidChannel(m_forwardChannel))
27  {
28  snprintf(buf, 64, "Solenoid Channel %d", m_forwardChannel);
29  wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf);
30  return;
31  }
32  if (!CheckSolenoidChannel(m_reverseChannel))
33  {
34  snprintf(buf, 64, "Solenoid Channel %d", m_reverseChannel);
35  wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf);
36  return;
37  }
38  Resource::CreateResourceObject(&m_allocated, tSolenoid::kNumDO7_0Elements * kSolenoidChannels);
39 
40  snprintf(buf, 64, "Solenoid %d (Module %d)", m_forwardChannel, m_moduleNumber);
41  if (m_allocated->Allocate((m_moduleNumber - 1) * kSolenoidChannels + m_forwardChannel - 1, buf) == ~0ul)
42  {
43  CloneError(m_allocated);
44  return;
45  }
46  snprintf(buf, 64, "Solenoid %d (Module %d)", m_reverseChannel, m_moduleNumber);
47  if (m_allocated->Allocate((m_moduleNumber - 1) * kSolenoidChannels + m_reverseChannel - 1, buf) == ~0ul)
48  {
49  CloneError(m_allocated);
50  return;
51  }
52  m_forwardMask = 1 << (m_forwardChannel - 1);
53  m_reverseMask = 1 << (m_reverseChannel - 1);
54 
55  nUsageReporting::report(nUsageReporting::kResourceType_Solenoid, m_forwardChannel, m_moduleNumber - 1);
56  nUsageReporting::report(nUsageReporting::kResourceType_Solenoid, m_reverseChannel, m_moduleNumber - 1);
57  LiveWindow::GetInstance()->AddActuator("DoubleSolenoid", m_moduleNumber, m_forwardChannel, this);
58 }
59 
66 DoubleSolenoid::DoubleSolenoid(uint32_t forwardChannel, uint32_t reverseChannel)
67  : SolenoidBase (GetDefaultSolenoidModule())
68  , m_forwardChannel (forwardChannel)
69  , m_reverseChannel (reverseChannel)
70 {
71  InitSolenoid();
72 }
73 
81 DoubleSolenoid::DoubleSolenoid(uint8_t moduleNumber, uint32_t forwardChannel, uint32_t reverseChannel)
82  : SolenoidBase (moduleNumber)
83  , m_forwardChannel (forwardChannel)
84  , m_reverseChannel (reverseChannel)
85 {
86  InitSolenoid();
87 }
88 
93 {
95  {
96  m_allocated->Free((m_moduleNumber - 1) * kSolenoidChannels + m_forwardChannel - 1);
97  m_allocated->Free((m_moduleNumber - 1) * kSolenoidChannels + m_reverseChannel - 1);
98  }
99 }
100 
106 void DoubleSolenoid::Set(Value value)
107 {
108  if (StatusIsFatal()) return;
109  uint8_t rawValue = 0x00;
110 
111  switch(value)
112  {
113  case kOff:
114  rawValue = 0x00;
115  break;
116  case kForward:
117  rawValue = m_forwardMask;
118  break;
119  case kReverse:
120  rawValue = m_reverseMask;
121  break;
122  }
123 
124  SolenoidBase::Set(rawValue, m_forwardMask | m_reverseMask);
125 }
126 
132 DoubleSolenoid::Value DoubleSolenoid::Get()
133 {
134  if (StatusIsFatal()) return kOff;
135  uint8_t value = GetAll();
136 
137  if (value & m_forwardMask) return kForward;
138  if (value & m_reverseMask) return kReverse;
139  return kOff;
140 }
141 
142 void DoubleSolenoid::ValueChanged(ITable* source, const std::string& key, EntryValue value, bool isNew) {
143  Value lvalue = kOff;
144  std::string *val = (std::string *)value.ptr;
145  if (*val == "Forward")
146  lvalue = kForward;
147  else if (*val == "Reverse")
148  lvalue = kReverse;
149  Set(lvalue);
150 }
151 
153  if (m_table != NULL) {
154  m_table->PutString("Value", (Get() == kForward ? "Forward" : (Get() == kReverse ? "Reverse" : "Off")));
155  }
156 }
157 
159  Set(kOff);
160  if (m_table != NULL) {
161  m_table->AddTableListener("Value", this, true);
162  }
163 }
164 
166  Set(kOff);
167  if (m_table != NULL) {
168  m_table->RemoveTableListener(this);
169  }
170 }
171 
173  return "Double Solenoid";
174 }
175 
177  m_table = subTable;
178  UpdateTable();
179 }
180 
182  return m_table;
183 }
184 
virtual void RemoveTableListener(ITableListener *listener)=0
void AddActuator(const char *subsystem, const char *name, LiveWindowSendable *component)
Definition: LiveWindow.cpp:96
DoubleSolenoid(uint32_t forwardChannel, uint32_t reverseChannel)
uint32_t m_moduleNumber
Slot number where the module is plugged into the chassis.
Definition: SolenoidBase.h:29
Definition: ITable.h:26
virtual void AddTableListener(ITableListener *listener)=0
ITable * GetTable()
void InitTable(ITable *subTable)
std::string GetSmartDashboardType()
void Free(uint32_t index)
Definition: Resource.cpp:105
uint32_t Allocate(const char *resourceDesc)
Definition: Resource.cpp:62
void StartLiveWindowMode()
void ValueChanged(ITable *source, const std::string &key, EntryValue value, bool isNew)
virtual void Set(Value value)
static LiveWindow * GetInstance()
Definition: LiveWindow.cpp:13
virtual ~DoubleSolenoid()
virtual bool StatusIsFatal() const
Check if the current error code represents a fatal error.
Definition: ErrorBase.cpp:178
static bool CheckSolenoidModule(uint8_t moduleNumber)
Definition: SensorBase.cpp:118
static void CreateResourceObject(Resource **r, uint32_t elements)
Definition: Resource.cpp:39
uint8_t GetAll()
virtual Value Get()
void Set(uint8_t value, uint8_t mask)
Definition: ITable.h:13
static bool CheckSolenoidChannel(uint32_t channel)
Definition: SensorBase.cpp:186
virtual void PutString(std::string key, std::string value)=0

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6