chore: migrate project into clean repository

This commit is contained in:
yuuux
2026-08-13 16:50:52 +08:00
commit d1d25a09e7
27405 changed files with 9422808 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.13)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(listenai-cmake REQUIRED HINTS $ENV{ARCS_BASE})
project(arcs)
listenai_add_executable(${PROJECT_NAME})
target_sources(${PROJECT_NAME} PRIVATE
src/main.cpp
src/class_examples.cpp
src/vector_examples.cpp
)
listenai_include_directories(src)

View File

@@ -0,0 +1 @@
osource "$ARCS_BASE/Kconfig"

View File

@@ -0,0 +1,182 @@
C++ 基础使用示例
=================
功能说明
--------
演示 C++ 语言的基础特性,包括类、对象、C++11 新特性以及标准库容器的使用。本示例展示了如何在 ARCS SDK 中使用 C++ 进行开发。
硬件连接
--------
无需外部连接,C++ 语言特性为软件功能,不依赖硬件资源。
示例内容
--------
1. 演示 C++ 类和对象的基本使用
2. 演示 C++11 新特性(``constexpr``、智能指针等)
3. 演示 ``std::vector`` 容器的各种操作
4. 演示动态内存分配(``new``/``delete``)
5. 演示标准输入输出流的使用
编译
--------
.. include:: /sample_build.rst
烧录固件
--------
.. include:: /sample_flash.rst
预期输出
--------
.. code-block:: text
printf: Hello C++11 World!
cout: Hello World
=== C++ new 特性演示 ===
--- 单个对象分配 ---
A 构造函数调用
这是类 A
A 析构函数调用
--- 数组分配 ---
B 构造函数调用
B 构造函数调用
B 构造函数调用
这是类 B
这是类 B
这是类 B
B 析构函数调用
B 析构函数调用
B 析构函数调用
--- 不同类型对象分配 ---
A 构造函数调用
B 构造函数调用
C 构造函数调用
这是类 A
这是类 B
这是类 C
A 析构函数调用
B 析构函数调用
C 析构函数调用
--- 使用智能指针 ---
A 构造函数调用
这是类 A
A 析构函数调用
B 构造函数调用
这是类 B
这是类 B
B 析构函数调用
--- 二维数组分配 ---
A 构造函数调用
A 构造函数调用
A 构造函数调用
A 构造函数调用
A 构造函数调用
A 构造函数调用
这是类 A
这是类 A
A 析构函数调用
A 析构函数调用
A 析构函数调用
A 析构函数调用
A 析构函数调用
A 析构函数调用
--- 定位new (placement new) ---
C 构造函数调用
这是类 C
C 析构函数调用
=== Vector Basic Tests ===
Number: 1
Number: 2
Number: 3
=== Vector Initialization Tests ===
vector with list initialization: size=5
vector with fill constructor: size=5, first value=100
vector with range constructor: size=3
=== Vector Modification Tests ===
Adding elements to vector...
After adding 3 elements: size=3
After inserting at position 1: 1 99 2 3
After removing last element: size=3
=== Vector Capacity Tests ===
Empty vector: size=0, capacity=0
After reserve(10): size=0, capacity=10
After resize(5, 7): size=5, capacity=10
=== Vector Algorithm Tests ===
Sorted vector: 1 1 2 3 4 5 6 9
Found element 5 at position: 5
Count of element 1: 2
Simple Use Completed
核心 API
--------
.. list-table::
:header-rows: 1
* - API
- 说明
* - ``std::vector``
- C++ 标准库动态数组容器
* - ``new``/``delete``
- C++ 动态内存分配和释放
* - ``std::cout``
- C++ 标准输出流
* - ``std::unique_ptr``
- C++11 智能指针,自动管理内存
* - ``constexpr``
- C++11 编译时常量表达式
关键代码
--------
.. code-block:: cpp
// 使用 C++11 特性的类
class HelloWorld {
public:
void sayHello() const {
printf("printf: Hello C++11 World!\n");
std::cout << "cout: Hello World\r\n" << std::endl;
}
static constexpr int VALUE = 42;
};
// 使用 vector 容器
std::vector<int> numbers = {1, 2, 3};
for (const auto& num : numbers) {
printf(" Number: %d\n", num);
}
// 动态内存分配
A* pA = new A();
pA->display();
delete pA;
注意事项
--------
1. **内存管理**: 使用 ``new`` 分配的内存必须使用 ``delete`` 释放,使用 ``new[]`` 分配的内存必须使用 ``delete[]`` 释放
2. **智能指针**: 推荐使用 ``std::unique_ptr`` 等智能指针自动管理内存,避免内存泄漏
3. **容器使用**: ``std::vector`` 等容器会自动管理内存,无需手动释放
4. **C++11 特性**: 本示例使用了 C++11 标准特性,确保编译器支持 C++11 或更高版本
5. **混合使用**: 示例中混合使用了 C 标准库(``printf``)和 C++ 标准库(``std::cout``),两者可以共存使用

