123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- //
- // Created by huli on 2021/12/23.
- //
- #ifndef NNXX_TESTS_LED_PROTOCOL_H
- #define NNXX_TESTS_LED_PROTOCOL_H
- #include <string>
- #include <map>
- #include "../error_code/error_code.h"
- //led的通信协议, 使用tcp通信, 负责封装和解析led的通信消息
- //将需要显示的内容,封装为发送数据.
- //将接受反馈解析为正常的消息.
- //详情请看资料 <<BX-5(M)K&6K(YY)通讯协议用户版.pdf>> 的 3 4 8.2 9 11 这些章节
- class Led_protocol
- {
- public:
- #define LED_DEVICE_TYPE 0x63 //led设备类型, 上海仰邦科技股份有限公司BX-6K3, 默认为0x63
- //led通信的区域数据, 修改每个区域的显示内容
- struct Led_communication_area
- {
- unsigned char m_area_head[29]; //区域头, 默认29个byte
- std::string m_show_string; //需要在区域显示的的字符串,
- std::string m_total_string; //当前结构体, 汇总产生的字符串
- };
- //led通信的控制指令, 直接对led屏生效, 可以操作led的各种配置和显示状态
- //本模块只做了 发送实时显示区域数据 的功能
- struct Led_communication_cmd
- {
- unsigned char m_cmd_head[7]; //指令头, 默认7个byte
- std::map<int, Led_communication_area> m_led_communication_area_map; //led通信的区域数据的容器, 最多支持5个消息的显示
- int m_area_size; //led通信的区域数据的容器 的计数
- std::string m_total_string; //当前结构体, 汇总产生的字符串
- };
- //led通信的物理传输层, 把需要传输的数据封装一层, 后再通过tcp发送
- struct Led_communication_msg
- {
- unsigned char m_frame_head[8]; //帧头, 默认8个0xA5
- unsigned char m_frame_configuration[14]; //包头, 配置参数, 14个byte
- Led_communication_cmd m_led_communication_cmd; //数据域, 也是led通信的控制指令
- unsigned char m_frame_crc[2]; //crc校验码, 2个byte, 包校验为包头数据和数据域的校验值
- unsigned char m_frame_tail[1]; //帧尾, 默认1个0x5A
- std::string m_total_string; //当前结构体, 汇总产生的字符串
- };
- public:
- Led_protocol();
- Led_protocol(const Led_protocol& other)= default;
- Led_protocol& operator =(const Led_protocol& other)= default;
- ~Led_protocol();
- public://API functions
- //led通信协议初始化, area_size必须为1~5
- Error_manager led_protocol_init(int area_size = 0);
- Error_manager led_protocol_uninit();
- Error_manager led_protocol_reset();
- Error_manager led_protocol_delete(int area_index);
- //创建led消息, 得到封装好的通信消息, 参数输入需要, 更新的区域编号, 和显示的字符串即可
- std::string create_led_message(int area_index, std::string show_string, unsigned short x, unsigned short y, unsigned short width, unsigned short heigth);
- public://get or set member variable
- //获取led协议封装后的最终消息, 这个结果就可以直接tcp发送了
- std::string get_led_msg_total_string();
- //获取led协议封装后的最终消息, 这个结果就可以直接tcp发送了
- std::string get_led_msg_total_string(int area_index);
- //设置显示的内容
- Error_manager set_show_string(int area_index, std::string show_string);
- //设置显示的矩形框, 单位像素点, (注:一个汉字为16*16像素)
- Error_manager set_show_rectangle(int area_index, unsigned short x, unsigned short y, unsigned short width, unsigned short heigth);
- //设置显示的行间距
- Error_manager set_lines_sizes(int area_index, unsigned char lines_sizes);
- //设置显示动态区运行模式
- Error_manager set_run_mode(int area_index, unsigned char run_mode, unsigned short time_out);
- //设置显示的排版方式
- Error_manager set_type_setting(int area_index, unsigned char type_setting);
- //设置显示的对齐方式
- Error_manager set_text_alignment(int area_index, unsigned char text_alignment);
- //设置显示 是否单行显示 是否自动换行
- Error_manager set_line_mode(int area_index, unsigned char single_line, unsigned char new_line);
- //设置显示方式, 文字移动的方式
- Error_manager set_display_mode(int area_index, unsigned char display_mode);
- //设置显示速度 显示特技停留时间
- Error_manager set_speed(int area_index, unsigned char speed, unsigned char stay_time);
- protected://member functions
- //led通信的区域数据, 新建, area_size为更新区域的索引编号, area_index 为0~4//map里面没有就新建一个, 有就什么都不做.
- Error_manager led_communication_area_create(int area_index);
- //led通信的控制指令, 初始化, area_size为更新区域的数量, 默认为0
- Error_manager led_communication_cmd_init(int area_size = 0);
- //led通信的物理传输层, 初始化, area_size为更新区域的数量, 默认为0
- Error_manager led_communication_msg_init(int area_size = 0);
- //led通信的区域数据, 通过协议转化, m_led_communication_area 封装成 m_total_string
- Error_manager led_communication_area_encapsulate();
- //led通信的区域数据, 通过协议转化, m_led_communication_area 封装成 m_total_string
- Error_manager led_communication_area_encapsulate(int area_index);
- //led通信的控制指令, 通过协议转化, m_led_communication_cmd 封装成 m_total_string
- Error_manager led_communication_cmd_encapsulate();
- //led通信的控制指令, 通过协议转化, m_led_communication_cmd 封装成 m_total_string
- Error_manager led_communication_cmd_encapsulate(int area_index);
- //led通信的物理传输层, 通过协议转化, m_led_communication_msg 封装成 m_total_string
- Error_manager led_communication_msg_encapsulate();
- //led通信的物理传输层, 通过协议转化, m_led_communication_msg 封装成 m_total_string
- Error_manager led_communication_msg_encapsulate(int area_index);
- //led通信协议的整体封装
- Error_manager led_communication_total_encapsulate();
- //led通信协议的整体封装
- Error_manager led_communication_total_encapsulate(int area_index);
- //crc校验码
- unsigned short crc16(unsigned char * data,int size);
- //字符转义
- void character_escape(unsigned char * p_dst, int * p_dst_length, unsigned char * p_src, int * p_src_length);
- protected://member variable
- public:
- Led_communication_msg m_led_communication_msg; //led通信的物理传输层, 结构体
- bool m_encapsulate_flag; //led通信 封装成功的标记位
- private:
- };
- #endif //NNXX_TESTS_LED_PROTOCOL_H
- /*
- 测试用例
- //led test
- std::string result;
- Led_protocol led_protocol;
- t_error = led_protocol.led_protocol_init();
- std::cout << " huli test :::: " << " t_error = " << t_error << std::endl;
- //方法1, 直接创建, 只需要设置显示内容和矩形框
- result = led_protocol.create_led_message(0, "123456", 0, 0, 0x40, 0x20);
- //方法2, 自定义配置,
- led_protocol.set_show_string(0, "123456");
- led_protocol.set_show_rectangle(0, 0, 0, 0x40, 0x20);
- led_protocol.set_line_mode(0, 1, 1);
- result = led_protocol.get_led_msg_total_string();
- //打印
- unsigned char * ch = (unsigned char * )result.c_str();
- printf("Led_communication_msg string = \n");
- for (int i = 0; i < result.length(); ++i)
- {
- printf("%02x", *(ch+i));
- }
- printf("\n");
- for (int i = 0; i < result.length(); ++i)
- {
- printf("%02x ", *(ch+i));
- }
- printf("\n");
- */
|