Guest User

Untitled

a guest
Jun 22nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
  2.  
  3.  
  4. if( CHECK_BIT(received_data[1], 7)) //set or clear LED2
  5. {LATAbits.LATA2=1;}else{LATAbits.LATA2=0;}
  6. if( CHECK_BIT(received_data[5], 7)) //set or clear LED3
  7. {LATAbits.LATA3=1;}else{LATAbits.LATA3=0;}
  8.  
  9. float f1 = *(float*)(received_data+1)
  10. float f2 = *(float*)(received_data+5)
  11.  
  12. union {
  13. char chars[4];
  14. float f;
  15. } u;
  16.  
  17. for (i = 0; i < 4; i++)
  18. u.chars[3-i] = received_data[i+1];
  19. float f1 = u.f;
  20. // ... and similarly for second float
  21.  
  22. float f = *((float*)(&received_data[1]));
  23.  
  24. char *data;
  25.  
  26. //Assemble in the *right* order
  27. data[0] = received_data[4]; // index is machine dependant!
  28. ...
  29.  
  30. // cast
  31. float *fptr = (float *)data;
  32. printf ("%fn",*fptr);
  33.  
  34. struct packet_s {
  35. char byte0;
  36. float f1;
  37. float f2;
  38. ...
  39. }
  40.  
  41. static char* bufferFloat (int* bLen, float value) {
  42. *bLen = sizeof(float);
  43. char* buf = malloc (*bLen);
  44. int storage;
  45.  
  46. memcpy (&storage, &value, sizeof (int));
  47. uint32_t val = htonl (storage);
  48. memcpy (buf, &val, *bLen);
  49.  
  50. return buf;
  51. }
  52.  
  53. static float readFloat (char* buf, int* bLen) {
  54. float ret;
  55. int temp;
  56.  
  57. if (*bLen < sizeof (float)) {
  58. *bLen = 0;
  59. return 0.0f;
  60. }
  61.  
  62. memcpy (&temp, buf, sizeof (int));
  63. uint32_t val = htonl(temp);
  64. memcpy (&ret, &val, sizeof (float));
  65. *bLen = sizeof (float);
  66.  
  67. return ret;
  68. }
  69.  
  70. inline float FloatFromByteArray (const unsigned char * received_data)
  71. {
  72. float f;
  73. memcpy (&f, received_data, sizeof (float));
  74. return f;
  75. }
  76.  
  77. inline float FloatFromByteArray (const unsigned char * received_data)
  78. {
  79. float f;
  80. memcpy (&f, received_data+1, sizeof (float));
  81. return f;
  82. }
Add Comment
Please, Sign In to add comment