View File

@@ -0,0 +1,177 @@
#!/bin/bash
set -e
usage() {
echo "使用方式: $0 [选项]"
echo "选项:"
echo " -S, --Source <path> 指定项目源码路径 (默认为当前脚本所在目录)"
echo " -t, --target <target> 指定构建目标 (如 menuconfig)"
echo " -C, --Clean 清理构建目录"
echo " -B, --build 构建输出目录"
echo " -h, --help 显示此帮助信息"
echo " -r, --release 以 Release 模式构建 (移除 DEBUG_PATH 信息)"
echo " -w, --warnings-as-errors 将警告视为错误"
echo " -D<var>=<value> 传递 CMake 变量 (可多次使用)"
echo ""
echo "示例:"
echo " $0 -S samples/helloworld -DBOARD=arcs_mini 指定板型构建"
echo " $0 -S samples/helloworld -DBOARD=arcs_evb 使用 EVB 板型"
echo " $0 -S samples/helloworld -t menuconfig -DBOARD=arcs_mini 运行 menuconfig"
echo " $0 -C -S samples/helloworld -DBOARD=arcs_mini 清理并重新构建"
echo " $0 -S samples/helloworld -DBOARD=my_board -DBOARD_SEARCH_PATH=/path/to/boards 使用自定义板型"
exit 1
}
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
PROJECT_PATH="$SCRIPT_DIR"
TARGET=""
CLEAN=false
OUTPUT="build"
WARNINGS_AS_ERRORS=false
RELEASE=false
ARCS_BASE_DIR_NAME="arcs-sdk"
ARCS_DEV_TOOLS_DIR_NAME="listenai-dev-tools"
ARCS_DEV_TOOL_TOOLCHAIN_DIR_NAME="gcc"
ARCS_DEV_TOOL_LISTENAI_TOOLS_DIR_NAME="listenai-tools"
find_arcs_base() {
local current_dir=$(cd "$(dirname "$0")" && pwd)
local dir_name="$ARCS_BASE_DIR_NAME"
while [ "$current_dir" != "/" ]; do
if [ -d "$current_dir/$dir_name" ]; then
echo "Found ARCS_BASE: $current_dir/$dir_name"
export ARCS_BASE="$current_dir/$dir_name"
return 0
fi
current_dir=$(dirname "$current_dir")
done
echo "ARCS_BASE not found, Please add ARCS_BASE environment variable or set ARCS_BASE_DIR_NAME to the correct directory."
echo "Current Target ARCS_BASE directory name: $ARCS_BASE_DIR_NAME."
exit 1
}
find_dev_tools() {
local current_dir=$(cd "$(dirname "$0")" && pwd)
local dir_name="$ARCS_DEV_TOOLS_DIR_NAME"
echo "trying to find $dir_name in parent directories..."
while [ "$current_dir" != "/" ]; do
if [ -d "$current_dir/$dir_name" ]; then
echo "Found $dir_name: $current_dir/$dir_name"
if [ -d "$current_dir/$dir_name/$ARCS_DEV_TOOL_LISTENAI_TOOLS_DIR_NAME" ]; then
echo "Found LISTENAI_TOOLS_PATH: $current_dir/$dir_name/$ARCS_DEV_TOOL_LISTENAI_TOOLS_DIR_NAME"
export LISTENAI_TOOLS_PATH="$current_dir/$dir_name/$ARCS_DEV_TOOL_LISTENAI_TOOLS_DIR_NAME"
fi
if [ -d "$current_dir/$dir_name/$ARCS_DEV_TOOL_TOOLCHAIN_DIR_NAME" ]; then
echo "Found NUCLEI_TOOLCHAIN_PATH: $current_dir/$dir_name/$ARCS_DEV_TOOL_TOOLCHAIN_DIR_NAME"
export NUCLEI_TOOLCHAIN_PATH="$current_dir/$dir_name/$ARCS_DEV_TOOL_TOOLCHAIN_DIR_NAME"
fi
return 0
fi
current_dir=$(dirname "$current_dir")
done
}
while [[ $# -gt 0 ]]; do
case $1 in
-S|--Source)
PROJECT_PATH="$2"
shift 2
;;
-B|--build)
OUTPUT="$2"
shift 2
;;
-t|--target)
TARGET="$2"
shift 2
;;
-C|--Clean)
CLEAN=true
shift 1
;;
-w|--warnings-as-errors)
WARNINGS_AS_ERRORS=true
shift 1
;;
-h|--help)
usage
;;
-r|--release)
RELEASE=true
shift 1
;;
-D*)
CMAKE_VARS+=("$1")
shift 1
;;
*)
echo "未知参数: $1"
usage
;;
esac
done
echo "Source: $PROJECT_PATH"
echo "Target: $TARGET"
echo "Clean : $CLEAN"
if [ -z "$LISTENAI_TOOLS_PATH" ] || [ -z "$NUCLEI_TOOLCHAIN_PATH" ]; then
find_dev_tools
fi
if [ -z "${LISTENAI_TOOLS_PATH}" ]; then
export LISTENAI_TOOLS_PATH="请添加 LISTENAI_TOOLS_PATH 环境变量,或在此行设置正确的路径"
echo "请添加 LISTENAI_TOOLS_PATH 环境变量或者修改脚本后, 注释脚本第 $LINENO 行";exit 1;
fi
if [ -z "${NUCLEI_TOOLCHAIN_PATH}" ]; then
export NUCLEI_TOOLCHAIN_PATH="请添加 NUCLEI_TOOLCHAIN_PATH 环境变量,或在此行设置正确的路径"
echo "请添加 NUCLEI_TOOLCHAIN_PATH 环境变量或者修改脚本后, 注释脚本第 $LINENO 行";exit 1;
fi
############### 下面代码不用修改 ##################
# 构建工具的位置
CMAKE_PROGRAM="$LISTENAI_TOOLS_PATH/cmake/bin/cmake"
NINJA_PROGRAM="$LISTENAI_TOOLS_PATH/ninja/ninja"
# 配置环境变量 ARCS_BASE
if [ -z "$ARCS_BASE" ]; then
find_arcs_base
fi
if [ "$CLEAN" = true ]; then
rm -rf $OUTPUT
fi
# Initialize CMAKE_VARS array if it doesn't exist
declare -a CMAKE_VARS
# Add warnings-as-errors flag if enabled
if [ "$WARNINGS_AS_ERRORS" = true ]; then
CMAKE_VARS+=("-DCMAKE_C_FLAGS=-Werror")
CMAKE_VARS+=("-DCMAKE_CXX_FLAGS=-Werror")
echo "Treating warnings as errors"
fi
# Add release flags if enabled
if [ "$RELEASE" = true ]; then
CMAKE_VARS+=("-DENABLE_DEBUG_PATH=OFF")
echo "Release mode enabled (-DENABLE_DEBUG_PATH=OFF)"
fi
$CMAKE_PROGRAM -B "$OUTPUT" -G Ninja -S "$PROJECT_PATH" \
-DCMAKE_MAKE_PROGRAM="$NINJA_PROGRAM" \
"${CMAKE_VARS[@]}"
if [ -z "$TARGET" ]; then
$CMAKE_PROGRAM --build "$OUTPUT" -j4
else
$CMAKE_PROGRAM --build "$OUTPUT" --target "$TARGET"
fi

