活动介绍

queque

时间: 2025-04-06 22:06:54 浏览: 28
### 队列(Queue)的概念及其编程中的实现 队列是一种线性的数据结构,遵循先进先出(First-In-First-Out, FIFO)的原则。这意味着最早进入队列的元素会最先被移除[^1]。在实际应用中,队列通常用于模拟等待服务的对象序列,例如打印作业排队、任务调度等场景。 #### 基本操作 队列支持两种主要的操作: 1. **入队(Enqueue)**:将新元素添加到队列的末尾。 2. **出队(Dequeue)**:从队列的头部移除元素。 这些操作的时间复杂度通常是 O(1)[^2],因为它们只涉及修改队列的一端或两端指针。 #### 实现方式 队列可以通过多种方式进行实现,常见的有基于数组和链表的方式: 1. **基于数组的实现**: 使用固定大小的数组来存储队列中的元素。为了提高效率,可以采用循环缓冲区的方法,即当队列到达数组末端时重新回到起始位置继续存储。这种方式避免了频繁移动元素带来的开销[^3]。 2. **基于链表的实现**: 利用单向链表或者双向链表作为底层存储结构。由于链表动态分配内存的特点,它可以灵活处理不同数量级的数据量而无需预先定义容量限制。对于标准库 `std::queue` 的 C++ 实现来说,默认情况下其内部采用了双端队列(deque),从而兼顾性能与功能扩展性[^2]。 以下是使用 Python 和 C++ 编写的简单队列示例代码: ```python class Queue: def __init__(self): self.items = [] def is_empty(self): return not bool(self.items) def enqueue(self, item): self.items.append(item) def dequeue(self): if not self.is_empty(): return self.items.pop(0) ``` ```cpp #include <iostream> #include <queue> int main() { std::queue<int> q; // Enqueue elements q.push(1); q.push(2); // Dequeue element while (!q.empty()) { std::cout << q.front() << "\n"; q.pop(); } return 0; } ``` 上述例子展示了如何通过自定义类以及 STL 容器分别构建一个基础版的队列实例。
阅读全文

相关推荐

