utils.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #ifndef _UTILS_H__
  2. #define _UTILS_H__
  3. #include "os/os_types.h"
  4. #include "os/os_task.h"
  5. #include "bsp/delay.h"
  6. static __inline__ u8 decode_u8(u8 *buff) {
  7. return buff[0];
  8. }
  9. static __inline__ u16 decode_u16(u8 *buff) {
  10. return (((u16)(buff[1]) << 8) | buff[0]);
  11. }
  12. static __inline__ u32 decode_u24(u8 *buff) {
  13. return ((u32)(buff[2]) << 16 | decode_u16(buff));
  14. }
  15. static __inline__ u16 decode_u32(u8 *buff) {
  16. return ((u32)(buff[3]) << 24 | decode_u24(buff));
  17. }
  18. static __inline__ void encode_u8(u8 *buff, u8 value)
  19. {
  20. buff[0] = value;
  21. }
  22. static __inline__ void encode_u16(u8 *buff, u16 value)
  23. {
  24. buff[0] = value;
  25. buff[1] = value >> 8;
  26. }
  27. static __inline__ void encode_u24(u8 *buff, u32 value)
  28. {
  29. encode_u16(buff, value);
  30. buff[2] = value >> 16;
  31. }
  32. static __inline__ void encode_u32(u8 *buff, u32 value)
  33. {
  34. encode_u24(buff, value);
  35. buff[3] = value >> 24;
  36. }
  37. static __inline__ s8 decode_s08(const u8 *buff)
  38. {
  39. return ((s8) buff[0]);
  40. }
  41. static __inline__ s16 decode_s16(const u8 *buff)
  42. {
  43. return ((s16) buff[1]) << 8 | buff[0];
  44. }
  45. static __inline__ s32 decode_s24(const u8 *buff)
  46. {
  47. return ((s32) buff[2]) << 16 | decode_s16(buff);
  48. }
  49. static __inline__ s32 decode_s32(const u8 *buff)
  50. {
  51. return ((s32) buff[3]) << 24 | decode_s24(buff);
  52. }
  53. static __inline__ void encode_s08(u8 *buff, s8 value)
  54. {
  55. buff[0] = value;
  56. }
  57. static __inline__ void encode_s16(u8 *buff, s16 value)
  58. {
  59. encode_s08(buff,value&0xFF);
  60. encode_s08(buff+1,value >> 8);
  61. }
  62. static __inline__ void encode_s24(u8 *buff, s32 value)
  63. {
  64. encode_s16(buff, value&0xFF);
  65. encode_s08(buff+2,value >> 16);
  66. }
  67. static __inline__ void encode_s32(u8 *buff, s32 value)
  68. {
  69. encode_s24(buff, value&0xFF);
  70. encode_s08(buff+3,value >> 24);
  71. }
  72. static __inline__ void encode_float(u8 *buff, float value)
  73. {
  74. u8 *p_f = (u8 *)&value;
  75. buff[0] = p_f[0];
  76. buff[1] = p_f[1];
  77. buff[2] = p_f[2];
  78. buff[3] = p_f[3];
  79. }
  80. static __inline__ float decode_float(const u8 *buff)
  81. {
  82. float value;
  83. u8 *p_f = (u8 *)&value;
  84. p_f[0] = buff[0];
  85. p_f[1] = buff[1];
  86. p_f[2] = buff[2];
  87. p_f[3] = buff[3];
  88. return value;
  89. }
  90. #define u8_bits(start, end) ((0xFFU << (start)) & (0xFFU >> (7U - (uint32_t)(end))))
  91. #define decode_8bits(v, start, end) (((v) & u8_bits((start),(end))) >> (start))
  92. #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
  93. #define min(a,b) ((a)>(b)?(b):(a))
  94. #define MAX(x, y) ((x)>(y)?(x):(y))
  95. #define F2I(f) ((int)(f))
  96. #endif /* _UTILS_H__ */