blob: 2b753d3a244e3f13535510bf0115376b86cafa1a [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{
jerrym37afdca2013-03-03 01:17:57 +000017 m_table = NULL;
jerrymf1579332013-02-07 01:56:28 +000018 char buf[64];
19 if (!CheckSolenoidModule(m_moduleNumber))
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
44/**
45 * Constructor.
46 *
47 * @param channel The channel on the solenoid module to control (1..8).
48 */
49Solenoid::Solenoid(UINT32 channel)
50 : SolenoidBase (GetDefaultSolenoidModule())
51 , m_channel (channel)
52{
53 InitSolenoid();
54}
55
56/**
57 * Constructor.
58 *
59 * @param moduleNumber The solenoid module (1 or 2).
60 * @param channel The channel on the solenoid module to control (1..8).
61 */
62Solenoid::Solenoid(UINT8 moduleNumber, UINT32 channel)
63 : SolenoidBase (moduleNumber)
64 , m_channel (channel)
65{
66 InitSolenoid();
67}
68
69/**
70 * Destructor.
71 */
72Solenoid::~Solenoid()
73{
74 if (CheckSolenoidModule(m_moduleNumber))
75 {
76 m_allocated->Free((m_moduleNumber - 1) * kSolenoidChannels + m_channel - 1);
77 }
78}
79
80/**
81 * Set the value of a solenoid.
82 *
83 * @param on Turn the solenoid output off or on.
84 */
85void Solenoid::Set(bool on)
86{
87 if (StatusIsFatal()) return;
88 UINT8 value = on ? 0xFF : 0x00;
89 UINT8 mask = 1 << (m_channel - 1);
90
91 SolenoidBase::Set(value, mask);
92}
93
94/**
95 * Read the current value of the solenoid.
96 *
97 * @return The current value of the solenoid.
98 */
99bool Solenoid::Get()
100{
101 if (StatusIsFatal()) return false;
102 UINT8 value = GetAll() & ( 1 << (m_channel - 1));
103 return (value != 0);
104}
105
106
107void Solenoid::ValueChanged(ITable* source, const std::string& key, EntryValue value, bool isNew) {
108 Set(value.b);
109}
110
111void Solenoid::UpdateTable() {
112 if (m_table != NULL) {
113 m_table->PutBoolean("Value", Get());
114 }
115}
116
117void Solenoid::StartLiveWindowMode() {
118 Set(false);
jerrym37afdca2013-03-03 01:17:57 +0000119 if (m_table != NULL) {
120 m_table->AddTableListener("Value", this, true);
121 }
jerrymf1579332013-02-07 01:56:28 +0000122}
123
124void Solenoid::StopLiveWindowMode() {
125 Set(false);
jerrym37afdca2013-03-03 01:17:57 +0000126 if (m_table != NULL) {
127 m_table->RemoveTableListener(this);
128 }
jerrymf1579332013-02-07 01:56:28 +0000129}
130
131std::string Solenoid::GetSmartDashboardType() {
132 return "Solenoid";
133}
134
135void Solenoid::InitTable(ITable *subTable) {
136 m_table = subTable;
137 UpdateTable();
138}
139
140ITable * Solenoid::GetTable() {
141 return m_table;
142}
143