int main() { int num; printf("0: ActiveDia and send 3E 80\n"); printf("1: ActiveDia don't send 3E 80\n"); printf("2: Read ECU version\n"); printf("3: program ECU\n"); printf("4: send command\n"); int Dia_status=0; int sock; Config config; config=loadjsonfile_tcpsetting("/home/pi/Cdaima/ipConfig.json"); printf("target_ip: %s\n", config.tcp_setting.target_ip); Queue queue; initQueue(&queue); ECUresponse_buffer ECUss[200]; while(1) { printf("请输入一个数字:"); if (scanf("%d", &num) != 1) { // 对用户的输入进行错误处理 printf("输入错误!\n"); return 1; } if (num < 0 || num > 4) { // 对输入的数字进行范围判断 printf("输入的数字超出范围!\n"); return 1; } else if (num ==0) { if (Dia_status==0) { sock=1; sock=creat_tcp_connect(config.tcp_setting.target_ip,config.tcp_setting.target_port); if(sock>0){Dia_status=1;} /* if (sock>0) { Dia_status=1; printf("输入的数字超出范围!\n"); pthread_t queque_datahandle; printf("输入的数字超出范围!\n"); ThreadArgs_queue args = {sock, queue, ECUss,Dia_status}; printf("输入的数字超出范围!\n"); int ret = pthread_create(&queque_datahandle, NULL, queque_txdatahandle, &args); printf("输入的数字超出范围!\n"); if (ret != 0) { printf("pthread_create error: %d\n", ret); return -1; } printf("Main thread.\n"); pthread_join(queque_datahandle, NULL); }*/ } } printf("您输入的数字是:%d\n", num); } return 0; } 上面的代码运行时发生了段错误 是什么原因 (执行TCP连接那个函数会)

/*! \file main.c \brief Implementation of ble datatrans server demo. \version 2023-07-20, V1.0.0, firmware for GD32VW55x */ /* Copyright (c) 2023, GigaDevice Semiconductor Inc. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include <stdint.h> #include "dbg_print.h" #include "gd32vw55x_platform.h" #include "wrapper_os.h" #include "ble_adapter.h" #include "ble_adv.h" #include "ble_conn.h" #include "ble_utils.h" #include "ble_export.h" #include "ble_sec.h" #include "ble_datatrans_srv.h" #include "app_uart.h" /* Device name */ #define DEV_NAME "GD-BLE-DEV" /* Device name length*/ #define DEV_NAME_LEN strlen(dev_name) /* Advertising parameters */ typedef struct { uint8_t adv_idx; /*!< Advertising id. use to stop advertising */ ble_adv_state_t adv_state; /*!< Advertising state */ } app_adv_param_t; /* Definitions of the different task priorities */ enum { BLE_STACK_TASK_PRIORITY = OS_TASK_PRIORITY(2), /*!< Priority of the BLE stack task */ BLE_APP_TASK_PRIORITY = OS_TASK_PRIORITY(1), /*!< Priority of the BLE APP task */ }; /* Definitions of the different BLE task stack size requirements */ enum { BLE_STACK_TASK_STACK_SIZE = 768, /*!< BLE stack task stack size */ BLE_APP_TASK_STACK_SIZE = 512, /*!< BLE APP task stack size */ }; /* Device name array*/ char dev_name[] = {DEV_NAME}; /* advertising env*/ app_adv_param_t app_adv_env = {0}; /* connection index */ uint8_t conn_idx = 0; /*! \brief Start advertising \param[in] p_adv: pointer to BLE advertising set \param[out] none \retval none */ static ble_status_t app_adv_start(void) { ble_data_t adv_data = {0}; ble_data_t adv_scanrsp_data = {0}; ble_adv_data_set_t adv = {0}; ble_adv_data_set_t scan_rsp = {0}; uint8_t data[BLE_GAP_LEGACY_ADV_MAX_LEN] = {0}; uint8_t idx = 0; data[idx++] = 2; data[idx++] = BLE_AD_TYPE_FLAGS; data[idx++] = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED | BLE_GAP_ADV_FLAG_LE_GENERAL_DISC_MODE; data[idx++] = DEV_NAME_LEN + 1; data[idx++] = 0x09; memcpy(&data[idx], dev_name, DEV_NAME_LEN); idx += DEV_NAME_LEN; adv_data.len = idx; adv_data.p_data = data; adv_scanrsp_data.len = idx - 3; adv_scanrsp_data.p_data = &data[3]; adv.data_force = true; adv.data.p_data_force = &adv_data; scan_rsp.data_force = true; scan_rsp.data.p_data_force = &adv_scanrsp_data; return ble_adv_start(app_adv_env.adv_idx, &adv, &scan_rsp, NULL); } /*! \brief Callback function to handle BLE advertising events \param[in] adv_evt: BLE advertising event type \param[in] p_data: pointer to BLE advertising event data \param[in] p_context: context data used when create advertising \param[out] none \retval none */ static void app_adv_mgr_evt_hdlr(ble_adv_evt_t adv_evt, void *p_data, void *p_context) { if (adv_evt == BLE_ADV_EVT_STATE_CHG) { ble_adv_state_chg_t *p_chg = (ble_adv_state_chg_t *)p_data; ble_adv_state_t old_state = app_adv_env.adv_state; app_print("%s state change 0x%x ==> 0x%x, reason 0x%x\r\n", __func__, old_state, p_chg->state, p_chg->reason); app_adv_env.adv_state = p_chg->state; if ((p_chg->state == BLE_ADV_STATE_CREATE) && (old_state == BLE_ADV_STATE_CREATING)) { app_adv_env.adv_idx = p_chg->adv_idx; app_adv_start(); } } } /*! \brief Create an advertising \param[in] p_param: pointer to advertising parameters \param[out] none \retval ble_status_t: BLE_ERR_NO_ERROR on success, otherwise an error code */ static ble_status_t app_adv_create(void) { ble_adv_param_t adv_param = {0}; adv_param.param.disc_mode = BLE_GAP_ADV_MODE_GEN_DISC; adv_param.param.own_addr_type = BLE_GAP_LOCAL_ADDR_STATIC; adv_param.param.type = BLE_GAP_ADV_TYPE_LEGACY; adv_param.param.prop = BLE_GAP_ADV_PROP_UNDIR_CONN; adv_param.param.filter_pol = BLE_GAP_ADV_ALLOW_SCAN_ANY_CON_ANY; adv_param.param.ch_map = 0x07; adv_param.param.primary_phy = BLE_GAP_PHY_1MBPS; adv_param.param.adv_intv_min = 160; adv_param.param.adv_intv_max = 160; adv_param.restart_after_disconn = true; return ble_adv_create(&adv_param, app_adv_mgr_evt_hdlr, NULL); } /*! \brief Function to execute app code after stack ready \param[in] none \param[out] none \retval none */ void ble_task_ready(void) { app_adv_create(); } /*! \brief Callback function to handle BLE adapter events \param[in] event: BLE adapter event type \param[in] p_data: pointer to BLE adapter event data \param[out] none \retval none */ static void app_adp_evt_handler(ble_adp_evt_t event, ble_adp_data_u *p_data) { uint8_t i = 0; if (event == BLE_ADP_EVT_ENABLE_CMPL_INFO) { if (p_data->adapter_info.status == BLE_ERR_NO_ERROR) { app_print("=== Adapter enable success ===\r\n"); app_print("hci_ver 0x%x, hci_subver 0x%x, lmp_ver 0x%x, lmp_subver 0x%x, manuf_name 0x%x\r\n", p_data->adapter_info.version.hci_ver, p_data->adapter_info.version.hci_subver, p_data->adapter_info.version.lmp_ver, p_data->adapter_info.version.lmp_subver, p_data->adapter_info.version.manuf_name); app_print("adv_set_num %u, min_tx_pwr %d, max_tx_pwr %d, max_adv_data_len %d \r\n", p_data->adapter_info.adv_set_num, p_data->adapter_info.tx_pwr_range.min_tx_pwr, p_data->adapter_info.tx_pwr_range.max_tx_pwr, p_data->adapter_info.max_adv_data_len); app_print("sugg_max_tx_octets %u, sugg_max_tx_time %u \r\n", p_data->adapter_info.sugg_dft_data.sugg_max_tx_octets, p_data->adapter_info.sugg_dft_data.sugg_max_tx_time); app_print("loc irk:"); for (i = 0; i < BLE_GAP_KEY_LEN; i++) { app_print(" %02x", p_data->adapter_info.loc_irk_info.irk[i]); } app_print("\r\n"); app_print("identity addr %02X:%02X:%02X:%02X:%02X:%02X \r\n ", p_data->adapter_info.loc_irk_info.identity.addr[5], p_data->adapter_info.loc_irk_info.identity.addr[4], p_data->adapter_info.loc_irk_info.identity.addr[3], p_data->adapter_info.loc_irk_info.identity.addr[2], p_data->adapter_info.loc_irk_info.identity.addr[1], p_data->adapter_info.loc_irk_info.identity.addr[0]); app_print("=== BLE Adapter enable complete ===\r\n"); ble_task_ready(); } else { app_print("=== BLE Adapter enable fail ===\r\n"); } } } /*! \brief Init adapter application module \param[in] none \param[out] none \retval none */ void app_adapter_init(void) { ble_adp_callback_register(app_adp_evt_handler); } /*! \brief Send security request \param[in] conidx: connection index \param[out] none \retval none */ void app_sec_send_security_req(uint8_t conidx) { uint8_t auth = BLE_GAP_AUTH_REQ_NO_MITM_NO_BOND; if (ble_sec_security_req(conidx, auth) != BLE_ERR_NO_ERROR) { app_print("app_sec_send_security_req fail! \r\n"); } } /*! \brief Callback function to handle BLE connection event \param[in] event: BLE connection event type \param[in] p_data: pointer to BLE connection event data \param[out] none \retval none */ static void app_conn_evt_handler(ble_conn_evt_t event, ble_conn_data_u *p_data) { switch (event) { case BLE_CONN_EVT_STATE_CHG: { if (p_data->conn_state.state == BLE_CONN_STATE_DISCONNECTD) { app_print("disconnected. conn idx: %u, conn_hdl: 0x%x reason 0x%x\r\n", p_data->conn_state.info.discon_info.conn_idx, p_data->conn_state.info.discon_info.conn_hdl, p_data->conn_state.info.discon_info.reason); } else if (p_data->conn_state.state == BLE_CONN_STATE_CONNECTED) { app_print("connect success. conn idx:%u, conn_hdl:0x%x \r\n", p_data->conn_state.info.conn_info.conn_idx, p_data->conn_state.info.conn_info.conn_hdl); if (p_data->conn_state.info.conn_info.role == BLE_SLAVE) { app_sec_send_security_req(p_data->conn_state.info.conn_info.conn_idx); } conn_idx = p_data->conn_state.info.conn_info.conn_idx; } } break; case BLE_CONN_EVT_PARAM_UPDATE_IND: { app_print("conn idx %u, intv_min 0x%x, intv_max 0x%x, latency %u, supv_tout %u\r\n", p_data->conn_param_req_ind.conn_idx, p_data->conn_param_req_ind.intv_min, p_data->conn_param_req_ind.intv_max, p_data->conn_param_req_ind.latency, p_data->conn_param_req_ind.supv_tout); ble_conn_param_update_cfm(p_data->conn_param_req_ind.conn_idx, true, 2, 4); } break; case BLE_CONN_EVT_PARAM_UPDATE_RSP: { app_print("conn idx %u, param update result status: 0x%x\r\n", p_data->conn_param_rsp.conn_idx, p_data->conn_param_rsp.status); } break; case BLE_CONN_EVT_PARAM_UPDATE_INFO: { app_print("conn idx %u, param update ind: interval %d, latency %d, sup to %d\r\n", p_data->conn_params.conn_idx, p_data->conn_params.interval, p_data->conn_params.latency, p_data->conn_params.supv_tout); } break; case BLE_CONN_EVT_PKT_SIZE_SET_RSP: { app_print("conn idx %u, packet size set status 0x%x\r\n", p_data->pkt_size_set_rsp.conn_idx, p_data->pkt_size_set_rsp.status); } break; case BLE_CONN_EVT_PKT_SIZE_INFO: { app_print("le pkt size info: conn idx %u, tx oct %d, tx time %d, rx oct %d, rx time %d\r\n", p_data->pkt_size_info.conn_idx, p_data->pkt_size_info.max_tx_octets, p_data->pkt_size_info.max_tx_time, p_data->pkt_size_info.max_rx_octets, p_data->pkt_size_info.max_rx_time); } break; case BLE_CONN_EVT_NAME_GET_IND: { ble_conn_name_get_cfm(p_data->name_get_ind.conn_idx, 0, p_data->name_get_ind.token, DEV_NAME_LEN, (uint8_t *)dev_name, DEV_NAME_LEN); } break; case BLE_CONN_EVT_APPEARANCE_GET_IND: { ble_conn_appearance_get_cfm(p_data->appearance_get_ind.conn_idx, 0, p_data->appearance_get_ind.token, 0); } break; default: break; } } /*! \brief Callback function to handle @ref BLE_SEC_EVT_PAIRING_REQ_IND event \param[in] p_ind: pointer to the pairing request indication data \param[out] none \retval none */ static void app_pairing_req_hdlr(ble_gap_pairing_req_ind_t *p_ind) { ble_gap_pairing_param_t param = {0}; param.auth = BLE_GAP_AUTH_MASK_NONE; param.iocap = BLE_GAP_IO_CAP_NO_IO; param.key_size = 16; param.ikey_dist = BLE_GAP_KDIST_IDKEY | BLE_GAP_KDIST_SIGNKEY | BLE_GAP_KDIST_ENCKEY; param.rkey_dist = BLE_GAP_KDIST_IDKEY | BLE_GAP_KDIST_SIGNKEY | BLE_GAP_KDIST_ENCKEY; ble_sec_pairing_req_cfm(p_ind->conn_idx, true, ¶m, BLE_GAP_NO_SEC); } /*! \brief Callback function to handle @ref BLE_SEC_EVT_PAIRING_FAIL_INFO event \param[in] p_info: pointer to the pairing fail information data \param[out] none \retval none */ static void app_pairing_fail_hdlr(ble_sec_pairing_fail_t *p_info) { app_print("pairing fail reason 0x%x\r\n", p_info->param.reason); } /*! \brief Callback function to handle @ref BLE_SEC_EVT_PAIRING_SUCCESS_INFO event \param[in] p_info: pointer to the pairing success information data \param[out] none \retval none */ static void app_pairing_success_hdlr(ble_sec_pairing_success_t *p_info) { app_print("conn_idx %u pairing success, level 0x%x ltk_present %d sc %d\r\n", p_info->conidx, p_info->bond_info.pairing_lvl, p_info->bond_info.enc_key_present, p_info->sc); } /*! \brief Callback function to handle BLE security events \param[in] event: BLE security event type \param[in] p_data: pointer to the BLE security event data \param[out] none \retval none */ static void app_sec_evt_handler(ble_sec_evt_t event, ble_sec_data_u *p_data) { switch (event) { case BLE_SEC_EVT_PAIRING_REQ_IND: app_pairing_req_hdlr((ble_gap_pairing_req_ind_t *)p_data); break; case BLE_SEC_EVT_PAIRING_SUCCESS_INFO: app_pairing_success_hdlr((ble_sec_pairing_success_t *)p_data); break; case BLE_SEC_EVT_PAIRING_FAIL_INFO: app_pairing_fail_hdlr((ble_sec_pairing_fail_t *)p_data); break; default: break; } } /*! \brief Callback function to handle data received by datatrans server service \param[in] data_len: received data length \param[in] p_data: pointer to received data \param[out] none \retval none */ void app_datatrans_srv_rx_callback(uint16_t data_len, uint8_t *p_data) { uint8_t *p_str = sys_malloc(data_len + 1); if (p_str) { app_print("datatrans srv receive data: \r\n"); memset(p_str, 0, data_len + 1); memcpy(p_str, p_data, data_len); app_print("%s\r\n", p_str); sys_mfree(p_str); } } /*! \brief Init application security module \param[in] none \param[out] none \retval none */ void app_sec_mgr_init(void) { ble_sec_callback_register(app_sec_evt_handler); } /*! \brief Init APP connection manager module \param[in] none \param[out] none \retval none */ void app_conn_mgr_init(void) { ble_conn_callback_register(app_conn_evt_handler); } /*! \brief Init BLE component modules needed \param[in] none \param[out] none \retval none */ void ble_init(void) { ble_init_param_t param = {0}; ble_os_api_t os_interface = { .os_malloc = sys_malloc, .os_calloc = sys_calloc, .os_mfree = sys_mfree, .os_memset = sys_memset, .os_memcpy = sys_memcpy, .os_memcmp = sys_memcmp, .os_task_create = sys_task_create, .os_task_init_notification = sys_task_init_notification, .os_task_wait_notification = sys_task_wait_notification, .os_task_notify = sys_task_notify, .os_task_delete = sys_task_delete, .os_ms_sleep = sys_ms_sleep, .os_current_task_handle_get = sys_current_task_handle_get, .os_queue_init = sys_queue_init, .os_queue_free = sys_queue_free, .os_queue_write = sys_queue_write, .os_queue_read = sys_queue_read, .os_random_bytes_get = sys_random_bytes_get, }; ble_power_on(); param.role = BLE_GAP_ROLE_PERIPHERAL; param.keys_user_mgr = false; param.pairing_mode = BLE_GAP_PAIRING_SECURE_CONNECTION | BLE_GAP_PAIRING_LEGACY; param.privacy_cfg = BLE_GAP_PRIV_CFG_PRIV_EN_BIT; param.ble_task_stack_size = BLE_STACK_TASK_STACK_SIZE; param.ble_task_priority = BLE_STACK_TASK_PRIORITY; param.ble_app_task_stack_size = BLE_APP_TASK_STACK_SIZE; param.ble_app_task_priority = BLE_APP_TASK_PRIORITY; param.name_perm = BLE_GAP_WRITE_NOT_ENC; param.appearance_perm = BLE_GAP_WRITE_NOT_ENC; param.en_cfg = 0; param.p_os_api = &os_interface; ble_sw_init(¶m); app_adapter_init(); app_conn_mgr_init(); app_sec_mgr_init(); ble_datatrans_srv_init(); ble_datatrans_srv_rx_cb_reg(app_datatrans_srv_rx_callback); /* The BLE interrupt must be enabled after ble_sw_init. */ ble_irq_enable(); } /*! \brief Main entry point This function is called right after the booting process has completed. \param[in] none \param[out] none \retval none */ int main(void) { sys_os_init(); platform_init(); app_uart_init(); ble_init(); sys_os_start(); for ( ; ; ); } /*! \file app_uart.c \brief APP UART for GD32VW55x SDK. \version 2023-07-20, V1.0.0, firmware for GD32VW55x */ /* Copyright (c) 2023, GigaDevice Semiconductor Inc. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include <ctype.h> #include "gd32vw55x.h" #include <stdio.h> #include "app_uart.h" #include "wrapper_os.h" #include "dbg_print.h" #include "wakelock.h" #include "log_uart.h" #include "ble_datatrans_srv.h" /* Priority of the uart task */ #define UART_TASK_PRIORITY OS_TASK_PRIORITY(4) /* Uart queque size */ #define UART_QUEUE_SIZE 3 /* Message format */ struct uart_msg { uint16_t len; /*!< Length, in bytes, of the message */ void *data; /*!< Pointer to the message */ }; extern uint8_t conn_idx; /* Uart queque */ static os_queue_t uart_queue; /* Uart cyclic buffer */ static cyclic_buf_t uart_cyc_buf; /* Uart RX buffer */ char uart_rx_buf[UART_BUFFER_SIZE]; /* Uart RX buffer index*/ uint32_t uart_index = 0; /*! \brief app uart rx handle \param[in] uart_cyc_buf: uart cyclic buffer \param[out] buf: data buf \param[in] len: data length \retval none */ static void app_uart_rx_handle_done(cyclic_buf_t *uart_cyc_buf, uint8_t *buf, uint16_t *len) { if (*len > cyclic_buf_count(uart_cyc_buf)) { *len = cyclic_buf_count(uart_cyc_buf); } if (buf == NULL) { cyclic_buf_drop(uart_cyc_buf, *len); } else { cyclic_buf_read(uart_cyc_buf, buf, *len); } } /*! \brief uart message precess \param[in] msg: uart message \retval none */ static void app_uart_msg_process(struct uart_msg *msg) { cyclic_buf_t *p_cyclic_buf = (cyclic_buf_t *)msg->data; char *command; command = sys_malloc(msg->len + 1); if (command == NULL) { app_print("No buffer alloc for uart msg !\r\n"); return; } app_uart_rx_handle_done((cyclic_buf_t *)msg->data, (uint8_t *)command, &msg->len); command[msg->len] = '\0'; if (ble_datatrans_srv_tx(conn_idx, (uint8_t *)command, msg->len) == BLE_ERR_NO_ERROR) { app_print("datatrans srv send data: \r\n"); app_print("%s\r\n", command); } sys_mfree(command); return; } /*! \brief uart task \param[in] param: \retval none */ static void app_uart_task(void *param) { struct uart_msg msg; for (;;) { sys_queue_read(&uart_queue, &msg, -1, false); app_uart_msg_process(&msg); } } /*! \brief uart message send \param[in] msg_data: message data \param[in] len: message length \retval success 0 */ int app_uart_send(void *msg_data, uint16_t len) { struct uart_msg msg; msg.len = len; msg.data = msg_data; return sys_queue_write(&uart_queue, &msg, 0, true); } /*! \brief uart rx data handler \param[in] none \retval none */ static void rx_handler(void) { // uart_index - 2 to remove 0x0d and 0x0a when using Husky if (uart_index > 2) { if (app_uart_send((void *)(&uart_cyc_buf), uart_index - 2) == 0) { if (cyclic_buf_write(&uart_cyc_buf, (uint8_t *)uart_rx_buf, uart_index - 2) == false) { dbg_print(ERR, "uart cyclic buffer full\r\n"); } } else { /* queue was full */ dbg_print(ERR, "queue full\r\n"); /* TODO: report 'message ignored' status */ } } uart_index = 0; } /*! \brief app uart rx irq handler \param[in] uart_port: uart port \retval none */ static void app_uart_rx_irq_hdl(uint32_t uart_port) { usart_interrupt_disable(uart_port, USART_INT_RBNE); while (1) { // We should have chance to check overflow error // Otherwise it may cause dead loop handle rx interrupt if (RESET != usart_flag_get(uart_port, USART_FLAG_ORERR)) { usart_flag_clear(uart_port, USART_FLAG_ORERR); } if ((RESET != usart_flag_get(uart_port, USART_FLAG_RBNE))) { uart_rx_buf[uart_index++] = (char)usart_data_receive(uart_port); if (uart_index >= UART_BUFFER_SIZE) { uart_index = 0; } } else { break; } } if (RESET != usart_flag_get(uart_port, USART_FLAG_IDLE)) { usart_flag_clear(UART2, USART_FLAG_IDLE); if (uart_index > 0) rx_handler(); } sys_wakelock_release(LOCK_ID_USART); usart_interrupt_enable(uart_port, USART_INT_RBNE); } /*! \brief app uart config \param[in] none \retval none */ void app_uart_config(void) { rcu_periph_clock_disable(RCU_GPIOA); rcu_periph_clock_disable(RCU_GPIOB); rcu_periph_clock_disable(RCU_UART2); rcu_periph_clock_enable(RCU_GPIOA); rcu_periph_clock_enable(RCU_GPIOB); rcu_periph_clock_enable(RCU_UART2); gpio_af_set(UART2_TX_GPIO, UART2_TX_AF_NUM, UART2_TX_PIN); gpio_af_set(UART2_RX_GPIO, UART2_RX_AF_NUM, UART2_RX_PIN); gpio_mode_set(UART2_TX_GPIO, GPIO_MODE_AF, GPIO_PUPD_NONE, UART2_TX_PIN); gpio_output_options_set(UART2_TX_GPIO, GPIO_OTYPE_PP, GPIO_OSPEED_25MHZ, UART2_TX_PIN); gpio_mode_set(UART2_RX_GPIO, GPIO_MODE_AF, GPIO_PUPD_NONE, UART2_RX_PIN); gpio_output_options_set(UART2_RX_GPIO, GPIO_OTYPE_PP, GPIO_OSPEED_25MHZ, UART2_RX_PIN); /* close printf buffer */ setvbuf(stdout, NULL, _IONBF, 0); usart_deinit(UART2); usart_word_length_set(UART2, USART_WL_8BIT); usart_stop_bit_set(UART2, USART_STB_1BIT); usart_parity_config(UART2, USART_PM_NONE); usart_baudrate_set(UART2, 115200U); usart_receive_config(UART2, USART_RECEIVE_ENABLE); usart_transmit_config(UART2, USART_TRANSMIT_ENABLE); usart_interrupt_enable(UART2, USART_INT_RBNE); usart_receive_fifo_enable(UART2); usart_enable(UART2); /*wait IDLEF set and clear it*/ while(RESET == usart_flag_get(UART2, USART_FLAG_IDLE)) { } usart_flag_clear(UART2, USART_FLAG_IDLE); usart_interrupt_enable(UART2, USART_INT_IDLE); } /*! \brief app uart initialize \retval success 0 */ int app_uart_init(void) { if (sys_task_create_dynamic((const uint8_t *)"UART task", 512, UART_TASK_PRIORITY, app_uart_task, NULL) == NULL) { return -1; } if (sys_queue_init(&uart_queue, UART_QUEUE_SIZE, sizeof(struct uart_msg))) { return -2; } app_uart_config(); memset(uart_rx_buf, 0, UART_BUFFER_SIZE); uart_index = 0; cyclic_buf_init(&uart_cyc_buf, 4 * UART_BUFFER_SIZE); uart_irq_callback_register(UART2, app_uart_rx_irq_hdl); return 0; } 详细介绍代码

zip
CH341A编程器是一款广泛应用的通用编程设备,尤其在电子工程和嵌入式系统开发领域中,它被用来烧录各种类型的微控制器、存储器和其他IC芯片。这款编程器的最新版本为1.3,它的一个显著特点是增加了对25Q256等32M芯片的支持。 25Q256是一种串行EEPROM(电可擦可编程只读存储器)芯片,通常用于存储程序代码、配置数据或其他非易失性信息。32M在这里指的是存储容量,即该芯片可以存储32兆位(Mbit)的数据,换算成字节数就是4MB。这种大容量的存储器在许多嵌入式系统中都有应用,例如汽车电子、工业控制、消费电子设备等。 CH341A编程器的1.3版更新,意味着它可以与更多的芯片型号兼容,特别是针对32M容量的芯片进行了优化,提高了编程效率和稳定性。26系列芯片通常指的是Microchip公司的25系列SPI(串行外围接口)EEPROM产品线,这些芯片广泛应用于各种需要小体积、低功耗和非易失性存储的应用场景。 全功能版的CH341A编程器不仅支持25Q256,还支持其他大容量芯片,这意味着它具有广泛的兼容性,能够满足不同项目的需求。这包括但不限于微控制器、EPROM、EEPROM、闪存、逻辑门电路等多种类型芯片的编程。 使用CH341A编程器进行编程操作时,首先需要将设备通过USB连接到计算机,然后安装相应的驱动程序和编程软件。在本例中,压缩包中的"CH341A_1.30"很可能是编程软件的安装程序。安装后,用户可以通过软件界面选择需要编程的芯片类型,加载待烧录的固件或数据,然后执行编程操作。编程过程中需要注意的是,确保正确设置芯片的电压、时钟频率等参数,以防止损坏芯片。 CH341A编程器1.3版是面向电子爱好者和专业工程师的一款实用工具,其强大的兼容性和易用性使其在众多编程器中脱颖而出。对于需要处理25Q256等32M芯片的项目,或者26系列芯片的编程工作,CH341A编程器是理想的选择。通过持续的软件更新和升级,它保持了与现代电子技术同步,确保用户能方便地对各种芯片进行编程和调试。

大家在看

recommend-type

密码::unlocked::sparkles::locked:创新,方便,安全的加密应用程序

隐身者 创新,方便,安全的加密应用程序。 加密无限位。 只记得一点。 Crypter是一款跨平台的加密应用程序,它使加密和解密变得很方便,同时仍然保持强大的安全性。 它解决了当今大多数安全系统中最弱的链接之一-弱密码。 它简化了安全密码的生成和管理,并且只需要记住一个位-MasterPass。 是一个加密应用程序,可以解密和加密包括文件和文件夹在内的任意数据。 该版本已发布,并针对macOS(OSX),Linux(适用于所有通过发行的发行版)和Windows(32和64位)进行了全面测试。 所有核心模块(提供核心功能的模块)都经过了全面测试。 会将MasterPass保存在操作系统的钥匙串中,因此您不必在每次打开应用程序时都输入它。 为了帮助加快开发速度,请发送PR剩下的内容做 如果您有任何建议,请打开一个问题,并通过PR进行改进! 还要签出 ( )一个分散的端到端加密消息传递应用程序。 链接到此自述文件: : 内容 安装 适用于所有主要平台的所有预构建二进制文件都可以在。 Crypter也适用于macOS的 。 因此,要安装它,只需在终端中运行以下命令:
recommend-type

cpptools-win32.vsix.zip

当vscode安装c/c++扩展时出现与系统不兼容,可离线下载并在扩展中从vsix中安装。使vscode可以自动跳转到变量、函数的声明、定义处,同时支持自动补全。安装完了,重启vscode就可以生效。
recommend-type

模拟电子技术基础简明教程Multisim

模拟电子技术基础简明教程Multisim,仿真实例,很珍贵的
recommend-type

01.WS 445-2014 电子病历基本数据集.rar

WS 445-2014 电子病历基本数据集
recommend-type

制作仪器半高宽补正曲线-jade初学者教程分析

制作仪器半高宽补正曲线 在一些需要仪器半高宽计算的处理前,必须设置好仪器的半高宽,Jade使用标准样品来制作一条随衍射角变化的半高宽曲线,当该曲线制作完成后,保存到参数文件中,以后测量所有的样品都使用该曲线所表示的半高宽作为仪器宽度。 标准样品必须是无晶粒细化、无应力(宏观应力或微观应力)、无畸变的完全退火态样品,一般采用 NIST-LaB6,Silicon-640作为标准样品。

最新推荐

recommend-type

应用CNN卷积神经网络构建的auto encoder自编码器,经过训练实现了对带有噪点的MNIST手写字体图片进行去噪的处理

资源下载链接为: https://pan.quark.cn/s/864eaed220e0 应用CNN卷积神经网络构建的auto encoder自编码器,经过训练实现了对带有噪点的MNIST手写字体图片进行去噪的处理(最新、最全版本!打开链接下载即可用!)
recommend-type

IP-guard应用程序预定义库

IP-guard应用程序预定义库
recommend-type

CH341A编程器软件1.3支持25Q256等32M芯片

CH341A编程器是一款广泛应用的通用编程设备,尤其在电子工程和嵌入式系统开发领域中,它被用来烧录各种类型的微控制器、存储器和其他IC芯片。这款编程器的最新版本为1.3,它的一个显著特点是增加了对25Q256等32M芯片的支持。 25Q256是一种串行EEPROM(电可擦可编程只读存储器)芯片,通常用于存储程序代码、配置数据或其他非易失性信息。32M在这里指的是存储容量,即该芯片可以存储32兆位(Mbit)的数据,换算成字节数就是4MB。这种大容量的存储器在许多嵌入式系统中都有应用,例如汽车电子、工业控制、消费电子设备等。 CH341A编程器的1.3版更新,意味着它可以与更多的芯片型号兼容,特别是针对32M容量的芯片进行了优化,提高了编程效率和稳定性。26系列芯片通常指的是Microchip公司的25系列SPI(串行外围接口)EEPROM产品线,这些芯片广泛应用于各种需要小体积、低功耗和非易失性存储的应用场景。 全功能版的CH341A编程器不仅支持25Q256,还支持其他大容量芯片,这意味着它具有广泛的兼容性,能够满足不同项目的需求。这包括但不限于微控制器、EPROM、EEPROM、闪存、逻辑门电路等多种类型芯片的编程。 使用CH341A编程器进行编程操作时,首先需要将设备通过USB连接到计算机,然后安装相应的驱动程序和编程软件。在本例中,压缩包中的"CH341A_1.30"很可能是编程软件的安装程序。安装后,用户可以通过软件界面选择需要编程的芯片类型,加载待烧录的固件或数据,然后执行编程操作。编程过程中需要注意的是,确保正确设置芯片的电压、时钟频率等参数,以防止损坏芯片。 CH341A编程器1.3版是面向电子爱好者和专业工程师的一款实用工具,其强大的兼容性和易用性使其在众多编程器中脱颖而出。对于需要处理25Q256等32M芯片的项目,或者26系列芯片的编程工作,CH341A编程器是理想的选择。通过持续的软件更新和升级,它保持了与现代电子技术同步,确保用户能方便地对各种芯片进行编程和调试。
recommend-type

修改多多巴枪SN软件,小白慎用

修改多多巴枪SN软件,小白慎用,米商巴枪的改机包
recommend-type

Python变量与数据类型详解

Python变量与数据类型详解
recommend-type

美国国际航空交通数据分析报告(1990-2020)

根据给定的信息,我们可以从中提取和分析以下知识点: 1. 数据集概述: 该数据集名为“U.S. International Air Traffic data(1990-2020)”,记录了美国与国际间航空客运和货运的详细统计信息。数据集涵盖的时间范围从1990年至2020年,这说明它包含了长达30年的时间序列数据,对于进行长期趋势分析非常有价值。 2. 数据来源及意义: 此数据来源于《美国国际航空客运和货运统计报告》,该报告是美国运输部(USDOT)所管理的T-100计划的一部分。T-100计划旨在收集和发布美国和国际航空公司在美国机场的出入境交通报告,这表明数据的权威性和可靠性较高,适用于政府、企业和学术研究等领域。 3. 数据内容及应用: 数据集包含两个主要的CSV文件,分别是“International_Report_Departures.csv”和“International_Report_Passengers.csv”。 a. International_Report_Departures.csv文件可能包含了以下内容: - 离港航班信息:记录了各航空公司的航班号、起飞和到达时间、起飞和到达机场的代码以及国际地区等信息。 - 航空公司信息:可能包括航空公司代码、名称以及所属国家等。 - 飞机机型信息:如飞机类型、座位容量等,这有助于分析不同机型的使用频率和趋势。 - 航线信息:包括航线的起始和目的国家及城市,对于研究航线网络和优化航班计划具有参考价值。 这些数据可以用于航空交通流量分析、机场运营效率评估、航空市场分析等。 b. International_Report_Passengers.csv文件可能包含了以下内容: - 航班乘客信息:可能包括乘客的国籍、年龄、性别等信息。 - 航班类型:如全客机、全货机或混合型航班,可以分析乘客运输和货物运输的比例。 - 乘客数量:记录了各航班或航线的乘客数量,对于分析航空市场容量和增长趋势很有帮助。 - 飞行里程信息:有助于了解国际间不同航线的长度和飞行距离,为票价设置和燃油成本分析提供数据支持。 这些数据可以用于航空客运市场分析、需求预测、收益管理等方面。 4. 数据分析和应用实例: - 航空流量分析:通过分析离港航班数据,可以观察到哪些航线最为繁忙,哪些机场的国际航空流量最大,这有助于航空公司调整航班时刻表和运力分配。 - 市场研究:乘客数据可以揭示不同国家和地区之间的人口流动趋势,帮助航空公司和政府机构了解国际旅行市场的需求变化。 - 飞机利用率:结合飞机机型和飞行频率信息,可以对特定机型的使用率进行分析,评估飞机维护需求和燃油效率。 - 安全监管:通过对比不同航空公司和航班的安全记录,监管机构可以更有效地评估航空公司的安全性能,并采取必要的监管措施。 5. 技术和方法论: 分析此类数据通常涉及数据清洗、数据整合、统计分析、时间序列分析、预测建模等数据科学方法。使用Excel、SQL、R、Python等工具进行数据处理和分析是常见的做法。例如,可以使用Python的Pandas库来清洗和准备数据,使用Matplotlib和Seaborn库来可视化数据,然后利用Scikit-learn或Statsmodels库来构建预测模型。 通过以上知识点的提取和分析,我们可以理解到“U.S. International Air Traffic data(1990-2020)-数据集”的重要性,它不仅记录了跨越30年的航空交通数据,还为各种分析和应用提供了详实的基础信息。对于航空业从业者、政策制定者、研究人员以及数据分析师来说,这是一个极具价值的数据资源。
recommend-type

统计学视角:深入理解最小二乘法的概率论基础

# 1. 最小二乘法的基本概念 最小二乘法(Least Squares Method, LSM)是统计学和数据分析中广泛使用的一种数学优化技术。其主要目的是通过最小化误差的平方和来寻找数据的最佳函数匹配。这种方法最早由高斯提出,但在工程、物理、经济和其他领域中有着广泛的应用。 在本章中,我们将首先了
recommend-type

vscode中使用Codeium

<think>好的,我需要回答用户如何在VSCode中使用Codeium插件的问题。首先,我需要回顾用户提供的引用内容,看看是否有相关信息。用户提供的引用[1]提到了Codeium Chat可以解释代码中的问题,引用[2]提到了在IDEA中的一些问题,可能对VSCode的安装有帮助。用户还提到了安装步骤、基本功能、常见问题等。 首先,我应该分步骤说明安装过程,包括在VSCode扩展商店搜索Codeium并安装。然后,登录部分可能需要用户访问仪表板获取API密钥,引用[2]中提到登录问题,可能需要提醒用户注意网络或权限设置。 接下来是基本功能,比如代码自动补全和Chat功能。引用[1]提到C
recommend-type

UniMoCo:统一框架下的多监督视觉学习方法

在详细解析“unimoco”这个概念之前,我们需要明确几个关键点。首先,“unimoco”代表的是一种视觉表示学习方法,它在机器学习尤其是深度学习领域中扮演着重要角色。其次,文章作者通过这篇论文介绍了UniMoCo的全称,即“Unsupervised, Semi-Supervised and Full-Supervised Visual Representation Learning”,其背后的含义是在于UniMoCo框架整合了无监督学习、半监督学习和全监督学习三种不同的学习策略。最后,该框架被官方用PyTorch库实现,并被提供给了研究者和开发者社区。 ### 1. 对比学习(Contrastive Learning) UniMoCo的概念根植于对比学习的思想,这是一种无监督学习的范式。对比学习的核心在于让模型学会区分不同的样本,通过将相似的样本拉近,将不相似的样本推远,从而学习到有效的数据表示。对比学习与传统的分类任务最大的不同在于不需要手动标注的标签来指导学习过程,取而代之的是从数据自身结构中挖掘信息。 ### 2. MoCo(Momentum Contrast) UniMoCo的实现基于MoCo框架,MoCo是一种基于队列(queue)的对比学习方法,它在训练过程中维持一个动态的队列,其中包含了成对的负样本。MoCo通过 Momentum Encoder(动量编码器)和一个队列来保持稳定和历史性的负样本信息,使得模型能够持续地进行对比学习,即使是在没有足够负样本的情况下。 ### 3. 无监督学习(Unsupervised Learning) 在无监督学习场景中,数据样本没有被标记任何类别或标签,算法需自行发现数据中的模式和结构。UniMoCo框架中,无监督学习的关键在于使用没有标签的数据进行训练,其目的是让模型学习到数据的基础特征表示,这对于那些标注资源稀缺的领域具有重要意义。 ### 4. 半监督学习(Semi-Supervised Learning) 半监督学习结合了无监督和有监督学习的优势,它使用少量的标注数据与大量的未标注数据进行训练。UniMoCo中实现半监督学习的方式,可能是通过将已标注的数据作为对比学习的一部分,以此来指导模型学习到更精准的特征表示。这对于那些拥有少量标注数据的场景尤为有用。 ### 5. 全监督学习(Full-Supervised Learning) 在全监督学习中,所有的训练样本都有相应的标签,这种学习方式的目的是让模型学习到映射关系,从输入到输出。在UniMoCo中,全监督学习用于训练阶段,让模型在有明确指示的学习目标下进行优化,学习到的任务相关的特征表示。这通常用于有充足标注数据的场景,比如图像分类任务。 ### 6. PyTorch PyTorch是一个开源机器学习库,由Facebook的人工智能研究团队开发,主要用于计算机视觉和自然语言处理等任务。它被广泛用于研究和生产环境,并且因其易用性、灵活性和动态计算图等特性受到研究人员的青睐。UniMoCo官方实现选择PyTorch作为开发平台,说明了其对科研社区的支持和对易于实现的重视。 ### 7. 可视化表示学习(Visual Representation Learning) 可视化表示学习的目的是从原始视觉数据中提取特征,并将它们转换为能够反映重要信息且更易于处理的形式。在UniMoCo中,无论是无监督、半监督还是全监督学习,最终的目标都是让模型学习到有效的视觉表示,这些表示可以用于下游任务,如图像分类、目标检测、图像分割等。 ### 8. 标签队列(Label Queue) UniMoCo通过标签队列维护受监管的标签,这可能意味着对于那些半监督或全监督学习的任务,模型在进行对比学习时,会参考这些来自标签队列的数据。标签队列机制能帮助模型更好地利用有限的标注数据,增强模型的泛化能力。 ### 结论 UniMoCo的提出,以及其官方PyTorch实现的发布,将对计算机视觉领域产生深远影响。它不仅提供了一个统一的对比学习框架,使得从无监督到全监督的学习过程更加灵活和高效,而且为研究者们提供了一个强力的工具,以便更好地探索和实现各种视觉任务。UniMoCo的研究和应用前景,为机器学习尤其是深度学习在视觉领域的研究和实践提供了新的视角和可能。
recommend-type

【MATLAB算法精讲】:最小二乘法的实现与案例深度分析

# 1. 最小二乘法的基本原理 最小二乘法是一种数学优化技术,它通过最小化误差的平方和来寻找数据的最佳函数匹配。其核心思想是选择一条曲线,使得所有观察点到这条曲线的距离之和最小。这种方法广泛应用于统计学、信号处理、工程学和经济学等领域,尤其适用于需要通过一组数据点来确定函数参数的情况。 ## 1.1 统计学视角下的最小二乘法 在统计学中,最小二乘法经常用于