misc.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef MISC_H
  2. #define MISC_H
  3. #define EPS (0.000001)
  4. #define ISZERO(x) (((x<EPS)&&(x>-EPS))?1:0)
  5. #define NONE "\e[0m"
  6. #define BLACK "\e[0;30m"
  7. #define L_BLACK "\e[1;30m"
  8. #define RED "\e[0;31m"
  9. #define L_RED "\e[1;31m"
  10. #define GREEN "\e[0;32m"
  11. #define L_GREEN "\e[1;32m"
  12. #define BROWN "\e[0;33m"
  13. #define YELLOW "\e[1;33m"
  14. #define BLUE "\e[0;34m"
  15. #define L_BLUE "\e[1;34m"
  16. #define PURPLE "\e[0;35m"
  17. #define L_PURPLE "\e[1;35m"
  18. #define CYAN "\e[0;36m"
  19. #define L_CYAN "\e[1;36m"
  20. #define GRAY "\e[0;37m"
  21. #define WHITE "\e[1;37m"
  22. #define BOLD "\e[1m"
  23. #define UNDERLINE "\e[4m"
  24. #define BLINK "\e[5m"
  25. #define REVERSE "\e[7m"
  26. #define HIDE "\e[8m"
  27. #define CLEAR "\e[2J"
  28. #define CLRLINE "\r\e[K"
  29. //use JudgeEnd()
  30. #define BYTEP2INT(a) ((int)((((unsigned char*)(a))[0]) | (((unsigned char*)(a))[1])<<8 | (((unsigned char*)(a))[2])<<16 | (((unsigned char*)(a))[3])<<24))
  31. //#define pu8_to_i16(a) ((short)((((unsigned char*)(a))[0]) | (((unsigned char*)(a))[1])<<8))
  32. #define max(a,b) ( ((a)>(b)) ? (a):(b) )
  33. #define min(a,b) ( ((a)>(b)) ? (b):(a) )
  34. struct tick_t{
  35. int en;
  36. int chken;
  37. int timer;
  38. int chkcnt;
  39. int timeout;
  40. unsigned char to_dev;
  41. unsigned char from_host; /* increase 1 every second when dhg or chg by host */
  42. unsigned char last_from_host;
  43. unsigned char from_dev;
  44. };
  45. struct power_t{
  46. double soc;
  47. double soh;
  48. int active_p; // kW
  49. int active_p_set;
  50. int last_active_p_set;
  51. int norm_cap;
  52. int norm_pow;
  53. int min_pow;
  54. int bdhgable;
  55. int bchgable;
  56. };
  57. //
  58. // some userful bit tricks
  59. //
  60. #define misc_resetbits(x,m) ((x) &= ~(m))
  61. #define misc_setbits(x,m) ((x) |= (m))
  62. #define misc_testbits(x,m) ((x) & (m))
  63. #define misc_bitmask(b) (1<<(b))
  64. #define misc_bit2mask(b1,b2) (misc_bitmask(b1) | misc_bitmask(b2))
  65. #define misc_setbit(x,b) misc_setbits(x, misc_bitmask(b))
  66. #define misc_resetbit(x,b) misc_resetbits(x, misc_bitmask(b))
  67. #define misc_testbit(x,b) misc_testbits(x, misc_bitmask(b))
  68. #define misc_set2bits(x,b1,b2) misc_setbits(x, (misc_bit2mask(b1, b2)))
  69. #define misc_reset2bits(x,b1,b2) misc_resetbits(x, (misc_bit2mask(b1, b2)))
  70. #define misc_test2bits(x,b1,b2) misc_testbits(x, (misc_bit2mask(b1, b2)))
  71. void dump(char* buf_s, unsigned char* buf, int len);
  72. unsigned char CalChecksum8(unsigned char* psrc, int count);
  73. unsigned short calChecksum16(unsigned char* psrc, int count);
  74. unsigned int calChecksum32(unsigned char* psrc, int count);
  75. void StateLed_SetNormal(void);
  76. void StateLed_SetAlert(void);
  77. void Task_StateLed(void);
  78. int JudgeEnd(void);
  79. unsigned short f32_to_u16(float val);
  80. short pu8_to_i16(unsigned char* pval);
  81. void f32_to_pi32(unsigned char* pdst,float fval);
  82. void i32_to_pu8(unsigned char* pdst, int ival);
  83. void f32_to_pi16(unsigned char* pdst,float fval);
  84. int file_size(char* filename);
  85. int misc_thrdPriority(void);
  86. int misc_createDir(const char *sPathName);
  87. int misc_get_datetime(int* y, int* m, int* d, int* h, int* min, int* s);
  88. void misc_get_datetimestr(char* buf, int len);
  89. int misc_daemon(int nochdir, int noclose);
  90. int misc_becomeDaemon(int flags);
  91. double misc_gettimeofday();
  92. void misc_gen_datetimestr(char* buf, int len);
  93. int misc_day_diff(int year_start, int month_start, int day_start, int year_end, int month_end, int day_end);
  94. #endif /* MISC_H */