View File

@@ -0,0 +1,3 @@
# 项目可根据需要在此文件中设置对应的配置
CONFIG_MEM_CONFIG=y
CONFIG_LINK_OPTION_NONE_SPECS=y

View File

@@ -0,0 +1,8 @@
tests:
samples.cpp.simple_use:
programmer: arcs
runner: uart
log_analyzer:
type: "regex"
pattern: "Simple Use Completed"

View File

@@ -0,0 +1,100 @@
#include <stdio.h>
#include <memory> // 智能指针支持
class A {
public:
A() { printf("A 构造函数调用\n"); }
~A() { printf("A 析构函数调用\n"); }
void display() { printf("这是类 A\n"); }
};
class B {
public:
B() { printf("B 构造函数调用\n"); }
~B() { printf("B 析构函数调用\n"); }
void display() { printf("这是类 B\n"); }
};
class C {
public:
C() { printf("C 构造函数调用\n"); }
~C() { printf("C 析构函数调用\n"); }
void display() { printf("这是类 C\n"); }
};
// 演示C++的new多个class的特性
void demonstrateNewFeatures() {
printf("\n=== C++ new 特性演示 ===\n");
// 1. 基本单对象分配
printf("\n--- 单个对象分配 ---\n");
A* pA = new A();
pA->display();
delete pA; // 不要忘记释放内存
// 2. 数组分配
printf("\n--- 数组分配 ---\n");
B* arrB = new B[3]; // 创建3个B类对象
for (int i = 0; i < 3; i++) {
arrB[i].display();
}
delete[] arrB; // 使用delete[]释放数组
// 3. 不同类型对象的动态分配
printf("\n--- 不同类型对象分配 ---\n");
A* pA2 = new A();
B* pB = new B();
C* pC = new C();
pA2->display();
pB->display();
pC->display();
delete pA2;
delete pB;
delete pC;
// 4. 使用C++11智能指针
printf("\n--- 使用智能指针 ---\n");
std::unique_ptr<A> uptrA(new A());
uptrA->display();
// 自动释放,不需要手动delete
std::shared_ptr<B> sptrB1(new B());
{
std::shared_ptr<B> sptrB2 = sptrB1; // 共享所有权
sptrB2->display();
} // sptrB2离开作用域,但对象不会被销毁,因为sptrB1仍持有引用
sptrB1->display();
// 当最后一个shared_ptr离开作用域时,对象会自动被销毁
// 5. 二维数组分配
printf("\n--- 二维数组分配 ---\n");
int rows = 2, cols = 3;
// 方法1:一维数组指针的数组
A** matrix1 = new A*[rows];
for (int i = 0; i < rows; i++) {
matrix1[i] = new A[cols];
}
// 使用二维数组
matrix1[0][1].display();
matrix1[1][2].display();
// 释放二维数组内存
for (int i = 0; i < rows; i++) {
delete[] matrix1[i];
}
delete[] matrix1;
// 6. 使用定位new (placement new)
printf("\n--- 定位new (placement new) ---\n");
char* buffer = new char[sizeof(C)]; // 分配原始内存
C* placementC = new (buffer) C(); // 在分配的内存上构造对象
placementC->display();
placementC->~C(); // 手动调用析构函数
delete[] buffer; // 释放原始内存
}

