Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members | Related Pages

LOW_devDS2890.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           LOW_devDS2890.cpp  -  description
00003                              -------------------
00004     begin                : Sun Nov 9 2003
00005     copyright            : (C) 2003 by Harald Roelle
00006     email                : roelle@informatik.uni-muenchen.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018  
00019 #include "LOW_devDS2890.h"
00020 
00021 
00022 //=====================================================================================
00023 //
00024 // static initializer
00025 //
00026 
00027 const std::string LOW_devDS2890::familyName = "DS2890";
00028 
00029 int LOW_devDS2890::initHelper = initialize();
00030 int LOW_devDS2890::initialize()
00031 {
00032   LOW_deviceFactory::registerSpecificCtor( familyCode, &LOW_devDS2890::new_Instance);
00033   return 0;
00034 }
00035 
00036 
00037 
00038 //=====================================================================================
00039 //
00040 // static methods
00041 //
00042 
00043 LOW_device* LOW_devDS2890::new_Instance( LOW_netSegment &inNetSegment, const LOW_deviceID &inDevID)
00044 {
00045   return new LOW_devDS2890( inNetSegment, inDevID);
00046 }
00047 
00048 
00049 
00050 //=====================================================================================
00051 //
00052 // public constructors
00053 //
00054 
00055 LOW_devDS2890::LOW_devDS2890( LOW_netSegment &inSegment, const LOW_deviceID &inDevID) :
00056   LOW_device( inSegment, inDevID, familyCode)
00057 {
00058   chargePumpAllowed = false;
00059   cmd_ReadControlRegister();  // initially read feature values
00060   cachedWiperPos = new wiperPosition_t[getPotentiometerCount()];
00061   for( unsigned int a=0; a<getPotentiometerCount(); ++a)
00062     cachedWiperPos[a] = 0;
00063 }
00064 
00065 
00066 LOW_devDS2890::~LOW_devDS2890()
00067 {
00068   delete[] cachedWiperPos;
00069 }
00070 
00071 
00072 //=====================================================================================
00073 //
00074 // public methods
00075 //
00076 
00077 void LOW_devDS2890::setChargePumpAllowed( const bool inCharpePumpAllowed)
00078 {
00079   chargePumpAllowed = inCharpePumpAllowed;
00080 }
00081 
00082 
00083 bool LOW_devDS2890::getChargePumpAllowed()
00084 {
00085   return chargePumpAllowed;
00086 }
00087 
00088 
00089 bool LOW_devDS2890::getHasLinearPotElements() const
00090 {
00091   return hasLinearPotElements;
00092 }
00093 
00094 
00095 bool LOW_devDS2890::getIsWiperSettingVolatile() const
00096 {
00097   return isWiperSettingVolatile;
00098 }
00099 
00100 
00101 uint8_t LOW_devDS2890::getPotentiometerCount() const
00102 {
00103   return potentiometerCount;
00104 }
00105 
00106 
00107 LOW_devDS2890::wiperPosition_t LOW_devDS2890::getWiperPositionsCount() const
00108 {
00109   return wiperPositionsCount;
00110 }
00111 
00112 
00113 uint32_t LOW_devDS2890::getPotentiometerResistance() const
00114 {
00115   return potentiometerResistance;
00116 }
00117 
00118 
00119 uint8_t LOW_devDS2890::getCachedWiperNumber()
00120 {
00121   return cachedCurrentWiper;
00122 }
00123 
00124 
00125 LOW_devDS2890::wiperPosition_t LOW_devDS2890::getCachedWiperPosition( const uint8_t inControlledWiperNum)
00126 {
00127   if ( inControlledWiperNum >= getPotentiometerCount() )
00128     throw devDS2890_error( "Illegal wiper number specified", __FILE__, __LINE__);
00129   
00130   return cachedWiperPos[inControlledWiperNum];
00131 }
00132 
00133 
00134 LOW_devDS2890::wiperPosition_t LOW_devDS2890::cmd_ReadPosition()
00135 {
00136   linkLock  lock( *this);
00137 
00138   cmd_MatchROM();
00139   
00140   getLink().writeData( ReadPosition_COMMAND);
00141 
00142   byteVec_t dataFromDev = byteVec_t( 2);
00143   getLink().readData( dataFromDev); // 1st byte: control register, 2nd byte: position
00144 
00145   if ( dataFromDev[1]&wiperNum_ctlRegMsk != ((~dataFromDev[1]) & wiperNum_ctlRegMsk)>>2 )
00146     throw devDS2890_error( "Error sanity checking control register value", __FILE__, __LINE__);
00147 
00148   cachedCurrentWiper = dataFromDev[1] & wiperNum_ctlRegMsk;
00149   cachedWiperPos[cachedCurrentWiper] = dataFromDev[1];
00150 
00151   return dataFromDev[1];
00152 }
00153 
00154 
00155 void LOW_devDS2890::cmd_WritePosition( const LOW_devDS2890::wiperPosition_t inNewPosition)
00156 {
00157   linkLock  lock( *this);
00158 
00159   if ( inNewPosition >= getWiperPositionsCount() )
00160     throw devDS2890_error( "New position exceeds maximal position", __FILE__, __LINE__);
00161 
00162   cmd_MatchROM();
00163 
00164   byteVec_t outputBytes;
00165   outputBytes.push_back( static_cast<uint8_t>( WritePosition_COMMAND));
00166   outputBytes.push_back( inNewPosition);
00167   getLink().writeData( outputBytes);
00168   
00169   uint8_t posCheckByte = getLink().readDataByte();
00170   if ( posCheckByte != inNewPosition) {
00171     getLink().resetBus();
00172     throw devDS2890_error( "Error verifying new position", __FILE__, __LINE__);
00173   }
00174 
00175   getLink().writeData( releaseCode_subcommand);
00176 
00177   bool errorBit = getLink().readDataBit();
00178   if ( errorBit == true ) {
00179     getLink().resetBus();
00180     throw devDS2890_error( "Error activating new position", __FILE__, __LINE__);
00181   }
00182 
00183   cachedWiperPos[cachedCurrentWiper] = inNewPosition;
00184 }
00185 
00186 
00187 LOW_devDS2890::controlRegister_t LOW_devDS2890::cmd_ReadControlRegister()
00188 {
00189   linkLock  lock( *this);
00190 
00191   cmd_MatchROM();
00192 
00193   getLink().writeData( ReadControlRegister_COMMAND);
00194 
00195   byteVec_t dataFromDev = byteVec_t( 2);
00196   getLink().readData( dataFromDev); // 1st byte: feature register, 2nd byte: control register
00197 
00198   hasLinearPotElements   = dataFromDev[0] & potentiometerType_featRegMask;
00199   isWiperSettingVolatile = dataFromDev[0] & wiperSetVolatility_featRegMask;
00200   potentiometerCount     = ((dataFromDev[0]&potentiometerCount_featRegMask)>>2) + 1;
00201 
00202   switch ( (dataFromDev[0]&wiperPositions_featRegMask)>>4 ) {
00203     case 0x00: wiperPositionsCount =  32;  break;
00204     case 0x01: wiperPositionsCount =  64;  break;
00205     case 0x02: wiperPositionsCount = 128;  break;
00206     case 0x03: wiperPositionsCount = 256;  break;
00207   }
00208 
00209   switch ( (dataFromDev[0]&potentiometerResistance_featRegMask)>>6 ) {
00210     case 0x00: potentiometerResistance =   5000;  break;
00211     case 0x01: potentiometerResistance =  10000;  break;
00212     case 0x02: potentiometerResistance =  50000;  break;
00213     case 0x03: potentiometerResistance = 100000;  break;
00214   }
00215 
00216   if ( dataFromDev[1]&wiperNum_ctlRegMsk != ((~dataFromDev[1]) & invWiperNum_ctlRegMsk)>>2 )
00217     throw devDS2890_error( "Error sanity checking control register value", __FILE__, __LINE__);
00218   
00219   LOW_devDS2890::controlRegister_t retValue;
00220   retValue.controlledWiperNum = dataFromDev[1] & wiperNum_ctlRegMsk;
00221   retValue.isChargePumpOn     = (dataFromDev[1]&chargePump_ctlRegMsk==chargePump_ctlRegMsk);
00222 
00223   cachedCurrentWiper = retValue.controlledWiperNum;
00224       
00225   return retValue;
00226 }
00227 
00228 
00229 void LOW_devDS2890::cmd_WriteControlRegister( const uint8_t inControlledWiperNum, const bool inChargePumpOn)
00230 {
00231   linkLock  lock( *this);
00232 
00233   if ( inControlledWiperNum >= getPotentiometerCount() )
00234     throw devDS2890_error( "Illegal wiper number specified", __FILE__, __LINE__);
00235 
00236   if ( inChargePumpOn && !chargePumpAllowed )
00237     throw devDS2890_error( "Charge pump requested but not allowed", __FILE__, __LINE__);
00238 
00239   uint8_t controlByte =   (inControlledWiperNum&wiperNum_ctlRegMsk)
00240                         | (((~inControlledWiperNum)<<2)&invWiperNum_ctlRegMsk)
00241                         | (inChargePumpOn==true?chargePump_ctlRegMsk:0x00);
00242 
00243   cmd_MatchROM();
00244 
00245   byteVec_t outputBytes;
00246   outputBytes.push_back( static_cast<uint8_t>( WriteControlRegister_COMMAND));
00247   outputBytes.push_back( controlByte);
00248   getLink().writeData( outputBytes);
00249 
00250   uint8_t checkByte = getLink().readDataByte();
00251   if ( checkByte == 0xff ) {
00252     getLink().resetBus();
00253     throw devDS2890_error( "Illegal control byte sent", __FILE__, __LINE__);
00254   }
00255   if ( checkByte != controlByte) {
00256     getLink().resetBus();
00257     throw devDS2890_error( "Error verifying control byte", __FILE__, __LINE__);
00258   }
00259 
00260   getLink().writeData( releaseCode_subcommand);
00261 
00262   bool errorBit = getLink().readDataBit();
00263   if ( errorBit == true ) {
00264     getLink().resetBus();
00265     throw devDS2890_error( "Error activating new control setting", __FILE__, __LINE__);
00266   }
00267 
00268   cachedCurrentWiper = inControlledWiperNum;
00269 }
00270 
00271 
00272 LOW_devDS2890::wiperPosition_t LOW_devDS2890::cmd_Increment()
00273 {
00274   linkLock  lock( *this);
00275 
00276   cmd_MatchROM();
00277 
00278   getLink().writeData( Increment_COMMAND);
00279   cachedWiperPos[cachedCurrentWiper] = getLink().readDataByte();
00280 
00281   getLink().resetBus();  // leave inc/dec mode
00282 
00283   return cachedWiperPos[cachedCurrentWiper];
00284 }
00285 
00286 
00287 LOW_devDS2890::wiperPosition_t LOW_devDS2890::cmd_Decrement()
00288 {
00289   linkLock  lock( *this);
00290 
00291   cmd_MatchROM();
00292 
00293   getLink().writeData( Decrement_COMMAND);
00294   cachedWiperPos[cachedCurrentWiper] = getLink().readDataByte();
00295 
00296   getLink().resetBus();  // leave inc/dec mode
00297 
00298   return cachedWiperPos[cachedCurrentWiper];
00299 }
00300 

Generated on Tue Feb 3 11:30:25 2004 for OneWireLibrary++ by doxygen 1.3.2