From aaec5cbf0f8f3b4c554a3a081a549eab8c1ba96d Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Thu, 25 Jul 2024 14:23:44 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=88=86=E6=94=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platfromIO_ESP32_oscilloscope/include/note.h | 124 -------------- .../lib/myBlueTooth/myBlueTooth.cpp | 155 +++++++++--------- .../lib/myBlueTooth/myBlueTooth.h | 35 ++-- platfromIO_ESP32_oscilloscope/platformio.ini | 1 + platfromIO_ESP32_oscilloscope/src/main.cpp | 16 +- 5 files changed, 100 insertions(+), 231 deletions(-) delete mode 100644 platfromIO_ESP32_oscilloscope/include/note.h diff --git a/platfromIO_ESP32_oscilloscope/include/note.h b/platfromIO_ESP32_oscilloscope/include/note.h deleted file mode 100644 index f0d7683e..00000000 --- a/platfromIO_ESP32_oscilloscope/include/note.h +++ /dev/null @@ -1,124 +0,0 @@ -/* -# learn note -# updata time: 2024-07-20 - # 关于“学会助手”移植想法,学会助手工程代码基于STM32F103 - # 硬件是:STM32F103C8T6 和 蓝牙模块,包括了经典蓝牙和低功耗蓝牙 - # 软件是:keil工程,stm32f1 使用 uart 和 蓝牙通信,实现蓝牙的通信,与学会助手的数据进行交互 - - # 分析,如何将硬件和软件移植 - # 硬件部分:STM32F1 and blueTooth --> ESP32-WROOM-32E 自带蓝牙模块 - # 软件部分:实现 ESP32 蓝牙的通信功能,封装好发送数据和接收数据的接口 api - # 移植部分:找到与学会助手相关的代码,往下找到控制硬件代码,将蓝牙发送和接收 api, - 换成 ESP32 的发送和接收 api, 对其逻辑代码进行测试和修改,满足学会助手规则 - - # 分层思想: - # 用户层:直接调用函数实现功能 - # 软件层:把规则和逻辑封装函数,对外提供接口 - # 控制层:对寄存器的操作,给硬件逻辑信号驱动工作 - # 硬件层:GPIO 输出高低电平,电机旋转,蓝牙发送和接收 - - - -// ESP32-WROOM-32E 引脚功能 start -------------------- - -GPIO 34 -GPIO 35 -GPIO 36 -GPIO 39 -上面四个引脚只能输入使用,下面引脚不建议使用 -GPIO 5 (启动时必须为高电平) -GPIO 12 (启动时必须为低电平) -GPIO 15 (启动时必须为高电平) - - -ESP32-WROOM-32E -3.3V GND 1 -EN P23 2 -SVP P22 3 -SVN TX 4 -P34 RX 5 -P35 P21 6 - -P32 GND 7 -P33 P19 8 -P25 P18 9 -P26 P5 10 -P27 P17 11 -P14 P16 12 -P12 P4 13 - -GND P0 14 -P13 P2 15 -SD2 P15 16 -SD3 SD1 17 -CMD SD0 18 -5V CLK 19 - -// ESP32-WROOM-32E 引脚功能 end -------------------- - - -// ADC 电压采集 -void setup() -{ - Serial.begin(115200); - // 9 bits and 12 bits 电压宽度映射值 - analogReadResolution(12); -} - -void loop() -{ - int analogValue = analogRead(35); - int anglogVoles = analogReadMilliVolts(35); - Serial.println("----------"); - Serial.printf("analogValue: %d\n", analogValue); - Serial.printf("anglogVoles: %d\n", anglogVoles); - - delay(300); -} -// ADC 电压采集 - - -*/ - -// #include -// #include - -// // DMA 传输完成的中断服务例程 -// void dma_isr() -// { -// dma.end(); // 结束 DMA 传输 -// // 在这里可以添加处理 DMA 完成后的动作 -// } - -// void setup() -// { -// Serial.begin(115200); - -// uint32_t src_buffer[1024]; -// uint32_t dest_buffer[1024]; - -// // 初始化源和目标缓冲区 -// for (int i = 0; i < 1024; i++) -// { -// src_buffer[i] = i; -// } - -// // 创建 DMA 通道 -// DMAChannel dma; - -// // 配置 DMA 通道 -// dma.transfer(src_buffer, dest_buffer, 1024); - -// // 设置 DMA 中断服务例程 -// dma.setCompleteCallback(dma_isr); - -// // 启动 DMA 传输 -// dma.start(); - -// // 从这里开始,CPU 可以执行其他任务,直到 DMA 完成触发中断 -// } - -// void loop() -// { -// // 在这里执行其他任务 -// } diff --git a/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp b/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp index c4509954..65872d3f 100644 --- a/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -7,7 +7,7 @@ */ BluetoothSerial SerialBT; -static char receivedBuffer[100]; +static char receivedBuffer[120]; void BTConfirmRequestCallback(uint32_t numVal); void BTAuthCompleteCallback(boolean success); @@ -127,7 +127,7 @@ void BTAuthCompleteCallback(boolean success) *********************************************************************************************************************/ // 发送一个字节 -bool XHZS_SendByte(uint8_t *byte) +bool XHZS_SendByte(const uint8_t *byte) { // 参数校验:确保byte不是空指针。 if (byte == nullptr) @@ -142,7 +142,7 @@ bool XHZS_SendByte(uint8_t *byte) } // 发送数组 -bool XHZS_SendArray(uint8_t *array, size_t arraySize) +bool XHZS_SendArray(const uint8_t *array, size_t arraySize) { if (array == nullptr || arraySize == 0) @@ -181,59 +181,6 @@ void XHZS_SendWave(const void *waveAddr, uint16_t waveSize) XHZS_SendArray(cmd_tail, sizeof(cmd_tail)); // 发送帧尾 } -// /************************************************************** -// *函数名称:SendWave -// *简 介:发送数据到学会助手APP虚拟示波器 -// *输 入:无 -// *输 出:无 -// *注意事项:将要发送的数据赋值给Wave数组即可,通道依次对应 -// **************************************************************/ -void SendWave() -{ - float i; - float Wave[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 - - // 正弦波测试 - for (i = 0; i < 500; i += 0.1) - { - Wave[0] = sin(i); - Wave[1] = cos(i); - Wave[2] = -sin(i); - Wave[3] = -cos(i); - XHZS_SendWave(Wave, sizeof(Wave)); - - vTaskDelay(100 / portTICK_PERIOD_MS); - } -} -// // 重载函数测试中使用 -// void XHZS_SendWave() -// { -// float i; -// float Wave[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 -// // // test one channel start -// // Wave[0] = 2; -// // Wave[1] = 4; -// // Wave[2] = 6; -// // Wave[3] = 8; - -// // XHZS_SendWave((uint8_t *)Wave, sizeof(Wave)); -// // test one channel end -// // --------------------------------------------- -// // // test two channel start -// // // 正弦波测试 -// for (i = 0; i < 500; i += 0.1) -// { -// Wave[0] = sin(i); -// Wave[1] = cos(i); -// Wave[2] = -sin(i); -// Wave[3] = -cos(i); -// XHZS_SendWave((uint8_t *)Wave, sizeof(Wave)); - -// vTaskDelay(100 / portTICK_PERIOD_MS); -// } -// // test two channel end -// } - /************************************************************** *函数名称:OscGetFloat *简 介:将字符转为浮点数 @@ -313,50 +260,94 @@ char XHZS_OscGetValue(const char *p, const char *name, float *value) } return 2; } - -// 蓝牙部分测试 start --------------------- -void test_blueTooth() +/************************************************************** + *函数名称:XHZS_ReceiveData + *简 介:读取接收到的蓝牙数据 + *输 入:无 + *输 出:char *data type + *注意事项:接收数据的缓冲区大小为120字节 + **************************************************************/ +char *XHZS_ReceiveData() { - - float floatValue = 3.14; - String str = "hello world"; - // 串口发送数据 - sendFloatViaBluetooth(floatValue); - sendStringViaBluetooth(str.c_str()); - // 串口接收数据,发送到串口, - SerialBT.println(getReceivedBluetoothData()); - // 蓝牙部分测试 end + return receivedBuffer; } -// 学会助手 test1 start -int intValue = 2; -float floatValue = 6.04; -// 测试字符串的变量发送 -void test_sendString() +// /************************************************************** +// *函数名称:SendWave +// *简 介:发送数据到学会助手APP虚拟示波器 +// *输 入:无 +// *输 出:无 +// *注意事项:将要发送的数据赋值给Wave数组即可,通道依次对应 +// **************************************************************/ +void SendWave() { - intValue = (intValue > 20 ? 0 : (intValue += 1)); - floatValue = (floatValue > 20 ? -10 : (floatValue += 4)); + // 输出平行线测试 start ----------------------- + int32_t line[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 + for (size_t i = 0; i < 500; i += 0.1) + { + line[0] = (line[0] > 10 ? -10 : (line[0] += 1)); + line[1] = 10.0; + line[2] = 5.0; + line[3] = 0.0; - SerialBT.printf("w:%d,%0.2f,6,8\r\n", intValue, floatValue); + XHZS_SendWave(line, sizeof(line)); + vTaskDelay(50 / portTICK_PERIOD_MS); // 延时 100ms + } + // 输出平行线测试 end ----------------------- + + // // 正弦波测试 start ----------------------- + // float Wave[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 + + // for (float i = 0; i < 500; i += 0.1) + // { + // Wave[0] = sin(i); + // Wave[1] = cos(i); + // Wave[2] = -sin(i); + // Wave[3] = -cos(i); + // XHZS_SendWave(Wave, sizeof(Wave)); + + // vTaskDelay(50 / portTICK_PERIOD_MS); + // } + // // 正弦波测试 end ----------------------- } -// 正弦波输出测试 -void test_sendSin() +// 串口输入和输出案例 +void test_serial_port_sending_and_receiving() +{ + vTaskDelay(300 / portTICK_PERIOD_MS); // 延时函数100ms,相当于 delay(100); 效果一样, 相当于刷新 + // 假设手机端的学会助手串口发送:hello + char *receiveData = XHZS_ReceiveData(); // 获取蓝牙发送过来的数据 + + // // Case one start + XHZS_SendByte((uint8_t *)receiveData); // 向手机串口发送数据,接收到:h + XHZS_SendArray((uint8_t *)receiveData, strlen(receiveData)); // 向手机串口发送数据,接收到:hello + // // Case one end + + // case two start + // Serial.printf("Computer:%s\r\n", receiveData); // 向电脑发送数据,接收到: hello + // SerialBT.printf("mobile phone:%s\r\n", receiveData); // 向手机串口发送数据,接收到:hello + // case two end +} +// 输出指定的数据类型案例 +void test_type_of_output() { - // XHZS_SendWave(); SendWave(); } -// 示波器的输入和输出测试 +// 示波器输入和输出案例 void test_oscilloscope_input_and_output() { - float floatValue1, floatValue2, floatValue3, floatValue4; - char *charData = getReceivedBluetoothData(); + float floatValue1, floatValue2, floatValue3, floatValue4; // 准备数据类型 + char *charData = getReceivedBluetoothData(); // 获取蓝牙接收的数据 + // 将字符串中的字段的数值提取出来 + // c11:14.12,c12:22,c13:33,c14: 55.66, + // c13字段,数值为33 XHZS_OscGetValue(charData, "c11", &floatValue1); XHZS_OscGetValue(charData, "c12", &floatValue2); XHZS_OscGetValue(charData, "c13", &floatValue3); XHZS_OscGetValue(charData, "c14", &floatValue4); SerialBT.printf("w:%0.2f,%0.2f,%0.2f,%0.2f\r\n", floatValue1, floatValue2, floatValue3, floatValue4); + vTaskDelay(50 / portTICK_PERIOD_MS); // 延时函数100ms,相当于 delay(100); 效果一样, 相当于刷新 } diff --git a/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h b/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h index da0c8b7e..91fdabdb 100644 --- a/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h +++ b/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h @@ -10,6 +10,9 @@ #include #include + +// 对蓝牙进行封装 start ------------------------- + // 初始化蓝牙设备并设置设备名称 void initializeBluetooth(const String &deviceName = "mini_bot"); @@ -22,41 +25,41 @@ void sendStringViaBluetooth(const char *stringData); // 获取通过蓝牙接收的字符串数据 char *getReceivedBluetoothData(); +// 对蓝牙进行封装 end ------------------------- // 学会助手移植部分 start ------------------------- -// 发送单个字节数据 -bool XHZS_SendByte(uint8_t *byte); -// 发送字节数组 -bool XHZS_SendArray(uint8_t *array, size_t arraySize); +// 发送单个字节数据 +bool XHZS_SendByte(const uint8_t *byte); -// 发送波形数据, 测试 -// void XHZS_SendWave(); +// 发送数组数据 +bool XHZS_SendArray(const uint8_t *array, size_t arraySize); // 此函数用于向示波器发送波形数据 -// 它作为一个模板,演示了如何正确地输出波形。 -// 用户应根据所需的具体波形类型调整函数内部的逻辑。 +// 它作为一个模板,演示了如何正确地输出波形 +// 用户应根据所需的具体波形类型调整函数内部的逻辑 void SendWave(); // 发送一组数据,包括帧头、内容和帧尾 void XHZS_SendWave(const void *waveAddr, uint16_t waveSize); // 从字符串中解析并获取浮点数 -// char XHZS_OscGetFloat(const char *p, float *value); +char XHZS_OscGetFloat(const char *p, float *value); // 根据名称从字符串中提取并获取值 char XHZS_OscGetValue(const char *p, const char *name, float *value); -// 学会助手移植部分 end ------------------------- -// 学会助手使用案例 start --------------------- +// 读取接收到的蓝牙数据 +char *XHZS_ReceiveData(); -void test_blueTooth(); // 蓝牙测试 +// 学会助手移植部分 end ------------------------- +// 学会助手,函数使用示范 start --------------------- -void test_sendString(); // 测试字符串的变量发送 +void test_serial_port_sending_and_receiving(); // 串口输入和输出案例 -void test_sendSin(); // 正弦波输出测试 +void test_type_of_output(); // 输出指定的数据类型案例 -void test_oscilloscope_input_and_output(); // 示波器的输入和输出测试 +void test_oscilloscope_input_and_output(); // 示波器输入和输出案例 -// 学会助手使用案例 end --------------------- +// 学会助手,函数使用示范 end --------------------- #endif /* MYBLUETOOTH_H_ */ diff --git a/platfromIO_ESP32_oscilloscope/platformio.ini b/platfromIO_ESP32_oscilloscope/platformio.ini index d1f99ca5..7e9a2ad9 100644 --- a/platfromIO_ESP32_oscilloscope/platformio.ini +++ b/platfromIO_ESP32_oscilloscope/platformio.ini @@ -12,3 +12,4 @@ platform = espressif32 board = esp-wrover-kit framework = arduino +board_build.partitions = partitions.csv diff --git a/platfromIO_ESP32_oscilloscope/src/main.cpp b/platfromIO_ESP32_oscilloscope/src/main.cpp index 61a11263..3fe13c7c 100644 --- a/platfromIO_ESP32_oscilloscope/src/main.cpp +++ b/platfromIO_ESP32_oscilloscope/src/main.cpp @@ -1,26 +1,24 @@ // // select engineering environment: ESP32_WROVER_Kit(all_versions) // 想使用“学会助手”功能,查看 myBlueTooth.h 头文件里面有相关说明 -// 学会助手的示波器部分可以使用,功能与基于 STM32 的 keil工程一致 +// 学会助手的串口和示波器,可以使用 ^_^ // #include #include "myBlueTooth.h" +BluetoothSerial SerialBlueTooth; // 实例化蓝牙串口 void setup() { - initializeBluetooth(); // 默认蓝牙名称为:mini_bot + initializeBluetooth(); // 默认蓝牙名称:mini_bot Serial.begin(115200); // 开启串口通信,频率为:115200 } void loop() { - // test_blueTooth(); // 蓝牙测试 + // test_serial_port_sending_and_receiving(); // 串口发送和接收案例 - // test_sendString(); // 测试字符串的变量发送 + test_type_of_output(); - test_sendSin(); // 正弦波输出测试 + // test_oscilloscope_input_and_output(); +} - // test_oscilloscope_input_and_output(); // 示波器的输入和输出测试 - vTaskDelay(100 / portTICK_PERIOD_MS); -} -// 想使用“学会助手”功能,查看 myBlueTooth.h 头文件里面有相关说明 -- Gitee From 74c513785079c9f08346d42b8e5a6879bd2148d1 Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Thu, 25 Jul 2024 15:55:43 +0800 Subject: [PATCH 2/4] add serial --- arduino_ESP32_oscilloscope/myBlueTooth.h | 62 ------ arduino_ESP32_oscilloscope/oscilloscope.ino | 26 --- .../{ => oscilloscope}/myBlueTooth.cpp | 183 +++++++++--------- .../oscilloscope/myBlueTooth.h | 62 ++++++ .../oscilloscope/oscilloscope.ino | 23 +++ platfromIO_ESP32_oscilloscope/include/note.h | 124 ++++++++++++ .../lib/myBlueTooth/myBlueTooth.cpp | 94 ++++----- .../lib/myBlueTooth/myBlueTooth.h | 63 +++--- platfromIO_ESP32_oscilloscope/src/main.cpp | 19 +- 9 files changed, 386 insertions(+), 270 deletions(-) delete mode 100644 arduino_ESP32_oscilloscope/myBlueTooth.h delete mode 100644 arduino_ESP32_oscilloscope/oscilloscope.ino rename arduino_ESP32_oscilloscope/{ => oscilloscope}/myBlueTooth.cpp (72%) create mode 100644 arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.h create mode 100644 arduino_ESP32_oscilloscope/oscilloscope/oscilloscope.ino create mode 100644 platfromIO_ESP32_oscilloscope/include/note.h diff --git a/arduino_ESP32_oscilloscope/myBlueTooth.h b/arduino_ESP32_oscilloscope/myBlueTooth.h deleted file mode 100644 index 89af7839..00000000 --- a/arduino_ESP32_oscilloscope/myBlueTooth.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * myBlueTooth.cpp + myBlueTooth.h - * 功能:对ESP32经典蓝牙的简单封装,工作在从机模式,基于单字节蓝牙发送定义了浮点数、字符串等发送函数 - * 说明:接收蓝牙数据时通过中断接收,直接调用相应接口就能读取 - */ -#ifndef MYBLUETOOTH_H_ -#define MYBLUETOOTH_H_ - -#include -#include -#include - -// 初始化蓝牙设备并设置设备名称 -void initializeBluetooth(const String &deviceName = "mini_bot"); - -// 将浮点数转换为字符串并通过蓝牙发送 -void sendFloatViaBluetooth(const float data); - -// 通过蓝牙发送字符串 -void sendStringViaBluetooth(const char *stringData); - -// 获取通过蓝牙接收的字符串数据 -char *getReceivedBluetoothData(); - -// 学会助手移植部分 start ------------------------- -// 发送单个字节数据 -bool XHZS_SendByte(uint8_t *byte); - -// 发送字节数组 -bool XHZS_SendArray(uint8_t *array, size_t arraySize); - -// 发送波形数据, 测试 -// void XHZS_SendWave(); - -// 此函数用于向示波器发送波形数据 -// 它作为一个模板,演示了如何正确地输出波形 -// 用户应根据所需的具体波形类型调整函数内部的逻辑 -void SendWave(); - -// 发送一组数据,包括帧头、内容和帧尾 -void XHZS_SendWave(const void *waveAddr, uint16_t waveSize); - -// 从字符串中解析并获取浮点数 -// char XHZS_OscGetFloat(const char *p, float *value); - -// 根据名称从字符串中提取并获取值 -char XHZS_OscGetValue(const char *p, const char *name, float *value); - -// 学会助手移植部分 end ------------------------- -// 学会助手使用案例 start --------------------- - -void test_blueTooth(); // 蓝牙测试 - -void test_sendString(); // 测试字符串的变量发送 - -void test_sendSin(); // 正弦波输出测试 - -void test_oscilloscope_input_and_output(); // 示波器的输入和输出测试 - -// 学会助手使用案例 end --------------------- - -#endif /* MYBLUETOOTH_H_ */ diff --git a/arduino_ESP32_oscilloscope/oscilloscope.ino b/arduino_ESP32_oscilloscope/oscilloscope.ino deleted file mode 100644 index ce8efc71..00000000 --- a/arduino_ESP32_oscilloscope/oscilloscope.ino +++ /dev/null @@ -1,26 +0,0 @@ -// -// select engineering environment: ESP32_WROVER_Kit(all_versions) -// 想使用“学会助手”功能,查看 myBlueTooth.h 头文件里面有相关说明 -// 学会助手的示波器部分可以使用,功能与基于 STM32 的 keil工程一致 -// -// #include -#include "myBlueTooth.h" - -void setup() -{ - initializeBluetooth(); // 默认蓝牙名称为:mini_bot - Serial.begin(115200); // 开启串口通信,频率为:115200 -} -void loop() -{ - // test_blueTooth(); // 蓝牙测试 - - // test_sendString(); // 测试字符串的变量发送 - - test_sendSin(); // 正弦波输出测试 - - // test_oscilloscope_input_and_output(); // 示波器的输入和输出测试 - - vTaskDelay(100 / portTICK_PERIOD_MS); -} -// 想使用“学会助手”功能,查看 myBlueTooth.h 头文件里面有相关说明 diff --git a/arduino_ESP32_oscilloscope/myBlueTooth.cpp b/arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.cpp similarity index 72% rename from arduino_ESP32_oscilloscope/myBlueTooth.cpp rename to arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.cpp index c4509954..63ed6f06 100644 --- a/arduino_ESP32_oscilloscope/myBlueTooth.cpp +++ b/arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.cpp @@ -1,21 +1,14 @@ #include "myBlueTooth.h" -/* -1)前半段是对蓝牙的进行封装, -2)后半段是对“学会助手”的ESP32核心板移植操作 - 如果出现 stm32f1 可以使用,在 esp32 中不能使用, - 可以自己进行修改代码,或者向“学会助手”的群里提出来,等待处理 -*/ + BluetoothSerial SerialBT; -static char receivedBuffer[100]; +static char receivedBuffer[120]; void BTConfirmRequestCallback(uint32_t numVal); void BTAuthCompleteCallback(boolean success); -/*************蓝牙参数配置**********************/ void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); // 蓝牙事件回调函数 -/*蓝牙参数配置*/ -// 初始化蓝牙配置 +/* 初始化蓝牙配置 */ void initializeBluetooth(const String &deviceName) { SerialBT.enableSSP(); // 在begin之前调用 @@ -48,7 +41,7 @@ char *getReceivedBluetoothData() return receivedBuffer; } -/******************* 蓝牙事件回调函数 ******************/ +/* 蓝牙事件回调函数 */ void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) // 蓝牙事件回调函数 { uint16_t index = 0; @@ -73,9 +66,20 @@ void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) // 蓝 // 数据接收标志 case ESP_SPP_DATA_IND_EVT: { + // memset(receivedBuffer, 0, sizeof(receivedBuffer)); + // while (SerialBT.available()) // 等待接收完成 + // { + // receivedBuffer[index++] = SerialBT.read(); // 按字节存储 + // } + memset(receivedBuffer, 0, sizeof(receivedBuffer)); while (SerialBT.available()) // 等待接收完成 { + if (index >= sizeof(receivedBuffer)) // 防止缓冲区溢出 + { + Serial.println("Received data too long."); + break; + } receivedBuffer[index++] = SerialBT.read(); // 按字节存储 } break; @@ -83,7 +87,7 @@ void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) // 蓝 // 数据发送标志 case ESP_SPP_WRITE_EVT: { - Serial.write("send complete!\r\n"); + // Serial.write("send complete!\r\n"); break; } } @@ -127,7 +131,7 @@ void BTAuthCompleteCallback(boolean success) *********************************************************************************************************************/ // 发送一个字节 -bool XHZS_SendByte(uint8_t *byte) +bool XHZS_SendByte(const uint8_t *byte) { // 参数校验:确保byte不是空指针。 if (byte == nullptr) @@ -142,7 +146,7 @@ bool XHZS_SendByte(uint8_t *byte) } // 发送数组 -bool XHZS_SendArray(uint8_t *array, size_t arraySize) +bool XHZS_SendArray(const uint8_t *array, size_t arraySize) { if (array == nullptr || arraySize == 0) @@ -151,8 +155,7 @@ bool XHZS_SendArray(uint8_t *array, size_t arraySize) } else { - // 数组发生是个耗时操作,建议DMA优化 - SerialBT.write(array, arraySize); + SerialBT.write(array, arraySize); // 数组发生是个耗时操作,建议DMA优化 return true; } } @@ -181,59 +184,6 @@ void XHZS_SendWave(const void *waveAddr, uint16_t waveSize) XHZS_SendArray(cmd_tail, sizeof(cmd_tail)); // 发送帧尾 } -// /************************************************************** -// *函数名称:SendWave -// *简 介:发送数据到学会助手APP虚拟示波器 -// *输 入:无 -// *输 出:无 -// *注意事项:将要发送的数据赋值给Wave数组即可,通道依次对应 -// **************************************************************/ -void SendWave() -{ - float i; - float Wave[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 - - // 正弦波测试 - for (i = 0; i < 500; i += 0.1) - { - Wave[0] = sin(i); - Wave[1] = cos(i); - Wave[2] = -sin(i); - Wave[3] = -cos(i); - XHZS_SendWave(Wave, sizeof(Wave)); - - vTaskDelay(100 / portTICK_PERIOD_MS); - } -} -// // 重载函数测试中使用 -// void XHZS_SendWave() -// { -// float i; -// float Wave[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 -// // // test one channel start -// // Wave[0] = 2; -// // Wave[1] = 4; -// // Wave[2] = 6; -// // Wave[3] = 8; - -// // XHZS_SendWave((uint8_t *)Wave, sizeof(Wave)); -// // test one channel end -// // --------------------------------------------- -// // // test two channel start -// // // 正弦波测试 -// for (i = 0; i < 500; i += 0.1) -// { -// Wave[0] = sin(i); -// Wave[1] = cos(i); -// Wave[2] = -sin(i); -// Wave[3] = -cos(i); -// XHZS_SendWave((uint8_t *)Wave, sizeof(Wave)); - -// vTaskDelay(100 / portTICK_PERIOD_MS); -// } -// // test two channel end -// } - /************************************************************** *函数名称:OscGetFloat *简 介:将字符转为浮点数 @@ -314,49 +264,94 @@ char XHZS_OscGetValue(const char *p, const char *name, float *value) return 2; } -// 蓝牙部分测试 start --------------------- -void test_blueTooth() +/************************************************************** + *函数名称:XHZS_ReceiveData + *简 介:读取接收到的蓝牙数据 + *输 入:无 + *输 出:char *data type + *注意事项:接收数据的缓冲区大小为120字节 + **************************************************************/ +char *XHZS_ReceiveData() { - - float floatValue = 3.14; - String str = "hello world"; - // 串口发送数据 - sendFloatViaBluetooth(floatValue); - sendStringViaBluetooth(str.c_str()); - // 串口接收数据,发送到串口, - SerialBT.println(getReceivedBluetoothData()); - // 蓝牙部分测试 end + return receivedBuffer; } -// 学会助手 test1 start -int intValue = 2; -float floatValue = 6.04; -// 测试字符串的变量发送 -void test_sendString() +// /************************************************************** +// *函数名称:SendWave +// *简 介:发送数据到学会助手APP虚拟示波器 +// *输 入:无 +// *输 出:无 +// *注意事项:将要发送的数据赋值给Wave数组即可,通道依次对应 +// **************************************************************/ +void SendWave() { - intValue = (intValue > 20 ? 0 : (intValue += 1)); - floatValue = (floatValue > 20 ? -10 : (floatValue += 4)); + // // 输出平行线测试 start ----------------------- + // int32_t line[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 + // for (size_t i = 0; i < 500; i += 0.1) + // { + // line[0] = (line[0] > 10 ? -10 : (line[0] += 1)); + // line[1] = 10.0; + // line[2] = 5.0; + // line[3] = 0.0; + + // XHZS_SendWave(line, sizeof(line)); + // vTaskDelay(50 / portTICK_PERIOD_MS); // 延时 100ms + // } + // // 输出平行线测试 end ----------------------- + + // 正弦波测试 start ----------------------- + float Wave[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 + + for (float i = 0; i < 500; i += 0.1) + { + Wave[0] = sin(i); + Wave[1] = cos(i); + Wave[2] = -sin(i); + Wave[3] = -cos(i); + XHZS_SendWave(Wave, sizeof(Wave)); - SerialBT.printf("w:%d,%0.2f,6,8\r\n", intValue, floatValue); + vTaskDelay(50 / portTICK_PERIOD_MS); + } + // 正弦波测试 end ----------------------- } -// 正弦波输出测试 -void test_sendSin() +// 串口输入和输出案例 +void test_serial_port_sending_and_receiving() +{ + vTaskDelay(300 / portTICK_PERIOD_MS); // 延时函数100ms,相当于 delay(100); 效果一样, 相当于刷新 + // 假设手机端的学会助手串口发送:hello + char *receiveData = XHZS_ReceiveData(); // 获取蓝牙发送过来的数据 + + // Case one start + XHZS_SendByte((uint8_t *)receiveData); // 发送单个字节:h + XHZS_SendArray((uint8_t *)receiveData, strlen(receiveData)); // 发送整个字符串:hello + // Case one end + + // // case two start + // Serial.printf("Computer:%s\r\n", receiveData); // 电脑端打印数据: hello + // SerialBT.printf("mobile phone:%s\r\n", receiveData); // 手机端接收数据:hello + // // case two end +} +// 输出指定的数据类型案例 +void test_type_of_output() { - // XHZS_SendWave(); SendWave(); } -// 示波器的输入和输出测试 +// 示波器输入和输出案例 void test_oscilloscope_input_and_output() { - float floatValue1, floatValue2, floatValue3, floatValue4; - char *charData = getReceivedBluetoothData(); + float floatValue1, floatValue2, floatValue3, floatValue4; // 准备数据类型 + char *charData = getReceivedBluetoothData(); // 获取蓝牙接收的数据 + // 将字符串中的字段的数值提取出来 + // c11:14.12,c12:22,c13:33,c14: 55.66, + // c13字段,数值为33 XHZS_OscGetValue(charData, "c11", &floatValue1); XHZS_OscGetValue(charData, "c12", &floatValue2); XHZS_OscGetValue(charData, "c13", &floatValue3); XHZS_OscGetValue(charData, "c14", &floatValue4); SerialBT.printf("w:%0.2f,%0.2f,%0.2f,%0.2f\r\n", floatValue1, floatValue2, floatValue3, floatValue4); + vTaskDelay(50 / portTICK_PERIOD_MS); // 延时函数100ms,相当于 delay(100); 效果一样, 相当于刷新 } diff --git a/arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.h b/arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.h new file mode 100644 index 00000000..82d8f66c --- /dev/null +++ b/arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.h @@ -0,0 +1,62 @@ +/* + * 文件: myBlueTooth.cpp 和 myBlueTooth.h + * + * 功能概述: 此代码提供了ESP32蓝牙操作的简易封装库,专为从机模式设计。 + * 实现了浮点数、字符串的蓝牙发送功能,并利用中断接收数据。 + */ + +#ifndef MYBLUETOOTH_H_ +#define MYBLUETOOTH_H_ + +#include +#include // 字符串操作库 +#include // ESP32蓝牙库 + +/************************* 蓝牙功能核心接口 *************************/ + +// 设定蓝牙名称,默认为"mini_bot"。 +void initializeBluetooth(const String &deviceName = "mini_bot"); + +// 将浮点数转换为字符串后通过蓝牙发送。 +void sendFloatViaBluetooth(const float data); + +// 发送字符串到蓝牙连接的设备 +void sendStringViaBluetooth(const char *stringData); + +// 返回一个指针指向接收到的字符串数据。 +char *getReceivedBluetoothData(); + +/************************* 高级数据处理接口 ************************/ + +// 这些接口用于更复杂的蓝牙数据交互场景。 + +// 发送单个字节 +bool XHZS_SendByte(const uint8_t byte); // 修改参数去指针化,直接传递字节值 + +// 发送数据数组 +bool XHZS_SendArray(const uint8_t *array, size_t arraySize); + +// 示例: 向示波器发送波形数据 +void SendWave(); + +// 组合发送数据帧 +void XHZS_SendWave(const void *waveAddr, uint16_t waveSize); + +// 解析字符串中的浮点数 +char XHZS_OscGetFloat(const char *p, float *value); + +// 根据名称从字符串中提取浮点数 +char XHZS_OscGetValue(const char *p, const char *name, float *value); + +// 接收蓝牙数据 +char *XHZS_ReceiveData(); + +/************************* 实战示例函数 ************************/ + +// 这些函数展示了如何实际应用上述接口,适合学习和参考。 + +void test_serial_port_sending_and_receiving(); // 学习如何进行基本的串口数据收发 +void test_type_of_output(); // 示例展示发送不同数据类型的使用方法 +void test_oscilloscope_input_and_output(); // 特定场景示例:与示波器的交互 + +#endif /* MYBLUETOOTH_H_ */ \ No newline at end of file diff --git a/arduino_ESP32_oscilloscope/oscilloscope/oscilloscope.ino b/arduino_ESP32_oscilloscope/oscilloscope/oscilloscope.ino new file mode 100644 index 00000000..60028f88 --- /dev/null +++ b/arduino_ESP32_oscilloscope/oscilloscope/oscilloscope.ino @@ -0,0 +1,23 @@ +/* +select engineering environment: ESP32_WROVER_Kit(all_versions) +想使用“学会助手”功能,查看 myBlueTooth.h 头文件里面有相关说明 +学会助手的串口和示波器功能,可以使用了 ^_^ +*/ + +// #include +#include "myBlueTooth.h" + +BluetoothSerial SerialBlueTooth; // 实例化蓝牙串口 +void setup() +{ + initializeBluetooth(); // 默认蓝牙名称:mini_bot + Serial.begin(115200); // 开启串口通信,频率为:115200 +} +void loop() +{ + test_serial_port_sending_and_receiving(); // 串口发送和接收案例 + + // test_type_of_output(); // 输出指定的数据类型案例 + + // test_oscilloscope_input_and_output();// 示波器输入和输出案例 +} diff --git a/platfromIO_ESP32_oscilloscope/include/note.h b/platfromIO_ESP32_oscilloscope/include/note.h new file mode 100644 index 00000000..f0d7683e --- /dev/null +++ b/platfromIO_ESP32_oscilloscope/include/note.h @@ -0,0 +1,124 @@ +/* +# learn note +# updata time: 2024-07-20 + # 关于“学会助手”移植想法,学会助手工程代码基于STM32F103 + # 硬件是:STM32F103C8T6 和 蓝牙模块,包括了经典蓝牙和低功耗蓝牙 + # 软件是:keil工程,stm32f1 使用 uart 和 蓝牙通信,实现蓝牙的通信,与学会助手的数据进行交互 + + # 分析,如何将硬件和软件移植 + # 硬件部分:STM32F1 and blueTooth --> ESP32-WROOM-32E 自带蓝牙模块 + # 软件部分:实现 ESP32 蓝牙的通信功能,封装好发送数据和接收数据的接口 api + # 移植部分:找到与学会助手相关的代码,往下找到控制硬件代码,将蓝牙发送和接收 api, + 换成 ESP32 的发送和接收 api, 对其逻辑代码进行测试和修改,满足学会助手规则 + + # 分层思想: + # 用户层:直接调用函数实现功能 + # 软件层:把规则和逻辑封装函数,对外提供接口 + # 控制层:对寄存器的操作,给硬件逻辑信号驱动工作 + # 硬件层:GPIO 输出高低电平,电机旋转,蓝牙发送和接收 + + + +// ESP32-WROOM-32E 引脚功能 start -------------------- + +GPIO 34 +GPIO 35 +GPIO 36 +GPIO 39 +上面四个引脚只能输入使用,下面引脚不建议使用 +GPIO 5 (启动时必须为高电平) +GPIO 12 (启动时必须为低电平) +GPIO 15 (启动时必须为高电平) + + +ESP32-WROOM-32E +3.3V GND 1 +EN P23 2 +SVP P22 3 +SVN TX 4 +P34 RX 5 +P35 P21 6 + +P32 GND 7 +P33 P19 8 +P25 P18 9 +P26 P5 10 +P27 P17 11 +P14 P16 12 +P12 P4 13 + +GND P0 14 +P13 P2 15 +SD2 P15 16 +SD3 SD1 17 +CMD SD0 18 +5V CLK 19 + +// ESP32-WROOM-32E 引脚功能 end -------------------- + + +// ADC 电压采集 +void setup() +{ + Serial.begin(115200); + // 9 bits and 12 bits 电压宽度映射值 + analogReadResolution(12); +} + +void loop() +{ + int analogValue = analogRead(35); + int anglogVoles = analogReadMilliVolts(35); + Serial.println("----------"); + Serial.printf("analogValue: %d\n", analogValue); + Serial.printf("anglogVoles: %d\n", anglogVoles); + + delay(300); +} +// ADC 电压采集 + + +*/ + +// #include +// #include + +// // DMA 传输完成的中断服务例程 +// void dma_isr() +// { +// dma.end(); // 结束 DMA 传输 +// // 在这里可以添加处理 DMA 完成后的动作 +// } + +// void setup() +// { +// Serial.begin(115200); + +// uint32_t src_buffer[1024]; +// uint32_t dest_buffer[1024]; + +// // 初始化源和目标缓冲区 +// for (int i = 0; i < 1024; i++) +// { +// src_buffer[i] = i; +// } + +// // 创建 DMA 通道 +// DMAChannel dma; + +// // 配置 DMA 通道 +// dma.transfer(src_buffer, dest_buffer, 1024); + +// // 设置 DMA 中断服务例程 +// dma.setCompleteCallback(dma_isr); + +// // 启动 DMA 传输 +// dma.start(); + +// // 从这里开始,CPU 可以执行其他任务,直到 DMA 完成触发中断 +// } + +// void loop() +// { +// // 在这里执行其他任务 +// } diff --git a/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp b/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp index 65872d3f..63ed6f06 100644 --- a/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -1,21 +1,14 @@ #include "myBlueTooth.h" -/* -1)前半段是对蓝牙的进行封装, -2)后半段是对“学会助手”的ESP32核心板移植操作 - 如果出现 stm32f1 可以使用,在 esp32 中不能使用, - 可以自己进行修改代码,或者向“学会助手”的群里提出来,等待处理 -*/ + BluetoothSerial SerialBT; static char receivedBuffer[120]; void BTConfirmRequestCallback(uint32_t numVal); void BTAuthCompleteCallback(boolean success); -/*************蓝牙参数配置**********************/ void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); // 蓝牙事件回调函数 -/*蓝牙参数配置*/ -// 初始化蓝牙配置 +/* 初始化蓝牙配置 */ void initializeBluetooth(const String &deviceName) { SerialBT.enableSSP(); // 在begin之前调用 @@ -48,7 +41,7 @@ char *getReceivedBluetoothData() return receivedBuffer; } -/******************* 蓝牙事件回调函数 ******************/ +/* 蓝牙事件回调函数 */ void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) // 蓝牙事件回调函数 { uint16_t index = 0; @@ -73,9 +66,20 @@ void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) // 蓝 // 数据接收标志 case ESP_SPP_DATA_IND_EVT: { + // memset(receivedBuffer, 0, sizeof(receivedBuffer)); + // while (SerialBT.available()) // 等待接收完成 + // { + // receivedBuffer[index++] = SerialBT.read(); // 按字节存储 + // } + memset(receivedBuffer, 0, sizeof(receivedBuffer)); while (SerialBT.available()) // 等待接收完成 { + if (index >= sizeof(receivedBuffer)) // 防止缓冲区溢出 + { + Serial.println("Received data too long."); + break; + } receivedBuffer[index++] = SerialBT.read(); // 按字节存储 } break; @@ -83,7 +87,7 @@ void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) // 蓝 // 数据发送标志 case ESP_SPP_WRITE_EVT: { - Serial.write("send complete!\r\n"); + // Serial.write("send complete!\r\n"); break; } } @@ -151,8 +155,7 @@ bool XHZS_SendArray(const uint8_t *array, size_t arraySize) } else { - // 数组发生是个耗时操作,建议DMA优化 - SerialBT.write(array, arraySize); + SerialBT.write(array, arraySize); // 数组发生是个耗时操作,建议DMA优化 return true; } } @@ -260,6 +263,7 @@ char XHZS_OscGetValue(const char *p, const char *name, float *value) } return 2; } + /************************************************************** *函数名称:XHZS_ReceiveData *简 介:读取接收到的蓝牙数据 @@ -281,34 +285,34 @@ char *XHZS_ReceiveData() // **************************************************************/ void SendWave() { - // 输出平行线测试 start ----------------------- - int32_t line[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 - for (size_t i = 0; i < 500; i += 0.1) - { - line[0] = (line[0] > 10 ? -10 : (line[0] += 1)); - line[1] = 10.0; - line[2] = 5.0; - line[3] = 0.0; + // // 输出平行线测试 start ----------------------- + // int32_t line[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 + // for (size_t i = 0; i < 500; i += 0.1) + // { + // line[0] = (line[0] > 10 ? -10 : (line[0] += 1)); + // line[1] = 10.0; + // line[2] = 5.0; + // line[3] = 0.0; - XHZS_SendWave(line, sizeof(line)); - vTaskDelay(50 / portTICK_PERIOD_MS); // 延时 100ms - } - // 输出平行线测试 end ----------------------- + // XHZS_SendWave(line, sizeof(line)); + // vTaskDelay(50 / portTICK_PERIOD_MS); // 延时 100ms + // } + // // 输出平行线测试 end ----------------------- - // // 正弦波测试 start ----------------------- - // float Wave[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 + // 正弦波测试 start ----------------------- + float Wave[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 - // for (float i = 0; i < 500; i += 0.1) - // { - // Wave[0] = sin(i); - // Wave[1] = cos(i); - // Wave[2] = -sin(i); - // Wave[3] = -cos(i); - // XHZS_SendWave(Wave, sizeof(Wave)); + for (float i = 0; i < 500; i += 0.1) + { + Wave[0] = sin(i); + Wave[1] = cos(i); + Wave[2] = -sin(i); + Wave[3] = -cos(i); + XHZS_SendWave(Wave, sizeof(Wave)); - // vTaskDelay(50 / portTICK_PERIOD_MS); - // } - // // 正弦波测试 end ----------------------- + vTaskDelay(50 / portTICK_PERIOD_MS); + } + // 正弦波测试 end ----------------------- } // 串口输入和输出案例 @@ -318,15 +322,15 @@ void test_serial_port_sending_and_receiving() // 假设手机端的学会助手串口发送:hello char *receiveData = XHZS_ReceiveData(); // 获取蓝牙发送过来的数据 - // // Case one start - XHZS_SendByte((uint8_t *)receiveData); // 向手机串口发送数据,接收到:h - XHZS_SendArray((uint8_t *)receiveData, strlen(receiveData)); // 向手机串口发送数据,接收到:hello - // // Case one end + // Case one start + XHZS_SendByte((uint8_t *)receiveData); // 发送单个字节:h + XHZS_SendArray((uint8_t *)receiveData, strlen(receiveData)); // 发送整个字符串:hello + // Case one end - // case two start - // Serial.printf("Computer:%s\r\n", receiveData); // 向电脑发送数据,接收到: hello - // SerialBT.printf("mobile phone:%s\r\n", receiveData); // 向手机串口发送数据,接收到:hello - // case two end + // // case two start + // Serial.printf("Computer:%s\r\n", receiveData); // 电脑端打印数据: hello + // SerialBT.printf("mobile phone:%s\r\n", receiveData); // 手机端接收数据:hello + // // case two end } // 输出指定的数据类型案例 void test_type_of_output() diff --git a/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h b/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h index 91fdabdb..82d8f66c 100644 --- a/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h +++ b/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h @@ -1,65 +1,62 @@ /* - * myBlueTooth.cpp + myBlueTooth.h - * 功能:对ESP32经典蓝牙的简单封装,工作在从机模式,基于单字节蓝牙发送定义了浮点数、字符串等发送函数 - * 说明:接收蓝牙数据时通过中断接收,直接调用相应接口就能读取 + * 文件: myBlueTooth.cpp 和 myBlueTooth.h + * + * 功能概述: 此代码提供了ESP32蓝牙操作的简易封装库,专为从机模式设计。 + * 实现了浮点数、字符串的蓝牙发送功能,并利用中断接收数据。 */ + #ifndef MYBLUETOOTH_H_ #define MYBLUETOOTH_H_ #include -#include -#include - +#include // 字符串操作库 +#include // ESP32蓝牙库 -// 对蓝牙进行封装 start ------------------------- +/************************* 蓝牙功能核心接口 *************************/ -// 初始化蓝牙设备并设置设备名称 +// 设定蓝牙名称,默认为"mini_bot"。 void initializeBluetooth(const String &deviceName = "mini_bot"); -// 将浮点数转换为字符串并通过蓝牙发送 +// 将浮点数转换为字符串后通过蓝牙发送。 void sendFloatViaBluetooth(const float data); -// 通过蓝牙发送字符串 +// 发送字符串到蓝牙连接的设备 void sendStringViaBluetooth(const char *stringData); -// 获取通过蓝牙接收的字符串数据 +// 返回一个指针指向接收到的字符串数据。 char *getReceivedBluetoothData(); -// 对蓝牙进行封装 end ------------------------- -// 学会助手移植部分 start ------------------------- +/************************* 高级数据处理接口 ************************/ + +// 这些接口用于更复杂的蓝牙数据交互场景。 -// 发送单个字节数据 -bool XHZS_SendByte(const uint8_t *byte); +// 发送单个字节 +bool XHZS_SendByte(const uint8_t byte); // 修改参数去指针化,直接传递字节值 -// 发送数组数据 +// 发送数据数组 bool XHZS_SendArray(const uint8_t *array, size_t arraySize); -// 此函数用于向示波器发送波形数据 -// 它作为一个模板,演示了如何正确地输出波形 -// 用户应根据所需的具体波形类型调整函数内部的逻辑 +// 示例: 向示波器发送波形数据 void SendWave(); -// 发送一组数据,包括帧头、内容和帧尾 +// 组合发送数据帧 void XHZS_SendWave(const void *waveAddr, uint16_t waveSize); -// 从字符串中解析并获取浮点数 +// 解析字符串中的浮点数 char XHZS_OscGetFloat(const char *p, float *value); -// 根据名称从字符串中提取并获取值 +// 根据名称从字符串中提取浮点数 char XHZS_OscGetValue(const char *p, const char *name, float *value); -// 读取接收到的蓝牙数据 -char *XHZS_ReceiveData(); - -// 学会助手移植部分 end ------------------------- -// 学会助手,函数使用示范 start --------------------- - -void test_serial_port_sending_and_receiving(); // 串口输入和输出案例 +// 接收蓝牙数据 +char *XHZS_ReceiveData(); -void test_type_of_output(); // 输出指定的数据类型案例 +/************************* 实战示例函数 ************************/ -void test_oscilloscope_input_and_output(); // 示波器输入和输出案例 +// 这些函数展示了如何实际应用上述接口,适合学习和参考。 -// 学会助手,函数使用示范 end --------------------- +void test_serial_port_sending_and_receiving(); // 学习如何进行基本的串口数据收发 +void test_type_of_output(); // 示例展示发送不同数据类型的使用方法 +void test_oscilloscope_input_and_output(); // 特定场景示例:与示波器的交互 -#endif /* MYBLUETOOTH_H_ */ +#endif /* MYBLUETOOTH_H_ */ \ No newline at end of file diff --git a/platfromIO_ESP32_oscilloscope/src/main.cpp b/platfromIO_ESP32_oscilloscope/src/main.cpp index 3fe13c7c..ef91800f 100644 --- a/platfromIO_ESP32_oscilloscope/src/main.cpp +++ b/platfromIO_ESP32_oscilloscope/src/main.cpp @@ -1,8 +1,9 @@ -// -// select engineering environment: ESP32_WROVER_Kit(all_versions) -// 想使用“学会助手”功能,查看 myBlueTooth.h 头文件里面有相关说明 -// 学会助手的串口和示波器,可以使用 ^_^ -// +/* +select engineering environment: ESP32_WROVER_Kit(all_versions) +想使用“学会助手”功能,查看 myBlueTooth.h 头文件里面有相关说明 +学会助手的串口和示波器功能,可以使用了 ^_^ +*/ + #include #include "myBlueTooth.h" @@ -14,11 +15,9 @@ void setup() } void loop() { - // test_serial_port_sending_and_receiving(); // 串口发送和接收案例 + test_serial_port_sending_and_receiving(); // 串口发送和接收案例 - test_type_of_output(); + // test_type_of_output(); // 输出指定的数据类型案例 - // test_oscilloscope_input_and_output(); + // test_oscilloscope_input_and_output();// 示波器输入和输出案例 } - - -- Gitee From f08d815be9c91ef17571f352f7b7f61898834a88 Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Thu, 25 Jul 2024 15:57:36 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h b/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h index 82d8f66c..02ebd3ad 100644 --- a/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h +++ b/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h @@ -31,7 +31,7 @@ char *getReceivedBluetoothData(); // 这些接口用于更复杂的蓝牙数据交互场景。 // 发送单个字节 -bool XHZS_SendByte(const uint8_t byte); // 修改参数去指针化,直接传递字节值 +bool XHZS_SendByte(const uint8_t *byte); // 修改参数去指针化,直接传递字节值 // 发送数据数组 bool XHZS_SendArray(const uint8_t *array, size_t arraySize); -- Gitee From ecad7323ec255177d0fe8cff440504e47f5d04d9 Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Thu, 25 Jul 2024 23:50:51 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E8=A7=A3=E5=86=B3=EF=BC=8C=E4=B8=B2?= =?UTF-8?q?=E5=8F=A3=E5=8F=91=E9=80=81=E4=B8=80=E6=9D=A1=E6=B6=88=E6=81=AF?= =?UTF-8?q?=EF=BC=8C=E6=8E=A5=E6=94=B6=E4=B8=80=E6=9D=A1=E6=B6=88=E6=81=AF?= =?UTF-8?q?=EF=BC=8C=E4=B8=8D=E6=98=AF=E8=BF=9E=E7=BB=AD=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oscilloscope/myBlueTooth.cpp | 101 +++++++------- .../oscilloscope/myBlueTooth.h | 16 ++- .../oscilloscope/oscilloscope.ino | 11 +- platfromIO_ESP32_oscilloscope/include/note.h | 124 ++++++++++++++++++ .../lib/myBlueTooth/myBlueTooth.cpp | 101 +++++++------- .../lib/myBlueTooth/myBlueTooth.h | 16 ++- platfromIO_ESP32_oscilloscope/src/main.cpp | 11 +- 7 files changed, 272 insertions(+), 108 deletions(-) create mode 100644 platfromIO_ESP32_oscilloscope/include/note.h diff --git a/arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.cpp b/arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.cpp index 63ed6f06..cbea9628 100644 --- a/arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.cpp +++ b/arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.cpp @@ -1,11 +1,15 @@ #include "myBlueTooth.h" -BluetoothSerial SerialBT; +BluetoothSerial SerialBT; // 蓝牙类实例化 -static char receivedBuffer[120]; +static char receivedBuffer[120]; // 接收缓冲区 -void BTConfirmRequestCallback(uint32_t numVal); -void BTAuthCompleteCallback(boolean success); +bool SERIAL_PORT_FUNCTION_ENABLE = false; // 串口功能开关 + +String BlueToothName; + +void BTConfirmRequestCallback(uint32_t numVal); // 认证请求回调 +void BTAuthCompleteCallback(boolean success); // 认证结果回调函数 void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); // 蓝牙事件回调函数 /* 初始化蓝牙配置 */ @@ -14,8 +18,15 @@ void initializeBluetooth(const String &deviceName) SerialBT.enableSSP(); // 在begin之前调用 SerialBT.onConfirmRequest(BTConfirmRequestCallback); SerialBT.onAuthComplete(BTAuthCompleteCallback); + SerialBT.register_callback(Bluetooth_Event); // 设置事件回调函数 连接 断开 发送 接收 - SerialBT.begin(deviceName); // Bluetooth device name + + if (SERIAL_PORT_FUNCTION_ENABLE == true) + { + SerialBT.onData(XHZS_SeriaPortReceivingData); // 串口数据接收回调函数 + } + SerialBT.begin(deviceName); // Bluetooth device name + BlueToothName = deviceName; } /*蓝牙发送浮点数据,转为字符串发送*/ @@ -66,20 +77,9 @@ void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) // 蓝 // 数据接收标志 case ESP_SPP_DATA_IND_EVT: { - // memset(receivedBuffer, 0, sizeof(receivedBuffer)); - // while (SerialBT.available()) // 等待接收完成 - // { - // receivedBuffer[index++] = SerialBT.read(); // 按字节存储 - // } - memset(receivedBuffer, 0, sizeof(receivedBuffer)); while (SerialBT.available()) // 等待接收完成 { - if (index >= sizeof(receivedBuffer)) // 防止缓冲区溢出 - { - Serial.println("Received data too long."); - break; - } receivedBuffer[index++] = SerialBT.read(); // 按字节存储 } break; @@ -130,33 +130,56 @@ void BTAuthCompleteCallback(boolean success) * 西电 嵌牛实验室 *********************************************************************************************************************/ -// 发送一个字节 -bool XHZS_SendByte(const uint8_t *byte) +// 串口数据接收回调函数 +void XHZS_SeriaPortReceivingData(const uint8_t *buffer, size_t size) { - // 参数校验:确保byte不是空指针。 - if (byte == nullptr) - { - return false; - } - else + if (size > 0) { - SerialBT.write(*byte); - return true; + Serial.write(buffer, size); // 将数据转发至标准串口,串口监视器 + SerialBT.write(buffer, size); // 将数据转发至蓝牙串口,手机端 } } +/************************************************************** + *函数名称:XHZS_ReceiveData + *简 介:读取接收到的蓝牙数据 + *输 入:无 + *输 出:uint8_t + *注意事项:接收数据的缓冲区大小为120字节 + **************************************************************/ +char *XHZS_ReceiveData() +{ + return receivedBuffer; +} + +/* +是否开启串口功能,默认关闭串口功能 +连接的手机端发送数据后,蓝牙端ESP32接收的数据后,触发回调函数 XHZS_SeriaPortReceivingData() +发送一次,触发一次,想修改逻辑需要在回调函数中修改,连续触发不建议使用 +*/ +void XHZS_SerialPortFunctionEnabled(bool Serial_port_status) +{ + SERIAL_PORT_FUNCTION_ENABLE = Serial_port_status; + initializeBluetooth(BlueToothName); +} + +// 发送一个字节 +void XHZS_SendByte(const uint8_t *byte) +{ + SerialBT.write(*byte); +} + // 发送数组 -bool XHZS_SendArray(const uint8_t *array, size_t arraySize) +void XHZS_SendArray(const uint8_t *array, size_t arraySize) { if (array == nullptr || arraySize == 0) { - return false; + return; } else { SerialBT.write(array, arraySize); // 数组发生是个耗时操作,建议DMA优化 - return true; } } @@ -264,18 +287,6 @@ char XHZS_OscGetValue(const char *p, const char *name, float *value) return 2; } -/************************************************************** - *函数名称:XHZS_ReceiveData - *简 介:读取接收到的蓝牙数据 - *输 入:无 - *输 出:char *data type - *注意事项:接收数据的缓冲区大小为120字节 - **************************************************************/ -char *XHZS_ReceiveData() -{ - return receivedBuffer; -} - // /************************************************************** // *函数名称:SendWave // *简 介:发送数据到学会助手APP虚拟示波器 @@ -327,10 +338,10 @@ void test_serial_port_sending_and_receiving() XHZS_SendArray((uint8_t *)receiveData, strlen(receiveData)); // 发送整个字符串:hello // Case one end - // // case two start - // Serial.printf("Computer:%s\r\n", receiveData); // 电脑端打印数据: hello - // SerialBT.printf("mobile phone:%s\r\n", receiveData); // 手机端接收数据:hello - // // case two end + // case two start + // Serial.printf("Computer:%s\r\n", receiveData); // 输出到串口监视器: hello + // SerialBT.printf("mobile phone:%s\r\n", receiveData); // 输出的连接端:hello + // case two end } // 输出指定的数据类型案例 void test_type_of_output() diff --git a/arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.h b/arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.h index 82d8f66c..5aed81fd 100644 --- a/arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.h +++ b/arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.h @@ -30,11 +30,14 @@ char *getReceivedBluetoothData(); // 这些接口用于更复杂的蓝牙数据交互场景。 +// 是否开启串口功能,默认关闭串口功能 +void XHZS_SerialPortFunctionEnabled(bool Serial_port_status = false); + // 发送单个字节 -bool XHZS_SendByte(const uint8_t byte); // 修改参数去指针化,直接传递字节值 +void XHZS_SendByte(const uint8_t *byte); // 发送数据数组 -bool XHZS_SendArray(const uint8_t *array, size_t arraySize); +void XHZS_SendArray(const uint8_t *array, size_t arraySize); // 示例: 向示波器发送波形数据 void SendWave(); @@ -49,7 +52,10 @@ char XHZS_OscGetFloat(const char *p, float *value); char XHZS_OscGetValue(const char *p, const char *name, float *value); // 接收蓝牙数据 -char *XHZS_ReceiveData(); +char *XHZS_ReceiveData(); + +// 串口数据接收回调函数 +void XHZS_SeriaPortReceivingData(const uint8_t *buffer, size_t size); /************************* 实战示例函数 ************************/ @@ -57,6 +63,6 @@ char *XHZS_ReceiveData(); void test_serial_port_sending_and_receiving(); // 学习如何进行基本的串口数据收发 void test_type_of_output(); // 示例展示发送不同数据类型的使用方法 -void test_oscilloscope_input_and_output(); // 特定场景示例:与示波器的交互 +void test_oscilloscope_input_and_output(); // 示波器输入和输出案例 -#endif /* MYBLUETOOTH_H_ */ \ No newline at end of file +#endif /* MYBLUETOOTH_H_ */ diff --git a/arduino_ESP32_oscilloscope/oscilloscope/oscilloscope.ino b/arduino_ESP32_oscilloscope/oscilloscope/oscilloscope.ino index 60028f88..d1cc71c1 100644 --- a/arduino_ESP32_oscilloscope/oscilloscope/oscilloscope.ino +++ b/arduino_ESP32_oscilloscope/oscilloscope/oscilloscope.ino @@ -7,17 +7,20 @@ select engineering environment: ESP32_WROVER_Kit(all_versions) // #include #include "myBlueTooth.h" -BluetoothSerial SerialBlueTooth; // 实例化蓝牙串口 void setup() { - initializeBluetooth(); // 默认蓝牙名称:mini_bot Serial.begin(115200); // 开启串口通信,频率为:115200 + + initializeBluetooth(); // 默认蓝牙名称:mini_bot + + // XHZS_SerialPortFunctionEnabled(true); // 开启串口功能 } + void loop() { - test_serial_port_sending_and_receiving(); // 串口发送和接收案例 + // test_serial_port_sending_and_receiving(); // 串口发送和接收案例 - // test_type_of_output(); // 输出指定的数据类型案例 + test_type_of_output(); // 输出指定的数据类型案例 // test_oscilloscope_input_and_output();// 示波器输入和输出案例 } diff --git a/platfromIO_ESP32_oscilloscope/include/note.h b/platfromIO_ESP32_oscilloscope/include/note.h new file mode 100644 index 00000000..f0d7683e --- /dev/null +++ b/platfromIO_ESP32_oscilloscope/include/note.h @@ -0,0 +1,124 @@ +/* +# learn note +# updata time: 2024-07-20 + # 关于“学会助手”移植想法,学会助手工程代码基于STM32F103 + # 硬件是:STM32F103C8T6 和 蓝牙模块,包括了经典蓝牙和低功耗蓝牙 + # 软件是:keil工程,stm32f1 使用 uart 和 蓝牙通信,实现蓝牙的通信,与学会助手的数据进行交互 + + # 分析,如何将硬件和软件移植 + # 硬件部分:STM32F1 and blueTooth --> ESP32-WROOM-32E 自带蓝牙模块 + # 软件部分:实现 ESP32 蓝牙的通信功能,封装好发送数据和接收数据的接口 api + # 移植部分:找到与学会助手相关的代码,往下找到控制硬件代码,将蓝牙发送和接收 api, + 换成 ESP32 的发送和接收 api, 对其逻辑代码进行测试和修改,满足学会助手规则 + + # 分层思想: + # 用户层:直接调用函数实现功能 + # 软件层:把规则和逻辑封装函数,对外提供接口 + # 控制层:对寄存器的操作,给硬件逻辑信号驱动工作 + # 硬件层:GPIO 输出高低电平,电机旋转,蓝牙发送和接收 + + + +// ESP32-WROOM-32E 引脚功能 start -------------------- + +GPIO 34 +GPIO 35 +GPIO 36 +GPIO 39 +上面四个引脚只能输入使用,下面引脚不建议使用 +GPIO 5 (启动时必须为高电平) +GPIO 12 (启动时必须为低电平) +GPIO 15 (启动时必须为高电平) + + +ESP32-WROOM-32E +3.3V GND 1 +EN P23 2 +SVP P22 3 +SVN TX 4 +P34 RX 5 +P35 P21 6 + +P32 GND 7 +P33 P19 8 +P25 P18 9 +P26 P5 10 +P27 P17 11 +P14 P16 12 +P12 P4 13 + +GND P0 14 +P13 P2 15 +SD2 P15 16 +SD3 SD1 17 +CMD SD0 18 +5V CLK 19 + +// ESP32-WROOM-32E 引脚功能 end -------------------- + + +// ADC 电压采集 +void setup() +{ + Serial.begin(115200); + // 9 bits and 12 bits 电压宽度映射值 + analogReadResolution(12); +} + +void loop() +{ + int analogValue = analogRead(35); + int anglogVoles = analogReadMilliVolts(35); + Serial.println("----------"); + Serial.printf("analogValue: %d\n", analogValue); + Serial.printf("anglogVoles: %d\n", anglogVoles); + + delay(300); +} +// ADC 电压采集 + + +*/ + +// #include +// #include + +// // DMA 传输完成的中断服务例程 +// void dma_isr() +// { +// dma.end(); // 结束 DMA 传输 +// // 在这里可以添加处理 DMA 完成后的动作 +// } + +// void setup() +// { +// Serial.begin(115200); + +// uint32_t src_buffer[1024]; +// uint32_t dest_buffer[1024]; + +// // 初始化源和目标缓冲区 +// for (int i = 0; i < 1024; i++) +// { +// src_buffer[i] = i; +// } + +// // 创建 DMA 通道 +// DMAChannel dma; + +// // 配置 DMA 通道 +// dma.transfer(src_buffer, dest_buffer, 1024); + +// // 设置 DMA 中断服务例程 +// dma.setCompleteCallback(dma_isr); + +// // 启动 DMA 传输 +// dma.start(); + +// // 从这里开始,CPU 可以执行其他任务,直到 DMA 完成触发中断 +// } + +// void loop() +// { +// // 在这里执行其他任务 +// } diff --git a/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp b/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp index 63ed6f06..cbea9628 100644 --- a/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -1,11 +1,15 @@ #include "myBlueTooth.h" -BluetoothSerial SerialBT; +BluetoothSerial SerialBT; // 蓝牙类实例化 -static char receivedBuffer[120]; +static char receivedBuffer[120]; // 接收缓冲区 -void BTConfirmRequestCallback(uint32_t numVal); -void BTAuthCompleteCallback(boolean success); +bool SERIAL_PORT_FUNCTION_ENABLE = false; // 串口功能开关 + +String BlueToothName; + +void BTConfirmRequestCallback(uint32_t numVal); // 认证请求回调 +void BTAuthCompleteCallback(boolean success); // 认证结果回调函数 void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); // 蓝牙事件回调函数 /* 初始化蓝牙配置 */ @@ -14,8 +18,15 @@ void initializeBluetooth(const String &deviceName) SerialBT.enableSSP(); // 在begin之前调用 SerialBT.onConfirmRequest(BTConfirmRequestCallback); SerialBT.onAuthComplete(BTAuthCompleteCallback); + SerialBT.register_callback(Bluetooth_Event); // 设置事件回调函数 连接 断开 发送 接收 - SerialBT.begin(deviceName); // Bluetooth device name + + if (SERIAL_PORT_FUNCTION_ENABLE == true) + { + SerialBT.onData(XHZS_SeriaPortReceivingData); // 串口数据接收回调函数 + } + SerialBT.begin(deviceName); // Bluetooth device name + BlueToothName = deviceName; } /*蓝牙发送浮点数据,转为字符串发送*/ @@ -66,20 +77,9 @@ void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) // 蓝 // 数据接收标志 case ESP_SPP_DATA_IND_EVT: { - // memset(receivedBuffer, 0, sizeof(receivedBuffer)); - // while (SerialBT.available()) // 等待接收完成 - // { - // receivedBuffer[index++] = SerialBT.read(); // 按字节存储 - // } - memset(receivedBuffer, 0, sizeof(receivedBuffer)); while (SerialBT.available()) // 等待接收完成 { - if (index >= sizeof(receivedBuffer)) // 防止缓冲区溢出 - { - Serial.println("Received data too long."); - break; - } receivedBuffer[index++] = SerialBT.read(); // 按字节存储 } break; @@ -130,33 +130,56 @@ void BTAuthCompleteCallback(boolean success) * 西电 嵌牛实验室 *********************************************************************************************************************/ -// 发送一个字节 -bool XHZS_SendByte(const uint8_t *byte) +// 串口数据接收回调函数 +void XHZS_SeriaPortReceivingData(const uint8_t *buffer, size_t size) { - // 参数校验:确保byte不是空指针。 - if (byte == nullptr) - { - return false; - } - else + if (size > 0) { - SerialBT.write(*byte); - return true; + Serial.write(buffer, size); // 将数据转发至标准串口,串口监视器 + SerialBT.write(buffer, size); // 将数据转发至蓝牙串口,手机端 } } +/************************************************************** + *函数名称:XHZS_ReceiveData + *简 介:读取接收到的蓝牙数据 + *输 入:无 + *输 出:uint8_t + *注意事项:接收数据的缓冲区大小为120字节 + **************************************************************/ +char *XHZS_ReceiveData() +{ + return receivedBuffer; +} + +/* +是否开启串口功能,默认关闭串口功能 +连接的手机端发送数据后,蓝牙端ESP32接收的数据后,触发回调函数 XHZS_SeriaPortReceivingData() +发送一次,触发一次,想修改逻辑需要在回调函数中修改,连续触发不建议使用 +*/ +void XHZS_SerialPortFunctionEnabled(bool Serial_port_status) +{ + SERIAL_PORT_FUNCTION_ENABLE = Serial_port_status; + initializeBluetooth(BlueToothName); +} + +// 发送一个字节 +void XHZS_SendByte(const uint8_t *byte) +{ + SerialBT.write(*byte); +} + // 发送数组 -bool XHZS_SendArray(const uint8_t *array, size_t arraySize) +void XHZS_SendArray(const uint8_t *array, size_t arraySize) { if (array == nullptr || arraySize == 0) { - return false; + return; } else { SerialBT.write(array, arraySize); // 数组发生是个耗时操作,建议DMA优化 - return true; } } @@ -264,18 +287,6 @@ char XHZS_OscGetValue(const char *p, const char *name, float *value) return 2; } -/************************************************************** - *函数名称:XHZS_ReceiveData - *简 介:读取接收到的蓝牙数据 - *输 入:无 - *输 出:char *data type - *注意事项:接收数据的缓冲区大小为120字节 - **************************************************************/ -char *XHZS_ReceiveData() -{ - return receivedBuffer; -} - // /************************************************************** // *函数名称:SendWave // *简 介:发送数据到学会助手APP虚拟示波器 @@ -327,10 +338,10 @@ void test_serial_port_sending_and_receiving() XHZS_SendArray((uint8_t *)receiveData, strlen(receiveData)); // 发送整个字符串:hello // Case one end - // // case two start - // Serial.printf("Computer:%s\r\n", receiveData); // 电脑端打印数据: hello - // SerialBT.printf("mobile phone:%s\r\n", receiveData); // 手机端接收数据:hello - // // case two end + // case two start + // Serial.printf("Computer:%s\r\n", receiveData); // 输出到串口监视器: hello + // SerialBT.printf("mobile phone:%s\r\n", receiveData); // 输出的连接端:hello + // case two end } // 输出指定的数据类型案例 void test_type_of_output() diff --git a/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h b/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h index 02ebd3ad..5aed81fd 100644 --- a/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h +++ b/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h @@ -30,11 +30,14 @@ char *getReceivedBluetoothData(); // 这些接口用于更复杂的蓝牙数据交互场景。 +// 是否开启串口功能,默认关闭串口功能 +void XHZS_SerialPortFunctionEnabled(bool Serial_port_status = false); + // 发送单个字节 -bool XHZS_SendByte(const uint8_t *byte); // 修改参数去指针化,直接传递字节值 +void XHZS_SendByte(const uint8_t *byte); // 发送数据数组 -bool XHZS_SendArray(const uint8_t *array, size_t arraySize); +void XHZS_SendArray(const uint8_t *array, size_t arraySize); // 示例: 向示波器发送波形数据 void SendWave(); @@ -49,7 +52,10 @@ char XHZS_OscGetFloat(const char *p, float *value); char XHZS_OscGetValue(const char *p, const char *name, float *value); // 接收蓝牙数据 -char *XHZS_ReceiveData(); +char *XHZS_ReceiveData(); + +// 串口数据接收回调函数 +void XHZS_SeriaPortReceivingData(const uint8_t *buffer, size_t size); /************************* 实战示例函数 ************************/ @@ -57,6 +63,6 @@ char *XHZS_ReceiveData(); void test_serial_port_sending_and_receiving(); // 学习如何进行基本的串口数据收发 void test_type_of_output(); // 示例展示发送不同数据类型的使用方法 -void test_oscilloscope_input_and_output(); // 特定场景示例:与示波器的交互 +void test_oscilloscope_input_and_output(); // 示波器输入和输出案例 -#endif /* MYBLUETOOTH_H_ */ \ No newline at end of file +#endif /* MYBLUETOOTH_H_ */ diff --git a/platfromIO_ESP32_oscilloscope/src/main.cpp b/platfromIO_ESP32_oscilloscope/src/main.cpp index ef91800f..57344b35 100644 --- a/platfromIO_ESP32_oscilloscope/src/main.cpp +++ b/platfromIO_ESP32_oscilloscope/src/main.cpp @@ -7,17 +7,20 @@ select engineering environment: ESP32_WROVER_Kit(all_versions) #include #include "myBlueTooth.h" -BluetoothSerial SerialBlueTooth; // 实例化蓝牙串口 void setup() { - initializeBluetooth(); // 默认蓝牙名称:mini_bot Serial.begin(115200); // 开启串口通信,频率为:115200 + + initializeBluetooth(); // 默认蓝牙名称:mini_bot + + // XHZS_SerialPortFunctionEnabled(true); // 开启串口功能 } + void loop() { - test_serial_port_sending_and_receiving(); // 串口发送和接收案例 + // test_serial_port_sending_and_receiving(); // 串口发送和接收案例 - // test_type_of_output(); // 输出指定的数据类型案例 + test_type_of_output(); // 输出指定的数据类型案例 // test_oscilloscope_input_and_output();// 示波器输入和输出案例 } -- Gitee