Now you can download a copy of these docs so you can use them offline! Download now
Solenoid.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 "Solenoid.h"
8 #include "NetworkCommunication/UsageReporting.h"
9 #include "WPIErrors.h"
10 #include "LiveWindow/LiveWindow.h"
11 
15 void Solenoid::InitSolenoid()
16 {
17  m_table = NULL;
18  char buf[64];
20  {
21  snprintf(buf, 64, "Solenoid Module %d", m_moduleNumber);
22  wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf);
23  return;
24  }
25  if (!CheckSolenoidChannel(m_channel))
26  {
27  snprintf(buf, 64, "Solenoid Channel %d", m_channel);
28  wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf);
29  return;
30  }
31  Resource::CreateResourceObject(&m_allocated, tSolenoid::kNumDO7_0Elements * kSolenoidChannels);
32 
33  snprintf(buf, 64, "Solenoid %d (Module: %d)", m_channel, m_moduleNumber);
34  if (m_allocated->Allocate((m_moduleNumber - 1) * kSolenoidChannels + m_channel - 1, buf) == ~0ul)
35  {
36  CloneError(m_allocated);
37  return;
38  }
39 
40  LiveWindow::GetInstance()->AddActuator("Solenoid", m_moduleNumber, m_channel, this);
41  nUsageReporting::report(nUsageReporting::kResourceType_Solenoid, m_channel, m_moduleNumber - 1);
42 }
43 
49 Solenoid::Solenoid(uint32_t channel)
50  : SolenoidBase (GetDefaultSolenoidModule())
51  , m_channel (channel)
52 {
53  InitSolenoid();
54 }
55 
62 Solenoid::Solenoid(uint8_t moduleNumber, uint32_t channel)
63  : SolenoidBase (moduleNumber)
64  , m_channel (channel)
65 {
66  InitSolenoid();
67 }
68 
73 {
75  {
76  m_allocated->Free((m_moduleNumber - 1) * kSolenoidChannels + m_channel - 1);
77  }
78 }
79 
85 void Solenoid::Set(bool on)
86 {
87  if (StatusIsFatal()) return;
88  uint8_t value = on ? 0xFF : 0x00;
89  uint8_t mask = 1 << (m_channel - 1);
90 
91  SolenoidBase::Set(value, mask);
92 }
93 
100 {
101  if (StatusIsFatal()) return false;
102  uint8_t value = GetAll() & ( 1 << (m_channel - 1));
103  return (value != 0);
104 }
105 
106 
107 void Solenoid::ValueChanged(ITable* source, const std::string& key, EntryValue value, bool isNew) {
108  Set(value.b);
109 }
110 
112  if (m_table != NULL) {
113  m_table->PutBoolean("Value", Get());
114  }
115 }
116 
118  Set(false);
119  if (m_table != NULL) {
120  m_table->AddTableListener("Value", this, true);
121  }
122 }
123 
125  Set(false);
126  if (m_table != NULL) {
127  m_table->RemoveTableListener(this);
128  }
129 }
130 
132  return "Solenoid";
133 }
134 
135 void Solenoid::InitTable(ITable *subTable) {
136  m_table = subTable;
137  UpdateTable();
138 }
139 
141  return m_table;
142 }
143 
virtual void RemoveTableListener(ITableListener *listener)=0
void AddActuator(const char *subsystem, const char *name, LiveWindowSendable *component)
Definition: LiveWindow.cpp:96
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
void StartLiveWindowMode()
Definition: Solenoid.cpp:117
void ValueChanged(ITable *source, const std::string &key, EntryValue value, bool isNew)
Definition: Solenoid.cpp:107
Solenoid(uint32_t channel)
Definition: Solenoid.cpp:49
virtual void PutBoolean(std::string key, bool value)=0
void Free(uint32_t index)
Definition: Resource.cpp:105
uint32_t Allocate(const char *resourceDesc)
Definition: Resource.cpp:62
ITable * GetTable()
Definition: Solenoid.cpp:140
virtual ~Solenoid()
Definition: Solenoid.cpp:72
static LiveWindow * GetInstance()
Definition: LiveWindow.cpp:13
void InitTable(ITable *subTable)
Definition: Solenoid.cpp:135
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
virtual bool Get()
Definition: Solenoid.cpp:99
virtual void Set(bool on)
Definition: Solenoid.cpp:85
static void CreateResourceObject(Resource **r, uint32_t elements)
Definition: Resource.cpp:39
uint8_t GetAll()
void UpdateTable()
Definition: Solenoid.cpp:111
void StopLiveWindowMode()
Definition: Solenoid.cpp:124
void Set(uint8_t value, uint8_t mask)
Definition: ITable.h:13
std::string GetSmartDashboardType()
Definition: Solenoid.cpp:131
static bool CheckSolenoidChannel(uint32_t channel)
Definition: SensorBase.cpp:186

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