utils.h 2.3 KB

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