|
|
#include "main.hpp"
#include <stddef.h>
#include <stdio.h>
#include "main.h"
#include "project.hpp"
//
// #include "sdk/components/single_axis_motor_control_v2/single_axis_motor_control_v2.hpp"
#include "sdk/components/iflytop_can_slave_modules/idcard_reader_service.hpp"
#include "sdk/components/single_axis_motor_control/single_axis_motor_control.hpp"
#include "sdk/hal/zhal.hpp"
#include "sdk\components\iflytop_can_slave_modules\io_control_service.hpp"
#include "sdk\components\iflytop_can_slave_v1\iflytop_can_slave.hpp"
#include "sdk\components\m3078\m3078_code_scaner.hpp"
#include "sdk\components\tmc\ic\ztmc4361A.hpp"
#include "sdk\components\tmc\ic\ztmc5130.hpp"
//
#include "hardware.hpp"
#include "sdk\components\huacheng_sensor\dp600_pressure_sensor.hpp"
#include "sdk\components\zcan_module\huacheng_pressure_sensor.hpp"
#include "sdk\components\zcan_module\zcan_basic_order_module.hpp"
#include "sdk\components\zcan_module\zcan_pump_ctrl_module.hpp"
#include "sdk\components\zcan_module\zcan_trigle_warning_light_ctl_module.hpp"
#define TAG "main"
namespace iflytop { Main gmain; };
using namespace iflytop;
/*******************************************************************************
* TOOLS * *******************************************************************************/ static uint8_t *hex_str_to_bytes(char *data, int32_t len, int32_t &bytelen) { /**
* @brief * data: * 12 34 56 78 90 ab cd ef * */ static uint8_t bytes_cache[1024] = {0}; static uint8_t data_cache[1024] = {0};
int32_t data_len = 0;
memset(bytes_cache, 0, sizeof(bytes_cache)); memset(data_cache, 0, sizeof(data_cache));
for (int32_t i = 0; i < len; i++) { if (data[i] == ' ') continue; if (data[i] == '\r' || data[i] == '\n') break; data_cache[i] = data[i]; data_len++; }
if (data_len % 2 != 0) { ZLOGE(TAG, "data_len %d", data_len); return NULL; }
for (int32_t i = 0; i < data_len; i += 2) { char c1 = data_cache[i]; char c2 = data_cache[i + 1]; if (c1 >= '0' && c1 <= '9') { c1 = c1 - '0'; } else if (c1 >= 'a' && c1 <= 'f') { c1 = c1 - 'a' + 10; } else if (c1 >= 'A' && c1 <= 'F') { c1 = c1 - 'A' + 10; } else { ZLOGE(TAG, "c1 %c", c1); return NULL; }
if (c2 >= '0' && c2 <= '9') { c2 = c2 - '0'; } else if (c2 >= 'a' && c2 <= 'f') { c2 = c2 - 'a' + 10; } else if (c2 >= 'A' && c2 <= 'F') { c2 = c2 - 'A' + 10; } else { ZLOGE(TAG, "c2 %c", c2); return NULL; }
bytes_cache[i / 2] = (c1 << 4) | c2; }
bytelen = data_len / 2; return bytes_cache; }
void dumphexdata(uint8_t *data, int32_t len) { for (int32_t i = 0; i < len; i++) { printf("%02X ", data[i]); } printf("\n"); }
/*******************************************************************************
* GLOBAL * *******************************************************************************/
IflytopCanProtocolStackProcesser m_protocolStack; Hardware m_hardware; ZGPIO debuglight; ZCanReceiver m_canReceiver;
/*******************************************************************************
* MESSAGE_HANDLER * *******************************************************************************/ /**
* @brief 处理CAN接收到消息 */ void Main::onRceivePacket(CanPacketRxBuffer *rxbuf, uint8_t *packet, size_t len) { // ZLOGI(TAG, "onRceivePacket from %d %d", rxbuf->id, len);
static uint8_t rxdata[1024] = {0}; memset(rxdata, 0, sizeof(rxdata)); Cmdheader_t *cmdheader = (Cmdheader_t *)packet; bool match = false; int32_t receipt_size = 0; int32_t ecode = m_hardware.process_rx_packet(Hardware::from_where_t::kcan, packet, len, rxdata, receipt_size, match); if (match) { if (ecode != 0) { m_canReceiver.sendErrorAck(cmdheader, ecode); } else { m_canReceiver.sendAck(cmdheader, rxdata, sizeof(rxdata)); } } } /**
* @brief 处理串口接收到的消息 */ static void processUartRX(uint8_t *packet, size_t len) { static uint8_t rxdata[1024] = {0}; int32_t receipt_size = 0; bool match = false;
memset(rxdata, 0, sizeof(rxdata)); //
int32_t bytelen = 0; uint8_t *hexbytes = hex_str_to_bytes((char *)packet, len, bytelen); if (hexbytes == NULL) { ZLOGE(TAG, "hex_str_to_bytes failed"); return; } dumphexdata(hexbytes, bytelen);
m_hardware.process_rx_packet(Hardware::kuart, packet, len, rxdata, receipt_size, match); }
/*******************************************************************************
* MAIN * *******************************************************************************/ void Main::run() { ZHALCORE::cfg_t oscfg = { .delayhtim = &DELAY_US_TIMER, .debuguart = &DEBUG_UART, }; ZHALCORE::getInstance()->initialize(oscfg);
ZLOGI(TAG, "zapp:%s", VERSION); printf("int32_t %d int %d longint %d\n", sizeof(int32_t), sizeof(int), sizeof(long int));
debuglight.initAsOutput(DEBUG_LIGHT_GPIO, ZGPIO::kMode_nopull, false, false); ZHAL_CORE_REG(200, { debuglight.toggleState(); });
m_hardware.initialize();
static ZUART uartreceiver; static ZUART::cfg_t uartreceiver_cfg = { .name = "uartreceiver", .huart = &DEBUG_UART, .rxbuffersize = 512, .rxovertime_ms = 30, }; uartreceiver.initialize(&uartreceiver_cfg); uartreceiver.setrxcb([this](uint8_t *data, size_t len) { processUartRX(data, len); });
ZCanReceiver::CFG *cfg = m_canReceiver.createCFG(DEVICE_ID); m_canReceiver.init(cfg); m_canReceiver.registerListener(this);
ZLOGI(TAG, "init done"); while (1) { ZHALCORE::getInstance()->loop(); m_hardware.loop(); } }
|