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.

619 lines
23 KiB

6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
​
​
6 years ago
​
​
​
6 years ago
​
​
​
6 years ago
​
​
​
​
​
​
​
​
6 years ago
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
​
6 years ago
6 years ago
​
​
​
​
6 years ago
6 years ago
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
6 years ago
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
  1. #include "LRF_TwinConnector.h"
  2. #include "LRF_Kernel.h"
  3. #include "LRF_TargetTracker.h"
  4. #define STB_IMAGE_IMPLEMENTATION
  5. #include "../../data/stbi/stb_image.h"
  6. #define STB_IMAGE_WRITE_IMPLEMENTATION
  7. #include "../../data/stbi/stb_image_write.h"
  8. #undef STB_IMAGE_WRITE_IMPLEMENTATION
  9. #undef STB_IMAGE_IMPLEMENTATION
  10. #define LOG( X ) m_pLog->Log( "TWINC", this, X )
  11. #ifdef DEBUG
  12. #define LOG_ERROR( X ) {\
  13. char _tBfrErrMSG234[256];\
  14. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  15. m_pLog->Log( "TWINC", this, _LOG_ERROR, _tBfrErrMSG234 );\
  16. }
  17. #else
  18. #define LOG_ERROR( X ) m_pLog->Log( "TWINC", this, _LOG_ERROR, X );
  19. #endif
  20. LRF_TwinConnector::LRF_TwinConnector( config_setting_t *Settings ): basicThread(){
  21. m_pLog = basicLogCollection::getInstance();
  22. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, (CSTR)"TWIN_3DLRF_CONNECTION" );
  23. m_pPort = NULL;
  24. m_pEngine = NULL;
  25. m_nStatus = 0;
  26. #ifdef PRU_SLAVE
  27. m_nMasterPort = 0;
  28. m_pMasterAddress = (CSTR) malloc( 16 );
  29. m_bWorkingMode = TWI_STAGE_WAITING_CONNECTION;
  30. #else
  31. m_nMainSocket = -1;
  32. m_nBindPort = 0;
  33. m_pBindAddress = (CSTR) malloc( 16 );
  34. m_bWorkingMode = TWI_STAGE_BEFORE_BIND;
  35. #endif
  36. };
  37. LRF_TwinConnector::~LRF_TwinConnector(){
  38. if( m_pPort != NULL ) delete( m_pPort );
  39. #ifdef PRU_SLAVE
  40. if( m_pMasterAddress ) free( m_pMasterAddress );
  41. #else
  42. if( m_pBindAddress ) free( m_pBindAddress );
  43. #endif
  44. };
  45. bool LRF_TwinConnector::create(){
  46. if( m_pSettings!= NULL ) {
  47. config_setting_t *pSetting;
  48. int tVal;
  49. char tBuffer[256];
  50. #ifdef PRU_SLAVE
  51. if (!config_setting_lookup_string(m_pSettings, "MASTER_ADDRESS", (const char**)&m_pMasterAddress)) {
  52. strncpy( m_pMasterAddress, "10.0.0.1", 15 );
  53. pSetting = config_setting_add(m_pSettings, "MASTER_ADDRESS", CONFIG_TYPE_STRING);
  54. config_setting_set_string(pSetting, m_pMasterAddress);
  55. basicSettingsProvider::getInstance()->compromise();
  56. } else {
  57. sprintf( tBuffer, "MASTER IP Address Read From Settings: %s", m_pMasterAddress );
  58. LOG( tBuffer );
  59. };
  60. if( !config_setting_lookup_int( m_pSettings, "MASTER_PORT", &tVal ) ){
  61. tVal = m_nMasterPort = 6564;
  62. pSetting = config_setting_add( m_pSettings, "MASTER_PORT", CONFIG_TYPE_INT );
  63. config_setting_set_bool( pSetting, tVal );
  64. basicSettingsProvider::getInstance()->compromise();
  65. } else {
  66. m_nMasterPort = tVal;
  67. sprintf( tBuffer, "MASTER PORT Read From Settings: %d", tVal );
  68. LOG( tBuffer );
  69. };
  70. #else
  71. if (!config_setting_lookup_string(m_pSettings, "BIND_ADDRESS", (const char**)&m_pBindAddress)) {
  72. strncpy( m_pBindAddress, "10.0.0.1", 15 );
  73. pSetting = config_setting_add(m_pSettings, "BIND_ADDRESS", CONFIG_TYPE_STRING);
  74. config_setting_set_string(pSetting, m_pBindAddress);
  75. basicSettingsProvider::getInstance()->compromise();
  76. } else {
  77. sprintf( tBuffer, "BIND to IP Address Read From Settings: %s", m_pBindAddress );
  78. LOG( tBuffer );
  79. };
  80. if( !config_setting_lookup_int( m_pSettings, "BIND_PORT", &tVal ) ){
  81. tVal = m_nBindPort = 6564;
  82. pSetting = config_setting_add( m_pSettings, "BIND_PORT", CONFIG_TYPE_INT );
  83. config_setting_set_bool( pSetting, tVal );
  84. basicSettingsProvider::getInstance()->compromise();
  85. } else {
  86. m_nBindPort = tVal;
  87. sprintf( tBuffer, "BIND to PORT Read From Settings: %d", tVal );
  88. LOG( tBuffer );
  89. };
  90. #endif
  91. if( basicSettingsProvider::getInstance()->isCompromised() ) {
  92. LOG("Save Settings");
  93. basicSettingsProvider::getInstance()->validate();
  94. };
  95. } else {
  96. LOG_ERROR("No Settings Provided!");
  97. return false;
  98. };
  99. #ifdef PRU_SLAVE
  100. //m_pProtocol = new protocolInternalLRF3D_SLAVE();
  101. //if( m_pProtocol == NULL ){
  102. // LOG_ERROR("Error creating protocol!");
  103. // return false;
  104. //};
  105. if( !protocolInternalLRF3D_SLAVE::create() ) return false;
  106. #else
  107. int RetCode;
  108. struct hostent * h_info;
  109. struct sockaddr_in * MainSocketAddress = NULL;
  110. //m_pProtocol = new protocolInternalLRF3D_MASTER();
  111. //if( m_pProtocol == NULL ){
  112. // LOG_ERROR("Error creating protocol!");
  113. // return false;
  114. //};
  115. if( !protocolInternalLRF3D_MASTER::create() ) return false;
  116. m_nMainSocket = socket( AF_INET, SOCK_STREAM, 0 );
  117. if( m_nMainSocket == -1){
  118. LOG_ERROR( "Some trouble with networking on this machine (Socket Error)!");
  119. return false;
  120. };
  121. MainSocketAddress = ( struct sockaddr_in * ) malloc( sizeof( struct sockaddr_in ) );
  122. if( MainSocketAddress == NULL ){
  123. LOG_ERROR( "Some trouble with networking on this machine (Cannot Alloc Sockaddr Struct)!" );
  124. return false;
  125. };
  126. bzero( MainSocketAddress, sizeof( struct sockaddr_in ) );
  127. h_info = gethostbyname ( m_pBindAddress );
  128. if ( h_info == NULL ){
  129. LOG_ERROR( "Some trouble with networking on this machine (Cannot Get Hostname)!" );
  130. return false;
  131. };
  132. //*****************************************
  133. //* there we building our network address *
  134. //*****************************************
  135. bcopy( h_info->h_addr, &(MainSocketAddress->sin_addr), h_info->h_length );
  136. MainSocketAddress->sin_family = h_info->h_addrtype;
  137. MainSocketAddress->sin_port = htons( m_nBindPort );
  138. RetCode = bind( m_nMainSocket ,(struct sockaddr *) MainSocketAddress, sizeof( struct sockaddr_in ) );
  139. if ( RetCode != 0 ){
  140. LOG_ERROR( "Error Binding to Port/Address" );
  141. return false;
  142. };
  143. RetCode = listen( m_nMainSocket, 128);
  144. if ( RetCode != 0 ){
  145. LOG_ERROR( "Cannot Listen Port/Address" );
  146. return false;
  147. };
  148. U8 dontblock;
  149. dontblock = fcntl( m_nMainSocket, F_GETFL, NULL );
  150. dontblock |= O_NONBLOCK | O_NDELAY;
  151. RetCode = ioctl( m_nMainSocket, FIONBIO, (char *) &dontblock );
  152. if ( RetCode == 0 ){
  153. LOG( "Set N-Blocking Mode of Accepting Socket" );
  154. } else {
  155. LOG( "Can't set N-Blocking Mode of Accepting Socket" );
  156. };
  157. #endif // PRU_SLAVE
  158. m_bWorkingMode = TWI_STAGE_WAITING_CONNECTION;
  159. return init();
  160. };
  161. #ifdef PRU_SLAVE
  162. bool LRF_TwinConnector::packetHandler( protocolPackage *Package ){
  163. if( Package == NULL ) return false;
  164. TWIN_COMMAND cCMD = (TWIN_COMMAND) Package->m_pData[3];
  165. switch( cCMD ) {
  166. case TWC_GET_STATUS:{
  167. sendStatus();
  168. break;
  169. };
  170. };
  171. return true;
  172. };
  173. void LRF_TwinConnector::m_fThreadProc( void ){
  174. TWIN_CMD_REC * tCMD = NULL;
  175. int ClientSocket = -1;
  176. struct sockaddr ClientAddress;
  177. socklen_t ClientAddressSize;
  178. m_bThreadStatus = TH_WORKING;
  179. while( m_bThreadStatus < TH_ENDED ){
  180. if( m_bCCMD == TWC_NO_COMMAND && tCMD == NULL ){
  181. // No CMD is EXEC so check new cmds
  182. CMD_PROTEC.lock();
  183. if( m_qCMD.size() > 0 ){
  184. tCMD = m_qCMD.front();
  185. m_qCMD.pop();
  186. };
  187. CMD_PROTEC.unlock();
  188. };
  189. if( m_pPort != NULL ){
  190. if( m_pPort->getConnectionStatus() == CS_DISCONNECTED ){
  191. m_bWorkingMode = TWI_STAGE_WAITING_CONNECTION;
  192. if( tCMD!= NULL ){
  193. //reset commands on disconnect
  194. m_bCCMD = TWC_NO_COMMAND;
  195. if( tCMD->PTR == NULL ) free( tCMD->PTR );
  196. free( tCMD );
  197. tCMD = NULL;
  198. };
  199. delete( m_pPort );
  200. m_pPort = NULL;
  201. continue;
  202. };
  203. if( tCMD!= NULL ){
  204. if( m_bCCMD == TWC_NO_COMMAND ){
  205. // CMD not issued yet!
  206. switch( tCMD->CMD ){
  207. case TWC_PUSH_TARGET:{
  208. break;
  209. };
  210. };
  211. } else {
  212. // Waiting CMD exe status
  213. switch( m_bCCMD ){
  214. case TWC_SET_THDRAW:{
  215. break;
  216. };
  217. };
  218. if( m_bCCMD == TWC_NO_COMMAND && tCMD != NULL ){
  219. // The command is finished - Will fetch next command from queue
  220. if( tCMD->PTR == NULL ) free( tCMD->PTR );
  221. free( tCMD );
  222. tCMD = NULL;
  223. };
  224. };
  225. SLEEP( 5 );
  226. } else {
  227. SLEEP( 100 );
  228. };
  229. } else {
  230. LOG("(re)Establish Connection");
  231. m_pPort = new clientPort( m_pMasterAddress, m_nMasterPort, true );
  232. if( m_pPort!= NULL ){
  233. setTransport( m_pPort );
  234. m_bWorkingMode = TWI_STAGE_CONNECTED;
  235. };
  236. };
  237. };
  238. };
  239. #else //Master
  240. bool LRF_TwinConnector::packetHandler( protocolPackage *Package ){
  241. if( Package == NULL ) return false;
  242. TWIN_COMMAND cCMD = (TWIN_COMMAND)Package->m_pData[3];
  243. switch( cCMD ) {
  244. case TWC_ACK_STATUS:{
  245. uint32_t tStatus;
  246. #ifdef DEBUG
  247. LOG("Status ACK reseived");
  248. #endif
  249. tStatus = ( Package->m_pData[4] << 24 ) | ( Package->m_pData[5] << 16 ) | ( Package->m_pData[6] << 8 ) | Package->m_pData[7];
  250. m_mStatusProtector.lock();
  251. m_nStatus = tStatus;
  252. m_mStatusProtector.unlock();
  253. break;
  254. };
  255. case TWC_TH_DATA:{
  256. };
  257. };
  258. m_mLastTypeProtector.lock();
  259. m_bLastPackageType = cCMD;
  260. m_mLastTypeProtector.unlock();
  261. return true;
  262. };
  263. void LRF_TwinConnector::m_fThreadProc( void ){
  264. int ClientSocket = -1;
  265. struct sockaddr ClientAddress;
  266. socklen_t ClientAddressSize;
  267. long long cMilliseconds;
  268. long long cStatusTO = 0;
  269. long long cDrawTO = 0;
  270. m_bThreadStatus = TH_WORKING;
  271. while( m_bThreadStatus < TH_ENDED ){
  272. cMilliseconds = getMilliseconds();
  273. // TH Saving procedure
  274. if( cDrawTO == 0 ) {
  275. cDrawTO = cMilliseconds;
  276. } else if ( ( cMilliseconds - cDrawTO ) > TWI_DRAW_LOOP_MS ){
  277. cDrawTO = cMilliseconds;
  278. if( m_pEngine!= NULL ) {
  279. // Store Local TH Draws
  280. thresholdNode *tMem;
  281. uint32_t tSize;
  282. for( uint8_t i=0; i<16; i++ ) {
  283. // just in case if we must end work
  284. if( m_bThreadStatus != TH_WORKING ) break;
  285. tMem = m_pEngine->getTreshold( tSize, i );
  286. if( tMem!= NULL ) {
  287. char tBuffer[256];
  288. sprintf( tBuffer, "data/th_data_%d.png", i );
  289. if( !saveTHPicture( tMem, tBuffer, false )){
  290. LOG_ERROR("TH PNG save failure!");
  291. };
  292. };
  293. };
  294. };
  295. };
  296. // Port exists - SLAVE is connected
  297. if( m_pPort != NULL ){
  298. // Check if still connected
  299. if( m_pPort->getConnectionStatus() == CS_DISCONNECTED ){
  300. // Handle Disconnection
  301. m_bWorkingMode = TWI_STAGE_WAITING_CONNECTION;
  302. delete( m_pPort );
  303. m_pPort = NULL;
  304. continue;
  305. };
  306. if( m_bCCMD == TWC_NO_COMMAND ){
  307. // No Current Request Issued
  308. switch( m_bWorkingMode ) {
  309. case TWI_STAGE_CONNECTED:{
  310. if( requestSoftwareVersion() ){
  311. m_bCCMD = TWC_ACK_VERSION;
  312. };
  313. break;
  314. };
  315. case TWI_STAGE_INIT_1:{
  316. if( requestCurrentSettings() ){
  317. m_bCCMD = TWC_ACK_CONFIG;
  318. };
  319. break;
  320. };
  321. case TWI_STAGE_INIT_2:{
  322. if( requestShadow() ){
  323. m_bCCMD = TWC_ACK_SHADOW;
  324. };
  325. break;
  326. };
  327. case TWI_STAGE_MAIN_LOOP:{
  328. if( ( cMilliseconds - cStatusTO ) > TWI_STATUS_LOOP_MS ) {
  329. cStatusTO = cMilliseconds;
  330. requestStatus();
  331. m_bCCMD = TWC_ACK_STATUS;
  332. LOG("Req Status");
  333. } else {
  334. SLEEP( TWI_SLOW_LOOP_SLEEP );
  335. }
  336. break;
  337. };
  338. };
  339. } else {
  340. // Waiting CMD ACK status
  341. if( getLastPackageType() == m_bCCMD ) {
  342. switch( m_bCCMD ){
  343. case TWC_ACK_VERSION:{
  344. m_bWorkingMode = TWI_STAGE_INIT_1;
  345. break;
  346. };
  347. case TWC_ACK_CONFIG:{
  348. m_bWorkingMode = TWI_STAGE_INIT_2;
  349. break;
  350. };
  351. case TWC_ACK_SHADOW:{
  352. m_bWorkingMode = TWI_STAGE_MAIN_LOOP;
  353. break;
  354. };
  355. case TWC_ACK_STATUS:{
  356. #ifdef DEBUG
  357. LOG("Registered Ack Status");
  358. #endif
  359. //uint32_t nStatus = getStatus();
  360. if( ( m_nStatus & TWI_STATUS_TARGET_MASK ) > 0 ){
  361. #ifdef DEBUG
  362. LOG("Have new Targets!");
  363. #endif
  364. //m_pProtocol->setTData( m_pTargets );
  365. requestTargets();
  366. m_bCCMD = TWC_GET_TARGETS;
  367. continue;
  368. };
  369. break;
  370. };
  371. case TWC_GET_TARGETS:{
  372. if( getLastPackageType() == TWC_TARGETS_DATA ){
  373. m_bCCMD = TWC_NO_COMMAND;
  374. #ifdef DEBUG
  375. LOG("Registered Ack Targets");
  376. #endif
  377. if( m_pTracker != NULL ) {
  378. uint8_t nCNT = (( m_nStatus & TWI_STATUS_TARGET_MASK ) >> 8 );
  379. for( uint8_t index = 0; index < nCNT; index++ ){
  380. m_pTracker->validateTarget( &m_pTargets[index], false );
  381. };
  382. };
  383. };
  384. break;
  385. };
  386. };
  387. m_bCCMD = TWC_NO_COMMAND;
  388. };
  389. SLEEP( TWI_QUICK_LOOP_SLEEP );
  390. };
  391. } else {
  392. // (re)Establish Connection
  393. ClientAddressSize = (socklen_t) sizeof( struct sockaddr_in );
  394. ClientSocket = accept ( m_nMainSocket, &ClientAddress, &ClientAddressSize );
  395. if ( ClientSocket == -1 ) {
  396. switch( errno ){
  397. case EAGAIN:{
  398. SLEEP(10);
  399. continue;
  400. };
  401. //case EWOULDBLOCK: me->Log(" The socket is marked non-blocking and no connections are present to be accepted."); break;
  402. case EBADF: LOG_ERROR( "The descriptor is invalid." ); break;
  403. case ECONNABORTED: LOG_ERROR( "A connection has been aborted."); break;
  404. case EINTR: LOG_ERROR( "The system call was interrupted by a signal that was caught before a valid connection arrived."); break;
  405. case EINVAL: LOG_ERROR( "Socket is not listening for connections, or addrlen is invalid (e.g., is negative)."); break;
  406. case EMFILE: LOG_ERROR( "The per-process limit of open file descriptors has been reached."); break;
  407. case ENFILE: LOG_ERROR( "The system limit on the total number of open files has been reached."); break;
  408. case ENOTSOCK: LOG_ERROR( "The descriptor references a file, not a socket."); break;
  409. case EOPNOTSUPP: LOG_ERROR( "The referenced socket is not of type SOCK_STREAM."); break;
  410. case EFAULT: LOG_ERROR( "The addr argument is not in a writable part of the user address space."); break;
  411. case ENOBUFS:
  412. case ENOMEM: LOG_ERROR( "Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory."); break;
  413. case EPROTO: LOG_ERROR( "Protocol error."); break;
  414. case EPERM: LOG_ERROR( "Firewall rules forbid connection."); break;
  415. case ENOSR:
  416. case ESOCKTNOSUPPORT:
  417. case EPROTONOSUPPORT:
  418. case ETIMEDOUT: LOG_ERROR( " Unusual error in Kernel (Timeout, No Socket Support in Kernel, Unsupported Protocol)"); break;
  419. //case ERESTARTSYS: me->Log(" Unusual error by Kernel"); break;
  420. default: LOG_ERROR( " Unhandled error in Kernel"); break;
  421. };
  422. m_bThreadStatus = TH_ENDED;
  423. } else {
  424. char tstr[CQUERY];
  425. inet_ntop( AF_INET, &(((struct sockaddr_in*) &(ClientAddress ))->sin_addr ), tstr, CQUERY );
  426. LOG( "SLAVE LRF Connection Accepted" );
  427. m_pPort = new serverPort( ClientSocket );
  428. };
  429. if( m_pPort!= NULL ){
  430. setTransport( m_pPort );
  431. // Reset Stage
  432. m_bWorkingMode = TWI_STAGE_CONNECTED;
  433. };
  434. };
  435. };
  436. };
  437. bool LRF_TwinConnector::attachTargetTracker( LRF_TargetTracker *ptr ){
  438. if(ptr == NULL) return false;
  439. m_pTracker = ptr;
  440. return false;
  441. };
  442. bool LRF_TwinConnector::updateSettings( void ){
  443. return true;
  444. };
  445. #endif //!PRU_SLAVE
  446. bool LRF_TwinConnector::cleanTHData( uint8_t kid ){
  447. char tBuffer[1024];
  448. FILE *FID = NULL;
  449. if( kid > 31 ) kid = 31;
  450. sprintf( tBuffer, "data/th_data_%d.raw", kid );
  451. if( !fileExist( "data" ) ){
  452. int RetVal = mkdir( "data", STORAGE_DIR_MODE );
  453. if( RetVal < 0 ) {
  454. LOG_ERROR( "Error Creating DATA DIR!" );
  455. return false;
  456. };
  457. };
  458. FID = fopen( tBuffer, "wb+" );
  459. if( FID == NULL ){
  460. LOG_ERROR( "Error Creating DATA FILE!" );
  461. return false;
  462. };
  463. uint32_t ZZ = 0;
  464. for( uint32_t i = 0; i < TH_SIZE_NODES; i++ ){
  465. fwrite( &ZZ, 4, 1, FID );
  466. };
  467. fclose( FID );
  468. return true;
  469. };
  470. bool LRF_TwinConnector::loadTHData( uint8_t kid, thresholdNode *ptr ){
  471. FILE *FID;
  472. char tBuffer[1024];
  473. if( ptr == NULL ) return false;
  474. if( kid > 31 ) kid = 31;
  475. sprintf( tBuffer, "data/th_data_%d.raw", kid );
  476. FID = fopen( tBuffer, "rb" );
  477. if( FID == NULL ) {
  478. LOG_ERROR( "Error Opening DATA FILE!" );
  479. return false;
  480. };
  481. for( uint32_t i = 0; i < TH_SIZE_NODES; i++ ){
  482. fread( &ptr[i], TH_NODE_SIZE, 1, FID );
  483. };
  484. fclose( FID );
  485. return true;
  486. };
  487. bool LRF_TwinConnector::saveTHData( uint8_t kid, thresholdNode *ptr ){
  488. FILE *FID;
  489. char tBuffer[1024];
  490. if( ptr == NULL ) return false;
  491. if( kid > 31 ) kid = 31;
  492. sprintf( tBuffer, "data/th_data_%d.raw", kid );
  493. FID = fopen( tBuffer, "wb+" );
  494. if( FID == NULL ) {
  495. LOG_ERROR( "Error Creating DATA FILE!" );
  496. return false;
  497. };
  498. for( uint32_t i = 0; i < TH_SIZE_NODES; i++ ){
  499. fwrite( &ptr[i], TH_NODE_SIZE, 1, FID );
  500. };
  501. fclose( FID );
  502. return true;
  503. };
  504. bool LRF_TwinConnector::saveTHPicture( thresholdNode *ptr, CSTR fileName, bool monochrome ){
  505. uint8_t *tMemory = NULL;
  506. thresholdNode *cNode = NULL;
  507. uint8_t *cPixel = NULL;
  508. uint8_t cSpace;
  509. if( monochrome ) {
  510. cSpace = 1;
  511. } else {
  512. cSpace = 4;
  513. };
  514. tMemory = (uint8_t *) malloc( TH_SIZE_NODES * cSpace );
  515. if( tMemory == NULL ){
  516. return false;
  517. };
  518. #ifndef PRU_SLAVE
  519. for( int j = 0; j < Q_SCAN_COUNT; j++ ) {
  520. for ( int i = 0; i < TH_LINE_NODES; i++ ) {
  521. cNode = ptr+(j*i);
  522. //cNode = ptr+( TH_LINE_NODES * j + i );
  523. #else
  524. for( int j = (Q_SCAN_COUNT-1); j < -1; j++ ) {
  525. for ( int i = 0; i < TH_LINE_NODES; i++ ) {
  526. cNode = ptr+( TH_LINE_NODES * j + i );
  527. #endif
  528. cPixel = tMemory+(cSpace*i*j);
  529. if( monochrome ) {
  530. #ifdef CHECK_SUN_BIT
  531. if( cNode->values.sbit ) {
  532. cPixel[0] = 0;
  533. } else {
  534. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  535. };
  536. #else
  537. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  538. #endif
  539. } else {
  540. #ifdef CHECK_SUN_BIT
  541. if( cNode->values.sbit ) {
  542. // Set Red
  543. cPixel[0] = 0x00;
  544. cPixel[1] = 0xff;
  545. cPixel[2] = 0x00;
  546. cPixel[3] = 0xff;
  547. } else {
  548. if( cNode->values.cnt == 0 ) {
  549. // Set Blue
  550. cPixel[0] = 0x00;
  551. cPixel[1] = 0x00;
  552. cPixel[2] = 0xff;
  553. cPixel[3] = 0xff;
  554. } else {
  555. uint32_t tColor;
  556. float tVal = ((float) cNode->values.min ) / 4096.f;
  557. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  558. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  559. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  560. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  561. cPixel[3] = tColor & 0xFF;
  562. };
  563. };
  564. #else
  565. if( cNode->values.cnt == 0 ) {
  566. // Set Blue
  567. cPixel[0] = 0x00;
  568. cPixel[1] = 0x00;
  569. cPixel[2] = 0xff;
  570. cPixel[3] = 0xff;
  571. } else {
  572. uint32_t tColor;
  573. float tVal = ((float) cNode->values.min ) / 4096.f;
  574. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  575. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  576. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  577. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  578. cPixel[3] = tColor & 0xFF;
  579. };
  580. #endif
  581. };
  582. };
  583. };
  584. int RetVal = stbi_write_png( fileName, TH_LINE_NODES, Q_SCAN_COUNT, cSpace, tMemory, TH_LINE_NODES * cSpace );
  585. free( tMemory );
  586. if( RetVal == 0 ) return false;
  587. return true;
  588. };
  589. #undef LOG
  590. #undef LOG_ERROR