diff --git a/arduino_ESP32_oscilloscope/myBlueTooth.h b/arduino_ESP32_oscilloscope/myBlueTooth.h deleted file mode 100644 index 89af78398bcd6f8522703265e6e190b114fd60e7..0000000000000000000000000000000000000000 --- 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 ce8efc717c8855f1ed75e4f400bcc519584c932e..0000000000000000000000000000000000000000 --- 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 65% rename from arduino_ESP32_oscilloscope/myBlueTooth.cpp rename to arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.cpp index c45099540d55a4d48da3583a3decc1fb088a9242..cbea9628575f599e21a87e52eb6f1246684f6262 100644 --- a/arduino_ESP32_oscilloscope/myBlueTooth.cpp +++ b/arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.cpp @@ -1,28 +1,32 @@ #include "myBlueTooth.h" -/* -1)前半段是对蓝牙的进行封装, -2)后半段是对“学会助手”的ESP32核心板移植操作 - 如果出现 stm32f1 可以使用,在 esp32 中不能使用, - 可以自己进行修改代码,或者向“学会助手”的群里提出来,等待处理 -*/ -BluetoothSerial SerialBT; -static char receivedBuffer[100]; +BluetoothSerial SerialBT; // 蓝牙类实例化 + +static char receivedBuffer[120]; // 接收缓冲区 + +bool SERIAL_PORT_FUNCTION_ENABLE = false; // 串口功能开关 + +String BlueToothName; -void BTConfirmRequestCallback(uint32_t numVal); -void BTAuthCompleteCallback(boolean success); -/*************蓝牙参数配置**********************/ +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之前调用 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; } /*蓝牙发送浮点数据,转为字符串发送*/ @@ -48,7 +52,7 @@ char *getReceivedBluetoothData() return receivedBuffer; } -/******************* 蓝牙事件回调函数 ******************/ +/* 蓝牙事件回调函数 */ void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) // 蓝牙事件回调函数 { uint16_t index = 0; @@ -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; } } @@ -126,34 +130,56 @@ void BTAuthCompleteCallback(boolean success) * 西电 嵌牛实验室 *********************************************************************************************************************/ -// 发送一个字节 -bool XHZS_SendByte(uint8_t *byte) +// 串口数据接收回调函数 +void XHZS_SeriaPortReceivingData(const uint8_t *buffer, size_t size) { - // 参数校验:确保byte不是空指针。 - if (byte == nullptr) + if (size > 0) { - return false; - } - else - { - 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(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 { - // 数组发生是个耗时操作,建议DMA优化 - SerialBT.write(array, arraySize); - return true; + SerialBT.write(array, arraySize); // 数组发生是个耗时操作,建议DMA优化 } } @@ -181,59 +207,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 +287,82 @@ char XHZS_OscGetValue(const char *p, const char *name, float *value) return 2; } -// 蓝牙部分测试 start --------------------- -void test_blueTooth() +// /************************************************************** +// *函数名称:SendWave +// *简 介:发送数据到学会助手APP虚拟示波器 +// *输 入:无 +// *输 出:无 +// *注意事项:将要发送的数据赋值给Wave数组即可,通道依次对应 +// **************************************************************/ +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; + + // XHZS_SendWave(line, sizeof(line)); + // vTaskDelay(50 / portTICK_PERIOD_MS); // 延时 100ms + // } + // // 输出平行线测试 end ----------------------- + + // 正弦波测试 start ----------------------- + float Wave[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 - float floatValue = 3.14; - String str = "hello world"; - // 串口发送数据 - sendFloatViaBluetooth(floatValue); - sendStringViaBluetooth(str.c_str()); - // 串口接收数据,发送到串口, - SerialBT.println(getReceivedBluetoothData()); - // 蓝牙部分测试 end + 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 ----------------------- } -// 学会助手 test1 start -int intValue = 2; -float floatValue = 6.04; -// 测试字符串的变量发送 -void test_sendString() +// 串口输入和输出案例 +void test_serial_port_sending_and_receiving() { - intValue = (intValue > 20 ? 0 : (intValue += 1)); - floatValue = (floatValue > 20 ? -10 : (floatValue += 4)); - - SerialBT.printf("w:%d,%0.2f,6,8\r\n", intValue, floatValue); + 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_sendSin() +// 输出指定的数据类型案例 +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 0000000000000000000000000000000000000000..5aed81fd0ab90d24a93cc70f37af31c7c46359ac --- /dev/null +++ b/arduino_ESP32_oscilloscope/oscilloscope/myBlueTooth.h @@ -0,0 +1,68 @@ +/* + * 文件: 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(); + +/************************* 高级数据处理接口 ************************/ + +// 这些接口用于更复杂的蓝牙数据交互场景。 + +// 是否开启串口功能,默认关闭串口功能 +void XHZS_SerialPortFunctionEnabled(bool Serial_port_status = false); + +// 发送单个字节 +void XHZS_SendByte(const uint8_t *byte); + +// 发送数据数组 +void 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 XHZS_SeriaPortReceivingData(const uint8_t *buffer, size_t size); + +/************************* 实战示例函数 ************************/ + +// 这些函数展示了如何实际应用上述接口,适合学习和参考。 + +void test_serial_port_sending_and_receiving(); // 学习如何进行基本的串口数据收发 +void test_type_of_output(); // 示例展示发送不同数据类型的使用方法 +void test_oscilloscope_input_and_output(); // 示波器输入和输出案例 + +#endif /* MYBLUETOOTH_H_ */ diff --git a/arduino_ESP32_oscilloscope/oscilloscope/oscilloscope.ino b/arduino_ESP32_oscilloscope/oscilloscope/oscilloscope.ino new file mode 100644 index 0000000000000000000000000000000000000000..d1cc71c1a841a3af4ef537b52903880d8a218a62 --- /dev/null +++ b/arduino_ESP32_oscilloscope/oscilloscope/oscilloscope.ino @@ -0,0 +1,26 @@ +/* +select engineering environment: ESP32_WROVER_Kit(all_versions) +想使用“学会助手”功能,查看 myBlueTooth.h 头文件里面有相关说明 +学会助手的串口和示波器功能,可以使用了 ^_^ +*/ + +// #include +#include "myBlueTooth.h" + +void setup() +{ + Serial.begin(115200); // 开启串口通信,频率为:115200 + + initializeBluetooth(); // 默认蓝牙名称:mini_bot + + // XHZS_SerialPortFunctionEnabled(true); // 开启串口功能 +} + +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 0000000000000000000000000000000000000000..f0d7683e81eb2606331807a49e4377b68726a0d4 --- /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 65872d3f4206804ac3d0ddd55105eb5b20987f7a..cbea9628575f599e21a87e52eb6f1246684f6262 100644 --- a/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -1,28 +1,32 @@ #include "myBlueTooth.h" -/* -1)前半段是对蓝牙的进行封装, -2)后半段是对“学会助手”的ESP32核心板移植操作 - 如果出现 stm32f1 可以使用,在 esp32 中不能使用, - 可以自己进行修改代码,或者向“学会助手”的群里提出来,等待处理 -*/ -BluetoothSerial SerialBT; -static char receivedBuffer[120]; +BluetoothSerial SerialBT; // 蓝牙类实例化 + +static char receivedBuffer[120]; // 接收缓冲区 + +bool SERIAL_PORT_FUNCTION_ENABLE = false; // 串口功能开关 -void BTConfirmRequestCallback(uint32_t numVal); -void BTAuthCompleteCallback(boolean success); -/*************蓝牙参数配置**********************/ +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); // 蓝牙事件回调函数 -/*蓝牙参数配置*/ -// 初始化蓝牙配置 +/* 初始化蓝牙配置 */ 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; } /*蓝牙发送浮点数据,转为字符串发送*/ @@ -48,7 +52,7 @@ char *getReceivedBluetoothData() return receivedBuffer; } -/******************* 蓝牙事件回调函数 ******************/ +/* 蓝牙事件回调函数 */ void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) // 蓝牙事件回调函数 { uint16_t index = 0; @@ -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; } } @@ -126,34 +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 { - // 数组发生是个耗时操作,建议DMA优化 - SerialBT.write(array, arraySize); - return true; + SerialBT.write(array, arraySize); // 数组发生是个耗时操作,建议DMA优化 } } @@ -260,17 +286,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 @@ -281,34 +296,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,14 +333,14 @@ 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 + // Serial.printf("Computer:%s\r\n", receiveData); // 输出到串口监视器: hello + // SerialBT.printf("mobile phone:%s\r\n", receiveData); // 输出的连接端:hello // case two end } // 输出指定的数据类型案例 diff --git a/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h b/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h index 91fdabdbb589a643bac4f20b693bcf9e4017b6fb..5aed81fd0ab90d24a93cc70f37af31c7c46359ac 100644 --- a/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h +++ b/platfromIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h @@ -1,65 +1,68 @@ /* - * 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); +// 是否开启串口功能,默认关闭串口功能 +void XHZS_SerialPortFunctionEnabled(bool Serial_port_status = false); -// 发送数组数据 -bool XHZS_SendArray(const uint8_t *array, size_t arraySize); +// 发送单个字节 +void XHZS_SendByte(const uint8_t *byte); -// 此函数用于向示波器发送波形数据 -// 它作为一个模板,演示了如何正确地输出波形 -// 用户应根据所需的具体波形类型调整函数内部的逻辑 +// 发送数据数组 +void 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(); // 串口输入和输出案例 +// 串口数据接收回调函数 +void XHZS_SeriaPortReceivingData(const uint8_t *buffer, size_t size); -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_ */ diff --git a/platfromIO_ESP32_oscilloscope/src/main.cpp b/platfromIO_ESP32_oscilloscope/src/main.cpp index 3fe13c7cf82c4501e84673d409dd23374017a931..57344b35bc333fff590a784b4b6b0bbdce86c20c 100644 --- a/platfromIO_ESP32_oscilloscope/src/main.cpp +++ b/platfromIO_ESP32_oscilloscope/src/main.cpp @@ -1,24 +1,26 @@ -// -// select engineering environment: ESP32_WROVER_Kit(all_versions) -// 想使用“学会助手”功能,查看 myBlueTooth.h 头文件里面有相关说明 -// 学会助手的串口和示波器,可以使用 ^_^ -// +/* +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 + + initializeBluetooth(); // 默认蓝牙名称:mini_bot + + // XHZS_SerialPortFunctionEnabled(true); // 开启串口功能 } + void loop() { // test_serial_port_sending_and_receiving(); // 串口发送和接收案例 - test_type_of_output(); + test_type_of_output(); // 输出指定的数据类型案例 - // test_oscilloscope_input_and_output(); + // test_oscilloscope_input_and_output();// 示波器输入和输出案例 } - -