|
| 1 | +/******************************************************************************************************************//** |
| 2 | + * @file PinPoint.cpp |
| 3 | + * @brief Class definition for PinPoint used for I/O Control of a Pin on a Device |
| 4 | + * @authors |
| 5 | + * tgit23 8/2017 Original |
| 6 | + **********************************************************************************************************************/ |
| 7 | +#include "PinPoint.h" |
| 8 | +#include "UserControl.h" |
| 9 | + |
| 10 | +PeerIOSerialControl *PinPoint::XBee = NULL; |
| 11 | +uint8_t PinPoint::ThisDeviceID = 0; |
| 12 | +unsigned int PinPoint::LastEpromOffset = 0; |
| 13 | + |
| 14 | +/******************************************************************************************************************//** |
| 15 | + * @brief Constructor |
| 16 | + * @remarks |
| 17 | + * - Raw values are still used behind the scene's only the displayed value is modified |
| 18 | + * @code |
| 19 | + * exmaple code |
| 20 | + * @endcode |
| 21 | +**********************************************************************************************************************/ |
| 22 | +PinPoint::PinPoint(uint8_t _Device, uint8_t _Pin, ePinType _PinType, char *_Name = NULL, char _ID = NULL ) { |
| 23 | + Device = _Device; Pin = _Pin; PinType = _PinType; Name = _Name; ID = _ID; |
| 24 | + if ( Pin <= A0 ) IsOnOff = true; |
| 25 | + if ( PinType == SETTABLE ) FirstControl = new UserControl( this, SET_PIN, this, ID ); |
| 26 | + switch ( PinType ) { |
| 27 | + case INPIN: pinMode(Pin,INPUT);break; |
| 28 | + case INPINHIGH: pinMode(Pin,INPUT_PULLUP);break; |
| 29 | + case OUTPIN: pinMode(Pin,OUTPUT);break; |
| 30 | + case BUZZPIN: pinMode(Pin,OUTPUT);break; |
| 31 | + case SONICPIN: break; |
| 32 | + case SETTABLE: pinMode(Pin,OUTPUT);break; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/******************************************************************************************************************//** |
| 37 | + * @brief Use a user-defined function in the main sketch to modify the raw value read from the arduino |
| 38 | + * @remarks |
| 39 | + * - Raw values are still used behind the scene's only the displayed value is modified |
| 40 | + * @code |
| 41 | + * APin.ReadValue(); |
| 42 | + * while ( APin.Status() == WAIT ) { // Loop Till Value is Retreived } |
| 43 | + * GottenValue = APin.GetValue(); |
| 44 | + * @endcode |
| 45 | +**********************************************************************************************************************/ |
| 46 | +void PinPoint::ReadValue(bool _ForceBlocking = false) { |
| 47 | + DB(("PinPoint::ReadValue(Blocking="));DB((_ForceBlocking));DBL((")")); |
| 48 | + |
| 49 | + mStatus = ERR; mPacketID = -1; |
| 50 | + if ( Device > 16 || Pin > 127 ) return; // Value Check |
| 51 | + |
| 52 | + if ( Device == ThisDeviceID ) { //--- Local Pin Read --- |
| 53 | + DB(("LOCAL Get: Device="));DB((Device));DB((" Pin="));DBL((Pin)); |
| 54 | + if ( Pin >= A0 ) { mValue = analogRead(Pin); } |
| 55 | + else { mValue = digitalRead(Pin); } |
| 56 | + if ( mValue != -1 ) mStatus = OKAY; |
| 57 | + } |
| 58 | + else if ( _ForceBlocking ) { |
| 59 | + DB(("REMOTE Get: Device="));DB((Device));DB((" Pin="));DBL((Pin)); |
| 60 | + if ( Device != XBee->TargetArduinoID() ) XBee->TargetArduinoID( Device ); //Set TransceiverID |
| 61 | + if ( Pin >= A0 ) { mValue = XBee->analogReadB(Pin); } |
| 62 | + else { mValue = XBee->digitalReadB(Pin); } |
| 63 | + if ( mValue != -1 ) mStatus = OKAY; |
| 64 | + } |
| 65 | + else { //--- Remote Pin Read --- |
| 66 | + DB(("REMOTE Get: Device="));DB((Device));DB((" Pin="));DBL((Pin)); |
| 67 | + if ( Device != XBee->TargetArduinoID() ) XBee->TargetArduinoID( Device ); //Set TransceiverID |
| 68 | + mWaitStart = millis(); |
| 69 | + if ( Pin >= A0 ) { mPacketID = XBee->analogReadNB(Pin); } |
| 70 | + else { mPacketID = XBee->digitalReadNB(Pin); } |
| 71 | + mStatus = WAIT; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/******************************************************************************************************************//** |
| 76 | + * @brief Checks XBee communications and returns 'true' if the pin Status has changed |
| 77 | + * @remarks |
| 78 | + * @code |
| 79 | + * exmaple code |
| 80 | + * @endcode |
| 81 | +**********************************************************************************************************************/ |
| 82 | +bool PinPoint::UpdateAvailable() { |
| 83 | + XBee->Available(); |
| 84 | + if ( mStatus == WAIT ) { |
| 85 | + if ( mPacketID != -1 ) { |
| 86 | + int Ret = XBee->GetReply(mPacketID); |
| 87 | + if ( Ret != -1 ) { |
| 88 | + mValue = Ret; |
| 89 | + mStatus = OKAY; |
| 90 | + return true; |
| 91 | + } |
| 92 | + } |
| 93 | + if ( millis() - mWaitStart > XBee->Timeout() ) { |
| 94 | + mPacketID = -1; |
| 95 | + mStatus = ERR; |
| 96 | + return true; |
| 97 | + } |
| 98 | + } |
| 99 | + return false; |
| 100 | +} |
| 101 | + |
| 102 | +/******************************************************************************************************************//** |
| 103 | + * @brief Use a user-defined function in the main sketch to modify the raw value read from the arduino |
| 104 | + * @remarks |
| 105 | + * - Raw values are still used behind the scene's only the displayed value is modified |
| 106 | + * @code |
| 107 | + * exmaple code |
| 108 | + * @endcode |
| 109 | +**********************************************************************************************************************/ |
| 110 | +PinStatus PinPoint::GetStatus() { |
| 111 | + return mStatus; |
| 112 | +} |
| 113 | + |
| 114 | +/******************************************************************************************************************//** |
| 115 | + * @brief Use a user-defined function in the main sketch to modify the raw value read from the arduino |
| 116 | + * @remarks |
| 117 | + * - Raw values are still used behind the scene's only the displayed value is modified |
| 118 | + * @code |
| 119 | + * exmaple code |
| 120 | + * @endcode |
| 121 | +**********************************************************************************************************************/ |
| 122 | +int PinPoint::GetRawValue() { |
| 123 | + return mValue; |
| 124 | +} |
| 125 | + |
| 126 | +/******************************************************************************************************************//** |
| 127 | + * @brief Use a user-defined function in the main sketch to modify the raw value read from the arduino |
| 128 | + * @remarks |
| 129 | + * - Raw values are still used behind the scene's only the displayed value is modified |
| 130 | + * @code |
| 131 | + * exmaple code |
| 132 | + * @endcode |
| 133 | +**********************************************************************************************************************/ |
| 134 | +int PinPoint::GetModifiedValue() { |
| 135 | + if ( ValueModifierCallback == NULL ) return mValue; |
| 136 | + return (*ValueModifierCallback)(mValue); |
| 137 | +} |
| 138 | + |
| 139 | +int PinPoint::ModifyValue(int _Value) { |
| 140 | + if ( ValueModifierCallback == NULL ) return _Value; |
| 141 | + return (*ValueModifierCallback)(_Value); |
| 142 | +} |
| 143 | +/******************************************************************************************************************//** |
| 144 | + * @brief Use a user-defined function in the main sketch to modify the raw value read from the arduino |
| 145 | + * @remarks |
| 146 | + * - Raw values are still used behind the scene's only the displayed value is modified |
| 147 | + * @code |
| 148 | + * exmaple code |
| 149 | + * @endcode |
| 150 | +**********************************************************************************************************************/ |
| 151 | +void PinPoint::SetTo(unsigned int _Value, PinStatus _Status = OKAY) { |
| 152 | + DB(("PinPoint::SetTo("));DB((Device));DBC;DB((Pin));DBC;DB((_Value));DBL((")")); |
| 153 | + |
| 154 | + if ( Device > 16 || Pin > 127 ) return; // Value Check |
| 155 | + |
| 156 | + // Drive Buzzer |
| 157 | + if ( PinType == BUZZPIN ) { |
| 158 | + if (_Value < 1) { noTone(Pin); } |
| 159 | + else { tone(Pin, _Value); } |
| 160 | + } |
| 161 | + |
| 162 | + // Drive Local Pin |
| 163 | + else if ( Device == ThisDeviceID ) { // Drive a LOCAL Pin |
| 164 | + if ( Pin > 63 ) { XBee->VirtualPin(Pin, _Value, _Status); } // Drive a Virtual Pin |
| 165 | + else { |
| 166 | + if ( Pin >= A0 ) { analogWrite(Pin, _Value); } |
| 167 | + else { digitalWrite(Pin, _Value); } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + // Drive Remote Pin |
| 172 | + else { |
| 173 | + if ( XBee == NULL ) return; |
| 174 | + if ( Device != XBee->TargetArduinoID() ) XBee->TargetArduinoID( Device ); |
| 175 | + if ( Pin >= A0 ) { XBee->analogWriteB(Pin, _Value); } // Set the Remote Arduino Analog Pin |
| 176 | + else { XBee->digitalWriteB(Pin, _Value); } // Set the Remote Arduino Digital Pin |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +/******************************************************************************************************************//** |
| 181 | + * @brief Use a user-defined function in the main sketch to modify the raw value read from the arduino |
| 182 | + * @remarks |
| 183 | + * - Raw values are still used behind the scene's only the displayed value is modified |
| 184 | + * @code |
| 185 | + * exmaple code |
| 186 | + * @endcode |
| 187 | +**********************************************************************************************************************/ |
| 188 | +void PinPoint::AttachValueModifier(int (*_ValueModifierCallback)(int)) { |
| 189 | + ValueModifierCallback = _ValueModifierCallback; |
| 190 | +} |
| 191 | + |
| 192 | +/******************************************************************************************************************//** |
| 193 | + * @brief Adds a 'UserControl' to the Controls Link-List |
| 194 | + * @remarks |
| 195 | + * @code |
| 196 | + * exmaple code |
| 197 | + * @endcode |
| 198 | +**********************************************************************************************************************/ |
| 199 | +void PinPoint::Controls(PinPoint* _OutPin, eControlType _ControlType, char _ID, PinPoint* _StorePin = NULL) { |
| 200 | + if ( FirstControl == NULL ) { |
| 201 | + FirstControl = new UserControl( this, _ControlType, _OutPin, _ID, _StorePin ); |
| 202 | + LastEpromOffset = FirstControl->SetEpromOffset(LastEpromOffset); |
| 203 | + } else { |
| 204 | + UserControl *thisControl = FirstControl; |
| 205 | + while ( thisControl->Next != NULL ) { thisControl = thisControl->Next; } |
| 206 | + thisControl->Next = new UserControl( this, _ControlType, _OutPin, _ID, _StorePin ); |
| 207 | + LastEpromOffset = thisControl->Next->SetEpromOffset(LastEpromOffset); |
| 208 | + thisControl->Next->Prev = thisControl; |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | + |
0 commit comments