/* Copyright (c) 20011 Hovanes Egiyan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "BaseIUParDigital.hh" //! Parameter type for DIGITAL parameters on the board const string BaseIUParDigital::bpdParType = "DIGITAL"; //! Map describing the mask for retrieval of the statuses for //! different pins. map BaseIUParDigital::bpdPinMaskMap; const uint32_t BaseIUParDigital::bpdInputMask = 0xFFFF0000 ; const uint32_t BaseIUParDigital::bpdOutputMask = 0x0000FFFF ; int bpdDummyInt = BaseIUParDigital::InitMaps(); //! Main constructor BaseIUParDigital::BaseIUParDigital(string name, BaseIUModule& board) : BaseIUVirtPar(bpdParType, name, board), bpdReadValue(0x00), bpdWriteValue( 0x0000) { if (bmDebFlag > 5) { cout << "BaseIUParDigital::BaseIUParDigital() : Main constructor is invoked " << " for DIGITAL parameter at address " << bmAddress << endl; } bvpWriteBuffer = reinterpret_cast( &bpdWriteValue ) ; return; } //! Short constructor BaseIUParDigital::BaseIUParDigital(string name) : BaseIUVirtPar(bpdParType, name), bpdReadValue(0x00), bpdWriteValue( 0x0000) { if (bmDebFlag > 5) { cout << "BaseIUParDigital::BaseIUParDigital() : Short constructor is invoked " << " for DIGITAL parameter named at address " << bmAddress << endl; } bvpWriteBuffer = reinterpret_cast( &bpdWriteValue ) ; return; } //! Copy constructor BaseIUParDigital::BaseIUParDigital( const BaseIUParDigital& par ) : BaseIUVirtPar ( par ) { if( bmDebFlag > 5 ) { cout << "BaseIUParDigital::BaseIUParDigital() : Copy constructor is invoked" << endl; } this->operator=( par ); bvpWriteBuffer = reinterpret_cast( &bpdWriteValue ) ; return; } //! Class destructor BaseIUParDigital::~BaseIUParDigital() { if( bmDebFlag > 5 ) { cout << "BaseIUParDigital::~BaseIUParDigital() : Destructor for DIGITAL parameter " << bvpType << " : " << bvpName << " has been called" << endl; } } //! Assignment operator BaseIUParDigital& BaseIUParDigital::operator=( const BaseIUParDigital& constPar ) { if( bmDebFlag > 5 ) { cout << "BaseIUParDigital::operator=() : Assignment operator is called" << endl; } //! Do not do anything if assigned to itself if( this == &constPar ) return *this; //! Call the assignment operator for the base class BaseIUVirtPar::operator=( constPar ); BaseIUParDigital& par = const_cast( constPar ); MtxLock objLock( bmMutex ); bpdReadValue = par.GetReadValue(); bpdWriteValue = par.GetWriteValue(); objLock.Unlock(); return *this; } //! Cloning function defined for polymorphism for copy constructor //! Invokes the copy constructor for this class and returns the //! the address of the resulting object BaseIUVirtPar* BaseIUParDigital::CloneParameter() { if( bmDebFlag > 5 ) { cout << "BaseIUParDigital::CloneParameter() : Cloning DIGITAL parameter " << " named " << bvpName << endl; } BaseIUParDigital* tmpPar = new BaseIUParDigital( *this ) ; return dynamic_cast( tmpPar ); } //! Request read of this DIGITAL for all boards on the bus inline void BaseIUParDigital::ReadRequest( BaseIUDevice* dev, string name ) { if( bmDebFlag > 1 ) { cout << "BaseIUParDigital::ReadRequest() : Dummy call " << bvpType << " : " << name << endl; } // There are no requests for the DIGITAL parameters. They are read directly from the DIG 10-pin port return; } //// Reading digital and writing digital seems to cause crashes //// Substitute with dummy method for testing. //bool BaseIUParDigital::Sync(bool syncFromHW) { // return false; //} //! Method to synch the parameters bool BaseIUParDigital::Sync(bool syncFromHW) { if (bmDebFlag > 1) { cout << "BaseIUParDigital::Sync() : Syncing parameter " << bvpType << " : " << bvpName << " for address " << bmAddress << endl; } //! Read the digital I/O pins for the bus that this parameter belongs to bool syncSuccessful = this->ReadDigital(); if (syncFromHW) { //! If sync from hardware requested read set the write value to the //! value that matches the pin statuses read from the hardware uint16_t newWriteVal = bpdReadValue & bpdOutputMask; // Dropped the upper bits for inputs if (bmDebFlag > 1) { cout << "BaseIUParDigital::Sync() : Requested to sync from hardware" << endl; cout << "BaseIUParDigital::Sync() : Will change the existing write value " << bpdWriteValue << " to " << newWriteVal << endl; } bpdWriteValue = newWriteVal; } else { //! Write to hardware if Sync is not requested from the hardware to the driver //! If the status word is different in the driver and hardware //! write to hardware. if ( (bpdReadValue & bpdOutputMask) != bpdWriteValue) { if (bmDebFlag > 1) { cout << "BaseIUParDigital::Sync() : Will write " << hex << showbase << bpdWriteValue << " to digital output of module with address " << bmAddress << endl; } //! Send the command bmDevice->WriteDigital( bpdWriteValue ); } else { if (bmDebFlag > 1) { cout << "BaseIUParDigital::Sync() : There was not need to write" << endl; } } } return syncSuccessful; } //! Read the digital inputs and outputs for the device that this parameter belongs to //! and assign the values to the data members of the object for this class. bool BaseIUParDigital::ReadDigital() { if (bmDebFlag > 1) { cout << "BaseIUParDigital::ReadDigital() : Reading digital inputs and outputs for parameter " << bvpType << " : " << bvpName << " for address " << hex << showbase << bmAddress << endl; } uint8_t inputByte(0), outputByte(0); //! Read the Digital inputs from the device try { inputByte = bmDevice->ReadDigitalInputs(); } catch (BaseIUDevice::FailedReadDevice& err) { cerr << "Exception: " << err.GetMessage() << endl; cerr << " Failed to read Digital Input " << hex << showbase << bmDevice->GetPortName() << endl; return false; } //! Read the Digital outputs from the device try { outputByte = bmDevice->ReadDigitalOutputs(); } catch (BaseIUDevice::FailedReadDevice& err) { cerr << "Exception: " << err.GetMessage() << endl; cerr << " Failed to read Digital Output " << hex << showbase << bmDevice->GetPortName() << endl; return false; } MtxLock objLock( bmMutex ); bpdReadValue = ( inputByte << 16) | outputByte ; return true; } //! Method to process deque from CANbus inline bool BaseIUParDigital::ProcessDeque( bool clearFlag ) { cout << "BaseIUParDigital::ProcessDeque : Dummy call with clearFlag " << clearFlag << " for parameter " << GetType() << " : " << GetName() << endl; return true; } // Set the write values for this object from the write value of another parameter. // It is assumed that the two parameters are of the same class, otherwise the // program will seg. fault. void BaseIUParDigital::SetWriteValueFrom( BaseIUVirtPar& fakePar ) { uint16_t* tmpValue = reinterpret_cast( fakePar.GetWriteBuffer() ); MtxLock objLock( bmMutex ); bpdWriteValue = *tmpValue; return; } //! Initialize the maps for DIGITALS int BaseIUParDigital::InitMaps() { if( bmDebFlag > 1 ) { cout << "BaseIUParDigital::InitMaps() : Initializing maps for DIGITAL parameters" << endl; } MtxLock classLock( bmGlobMutex ); //! These are the masks for retrieving the statuses for pins for the digital IO. //! The input and output bytes for an anagate are combined into single 32-bit unsigned //! The value for these masks is taken from the IU code, not 100% sure about interpretation. bpdPinMaskMap["OUTA"] = 1<<0; //! bpdPinMaskMap["OUTB"] = 1<<1; //! bpdPinMaskMap["OUTC"] = 1<<2; //! bpdPinMaskMap["OUTD"] = 1<<3; //! bpdPinMaskMap["INPA"] = 1<<16; //! bpdPinMaskMap["INPB"] = 1<<17; //! bpdPinMaskMap["INPC"] = 1<<18; //! bpdPinMaskMap["INPD"] = 1<<19; //! return 0; } //! return the digital input bit for the bus that this parameter belongs to bool BaseIUParDigital::GetInputBit() { string bitName = "INP" + bmDevice->GetBusStr(); return GetBit( bitName ); } //! return the Digital output bit for the bus that this parameter belongs to bool BaseIUParDigital::GetOutputBit() { string bitName = "OUT" + bmDevice->GetBusStr(); return GetBit( bitName ); } //! Return pin status from the board. If the pin name does not exist in //! pin mask map it will return "false", which means OFF. bool BaseIUParDigital::GetBit( const string bitName ) { bool pinExist = false; uint32_t bitMask = 0x00; { MtxLock classLock( bmGlobMutex ); pinExist = ( bpdPinMaskMap.count( bitName ) > 0 ); if( pinExist ) bitMask = bpdPinMaskMap[ bitName ]; } if( pinExist ) { MtxLock objLock( bmMutex ); return ( ( bpdReadValue & bitMask ) == bitMask ) ; } else { return false; } } //! Set the value for the status word which will be written out to //! the board based on the requested status for a particular pin. uint16_t BaseIUParDigital::SetPinStatus( const string bitName, const bool status ) { if( bmDebFlag > 1 ) { cout << "BaseIUParDigital::GetPinStatus() : Setting pin " << bitName << " to " << status << endl; } MtxLock classLock( bmGlobMutex ); int nElms = bpdPinMaskMap.count( bitName ); classLock.Unlock(); MtxLock localLock( bmMutex ); if( nElms > 0 ) { // cout << "Start value is " << bpdWriteValue << endl; uint16_t startVal = bpdWriteValue; localLock.Unlock(); uint16_t writeVal = SetPinToWrite( bitName, status, startVal ); localLock.Lock(); bpdWriteValue = writeVal; } return bpdWriteValue; } //! Set write bits based on the status of one particular pin. It starts with //! a given "current" write value. uint16_t BaseIUParDigital::SetPinToWrite( string bitName, bool bitON, uint16_t writeVal ) { MtxLock classLock( bmGlobMutex ); uint32_t onMask = bpdPinMaskMap[bitName]; classLock.Unlock(); //! Start with requiring that this pin be on writeVal |= onMask ; if( ! bitON ) { writeVal &= ~( onMask ); } return writeVal; } //// Checks if the pin value is 1. //// Note that this does not necessarily mean that the power (or whatever) is on, //// just that the value is set to 1 //bool BaseIUParDigital::GetBit( string bitName ) { // MtxLock classLock( bmGlobMutex ); // return ( ( bpdInputReadValue & bpdPinMaskMap[bitName] ) == bpdPinMaskMap[bitName] ); //} //// Checks if the pin value is 1. //// Note that this does not necessarily mean that the power (or whatever) is on, //// just that the value is set to 1 //bool BaseIUParDigital::GetOutputBool( string pinName ) { // MtxLock classLock( bmGlobMutex ); // return ( ( bpdOutputReadValue & bpdPinMaskMap[pinName] ) == bpdPinMaskMap[pinName] ); //} //