#include "UBLOX_I2C.h" #define LOG( X ) m_pLog->Log( "UBLOX", this, X ) #define LOG_ERROR( X ) m_pLog->Log( "UBLOX", this, _LOG_ERROR, X ) sensorUBLOX::sensorUBLOX( config_setting_t *Settings): basicThread(){ m_pLog = basicLogCollection::getInstance(); m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "UBLOX" ); m_pBus = NULL; m_pDBC = NULL; m_pDevicePath = "/dev/i2c-1"; m_bDeviceAddress = 0x10; m_nSampleDelay = 100; memset( m_pIn_Buffer, 0, UBX_INPUT_BUFFER_SIZE ); m_nIn_NextReadIndex = 0; }; sensorUBLOX::sensorUBLOX( PATHFINDER_DBC *DBC): basicThread(){ m_pLog = basicLogCollection::getInstance(); m_pSettings = NULL; m_pBus = NULL; m_pDBC = DBC; m_pDevicePath = "/dev/i2c-1"; m_bDeviceAddress = 0x10; m_nSampleDelay = 100; memset( m_pIn_Buffer, 0, UBX_INPUT_BUFFER_SIZE ); m_nIn_NextReadIndex = 0; }; sensorUBLOX::~sensorUBLOX(){ waitThreadEnd( true ); if(m_pBus != NULL ) delete( m_pBus ); }; uint16_t sensorUBLOX::finishupPackage( uint8_t *buffer, uint16_t payLoadLen ){ if( buffer != NULL ){ buffer[UBX_SYNC_BEGIN_IDX] = UBX_SYNC_CHAR1; buffer[UBX_SYNC_SECOND_IDX] = UBX_SYNC_CHAR2; buffer[UBX_LENGTH_LB_IDX] = payLoadLen & 0xFF; buffer[UBX_LENGTH_HB_IDX] = ( payLoadLen >> 8 ) & 0xFF; uint8_t C_A = 0; uint8_t C_B = 0; for( uint16_t idx = UBX_CLASS_IDX; idx < (UBX_PAYLOAD_BEGIN_IDX+payLoadLen); idx++ ){ C_A += buffer[idx]; C_B += C_A; }; buffer[UBX_PAYLOAD_BEGIN_IDX+payLoadLen] = C_A; buffer[UBX_PAYLOAD_BEGIN_IDX+payLoadLen+1] = C_B; return UBX_PAYLOAD_BEGIN_IDX+payLoadLen+2; }; return 0; }; bool sensorUBLOX::req_Version(){ if( m_pBus != NULL ) { uint8_t buffer[22]; uint16_t packLen; buffer[UBX_CLASS_IDX] = UBX_CLASS_MON; buffer[UBX_ID_IDX] = 0x04; packLen = finishupPackage( buffer, 0 ); if( packLen > 0 ){ if( m_pBus->writeMZ( UBX_STREAM_ADDR, buffer, packLen ) ){ //packLen = getPackLen(); //char tBuffer[64]; //sprintf( tBuffer,"Data Len: %d", packLen ); //LOG( tBuffer ); //if( packLen > 0 ) { // LOG("Have Data!"); // get_Data( 64 ); //} else { // LOG("Have no Data!"); //}; return true; } }; }; return false; }; uint16_t sensorUBLOX::getPackLen(){ uint16_t retVal = 0; if( m_pBus != NULL ) { retVal = m_pBus->read8( UBX_HB_LEN_ADDR ); retVal = retVal << 8; retVal |= m_pBus->read8( UBX_LB_LEN_ADDR ); char tBuf[64]; sprintf( tBuf, "Right Size: %d", retVal ); LOG( tBuf ); if( m_pBus->readMZ( UBX_HB_LEN_ADDR, (uint8_t *)&retVal, 2 ) ) //return retVal; return (( retVal & 0xFF ) << 8 ) | (( retVal >> 8 ) & 0xFF ); }; return 0; }; bool sensorUBLOX::get_Data(){ uint8_t in_Buffer[I2C_PAGE_BYTES]; uint16_t tLen = 0; char tBuff[64]; if( m_pBus == NULL ) return false; if( m_nIn_NextReadIndex >= ( UBX_INPUT_BUFFER_SIZE - I2C_PAGE_BYTES )){ LOG( "No Buffer Place!" ); return false; }; memset( in_Buffer, 0, I2C_PAGE_BYTES ); do{ if( !m_pBus->readMZ( 0, in_Buffer, I2C_PAGE_BYTES ) ) return false; if( ( in_Buffer[ 0 ] == 0x0A ) && ( in_Buffer[ 1 ] == 0x0A ) ) return false; for( uint16_t idx = 0; idx < I2C_PAGE_BYTES; idx++ ) { if( ( in_Buffer[ idx ] != 0x0D ) && ( in_Buffer[ idx ] != 0x0A ) ){ sprintf( tBuff, " 0x%02X %c", in_Buffer[idx], (char) in_Buffer[idx] ); LOG( tBuff ); }; m_pIn_Buffer[m_nIn_NextReadIndex] = in_Buffer[idx]; m_nIn_NextReadIndex++; //}; if( idx ) { if( ( in_Buffer[ idx - 1 ] == 0x0A ) && ( in_Buffer[ idx ] == 0x0A ) ) return true; }; }; } while( ( m_nIn_NextReadIndex < ( UBX_INPUT_BUFFER_SIZE - I2C_PAGE_BYTES ) ) && m_bThreadStatus < TH_ENDED ); if( m_bThreadStatus >= TH_ENDED ) return false; LOG("Done Read! - Buffer Full!"); return true; }; bool sensorUBLOX::create(){ if( m_pDevicePath == NULL ) return false; if( m_pBus != NULL ) delete( m_pBus ); m_pBus = new i2cPort( m_pDevicePath, m_bDeviceAddress ); if( m_pBus != NULL ) { //LOG("Read Chip ID"); //if( req_Version() ){ // LOG("Success!"); //}; return init(); } else LOG_ERROR("No I2C Bus Created!"); return false; }; void sensorUBLOX::m_fThreadProc( void ){ m_bThreadStatus = TH_WORKING; while( m_bThreadStatus < TH_ENDED ){ if( get_Data() ){ LOG("Data arived!"); }; SLEEP( m_nSampleDelay ); }; }; #undef LOG #undef LOG_ERROR