led_protocol.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // Created by huli on 2021/12/23.
  3. //
  4. #ifndef NNXX_TESTS_LED_PROTOCOL_H
  5. #define NNXX_TESTS_LED_PROTOCOL_H
  6. #include <string>
  7. #include <map>
  8. #include "../error_code/error_code.h"
  9. //led的通信协议, 使用tcp通信, 负责封装和解析led的通信消息
  10. //将需要显示的内容,封装为发送数据.
  11. //将接受反馈解析为正常的消息.
  12. //详情请看资料 <<BX-5(M)K&6K(YY)通讯协议用户版.pdf>> 的 3 4 8.2 9 11 这些章节
  13. class Led_protocol
  14. {
  15. public:
  16. #define LED_DEVICE_TYPE 0x63 //led设备类型, 上海仰邦科技股份有限公司BX-6K3, 默认为0x63
  17. //led通信的区域数据, 修改每个区域的显示内容
  18. struct Led_communication_area
  19. {
  20. unsigned char m_area_head[29]; //区域头, 默认29个byte
  21. std::string m_show_string; //需要在区域显示的的字符串,
  22. std::string m_total_string; //当前结构体, 汇总产生的字符串
  23. };
  24. //led通信的控制指令, 直接对led屏生效, 可以操作led的各种配置和显示状态
  25. //本模块只做了 发送实时显示区域数据 的功能
  26. struct Led_communication_cmd
  27. {
  28. unsigned char m_cmd_head[7]; //指令头, 默认7个byte
  29. std::map<int, Led_communication_area> m_led_communication_area_map; //led通信的区域数据的容器, 最多支持5个消息的显示
  30. int m_area_size; //led通信的区域数据的容器 的计数
  31. std::string m_total_string; //当前结构体, 汇总产生的字符串
  32. };
  33. //led通信的物理传输层, 把需要传输的数据封装一层, 后再通过tcp发送
  34. struct Led_communication_msg
  35. {
  36. unsigned char m_frame_head[8]; //帧头, 默认8个0xA5
  37. unsigned char m_frame_configuration[14]; //包头, 配置参数, 14个byte
  38. Led_communication_cmd m_led_communication_cmd; //数据域, 也是led通信的控制指令
  39. unsigned char m_frame_crc[2]; //crc校验码, 2个byte, 包校验为包头数据和数据域的校验值
  40. unsigned char m_frame_tail[1]; //帧尾, 默认1个0x5A
  41. std::string m_total_string; //当前结构体, 汇总产生的字符串
  42. };
  43. public:
  44. Led_protocol();
  45. Led_protocol(const Led_protocol& other)= default;
  46. Led_protocol& operator =(const Led_protocol& other)= default;
  47. ~Led_protocol();
  48. public://API functions
  49. //led通信协议初始化, area_size必须为1~5
  50. Error_manager led_protocol_init(int area_size = 0);
  51. Error_manager led_protocol_uninit();
  52. Error_manager led_protocol_reset();
  53. Error_manager led_protocol_delete(int area_index);
  54. //创建led消息, 得到封装好的通信消息, 参数输入需要, 更新的区域编号, 和显示的字符串即可
  55. std::string create_led_message(int area_index, std::string show_string, unsigned short x, unsigned short y, unsigned short width, unsigned short heigth);
  56. public://get or set member variable
  57. //获取led协议封装后的最终消息, 这个结果就可以直接tcp发送了
  58. std::string get_led_msg_total_string();
  59. //获取led协议封装后的最终消息, 这个结果就可以直接tcp发送了
  60. std::string get_led_msg_total_string(int area_index);
  61. //设置显示的内容
  62. Error_manager set_show_string(int area_index, std::string show_string);
  63. //设置显示的矩形框, 单位像素点, (注:一个汉字为16*16像素)
  64. Error_manager set_show_rectangle(int area_index, unsigned short x, unsigned short y, unsigned short width, unsigned short heigth);
  65. //设置显示的行间距
  66. Error_manager set_lines_sizes(int area_index, unsigned char lines_sizes);
  67. //设置显示动态区运行模式
  68. Error_manager set_run_mode(int area_index, unsigned char run_mode, unsigned short time_out);
  69. //设置显示的排版方式
  70. Error_manager set_type_setting(int area_index, unsigned char type_setting);
  71. //设置显示的对齐方式
  72. Error_manager set_text_alignment(int area_index, unsigned char text_alignment);
  73. //设置显示 是否单行显示 是否自动换行
  74. Error_manager set_line_mode(int area_index, unsigned char single_line, unsigned char new_line);
  75. //设置显示方式, 文字移动的方式
  76. Error_manager set_display_mode(int area_index, unsigned char display_mode);
  77. //设置显示速度 显示特技停留时间
  78. Error_manager set_speed(int area_index, unsigned char speed, unsigned char stay_time);
  79. protected://member functions
  80. //led通信的区域数据, 新建, area_size为更新区域的索引编号, area_index 为0~4//map里面没有就新建一个, 有就什么都不做.
  81. Error_manager led_communication_area_create(int area_index);
  82. //led通信的控制指令, 初始化, area_size为更新区域的数量, 默认为0
  83. Error_manager led_communication_cmd_init(int area_size = 0);
  84. //led通信的物理传输层, 初始化, area_size为更新区域的数量, 默认为0
  85. Error_manager led_communication_msg_init(int area_size = 0);
  86. //led通信的区域数据, 通过协议转化, m_led_communication_area 封装成 m_total_string
  87. Error_manager led_communication_area_encapsulate();
  88. //led通信的区域数据, 通过协议转化, m_led_communication_area 封装成 m_total_string
  89. Error_manager led_communication_area_encapsulate(int area_index);
  90. //led通信的控制指令, 通过协议转化, m_led_communication_cmd 封装成 m_total_string
  91. Error_manager led_communication_cmd_encapsulate();
  92. //led通信的控制指令, 通过协议转化, m_led_communication_cmd 封装成 m_total_string
  93. Error_manager led_communication_cmd_encapsulate(int area_index);
  94. //led通信的物理传输层, 通过协议转化, m_led_communication_msg 封装成 m_total_string
  95. Error_manager led_communication_msg_encapsulate();
  96. //led通信的物理传输层, 通过协议转化, m_led_communication_msg 封装成 m_total_string
  97. Error_manager led_communication_msg_encapsulate(int area_index);
  98. //led通信协议的整体封装
  99. Error_manager led_communication_total_encapsulate();
  100. //led通信协议的整体封装
  101. Error_manager led_communication_total_encapsulate(int area_index);
  102. //crc校验码
  103. unsigned short crc16(unsigned char * data,int size);
  104. //字符转义
  105. void character_escape(unsigned char * p_dst, int * p_dst_length, unsigned char * p_src, int * p_src_length);
  106. protected://member variable
  107. public:
  108. Led_communication_msg m_led_communication_msg; //led通信的物理传输层, 结构体
  109. bool m_encapsulate_flag; //led通信 封装成功的标记位
  110. private:
  111. };
  112. #endif //NNXX_TESTS_LED_PROTOCOL_H
  113. /*
  114. 测试用例
  115. //led test
  116. std::string result;
  117. Led_protocol led_protocol;
  118. t_error = led_protocol.led_protocol_init();
  119. std::cout << " huli test :::: " << " t_error = " << t_error << std::endl;
  120. //方法1, 直接创建, 只需要设置显示内容和矩形框
  121. result = led_protocol.create_led_message(0, "123456", 0, 0, 0x40, 0x20);
  122. //方法2, 自定义配置,
  123. led_protocol.set_show_string(0, "123456");
  124. led_protocol.set_show_rectangle(0, 0, 0, 0x40, 0x20);
  125. led_protocol.set_line_mode(0, 1, 1);
  126. result = led_protocol.get_led_msg_total_string();
  127. //打印
  128. unsigned char * ch = (unsigned char * )result.c_str();
  129. printf("Led_communication_msg string = \n");
  130. for (int i = 0; i < result.length(); ++i)
  131. {
  132. printf("%02x", *(ch+i));
  133. }
  134. printf("\n");
  135. for (int i = 0; i < result.length(); ++i)
  136. {
  137. printf("%02x ", *(ch+i));
  138. }
  139. printf("\n");
  140. */