You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "UBLOX_I2C.h"
  2. #define LOG( X ) m_pLog->Log( "UBLOX", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "UBLOX", this, _LOG_ERROR, X )
  4. sensorUBLOX::sensorUBLOX( config_setting_t *Settings): basicThread(){
  5. m_pLog = basicLogCollection::getInstance();
  6. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "UBLOX" );
  7. m_pBus = NULL;
  8. m_pDBC = NULL;
  9. m_pDevicePath = "/dev/i2c-1";
  10. m_bDeviceAddress = 0x10;
  11. m_nSampleDelay = 100;
  12. memset( m_pIn_Buffer, 0, UBX_INPUT_BUFFER_SIZE );
  13. m_nIn_NextReadIndex = 0;
  14. };
  15. sensorUBLOX::sensorUBLOX( PATHFINDER_DBC *DBC): basicThread(){
  16. m_pLog = basicLogCollection::getInstance();
  17. m_pSettings = NULL;
  18. m_pBus = NULL;
  19. m_pDBC = DBC;
  20. m_pDevicePath = "/dev/i2c-1";
  21. m_bDeviceAddress = 0x10;
  22. m_nSampleDelay = 100;
  23. memset( m_pIn_Buffer, 0, UBX_INPUT_BUFFER_SIZE );
  24. m_nIn_NextReadIndex = 0;
  25. };
  26. sensorUBLOX::~sensorUBLOX(){
  27. waitThreadEnd( true );
  28. if(m_pBus != NULL ) delete( m_pBus );
  29. };
  30. uint16_t sensorUBLOX::finishupPackage( uint8_t *buffer, uint16_t payLoadLen ){
  31. if( buffer != NULL ){
  32. buffer[UBX_SYNC_BEGIN_IDX] = UBX_SYNC_CHAR1;
  33. buffer[UBX_SYNC_SECOND_IDX] = UBX_SYNC_CHAR2;
  34. buffer[UBX_LENGTH_LB_IDX] = payLoadLen & 0xFF;
  35. buffer[UBX_LENGTH_HB_IDX] = ( payLoadLen >> 8 ) & 0xFF;
  36. uint8_t C_A = 0;
  37. uint8_t C_B = 0;
  38. for( uint16_t idx = UBX_CLASS_IDX; idx < (UBX_PAYLOAD_BEGIN_IDX+payLoadLen); idx++ ){
  39. C_A += buffer[idx];
  40. C_B += C_A;
  41. };
  42. buffer[UBX_PAYLOAD_BEGIN_IDX+payLoadLen] = C_A;
  43. buffer[UBX_PAYLOAD_BEGIN_IDX+payLoadLen+1] = C_B;
  44. return UBX_PAYLOAD_BEGIN_IDX+payLoadLen+2;
  45. };
  46. return 0;
  47. };
  48. bool sensorUBLOX::req_Version(){
  49. if( m_pBus != NULL ) {
  50. uint8_t buffer[22];
  51. uint16_t packLen;
  52. buffer[UBX_CLASS_IDX] = UBX_CLASS_MON;
  53. buffer[UBX_ID_IDX] = 0x04;
  54. packLen = finishupPackage( buffer, 0 );
  55. if( packLen > 0 ){
  56. if( m_pBus->writeMZ( UBX_STREAM_ADDR, buffer, packLen ) ){
  57. //packLen = getPackLen();
  58. //char tBuffer[64];
  59. //sprintf( tBuffer,"Data Len: %d", packLen );
  60. //LOG( tBuffer );
  61. //if( packLen > 0 ) {
  62. // LOG("Have Data!");
  63. // get_Data( 64 );
  64. //} else {
  65. // LOG("Have no Data!");
  66. //};
  67. return true;
  68. }
  69. };
  70. };
  71. return false;
  72. };
  73. uint16_t sensorUBLOX::getPackLen(){
  74. uint16_t retVal = 0;
  75. if( m_pBus != NULL ) {
  76. retVal = m_pBus->read8( UBX_HB_LEN_ADDR );
  77. retVal = retVal << 8;
  78. retVal |= m_pBus->read8( UBX_LB_LEN_ADDR );
  79. char tBuf[64];
  80. sprintf( tBuf, "Right Size: %d", retVal );
  81. LOG( tBuf );
  82. if( m_pBus->readMZ( UBX_HB_LEN_ADDR, (uint8_t *)&retVal, 2 ) )
  83. //return retVal;
  84. return (( retVal & 0xFF ) << 8 ) | (( retVal >> 8 ) & 0xFF );
  85. };
  86. return 0;
  87. };
  88. bool sensorUBLOX::get_Data(){
  89. uint8_t in_Buffer[I2C_PAGE_BYTES];
  90. uint16_t tLen = 0;
  91. char tBuff[64];
  92. if( m_pBus == NULL ) return false;
  93. if( m_nIn_NextReadIndex >= ( UBX_INPUT_BUFFER_SIZE - I2C_PAGE_BYTES )){
  94. LOG( "No Buffer Place!" );
  95. return false;
  96. };
  97. memset( in_Buffer, 0, I2C_PAGE_BYTES );
  98. do{
  99. if( !m_pBus->readMZ( 0, in_Buffer, I2C_PAGE_BYTES ) ) return false;
  100. if( ( in_Buffer[ 0 ] == 0x0A ) && ( in_Buffer[ 1 ] == 0x0A ) ) return false;
  101. for( uint16_t idx = 0; idx < I2C_PAGE_BYTES; idx++ ) {
  102. if( ( in_Buffer[ idx ] != 0x0D ) && ( in_Buffer[ idx ] != 0x0A ) ){
  103. sprintf( tBuff, " 0x%02X %c", in_Buffer[idx], (char) in_Buffer[idx] );
  104. LOG( tBuff );
  105. };
  106. m_pIn_Buffer[m_nIn_NextReadIndex] = in_Buffer[idx];
  107. m_nIn_NextReadIndex++;
  108. //};
  109. if( idx ) {
  110. if( ( in_Buffer[ idx - 1 ] == 0x0A ) && ( in_Buffer[ idx ] == 0x0A ) ) return true;
  111. };
  112. };
  113. } while( ( m_nIn_NextReadIndex < ( UBX_INPUT_BUFFER_SIZE - I2C_PAGE_BYTES ) ) && m_bThreadStatus < TH_ENDED );
  114. if( m_bThreadStatus >= TH_ENDED ) return false;
  115. LOG("Done Read! - Buffer Full!");
  116. return true;
  117. };
  118. bool sensorUBLOX::create(){
  119. if( m_pDevicePath == NULL ) return false;
  120. if( m_pBus != NULL ) delete( m_pBus );
  121. m_pBus = new i2cPort( m_pDevicePath, m_bDeviceAddress );
  122. if( m_pBus != NULL ) {
  123. //LOG("Read Chip ID");
  124. //if( req_Version() ){
  125. // LOG("Success!");
  126. //};
  127. return init();
  128. } else LOG_ERROR("No I2C Bus Created!");
  129. return false;
  130. };
  131. void sensorUBLOX::m_fThreadProc( void ){
  132. m_bThreadStatus = TH_WORKING;
  133. while( m_bThreadStatus < TH_ENDED ){
  134. if( get_Data() ){
  135. LOG("Data arived!");
  136. };
  137. SLEEP( m_nSampleDelay );
  138. };
  139. };
  140. #undef LOG
  141. #undef LOG_ERROR