commit 7b0d4fd2c3dd17f28b733728fe3105a4ca071f13 Author: lucius Date: Mon Sep 1 18:46:27 2025 +0800 新增:实现 EventQueue 类,单线程事件队列 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..02e88ee --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/build/libs/ +/build/obj/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..21231eb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "list": "cpp" + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6e8d942 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +## V1.1.0 + +### 新增: + +实现 `EventQueue`类,单线程事件队列 diff --git a/build/Ndk_Android.mk b/build/Ndk_Android.mk new file mode 100644 index 0000000..25c8b09 --- /dev/null +++ b/build/Ndk_Android.mk @@ -0,0 +1,28 @@ +# 设置当前模块的源文件路径 +LOCAL_PATH := $(call my-dir) + +# 清除几乎所有 LOCAL_XXX 变量,为定义新模块做准备 +include $(CLEAR_VARS) + +# 模块名称,这将决定输出文件的名字(例如生成 libmynative.so) +LOCAL_MODULE := myapp + +# 指定需要编译的源文件 +LOCAL_SRC_FILES := \ + ../code/EventQueue.cpp ../code/main.cpp + +# 指定头文件搜索路径 +LOCAL_C_INCLUDES := \ + $(LOCAL_PATH)/../code/ + +# 编译C文件时的标志 +LOCAL_CFLAGS := -D__OS_ANDROID + +# 编译C++文件时的标志 +LOCAL_CPPFLAGS += -std=c++11 -android + +# 链接时依赖的系统库 +LOCAL_LDLIBS := -llog -lc + +# 指示构建系统将其编译为动态链接库 +include $(BUILD_EXECUTABLE) \ No newline at end of file diff --git a/build/Ndk_Application.mk b/build/Ndk_Application.mk new file mode 100644 index 0000000..3effe8f --- /dev/null +++ b/build/Ndk_Application.mk @@ -0,0 +1,4 @@ +APP_STL:=gnustl_static +APP_ABI:=armeabi-v7a +APP_PLATFORM:=android-23 +APP_BUILD_SCRIPT:=Ndk_Android.mk diff --git a/build/ndk_make.sh b/build/ndk_make.sh new file mode 100755 index 0000000..a22201f --- /dev/null +++ b/build/ndk_make.sh @@ -0,0 +1,99 @@ +#!/bin/bash + +source /opt/ndk/env/ndk_env.sh + +# 定义颜色 +RED="\033[0;31m" +GREEN="\033[0;32m" +YELLOW="\033[1;33m" +BLUE="\033[0;34m" +RESET="\033[0m" + +# 路径 +NDK_PROJECT_PATH=$(pwd) +APP_MK="./Ndk_Application.mk" +ANDROID_MK="./Ndk_Android.mk" +FIRMWARE_PATH="obj/local/armeabi-v7a/myapp" +DEVICE_DATA="/data/" +DEVICE_IP="" + +clean() { + echo -e "${YELLOW}Cleaning up...${RESET}" + ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=$APP_MK APP_BUILD_SCRIPT=$ANDROID_MK clean + echo -e "${YELLOW}Removing obj/ and libs/ directories...${RESET}" + rm -rf obj/ libs/ + sync + echo -e "${GREEN}Cleaned up.${RESET}" +} + +build_only() { + echo -e "${BLUE}Building myapp...${RESET}" + ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=$APP_MK APP_BUILD_SCRIPT=$ANDROID_MK + echo -e "${GREEN}Only build completed successfully.${RESET}" +} + +push_only() { + if [ -z "$DEVICE_IP" ]; then + echo -e "${RED}Error: No device IP provided. Use -p ${RESET}" + exit 1 + fi + echo -e "${BLUE}Connecting to device $DEVICE_IP...${RESET}" + adb connect $DEVICE_IP + if [ $? -ne 0 ]; then + echo -e "${RED}Failed to connect to device $DEVICE_IP${RESET}" + exit 1 + fi + + echo -e "${BLUE}Pushing myapp to $DEVICE_DATA...${RESET}" + adb -s $DEVICE_IP push $FIRMWARE_PATH $DEVICE_DATA + if [ $? -ne 0 ]; then + echo -e "${RED}Failed to push myapp to $DEVICE_DATA${RESET}" + exit 1 + fi + + echo -e "${GREEN}Push completed successfully.${RESET}" +} + +build_and_push() { + build_only + push_only +} + +show_help() { + echo -e "${YELLOW}Usage: $0 [options]${RESET}" + echo + echo "Options:" + echo -e " ${BLUE}-c${RESET} Clean project (remove obj/ and libs/)" + echo -e " ${BLUE}-p ${RESET} Push myapp to device with given IP" + echo -e " ${BLUE}-bp ${RESET} Build and push to device with given IP" + echo -e " ${BLUE}-h${RESET} Show this help message" + echo + echo -e "${YELLOW}Default:${RESET} If no options are given, only build the project." +} + +# 参数解析 +if [ $# -eq 0 ]; then + build_only + exit 0 +fi + +case "$1" in + -c) + clean + ;; + -p) + DEVICE_IP="$2" + push_only + ;; + -bp) + DEVICE_IP="$2" + build_and_push + ;; + -h) + show_help + ;; + *) + show_help + exit 1 + ;; +esac diff --git a/code/EventQueue.cpp b/code/EventQueue.cpp new file mode 100644 index 0000000..5cd9272 --- /dev/null +++ b/code/EventQueue.cpp @@ -0,0 +1,46 @@ +#include "EventQueue.h" + +bool EventQueue::EventQueue_AddEvent(int tInputCmd, void *pInputParam) +{ + cmdParam.push_back(pInputParam); + cmdList.push_back(tInputCmd); + return true; +} + +bool EventQueue::EventQueue_AddEventToFront(int tInputCmd, void *pInputParam) +{ + cmdParam.push_front(pInputParam); + cmdList.push_front(tInputCmd); + return true; +} + +bool EventQueue::EventQueue_PeekEvent(int *pOutputCmd, void **ppOutputParam) +{ + if (cmdList.empty()) + return false; + + (*pOutputCmd) = cmdList.front(); + (*ppOutputParam) = cmdParam.front(); + return true; +} + +void EventQueue::EventQueue_PopEvent(void) +{ + if (!cmdList.empty()) + { + cmdList.pop_front(); + cmdParam.pop_front(); + } +} + +bool EventQueue::EventQueue_GetEvent(int *pOutputCmd, void **ppOutputParam) +{ + if (cmdList.empty()) + return false; + + (*pOutputCmd) = cmdList.front(); + (*ppOutputParam) = cmdParam.front(); + cmdList.pop_front(); + cmdParam.pop_front(); + return true; +} diff --git a/code/EventQueue.h b/code/EventQueue.h new file mode 100644 index 0000000..5a547db --- /dev/null +++ b/code/EventQueue.h @@ -0,0 +1,23 @@ +#ifndef EVENTQUEUE_H +#define EVENTQUEUE_H + +#include +#include +#include +#include + +class EventQueue +{ +public: + bool EventQueue_AddEvent(int tInputCmd, void *pInputParam); + bool EventQueue_AddEventToFront(int tInputCmd, void *pInputParam); + bool EventQueue_PeekEvent(int *pOutputCmd, void **ppOutputParam); + void EventQueue_PopEvent(void); + bool EventQueue_GetEvent(int *pOutputCmd, void **ppOutputParam); + +private: + std::list cmdList; + std::list cmdParam; +}; + +#endif \ No newline at end of file diff --git a/code/main.cpp b/code/main.cpp new file mode 100644 index 0000000..0430dec --- /dev/null +++ b/code/main.cpp @@ -0,0 +1,43 @@ +#include "EventQueue.h" + +int main() +{ + EventQueue queue; + + /* 模拟事件数据 */ + int cmd1 = 101; + std::string param1 = "事件一"; + int cmd2 = 202; + std::string param2 = "事件二"; + int cmd3 = 303; + std::string param3 = "事件三"; + + std::cout << "=== 添加事件 ===" << std::endl; + queue.EventQueue_AddEvent(cmd1, ¶m1); // 队尾 + queue.EventQueue_AddEventToFront(cmd2, ¶m2); // 队头 + queue.EventQueue_AddEvent(cmd3, ¶m3); // 队尾 + + /* 查看队头事件 */ + int peekCmd; + void *peekParam; + if (queue.EventQueue_PeekEvent(&peekCmd, &peekParam)) + { + std::cout << "[Peek] cmd=" << peekCmd + << " param=" << *(std::string *)peekParam << std::endl; + } + + std::cout << "=== 获取并移除事件 ===" << std::endl; + int getCmd; + void *getParam; + while (queue.EventQueue_GetEvent(&getCmd, &getParam)) + { + std::cout << "[Get] cmd=" << getCmd + << " param=" << *(std::string *)getParam << std::endl; + + // queue.EventQueue_PopEvent(); // 队列中 Get 已经 pop 了,PopEvent 可省略 + } + + std::cout << "=== 队列已清空 ===" << std::endl; + + return 0; +} \ No newline at end of file