commit dbdfce1070c28400346d455da9862c34c767d4fc Author: zhaohe Date: Tue Aug 27 14:47:27 2024 +0800 init diff --git a/app_protocols.h b/app_protocols.h new file mode 100644 index 0000000..45b5e16 --- /dev/null +++ b/app_protocols.h @@ -0,0 +1,9 @@ +#pragma once +#include "bean/hand_acid_mode.h" +#include "bean/hand_acid_remoter_key_event.h" +#include "appecode/errorcode.h" +// +#include "zscanprotocol/zscanprotocol.h" +#include "hand_acid_val_protocol/hand_acid_val_protocol.h" +#include "ble_through/ble_proto.h" +#include "ble_through/ble_proto_utils.h" \ No newline at end of file diff --git a/appecode/errorcode.c b/appecode/errorcode.c new file mode 100644 index 0000000..db148c2 --- /dev/null +++ b/appecode/errorcode.c @@ -0,0 +1,53 @@ +#include "errorcode.h" + +#define ERR2STR(code) \ + case code: \ + return #code; + + +#define ERR_ITERM(enum) \ + { enum, #enum } + +static ecode_table_item_t table[] = { + + ERR_ITERM(ksucc), + ERR_ITERM(kfail), + ERR_ITERM(kparam_out_of_range), + ERR_ITERM(kcmd_not_support), + ERR_ITERM(kdevice_is_busy), + ERR_ITERM(kdevice_is_offline), + ERR_ITERM(kovertime), + ERR_ITERM(knoack), + ERR_ITERM(kerrorack), + ERR_ITERM(kdevice_offline), + ERR_ITERM(ksubdevice_overtime), + ERR_ITERM(kbuffer_not_enough), + ERR_ITERM(kcmd_param_num_error), + ERR_ITERM(kcheckcode_is_error), + ERR_ITERM(killegal_operation), + + ERR_ITERM(kstep_motor_not_found_zero_point), + ERR_ITERM(kstep_motor_not_go_zero), + ERR_ITERM(kstep_motor_over_temperature), + ERR_ITERM(kstep_motor_over_voltage), + ERR_ITERM(kstep_motor_run_overtime), + ERR_ITERM(kstep_motor_not_enable), + ERR_ITERM(kstep_motor_ioindex_out_of_range), + ERR_ITERM(kstep_motor_subic_reset), + ERR_ITERM(kstep_motor_drv_err), + ERR_ITERM(kstep_motor_uv_cp), + ERR_ITERM(kstep_motor_not_found_point_edge), +}; + +const char* error2str(int32_t code) { + for (int i = 0; i < sizeof(table) / sizeof(table[0]); i++) { + if (table[i].index == code) { + return table[i].info; + } + } + return "unknown error"; +} + +ecode_table_item_t* error_get_table() { return table; } +int error_get_table_size() { return sizeof(table) / sizeof(table[0]); } + diff --git a/appecode/errorcode.h b/appecode/errorcode.h new file mode 100644 index 0000000..d7c3ed4 --- /dev/null +++ b/appecode/errorcode.h @@ -0,0 +1,55 @@ +#pragma once +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define ERROR_CODE(errortype, suberrorcode) (errortype + suberrorcode) + +typedef enum { + + ksucc = 0, + kfail = 1, + + kparam_out_of_range = 102, + kcmd_not_support = 103, + kdevice_is_busy = 104, + kdevice_is_offline = 105, + kovertime = 106, + knoack = 107, + kerrorack = 108, + kdevice_offline = 109, + ksubdevice_overtime = 111, + kbuffer_not_enough = 112, + kcmd_param_num_error = 114, + kcheckcode_is_error = 115, + killegal_operation = 116, + + kstep_motor_not_found_zero_point = 600, + kstep_motor_not_go_zero = 601, + kstep_motor_over_temperature = 602, + kstep_motor_over_voltage = 603, + kstep_motor_run_overtime = 604, + kstep_motor_not_enable = 605, + kstep_motor_ioindex_out_of_range = 606, + kstep_motor_subic_reset = 607, + kstep_motor_drv_err = 608, + kstep_motor_uv_cp = 609, + kstep_motor_not_found_point_edge = 610, + +} error_t; + +typedef struct { + int index; + const char* info; +} ecode_table_item_t; + +const char* error2str(int32_t code); + +ecode_table_item_t* error_get_table(); +int error_get_table_size(); + +#ifdef __cplusplus +} +#endif diff --git a/bean/hand_acid_mode.c b/bean/hand_acid_mode.c new file mode 100644 index 0000000..e69de29 diff --git a/bean/hand_acid_mode.h b/bean/hand_acid_mode.h new file mode 100644 index 0000000..0d4dd1e --- /dev/null +++ b/bean/hand_acid_mode.h @@ -0,0 +1,6 @@ +#pragma once +typedef enum { + khand_acid_m_unset_mode = 0, // unset_mode + khand_acid_m_jog_mode = 1, // jog_mode + khand_acid_m_continuous_mode = 2, // continuous_mode +} hand_acid_mode_t; \ No newline at end of file diff --git a/bean/hand_acid_remoter_key_event.h b/bean/hand_acid_remoter_key_event.h new file mode 100644 index 0000000..75170e5 --- /dev/null +++ b/bean/hand_acid_remoter_key_event.h @@ -0,0 +1,7 @@ +#pragma once +typedef enum { + hand_acid_remoter_kevent_add_liquid = 0, // + hand_acid_remoter_kevent_change_next_mode = 1, // + hand_acid_remoter_kevent_reflux = 2, // + hand_acid_remoter_kevent_preFilling = 3, // +} hand_acid_remoter_key_event_t; diff --git a/ble_through/ble_proto.h b/ble_through/ble_proto.h new file mode 100644 index 0000000..5aa17d6 --- /dev/null +++ b/ble_through/ble_proto.h @@ -0,0 +1,93 @@ +#pragma once +#include +#include +// + +#ifdef __cplusplus +extern "C" { +#endif +#define PACKET_H1 (0xAA) +#define PACKET_H2 (0xBB) +#define PACKET_TAIL (0xCC) +typedef enum { + kzble_proto_cmd = 1, + kzble_proto_cmd_receipt = 2, + kzble_proto_report = 3, + kzble_proto_error_receipt = 4, +} zble_proto_packet_type_t; + +typedef enum { + kzble_upper = 1, + kzble_master = 2, + kzble_slave = 3, +} zble_proto_role_t; + +/*********************************************************************************************************************** + * ENUM * + ***********************************************************************************************************************/ + +#pragma pack(1) +typedef struct { + uint8_t h1; + uint8_t h2; + uint8_t packetlen; + uint8_t frameType; + uint8_t frameIndex; + uint16_t cmd; + uint8_t from; + uint8_t to; + uint8_t data[]; +} zble_proto_packet_t; + +typedef enum { + + kzble_cmd_reset = 100, // + kzble_cmd_read_version = 101, // param:{} receipt:ble_master_read_version_t + kzble_cmd_start_scan = 102, // param:zble_start_scan_t receipt:{} + kzble_cmd_stop_scan = 103, // param:{} receipt:{} + kzble_cmd_set_in_dfu_mode = 104, // param:{} receipt:{} + kzble_cmd_clear_reset_flag = 105, // + + kzble_report_connected_event = 150, // zble_connected_event_t + kzble_report_scan_result = 151, // zble_scan_result_t + kzble_report_power_on = 152, // + kzble_report_heart = 153, // zble_heart_t + + kzble_app_cmd_set_state = 200, // params:workmode(hand_acid_mode_t),pumpworkstate(0:notwork,1:work) + kzble_app_report_key_event = 250, // params:hand_acid_remoter_key_event_t + +} ble_app_proto_cmd_t; + +typedef struct { + uint8_t auto_connect; + uint8_t auto_connect_client_name[20]; +} zble_start_scan_t; + +typedef struct { + uint8_t connected; + char blename[20]; + char bleid[20]; +} zble_connected_event_t; + +typedef struct { + char blename[20]; + char bleid[20]; + uint8_t rssi; +} zble_scan_result_t; + +typedef struct { + int32_t firmware_version; + int32_t blestack_version; + int32_t bootloader_version; + int32_t hardware_version; +} zble_read_version_t; + +typedef struct { + uint8_t reset_flag; + uint8_t connected_flag; +} zble_heart_t; +#pragma pack() + +#ifdef __cplusplus +} +#endif diff --git a/ble_through/ble_proto_utils.c b/ble_through/ble_proto_utils.c new file mode 100644 index 0000000..a19bda3 --- /dev/null +++ b/ble_through/ble_proto_utils.c @@ -0,0 +1,55 @@ +#include "ble_proto_utils.h" + +#include +#include + +bool ble_through_proto_check_packet(zble_proto_packet_t *packet) { + uint8_t *pu8 = (uint8_t *)packet; + + if (packet->packetlen < (BLE_THROUGH_PACKET_BASE_SIZE())) { + return false; + } + if (packet->h1 != PACKET_H1 || packet->h2 != PACKET_H2) { + return false; + } + if (pu8[packet->packetlen - 1] != PACKET_TAIL) { + return false; + } + + uint8_t checksum = 0; + for (int i = 0; i < packet->packetlen - 2; i++) { + checksum += pu8[i]; + } + if (checksum != pu8[packet->packetlen - 2]) { + return false; + } + + return true; +} + +uint8_t ble_through_proto_sum(zble_proto_packet_t *packet) { + uint8_t *_packet = (uint8_t *)packet; + uint8_t sum = 0; + for (int i = 0; i < packet->packetlen - 2; i++) { + sum += _packet[i]; + } + return sum; +} + +void zble_proto_utils_create_cmd_packet(zble_proto_packet_t *packet, uint16_t cmd, uint8_t index, uint8_t *data, int32_t datalen) { + int32_t packetlen = BLE_THROUGH_PACKET_BASE_SIZE() + datalen; + + packet->h1 = PACKET_H1; + packet->h2 = PACKET_H2; + packet->packetlen = packetlen; + packet->frameType = kzble_proto_cmd; + packet->frameIndex = index; + packet->cmd = cmd; + packet->from = kzble_upper; + packet->to = kzble_slave; + memcpy((void *)packet->data, data, datalen); + BLE_THROUGH_PACKET_SET_CHECKSUM(packet); + BLE_THROUGH_PACKET_SET_TAIL(packet); +} + +void zble_proto_utils_create_cmd_packet_int32(zble_proto_packet_t *packet, uint16_t cmd, uint8_t index, int32_t *data, int32_t ndata) { zble_proto_utils_create_cmd_packet(packet, cmd, index, (uint8_t *)data, ndata * 4); } diff --git a/ble_through/ble_proto_utils.h b/ble_through/ble_proto_utils.h new file mode 100644 index 0000000..5713e71 --- /dev/null +++ b/ble_through/ble_proto_utils.h @@ -0,0 +1,31 @@ +#pragma once + +#include "ble_proto.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define BLE_THROUGH_PACKET_SET_TAIL(packet) \ + { \ + uint8_t *__data = (uint8_t *)packet; \ + __data[packet->packetlen - 1] = PACKET_TAIL; \ + } + +#define BLE_THROUGH_PACKET_SET_CHECKSUM(packet) \ + { \ + uint8_t *__data = (uint8_t *)packet; \ + __data[packet->packetlen - 2] = ble_through_proto_sum(packet); \ + } + +#define BLE_THROUGH_PACKET_BASE_SIZE() (sizeof(zble_proto_packet_t) + 1) + +bool ble_through_proto_check_packet(zble_proto_packet_t *packet); +uint8_t ble_through_proto_sum(zble_proto_packet_t *packet); + +void zble_proto_utils_create_cmd_packet(zble_proto_packet_t *packet, uint16_t cmd, uint8_t index, uint8_t *data, int32_t datalen); +void zble_proto_utils_create_cmd_packet_int32(zble_proto_packet_t *packet, uint16_t cmd, uint8_t index, int32_t *data, int32_t ndata); + +#ifdef __cplusplus +} +#endif diff --git a/hand_acid_val_protocol/hand_acid_val_protocol.h b/hand_acid_val_protocol/hand_acid_val_protocol.h new file mode 100644 index 0000000..e69de29 diff --git a/zscanprotocol/zscanprotocol.h b/zscanprotocol/zscanprotocol.h new file mode 100644 index 0000000..6fdf1b2 --- /dev/null +++ b/zscanprotocol/zscanprotocol.h @@ -0,0 +1,29 @@ +#pragma once +#include +typedef enum { + kcmd, + kreceipt, + kerror_receipt, + kreport, +} packet_type_t; + +typedef enum { + kpriority_emergency_report = 7, + kpriority_cmd = 8, + kpriority_receipt = 9, + kpriority_report = 10, +} priority_t; + +typedef struct { + uint8_t frameNumAndFrameId; + uint8_t to; + uint8_t from; + uint8_t pad; +} zcanid_t; + +typedef struct { + uint8_t ptype; + uint8_t index; + uint16_t function_id; + uint8_t params[]; +} zcanbus_packet_t;