View File

@@ -0,0 +1,8 @@
#ifndef NEW_FEATURES_DEMO_H
#define NEW_FEATURES_DEMO_H
#pragma once
void demonstrateNewFeatures();
#endif // NEW_FEATURES_DEMO_H

View File

@@ -0,0 +1,30 @@
#include <stdio.h> // C 标准库
#include <functional> // C++11 功能库
#include <iostream> // 简化版iostream实现
#include "class_examples.h"
#include "vector_examples.h"
// 使用 C++11 特性的类
class HelloWorld {
public:
void sayHello() const {
printf("printf: Hello C++11 World!\n");
std::cout << "cout: Hello World\r\n" << std::endl;
}
static constexpr int VALUE = 42;
};
int main(int argc, char **argv)
{
HelloWorld hello;
hello.sayHello();
demonstrateNewFeatures();
runAllVectorExamples();
printf("Simple Use Completed\r\n");
return 0;
}

View File

@@ -0,0 +1,21 @@
#include <cstring>
#include <iostream>
#include "uchardet.h"
#include "uchardet_test.h"
static const char *uchar_str[] = {
"简体中文简体中文简体中文简体中文简体中文简体中文简体中文简体中文简体中文简体中文简体中文", //gb18030
"姹夊瓧婕㈠瓧绲变竴绶ㄧ⒓钀<EFBFBD>湅纰<EFBFBD>", //zh_utf8
};
void uchardet_test(void){
for(int i=0;i<sizeof(uchar_str)/sizeof(uchar_str[0]);i++){
uchardet_t handle = uchardet_new();
uchardet_handle_data(handle, uchar_str[i], strlen(uchar_str[i]));
uchardet_data_end(handle);
std::cout << "Detected encoding: " << uchardet_get_charset(handle) << std::endl;
uchardet_delete(handle);
}
return;
}

