jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 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 |
|
| 12 | /**
|
| 13 | * Common function to implement constructor behavior.
|
| 14 | */
|
| 15 | void Solenoid::InitSolenoid()
|
| 16 | {
|
jerrym | 37afdca | 2013-03-03 01:17:57 +0000 | [diff] [blame] | 17 | m_table = NULL;
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 18 | 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 | */
|
| 49 | Solenoid::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 | */
|
| 62 | Solenoid::Solenoid(UINT8 moduleNumber, UINT32 channel)
|
| 63 | : SolenoidBase (moduleNumber)
|
| 64 | , m_channel (channel)
|
| 65 | {
|
| 66 | InitSolenoid();
|
| 67 | }
|
| 68 |
|
| 69 | /**
|
| 70 | * Destructor.
|
| 71 | */
|
| 72 | Solenoid::~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 | */
|
| 85 | void 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 | */
|
| 99 | bool Solenoid::Get()
|
| 100 | {
|
| 101 | if (StatusIsFatal()) return false;
|
| 102 | UINT8 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 |
|
| 111 | void Solenoid::UpdateTable() {
|
| 112 | if (m_table != NULL) {
|
| 113 | m_table->PutBoolean("Value", Get());
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | void Solenoid::StartLiveWindowMode() {
|
| 118 | Set(false);
|
jerrym | 37afdca | 2013-03-03 01:17:57 +0000 | [diff] [blame] | 119 | if (m_table != NULL) {
|
| 120 | m_table->AddTableListener("Value", this, true);
|
| 121 | }
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 122 | }
|
| 123 |
|
| 124 | void Solenoid::StopLiveWindowMode() {
|
| 125 | Set(false);
|
jerrym | 37afdca | 2013-03-03 01:17:57 +0000 | [diff] [blame] | 126 | if (m_table != NULL) {
|
| 127 | m_table->RemoveTableListener(this);
|
| 128 | }
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 129 | }
|
| 130 |
|
| 131 | std::string Solenoid::GetSmartDashboardType() {
|
| 132 | return "Solenoid";
|
| 133 | }
|
| 134 |
|
| 135 | void Solenoid::InitTable(ITable *subTable) {
|
| 136 | m_table = subTable;
|
| 137 | UpdateTable();
|
| 138 | }
|
| 139 |
|
| 140 | ITable * Solenoid::GetTable() {
|
| 141 | return m_table;
|
| 142 | }
|
| 143 |
|