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.

155 lines
4.8 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "main.hpp"
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "main.h"
  6. #include "project.hpp"
  7. //
  8. // #include "sdk/components/single_axis_motor_control_v2/single_axis_motor_control_v2.hpp"
  9. #include "sdk/hal/zhal.hpp"
  10. #include "sdk\components\m3078\m3078_code_scaner.hpp"
  11. #include "sdk\components\tmc\ic\ztmc4361A.hpp"
  12. #include "sdk\components\tmc\ic\ztmc5130.hpp"
  13. //
  14. #include "hardware.hpp"
  15. #include "sdk\components\huacheng_sensor\dp600_pressure_sensor.hpp"
  16. #include "sdk\components\string_utils.hpp"
  17. #include "sdk\components\zcan_module\huacheng_pressure_sensor.hpp"
  18. #include "sdk\components\zcan_module\zcan_basic_order_module.hpp"
  19. #include "sdk\components\zcan_module\zcan_pump_ctrl_module.hpp"
  20. #include "sdk\components\zcan_module\zcan_trigle_warning_light_ctl_module.hpp"
  21. #define TAG "main"
  22. namespace iflytop {
  23. Main gmain;
  24. };
  25. using namespace iflytop;
  26. void dumphexdata(uint8_t *data, int32_t len) {
  27. for (int32_t i = 0; i < len; i++) {
  28. printf("%02X ", data[i]);
  29. }
  30. printf("\n");
  31. }
  32. /*******************************************************************************
  33. * GLOBAL *
  34. *******************************************************************************/
  35. Hardware m_hardware;
  36. ZGPIO debuglight;
  37. ZCanReceiver m_canReceiver;
  38. /*******************************************************************************
  39. * MESSAGE_HANDLER *
  40. *******************************************************************************/
  41. /**
  42. * @brief CAN接收到消息
  43. */
  44. void Main::onRceivePacket(CanPacketRxBuffer *rxbuf, uint8_t *packet, size_t len) {
  45. // ZLOGI(TAG, "onRceivePacket from %d %d", rxbuf->id, len);
  46. static uint8_t rxdata[1024] = {0};
  47. memset(rxdata, 0, sizeof(rxdata));
  48. Cmdheader_t *cmdheader = (Cmdheader_t *)packet;
  49. bool match = false;
  50. int32_t receipt_size = 0;
  51. int32_t ecode = m_hardware.process_rx_packet(Hardware::from_where_t::kcan, packet, len, rxdata, receipt_size, match);
  52. if (match) {
  53. if (ecode != 0) {
  54. m_canReceiver.sendErrorAck(cmdheader, ecode);
  55. } else {
  56. m_canReceiver.sendAck(cmdheader, rxdata, receipt_size);
  57. }
  58. }
  59. }
  60. /**
  61. * @brief
  62. */
  63. static void processUARTEachLine(char *packet, size_t len) {
  64. static uint8_t rxdata[1024] = {0};
  65. int32_t receipt_size = 0;
  66. bool match = false;
  67. memset(rxdata, 0, sizeof(rxdata));
  68. //
  69. int32_t bytelen = 0;
  70. uint8_t *hexbytes = StringUtils::hex_str_to_bytes((char *)packet, len, bytelen);
  71. if (hexbytes == NULL) {
  72. ZLOGE(TAG, "hex_str_to_bytes failed");
  73. return;
  74. }
  75. dumphexdata(hexbytes, bytelen);
  76. int32_t ecode = m_hardware.process_rx_packet(Hardware::kuart, hexbytes, bytelen, rxdata, receipt_size, match);
  77. if (match) {
  78. printf("match\n");
  79. if (ecode < 0) {
  80. printf("ecode :%d\n", ecode);
  81. return;
  82. }
  83. dumphexdata(rxdata, receipt_size);
  84. }
  85. printf("\n");
  86. }
  87. static void processUartRX(uint8_t *packet, size_t len) {
  88. for (size_t i = 0; i < len; i++) {
  89. if (packet[i] == '\n' || packet[i] == '\r') {
  90. packet[i] = '\0';
  91. }
  92. }
  93. for (size_t i = 0; i < len; i++) {
  94. if (i == 0) {
  95. processUARTEachLine((char *)packet, strlen((char *)packet));
  96. } else if (packet[i - 1] == '\0' && packet[i] != '\0') {
  97. processUARTEachLine((char *)packet + i, strlen((char *)packet + i));
  98. }
  99. }
  100. }
  101. /*******************************************************************************
  102. * MAIN *
  103. *******************************************************************************/
  104. void Main::run() {
  105. ZHALCORE::cfg_t oscfg = {
  106. .delayhtim = &DELAY_US_TIMER,
  107. .debuguart = &DEBUG_UART,
  108. };
  109. ZHALCORE::getInstance()->initialize(oscfg);
  110. ZLOGI(TAG, "little_disinfection_liquid_path_control:%s", VERSION);
  111. debuglight.initAsOutput(DEBUG_LIGHT_GPIO, ZGPIO::kMode_nopull, false, false);
  112. ZHAL_CORE_REG(200, { debuglight.toggleState(); });
  113. m_hardware.initialize(DEVICE_ID);
  114. static ZUART uartreceiver;
  115. static ZUART::cfg_t uartreceiver_cfg = {
  116. .name = "uartreceiver",
  117. .huart = &DEBUG_UART,
  118. .rxbuffersize = 512,
  119. .rxovertime_ms = 30,
  120. };
  121. uartreceiver.initialize(&uartreceiver_cfg);
  122. uartreceiver.setrxcb([this](uint8_t *data, size_t len) { processUartRX(data, len); });
  123. uartreceiver.startRxIt();
  124. ZCanReceiver::CFG *cfg = m_canReceiver.createCFG(DEVICE_ID);
  125. m_canReceiver.init(cfg);
  126. m_canReceiver.registerListener(this);
  127. ZLOGI(TAG, "init done");
  128. while (1) {
  129. ZHALCORE::getInstance()->loop();
  130. m_hardware.loop();
  131. uartreceiver.forceCchedule();
  132. }
  133. }