View File

@@ -0,0 +1,6 @@
#ifndef UCHARDET_TEST_H
#define UCHARDET_TEST_H
void uchardet_test(void);
#endif

View File

@@ -0,0 +1,105 @@
#include "vector_examples.h"
#include <vector> // C++11 容器
#include <algorithm> // For vector algorithms
#include <stdio.h> // C 标准库
// 演示向量的基本用法
void demonstrateVectorBasic() {
printf("\n=== Vector Basic Tests ===\n");
std::vector<int> numbers = {1, 2, 3};
for (const auto& num : numbers) {
printf(" Number: %d\n", num);
}
}
// 演示向量的各种初始化方法
void demonstrateVectorInitialization() {
printf("\n=== Vector Initialization Tests ===\n");
// 使用列表初始化
std::vector<int> vec1 = {10, 20, 30, 40, 50};
printf("vector with list initialization: size=%zu\n", vec1.size());
// 使用填充构造函数
std::vector<int> vec2(5, 100); // 5个元素,每个值为100
printf("vector with fill constructor: size=%zu, first value=%d\n",
vec2.size(), vec2.front());
// 使用范围构造函数
std::vector<int> vec3(vec1.begin(), vec1.begin() + 3);
printf("vector with range constructor: size=%zu\n", vec3.size());
}
// 演示向量的修改操作(添加、插入、删除元素)
void demonstrateVectorModification() {
printf("\n=== Vector Modification Tests ===\n");
std::vector<int> vec4;
// 添加元素
printf("Adding elements to vector...\n");
vec4.push_back(1);
vec4.push_back(2);
vec4.push_back(3);
printf(" After adding 3 elements: size=%zu\n", vec4.size());
// 插入元素
vec4.insert(vec4.begin() + 1, 99);
printf(" After inserting at position 1: ");
for (const auto& n : vec4) {
printf("%d ", n);
}
printf("\n");
// 删除元素
vec4.pop_back();
printf(" After removing last element: size=%zu\n", vec4.size());
}
// 演示向量的容量和大小相关操作
void demonstrateVectorCapacity() {
printf("\n=== Vector Capacity Tests ===\n");
std::vector<int> vec5;
printf("Empty vector: size=%zu, capacity=%zu\n", vec5.size(), vec5.capacity());
// 预分配内存
vec5.reserve(10);
printf("After reserve(10): size=%zu, capacity=%zu\n", vec5.size(), vec5.capacity());
// 调整大小
vec5.resize(5, 7);
printf("After resize(5, 7): size=%zu, capacity=%zu\n", vec5.size(), vec5.capacity());
}
// 演示向量的算法操作(排序、查找、计数)
void demonstrateVectorAlgorithms() {
printf("\n=== Vector Algorithm Tests ===\n");
std::vector<int> vec6 = {3, 1, 4, 1, 5, 9, 2, 6};
// 排序
std::sort(vec6.begin(), vec6.end());
printf("Sorted vector: ");
for (const auto& n : vec6) {
printf("%d ", n);
}
printf("\n");
// 查找
auto it = std::find(vec6.begin(), vec6.end(), 5);
if (it != vec6.end()) {
printf("Found element 5 at position: %ld\n", it - vec6.begin());
} else {
printf("Element 5 not found\n");
}
// 计算元素数量
int count = std::count(vec6.begin(), vec6.end(), 1);
printf("Count of element 1: %d\n", count);
}
// 运行所有向量示例
void runAllVectorExamples() {
demonstrateVectorBasic();
demonstrateVectorInitialization();
demonstrateVectorModification();
demonstrateVectorCapacity();
demonstrateVectorAlgorithms();
}

View File

@@ -0,0 +1,22 @@
#ifndef VECTOR_EXAMPLES_H
#define VECTOR_EXAMPLES_H
// 演示向量的基本用法
void demonstrateVectorBasic();
// 演示向量的各种初始化方法
void demonstrateVectorInitialization();
// 演示向量的修改操作(添加、插入、删除元素)
void demonstrateVectorModification();
// 演示向量的容量和大小相关操作
void demonstrateVectorCapacity();
// 演示向量的算法操作(排序、查找、计数)
void demonstrateVectorAlgorithms();
// 运行所有向量示例
void runAllVectorExamples();
#endif // VECTOR_EXAMPLES_H