blob: 498a969c1930afefb4dde7a6d1cb6e706732aa68 [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 "Solenoid.h"
8#include "NetworkCommunication/UsageReporting.h"
9#include "WPIErrors.h"
10#include "LiveWindow/LiveWindow.h"
11
12/**
13 * Common function to implement constructor behavior.
14 */
15void Solenoid::InitSolenoid()
16{
17 char buf[64];
18 if (!CheckSolenoidModule(m_moduleNumber))
19 {
20 snprintf(buf, 64, "Solenoid Module %d", m_moduleNumber);
21 wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf);
22 return;
23 }
24 if (!CheckSolenoidChannel(m_channel))
25 {
26 snprintf(buf, 64, "Solenoid Channel %d", m_channel);
27 wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf);
28 return;
29 }
30 Resource::CreateResourceObject(&m_allocated, tSolenoid::kNumDO7_0Elements * kSolenoidChannels);
31
32 snprintf(buf, 64, "Solenoid %d (Module: %d)", m_channel, m_moduleNumber);
33 if (m_allocated->Allocate((m_moduleNumber - 1) * kSolenoidChannels + m_channel - 1, buf) == ~0ul)
34 {
35 CloneError(m_allocated);
36 return;
37 }
38
39 LiveWindow::GetInstance()->AddActuator("Solenoid", m_moduleNumber, m_channel, this);
40 nUsageReporting::report(nUsageReporting::kResourceType_Solenoid, m_channel, m_moduleNumber - 1);
41}
42
43/**
44 * Constructor.
45 *
46 * @param channel The channel on the solenoid module to control (1..8).
47 */
48Solenoid::Solenoid(UINT32 channel)
49 : SolenoidBase (GetDefaultSolenoidModule())
50 , m_channel (channel)
51{
52 InitSolenoid();
53}
54
55/**
56 * Constructor.
57 *
58 * @param moduleNumber The solenoid module (1 or 2).
59 * @param channel The channel on the solenoid module to control (1..8).
60 */
61Solenoid::Solenoid(UINT8 moduleNumber, UINT32 channel)
62 : SolenoidBase (moduleNumber)
63 , m_channel (channel)
64{
65 InitSolenoid();
66}
67
68/**
69 * Destructor.
70 */
71Solenoid::~Solenoid()
72{
73 if (CheckSolenoidModule(m_moduleNumber))
74 {
75 m_allocated->Free((m_moduleNumber - 1) * kSolenoidChannels + m_channel - 1);
76 }
77}
78
79/**
80 * Set the value of a solenoid.
81 *
82 * @param on Turn the solenoid output off or on.
83 */
84void Solenoid::Set(bool on)
85{
86 if (StatusIsFatal()) return;
87 UINT8 value = on ? 0xFF : 0x00;
88 UINT8 mask = 1 << (m_channel - 1);
89
90 SolenoidBase::Set(value, mask);
91}
92
93/**
94 * Read the current value of the solenoid.
95 *
96 * @return The current value of the solenoid.
97 */
98bool Solenoid::Get()
99{
100 if (StatusIsFatal()) return false;
101 UINT8 value = GetAll() & ( 1 << (m_channel - 1));
102 return (value != 0);
103}
104
105
106void Solenoid::ValueChanged(ITable* source, const std::string& key, EntryValue value, bool isNew) {
107 Set(value.b);
108}
109
110void Solenoid::UpdateTable() {
111 if (m_table != NULL) {
112 m_table->PutBoolean("Value", Get());
113 }
114}
115
116void Solenoid::StartLiveWindowMode() {
117 Set(false);
118 m_table->AddTableListener("Value", this, true);
119}
120
121void Solenoid::StopLiveWindowMode() {
122 Set(false);
123 m_table->RemoveTableListener(this);
124}
125
126std::string Solenoid::GetSmartDashboardType() {
127 return "Solenoid";
128}
129
130void Solenoid::InitTable(ITable *subTable) {
131 m_table = subTable;
132 UpdateTable();
133}
134
135ITable * Solenoid::GetTable() {
136 return m_table;
137}
138