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.
164 lines
4.0 KiB
164 lines
4.0 KiB
#pragma once
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
|
|
#include "errorcode.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#define HAND_ACID_BLE_BASE_SIZE 6 //
|
|
|
|
#define PACKET_H1 (0xAA)
|
|
#define PACKET_H2 (0xBB)
|
|
#define PACKET_TAIL (0xCC)
|
|
typedef enum {
|
|
kproto_cmd = 1,
|
|
kproto_cmd_receipt = 2,
|
|
kproto_report = 3,
|
|
kproto_error_receipt = 4,
|
|
} hand_acid_ble_proto_packet_type_t;
|
|
|
|
typedef enum {
|
|
kproto_cmd_set_state = 1, // set_state
|
|
kproto_cmd_reset = 2, //
|
|
kproto_cmd_setlong_key_delayms = 3, //
|
|
kproto_cmd_read_power = 4, //
|
|
kproto_cmd_read_version = 5, //
|
|
|
|
kproto_cmd_ble_master_start_scan = 50, // process by ble master ble_master_start_scan
|
|
kproto_cmd_ble_master_enter_dfu = 51, // process by ble master ble_master_enter_dfu
|
|
kproto_cmd_ble_master_reset = 52, // process by ble master
|
|
kproto_cmd_ble_master_read_version = 53, // process by ble master
|
|
|
|
kproto_report_key_event = 101, // key_event
|
|
kproto_report_connected_event = 102, // generater by ble master and slave
|
|
kproto_report_heart = 104, //
|
|
} hand_acid_ble_proto_cmd_t;
|
|
|
|
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;
|
|
|
|
typedef enum {
|
|
khand_acid_pump_is_idle = 0, //
|
|
khand_acid_pump_is_working = 1, //
|
|
} hand_pump_working_state_t;
|
|
|
|
typedef enum {
|
|
kkey_short_press_event = 0, //
|
|
kkey_long_press_event = 1, //
|
|
} key_event_t;
|
|
|
|
typedef enum {
|
|
kkey_id_mode_and_action_butt,
|
|
kkey_id_liquid_reflux_butt,
|
|
} key_id_t;
|
|
|
|
#pragma pack(1)
|
|
typedef struct {
|
|
uint8_t h1;
|
|
uint8_t h2;
|
|
uint8_t packetlen;
|
|
uint8_t frame_type;
|
|
uint8_t frame_index;
|
|
uint8_t cmd;
|
|
union {
|
|
struct {
|
|
uint8_t hand_acid_mode; // hand_acid_mode_t
|
|
uint8_t hand_pump_working_state; // hand_pump_working_state_t
|
|
} set_state;
|
|
//
|
|
struct {
|
|
uint8_t scanid[20]; // scan id
|
|
} ble_master_start_scan;
|
|
|
|
struct {
|
|
uint8_t keyid; // key_id_t
|
|
uint8_t keyevent; // key_event_t
|
|
} key_event;
|
|
|
|
struct {
|
|
uint8_t connected;
|
|
uint8_t from_ble_master;
|
|
} connected_event;
|
|
|
|
struct {
|
|
uint8_t powerlevel;
|
|
} heart;
|
|
|
|
struct {
|
|
uint32_t longkeydelay;
|
|
} setlong_key_delayms;
|
|
|
|
struct {
|
|
int32_t ecode;
|
|
} errreceipt;
|
|
|
|
struct {
|
|
int32_t power;
|
|
} read_power;
|
|
|
|
struct {
|
|
int32_t firmware_version;
|
|
int32_t blestack_version;
|
|
int32_t bootloader_version;
|
|
int32_t hardware_version;
|
|
} read_version;
|
|
|
|
} data;
|
|
} hand_acid_ble_proto_t;
|
|
|
|
static inline bool hand_acid_ble_proto_check_packet(hand_acid_ble_proto_t *packet) {
|
|
uint8_t *pu8 = (uint8_t *)packet;
|
|
|
|
if (packet->packetlen < (HAND_ACID_BLE_BASE_SIZE + 2)) {
|
|
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;
|
|
}
|
|
|
|
static inline uint8_t hand_acid_ble_proto_sum(hand_acid_ble_proto_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;
|
|
}
|
|
|
|
#define PACKET_SET_TAIL(packet) \
|
|
{ \
|
|
uint8_t *__data = (uint8_t *)packet; \
|
|
__data[packet->packetlen - 1] = PACKET_TAIL; \
|
|
}
|
|
|
|
#define PACKET_SET_CHECKSUM(packet) \
|
|
{ \
|
|
uint8_t *__data = (uint8_t *)packet; \
|
|
__data[packet->packetlen - 2] = hand_acid_ble_proto_sum(packet); \
|
|
}
|
|
|
|
#pragma pack()
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|