diff --git a/api/api.hpp b/api/api.hpp deleted file mode 100644 index 80ea86e..0000000 --- a/api/api.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once -#include "apibasic/basic.hpp" -// -#include "zi_module.hpp" -// -#include "zi_motor.hpp" -// \ No newline at end of file diff --git a/api/apibasic/basic.hpp b/api/apibasic/basic.hpp deleted file mode 100644 index ad23287..0000000 --- a/api/apibasic/basic.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include "cmdid.hpp" -// -#include "errorcode.hpp" -// -#include "packet_interface.hpp" -// -#include "module_type_index.hpp" -// -#include "reg_index.hpp" -// diff --git a/api/apibasic/module_type_index.hpp b/api/apibasic/module_type_index.hpp deleted file mode 100644 index f75beb8..0000000 --- a/api/apibasic/module_type_index.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include - -namespace iflytop { -typedef enum { - khbot_module = 1, // hbot模块 - ktemperature_ctrl_module = 3, // 温度控制 - kfan_ctrl_module = 5, // 风扇控制 - kcode_scaner = 6, // 扫码器 - kpipette_ctrl_module = 7, // 移液体枪控制 - ka8000_optical_module = 8, // a8000光学模组 - ktmc_step_motor = 10, // 步进电机 - kmini_servo_motor_module = 11, // 舵机 - kboard = 12, // 板子 - ka8000_idcard_reader = 13, // id卡读卡器 - ka8000_plate_code_scaner = 14, // 反应板条扫码器 -} module_type_t; -} \ No newline at end of file diff --git a/api/apibasic/packet_interface.hpp b/api/apibasic/packet_interface.hpp deleted file mode 100644 index 71d5353..0000000 --- a/api/apibasic/packet_interface.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once -#include -#define ZCANCMD_PACKET_MAX_LEN 64 - -namespace iflytop { -namespace zcr { -#pragma pack(push, 1) -typedef struct { - uint8_t packetType; - uint16_t cmdid; - uint8_t moduleId; - uint8_t index; - uint8_t datalen; - uint8_t data[]; - /* int8_t checksum;*/ -} zcr_cmd_header_t; - -#pragma pack(pop) - -typedef enum { - kptv2_cmd = 0xA0, - kptv2_ack = 0xA1, - kptv2_error_ack = 0xA2, - kptv2_event = 0xA3, -} zcan_cmd_packet_type_t; - -static inline bool zcr_cmd_checkpacket(const zcr_cmd_header_t* header, int len) { - if (len < sizeof(zcr_cmd_header_t)) { - return false; - } - if ((header->datalen + sizeof(zcr_cmd_header_t) + 1) != len) { - return false; - } - - uint8_t* rawpacket = (uint8_t*)header; - uint8_t checksum = 0; - for (int i = 0; i < len - 1; i++) { - checksum += rawpacket[i]; - } - - if (checksum != rawpacket[len - 1]) { - return false; - } - - return true; -} - -} // namespace zcr -} // namespace iflytop diff --git a/api/zi_module.cpp b/api/zi_module.cpp deleted file mode 100644 index 01844fe..0000000 --- a/api/zi_module.cpp +++ /dev/null @@ -1,96 +0,0 @@ - -#include "zi_module.hpp" - -#include - -#include - -using namespace iflytop; - -int32_t ZIModule::getid() { - int32_t id = 0; - getid(&id); - return id; -}; - -int32_t ZIModule::module_ping() { return 0; }; -int32_t ZIModule::module_set_reg(int32_t param_id, int32_t param_value) { return _module_xxx_reg(param_id, false, param_value); } -int32_t ZIModule::module_get_reg(int32_t param_id, int32_t *param_value) { return _module_xxx_reg(param_id, true, *param_value); } - -int32_t ZIModule::_module_xxx_reg(int32_t param_id, bool read, int32_t &val) { - if (param_id == kreg_module_version) { - if (read) { - module_get_version(&val); - return 0; - } else { - return 0; - } - } - - else if (param_id == kreg_module_type) { - if (read) { - module_get_type(&val); - return 0; - } else { - return 0; - } - } - - else if (param_id == kreg_module_status) { - if (read) { - module_get_status(&val); - return 0; - } else { - return 0; - } - } - - else if (param_id == kreg_module_errorcode) { - if (read) { - val = creg.module_errorcode; - return 0; - } else { - return 0; - } - } - - return module_xxx_reg(param_id, read, val); -} - -int32_t ZIModule::_module_set_reg(int32_t regoff, int32_t *regval, int32_t val, int32_t min, int32_t max) { - if (val < min || val > max) { - return err::kparam_out_of_range; - } - *regval = val; - return 0; -} -int32_t ZIModule::_module_get_reg(int32_t regoff, int32_t regval, int32_t &val) { - val = regval; - return 0; -} -int32_t ZIModule::_module_set_reg_float(int32_t regoff, float *regval, int32_t val, float precision, int32_t min, int32_t max) { - if (val < min || val > max) { - return err::kparam_out_of_range; - } - *regval = val * precision; - return 0; -} -int32_t ZIModule::_module_get_reg_float(int32_t regoff, float regval, int32_t &val, float precision) { - val = regval / precision; - return 0; -} - -int32_t ZIModule::module_get_error(int32_t *iserror) { - *iserror = creg.module_errorcode; - return 0; -} -int32_t ZIModule::module_clear_error() { - creg.module_errorcode = 0; - creg.module_status = 0; - return 0; -} - -int32_t ZIModule::module_get_status(int32_t *status) { - *status = creg.module_status; - return 0; -} diff --git a/api/zi_module.hpp b/api/zi_module.hpp deleted file mode 100644 index 8569f6a..0000000 --- a/api/zi_module.hpp +++ /dev/null @@ -1,88 +0,0 @@ -#pragma once -#include - -#include - -#include "apibasic/basic.hpp" - -namespace iflytop { -using namespace std; - -#define REG_SET(reg, ...) _module_set_reg(param_id, ®, val, ##__VA_ARGS__) -#define REG_GET(reg) _module_get_reg(param_id, reg, val) - -#define REG_SET_FLOAT(reg, precision, ...) _module_set_reg_float(param_id, ®, val, precision, ##__VA_ARGS__) -#define REG_GET_FLOAT(reg, precision) _module_get_reg_float(param_id, reg, val, precision) - -#define ACTION_NONE 0 -#define PROCESS_REG(param_id, readaction, writeacton) \ - case param_id: { \ - if (read) { \ - return readaction; \ - } else { \ - return writeacton; \ - } \ - } break; - -#define ENABLE_MODULE(name, type, version) \ - public: \ - virtual int32_t module_get_version(int32_t *val) { \ - *val = version; \ - return 0; \ - } \ - virtual int32_t module_get_type(int32_t *val) { \ - *val = type; \ - return 0; \ - } - -#define MODULE_COMMON_PROCESS_REG_CB() - -typedef struct { - int32_t module_errorcode; - int32_t module_status; -} module_common_reg_t; - -class ZIModule { - int32_t m_inited_flag = 0; - - protected: - module_common_reg_t creg; - - public: - virtual ~ZIModule() {} - - public: - virtual int32_t getid(); - virtual int32_t module_ping(); - virtual int32_t module_get_error(int32_t *iserror); - virtual int32_t module_clear_error(); - virtual int32_t module_set_reg(int32_t param_id, int32_t param_value); - virtual int32_t module_get_reg(int32_t param_id, int32_t *param_value); - virtual int32_t module_get_status(int32_t *status); - virtual int32_t module_get_version(int32_t *val) = 0; - virtual int32_t module_get_type(int32_t *val) = 0; - virtual int32_t module_stop() { return 0; } - virtual int32_t module_active_cfg() { return 0; } - - /*********************************************************************************************************************** - * 用户实现 * - ***********************************************************************************************************************/ - virtual int32_t getid(int32_t *id) = 0; - virtual int32_t module_xxx_reg(int32_t param_id, bool read, int32_t &val) = 0; - - public: - // slice - virtual int32_t bfcall(int32_t cmdid, uint8_t *param, int32_t len) { return 0; }; - virtual void aftercall(int32_t cmdid, uint8_t *param, int32_t len, uint8_t *ack, int32_t acklen, int32_t ret) {}; - - protected: - virtual int32_t _module_set_reg(int32_t regoff, int32_t *regval, int32_t val, int32_t min = INT32_MIN, int32_t max = INT32_MAX); - virtual int32_t _module_get_reg(int32_t regoff, int32_t regval, int32_t &val); - virtual int32_t _module_set_reg_float(int32_t regoff, float *regval, int32_t val, float precision, int32_t min = INT32_MIN, int32_t max = INT32_MAX); - virtual int32_t _module_get_reg_float(int32_t regoff, float regval, int32_t &val, float precision); - virtual int32_t _module_xxx_reg(int32_t param_id, bool read, int32_t &val); - - public: - public: -}; -} // namespace iflytop diff --git a/protocol.hpp b/protocol.hpp index 7de24a0..3ebe7cf 100644 --- a/protocol.hpp +++ b/protocol.hpp @@ -1,2 +1,7 @@ #pragma once -#include "api/api.hpp" +#include "protocol\cmdid.hpp" +#include "protocol\errorcode.hpp" +#include "protocol\packet.hpp" +#include "protocol\reg_index.hpp" +// +#include "protocol\zmotor_struct.hpp" diff --git a/api/apibasic/cmdid.cpp b/protocol/cmdid.cpp similarity index 100% rename from api/apibasic/cmdid.cpp rename to protocol/cmdid.cpp diff --git a/api/apibasic/cmdid.hpp b/protocol/cmdid.hpp similarity index 100% rename from api/apibasic/cmdid.hpp rename to protocol/cmdid.hpp diff --git a/api/apibasic/errorcode.cpp b/protocol/errorcode.cpp similarity index 100% rename from api/apibasic/errorcode.cpp rename to protocol/errorcode.cpp diff --git a/api/apibasic/errorcode.hpp b/protocol/errorcode.hpp similarity index 100% rename from api/apibasic/errorcode.hpp rename to protocol/errorcode.hpp diff --git a/protocol/packet.hpp b/protocol/packet.hpp new file mode 100644 index 0000000..481ba23 --- /dev/null +++ b/protocol/packet.hpp @@ -0,0 +1,53 @@ +#pragma once +#include +#define ZCANCMD_PACKET_MAX_LEN 64 + +namespace iflytop { + +typedef enum { + khbot_module = 1, // hbot模块 + ktemperature_ctrl_module = 3, // 温度控制 + kfan_ctrl_module = 5, // 风扇控制 + kcode_scaner = 6, // 扫码器 + kpipette_ctrl_module = 7, // 移液体枪控制 + ka8000_optical_module = 8, // a8000光学模组 + ktmc_step_motor = 10, // 步进电机 + kmini_servo_motor_module = 11, // 舵机 + kboard = 12, // 板子 + ka8000_idcard_reader = 13, // id卡读卡器 + ka8000_plate_code_scaner = 14, // 反应板条扫码器 +} module_type_t; + +namespace zcr { +#pragma pack(push, 1) +typedef struct { + uint8_t packetType; + uint16_t cmdid; + uint8_t moduleId; + uint8_t index; + uint8_t datalen; + uint8_t data[]; + /* int8_t checksum;*/ +} zcr_cmd_header_t; + +#pragma pack(pop) + +typedef enum { + kptv2_cmd = 0xA0, + kptv2_ack = 0xA1, + kptv2_error_ack = 0xA2, + kptv2_event = 0xA3, +} zcan_cmd_packet_type_t; + +static inline bool zcr_cmd_checkpacket(const zcr_cmd_header_t* header, int len) { + if (len < sizeof(zcr_cmd_header_t)) return false; + if ((header->datalen + sizeof(zcr_cmd_header_t) + 1) != len) return false; + + uint8_t checksum = 0; + for (int i = 0; i < len - 1; i++) checksum += ((uint8_t*)header)[i]; + + return checksum == ((uint8_t*)header)[len - 1]; +} + +} // namespace zcr +} // namespace iflytop diff --git a/api/apibasic/reg_index.cpp b/protocol/reg_index.cpp similarity index 100% rename from api/apibasic/reg_index.cpp rename to protocol/reg_index.cpp diff --git a/api/apibasic/reg_index.hpp b/protocol/reg_index.hpp similarity index 100% rename from api/apibasic/reg_index.hpp rename to protocol/reg_index.hpp diff --git a/api/zi_motor.hpp b/protocol/zmotor_struct.hpp similarity index 99% rename from api/zi_motor.hpp rename to protocol/zmotor_struct.hpp index 63dbd9b..40e88f8 100644 --- a/api/zi_motor.hpp +++ b/protocol/zmotor_struct.hpp @@ -3,8 +3,6 @@ #include -#include "apibasic/basic.hpp" - typedef struct { uint32_t sg_result : 10; uint32_t reserved0 : 5;