You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.7 KiB

1 year ago
  1. #pragma once
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <windows.h>
  5. #include <fstream>
  6. #include <functional>
  7. #include <iostream>
  8. #include <list>
  9. #include <map>
  10. #include <memory>
  11. #include <set>
  12. #include <sstream>
  13. #include <string>
  14. #include <thread>
  15. #include <vector>
  16. #include "idatachannel.hpp"
  17. namespace iflytop {
  18. #define CANUSB_TTY_BAUD_RATE_DEFAULT 2000000
  19. typedef enum {
  20. CANUSB_SPEED_1000000 = 0x01,
  21. CANUSB_SPEED_800000 = 0x02,
  22. CANUSB_SPEED_500000 = 0x03,
  23. CANUSB_SPEED_400000 = 0x04,
  24. CANUSB_SPEED_250000 = 0x05,
  25. CANUSB_SPEED_200000 = 0x06,
  26. CANUSB_SPEED_125000 = 0x07,
  27. CANUSB_SPEED_100000 = 0x08,
  28. CANUSB_SPEED_50000 = 0x09,
  29. CANUSB_SPEED_20000 = 0x0a,
  30. CANUSB_SPEED_10000 = 0x0b,
  31. CANUSB_SPEED_5000 = 0x0c,
  32. } CANUSB_SPEED;
  33. typedef enum {
  34. CANUSB_MODE_NORMAL = 0x00,
  35. CANUSB_MODE_LOOPBACK = 0x01,
  36. CANUSB_MODE_SILENT = 0x02,
  37. CANUSB_MODE_LOOPBACK_SILENT = 0x03,
  38. } CANUSB_MODE;
  39. typedef enum {
  40. CANUSB_FRAME_STANDARD = 0x01,
  41. CANUSB_FRAME_EXTENDED = 0x02,
  42. } CANUSB_FRAME;
  43. typedef enum {
  44. CANUSB_INJECT_PAYLOAD_MODE_RANDOM = 0,
  45. CANUSB_INJECT_PAYLOAD_MODE_INCREMENTAL = 1,
  46. CANUSB_INJECT_PAYLOAD_MODE_FIXED = 2,
  47. } CANUSB_PAYLOAD_MODE;
  48. typedef struct {
  49. uint8_t packet_header0;
  50. uint8_t packet_header1;
  51. uint8_t type;
  52. uint8_t frame_type;
  53. uint8_t frame_format;
  54. uint8_t frame_id_data_1;
  55. uint8_t frame_id_data_2;
  56. uint8_t frame_id_data_3;
  57. uint8_t frame_id_data_4;
  58. uint8_t frame_data_length;
  59. uint8_t frame_data_1;
  60. uint8_t frame_data_2;
  61. uint8_t frame_data_3;
  62. uint8_t frame_data_4;
  63. uint8_t frame_data_5;
  64. uint8_t frame_data_6;
  65. uint8_t frame_data_7;
  66. uint8_t frame_data_8;
  67. uint8_t reserve;
  68. uint8_t check_code;
  69. } waveshare_can_packet_t;
  70. class WaveshareCan {
  71. public:
  72. typedef struct {
  73. uint32_t id;
  74. uint8_t dlc;
  75. uint8_t data[8];
  76. } can_rx_frame_t;
  77. typedef function<void(can_rx_frame_t*)> frame_callback_t;
  78. private:
  79. unique_ptr<thread> m_thread;
  80. frame_callback_t m_frame_callback;
  81. IDataChannel* m_ch = nullptr;
  82. mutex lock_;
  83. uint8_t m_rxcache[1024] = {0};
  84. uint8_t m_rxlen = 0;
  85. public:
  86. bool init(IDataChannel* ch, frame_callback_t framecb);
  87. void setCanpPrameter(CANUSB_SPEED speed, CANUSB_MODE mode, CANUSB_FRAME frame);
  88. bool sendStdframe(uint32_t id, unsigned char data[], int data_length_code) { return sendframe(CANUSB_FRAME_STANDARD, id, data, data_length_code); }
  89. bool sendExtframe(uint32_t id, unsigned char data[], int data_length_code) { return sendframe(CANUSB_FRAME_EXTENDED, id, data, data_length_code); }
  90. bool sendframe(CANUSB_FRAME frame, uint32_t id, unsigned char data[], int data_length_code);
  91. private:
  92. int onReceivePacket(uint8_t* data, size_t len);
  93. };
  94. } // namespace iflytop