From c7ef237e7e759f86369e5cae52b7afc1ae3f6b48 Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Thu, 1 Aug 2024 18:16:40 +0800 Subject: [PATCH 01/18] =?UTF-8?q?=E7=BB=99=20main.cpp=20=E5=87=86=E5=A4=87?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=B7=A5=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/myBlueTooth/myBlueTooth.cpp | 7 ++++--- PlatformIO_ESP32_oscilloscope/src/main.cpp | 10 +++------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp index 9c1e5161..b43b5894 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -604,7 +604,7 @@ void XHZS_RemoteControlInterruptMode() /************************* XHZS end *************************/ // -/************************* example funtion begin *************************/ +/************************* example begin *************************/ // 开启串口回调函数,需要在蓝牙初始化之前调用 // isEnable = true 开启,false 关闭 // 串口(蓝牙)接收到数据触发回调函数 Uart_onDataReceive() @@ -626,8 +626,9 @@ void test_oscilloscope_send_wave() void test_oscilloscope_input_and_output() { float floatValue1, floatValue2, floatValue3, floatValue4; // 准备数据类型 - char *charData = Uart_getReceiveData(); + char *charData = Uart_getReceiveData(); // 获取蓝牙串口的数据 + // 给通道四输出波形数据 floatValue4 = (floatValue4 > 33 ? -22 : (floatValue4 += 0.1)); // 将字符串中的字段的数值提取出来 // c11:14.12,c12:22,c13:33,c14: 55.66, @@ -640,7 +641,7 @@ void test_oscilloscope_input_and_output() SerialBT.printf("w:%0.2f,%0.2f,%0.2f,%0.2f\r\n", floatValue1, floatValue2, floatValue3, floatValue4); vTaskDelay(50 / portTICK_PERIOD_MS); // 延时函数,可以注释掉 } -/************************* example funtion end *************************/ +/************************* example end *************************/ // /************************* test begin *************************/ struct myStruct diff --git a/PlatformIO_ESP32_oscilloscope/src/main.cpp b/PlatformIO_ESP32_oscilloscope/src/main.cpp index 0cc68d8f..bbc73ec6 100644 --- a/PlatformIO_ESP32_oscilloscope/src/main.cpp +++ b/PlatformIO_ESP32_oscilloscope/src/main.cpp @@ -22,16 +22,12 @@ void setup() void loop() { - // ... + // 使用示波器,不用启用串口中断模式,否则会冲突 + // test_oscilloscope_input_and_output(); - delay(100); + delay(10); } - - - - - /************************* test code area begin *************************/ // char *data = Uart_ReceiveData(); -- Gitee From f7aa55b1c6041371976082182f7d28c78f463237 Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Fri, 2 Aug 2024 18:35:15 +0800 Subject: [PATCH 02/18] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=81=A5=E6=8E=A7=E5=99=A8=E7=9A=84=E8=BF=94=E5=9B=9E=E5=80=BC?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E6=8A=8A=E6=95=B0=E5=80=BC=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E7=BB=93=E6=9E=84=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/myBlueTooth/myBlueTooth.cpp | 350 +++++++++--------- .../lib/myBlueTooth/myBlueTooth.h | 75 ++-- PlatformIO_ESP32_oscilloscope/src/main.cpp | 32 +- 3 files changed, 256 insertions(+), 201 deletions(-) diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp index b43b5894..d88d74b8 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -2,14 +2,15 @@ /************************* blueTooth part *************************/ BluetoothSerial SerialBT; // 蓝牙类实例化 -String BlueToothName = ""; // 蓝牙名称 static char blueToothBuffer[100]; // 接收缓冲区 /************************* XHZS part *************************/ -bool SERIAL_PORT_CALLBACK_FUNCTION_ENABLE = false; // 默认串口关闭 -char RemoteControlDataLength = 0; // 数据长度 -uint8_t DataFormat[8] = {0}; // 遥控器数据格式 +bool SERIAL_PORT_CALLBACK_ENABLE = false; // 默认串口关闭 +bool REMOTE_CONTROL_CALLBACK_ENABLE = false; // 默认遥控器关闭 +// 定义常量替代硬编码 +constexpr char DIGIT_ZERO = 0x30; +constexpr char DIGIT_ONE = 0x31; /************************* blueTooth begin *************************/ @@ -26,13 +27,16 @@ void BTinitialize(const String &deviceName) SerialBT.register_callback(Bluetooth_Event); // 设置事件回调函数 连接 断开 发送 接收 - if (SERIAL_PORT_CALLBACK_FUNCTION_ENABLE == true) + if (SERIAL_PORT_CALLBACK_ENABLE == true) { SerialBT.onData(Uart_onDataReceive); // 测试手柄功能未完成_2024-07-27 - // SerialBT.onData(BTcallbackFunction); + } + else if (REMOTE_CONTROL_CALLBACK_ENABLE == true) + { + SerialBT.onData(RemoteControlOnDataReceive); } SerialBT.begin(deviceName); // Bluetooth device name - BlueToothName = deviceName; + // BlueToothName = deviceName; } /*蓝牙发送浮点数据,转为字符串发送*/ @@ -122,9 +126,16 @@ void BTAuthCompleteCallback(boolean success) // /************************* XHZS begin *************************/ +void getInterruptJoystickvalue_XY(char *receiveBuffer, short *joystick_X, short *joystick_Y); +void RemoteControlInterruptMode(uint8_t *cmd, uint8_t *data, RemoteControlJoystick *Joystick); +void getJoystickValue(short *outputButton, char outputPosition, char *inputButton, char inputPosition); +void getDirectionalPad(short *outputButton, char outputPosition, char *inputButton, char inputPosition); +void getFunctionButtonValue(short *outputButton, char outputPosition, char *inputButton, char inputPosition); + // 当通过UART接收到数据时,此函数将被调用 void Uart_onDataReceive(const uint8_t *dataBuffer, size_t dataSize) { + // 判断接收到的数量 if (dataSize > 0) { SerialBT.write(dataBuffer, dataSize); // 输出到连接的设备 @@ -200,7 +211,7 @@ void SendWave() // line[3] = 0.0; // XHZS_SendWave(line, sizeof(line)); - // vTaskDelay(50 / portTICK_PERIOD_MS); // 延时 100ms + // vTaskDelay(50 / portTICK_PERIOD_MS); // 延时函数可以注释掉 // } // // 输出平行线测试 end ----------------------- @@ -215,7 +226,7 @@ void SendWave() Wave[3] = -cos(i); XHZS_SendWave(Wave, sizeof(Wave)); - vTaskDelay(50 / portTICK_PERIOD_MS); + vTaskDelay(50 / portTICK_PERIOD_MS); // 延时函数可以注释掉 } // 正弦波测试 end ----------------------- } @@ -300,8 +311,6 @@ char OscGetValue(const char *p, const char *name, float *value) return 2; } -/************************* XHZS end *************************/ - // 获取摇杆数据转换为 x and y void getInterruptJoystickvalue_XY(char *receiveBuffer, short *joystick_X, short *joystick_Y) { @@ -318,63 +327,27 @@ void getInterruptJoystickvalue_XY(char *receiveBuffer, short *joystick_X, short // cmd4 = receiveBuffer[3] & 0xF0; data[3] = receiveBuffer[3] & 0x0F; - // 对摇杆数值默认值做了修改,中心数值是0,偏离中心越远数值越大 0~100 + // 对摇杆数值默认值做了修改,中心数值是0,偏离中心越远数值越大 100~0~100 x = (data[0] * 16 + data[1]) - 100; *joystick_X = (x > 0) ? x : -x; y = (data[2] * 16 + data[3]) - 100; *joystick_Y = (y > 0) ? y : -y; } -// /************************************************************** -// *函数名称:test_remote_control -// *简 介:蓝牙数据接收,回调函数 -// *输 入:无 -// *输 出:默认输出按键状态 -// *注意事项:1)蓝牙接收到数据后,会调用此函数,不要在循环中使用,有卡死程序风险 -// 2)回调函数不能有耗时操作,延时操作禁止使用的,会触发看门狗,不断重启 -// 3)打印函数测试使用,注释掉写你的逻辑代码 -// **************************************************************/ - -void test_remote_control(const uint8_t *buffer, size_t size) +// 遥控器中断模式 +void RemoteControlInterruptMode(uint8_t *cmd, uint8_t *data, RemoteControlJoystick *Joystick) { - uint8_t cmd[4] = {0}; - uint8_t data[4] = {0}; - - // 摇杆相关变量 - short left_X = 0, left_Y = 0, right_X = 0, right_Y = 0; - char receiveBuffer[4] = {0}; // 中断接收数据 - char temp = 0; - memcpy(receiveBuffer, buffer, size); - - // 按键接收大小为1,摇杆接收大小为4 - if (size == 4) - { - temp = (receiveBuffer[0] & 0xF0); - // cmd[0] = (temp == 0x10) ? 0x9A : ((temp == 0x50) ? 0X9B : 0x00); - // data[0] = receiveBuffer[0] & 0x0F; - if (temp == 0x10) - { - cmd[0] = 0x9A; - getInterruptJoystickvalue_XY(receiveBuffer, &left_X, &left_Y); - } - else if (temp == 0x50) - { - cmd[0] = 0x9C; - getInterruptJoystickvalue_XY(receiveBuffer, &right_X, &right_Y); - } - } - else if (size == 1) - { - cmd[0] = (receiveBuffer[0] & 0xF0); - data[0] = (receiveBuffer[0] & 0x0F); - } - + // // 遥控器中断指令枚举定义 enum INTERRUPT_MODE_ORDER switch (cmd[0]) { case INTERRUPT_LEFT_UP: // 指令 cmd[0] 数据 data[0] + // 逻辑代码区 begin + SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); + + // 逻辑代码区 end break; case INTERRUPT_LEFT_DOWN: @@ -407,17 +380,17 @@ void test_remote_control(const uint8_t *buffer, size_t size) case INTERRUPT_LEFT_X_Y: // 摇杆数值在 left_X and left_Y - // 逻辑代码区 start + // 逻辑代码区 begin - SerialBT.printf("x: %d y: %d\n", left_X, left_Y); + SerialBT.printf("x: %d y: %d\n", Joystick->left_X, Joystick->left_Y); // 逻辑代码区 end break; case INTERRUPT_RIGHT_X_Y: - // 逻辑代码区 start + // 逻辑代码区 begin - SerialBT.printf("x: %d y: %d\n", right_X, right_Y); + SerialBT.printf("x: %d y: %d\n", Joystick->right_X, Joystick->right_Y); // 逻辑代码区 end break; @@ -447,173 +420,180 @@ void test_remote_control(const uint8_t *buffer, size_t size) break; } } + /* - * 遥控器的界面选择,使用 enum INTERFACE_SIZE - */ -void XHZS_RemoteControlIntialization(INTERFACE_SIZE isDirectionalPad1, - INTERFACE_SIZE isDirectionalPad2, - INTERFACE_SIZE isButtonA, - INTERFACE_SIZE isButtonB, - INTERFACE_SIZE isButtonC, - INTERFACE_SIZE isButtonD, - INTERFACE_SIZE isSwitchE, - INTERFACE_SIZE isSwitchF) +1)蓝牙接收到数据后,会调用此函数,不要在循环中使用,有卡死程序风险 +2)回调函数不能有耗时操作,延时操作禁止使用的,会触发看门狗,不断重启 +*/ +void RemoteControlOnDataReceive(const uint8_t *buffer, size_t size) { - RemoteControlDataLength = isDirectionalPad1 + - isDirectionalPad2 + - isButtonA + - isButtonB + - isButtonC + - isButtonD + - isSwitchE + - isSwitchF; - DataFormat[0] = isDirectionalPad1; - DataFormat[1] = isDirectionalPad2; - DataFormat[2] = isButtonA; - DataFormat[3] = isButtonB; - DataFormat[4] = isButtonC; - DataFormat[5] = isButtonD; - DataFormat[6] = isSwitchE; - DataFormat[7] = isSwitchF; + struct RemoteControlJoystick Joystick = {0}; + uint8_t cmd[4] = {0}; + uint8_t data[4] = {0}; + + char receiveBuffer[4] = {0}; + char temp = 0; + memcpy(receiveBuffer, buffer, size); + + // 判断是否摇杆数据,在进行解释 + if (size == 4) + { + if ((receiveBuffer[0] & 0xF0) == 0x10) + { + cmd[0] = 0x9A; + getInterruptJoystickvalue_XY(receiveBuffer, &Joystick.left_X, &Joystick.left_Y); + } + else if ((receiveBuffer[0] & 0xF0) == 0x50) + { + cmd[0] = 0x9C; + getInterruptJoystickvalue_XY(receiveBuffer, &Joystick.right_X, &Joystick.right_Y); + } + } + else if (size == 1) + { + cmd[0] = (receiveBuffer[0] & 0xF0); + data[0] = (receiveBuffer[0] & 0x0F); + } + RemoteControlInterruptMode(cmd, data, &Joystick); } // 获取摇杆数值 // (inputButton[inputPosition + 0] - 0x30) 把字符转化为数字 -void getJoystickValue(short *outputButton, char outputPosition, char *inputButton, char inputPosition) +void getJoystickValue(short *outputButton, char outputPosition, const char *inputButton, char inputPosition) { - outputButton[outputPosition + 0] = (inputButton[inputPosition + 0] - 0x30) * 100 + - (inputButton[inputPosition + 1] - 0x30) * 10 + - (inputButton[inputPosition + 2] - 0x30) - 100; - outputButton[outputPosition + 0] = ((outputButton[outputPosition + 0]) > 0 ? (outputButton[outputPosition + 0]) : -(outputButton[outputPosition + 0])); - - outputButton[outputPosition + 1] = (inputButton[inputPosition + 3] - 0x30) * 100 + - (inputButton[inputPosition + 4] - 0x30) * 10 + - (inputButton[inputPosition + 5] - 0x30) - 100; - outputButton[outputPosition + 1] = ((outputButton[outputPosition + 1]) > 0 ? (outputButton[outputPosition + 1]) : -(outputButton[outputPosition + 1])); + outputButton[outputPosition] = (inputButton[inputPosition] - DIGIT_ZERO) * 100 + + (inputButton[inputPosition + 1] - DIGIT_ZERO) * 10 + + (inputButton[inputPosition + 2] - DIGIT_ZERO) - 100; + outputButton[outputPosition] = outputButton[outputPosition] > 0 ? outputButton[outputPosition] : -outputButton[outputPosition]; + + outputButton[outputPosition + 1] = (inputButton[inputPosition + 3] - DIGIT_ZERO) * 100 + + (inputButton[inputPosition + 4] - DIGIT_ZERO) * 10 + + (inputButton[inputPosition + 5] - DIGIT_ZERO) - 100; + outputButton[outputPosition + 1] = outputButton[outputPosition + 1] > 0 ? outputButton[outputPosition + 1] : -outputButton[outputPosition + 1]; } // 获取按钮数值 // (inputButton[inputPosition + i] == 0x31) 识别字符是否等于数字的1 -void getDirectionalPad(short *outputButton, char outputPosition, char *inputButton, char inputPosition) +void getDirectionalPad(short *outputButton, char outputPosition, const char *inputButton, char inputPosition) { for (size_t i = 0; i < DIRECTIONAL_PAD_SIZE; i++) { - if (inputButton[inputPosition + i] == 0x31) + if (inputButton[inputPosition + i] == DIGIT_ONE) { outputButton[outputPosition + i] = 1; } } } - // 获取按钮数值 // (inputButton[inputPosition + i] == 0x31) 识别字符是否等于数字的1 -void getFunctionButtonValue(short *outputButton, char outputPosition, char *inputButton, char inputPosition) +void getFunctionButtonValue(short *outputButton, char outputPosition, const char *inputButton, char inputPosition) { for (size_t i = 0; i < FUNCTION_BUTTON_SIZE; i++) { - if (inputButton[inputPosition + i] == 0x31) + if (inputButton[inputPosition + i] == DIGIT_ONE) { outputButton[outputPosition + i] = 1; } } } - -// 遥控器循环模式 -// 数组 outputBuffer[20] 数据格式在枚举 enum LOOP_MODE_ORDER 中定义 -// 返回是数组类型,元素必须大于20个 -short XHZS_RemoteControlLoopMode() +// 专门给 RemoteControlLoopMode 函数打印和测试使用 +void RemoteControlLoopModePritnf(char handle, RemoteControlOutput *outputBuffer) { - short outputBuffer[20] = {0}; // 输出的缓冲区 - char tempBuffer[20] = {0}; // 临时缓冲区 - // 判断手柄的方向按钮和摇杆的组合 - char handle = (DataFormat[0] == JOYSTICK_SIZE ? JOYSTICK_AND_DIRECTIONAL_PAD : TWO_DIRECTIONAL_PAD) + - (DataFormat[1] == JOYSTICK_SIZE ? DIRECTIONAL_PAD_AND_JOYSTICK : TWO_DIRECTIONAL_PAD); - - size_t data_len = strlen(Uart_getReceiveData()); // 获取长度用于复制 - memcpy(tempBuffer, Uart_getReceiveData(), data_len); // 内存复制 - - // 确定,方向按钮和摇杆组合方式 + // 不同的遥控器组合,使用不同显示 // 打印在串口监视器中 switch (handle) { - case TWO_DIRECTIONAL_PAD: - getDirectionalPad(outputBuffer, 0, tempBuffer, 0); - getFunctionButtonValue(outputBuffer, 8, tempBuffer, 8); + case DIRECTIONAL_PAD: + Serial.printf("up: %d down: %d left: %d right: %d - up: %d down: %d left: %d right: %d ", + outputBuffer->LEFT_up, outputBuffer->LEFT_down, outputBuffer->LEFT_left, outputBuffer->LEFT_right, + outputBuffer->RIGHT_up, outputBuffer->RIGHT_down, outputBuffer->RIGHT_left, outputBuffer->RIGHT_right); break; case DIRECTIONAL_PAD_AND_JOYSTICK: - getDirectionalPad(outputBuffer, 0, tempBuffer, 0); - getJoystickValue(outputBuffer, 16, tempBuffer, 4); - getFunctionButtonValue(outputBuffer, 8, tempBuffer, 10); + Serial.printf("up: %d down: %d left: %d right: %d - x: %d y: %d ", + outputBuffer->LEFT_up, outputBuffer->LEFT_down, outputBuffer->LEFT_left, outputBuffer->LEFT_right, + outputBuffer->RIGHT_x, outputBuffer->RIGHT_y); break; case JOYSTICK_AND_DIRECTIONAL_PAD: - getJoystickValue(outputBuffer, 14, tempBuffer, 0); - getDirectionalPad(outputBuffer, 4, tempBuffer, 6); - getFunctionButtonValue(outputBuffer, 8, tempBuffer, 10); + Serial.printf(" x: %d y: %d - up: %d down: %d left: %d right: %d ", + outputBuffer->LEFT_x, outputBuffer->LEFT_y, + outputBuffer->LEFT_up, outputBuffer->LEFT_down, outputBuffer->LEFT_left, outputBuffer->LEFT_right); break; - case TWO_JOYSTICKS: - getJoystickValue(outputBuffer, 14, tempBuffer, 0); - getJoystickValue(outputBuffer, 16, tempBuffer, 6); - getFunctionButtonValue(outputBuffer, 8, tempBuffer, 12); + case JOYSTICKS: + Serial.printf(" x: %d y: %d - x: %d y: %d ", + outputBuffer->LEFT_x, outputBuffer->LEFT_y, outputBuffer->RIGHT_x, outputBuffer->RIGHT_y); + break; } - return *outputBuffer; - // #if true - // 不同的遥控器组合,使用不同显示 // 打印在串口监视器中 + Serial.printf(" A: %d B: %d C: %d D: %d E: %d F: %d \n", + outputBuffer->A, outputBuffer->B, outputBuffer->C, outputBuffer->D, outputBuffer->E, outputBuffer->F); +} + +// 遥控器循环模式 +RemoteControlOutput RemoteControlLoopMode1(MODULE_SIZE RemoteControlLayoutLeft, MODULE_SIZE RemoteControlLayoutRight) +{ + RemoteControlOutput outputBuffer = {}; // 输出的缓冲区 + char tempBuffer[20] = {}; // 临时缓冲区 + // 判断手柄的方向按钮和摇杆的组合 + char handle = (RemoteControlLayoutLeft == JOYSTICK_SIZE ? JOYSTICK_AND_DIRECTIONAL_PAD : DIRECTIONAL_PAD) + + (RemoteControlLayoutRight == JOYSTICK_SIZE ? DIRECTIONAL_PAD_AND_JOYSTICK : DIRECTIONAL_PAD); + + size_t data_len = strlen(Uart_getReceiveData()); // 获取长度用于复制 + memcpy(tempBuffer, Uart_getReceiveData(), data_len); // 内存复制 + + // 确定,方向按钮和摇杆组合方式 switch (handle) { - case TWO_DIRECTIONAL_PAD: - Serial.printf("up: %d down: %d left: %d right: %d - up: %d down: %d left: %d right: %d ", - outputBuffer[LOOP_LEFT_UP], outputBuffer[LOOP_LEFT_DOWN], outputBuffer[LOOP_LEFT_LEFT], outputBuffer[LOOP_LEFT_RIGHT], - outputBuffer[LOOP_RIGHT_UP], outputBuffer[LOOP_RIGHT_DOWN], outputBuffer[LOOP_RIGHT_LEFT], outputBuffer[LOOP_RIGHT_RIGHT]); - + case DIRECTIONAL_PAD: + // 获取结构体的首地址,在基础上移动到指定位置,进行赋值或者读取 + getDirectionalPad(&outputBuffer.LEFT_up, 0, tempBuffer, 0); + getDirectionalPad(&outputBuffer.LEFT_up, 4, tempBuffer, 4); + getFunctionButtonValue(&outputBuffer.LEFT_up, 8, tempBuffer, 8); break; + case DIRECTIONAL_PAD_AND_JOYSTICK: - Serial.printf("up: %d down: %d left: %d right: %d - x: %d y: %d ", - outputBuffer[LOOP_LEFT_UP], outputBuffer[LOOP_LEFT_DOWN], outputBuffer[LOOP_LEFT_LEFT], outputBuffer[LOOP_LEFT_RIGHT], - outputBuffer[LOOP_RIGHT_X], outputBuffer[LOOP_RIGHT_Y]); + getDirectionalPad(&outputBuffer.LEFT_up, 0, tempBuffer, 0); + getJoystickValue(&outputBuffer.LEFT_up, 16, tempBuffer, 4); + getFunctionButtonValue(&outputBuffer.LEFT_up, 8, tempBuffer, 10); break; + case JOYSTICK_AND_DIRECTIONAL_PAD: - Serial.printf(" x: %d y: %d - up: %d down: %d left: %d right: %d ", - outputBuffer[LOOP_LEFT_X], outputBuffer[LOOP_LEFT_Y], - outputBuffer[LOOP_RIGHT_UP], outputBuffer[LOOP_RIGHT_DOWN], outputBuffer[LOOP_RIGHT_LEFT], outputBuffer[LOOP_RIGHT_RIGHT]); + getJoystickValue(&outputBuffer.LEFT_up, 14, tempBuffer, 0); + getDirectionalPad(&outputBuffer.LEFT_up, 4, tempBuffer, 6); + getFunctionButtonValue(&outputBuffer.LEFT_up, 8, tempBuffer, 10); break; - case TWO_JOYSTICKS: - Serial.printf("x: %d y: %d - x: %d y: %d ", - outputBuffer[LOOP_LEFT_X], outputBuffer[LOOP_LEFT_Y], - outputBuffer[LOOP_RIGHT_X], outputBuffer[LOOP_RIGHT_Y]); + + case JOYSTICKS: + getJoystickValue(&outputBuffer.LEFT_up, 14, tempBuffer, 0); + getJoystickValue(&outputBuffer.LEFT_up, 16, tempBuffer, 6); + getFunctionButtonValue(&outputBuffer.LEFT_up, 8, tempBuffer, 12); break; } - Serial.printf(" A: %d B: %d C: %d D: %d E: %d F: %d \n", - outputBuffer[LOOP_BUTTON_A], - outputBuffer[LOOP_BUTTON_B], - outputBuffer[LOOP_BUTTON_C], - outputBuffer[LOOP_BUTTON_D], - outputBuffer[LOOP_BUTTON_E], - outputBuffer[LOOP_BUTTON_F]); - // #endif -} - -// 遥控器中断模式 -void XHZS_RemoteControlInterruptMode() -{ - // 准备整合 + RemoteControlLoopModePritnf(handle, &outputBuffer); + return outputBuffer; } /************************* XHZS end *************************/ // /************************* example begin *************************/ -// 开启串口回调函数,需要在蓝牙初始化之前调用 // isEnable = true 开启,false 关闭 -// 串口(蓝牙)接收到数据触发回调函数 Uart_onDataReceive() -// 底层 blueTooth 库中 onData() 函数绑定回调函数 Uart_onDataReceive() 函数作为触发调用 -// 找到 Uart_onDataReceive() 在里面写你的逻辑代码,不要有延时操作,中断函数中不能有延时操作 -// 在不使用串口功能时,请关闭防止与其它功能模块的冲突 -void test_serial_port_callback_function(bool isEnable) +/** + * @brief 启用或禁用串口回调机制 + * + * 此函数应在蓝牙初始化前调用,以配置串口(蓝牙)接收数据时的行为。 + * 当 isEnable 设置为 true 时,开启串口回调;设置为 false 时,则禁用。 + * + * 在底层蓝牙库中,onData() 函数将绑定 Uart_onDataReceive() 作为其回调,从而在接收到数据时触发相应的处理逻辑。 + * + * 注意:Uart_onDataReceive() 函数中不应包含任何延时操作,因其可能在中断上下文中执行。 + * + * 在不使用串口功能时,务必将其关闭,以避免与其他功能模块产生潜在冲突。 + */ +void test_serial_port_callback_enable(bool isEnable) { - SERIAL_PORT_CALLBACK_FUNCTION_ENABLE = isEnable; + SERIAL_PORT_CALLBACK_ENABLE = isEnable; } // 发送波形数据的函数例子 @@ -622,15 +602,19 @@ void test_oscilloscope_send_wave() SendWave(); } -// 示波器输入和输出案例 -void test_oscilloscope_input_and_output() + +/** + * @brief 示波器输入和输出案例 + * + */ +void test_oscilloscope_send_and_Receive() { float floatValue1, floatValue2, floatValue3, floatValue4; // 准备数据类型 char *charData = Uart_getReceiveData(); // 获取蓝牙串口的数据 // 给通道四输出波形数据 floatValue4 = (floatValue4 > 33 ? -22 : (floatValue4 += 0.1)); - // 将字符串中的字段的数值提取出来 + // 将字符串中的字段的数值提取出来,字符串格式为:下方 // c11:14.12,c12:22,c13:33,c14: 55.66, // c13字段,数值为33 OscGetValue(charData, "c11", &floatValue1); @@ -641,9 +625,32 @@ void test_oscilloscope_input_and_output() SerialBT.printf("w:%0.2f,%0.2f,%0.2f,%0.2f\r\n", floatValue1, floatValue2, floatValue3, floatValue4); vTaskDelay(50 / portTICK_PERIOD_MS); // 延时函数,可以注释掉 } + +/** + * @brief 启用或禁用,遥控器的回调函数,非定时模式有用 + * + * 此函数应在蓝牙初始化前调用,以配置串口(蓝牙)接收数据时的行为。 + * 当 isEnable 设置为 true 时,开启串口回调;设置为 false 时,则禁用。 + */ +void test_remote_control_callback_enable(bool isEnable) +{ + REMOTE_CONTROL_CALLBACK_ENABLE = isEnable; +} + +/** + * @brief 遥控器的定时发送模式 + * + * + */ +void test_remote_control_loop_mode() +{ + // short shortArray[18] = {RemoteControlLoopMode(DIRECTIONAL_PAD_SIZE, JOYSTICK_SIZE)}; +} + /************************* example end *************************/ // /************************* test begin *************************/ + struct myStruct { char a; @@ -653,6 +660,9 @@ struct myStruct char e; }; +/* +如何把结构体像数组一样使用? +*/ void test_struct_array() { myStruct box = {}; diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h index 5f8096a2..4bbdbdc2 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h @@ -12,6 +12,31 @@ #include // 字符串操作库 #include // ESP32蓝牙库 +struct RemoteControlOutput +{ + short LEFT_up; // 0 + short LEFT_down; + short LEFT_left; + short LEFT_right; + + short RIGHT_up; + short RIGHT_down; + short RIGHT_left; + short RIGHT_right; // 7 + + short A; // 8 + short B; + short C; + short D; + short E; + short F; // 13 + + short LEFT_x; // 14 + short LEFT_y; + short RIGHT_x; + short RIGHT_y; // 17 +}; + // 遥控器循环指令枚举定义 enum LOOP_MODE_ORDER { @@ -74,22 +99,29 @@ typedef enum MODULE_SIZE JOYSTICK_SIZE = 6, // 手柄摇杆,占位6 DIRECTIONAL_PAD_SIZE = 4, // 方向按钮,占位4 FUNCTION_BUTTON_SIZE = 6, // 功能按键,占位6 - BUTTON_SIZE = 1, // 按键,占位1 - SWITCH_SIZE = 1 // 开关,占位1 + // BUTTON_SIZE = 1, // 按键,占位1 + // SWITCH_SIZE = 1 // 开关,占位1 } INTERFACE_SIZE; // 遥控器的方向按钮和摇杆组合 enum POLYTYPE { - TWO_DIRECTIONAL_PAD = 0x00, // 0x00 + DIRECTIONAL_PAD = 0x00, // 0x00 DIRECTIONAL_PAD_AND_JOYSTICK = 0x01, // 0x01 JOYSTICK_AND_DIRECTIONAL_PAD = 0x10, // 0x10 - TWO_JOYSTICKS = 0x11 // 0x11 + JOYSTICKS = 0x11 // 0x11 +}; + +struct RemoteControlJoystick +{ + short left_X; + short left_Y; + short right_X; + short right_Y; }; -extern bool SERIAL_PORT_CALLBACK_FUNCTION_ENABLE; -/************************* 蓝牙功能核心接口 *************************/ +/************************* 蓝牙功能接口 *************************/ // 设定蓝牙名称,默认为"mini_bot" void BTinitialize(const String &deviceName = "mini_bot"); @@ -103,7 +135,7 @@ void BTsendStringVia(const char *stringData); // 返回一个指针指向接收到的字符串数据。 char *BTgetReceivedData(); -/************************* 高级数据处理接口 ************************/ +/************************* 学会助手接口 ************************/ // 当通过UART接收到数据时,此函数将被调用 void Uart_onDataReceive(const uint8_t *dataBuffer, size_t dataSize); @@ -129,32 +161,27 @@ char OscGetFloat(const char *p, float *value); // 从示波器获取的数据格式,按字段解析为数值 char OscGetValue(const char *p, const char *name, float *value); -/************************* 演示示例函数 ************************/ +void RemoteControlOnDataReceive(const uint8_t *buffer, size_t size); -void test_serial_port_callback_function(bool isEnable = false); // 开启串口回调函数,需要在蓝牙初始化之前调用 +void RemoteControlInterruptMode(uint8_t *cmd, uint8_t *data, RemoteControlJoystick *Joystick); + +/************************* 演示函数 ************************/ + +void test_serial_port_callback_enable(bool isEnable = false); // 开启串口回调函数,在蓝牙初始化之前调用 void test_oscilloscope_send_wave(); // 发送波形数据的函数 -void test_oscilloscope_input_and_output(); // 示波器输入和输出案例 +void test_oscilloscope_send_and_Receive(); // 示波器输入和输出案例 -/************************* 其它模块适配,适配中... ************************/ +void test_remote_control_callback_enable(bool isEnable=false); // 遥控器回调函数,在蓝牙初始化之前调用 -void test_remote_control(const uint8_t *buffer, size_t size); // 遥控器 +/************************* 其它模块适配,适配中... ************************/ -void XHZS_RemoteControlIntialization(INTERFACE_SIZE isDirectionalPad1, - INTERFACE_SIZE isDirectionalPad2, - INTERFACE_SIZE isButtonA = BUTTON_SIZE, - INTERFACE_SIZE isButtonB = BUTTON_SIZE, - INTERFACE_SIZE isButtonC = BUTTON_SIZE, - INTERFACE_SIZE isButtonD = BUTTON_SIZE, - INTERFACE_SIZE isSwitchE = SWITCH_SIZE, - INTERFACE_SIZE isSwitchF = SWITCH_SIZE); +RemoteControlOutput RemoteControlLoopMode1(MODULE_SIZE RemoteControlLayoutLeft, MODULE_SIZE RemoteControlLayoutRight); -void XHZS_RemoteControlIntialization(); // 遥控器初始化 -// void XHZS_RemoteControlLoopMode(); // 遥控器循环模式 -void XHZS_RemoteControlInterruptMode(); // 遥控器中断模式 +void test_remote_control(const uint8_t *buffer, size_t size); // 遥控器 -short XHZS_RemoteControlLoopMode(); +void RemoteControlInterruptMode(); // 遥控器中断模式 void test_struct_array(); diff --git a/PlatformIO_ESP32_oscilloscope/src/main.cpp b/PlatformIO_ESP32_oscilloscope/src/main.cpp index bbc73ec6..f8f923dc 100644 --- a/PlatformIO_ESP32_oscilloscope/src/main.cpp +++ b/PlatformIO_ESP32_oscilloscope/src/main.cpp @@ -13,19 +13,18 @@ void setup() { Serial.begin(115200); // 开启串口通信:115200 - // 此函数执行一次,可以了 - // 开启串口中断模式 - test_serial_port_callback_function(true); + // 开启串口中断模式 // 此函数执行一次,可以了 + // test_serial_port_callback_enable(true); - BTinitialize("mini_car"); // 蓝牙初始化 // 默认蓝牙名称:mini_bot + BTinitialize("mini_car"); // 蓝牙初始化,默认蓝牙名称:mini_bot } void loop() { - // 使用示波器,不用启用串口中断模式,否则会冲突 - // test_oscilloscope_input_and_output(); + // 使用示波器功能,不用启用串口中断模式,否则会冲突 + // test_oscilloscope_send_and_Receive(); - delay(10); + delay(100); } /************************* test code area begin *************************/ @@ -53,4 +52,23 @@ void loop() // test_oscilloscope_input_and_output(); +// RemoteControlOutput outputBuffer = RemoteControlLoopMode1(JOYSTICK_AND_DIRECTIONAL_PAD, DIRECTIONAL_PAD); +// // RemoteControlOutput outputBuffer={}; +// // outputBuffer.Lup = 1; +// // outputBuffer.Ldown = 2; +// // outputBuffer.Lleft = 3; +// // outputBuffer.Lright = 4; +// // outputBuffer.Rup = 5; +// // outputBuffer.Rdown = 6; +// // outputBuffer.Rleft = 7; +// // outputBuffer.Rright = 8; + +// short *ptr = &outputBuffer.Lup; + +// for (size_t i = 0; i < 18; i++) +// { +// Serial.printf("%d-%d ", i, ptr[i]); +// } +// Serial.println(); + /************************* test code area end *************************/ \ No newline at end of file -- Gitee From d00adf6253ef7cd2fddfb762446e558f867ff54a Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Sat, 3 Aug 2024 00:35:39 +0800 Subject: [PATCH 03/18] =?UTF-8?q?=E6=8A=8A=E9=81=A5=E6=8E=A7=E5=99=A8?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E7=9A=84=E5=87=BD=E6=95=B0=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E8=B0=83=E6=95=B4=EF=BC=8C=E6=8A=8A=E6=95=B0=E6=8D=AE=E8=A3=85?= =?UTF-8?q?=E8=BF=9B=E7=BB=93=E6=9E=84=E4=BD=93=EF=BC=8C=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=88=90=E5=91=98=20OK?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/myBlueTooth/myBlueTooth.cpp | 143 ++++++++++-------- .../lib/myBlueTooth/myBlueTooth.h | 127 +++++++++------- PlatformIO_ESP32_oscilloscope/src/main.cpp | 14 +- 3 files changed, 170 insertions(+), 114 deletions(-) diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp index d88d74b8..0135c26a 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -311,6 +311,38 @@ char OscGetValue(const char *p, const char *name, float *value) return 2; } +// // 定义遥控器输出结构体 +// struct RemoteControlOutput +// { +// short begin; + +// short LEFT_up; // 向上 // 1 +// short LEFT_down; // 向下 +// short LEFT_left; // 向左 +// short LEFT_right; // 向右 + +// short RIGHT_up; // 向上 // 5 +// short RIGHT_down; // 向下 +// short RIGHT_left; // 向左 +// short RIGHT_right; // 向右 + +// short middle; + +// short A; // 按钮A // 10 +// short B; // 按钮B +// short C; // 按钮C +// short D; // 按钮D +// short E; // 按钮E +// short F; // 按钮F + +// short LEFT_x; // 左摇杆X轴 // 16 +// short LEFT_y; // 左摇杆Y轴 +// short RIGHT_x; // 右摇杆X轴 +// short RIGHT_y; // 右摇杆Y轴 + +// short end; +// }; + // 获取摇杆数据转换为 x and y void getInterruptJoystickvalue_XY(char *receiveBuffer, short *joystick_X, short *joystick_Y) { @@ -378,7 +410,7 @@ void RemoteControlInterruptMode(uint8_t *cmd, uint8_t *data, RemoteControlJoysti SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); break; - case INTERRUPT_LEFT_X_Y: + case INTERRUPT_LEFT_X: // 摇杆数值在 left_X and left_Y // 逻辑代码区 begin @@ -387,7 +419,7 @@ void RemoteControlInterruptMode(uint8_t *cmd, uint8_t *data, RemoteControlJoysti // 逻辑代码区 end break; - case INTERRUPT_RIGHT_X_Y: + case INTERRUPT_RIGHT_X: // 逻辑代码区 begin SerialBT.printf("x: %d y: %d\n", Joystick->right_X, Joystick->right_Y); @@ -421,19 +453,32 @@ void RemoteControlInterruptMode(uint8_t *cmd, uint8_t *data, RemoteControlJoysti } } +// 接收数据解析了放进结构体里面,使用成员 OK +void RemoteControlInterruptMode(RemoteControlOutput *structBuffer) +{ + short *ptr = &structBuffer->begin; + for (size_t i = 0; i < structBuffer->end, i < 20; i++) + { + Serial.printf("%d-%d ", i, ptr[i]); + } + Serial.println(); +} + /* 1)蓝牙接收到数据后,会调用此函数,不要在循环中使用,有卡死程序风险 2)回调函数不能有耗时操作,延时操作禁止使用的,会触发看门狗,不断重启 */ void RemoteControlOnDataReceive(const uint8_t *buffer, size_t size) { - struct RemoteControlJoystick Joystick = {0}; + RemoteControlOutput outputBuffer = {}; // 输出的缓冲区 + outputBuffer.end = 20; + // RemoteControlJoystick Joystick = {0}; uint8_t cmd[4] = {0}; uint8_t data[4] = {0}; char receiveBuffer[4] = {0}; - char temp = 0; memcpy(receiveBuffer, buffer, size); + short *ptr = &outputBuffer.begin; // 判断是否摇杆数据,在进行解释 if (size == 4) @@ -441,20 +486,26 @@ void RemoteControlOnDataReceive(const uint8_t *buffer, size_t size) if ((receiveBuffer[0] & 0xF0) == 0x10) { cmd[0] = 0x9A; - getInterruptJoystickvalue_XY(receiveBuffer, &Joystick.left_X, &Joystick.left_Y); + getInterruptJoystickvalue_XY(receiveBuffer, &outputBuffer.LEFT_x, &outputBuffer.LEFT_y); } else if ((receiveBuffer[0] & 0xF0) == 0x50) { cmd[0] = 0x9C; - getInterruptJoystickvalue_XY(receiveBuffer, &Joystick.right_X, &Joystick.right_Y); + getInterruptJoystickvalue_XY(receiveBuffer, &outputBuffer.RIGHT_x, &outputBuffer.RIGHT_y); } + // Serial.printf("x: %d y: %d - x: %d y: %d\n", outputBuffer.LEFT_x, outputBuffer.LEFT_y, outputBuffer.RIGHT_x, outputBuffer.RIGHT_y); } else if (size == 1) { - cmd[0] = (receiveBuffer[0] & 0xF0); + cmd[0] = (receiveBuffer[0] & 0xF0) / 16; data[0] = (receiveBuffer[0] & 0x0F); + + ptr[cmd[0]] = data[0]; + + // Serial.printf("cmd: %d data: %d\n", cmd[0], data[0]); } - RemoteControlInterruptMode(cmd, data, &Joystick); + + RemoteControlInterruptMode(&outputBuffer); } // 获取摇杆数值 @@ -484,6 +535,7 @@ void getDirectionalPad(short *outputButton, char outputPosition, const char *inp } } } + // 获取按钮数值 // (inputButton[inputPosition + i] == 0x31) 识别字符是否等于数字的1 void getFunctionButtonValue(short *outputButton, char outputPosition, const char *inputButton, char inputPosition) @@ -496,46 +548,13 @@ void getFunctionButtonValue(short *outputButton, char outputPosition, const char } } } -// 专门给 RemoteControlLoopMode 函数打印和测试使用 -void RemoteControlLoopModePritnf(char handle, RemoteControlOutput *outputBuffer) -{ - // 不同的遥控器组合,使用不同显示 // 打印在串口监视器中 - switch (handle) - { - case DIRECTIONAL_PAD: - Serial.printf("up: %d down: %d left: %d right: %d - up: %d down: %d left: %d right: %d ", - outputBuffer->LEFT_up, outputBuffer->LEFT_down, outputBuffer->LEFT_left, outputBuffer->LEFT_right, - outputBuffer->RIGHT_up, outputBuffer->RIGHT_down, outputBuffer->RIGHT_left, outputBuffer->RIGHT_right); - break; - - case DIRECTIONAL_PAD_AND_JOYSTICK: - Serial.printf("up: %d down: %d left: %d right: %d - x: %d y: %d ", - outputBuffer->LEFT_up, outputBuffer->LEFT_down, outputBuffer->LEFT_left, outputBuffer->LEFT_right, - outputBuffer->RIGHT_x, outputBuffer->RIGHT_y); - break; - - case JOYSTICK_AND_DIRECTIONAL_PAD: - Serial.printf(" x: %d y: %d - up: %d down: %d left: %d right: %d ", - outputBuffer->LEFT_x, outputBuffer->LEFT_y, - outputBuffer->LEFT_up, outputBuffer->LEFT_down, outputBuffer->LEFT_left, outputBuffer->LEFT_right); - break; - - case JOYSTICKS: - Serial.printf(" x: %d y: %d - x: %d y: %d ", - outputBuffer->LEFT_x, outputBuffer->LEFT_y, outputBuffer->RIGHT_x, outputBuffer->RIGHT_y); - - break; - } - - Serial.printf(" A: %d B: %d C: %d D: %d E: %d F: %d \n", - outputBuffer->A, outputBuffer->B, outputBuffer->C, outputBuffer->D, outputBuffer->E, outputBuffer->F); -} // 遥控器循环模式 -RemoteControlOutput RemoteControlLoopMode1(MODULE_SIZE RemoteControlLayoutLeft, MODULE_SIZE RemoteControlLayoutRight) +RemoteControlOutput RemoteControlLoopMode(MODULE_SIZE RemoteControlLayoutLeft, MODULE_SIZE RemoteControlLayoutRight) { RemoteControlOutput outputBuffer = {}; // 输出的缓冲区 - char tempBuffer[20] = {}; // 临时缓冲区 + outputBuffer.end = 20; + char tempBuffer[20] = {}; // 临时缓冲区 // 判断手柄的方向按钮和摇杆的组合 char handle = (RemoteControlLayoutLeft == JOYSTICK_SIZE ? JOYSTICK_AND_DIRECTIONAL_PAD : DIRECTIONAL_PAD) + (RemoteControlLayoutRight == JOYSTICK_SIZE ? DIRECTIONAL_PAD_AND_JOYSTICK : DIRECTIONAL_PAD); @@ -548,30 +567,29 @@ RemoteControlOutput RemoteControlLoopMode1(MODULE_SIZE RemoteControlLayoutLeft, { case DIRECTIONAL_PAD: // 获取结构体的首地址,在基础上移动到指定位置,进行赋值或者读取 - getDirectionalPad(&outputBuffer.LEFT_up, 0, tempBuffer, 0); - getDirectionalPad(&outputBuffer.LEFT_up, 4, tempBuffer, 4); + getDirectionalPad(&outputBuffer.LEFT_up, 1, tempBuffer, 0); + getDirectionalPad(&outputBuffer.LEFT_up, 5, tempBuffer, 4); getFunctionButtonValue(&outputBuffer.LEFT_up, 8, tempBuffer, 8); break; case DIRECTIONAL_PAD_AND_JOYSTICK: - getDirectionalPad(&outputBuffer.LEFT_up, 0, tempBuffer, 0); - getJoystickValue(&outputBuffer.LEFT_up, 16, tempBuffer, 4); + getDirectionalPad(&outputBuffer.LEFT_up, 1, tempBuffer, 0); + getJoystickValue(&outputBuffer.LEFT_up, 17, tempBuffer, 4); getFunctionButtonValue(&outputBuffer.LEFT_up, 8, tempBuffer, 10); break; case JOYSTICK_AND_DIRECTIONAL_PAD: - getJoystickValue(&outputBuffer.LEFT_up, 14, tempBuffer, 0); - getDirectionalPad(&outputBuffer.LEFT_up, 4, tempBuffer, 6); - getFunctionButtonValue(&outputBuffer.LEFT_up, 8, tempBuffer, 10); + getJoystickValue(&outputBuffer.LEFT_up, 15, tempBuffer, 0); + getDirectionalPad(&outputBuffer.LEFT_up, 5, tempBuffer, 6); + getFunctionButtonValue(&outputBuffer.LEFT_up, 9, tempBuffer, 10); break; case JOYSTICKS: - getJoystickValue(&outputBuffer.LEFT_up, 14, tempBuffer, 0); - getJoystickValue(&outputBuffer.LEFT_up, 16, tempBuffer, 6); - getFunctionButtonValue(&outputBuffer.LEFT_up, 8, tempBuffer, 12); + getJoystickValue(&outputBuffer.LEFT_up, 15, tempBuffer, 0); + getJoystickValue(&outputBuffer.LEFT_up, 17, tempBuffer, 6); + getFunctionButtonValue(&outputBuffer.LEFT_up, 9, tempBuffer, 12); break; } - RemoteControlLoopModePritnf(handle, &outputBuffer); return outputBuffer; } @@ -602,7 +620,6 @@ void test_oscilloscope_send_wave() SendWave(); } - /** * @brief 示波器输入和输出案例 * @@ -640,17 +657,23 @@ void test_remote_control_callback_enable(bool isEnable) /** * @brief 遥控器的定时发送模式 * - * + * */ void test_remote_control_loop_mode() { - // short shortArray[18] = {RemoteControlLoopMode(DIRECTIONAL_PAD_SIZE, JOYSTICK_SIZE)}; } /************************* example end *************************/ // /************************* test begin *************************/ +/* +在处理十六进制的时候发现一个问题,如何把 0x10 and 0x20 转化成数字的 1 and 2, +第一想到的字节的移位操作,但是这样最少操作四次,一个字节八位, +在计算十六进制大小发现一个规律,高位每加一位相差15 +0x50 升一位需要加15,降一位需要15 +*/ + struct myStruct { char a; @@ -661,7 +684,9 @@ struct myStruct }; /* -如何把结构体像数组一样使用? +如何把结构体像数组一样使用?可以做到,前提条件时是结构体成员是单一类型时,结构体内部就像一个数组, +获取结构体的首地址,在基础上进行偏移,可以像数组一样访问了 +问题来了,结构体的大小,或者说时长度问题,需要手动计算,使用for循环遍历警惕循环次数 */ void test_struct_array() { diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h index 4bbdbdc2..a6a70e02 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h @@ -12,29 +12,36 @@ #include // 字符串操作库 #include // ESP32蓝牙库 +// 定义遥控器输出结构体 struct RemoteControlOutput { - short LEFT_up; // 0 - short LEFT_down; - short LEFT_left; - short LEFT_right; - - short RIGHT_up; - short RIGHT_down; - short RIGHT_left; - short RIGHT_right; // 7 - - short A; // 8 - short B; - short C; - short D; - short E; - short F; // 13 - - short LEFT_x; // 14 - short LEFT_y; - short RIGHT_x; - short RIGHT_y; // 17 + short begin; + + short LEFT_up; // 向上 // 1 + short LEFT_down; // 向下 + short LEFT_left; // 向左 + short LEFT_right; // 向右 + + short RIGHT_up; // 向上 // 5 + short RIGHT_down; // 向下 + short RIGHT_left; // 向左 + short RIGHT_right; // 向右 + + short middle; + + short A; // 按钮A // 10 + short B; // 按钮B + short C; // 按钮C + short D; // 按钮D + short E; // 按钮E + short F; // 按钮F + + short LEFT_x; // 左摇杆X轴 // 16 + short LEFT_y; // 左摇杆Y轴 + short RIGHT_x; // 右摇杆X轴 + short RIGHT_y; // 右摇杆Y轴 + + short end; }; // 遥控器循环指令枚举定义 @@ -66,25 +73,24 @@ enum LOOP_MODE_ORDER // 遥控器中断指令枚举定义 enum INTERRUPT_MODE_ORDER { - // 按键状态指令 INTERRUPT_BUTTON_RELEASE = 0X00, // 按键释放指令 INTERRUPT_BUTTON_PRESS = 0X01, // 按键按下指令 - // 左侧方向键指令 INTERRUPT_LEFT_UP = 0X10, // 左侧方向键上 INTERRUPT_LEFT_DOWN = 0X20, // 左侧方向键下 INTERRUPT_LEFT_LEFT = 0X30, // 左侧方向键左 INTERRUPT_LEFT_RIGHT = 0X40, // 左侧方向键右 - // 右侧方向键指令 INTERRUPT_RIGHT_UP = 0X50, // 右侧方向键上 INTERRUPT_RIGHT_DOWN = 0X60, // 右侧方向键下 INTERRUPT_RIGHT_LEFT = 0X70, // 右侧方向键左 INTERRUPT_RIGHT_RIGHT = 0X80, // 右侧方向键右 - INTERRUPT_LEFT_X_Y = 0X9A, - INTERRUPT_RIGHT_X_Y = 0X9C, - // 左右功能按键指令 + INTERRUPT_LEFT_X = 0X9A, + INTERRUPT_LEFT_Y = 0X9B, + INTERRUPT_RIGHT_X = 0X9C, + INTERRUPT_RIGHT_Y = 0X9D, + INTERRUPT_BUTTON_A = 0XA0, // 左侧A功能键 INTERRUPT_BUTTON_B = 0XB0, // 左侧B功能键 INTERRUPT_BUTTON_C = 0XC0, // 右侧C功能键 @@ -106,10 +112,10 @@ typedef enum MODULE_SIZE // 遥控器的方向按钮和摇杆组合 enum POLYTYPE { - DIRECTIONAL_PAD = 0x00, // 0x00 + DIRECTIONAL_PAD = 0x00, // 0x00 DIRECTIONAL_PAD_AND_JOYSTICK = 0x01, // 0x01 JOYSTICK_AND_DIRECTIONAL_PAD = 0x10, // 0x10 - JOYSTICKS = 0x11 // 0x11 + JOYSTICKS = 0x11 // 0x11 }; struct RemoteControlJoystick @@ -120,69 +126,82 @@ struct RemoteControlJoystick short right_Y; }; +/************************* 蓝牙功能相关函数 *************************/ -/************************* 蓝牙功能接口 *************************/ - -// 设定蓝牙名称,默认为"mini_bot" +// **初始化蓝牙模块**,设置设备名,默认为"mini_bot" void BTinitialize(const String &deviceName = "mini_bot"); -// 将浮点数转换字符串通过蓝牙发送 +// **发送浮点数**通过蓝牙 void BTsendFloatVia(const float *data); -// 发送字符串到蓝牙连接的设备 +// **发送字符串**到蓝牙连接的设备 void BTsendStringVia(const char *stringData); -// 返回一个指针指向接收到的字符串数据。 +// **获取蓝牙接收的字符串** char *BTgetReceivedData(); -/************************* 学会助手接口 ************************/ +/************************* 串口通讯辅助函数 ************************/ -// 当通过UART接收到数据时,此函数将被调用 +// **串口数据接收回调**,当有数据通过UART接收到时调用 void Uart_onDataReceive(const uint8_t *dataBuffer, size_t dataSize); -// 获取接收到的数据 +// **获取串口接收数据** char *Uart_getReceiveData(); -// 通过UART发送一个字节的数据 +// **发送单个字节**通过UART void Uart_SendByte(const uint8_t *byte); -// 通过UART发送一个字节数组 +// **发送字节数组**通过UART void Uart_SendArray(const uint8_t *array, size_t arraySize); -// 发送指定数据格式 +// **发送特定格式数据**,如波形数据 void XHZS_SendWave(const void *waveAddr, uint16_t waveSize); -// 发送波形数据的函数 +/************************* 示波器相关函数 ************************/ + +// **实际发送波形数据的函数** void SendWave(); -// 输入 char *p 解析为 float *value +// **解析字符串为浮点数** char OscGetFloat(const char *p, float *value); -// 从示波器获取的数据格式,按字段解析为数值 +// **从特定格式数据中提取浮点数** char OscGetValue(const char *p, const char *name, float *value); +/************************* 遥控器交互相关函数 ************************/ + +// 遥控器数据接收处理回调 void RemoteControlOnDataReceive(const uint8_t *buffer, size_t size); -void RemoteControlInterruptMode(uint8_t *cmd, uint8_t *data, RemoteControlJoystick *Joystick); +// 处理遥控器中断模式下的命令 +void RemoteControlInterruptMode(RemoteControlOutput *structBuffer); -/************************* 演示函数 ************************/ +// 根据布局获取遥控器循环模式的输出 +RemoteControlOutput RemoteControlLoopMode(MODULE_SIZE RemoteControlLayoutLeft, MODULE_SIZE RemoteControlLayoutRight); -void test_serial_port_callback_enable(bool isEnable = false); // 开启串口回调函数,在蓝牙初始化之前调用 +/************************* 示例与测试函数 ************************/ -void test_oscilloscope_send_wave(); // 发送波形数据的函数 +// 启用串口回调功能,通常在蓝牙初始化前调用 +void test_serial_port_callback_enable(bool isEnable = false); -void test_oscilloscope_send_and_Receive(); // 示波器输入和输出案例 +// 测试发送波形数据到其他设备 +void test_oscilloscope_send_wave(); -void test_remote_control_callback_enable(bool isEnable=false); // 遥控器回调函数,在蓝牙初始化之前调用 +// 示例:示波器数据收发互动 +void test_oscilloscope_send_and_Receive(); -/************************* 其它模块适配,适配中... ************************/ +// 启用遥控器数据接收的回调处理 +void test_remote_control_callback_enable(bool isEnable = false); -RemoteControlOutput RemoteControlLoopMode1(MODULE_SIZE RemoteControlLayoutLeft, MODULE_SIZE RemoteControlLayoutRight); +/************************* 其它模块适配,适配中... ************************/ -void test_remote_control(const uint8_t *buffer, size_t size); // 遥控器 +// 遥控器数据处理测试函数 +void test_remote_control(const uint8_t *buffer, size_t size); -void RemoteControlInterruptMode(); // 遥控器中断模式 +// 测试遥控器中断模式下的功能 +void XHZS_RemoteControlInterruptMode(); +// 结构体数组的使用示例 void test_struct_array(); #endif diff --git a/PlatformIO_ESP32_oscilloscope/src/main.cpp b/PlatformIO_ESP32_oscilloscope/src/main.cpp index f8f923dc..c0235f2c 100644 --- a/PlatformIO_ESP32_oscilloscope/src/main.cpp +++ b/PlatformIO_ESP32_oscilloscope/src/main.cpp @@ -6,6 +6,8 @@ select engineering environment: ESP32_WROVER_Kit(all_versions) #include #include "myBlueTooth.h" +#include +#include BluetoothSerial SerialBlueTooth; @@ -15,6 +17,7 @@ void setup() // 开启串口中断模式 // 此函数执行一次,可以了 // test_serial_port_callback_enable(true); + test_remote_control_callback_enable(true); BTinitialize("mini_car"); // 蓝牙初始化,默认蓝牙名称:mini_bot } @@ -24,7 +27,16 @@ void loop() // 使用示波器功能,不用启用串口中断模式,否则会冲突 // test_oscilloscope_send_and_Receive(); - delay(100); + // unsigned char ch[6] = {0x10, 0x20, 0x30, 0xA0, 0xB0, 0xC0}; + + // for (size_t i = 0; i < sizeof(ch); i++) + // { + // ch[i] = (ch[i] & 0xF0) / 16; + // Serial.printf("%d-%x ", ch[i], ch[i]); + // } + // Serial.println(); + + delay(10); } /************************* test code area begin *************************/ -- Gitee From 6b75fdba85306655a20b924ada08bf152b78c15e Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Sat, 3 Aug 2024 18:36:08 +0800 Subject: [PATCH 04/18] =?UTF-8?q?=E9=81=A5=E6=8E=A7=E5=99=A8=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/myBlueTooth/myBlueTooth.cpp | 203 ++++++++---------- .../lib/myBlueTooth/myBlueTooth.h | 75 +++---- PlatformIO_ESP32_oscilloscope/src/main.cpp | 83 ++----- 3 files changed, 135 insertions(+), 226 deletions(-) diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp index 0135c26a..801e57dc 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -29,7 +29,7 @@ void BTinitialize(const String &deviceName) if (SERIAL_PORT_CALLBACK_ENABLE == true) { - SerialBT.onData(Uart_onDataReceive); // 测试手柄功能未完成_2024-07-27 + SerialBT.onData(Uart_onDataReceive); } else if (REMOTE_CONTROL_CALLBACK_ENABLE == true) { @@ -123,16 +123,14 @@ void BTAuthCompleteCallback(boolean success) } /************************* blueTooth end *************************/ -// +// 函数前置声明 /************************* XHZS begin *************************/ -void getInterruptJoystickvalue_XY(char *receiveBuffer, short *joystick_X, short *joystick_Y); -void RemoteControlInterruptMode(uint8_t *cmd, uint8_t *data, RemoteControlJoystick *Joystick); -void getJoystickValue(short *outputButton, char outputPosition, char *inputButton, char inputPosition); -void getDirectionalPad(short *outputButton, char outputPosition, char *inputButton, char inputPosition); -void getFunctionButtonValue(short *outputButton, char outputPosition, char *inputButton, char inputPosition); - -// 当通过UART接收到数据时,此函数将被调用 +/* + * 串口接收到数据开启调用,和stm32的中断函数一样 + * 需要在串口发送和接收实现自己逻辑代码,直接写代码逻辑 + * 回调函数不能有耗时操作,延时操作禁止使用的,会触发看门狗,不断重启 + */ void Uart_onDataReceive(const uint8_t *dataBuffer, size_t dataSize) { // 判断接收到的数量 @@ -143,7 +141,7 @@ void Uart_onDataReceive(const uint8_t *dataBuffer, size_t dataSize) } } -// 获取接收到的数据 +// 获取串口接收到的数据 char *Uart_getReceiveData() { return blueToothBuffer; @@ -311,38 +309,6 @@ char OscGetValue(const char *p, const char *name, float *value) return 2; } -// // 定义遥控器输出结构体 -// struct RemoteControlOutput -// { -// short begin; - -// short LEFT_up; // 向上 // 1 -// short LEFT_down; // 向下 -// short LEFT_left; // 向左 -// short LEFT_right; // 向右 - -// short RIGHT_up; // 向上 // 5 -// short RIGHT_down; // 向下 -// short RIGHT_left; // 向左 -// short RIGHT_right; // 向右 - -// short middle; - -// short A; // 按钮A // 10 -// short B; // 按钮B -// short C; // 按钮C -// short D; // 按钮D -// short E; // 按钮E -// short F; // 按钮F - -// short LEFT_x; // 左摇杆X轴 // 16 -// short LEFT_y; // 左摇杆Y轴 -// short RIGHT_x; // 右摇杆X轴 -// short RIGHT_y; // 右摇杆Y轴 - -// short end; -// }; - // 获取摇杆数据转换为 x and y void getInterruptJoystickvalue_XY(char *receiveBuffer, short *joystick_X, short *joystick_Y) { @@ -366,11 +332,15 @@ void getInterruptJoystickvalue_XY(char *receiveBuffer, short *joystick_X, short *joystick_Y = (y > 0) ? y : -y; } -// 遥控器中断模式 -void RemoteControlInterruptMode(uint8_t *cmd, uint8_t *data, RemoteControlJoystick *Joystick) +/* + * 遥控器的非定时模式 + * RemoteControlOnDataReceive() 前面的函数解析好了,遥控器的每个按键对应着一个 case, + * 在 case INTERRUPT_RIGHT_UP 表示,右边的方向按钮的向上按钮,根据按键的状态,写自己的逻辑代码 + * cmd[0] 遥控器的指令,data[0] 表示遥控器的状态,0 表示松开,1 表示按下,摇杆显示 x and y 数值 + */ +void RemoteControlInterruptMode33(uint8_t *cmd, uint8_t *data, short left_X, short left_Y, short right_X, short right_Y) { - // - // 遥控器中断指令枚举定义 enum INTERRUPT_MODE_ORDER + // 在指定的按钮 case 中实现逻辑代码,中断不用有延时操作 switch (cmd[0]) { case INTERRUPT_LEFT_UP: @@ -414,7 +384,7 @@ void RemoteControlInterruptMode(uint8_t *cmd, uint8_t *data, RemoteControlJoysti // 摇杆数值在 left_X and left_Y // 逻辑代码区 begin - SerialBT.printf("x: %d y: %d\n", Joystick->left_X, Joystick->left_Y); + SerialBT.printf("x: %d y: %d\n", left_X, left_Y); // 逻辑代码区 end break; @@ -422,7 +392,7 @@ void RemoteControlInterruptMode(uint8_t *cmd, uint8_t *data, RemoteControlJoysti case INTERRUPT_RIGHT_X: // 逻辑代码区 begin - SerialBT.printf("x: %d y: %d\n", Joystick->right_X, Joystick->right_Y); + SerialBT.printf("x: %d y: %d\n", right_X, right_Y); // 逻辑代码区 end break; @@ -453,10 +423,15 @@ void RemoteControlInterruptMode(uint8_t *cmd, uint8_t *data, RemoteControlJoysti } } -// 接收数据解析了放进结构体里面,使用成员 OK -void RemoteControlInterruptMode(RemoteControlOutput *structBuffer) +/* + * RemoteControlOnDataReceive() 前面的函数解析好了,在这里使用结构体成员的方法,获取数值 + * 遥控器的非定时模式,1 表示按下,0 表示松开,摇杆显示 x and y 数值 + */ +void RemoteControlInterruptMode22(structRemoteControlFormat *structBuffer) { - short *ptr = &structBuffer->begin; + // 遍历结构体成员代码,了解 struct structRemoteControlFormat 格式内容 + // structBuffer->A 和 ptr[10] 这两个表达都是同一个地址,他们位置都是:10 + short *ptr = &structBuffer->begin; // 获取结构体首地址 for (size_t i = 0; i < structBuffer->end, i < 20; i++) { Serial.printf("%d-%d ", i, ptr[i]); @@ -465,16 +440,17 @@ void RemoteControlInterruptMode(RemoteControlOutput *structBuffer) } /* -1)蓝牙接收到数据后,会调用此函数,不要在循环中使用,有卡死程序风险 -2)回调函数不能有耗时操作,延时操作禁止使用的,会触发看门狗,不断重启 -*/ + * 串口接收到数据开启调用,和stm32的中断函数一样,在 RemoteControlOnDataReceive() 函数实现数据解析工作, + * 你的逻辑代码建议在 RemoteControlInterruptMode22() 或者在 RemoteControlInterruptMode33() 实现 + * 回调函数不能有耗时操作,延时操作禁止使用的,会触发看门狗,不断重启 + * 函数功能:把不同的按钮指令,存放在指定的结构体中 + */ void RemoteControlOnDataReceive(const uint8_t *buffer, size_t size) { - RemoteControlOutput outputBuffer = {}; // 输出的缓冲区 + structRemoteControlFormat outputBuffer = {}; // 输出的缓冲区 outputBuffer.end = 20; - // RemoteControlJoystick Joystick = {0}; - uint8_t cmd[4] = {0}; - uint8_t data[4] = {0}; + uint8_t cmd[4] = {0}; // 指令 + uint8_t data[4] = {0}; // 数据 char receiveBuffer[4] = {0}; memcpy(receiveBuffer, buffer, size); @@ -493,19 +469,19 @@ void RemoteControlOnDataReceive(const uint8_t *buffer, size_t size) cmd[0] = 0x9C; getInterruptJoystickvalue_XY(receiveBuffer, &outputBuffer.RIGHT_x, &outputBuffer.RIGHT_y); } - // Serial.printf("x: %d y: %d - x: %d y: %d\n", outputBuffer.LEFT_x, outputBuffer.LEFT_y, outputBuffer.RIGHT_x, outputBuffer.RIGHT_y); } else if (size == 1) { cmd[0] = (receiveBuffer[0] & 0xF0) / 16; data[0] = (receiveBuffer[0] & 0x0F); - + // 在这里把结构体当成一个数组来使用 ptr[cmd[0]] = data[0]; - - // Serial.printf("cmd: %d data: %d\n", cmd[0], data[0]); + // switch case 模式 * 16 + cmd[0] = cmd[0] * 16; } - - RemoteControlInterruptMode(&outputBuffer); + // 选择一个函数使用 + RemoteControlInterruptMode22(&outputBuffer); // 使用结构体的成员方法 + // RemoteControlInterruptMode33(cmd, data, outputBuffer.LEFT_x, outputBuffer.LEFT_y, outputBuffer.RIGHT_x, outputBuffer.RIGHT_y); // 使用 switch case 模式 } // 获取摇杆数值 @@ -549,12 +525,18 @@ void getFunctionButtonValue(short *outputButton, char outputPosition, const char } } -// 遥控器循环模式 -RemoteControlOutput RemoteControlLoopMode(MODULE_SIZE RemoteControlLayoutLeft, MODULE_SIZE RemoteControlLayoutRight) +/* + * 遥控器的定时模式 + * 功能:对一串有规律的字符串解析处理,放在指定结构体成员中 + * 使用了数组的方式,将结构体当成一个数组来使用,结构体的偏移量来确定成员位置 + */ +structRemoteControlFormat RemoteControlTimerMode(MODULE_SIZE RemoteControlLayoutLeft, MODULE_SIZE RemoteControlLayoutRight) { - RemoteControlOutput outputBuffer = {}; // 输出的缓冲区 + structRemoteControlFormat outputBuffer = {0}; // 输出的缓冲区 + outputBuffer.begin = 0; outputBuffer.end = 20; - char tempBuffer[20] = {}; // 临时缓冲区 + char tempBuffer[20] = {0}; // 临时缓冲区 + // 判断手柄的方向按钮和摇杆的组合 char handle = (RemoteControlLayoutLeft == JOYSTICK_SIZE ? JOYSTICK_AND_DIRECTIONAL_PAD : DIRECTIONAL_PAD) + (RemoteControlLayoutRight == JOYSTICK_SIZE ? DIRECTIONAL_PAD_AND_JOYSTICK : DIRECTIONAL_PAD); @@ -567,27 +549,27 @@ RemoteControlOutput RemoteControlLoopMode(MODULE_SIZE RemoteControlLayoutLeft, M { case DIRECTIONAL_PAD: // 获取结构体的首地址,在基础上移动到指定位置,进行赋值或者读取 - getDirectionalPad(&outputBuffer.LEFT_up, 1, tempBuffer, 0); - getDirectionalPad(&outputBuffer.LEFT_up, 5, tempBuffer, 4); - getFunctionButtonValue(&outputBuffer.LEFT_up, 8, tempBuffer, 8); + getDirectionalPad(&outputBuffer.begin, 1, tempBuffer, 0); + getDirectionalPad(&outputBuffer.begin, 5, tempBuffer, 4); + getFunctionButtonValue(&outputBuffer.begin, 10, tempBuffer, 8); break; case DIRECTIONAL_PAD_AND_JOYSTICK: - getDirectionalPad(&outputBuffer.LEFT_up, 1, tempBuffer, 0); - getJoystickValue(&outputBuffer.LEFT_up, 17, tempBuffer, 4); - getFunctionButtonValue(&outputBuffer.LEFT_up, 8, tempBuffer, 10); + getDirectionalPad(&outputBuffer.begin, 1, tempBuffer, 0); + getJoystickValue(&outputBuffer.begin, 18, tempBuffer, 4); + getFunctionButtonValue(&outputBuffer.begin, 10, tempBuffer, 10); break; case JOYSTICK_AND_DIRECTIONAL_PAD: - getJoystickValue(&outputBuffer.LEFT_up, 15, tempBuffer, 0); - getDirectionalPad(&outputBuffer.LEFT_up, 5, tempBuffer, 6); - getFunctionButtonValue(&outputBuffer.LEFT_up, 9, tempBuffer, 10); + getJoystickValue(&outputBuffer.begin, 16, tempBuffer, 0); + getDirectionalPad(&outputBuffer.begin, 5, tempBuffer, 6); + getFunctionButtonValue(&outputBuffer.begin, 10, tempBuffer, 10); break; case JOYSTICKS: - getJoystickValue(&outputBuffer.LEFT_up, 15, tempBuffer, 0); - getJoystickValue(&outputBuffer.LEFT_up, 17, tempBuffer, 6); - getFunctionButtonValue(&outputBuffer.LEFT_up, 9, tempBuffer, 12); + getJoystickValue(&outputBuffer.begin, 16, tempBuffer, 0); + getJoystickValue(&outputBuffer.begin, 18, tempBuffer, 6); + getFunctionButtonValue(&outputBuffer.begin, 10, tempBuffer, 12); break; } return outputBuffer; @@ -598,16 +580,9 @@ RemoteControlOutput RemoteControlLoopMode(MODULE_SIZE RemoteControlLayoutLeft, M /************************* example begin *************************/ /** - * @brief 启用或禁用串口回调机制 - * - * 此函数应在蓝牙初始化前调用,以配置串口(蓝牙)接收数据时的行为。 - * 当 isEnable 设置为 true 时,开启串口回调;设置为 false 时,则禁用。 - * + * 此函数应在蓝牙初始化前调用,当 isEnable 数值为 true 时开启,false 则禁用。 * 在底层蓝牙库中,onData() 函数将绑定 Uart_onDataReceive() 作为其回调,从而在接收到数据时触发相应的处理逻辑。 - * - * 注意:Uart_onDataReceive() 函数中不应包含任何延时操作,因其可能在中断上下文中执行。 - * - * 在不使用串口功能时,务必将其关闭,以避免与其他功能模块产生潜在冲突。 + * 串口回调时实现自己的代码逻辑,找到 Uart_onDataReceive() 修改里面代码 */ void test_serial_port_callback_enable(bool isEnable) { @@ -621,8 +596,7 @@ void test_oscilloscope_send_wave() } /** - * @brief 示波器输入和输出案例 - * + * 示波器发送和接收函数示例 */ void test_oscilloscope_send_and_Receive() { @@ -640,14 +614,13 @@ void test_oscilloscope_send_and_Receive() 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); // 延时函数,可以注释掉 + delay(50); // 延时函数,可以注释掉 } -/** - * @brief 启用或禁用,遥控器的回调函数,非定时模式有用 - * - * 此函数应在蓝牙初始化前调用,以配置串口(蓝牙)接收数据时的行为。 - * 当 isEnable 设置为 true 时,开启串口回调;设置为 false 时,则禁用。 +/* + * 此函数应在蓝牙初始化前调用,当 isEnable 数值为 true 时开启,false 则禁用。 + * 在底层蓝牙库中,onData() 函数将绑定 RemoteControlOnDataReceive() 作为其回调,从而在接收到数据时触发相应的处理逻辑。 + * 串口回调时实现自己的代码逻辑,找到 RemoteControlOnDataReceive() 修改里面代码 */ void test_remote_control_callback_enable(bool isEnable) { @@ -655,18 +628,26 @@ void test_remote_control_callback_enable(bool isEnable) } /** - * @brief 遥控器的定时发送模式 - * + * 遥控器的定时发送模式 * */ -void test_remote_control_loop_mode() +void test_remote_control_Timer_function() { + structRemoteControlFormat buffer = RemoteControlTimerMode(DIRECTIONAL_PAD_SIZE, JOYSTICK_SIZE); + + short *ptr = &buffer.begin; + for (size_t i = 0; i < 20, i < buffer.end; i++) + { + Serial.printf("%d-%d ", i, ptr[i]); + } + Serial.println(); + delay(50); // 延时函数可以删除 } /************************* example end *************************/ // /************************* test begin *************************/ - +#if false /* 在处理十六进制的时候发现一个问题,如何把 0x10 and 0x20 转化成数字的 1 and 2, 第一想到的字节的移位操作,但是这样最少操作四次,一个字节八位, @@ -705,19 +686,21 @@ void test_struct_array() delay(100); } -/* -struct MyStruct { +struct MyStruct +{ int a; int b; int c; // 假设a, b, c是连续存储的 }; - -MyStruct s; -int* ptr = &s.a; // 获取a的地址 -for (int i = 0; i < 3; ++i) { - *(ptr + i) = i; // 通过指针偏移访问,尽管能工作但不推荐 +void test_struct_move() +{ + MyStruct s; + int *ptr = &s.a; // 获取a的地址 + for (int i = 0; i < 3; ++i) + { + *(ptr + i) = i; // 通过指针偏移访问,尽管能工作但不推荐 + } } -*/ - +#endif /************************* test end *************************/ diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h index a6a70e02..238b94d0 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h @@ -9,11 +9,11 @@ #define MYBLUETOOTH_H_ #include -#include // 字符串操作库 -#include // ESP32蓝牙库 +#include +#include // 定义遥控器输出结构体 -struct RemoteControlOutput +struct structRemoteControlFormat { short begin; @@ -44,32 +44,6 @@ struct RemoteControlOutput short end; }; -// 遥控器循环指令枚举定义 -enum LOOP_MODE_ORDER -{ - LOOP_LEFT_UP = 0, // 左上方向 - LOOP_LEFT_DOWN = 1, // 左下方向 - LOOP_LEFT_LEFT = 2, // 左方向 - LOOP_LEFT_RIGHT = 3, // 右方向 - - LOOP_RIGHT_UP = 4, // 右上方向 - LOOP_RIGHT_DOWN = 5, // 右下方向 - LOOP_RIGHT_LEFT = 6, // 右左方向 - LOOP_RIGHT_RIGHT = 7, // 右右方向 - - LOOP_BUTTON_A = 8, // 按钮A - LOOP_BUTTON_B = 9, // 按钮B - LOOP_BUTTON_C = 10, // 按钮C - LOOP_BUTTON_D = 11, // 按钮D - LOOP_BUTTON_E = 12, // 按钮E - LOOP_BUTTON_F = 13, // 按钮F - - LOOP_LEFT_X = 14, // 左摇杆X轴 - LOOP_LEFT_Y = 15, // 左摇杆Y轴 - LOOP_RIGHT_X = 16, // 右摇杆X轴 - LOOP_RIGHT_Y = 17 // 右摇杆Y轴 -}; - // 遥控器中断指令枚举定义 enum INTERRUPT_MODE_ORDER { @@ -86,7 +60,7 @@ enum INTERRUPT_MODE_ORDER INTERRUPT_RIGHT_LEFT = 0X70, // 右侧方向键左 INTERRUPT_RIGHT_RIGHT = 0X80, // 右侧方向键右 - INTERRUPT_LEFT_X = 0X9A, + INTERRUPT_LEFT_X = 0X9A, // 摇杆的指令 INTERRUPT_LEFT_Y = 0X9B, INTERRUPT_RIGHT_X = 0X9C, INTERRUPT_RIGHT_Y = 0X9D, @@ -105,8 +79,7 @@ typedef enum MODULE_SIZE JOYSTICK_SIZE = 6, // 手柄摇杆,占位6 DIRECTIONAL_PAD_SIZE = 4, // 方向按钮,占位4 FUNCTION_BUTTON_SIZE = 6, // 功能按键,占位6 - // BUTTON_SIZE = 1, // 按键,占位1 - // SWITCH_SIZE = 1 // 开关,占位1 + } INTERFACE_SIZE; // 遥控器的方向按钮和摇杆组合 @@ -118,13 +91,6 @@ enum POLYTYPE JOYSTICKS = 0x11 // 0x11 }; -struct RemoteControlJoystick -{ - short left_X; - short left_Y; - short right_X; - short right_Y; -}; /************************* 蓝牙功能相关函数 *************************/ @@ -174,14 +140,17 @@ char OscGetValue(const char *p, const char *name, float *value); void RemoteControlOnDataReceive(const uint8_t *buffer, size_t size); // 处理遥控器中断模式下的命令 -void RemoteControlInterruptMode(RemoteControlOutput *structBuffer); +void RemoteControlInterruptMode22(structRemoteControlFormat *structBuffer); + +// 处理遥控器中断模式下的命令 // 备用 +void RemoteControlInterruptMode33(uint8_t *cmd, uint8_t *data, short left_X, short left_Y, short right_X, short right_Y); -// 根据布局获取遥控器循环模式的输出 -RemoteControlOutput RemoteControlLoopMode(MODULE_SIZE RemoteControlLayoutLeft, MODULE_SIZE RemoteControlLayoutRight); +// 获取遥控器定时模式的数据 +structRemoteControlFormat RemoteControlTimerMode(MODULE_SIZE RemoteControlLayoutLeft, MODULE_SIZE RemoteControlLayoutRight); /************************* 示例与测试函数 ************************/ -// 启用串口回调功能,通常在蓝牙初始化前调用 +// 启用串口函数回调功能,在蓝牙初始化前调用 void test_serial_port_callback_enable(bool isEnable = false); // 测试发送波形数据到其他设备 @@ -190,18 +159,24 @@ void test_oscilloscope_send_wave(); // 示例:示波器数据收发互动 void test_oscilloscope_send_and_Receive(); -// 启用遥控器数据接收的回调处理 +// 启用遥控器函数回调函数,在蓝牙初始化前调用 void test_remote_control_callback_enable(bool isEnable = false); +// 示例:遥控器定时模式下的功能 +void test_remote_control_Timer_function(); + /************************* 其它模块适配,适配中... ************************/ -// 遥控器数据处理测试函数 -void test_remote_control(const uint8_t *buffer, size_t size); +// // 遥控器数据处理测试函数 +// void test_remote_control(const uint8_t *buffer, size_t size); + +// // 测试遥控器中断模式下的功能 +// void XHZS_RemoteControlInterruptMode(); -// 测试遥控器中断模式下的功能 -void XHZS_RemoteControlInterruptMode(); +// // 结构体数组的使用示例 +// void test_struct_array(); -// 结构体数组的使用示例 -void test_struct_array(); +// // 结构体移动示例 +// void test_struct_move(); #endif diff --git a/PlatformIO_ESP32_oscilloscope/src/main.cpp b/PlatformIO_ESP32_oscilloscope/src/main.cpp index c0235f2c..9580ecc6 100644 --- a/PlatformIO_ESP32_oscilloscope/src/main.cpp +++ b/PlatformIO_ESP32_oscilloscope/src/main.cpp @@ -1,86 +1,37 @@ /* -select engineering environment: ESP32_WROVER_Kit(all_versions) -想使用“学会助手”功能,查看 myBlueTooth.h 头文件里面有相关说明 -学会助手的串口和示波器功能,可以使用了 ^_^ +选择工程环境:ESP32_WROVER_Kit +如需使用“学会助手”功能,请参阅myBlueTooth.h头文件中的相关说明 +“学会助手”的串口功能、示波器功能和遥控器功能现已可用 ^_^ */ #include #include "myBlueTooth.h" -#include #include -BluetoothSerial SerialBlueTooth; +BluetoothSerial SerialBlueTooth; // 定义蓝牙串口对象 +// 串口回调函数和遥控器回调函数不能同时使用 +// 回调函数和示波器和遥控器功能不能同时使用 void setup() { - Serial.begin(115200); // 开启串口通信:115200 + // 初始化串口通信,设置波特率为115200 + Serial.begin(115200); - // 开启串口中断模式 // 此函数执行一次,可以了 + // // 启用串口通信的回调功能 // test_serial_port_callback_enable(true); + + // 启用遥控器功能的回调 ok test_remote_control_callback_enable(true); - BTinitialize("mini_car"); // 蓝牙初始化,默认蓝牙名称:mini_bot + // 初始化蓝牙模块,设置蓝牙名称为"mini_car" + BTinitialize("mini_car"); } void loop() { - // 使用示波器功能,不用启用串口中断模式,否则会冲突 + // // 执行示波器功能的数据发送和接收 // test_oscilloscope_send_and_Receive(); - // unsigned char ch[6] = {0x10, 0x20, 0x30, 0xA0, 0xB0, 0xC0}; - - // for (size_t i = 0; i < sizeof(ch); i++) - // { - // ch[i] = (ch[i] & 0xF0) / 16; - // Serial.printf("%d-%x ", ch[i], ch[i]); - // } - // Serial.println(); - - delay(10); -} - -/************************* test code area begin *************************/ - -// char *data = Uart_ReceiveData(); -// SerialBlueTooth.write((uint8_t *)data, strlen(data)); // 输出到连接的设备 -// SerialBlueTooth.println(); -// SerialBlueTooth.printf(data, strlen(data)); // 输出到连接的设备 -// SerialBlueTooth.println(); - -// delay(1000); - -// Serial.write(data, strlen(data)); // 输出到串行监视器 -// Serial.println(); -// Serial.printf(data, strlen(data)); // 输出到串行监视器 -// Serial.println(); - -// char ch[] = "hello"; - -// Uart_SendByte((uint8_t *)ch); - -// Uart_SendArray((uint8_t *)ch, sizeof(ch)); - -// SendWave(); - -// test_oscilloscope_input_and_output(); - -// RemoteControlOutput outputBuffer = RemoteControlLoopMode1(JOYSTICK_AND_DIRECTIONAL_PAD, DIRECTIONAL_PAD); -// // RemoteControlOutput outputBuffer={}; -// // outputBuffer.Lup = 1; -// // outputBuffer.Ldown = 2; -// // outputBuffer.Lleft = 3; -// // outputBuffer.Lright = 4; -// // outputBuffer.Rup = 5; -// // outputBuffer.Rdown = 6; -// // outputBuffer.Rleft = 7; -// // outputBuffer.Rright = 8; - -// short *ptr = &outputBuffer.Lup; - -// for (size_t i = 0; i < 18; i++) -// { -// Serial.printf("%d-%d ", i, ptr[i]); -// } -// Serial.println(); - -/************************* test code area end *************************/ \ No newline at end of file + // // 遥控器的定时功能演示 + // test_remote_control_Timer_function(); +} \ No newline at end of file -- Gitee From cae3291c7e2d26896175f7620ce6b9e3e077c693 Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Sun, 4 Aug 2024 00:54:55 +0800 Subject: [PATCH 05/18] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BA=86=E4=B9=8B?= =?UTF-8?q?=E5=89=8D=E7=9A=84=E9=81=A5=E6=8E=A7=E5=99=A8=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E6=8A=8A=E9=81=A5=E6=8E=A7=E5=99=A8=E7=9A=84=E6=8C=89?= =?UTF-8?q?=E9=94=AE=E6=95=B0=E5=80=BC=E6=B1=87=E6=80=BB=E5=9C=A8=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E7=BB=93=E6=9E=84=E4=BD=93=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.vscode/settings.json | 3 +- PlatformIO_ESP32_oscilloscope/include/note.h | 11 ++++ .../lib/myBlueTooth/myBlueTooth.cpp | 59 +++++++++++++++---- .../lib/myBlueTooth/myBlueTooth.h | 2 +- PlatformIO_ESP32_oscilloscope/src/main.cpp | 9 ++- 5 files changed, 68 insertions(+), 16 deletions(-) diff --git a/PlatformIO_ESP32_oscilloscope/.vscode/settings.json b/PlatformIO_ESP32_oscilloscope/.vscode/settings.json index 0146d6b9..70cc27c7 100644 --- a/PlatformIO_ESP32_oscilloscope/.vscode/settings.json +++ b/PlatformIO_ESP32_oscilloscope/.vscode/settings.json @@ -15,5 +15,6 @@ "streambuf": "cpp", "initializer_list": "cpp", "utility": "cpp" - } + }, + "C_Cpp.errorSquiggles": "disabled" } \ No newline at end of file diff --git a/PlatformIO_ESP32_oscilloscope/include/note.h b/PlatformIO_ESP32_oscilloscope/include/note.h index f0d7683e..72b60dca 100644 --- a/PlatformIO_ESP32_oscilloscope/include/note.h +++ b/PlatformIO_ESP32_oscilloscope/include/note.h @@ -18,6 +18,17 @@ # 硬件层:GPIO 输出高低电平,电机旋转,蓝牙发送和接收 +# 在 platformIO 中遇到函数问题 + 在其它文件声明的函数 void test_output(); + 在 main() 主函数中重新写一遍不会报错,默认为新的的函数 + 当时处理没有发现前面加了 void 的函数,导致编译器认为这个函数是新的函数,而不是之前的函数 + 导致你的代码逻辑没有被执行 + + + + + + // ESP32-WROOM-32E 引脚功能 start -------------------- diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp index 801e57dc..12775e41 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -175,7 +175,7 @@ void Uart_SendArray(const uint8_t *array, size_t arraySize) *注意事项:示波器支持 uin16_t,建立一个 uint16_t fun[4] 数组, 元素是通道数量 **************************************************************/ -void XHZS_SendWave(const void *waveAddr, uint16_t waveSize) +void XHZS_SendWave(const uint8_t *waveAddr, uint16_t waveSize) { // 参数检查 if (waveAddr == nullptr || waveSize == 0) @@ -186,7 +186,7 @@ void XHZS_SendWave(const void *waveAddr, uint16_t waveSize) uint8_t cmd_tail[2] = {0xFC, 0x03}; // 帧尾 Uart_SendArray(cmd_head, sizeof(cmd_head)); // 发送帧头 - Uart_SendArray((uint8_t *)waveAddr, waveSize); // 发送数据 + Uart_SendArray(waveAddr, waveSize); // 发送数据 Uart_SendArray(cmd_tail, sizeof(cmd_tail)); // 发送帧尾 } @@ -200,7 +200,7 @@ void XHZS_SendWave(const void *waveAddr, uint16_t waveSize) void SendWave() { // // 输出平行线测试 start ----------------------- - // int32_t line[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 + // char line[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 // for (size_t i = 0; i < 500; i += 0.1) // { // line[0] = (line[0] > 10 ? -10 : (line[0] += 1)); @@ -208,8 +208,9 @@ void SendWave() // line[2] = 5.0; // line[3] = 0.0; - // XHZS_SendWave(line, sizeof(line)); + // XHZS_SendWave((uint8_t *)line, sizeof(line)); // vTaskDelay(50 / portTICK_PERIOD_MS); // 延时函数可以注释掉 + // // } // // 输出平行线测试 end ----------------------- @@ -222,7 +223,7 @@ void SendWave() Wave[1] = cos(i); Wave[2] = -sin(i); Wave[3] = -cos(i); - XHZS_SendWave(Wave, sizeof(Wave)); + XHZS_SendWave((uint8_t *)Wave, sizeof(Wave)); vTaskDelay(50 / portTICK_PERIOD_MS); // 延时函数可以注释掉 } @@ -480,7 +481,7 @@ void RemoteControlOnDataReceive(const uint8_t *buffer, size_t size) cmd[0] = cmd[0] * 16; } // 选择一个函数使用 - RemoteControlInterruptMode22(&outputBuffer); // 使用结构体的成员方法 + RemoteControlInterruptMode22(&outputBuffer); // 使用结构体的成员方法 // RemoteControlInterruptMode33(cmd, data, outputBuffer.LEFT_x, outputBuffer.LEFT_y, outputBuffer.RIGHT_x, outputBuffer.RIGHT_y); // 使用 switch case 模式 } @@ -525,6 +526,17 @@ void getFunctionButtonValue(short *outputButton, char outputPosition, const char } } +// 截取字符串 +void substring(char *dest, const char *src, size_t start, size_t length) +{ + size_t i; + for (i = 0; i < length && src[start + i] != '\0'; ++i) + { + dest[i] = src[start + i]; + } + dest[i] = '\0'; // 确保字符串以空字符结尾 +} + /* * 遥控器的定时模式 * 功能:对一串有规律的字符串解析处理,放在指定结构体成员中 @@ -544,11 +556,30 @@ structRemoteControlFormat RemoteControlTimerMode(MODULE_SIZE RemoteControlLayout size_t data_len = strlen(Uart_getReceiveData()); // 获取长度用于复制 memcpy(tempBuffer, Uart_getReceiveData(), data_len); // 内存复制 + //// 尝试使用串口改变,手柄的组合,失败 + // if (tempBuffer[0] == 's' && tempBuffer[1] == 'e' && tempBuffer[2] == 't') + // { + // char *end; + // char tempBufferCopy[data_len]; + + // substring(tempBufferCopy, tempBuffer, 4, 4); // 截取字符串 + // tempBufferCopy[0] = (char)strtol(tempBufferCopy, &end, 16); // 字符串转换十六进制 + // // SerialBT.printf("%s-%d\n", "pad", tempBufferCopy[0]); + + // if (tempBufferCopy[0] == 0x00 || tempBufferCopy[0] == 0x01 || tempBufferCopy[0] == 0x10 || tempBufferCopy[0] == 0x11) + // { + // // handle = *tempBufferCopy; + // handle = tempBufferCopy[0]; + // tempBufferCopy[0] = 100; + // SerialBT.printf("%s-%d\n", "ok", tempBufferCopy[0]); + // } + // } + // 确定,方向按钮和摇杆组合方式 switch (handle) { case DIRECTIONAL_PAD: - // 获取结构体的首地址,在基础上移动到指定位置,进行赋值或者读取 + // 获取结构体的首地址,在基础上移动到指定位置,进行赋值或者读取操作 getDirectionalPad(&outputBuffer.begin, 1, tempBuffer, 0); getDirectionalPad(&outputBuffer.begin, 5, tempBuffer, 4); getFunctionButtonValue(&outputBuffer.begin, 10, tempBuffer, 8); @@ -597,6 +628,8 @@ void test_oscilloscope_send_wave() /** * 示波器发送和接收函数示例 + * 数据类型:字符串,通道数量:4,开始接收 + * 参数调试:开启定时发送,选择参数调整 */ void test_oscilloscope_send_and_Receive() { @@ -628,12 +661,13 @@ void test_remote_control_callback_enable(bool isEnable) } /** - * 遥控器的定时发送模式 - * + * 遥控器的定时发送模式,函数的参数决定摇杆和方向按钮组合 + * 摇杆:JOYSTICK_SIZE and 方向按钮:DIRECTIONAL_PAD_SIZE + * 手柄组合有4种,配合学会助手的遥控器功能一起使用 */ void test_remote_control_Timer_function() { - structRemoteControlFormat buffer = RemoteControlTimerMode(DIRECTIONAL_PAD_SIZE, JOYSTICK_SIZE); + structRemoteControlFormat buffer = RemoteControlTimerMode(DIRECTIONAL_PAD_SIZE, DIRECTIONAL_PAD_SIZE); short *ptr = &buffer.begin; for (size_t i = 0; i < 20, i < buffer.end; i++) @@ -641,7 +675,8 @@ void test_remote_control_Timer_function() Serial.printf("%d-%d ", i, ptr[i]); } Serial.println(); - delay(50); // 延时函数可以删除 + + // delay(50); // 延时函数可以删除 } /************************* example end *************************/ @@ -683,7 +718,7 @@ void test_struct_array() Serial.printf("%d-%d- ", i, *(ptr + i)); } Serial.printf("a: %d b: %d c: %d d: %d e: %d\n", box.a, box.b, box.c, box.d, box.e); - delay(100); + delay(10); } struct MyStruct diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h index 238b94d0..a7ea9dae 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h @@ -121,7 +121,7 @@ void Uart_SendByte(const uint8_t *byte); void Uart_SendArray(const uint8_t *array, size_t arraySize); // **发送特定格式数据**,如波形数据 -void XHZS_SendWave(const void *waveAddr, uint16_t waveSize); +void XHZS_SendWave(const uint8_t *waveAddr, uint16_t waveSize); /************************* 示波器相关函数 ************************/ diff --git a/PlatformIO_ESP32_oscilloscope/src/main.cpp b/PlatformIO_ESP32_oscilloscope/src/main.cpp index 9580ecc6..7bbfd76a 100644 --- a/PlatformIO_ESP32_oscilloscope/src/main.cpp +++ b/PlatformIO_ESP32_oscilloscope/src/main.cpp @@ -20,8 +20,8 @@ void setup() // // 启用串口通信的回调功能 // test_serial_port_callback_enable(true); - // 启用遥控器功能的回调 ok - test_remote_control_callback_enable(true); + // // 启用遥控器功能的回调 + // test_remote_control_callback_enable(true); // 初始化蓝牙模块,设置蓝牙名称为"mini_car" BTinitialize("mini_car"); @@ -29,9 +29,14 @@ void setup() void loop() { + // 测试发送波形数据到其他设备 + test_oscilloscope_send_wave(); + // // 执行示波器功能的数据发送和接收 // test_oscilloscope_send_and_Receive(); // // 遥控器的定时功能演示 // test_remote_control_Timer_function(); + + delay(10); } \ No newline at end of file -- Gitee From 770c23446829d41472e56b497f8538fabc482a96 Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Tue, 6 Aug 2024 17:24:26 +0800 Subject: [PATCH 06/18] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PlatformIO_ESP32_oscilloscope/.pio/build/project.checksum | 2 +- PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json | 6 ++++-- PlatformIO_ESP32_oscilloscope/platformio.ini | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/PlatformIO_ESP32_oscilloscope/.pio/build/project.checksum b/PlatformIO_ESP32_oscilloscope/.pio/build/project.checksum index 33530321..63e3322f 100644 --- a/PlatformIO_ESP32_oscilloscope/.pio/build/project.checksum +++ b/PlatformIO_ESP32_oscilloscope/.pio/build/project.checksum @@ -1 +1 @@ -8a68528b3a0642cb871621c8627966e55583f040 \ No newline at end of file +097eeba9cb2968a7879a3ff516436d31058be27a \ No newline at end of file diff --git a/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json b/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json index d944b690..e1a02ab6 100644 --- a/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json +++ b/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json @@ -12,6 +12,7 @@ "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/BluetoothSerial/src", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/FastLED/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/newlib/platform_include", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/freertos/include", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/freertos/include/esp_additions/freertos", @@ -208,6 +209,7 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/dio_qspi/include", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/cores/esp32", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/variants/esp32", + "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SPI/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/Interrupts_and_timers", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/WS2812B_LED", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ArduinoOTA/src", @@ -231,7 +233,6 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/RainMaker/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SD/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SD_MMC/src", - "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SPI/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SPIFFS/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SimpleBLE/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/Ticker/src", @@ -251,6 +252,7 @@ "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/BluetoothSerial/src", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/FastLED/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/newlib/platform_include", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/freertos/include", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/freertos/include/esp_additions/freertos", @@ -447,6 +449,7 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/dio_qspi/include", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/cores/esp32", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/variants/esp32", + "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SPI/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/Interrupts_and_timers", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/WS2812B_LED", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ArduinoOTA/src", @@ -470,7 +473,6 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/RainMaker/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SD/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SD_MMC/src", - "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SPI/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SPIFFS/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SimpleBLE/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/Ticker/src", diff --git a/PlatformIO_ESP32_oscilloscope/platformio.ini b/PlatformIO_ESP32_oscilloscope/platformio.ini index 9bf8b360..316e35ea 100644 --- a/PlatformIO_ESP32_oscilloscope/platformio.ini +++ b/PlatformIO_ESP32_oscilloscope/platformio.ini @@ -12,4 +12,5 @@ platform = espressif32 board = esp32dev framework = arduino -board_build.partitions = partitions.csv \ No newline at end of file +board_build.partitions = partitions.csv +lib_deps = fastled/FastLED@^3.7.0 -- Gitee From 22d28d53f33ca0a16b5bfe06ea16c9b00786889a Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Tue, 6 Aug 2024 09:50:07 +0000 Subject: [PATCH 07/18] =?UTF-8?q?update=20README.md.=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E9=81=A5=E6=8E=A7=E5=99=A8=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: klein1412 <1148878282@qq.com> --- README.md | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 39713223..5e15d360 100644 --- a/README.md +++ b/README.md @@ -1,42 +1,38 @@ # ESP32_oscilloscope #### 项目介绍 -这是一个将原来基于STM32F1的keil工程移植到ESP32中的项目,旨在实现与原工程一致的功能和逻辑。 -通讯方式采样蓝牙,本项目适配了“学会助手”软件的部分模块,工程适配工作在更新中,会有改动的地方,有其它问题,欢迎在群里提问 -或在 gitee 的 issue 中发起讨论 ^_^ +此项目旨在将原本基于STM32F1在Keil环境中开发的工程成功移植至ESP32平台,保持原有功能与逻辑的实现。通过蓝牙通信技术,项目已初步适配“学会助手”应用的若干功能模块,正处于工程迭代阶段。我们鼓励用户在遇到任何疑问时,积极在项目社群提问 ^_^ +##### 适配功能 - 串口功能 - 示波器功能 - - x... + - 遥控器功能 + - 待添加... #### 软件架构 -本项目采用ESP32微控制器,结合Arduino环境或PlatformIO进行开发 +采用ESP32微控制器为核心,项目支持在Arduino IDE或PlatformIO for VSCode环境下进行开发。 #### 安装教程 -1. 已经工程情况下,下载 myBluetTooth.h 头文件和 myBluetooth.cpp 源文件到工程文件夹中,在main.cpp中添加头文件 -2. xxxx +1. 现有工程整合:下载myBluetooth.h头文件及myBluetooth.cpp源码至项目目录。随后,在main.cpp顶部加入对myBluetooth.h的引用。 +2. 配置环境:请确保您的开发环境已正确配置为支持ESP32开发。 3. xxxx #### 使用说明 -1. 硬件:ESP32-WROOM-32E -2. 软件:Arduino IDE or PlatfromIO for vsCode -3. 在工程中包含 myBlueTooth.h 头文件(已经下载到本地) -4. myBlueTooth_头文件_有注释和_源文件_有测试案例 +1. 硬件需求:ESP32-WROOM-32E作为基础硬件平台 +2. 开发软件:推荐使用Arduino IDE或PlatformIO插件配合VSCode +4. 测试示例:库文件内含详细注释及测试用例,便于快速验证功能 #### 参与贡献 -0. 感谢来自 bilibili 的 一品芝麻糕 分享 thank you -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request +0. 感谢分享:Bilibili用户“一品芝麻糕”的分享与启发 +1. xxxx #### 特技 -1)神乎其技,爱折腾 -2)来,看你如何表演 +1)在路上,探索中... +2)xxxx -- Gitee From ddc812218880909e7013f0cdaa34439cfc1dd798 Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Sat, 17 Aug 2024 14:45:55 +0800 Subject: [PATCH 08/18] =?UTF-8?q?platfromIO=20=E6=B5=8B=E8=AF=95=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E9=97=AE=E9=A2=98=EF=BC=8C=E5=90=8C=E6=AD=A5=E5=88=B0?= =?UTF-8?q?=20arduino=20=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Arduino_ESP32_oscilloscope.ino | 67 +-- Arduino_ESP32_oscilloscope/myBlueTooth.cpp | 448 ++++++++++-------- Arduino_ESP32_oscilloscope/myBlueTooth.h | 159 ++++--- .../lib/myBlueTooth/myBlueTooth.cpp | 8 +- .../lib/myBlueTooth/myBlueTooth.h | 22 +- PlatformIO_ESP32_oscilloscope/src/main.cpp | 1 + 6 files changed, 384 insertions(+), 321 deletions(-) diff --git a/Arduino_ESP32_oscilloscope/Arduino_ESP32_oscilloscope.ino b/Arduino_ESP32_oscilloscope/Arduino_ESP32_oscilloscope.ino index 0cc68d8f..a9e7f7d9 100644 --- a/Arduino_ESP32_oscilloscope/Arduino_ESP32_oscilloscope.ino +++ b/Arduino_ESP32_oscilloscope/Arduino_ESP32_oscilloscope.ino @@ -1,60 +1,43 @@ /* -select engineering environment: ESP32_WROVER_Kit(all_versions) -想使用“学会助手”功能,查看 myBlueTooth.h 头文件里面有相关说明 -学会助手的串口和示波器功能,可以使用了 ^_^ +选择工程环境:ESP32_WROVER_Kit +如需使用“学会助手”功能,请参阅myBlueTooth.h头文件中的相关说明 +“学会助手”的串口功能、示波器功能和遥控器功能现已可用 ^_^ */ #include #include "myBlueTooth.h" +#include -BluetoothSerial SerialBlueTooth; +BluetoothSerial SerialBlueTooth; // 定义蓝牙串口对象 + +// 串口回调函数和遥控器回调函数不能同时使用 +// 回调函数和示波器和遥控器功能不能同时使用 void setup() { - Serial.begin(115200); // 开启串口通信:115200 + // 初始化串口通信,设置波特率为115200 + Serial.begin(115200); + + // // 启用串口通信的回调功能 + // test_serial_port_callback_enable(true); - // 此函数执行一次,可以了 - // 开启串口中断模式 - test_serial_port_callback_function(true); + // // 启用遥控器功能的回调 + // test_remote_control_callback_enable(true); - BTinitialize("mini_car"); // 蓝牙初始化 // 默认蓝牙名称:mini_bot + // 初始化蓝牙模块,设置蓝牙名称为"mini_car" + BTinitialize("mini_car"); } void loop() { - // ... - - delay(100); -} - - - - - - -/************************* test code area begin *************************/ - -// char *data = Uart_ReceiveData(); -// SerialBlueTooth.write((uint8_t *)data, strlen(data)); // 输出到连接的设备 -// SerialBlueTooth.println(); -// SerialBlueTooth.printf(data, strlen(data)); // 输出到连接的设备 -// SerialBlueTooth.println(); - -// delay(1000); - -// Serial.write(data, strlen(data)); // 输出到串行监视器 -// Serial.println(); -// Serial.printf(data, strlen(data)); // 输出到串行监视器 -// Serial.println(); - -// char ch[] = "hello"; - -// Uart_SendByte((uint8_t *)ch); - -// Uart_SendArray((uint8_t *)ch, sizeof(ch)); + // 测试发送波形数据到其他设备 + test_oscilloscope_send_wave(); -// SendWave(); + // // 执行示波器功能的数据发送和接收 + // test_oscilloscope_send_and_Receive(); -// test_oscilloscope_input_and_output(); + // // 遥控器的定时功能演示 + // test_remote_control_Timer_function(); -/************************* test code area end *************************/ \ No newline at end of file + delay(10); +} \ No newline at end of file diff --git a/Arduino_ESP32_oscilloscope/myBlueTooth.cpp b/Arduino_ESP32_oscilloscope/myBlueTooth.cpp index 9c1e5161..dd48703d 100644 --- a/Arduino_ESP32_oscilloscope/myBlueTooth.cpp +++ b/Arduino_ESP32_oscilloscope/myBlueTooth.cpp @@ -2,14 +2,15 @@ /************************* blueTooth part *************************/ BluetoothSerial SerialBT; // 蓝牙类实例化 -String BlueToothName = ""; // 蓝牙名称 static char blueToothBuffer[100]; // 接收缓冲区 /************************* XHZS part *************************/ -bool SERIAL_PORT_CALLBACK_FUNCTION_ENABLE = false; // 默认串口关闭 -char RemoteControlDataLength = 0; // 数据长度 -uint8_t DataFormat[8] = {0}; // 遥控器数据格式 +bool SERIAL_PORT_CALLBACK_ENABLE = false; // 默认串口关闭 +bool REMOTE_CONTROL_CALLBACK_ENABLE = false; // 默认遥控器关闭 +// 定义常量替代硬编码 +constexpr char DIGIT_ZERO = 0x30; +constexpr char DIGIT_ONE = 0x31; /************************* blueTooth begin *************************/ @@ -26,13 +27,16 @@ void BTinitialize(const String &deviceName) SerialBT.register_callback(Bluetooth_Event); // 设置事件回调函数 连接 断开 发送 接收 - if (SERIAL_PORT_CALLBACK_FUNCTION_ENABLE == true) + if (SERIAL_PORT_CALLBACK_ENABLE == true) { - SerialBT.onData(Uart_onDataReceive); // 测试手柄功能未完成_2024-07-27 - // SerialBT.onData(BTcallbackFunction); + SerialBT.onData(Uart_onDataReceive); + } + else if (REMOTE_CONTROL_CALLBACK_ENABLE == true) + { + SerialBT.onData(RemoteControlOnDataReceive); } SerialBT.begin(deviceName); // Bluetooth device name - BlueToothName = deviceName; + // BlueToothName = deviceName; } /*蓝牙发送浮点数据,转为字符串发送*/ @@ -119,12 +123,17 @@ void BTAuthCompleteCallback(boolean success) } /************************* blueTooth end *************************/ -// +// 函数前置声明 /************************* XHZS begin *************************/ -// 当通过UART接收到数据时,此函数将被调用 +/* + * 串口接收到数据开启调用,和stm32的中断函数一样 + * 需要在串口发送和接收实现自己逻辑代码,直接写代码逻辑 + * 回调函数不能有耗时操作,延时操作禁止使用的,会触发看门狗,不断重启 + */ void Uart_onDataReceive(const uint8_t *dataBuffer, size_t dataSize) { + // 判断接收到的数量 if (dataSize > 0) { SerialBT.write(dataBuffer, dataSize); // 输出到连接的设备 @@ -132,7 +141,7 @@ void Uart_onDataReceive(const uint8_t *dataBuffer, size_t dataSize) } } -// 获取接收到的数据 +// 获取串口接收到的数据 char *Uart_getReceiveData() { return blueToothBuffer; @@ -166,7 +175,7 @@ void Uart_SendArray(const uint8_t *array, size_t arraySize) *注意事项:示波器支持 uin16_t,建立一个 uint16_t fun[4] 数组, 元素是通道数量 **************************************************************/ -void XHZS_SendWave(const void *waveAddr, uint16_t waveSize) +void XHZS_SendWave(const uint8_t *waveAddr, uint16_t waveSize) { // 参数检查 if (waveAddr == nullptr || waveSize == 0) @@ -177,7 +186,7 @@ void XHZS_SendWave(const void *waveAddr, uint16_t waveSize) uint8_t cmd_tail[2] = {0xFC, 0x03}; // 帧尾 Uart_SendArray(cmd_head, sizeof(cmd_head)); // 发送帧头 - Uart_SendArray((uint8_t *)waveAddr, waveSize); // 发送数据 + Uart_SendArray(waveAddr, waveSize); // 发送数据 Uart_SendArray(cmd_tail, sizeof(cmd_tail)); // 发送帧尾 } @@ -191,7 +200,7 @@ void XHZS_SendWave(const void *waveAddr, uint16_t waveSize) void SendWave() { // // 输出平行线测试 start ----------------------- - // int32_t line[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 + // char line[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 // for (size_t i = 0; i < 500; i += 0.1) // { // line[0] = (line[0] > 10 ? -10 : (line[0] += 1)); @@ -199,8 +208,9 @@ void SendWave() // line[2] = 5.0; // line[3] = 0.0; - // XHZS_SendWave(line, sizeof(line)); - // vTaskDelay(50 / portTICK_PERIOD_MS); // 延时 100ms + // XHZS_SendWave((uint8_t *)line, sizeof(line)); + // vTaskDelay(50 / portTICK_PERIOD_MS); // 延时函数可以注释掉 + // // } // // 输出平行线测试 end ----------------------- @@ -213,9 +223,9 @@ void SendWave() Wave[1] = cos(i); Wave[2] = -sin(i); Wave[3] = -cos(i); - XHZS_SendWave(Wave, sizeof(Wave)); + XHZS_SendWave((uint8_t *)Wave, sizeof(Wave)); - vTaskDelay(50 / portTICK_PERIOD_MS); + vTaskDelay(50 / portTICK_PERIOD_MS); // 延时函数可以注释掉 } // 正弦波测试 end ----------------------- } @@ -300,8 +310,6 @@ char OscGetValue(const char *p, const char *name, float *value) return 2; } -/************************* XHZS end *************************/ - // 获取摇杆数据转换为 x and y void getInterruptJoystickvalue_XY(char *receiveBuffer, short *joystick_X, short *joystick_Y) { @@ -318,63 +326,31 @@ void getInterruptJoystickvalue_XY(char *receiveBuffer, short *joystick_X, short // cmd4 = receiveBuffer[3] & 0xF0; data[3] = receiveBuffer[3] & 0x0F; - // 对摇杆数值默认值做了修改,中心数值是0,偏离中心越远数值越大 0~100 + // 对摇杆数值默认值做了修改,中心数值是0,偏离中心越远数值越大 100~0~100 x = (data[0] * 16 + data[1]) - 100; *joystick_X = (x > 0) ? x : -x; y = (data[2] * 16 + data[3]) - 100; *joystick_Y = (y > 0) ? y : -y; } -// /************************************************************** -// *函数名称:test_remote_control -// *简 介:蓝牙数据接收,回调函数 -// *输 入:无 -// *输 出:默认输出按键状态 -// *注意事项:1)蓝牙接收到数据后,会调用此函数,不要在循环中使用,有卡死程序风险 -// 2)回调函数不能有耗时操作,延时操作禁止使用的,会触发看门狗,不断重启 -// 3)打印函数测试使用,注释掉写你的逻辑代码 -// **************************************************************/ - -void test_remote_control(const uint8_t *buffer, size_t size) +/* + * 遥控器的非定时模式 + * RemoteControlOnDataReceive() 前面的函数解析好了,遥控器的每个按键对应着一个 case, + * 在 case INTERRUPT_RIGHT_UP 表示,右边的方向按钮的向上按钮,根据按键的状态,写自己的逻辑代码 + * cmd[0] 遥控器的指令,data[0] 表示遥控器的状态,0 表示松开,1 表示按下,摇杆显示 x and y 数值 + */ +void RemoteControlInterruptMode33(uint8_t *cmd, uint8_t *data, short left_X, short left_Y, short right_X, short right_Y) { - uint8_t cmd[4] = {0}; - uint8_t data[4] = {0}; - - // 摇杆相关变量 - short left_X = 0, left_Y = 0, right_X = 0, right_Y = 0; - char receiveBuffer[4] = {0}; // 中断接收数据 - char temp = 0; - memcpy(receiveBuffer, buffer, size); - - // 按键接收大小为1,摇杆接收大小为4 - if (size == 4) - { - temp = (receiveBuffer[0] & 0xF0); - // cmd[0] = (temp == 0x10) ? 0x9A : ((temp == 0x50) ? 0X9B : 0x00); - // data[0] = receiveBuffer[0] & 0x0F; - if (temp == 0x10) - { - cmd[0] = 0x9A; - getInterruptJoystickvalue_XY(receiveBuffer, &left_X, &left_Y); - } - else if (temp == 0x50) - { - cmd[0] = 0x9C; - getInterruptJoystickvalue_XY(receiveBuffer, &right_X, &right_Y); - } - } - else if (size == 1) - { - cmd[0] = (receiveBuffer[0] & 0xF0); - data[0] = (receiveBuffer[0] & 0x0F); - } - - // 遥控器中断指令枚举定义 enum INTERRUPT_MODE_ORDER + // 在指定的按钮 case 中实现逻辑代码,中断不用有延时操作 switch (cmd[0]) { case INTERRUPT_LEFT_UP: // 指令 cmd[0] 数据 data[0] + // 逻辑代码区 begin + SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); + + // 逻辑代码区 end break; case INTERRUPT_LEFT_DOWN: @@ -405,17 +381,17 @@ void test_remote_control(const uint8_t *buffer, size_t size) SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); break; - case INTERRUPT_LEFT_X_Y: + case INTERRUPT_LEFT_X: // 摇杆数值在 left_X and left_Y - // 逻辑代码区 start + // 逻辑代码区 begin SerialBT.printf("x: %d y: %d\n", left_X, left_Y); // 逻辑代码区 end break; - case INTERRUPT_RIGHT_X_Y: - // 逻辑代码区 start + case INTERRUPT_RIGHT_X: + // 逻辑代码区 begin SerialBT.printf("x: %d y: %d\n", right_X, right_Y); @@ -447,58 +423,92 @@ void test_remote_control(const uint8_t *buffer, size_t size) break; } } + /* - * 遥控器的界面选择,使用 enum INTERFACE_SIZE + * RemoteControlOnDataReceive() 前面的函数解析好了,在这里使用结构体成员的方法,获取数值 + * 遥控器的非定时模式,1 表示按下,0 表示松开,摇杆显示 x and y 数值 */ -void XHZS_RemoteControlIntialization(INTERFACE_SIZE isDirectionalPad1, - INTERFACE_SIZE isDirectionalPad2, - INTERFACE_SIZE isButtonA, - INTERFACE_SIZE isButtonB, - INTERFACE_SIZE isButtonC, - INTERFACE_SIZE isButtonD, - INTERFACE_SIZE isSwitchE, - INTERFACE_SIZE isSwitchF) -{ - RemoteControlDataLength = isDirectionalPad1 + - isDirectionalPad2 + - isButtonA + - isButtonB + - isButtonC + - isButtonD + - isSwitchE + - isSwitchF; - DataFormat[0] = isDirectionalPad1; - DataFormat[1] = isDirectionalPad2; - DataFormat[2] = isButtonA; - DataFormat[3] = isButtonB; - DataFormat[4] = isButtonC; - DataFormat[5] = isButtonD; - DataFormat[6] = isSwitchE; - DataFormat[7] = isSwitchF; +void RemoteControlInterruptMode22(structRemoteControlFormat *structBuffer) +{ + // 遍历结构体成员代码,了解 struct structRemoteControlFormat 格式内容 + // structBuffer->A 和 ptr[10] 这两个表达都是同一个地址,他们位置都是:10 + short *ptr = &structBuffer->begin; // 获取结构体首地址 + for (size_t i = 0; i < structBuffer->end, i < 20; i++) + { + Serial.printf("%d-%d ", i, ptr[i]); + } + Serial.println(); +} + +/* + * 串口接收到数据开启调用,和stm32的中断函数一样,在 RemoteControlOnDataReceive() 函数实现数据解析工作, + * 你的逻辑代码建议在 RemoteControlInterruptMode22() 或者在 RemoteControlInterruptMode33() 实现 + * 回调函数不能有耗时操作,延时操作禁止使用的,会触发看门狗,不断重启 + * 函数功能:把不同的按钮指令,存放在指定的结构体中 + */ +void RemoteControlOnDataReceive(const uint8_t *buffer, size_t size) +{ + structRemoteControlFormat outputBuffer = {}; // 输出的缓冲区 + outputBuffer.end = 20; + uint8_t cmd[4] = {0}; // 指令 + uint8_t data[4] = {0}; // 数据 + + char receiveBuffer[4] = {0}; + memcpy(receiveBuffer, buffer, size); + short *ptr = &outputBuffer.begin; + + // 判断是否摇杆数据,在进行解释 + if (size == 4) + { + if ((receiveBuffer[0] & 0xF0) == 0x10) + { + cmd[0] = 0x9A; + getInterruptJoystickvalue_XY(receiveBuffer, &outputBuffer.LEFT_x, &outputBuffer.LEFT_y); + } + else if ((receiveBuffer[0] & 0xF0) == 0x50) + { + cmd[0] = 0x9C; + getInterruptJoystickvalue_XY(receiveBuffer, &outputBuffer.RIGHT_x, &outputBuffer.RIGHT_y); + } + } + else if (size == 1) + { + cmd[0] = (receiveBuffer[0] & 0xF0) / 16; + data[0] = (receiveBuffer[0] & 0x0F); + // 在这里把结构体当成一个数组来使用 + ptr[cmd[0]] = data[0]; + // switch case 模式 * 16 + cmd[0] = cmd[0] * 16; + } + // 选择一个函数使用 + // 使用结构体的成员方法 + RemoteControlInterruptMode22(&outputBuffer); + // 使用 switch case 模式 + // RemoteControlInterruptMode33(cmd, data, outputBuffer.LEFT_x, outputBuffer.LEFT_y, outputBuffer.RIGHT_x, outputBuffer.RIGHT_y); } // 获取摇杆数值 // (inputButton[inputPosition + 0] - 0x30) 把字符转化为数字 -void getJoystickValue(short *outputButton, char outputPosition, char *inputButton, char inputPosition) +void getJoystickValue(short *outputButton, char outputPosition, const char *inputButton, char inputPosition) { - outputButton[outputPosition + 0] = (inputButton[inputPosition + 0] - 0x30) * 100 + - (inputButton[inputPosition + 1] - 0x30) * 10 + - (inputButton[inputPosition + 2] - 0x30) - 100; - outputButton[outputPosition + 0] = ((outputButton[outputPosition + 0]) > 0 ? (outputButton[outputPosition + 0]) : -(outputButton[outputPosition + 0])); - - outputButton[outputPosition + 1] = (inputButton[inputPosition + 3] - 0x30) * 100 + - (inputButton[inputPosition + 4] - 0x30) * 10 + - (inputButton[inputPosition + 5] - 0x30) - 100; - outputButton[outputPosition + 1] = ((outputButton[outputPosition + 1]) > 0 ? (outputButton[outputPosition + 1]) : -(outputButton[outputPosition + 1])); + outputButton[outputPosition] = (inputButton[inputPosition] - DIGIT_ZERO) * 100 + + (inputButton[inputPosition + 1] - DIGIT_ZERO) * 10 + + (inputButton[inputPosition + 2] - DIGIT_ZERO) - 100; + outputButton[outputPosition] = outputButton[outputPosition] > 0 ? outputButton[outputPosition] : -outputButton[outputPosition]; + + outputButton[outputPosition + 1] = (inputButton[inputPosition + 3] - DIGIT_ZERO) * 100 + + (inputButton[inputPosition + 4] - DIGIT_ZERO) * 10 + + (inputButton[inputPosition + 5] - DIGIT_ZERO) - 100; + outputButton[outputPosition + 1] = outputButton[outputPosition + 1] > 0 ? outputButton[outputPosition + 1] : -outputButton[outputPosition + 1]; } // 获取按钮数值 // (inputButton[inputPosition + i] == 0x31) 识别字符是否等于数字的1 -void getDirectionalPad(short *outputButton, char outputPosition, char *inputButton, char inputPosition) +void getDirectionalPad(short *outputButton, char outputPosition, const char *inputButton, char inputPosition) { for (size_t i = 0; i < DIRECTIONAL_PAD_SIZE; i++) { - if (inputButton[inputPosition + i] == 0x31) + if (inputButton[inputPosition + i] == DIGIT_ONE) { outputButton[outputPosition + i] = 1; } @@ -507,113 +517,109 @@ void getDirectionalPad(short *outputButton, char outputPosition, char *inputButt // 获取按钮数值 // (inputButton[inputPosition + i] == 0x31) 识别字符是否等于数字的1 -void getFunctionButtonValue(short *outputButton, char outputPosition, char *inputButton, char inputPosition) +void getFunctionButtonValue(short *outputButton, char outputPosition, const char *inputButton, char inputPosition) { for (size_t i = 0; i < FUNCTION_BUTTON_SIZE; i++) { - if (inputButton[inputPosition + i] == 0x31) + if (inputButton[inputPosition + i] == DIGIT_ONE) { outputButton[outputPosition + i] = 1; } } } -// 遥控器循环模式 -// 数组 outputBuffer[20] 数据格式在枚举 enum LOOP_MODE_ORDER 中定义 -// 返回是数组类型,元素必须大于20个 -short XHZS_RemoteControlLoopMode() +// 截取字符串 +void substring(char *dest, const char *src, size_t start, size_t length) { - short outputBuffer[20] = {0}; // 输出的缓冲区 - char tempBuffer[20] = {0}; // 临时缓冲区 + size_t i; + for (i = 0; i < length && src[start + i] != '\0'; ++i) + { + dest[i] = src[start + i]; + } + dest[i] = '\0'; // 确保字符串以空字符结尾 +} + +/* + * 遥控器的定时模式 + * 功能:对一串有规律的字符串解析处理,放在指定结构体成员中 + * 使用了数组的方式,将结构体当成一个数组来使用,结构体的偏移量来确定成员位置 + */ +structRemoteControlFormat RemoteControlTimerMode(MODULE_SIZE RemoteControlLayoutLeft, MODULE_SIZE RemoteControlLayoutRight) +{ + structRemoteControlFormat outputBuffer = {0}; // 输出的缓冲区 + outputBuffer.begin = 0; + outputBuffer.end = 20; + char tempBuffer[20] = {0}; // 临时缓冲区 + // 判断手柄的方向按钮和摇杆的组合 - char handle = (DataFormat[0] == JOYSTICK_SIZE ? JOYSTICK_AND_DIRECTIONAL_PAD : TWO_DIRECTIONAL_PAD) + - (DataFormat[1] == JOYSTICK_SIZE ? DIRECTIONAL_PAD_AND_JOYSTICK : TWO_DIRECTIONAL_PAD); + char handle = (RemoteControlLayoutLeft == JOYSTICK_SIZE ? JOYSTICK_AND_DIRECTIONAL_PAD : DIRECTIONAL_PAD) + + (RemoteControlLayoutRight == JOYSTICK_SIZE ? DIRECTIONAL_PAD_AND_JOYSTICK : DIRECTIONAL_PAD); size_t data_len = strlen(Uart_getReceiveData()); // 获取长度用于复制 memcpy(tempBuffer, Uart_getReceiveData(), data_len); // 内存复制 + //// 尝试使用 cmd 串口改变,手柄的组合,失败 + // if (tempBuffer[0] == 's' && tempBuffer[1] == 'e' && tempBuffer[2] == 't') + // { + // char *end; + // char tempBufferCopy[data_len]; + + // substring(tempBufferCopy, tempBuffer, 4, 4); // 截取字符串 + // tempBufferCopy[0] = (char)strtol(tempBufferCopy, &end, 16); // 字符串转换十六进制 + // // SerialBT.printf("%s-%d\n", "pad", tempBufferCopy[0]); + + // if (tempBufferCopy[0] == 0x00 || tempBufferCopy[0] == 0x01 || tempBufferCopy[0] == 0x10 || tempBufferCopy[0] == 0x11) + // { + // // handle = *tempBufferCopy; + // handle = tempBufferCopy[0]; + // tempBufferCopy[0] = 100; + // SerialBT.printf("%s-%d\n", "ok", tempBufferCopy[0]); + // } + // } + // 确定,方向按钮和摇杆组合方式 switch (handle) { - case TWO_DIRECTIONAL_PAD: - getDirectionalPad(outputBuffer, 0, tempBuffer, 0); - getFunctionButtonValue(outputBuffer, 8, tempBuffer, 8); + case DIRECTIONAL_PAD: + // 获取结构体的首地址,在基础上移动到指定位置,进行赋值或者读取操作 + getDirectionalPad(&outputBuffer.begin, 1, tempBuffer, 0); + getDirectionalPad(&outputBuffer.begin, 5, tempBuffer, 4); + getFunctionButtonValue(&outputBuffer.begin, 10, tempBuffer, 8); break; case DIRECTIONAL_PAD_AND_JOYSTICK: - getDirectionalPad(outputBuffer, 0, tempBuffer, 0); - getJoystickValue(outputBuffer, 16, tempBuffer, 4); - getFunctionButtonValue(outputBuffer, 8, tempBuffer, 10); + getDirectionalPad(&outputBuffer.begin, 1, tempBuffer, 0); + getJoystickValue(&outputBuffer.begin, 18, tempBuffer, 4); + getFunctionButtonValue(&outputBuffer.begin, 10, tempBuffer, 10); break; case JOYSTICK_AND_DIRECTIONAL_PAD: - getJoystickValue(outputBuffer, 14, tempBuffer, 0); - getDirectionalPad(outputBuffer, 4, tempBuffer, 6); - getFunctionButtonValue(outputBuffer, 8, tempBuffer, 10); + getJoystickValue(&outputBuffer.begin, 16, tempBuffer, 0); + getDirectionalPad(&outputBuffer.begin, 5, tempBuffer, 6); + getFunctionButtonValue(&outputBuffer.begin, 10, tempBuffer, 10); break; - case TWO_JOYSTICKS: - getJoystickValue(outputBuffer, 14, tempBuffer, 0); - getJoystickValue(outputBuffer, 16, tempBuffer, 6); - getFunctionButtonValue(outputBuffer, 8, tempBuffer, 12); + case JOYSTICKS: + getJoystickValue(&outputBuffer.begin, 16, tempBuffer, 0); + getJoystickValue(&outputBuffer.begin, 18, tempBuffer, 6); + getFunctionButtonValue(&outputBuffer.begin, 10, tempBuffer, 12); break; } - return *outputBuffer; - - // #if true - // 不同的遥控器组合,使用不同显示 // 打印在串口监视器中 - switch (handle) - { - case TWO_DIRECTIONAL_PAD: - Serial.printf("up: %d down: %d left: %d right: %d - up: %d down: %d left: %d right: %d ", - outputBuffer[LOOP_LEFT_UP], outputBuffer[LOOP_LEFT_DOWN], outputBuffer[LOOP_LEFT_LEFT], outputBuffer[LOOP_LEFT_RIGHT], - outputBuffer[LOOP_RIGHT_UP], outputBuffer[LOOP_RIGHT_DOWN], outputBuffer[LOOP_RIGHT_LEFT], outputBuffer[LOOP_RIGHT_RIGHT]); - - break; - case DIRECTIONAL_PAD_AND_JOYSTICK: - Serial.printf("up: %d down: %d left: %d right: %d - x: %d y: %d ", - outputBuffer[LOOP_LEFT_UP], outputBuffer[LOOP_LEFT_DOWN], outputBuffer[LOOP_LEFT_LEFT], outputBuffer[LOOP_LEFT_RIGHT], - outputBuffer[LOOP_RIGHT_X], outputBuffer[LOOP_RIGHT_Y]); - break; - case JOYSTICK_AND_DIRECTIONAL_PAD: - Serial.printf(" x: %d y: %d - up: %d down: %d left: %d right: %d ", - outputBuffer[LOOP_LEFT_X], outputBuffer[LOOP_LEFT_Y], - outputBuffer[LOOP_RIGHT_UP], outputBuffer[LOOP_RIGHT_DOWN], outputBuffer[LOOP_RIGHT_LEFT], outputBuffer[LOOP_RIGHT_RIGHT]); - break; - case TWO_JOYSTICKS: - Serial.printf("x: %d y: %d - x: %d y: %d ", - outputBuffer[LOOP_LEFT_X], outputBuffer[LOOP_LEFT_Y], - outputBuffer[LOOP_RIGHT_X], outputBuffer[LOOP_RIGHT_Y]); - break; - } - Serial.printf(" A: %d B: %d C: %d D: %d E: %d F: %d \n", - outputBuffer[LOOP_BUTTON_A], - outputBuffer[LOOP_BUTTON_B], - outputBuffer[LOOP_BUTTON_C], - outputBuffer[LOOP_BUTTON_D], - outputBuffer[LOOP_BUTTON_E], - outputBuffer[LOOP_BUTTON_F]); - // #endif -} - -// 遥控器中断模式 -void XHZS_RemoteControlInterruptMode() -{ - // 准备整合 + return outputBuffer; } /************************* XHZS end *************************/ // -/************************* example funtion begin *************************/ +/************************* example begin *************************/ -// 开启串口回调函数,需要在蓝牙初始化之前调用 // isEnable = true 开启,false 关闭 -// 串口(蓝牙)接收到数据触发回调函数 Uart_onDataReceive() -// 底层 blueTooth 库中 onData() 函数绑定回调函数 Uart_onDataReceive() 函数作为触发调用 -// 找到 Uart_onDataReceive() 在里面写你的逻辑代码,不要有延时操作,中断函数中不能有延时操作 -// 在不使用串口功能时,请关闭防止与其它功能模块的冲突 -void test_serial_port_callback_function(bool isEnable) +/** + * 此函数应在蓝牙初始化前调用,当 isEnable 数值为 true 时开启,false 则禁用。 + * 在底层蓝牙库中,onData() 函数将绑定 Uart_onDataReceive() 作为其回调,从而在接收到数据时触发相应的处理逻辑。 + * 串口回调时实现自己的代码逻辑,找到 Uart_onDataReceive() 修改里面代码 + */ +void test_serial_port_callback_enable(bool isEnable) { - SERIAL_PORT_CALLBACK_FUNCTION_ENABLE = isEnable; + SERIAL_PORT_CALLBACK_ENABLE = isEnable; } // 发送波形数据的函数例子 @@ -622,14 +628,19 @@ void test_oscilloscope_send_wave() SendWave(); } -// 示波器输入和输出案例 -void test_oscilloscope_input_and_output() +/** + * 示波器发送和接收函数示例 + * 数据类型:字符串,通道数量:4,开始接收 + * 参数调试:开启定时发送,选择参数调整 + */ +void test_oscilloscope_send_and_Receive() { float floatValue1, floatValue2, floatValue3, floatValue4; // 准备数据类型 - char *charData = Uart_getReceiveData(); + char *charData = Uart_getReceiveData(); // 获取蓝牙串口的数据 + // 给通道四输出波形数据 floatValue4 = (floatValue4 > 33 ? -22 : (floatValue4 += 0.1)); - // 将字符串中的字段的数值提取出来 + // 将字符串中的字段的数值提取出来,字符串格式为:下方 // c11:14.12,c12:22,c13:33,c14: 55.66, // c13字段,数值为33 OscGetValue(charData, "c11", &floatValue1); @@ -638,11 +649,49 @@ void test_oscilloscope_input_and_output() 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); // 延时函数,可以注释掉 + delay(50); // 延时函数,可以注释掉 +} + +/* + * 此函数应在蓝牙初始化前调用,当 isEnable 数值为 true 时开启,false 则禁用。 + * 在底层蓝牙库中,onData() 函数将绑定 RemoteControlOnDataReceive() 作为其回调,从而在接收到数据时触发相应的处理逻辑。 + * 串口回调时实现自己的代码逻辑,找到 RemoteControlOnDataReceive() 修改里面代码 + */ +void test_remote_control_callback_enable(bool isEnable) +{ + REMOTE_CONTROL_CALLBACK_ENABLE = isEnable; +} + +/** + * 遥控器的定时发送模式,函数的参数决定摇杆和方向按钮组合 + * 摇杆:JOYSTICK_SIZE and 方向按钮:DIRECTIONAL_PAD_SIZE + * 手柄组合有4种,配合学会助手的遥控器功能一起使用 + */ +void test_remote_control_Timer_function() +{ + structRemoteControlFormat buffer = RemoteControlTimerMode(DIRECTIONAL_PAD_SIZE, DIRECTIONAL_PAD_SIZE); + + short *ptr = &buffer.begin; + for (size_t i = 0; i < 20, i < buffer.end; i++) + { + Serial.printf("%d-%d ", i, ptr[i]); + } + Serial.println(); + + // delay(50); // 延时函数可以删除 } -/************************* example funtion end *************************/ + +/************************* example end *************************/ // /************************* test begin *************************/ +#if false +/* +在处理十六进制的时候发现一个问题,如何把 0x10 and 0x20 转化成数字的 1 and 2, +第一想到的字节的移位操作,但是这样最少操作四次,一个字节八位, +在计算十六进制大小发现一个规律,高位每加一位相差15 +0x50 升一位需要加15,降一位需要15 +*/ + struct myStruct { char a; @@ -652,6 +701,11 @@ struct myStruct char e; }; +/* +如何把结构体像数组一样使用?可以做到,前提条件时是结构体成员是单一类型时,结构体内部就像一个数组, +获取结构体的首地址,在基础上进行偏移,可以像数组一样访问了 +问题来了,结构体的大小,或者说时长度问题,需要手动计算,使用for循环遍历警惕循环次数 +*/ void test_struct_array() { myStruct box = {}; @@ -666,22 +720,24 @@ void test_struct_array() Serial.printf("%d-%d- ", i, *(ptr + i)); } Serial.printf("a: %d b: %d c: %d d: %d e: %d\n", box.a, box.b, box.c, box.d, box.e); - delay(100); + delay(10); } -/* -struct MyStruct { +struct MyStruct +{ int a; int b; int c; // 假设a, b, c是连续存储的 }; - -MyStruct s; -int* ptr = &s.a; // 获取a的地址 -for (int i = 0; i < 3; ++i) { - *(ptr + i) = i; // 通过指针偏移访问,尽管能工作但不推荐 +void test_struct_move() +{ + MyStruct s; + int *ptr = &s.a; // 获取a的地址 + for (int i = 0; i < 3; ++i) + { + *(ptr + i) = i; // 通过指针偏移访问,尽管能工作但不推荐 + } } -*/ - +#endif /************************* test end *************************/ diff --git a/Arduino_ESP32_oscilloscope/myBlueTooth.h b/Arduino_ESP32_oscilloscope/myBlueTooth.h index 5f8096a2..38631171 100644 --- a/Arduino_ESP32_oscilloscope/myBlueTooth.h +++ b/Arduino_ESP32_oscilloscope/myBlueTooth.h @@ -9,57 +9,62 @@ #define MYBLUETOOTH_H_ #include -#include // 字符串操作库 -#include // ESP32蓝牙库 +#include +#include -// 遥控器循环指令枚举定义 -enum LOOP_MODE_ORDER +// 定义遥控器输出结构体 +struct structRemoteControlFormat { - LOOP_LEFT_UP = 0, // 左上方向 - LOOP_LEFT_DOWN = 1, // 左下方向 - LOOP_LEFT_LEFT = 2, // 左方向 - LOOP_LEFT_RIGHT = 3, // 右方向 - - LOOP_RIGHT_UP = 4, // 右上方向 - LOOP_RIGHT_DOWN = 5, // 右下方向 - LOOP_RIGHT_LEFT = 6, // 右左方向 - LOOP_RIGHT_RIGHT = 7, // 右右方向 - - LOOP_BUTTON_A = 8, // 按钮A - LOOP_BUTTON_B = 9, // 按钮B - LOOP_BUTTON_C = 10, // 按钮C - LOOP_BUTTON_D = 11, // 按钮D - LOOP_BUTTON_E = 12, // 按钮E - LOOP_BUTTON_F = 13, // 按钮F - - LOOP_LEFT_X = 14, // 左摇杆X轴 - LOOP_LEFT_Y = 15, // 左摇杆Y轴 - LOOP_RIGHT_X = 16, // 右摇杆X轴 - LOOP_RIGHT_Y = 17 // 右摇杆Y轴 + short begin; + + short LEFT_up; // 向上 // 1 + short LEFT_down; // 向下 + short LEFT_left; // 向左 + short LEFT_right; // 向右 + + short RIGHT_up; // 向上 // 5 + short RIGHT_down; // 向下 + short RIGHT_left; // 向左 + short RIGHT_right; // 向右 + + short middle; + + short A; // 按钮A // 10 + short B; // 按钮B + short C; // 按钮C + short D; // 按钮D + short E; // 按钮E + short F; // 按钮F + + short LEFT_x; // 左摇杆X轴 // 16 + short LEFT_y; // 左摇杆Y轴 + short RIGHT_x; // 右摇杆X轴 + short RIGHT_y; // 右摇杆Y轴 + + short end; // 结束标志位 // 20 }; // 遥控器中断指令枚举定义 enum INTERRUPT_MODE_ORDER { - // 按键状态指令 INTERRUPT_BUTTON_RELEASE = 0X00, // 按键释放指令 INTERRUPT_BUTTON_PRESS = 0X01, // 按键按下指令 - // 左侧方向键指令 INTERRUPT_LEFT_UP = 0X10, // 左侧方向键上 INTERRUPT_LEFT_DOWN = 0X20, // 左侧方向键下 INTERRUPT_LEFT_LEFT = 0X30, // 左侧方向键左 INTERRUPT_LEFT_RIGHT = 0X40, // 左侧方向键右 - // 右侧方向键指令 INTERRUPT_RIGHT_UP = 0X50, // 右侧方向键上 INTERRUPT_RIGHT_DOWN = 0X60, // 右侧方向键下 INTERRUPT_RIGHT_LEFT = 0X70, // 右侧方向键左 INTERRUPT_RIGHT_RIGHT = 0X80, // 右侧方向键右 - INTERRUPT_LEFT_X_Y = 0X9A, - INTERRUPT_RIGHT_X_Y = 0X9C, - // 左右功能按键指令 + INTERRUPT_LEFT_X = 0X9A, // 摇杆的指令 + INTERRUPT_LEFT_Y = 0X9B, + INTERRUPT_RIGHT_X = 0X9C, + INTERRUPT_RIGHT_Y = 0X9D, + INTERRUPT_BUTTON_A = 0XA0, // 左侧A功能键 INTERRUPT_BUTTON_B = 0XB0, // 左侧B功能键 INTERRUPT_BUTTON_C = 0XC0, // 右侧C功能键 @@ -74,88 +79,104 @@ typedef enum MODULE_SIZE JOYSTICK_SIZE = 6, // 手柄摇杆,占位6 DIRECTIONAL_PAD_SIZE = 4, // 方向按钮,占位4 FUNCTION_BUTTON_SIZE = 6, // 功能按键,占位6 - BUTTON_SIZE = 1, // 按键,占位1 - SWITCH_SIZE = 1 // 开关,占位1 + } INTERFACE_SIZE; // 遥控器的方向按钮和摇杆组合 enum POLYTYPE { - TWO_DIRECTIONAL_PAD = 0x00, // 0x00 + DIRECTIONAL_PAD = 0x00, // 0x00 DIRECTIONAL_PAD_AND_JOYSTICK = 0x01, // 0x01 JOYSTICK_AND_DIRECTIONAL_PAD = 0x10, // 0x10 - TWO_JOYSTICKS = 0x11 // 0x11 + JOYSTICKS = 0x11 // 0x11 }; -extern bool SERIAL_PORT_CALLBACK_FUNCTION_ENABLE; -/************************* 蓝牙功能核心接口 *************************/ +/************************* 蓝牙功能相关函数 *************************/ -// 设定蓝牙名称,默认为"mini_bot" +// **初始化蓝牙模块**,设置设备名,默认为"mini_bot" void BTinitialize(const String &deviceName = "mini_bot"); -// 将浮点数转换字符串通过蓝牙发送 +// **发送浮点数**通过蓝牙 void BTsendFloatVia(const float *data); -// 发送字符串到蓝牙连接的设备 +// **发送字符串**到蓝牙连接的设备 void BTsendStringVia(const char *stringData); -// 返回一个指针指向接收到的字符串数据。 +// **获取蓝牙接收的字符串** char *BTgetReceivedData(); -/************************* 高级数据处理接口 ************************/ +/************************* 串口通讯辅助函数 ************************/ -// 当通过UART接收到数据时,此函数将被调用 +// 串口数据接收回调,当有数据通过UART接收到时调用 void Uart_onDataReceive(const uint8_t *dataBuffer, size_t dataSize); -// 获取接收到的数据 +// 获取串口接收数据 char *Uart_getReceiveData(); -// 通过UART发送一个字节的数据 +// 发送单个字节,通过UART void Uart_SendByte(const uint8_t *byte); -// 通过UART发送一个字节数组 +// 发送字节数组,通过UART void Uart_SendArray(const uint8_t *array, size_t arraySize); -// 发送指定数据格式 -void XHZS_SendWave(const void *waveAddr, uint16_t waveSize); +// 发送特定格式数据,如波形数据 +void XHZS_SendWave(const uint8_t *waveAddr, uint16_t waveSize); + +/************************* 示波器相关函数 ************************/ // 发送波形数据的函数 void SendWave(); -// 输入 char *p 解析为 float *value +// 解析字符串为浮点数 char OscGetFloat(const char *p, float *value); -// 从示波器获取的数据格式,按字段解析为数值 +// 从特定格式数据中提取浮点数 char OscGetValue(const char *p, const char *name, float *value); -/************************* 演示示例函数 ************************/ +/************************* 遥控器交互相关函数 ************************/ + +// 遥控器数据接收处理回调 +void RemoteControlOnDataReceive(const uint8_t *buffer, size_t size); + +// 处理遥控器中断模式下的命令 +void RemoteControlInterruptMode22(structRemoteControlFormat *structBuffer); + +// 处理遥控器中断模式下的命令 // 备用 +void RemoteControlInterruptMode33(uint8_t *cmd, uint8_t *data, short left_X, short left_Y, short right_X, short right_Y); + +// 获取遥控器定时模式的数据 +structRemoteControlFormat RemoteControlTimerMode(MODULE_SIZE RemoteControlLayoutLeft, MODULE_SIZE RemoteControlLayoutRight); + +/************************* 示例与测试函数 ************************/ + +// 启用串口函数回调功能,在蓝牙初始化前调用 +void test_serial_port_callback_enable(bool isEnable = false); -void test_serial_port_callback_function(bool isEnable = false); // 开启串口回调函数,需要在蓝牙初始化之前调用 +// 测试发送波形数据到其他设备 +void test_oscilloscope_send_wave(); -void test_oscilloscope_send_wave(); // 发送波形数据的函数 +// 示例:示波器数据收发互动 +void test_oscilloscope_send_and_Receive(); -void test_oscilloscope_input_and_output(); // 示波器输入和输出案例 +// 启用遥控器函数回调函数,在蓝牙初始化前调用 +void test_remote_control_callback_enable(bool isEnable = false); -/************************* 其它模块适配,适配中... ************************/ +// 遥控器定时模式下的功能 +void test_remote_control_Timer_function(); -void test_remote_control(const uint8_t *buffer, size_t size); // 遥控器 +/************************* 功能测试,适配中... ************************/ -void XHZS_RemoteControlIntialization(INTERFACE_SIZE isDirectionalPad1, - INTERFACE_SIZE isDirectionalPad2, - INTERFACE_SIZE isButtonA = BUTTON_SIZE, - INTERFACE_SIZE isButtonB = BUTTON_SIZE, - INTERFACE_SIZE isButtonC = BUTTON_SIZE, - INTERFACE_SIZE isButtonD = BUTTON_SIZE, - INTERFACE_SIZE isSwitchE = SWITCH_SIZE, - INTERFACE_SIZE isSwitchF = SWITCH_SIZE); +// // 遥控器数据处理测试函数 +// void test_remote_control(const uint8_t *buffer, size_t size); -void XHZS_RemoteControlIntialization(); // 遥控器初始化 -// void XHZS_RemoteControlLoopMode(); // 遥控器循环模式 -void XHZS_RemoteControlInterruptMode(); // 遥控器中断模式 +// // 测试遥控器中断模式下的功能 +// void XHZS_RemoteControlInterruptMode(); -short XHZS_RemoteControlLoopMode(); +// // 结构体数组的使用示例 +// void test_struct_array(); -void test_struct_array(); +// // 结构体移动示例 +// void test_struct_move(); #endif diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp index 12775e41..dd48703d 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -481,8 +481,10 @@ void RemoteControlOnDataReceive(const uint8_t *buffer, size_t size) cmd[0] = cmd[0] * 16; } // 选择一个函数使用 - RemoteControlInterruptMode22(&outputBuffer); // 使用结构体的成员方法 - // RemoteControlInterruptMode33(cmd, data, outputBuffer.LEFT_x, outputBuffer.LEFT_y, outputBuffer.RIGHT_x, outputBuffer.RIGHT_y); // 使用 switch case 模式 + // 使用结构体的成员方法 + RemoteControlInterruptMode22(&outputBuffer); + // 使用 switch case 模式 + // RemoteControlInterruptMode33(cmd, data, outputBuffer.LEFT_x, outputBuffer.LEFT_y, outputBuffer.RIGHT_x, outputBuffer.RIGHT_y); } // 获取摇杆数值 @@ -556,7 +558,7 @@ structRemoteControlFormat RemoteControlTimerMode(MODULE_SIZE RemoteControlLayout size_t data_len = strlen(Uart_getReceiveData()); // 获取长度用于复制 memcpy(tempBuffer, Uart_getReceiveData(), data_len); // 内存复制 - //// 尝试使用串口改变,手柄的组合,失败 + //// 尝试使用 cmd 串口改变,手柄的组合,失败 // if (tempBuffer[0] == 's' && tempBuffer[1] == 'e' && tempBuffer[2] == 't') // { // char *end; diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h index a7ea9dae..38631171 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h @@ -41,7 +41,7 @@ struct structRemoteControlFormat short RIGHT_x; // 右摇杆X轴 short RIGHT_y; // 右摇杆Y轴 - short end; + short end; // 结束标志位 // 20 }; // 遥控器中断指令枚举定义 @@ -108,30 +108,30 @@ char *BTgetReceivedData(); /************************* 串口通讯辅助函数 ************************/ -// **串口数据接收回调**,当有数据通过UART接收到时调用 +// 串口数据接收回调,当有数据通过UART接收到时调用 void Uart_onDataReceive(const uint8_t *dataBuffer, size_t dataSize); -// **获取串口接收数据** +// 获取串口接收数据 char *Uart_getReceiveData(); -// **发送单个字节**通过UART +// 发送单个字节,通过UART void Uart_SendByte(const uint8_t *byte); -// **发送字节数组**通过UART +// 发送字节数组,通过UART void Uart_SendArray(const uint8_t *array, size_t arraySize); -// **发送特定格式数据**,如波形数据 +// 发送特定格式数据,如波形数据 void XHZS_SendWave(const uint8_t *waveAddr, uint16_t waveSize); /************************* 示波器相关函数 ************************/ -// **实际发送波形数据的函数** +// 发送波形数据的函数 void SendWave(); -// **解析字符串为浮点数** +// 解析字符串为浮点数 char OscGetFloat(const char *p, float *value); -// **从特定格式数据中提取浮点数** +// 从特定格式数据中提取浮点数 char OscGetValue(const char *p, const char *name, float *value); /************************* 遥控器交互相关函数 ************************/ @@ -162,10 +162,10 @@ void test_oscilloscope_send_and_Receive(); // 启用遥控器函数回调函数,在蓝牙初始化前调用 void test_remote_control_callback_enable(bool isEnable = false); -// 示例:遥控器定时模式下的功能 +// 遥控器定时模式下的功能 void test_remote_control_Timer_function(); -/************************* 其它模块适配,适配中... ************************/ +/************************* 功能测试,适配中... ************************/ // // 遥控器数据处理测试函数 // void test_remote_control(const uint8_t *buffer, size_t size); diff --git a/PlatformIO_ESP32_oscilloscope/src/main.cpp b/PlatformIO_ESP32_oscilloscope/src/main.cpp index 7bbfd76a..a9e7f7d9 100644 --- a/PlatformIO_ESP32_oscilloscope/src/main.cpp +++ b/PlatformIO_ESP32_oscilloscope/src/main.cpp @@ -12,6 +12,7 @@ BluetoothSerial SerialBlueTooth; // 定义蓝牙串口对象 // 串口回调函数和遥控器回调函数不能同时使用 // 回调函数和示波器和遥控器功能不能同时使用 + void setup() { // 初始化串口通信,设置波特率为115200 -- Gitee From 2cbe2817487072e495f170ed47bde36ba4f1f02d Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Sat, 17 Aug 2024 12:53:59 +0000 Subject: [PATCH 09/18] =?UTF-8?q?update=20README.md.=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=EF=BC=8C=E5=AE=8C=E5=96=84=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: klein1412 <1148878282@qq.com> --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5e15d360..175faa52 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # ESP32_oscilloscope #### 项目介绍 -此项目旨在将原本基于STM32F1在Keil环境中开发的工程成功移植至ESP32平台,保持原有功能与逻辑的实现。通过蓝牙通信技术,项目已初步适配“学会助手”应用的若干功能模块,正处于工程迭代阶段。我们鼓励用户在遇到任何疑问时,积极在项目社群提问 ^_^ +此项目旨在将原本基于STM32F1在Keil环境中开发的工程成功移植至ESP32芯片的arduino平台,保持原有功能与逻辑的实现。通过蓝牙通信功能,项目已初步适配“学会助手”应用的若干功能模块,正处于工程迭代阶段。我们鼓励用户在遇到任何疑问时,积极在项目社群提问 ^_^ ##### 适配功能 - 串口功能 @@ -29,10 +29,11 @@ 0. 感谢分享:Bilibili用户“一品芝麻糕”的分享与启发 1. xxxx +2. xxxx #### 特技 -1)在路上,探索中... -2)xxxx +- 在路上,探索中 ... +- 哈哈 ^_^ -- Gitee From 93956c5d58923b9bee168c8580d104c7edebe142 Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Sun, 18 Aug 2024 00:31:33 +0800 Subject: [PATCH 10/18] add wifi library ... --- .../.pio/build/project.checksum | 2 +- .../.vscode/c_cpp_properties.json | 28 +++--- .../lib/WS2812B_LED/WS2812B_LED.cpp | 64 ------------ .../lib/WS2812B_LED/WS2812B_LED.h | 23 ----- .../lib/myBlueTooth/myBlueTooth.cpp | 39 +++++++- .../lib/myBlueTooth/myBlueTooth.h | 1 + .../lib/myWiFi/myWiFi.cpp | 2 + .../lib/myWiFi/myWiFi.h | 11 +++ PlatformIO_ESP32_oscilloscope/platformio.ini | 2 +- PlatformIO_ESP32_oscilloscope/src/main.cpp | 98 +++++++++++++------ PlatformIO_ESP32_oscilloscope/src/main_BT.cpp | 44 +++++++++ 11 files changed, 177 insertions(+), 137 deletions(-) delete mode 100644 PlatformIO_ESP32_oscilloscope/lib/WS2812B_LED/WS2812B_LED.cpp delete mode 100644 PlatformIO_ESP32_oscilloscope/lib/WS2812B_LED/WS2812B_LED.h create mode 100644 PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp create mode 100644 PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h create mode 100644 PlatformIO_ESP32_oscilloscope/src/main_BT.cpp diff --git a/PlatformIO_ESP32_oscilloscope/.pio/build/project.checksum b/PlatformIO_ESP32_oscilloscope/.pio/build/project.checksum index 63e3322f..46b5dbb7 100644 --- a/PlatformIO_ESP32_oscilloscope/.pio/build/project.checksum +++ b/PlatformIO_ESP32_oscilloscope/.pio/build/project.checksum @@ -1 +1 @@ -097eeba9cb2968a7879a3ff516436d31058be27a \ No newline at end of file +1b4af5e5320823d4aa59c0376746c1577ffef38b \ No newline at end of file diff --git a/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json b/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json index e1a02ab6..c65f2902 100644 --- a/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json +++ b/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json @@ -10,9 +10,9 @@ "includePath": [ "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/include", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/src", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", - "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/BluetoothSerial/src", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/FastLED/src", + "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", + "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/FS/src", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/AsyncTCP/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/newlib/platform_include", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/freertos/include", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/freertos/include/esp_additions/freertos", @@ -209,19 +209,19 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/dio_qspi/include", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/cores/esp32", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/variants/esp32", - "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SPI/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/Interrupts_and_timers", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/WS2812B_LED", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myWiFi", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ArduinoOTA/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/BLE/src", + "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/BluetoothSerial/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/DNSServer/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/EEPROM/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ESP32/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ESPmDNS/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/Ethernet/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/FFat/src", - "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/FS/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/HTTPClient/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/HTTPUpdate/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/HTTPUpdateServer/src", @@ -233,13 +233,13 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/RainMaker/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SD/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SD_MMC/src", + "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SPI/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SPIFFS/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SimpleBLE/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/Ticker/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/USB/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/Update/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WebServer/src", - "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFiClientSecure/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFiProv/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/Wire/src", @@ -250,9 +250,9 @@ "path": [ "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/include", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/src", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", - "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/BluetoothSerial/src", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/FastLED/src", + "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", + "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/FS/src", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/AsyncTCP/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/newlib/platform_include", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/freertos/include", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/freertos/include/esp_additions/freertos", @@ -449,19 +449,19 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/dio_qspi/include", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/cores/esp32", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/variants/esp32", - "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SPI/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/Interrupts_and_timers", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/WS2812B_LED", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myWiFi", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ArduinoOTA/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/BLE/src", + "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/BluetoothSerial/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/DNSServer/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/EEPROM/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ESP32/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ESPmDNS/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/Ethernet/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/FFat/src", - "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/FS/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/HTTPClient/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/HTTPUpdate/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/HTTPUpdateServer/src", @@ -473,13 +473,13 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/RainMaker/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SD/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SD_MMC/src", + "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SPI/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SPIFFS/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/SimpleBLE/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/Ticker/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/USB/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/Update/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WebServer/src", - "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFiClientSecure/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFiProv/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/Wire/src", diff --git a/PlatformIO_ESP32_oscilloscope/lib/WS2812B_LED/WS2812B_LED.cpp b/PlatformIO_ESP32_oscilloscope/lib/WS2812B_LED/WS2812B_LED.cpp deleted file mode 100644 index cd7ba119..00000000 --- a/PlatformIO_ESP32_oscilloscope/lib/WS2812B_LED/WS2812B_LED.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include "WS2812B_LED.h" - -// Define the array of leds - -CRGB leds[NUM_LEDS]; -int delay_time = 500; - -bool isLed = true; -void ws2812b_led_init() -{ - - FastLED.addLeds(leds, NUM_LEDS); // GRB ordering is typical -} -void ws2812b_led_Blink() -{ - // leds[0] = CRGB::Green; - // FastLED.setBrightness(1); // 设置亮度 - // FastLED.show(); - // delay(delay_time); - // leds[0] = CRGB::Yellow; - // FastLED.setBrightness(1); // 设置亮度 - // FastLED.show(); - // delay(delay_time); - // // Turn the LED on, then pause - // leds[0] = CRGB::Blue; - // FastLED.setBrightness(1); // 设置亮度 - // FastLED.show(); - // delay(delay_time); - - // if (isLed == true) - // { - // leds[0] = CRGB::DarkGreen; - // FastLED.setBrightness(1); // 设置亮度 - // FastLED.show(); - // isLed = false; - // } - // else - // { - // leds[0] = CRGB::DarkRed; - // FastLED.setBrightness(1); // 设置亮度 - // FastLED.show(); - // isLed = true; - // } - - // isLed = !isLed; - - switch (isLed) - { - case true: - leds[0] = CRGB::DarkGreen; - FastLED.setBrightness(1); // 设置亮度 - FastLED.show(); - isLed = false; - delay(delay_time); - break; - case false: - leds[0] = CRGB::DarkRed; - FastLED.setBrightness(1); // 设置亮度 - FastLED.show(); - isLed = true; - delay(delay_time); - break; - } -} diff --git a/PlatformIO_ESP32_oscilloscope/lib/WS2812B_LED/WS2812B_LED.h b/PlatformIO_ESP32_oscilloscope/lib/WS2812B_LED/WS2812B_LED.h deleted file mode 100644 index 41ffa8dc..00000000 --- a/PlatformIO_ESP32_oscilloscope/lib/WS2812B_LED/WS2812B_LED.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef __WS2812B_LED_H__ -#define __WS2812B_LED_H__ -/* -ESP32-WROOM-32E 硬件 HSPI 引脚 -MISO 12 -MISI 13 -SCK 14 -SS 15 -*/ -#include -#include - -// How many leds in your strip? -#define NUM_LEDS 1 - -#define DATA_PIN 16 -#define CLOCK_PIN 14 - - -void ws2812b_led_init(); -void ws2812b_led_Blink(); - -#endif diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp index dd48703d..ac93ecfc 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -723,15 +723,44 @@ void test_struct_array() delay(10); } -struct MyStruct + + +/** + * \brief 说明。 + * + * \note namespace_name + */ +void new_function() +{ + Serial.println("new function"); +} + + +struct myColor { - int a; - int b; - int c; // 假设a, b, c是连续存储的 + int blue; + int green; + int yellow; // 假设a, b, c是连续存储的 }; +/** + * @brief 函数的功能简介。 + * + * 详细描述函数的作用、输入参数、返回值以及可能抛出的异常等。 + * + * @param[in] paramName 参数名 - 描述该参数的意义、类型及取值范围。 + * @param[out] paramName 输出参数 - 同上。 + * @return 返回值说明 - 描述函数返回值的含义及其类型。 + * + * @note 特别注意的地方,比如某些限制条件或行为说明。 + * @warning 警告信息,使用该函数需要注意的事项。 + * @pre 调用前的条件或准备工作。 + * @post 调用后的状态或效果。 + * + * @ingroup groupname 可选,函数所属的分组名。 + */ void test_struct_move() { - MyStruct s; + myColor s; int *ptr = &s.a; // 获取a的地址 for (int i = 0; i < 3; ++i) { diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h index e33945cb..e5c0c9d3 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h @@ -165,6 +165,7 @@ void test_remote_control_callback_enable(bool isEnable = false); // 遥控器定时模式下的功能 void test_remote_control_Timer_function(); + /************************* 功能测试,适配中... ************************/ // // 遥控器数据处理测试函数 diff --git a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp new file mode 100644 index 00000000..fcaf54c4 --- /dev/null +++ b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp @@ -0,0 +1,2 @@ +#include "myWiFi.h" + diff --git a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h new file mode 100644 index 00000000..8469d49a --- /dev/null +++ b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h @@ -0,0 +1,11 @@ +#ifndef MYWIFI_H +#define MYWIFI_H + +#include +#include +#include +#include "FS.h" + + +#endif + diff --git a/PlatformIO_ESP32_oscilloscope/platformio.ini b/PlatformIO_ESP32_oscilloscope/platformio.ini index 316e35ea..df08a3d0 100644 --- a/PlatformIO_ESP32_oscilloscope/platformio.ini +++ b/PlatformIO_ESP32_oscilloscope/platformio.ini @@ -13,4 +13,4 @@ platform = espressif32 board = esp32dev framework = arduino board_build.partitions = partitions.csv -lib_deps = fastled/FastLED@^3.7.0 +lib_deps = me-no-dev/AsyncTCP@^1.1.1 diff --git a/PlatformIO_ESP32_oscilloscope/src/main.cpp b/PlatformIO_ESP32_oscilloscope/src/main.cpp index 70687cae..ca110dbb 100644 --- a/PlatformIO_ESP32_oscilloscope/src/main.cpp +++ b/PlatformIO_ESP32_oscilloscope/src/main.cpp @@ -1,45 +1,85 @@ -/* -选择工程环境:ESP32_WROVER_Kit -如需使用“学会助手”功能,请参阅myBlueTooth.h头文件中的相关说明 -“学会助手”的串口功能、示波器功能和遥控器功能现已可用 ^_^ -*/ #include -#include "myBlueTooth.h" -#include +#include +#include +#include "FS.h" -BluetoothSerial SerialBlueTooth; // 定义蓝牙串口对象 +// 定义 Wi-Fi 热点的 SSID 和密码 +const char *ssid = "ESP32_AP"; +const char *password = "12345678"; +// 接收数据缓冲区 +char dataBuffer[64] = {}; -// 串口回调函数和遥控器回调函数不能同时使用 -// 回调函数和示波器和遥控器功能不能同时使用 +// AsyncTCP 服务器 +uint16_t ip_port = 80; +AsyncServer server(ip_port); -// 串口回调函数和遥控器回调函数不能同时使用 -// 回调函数和示波器和遥控器功能不能同时使用 -void setup() +void onClientConnect(void *arg, AsyncClient *client) { - // 初始化串口通信,设置波特率为115200 - Serial.begin(115200); + IPAddress remoteIP = client->remoteIP(); + Serial.printf("Client connected from IP: %s, port: %u\n", remoteIP.toString().c_str(), client->remotePort()); +} - // // 启用串口通信的回调功能 - // test_serial_port_callback_enable(true); +void onClientDisconnect(void *arg, AsyncClient *client) +{ + IPAddress remoteIP = client->remoteIP(); + Serial.printf("Client disconnected from IP: %s, port: %u\n", remoteIP.toString().c_str(), client->remotePort()); +} + +void onDataReceived(void *arg, AsyncClient *client, void *data, size_t len) +{ + len = (len >= 64 ? 64 : len); // 限制数据长度 - // // 启用遥控器功能的回调 - // test_remote_control_callback_enable(true); + memset(dataBuffer, '\0', sizeof(dataBuffer)); // 清空缓冲区 - // 初始化蓝牙模块,设置蓝牙名称为"mini_car" - BTinitialize("mini_car"); + memcpy(dataBuffer, data, len); // 将数据复制到缓冲区 + + Serial.printf("%s", dataBuffer); // 在串行监视器中打印信息 + + client->write(dataBuffer, strlen(dataBuffer)); // 回发数据连接端 } -void loop() +void onClientError(void *arg, AsyncClient *client, int8_t error) { - // 测试发送波形数据到其他设备 - test_oscilloscope_send_wave(); + Serial.printf("Client error: %s\n", client->errorToString(error)); +} - // // 执行示波器功能的数据发送和接收 - // test_oscilloscope_send_and_Receive(); +// 触发用户端事件回调函数 +void onClientEvent(void *arg, AsyncClient *client) +{ + client->onConnect(onClientConnect); + client->onDisconnect(onClientDisconnect); + client->onData(onDataReceived); + client->onError(onClientError); +} - // // 遥控器的定时功能演示 - // test_remote_control_Timer_function(); +void setup() +{ + memset(dataBuffer, '\0', sizeof(dataBuffer)); + + Serial.begin(115200); + + // 设置 ESP32 为 AP 模式 + WiFi.mode(WIFI_AP); + + // 启动 AP,并设置 SSID 和密码 + WiFi.softAP(ssid, password); + Serial.println("AP started"); - delay(10); + // 设置服务器回调 + server.onClient(onClientEvent, (void *)nullptr); + + server.begin(); + // 打印 IP 地址和端口号 + Serial.print("IP: "); + Serial.print(WiFi.softAPIP()); + Serial.printf(" port: %d\r\n", ip_port); + + Serial.println("Server started"); +} + +void loop() +{ + // 主循环中不需要手动检查连接或数据,AsyncTCP 会自动处理 + delay(1000); } \ No newline at end of file diff --git a/PlatformIO_ESP32_oscilloscope/src/main_BT.cpp b/PlatformIO_ESP32_oscilloscope/src/main_BT.cpp new file mode 100644 index 00000000..be928de2 --- /dev/null +++ b/PlatformIO_ESP32_oscilloscope/src/main_BT.cpp @@ -0,0 +1,44 @@ +// /* +// 选择工程环境:ESP32_WROVER_Kit +// 如需使用“学会助手”功能,请参阅myBlueTooth.h头文件中的相关说明 +// “学会助手”的串口功能、示波器功能和遥控器功能现已可用 ^_^ +// */ + +// #include +// #include "myBlueTooth.h" + +// BluetoothSerial SerialBlueTooth; // 定义蓝牙串口对象 + +// // 串口回调函数和遥控器回调函数不能同时使用 +// // 回调函数和示波器和遥控器功能不能同时使用 + +// // 串口回调函数和遥控器回调函数不能同时使用 +// // 回调函数和示波器和遥控器功能不能同时使用 +// void setup() +// { +// // 初始化串口通信,设置波特率为115200 +// Serial.begin(115200); + +// // // 启用串口通信的回调功能 +// // test_serial_port_callback_enable(true); + +// // // 启用遥控器功能的回调 +// // test_remote_control_callback_enable(true); + +// // 初始化蓝牙模块,设置蓝牙名称为"mini_car" +// BTinitialize("mini_car"); +// } + +// void loop() +// { +// // 测试发送波形数据到其他设备 +// test_oscilloscope_send_wave(); + +// // // 执行示波器功能的数据发送和接收 +// // test_oscilloscope_send_and_Receive(); + +// // // 遥控器的定时功能演示 +// // test_remote_control_Timer_function(); + +// delay(10); +// } \ No newline at end of file -- Gitee From fdea0665b850e0ad9db3b2223dc39a0491ca425e Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Tue, 20 Aug 2024 11:19:32 +0800 Subject: [PATCH 11/18] =?UTF-8?q?=E6=8A=8AblueTooth=20=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=EF=BC=8C=E6=8B=86=E5=88=86=E4=B8=89=E4=B8=AA?= =?UTF-8?q?=20=20blueTooth=20and=20wifi=20and=20XHZS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.pio/build/project.checksum | 2 +- .../.vscode/c_cpp_properties.json | 16 +-- .../.vscode/settings.json | 8 +- .../lib/myBlueTooth/myBlueTooth.cpp | 69 ++++++------ .../lib/myBlueTooth/myBlueTooth.h | 10 +- .../lib/myWiFi/myWiFi.cpp | 98 +++++++++++++++++ .../lib/myWiFi/myWiFi.h | 26 ++++- .../lib/myXHZS/myXHZS.cpp | 14 +++ .../lib/myXHZS/myXHZS.h | 10 ++ PlatformIO_ESP32_oscilloscope/src/main.cpp | 101 +++++++----------- PlatformIO_ESP32_oscilloscope/src/main_BT.cpp | 68 ++++++------ 11 files changed, 280 insertions(+), 142 deletions(-) create mode 100644 PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp create mode 100644 PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h diff --git a/PlatformIO_ESP32_oscilloscope/.pio/build/project.checksum b/PlatformIO_ESP32_oscilloscope/.pio/build/project.checksum index 46b5dbb7..03885d29 100644 --- a/PlatformIO_ESP32_oscilloscope/.pio/build/project.checksum +++ b/PlatformIO_ESP32_oscilloscope/.pio/build/project.checksum @@ -1 +1 @@ -1b4af5e5320823d4aa59c0376746c1577ffef38b \ No newline at end of file +63c018cba04d0320a446a25498d46e9a59099c94 \ No newline at end of file diff --git a/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json b/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json index c65f2902..d3822251 100644 --- a/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json +++ b/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json @@ -10,8 +10,12 @@ "includePath": [ "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/include", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/src", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myWiFi", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/FS/src", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", + "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/BluetoothSerial/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/AsyncTCP/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/newlib/platform_include", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/freertos/include", @@ -210,12 +214,10 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/cores/esp32", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/variants/esp32", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/Interrupts_and_timers", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myWiFi", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/Unity/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ArduinoOTA/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/BLE/src", - "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/BluetoothSerial/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/DNSServer/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/EEPROM/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ESP32/src", @@ -250,8 +252,12 @@ "path": [ "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/include", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/src", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myWiFi", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/FS/src", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", + "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/BluetoothSerial/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/AsyncTCP/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/newlib/platform_include", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/freertos/include", @@ -450,12 +456,10 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/cores/esp32", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/variants/esp32", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/Interrupts_and_timers", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myWiFi", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/Unity/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ArduinoOTA/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/BLE/src", - "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/BluetoothSerial/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/DNSServer/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/EEPROM/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ESP32/src", diff --git a/PlatformIO_ESP32_oscilloscope/.vscode/settings.json b/PlatformIO_ESP32_oscilloscope/.vscode/settings.json index 70cc27c7..98a26258 100644 --- a/PlatformIO_ESP32_oscilloscope/.vscode/settings.json +++ b/PlatformIO_ESP32_oscilloscope/.vscode/settings.json @@ -14,7 +14,13 @@ "sstream": "cpp", "streambuf": "cpp", "initializer_list": "cpp", - "utility": "cpp" + "utility": "cpp", + "functional": "cpp", + "type_traits": "cpp", + "deque": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp" }, "C_Cpp.errorSquiggles": "disabled" } \ No newline at end of file diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp index ac93ecfc..cb0259a4 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -6,37 +6,31 @@ static char blueToothBuffer[100]; // 接收缓冲区 /************************* XHZS part *************************/ -bool SERIAL_PORT_CALLBACK_ENABLE = false; // 默认串口关闭 -bool REMOTE_CONTROL_CALLBACK_ENABLE = false; // 默认遥控器关闭 +bool SERIAL_CALLBACK_FUNCTION_STATUS = true; +bool REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS = true; // 定义常量替代硬编码 constexpr char DIGIT_ZERO = 0x30; constexpr char DIGIT_ONE = 0x31; +bool SERIAL_EXECUTE_ONCE = false; +bool REMOTE_CONTROL_EXECUTE_ONCE = false; /************************* blueTooth begin *************************/ -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 BTinitialize(const String &deviceName) +/* 初始化蓝牙 */ +void BlueTooth_initialization(const String blueTooth_name) { - SerialBT.enableSSP(); // 在begin之前调用 + Serial.begin(115200); + // 蓝牙 spp 配对 begin + SerialBT.enableSSP(); SerialBT.onConfirmRequest(BTConfirmRequestCallback); SerialBT.onAuthComplete(BTAuthCompleteCallback); + // 蓝牙 spp 配对 end - SerialBT.register_callback(Bluetooth_Event); // 设置事件回调函数 连接 断开 发送 接收 + // 设置事件回调函数 连接 断开 发送 接收 + SerialBT.register_callback(Bluetooth_Event); + SerialBT.begin(blueTooth_name); - if (SERIAL_PORT_CALLBACK_ENABLE == true) - { - SerialBT.onData(Uart_onDataReceive); - } - else if (REMOTE_CONTROL_CALLBACK_ENABLE == true) - { - SerialBT.onData(RemoteControlOnDataReceive); - } - SerialBT.begin(deviceName); // Bluetooth device name - // BlueToothName = deviceName; + Serial.printf("Bluetooth name: %s\r\n", blueTooth_name); } /*蓝牙发送浮点数据,转为字符串发送*/ @@ -89,6 +83,7 @@ void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) // 蓝 { blueToothBuffer[index++] = SerialBT.read(); // 按字节存储 } + break; } // 数据发送标志 @@ -123,7 +118,7 @@ void BTAuthCompleteCallback(boolean success) } /************************* blueTooth end *************************/ -// 函数前置声明 +// /************************* XHZS begin *************************/ /* @@ -133,12 +128,8 @@ void BTAuthCompleteCallback(boolean success) */ void Uart_onDataReceive(const uint8_t *dataBuffer, size_t dataSize) { - // 判断接收到的数量 - if (dataSize > 0) - { - SerialBT.write(dataBuffer, dataSize); // 输出到连接的设备 - Serial.write(dataBuffer, dataSize); // 输出到串行监视器 - } + SerialBT.write(dataBuffer, dataSize); // 输出到连接的设备 + Serial.write(dataBuffer, dataSize); // 输出到串行监视器 } // 获取串口接收到的数据 @@ -185,9 +176,9 @@ void XHZS_SendWave(const uint8_t *waveAddr, uint16_t waveSize) uint8_t cmd_head[2] = {0x03, 0xFC}; // 帧头 uint8_t cmd_tail[2] = {0xFC, 0x03}; // 帧尾 - Uart_SendArray(cmd_head, sizeof(cmd_head)); // 发送帧头 - Uart_SendArray(waveAddr, waveSize); // 发送数据 - Uart_SendArray(cmd_tail, sizeof(cmd_tail)); // 发送帧尾 + Uart_SendArray(cmd_head, sizeof(cmd_head)); // 发送帧头 + Uart_SendArray(waveAddr, waveSize); // 发送数据 + Uart_SendArray(cmd_tail, sizeof(cmd_tail)); // 发送帧尾 } // /************************************************************** @@ -210,7 +201,7 @@ void SendWave() // XHZS_SendWave((uint8_t *)line, sizeof(line)); // vTaskDelay(50 / portTICK_PERIOD_MS); // 延时函数可以注释掉 - // + // // } // // 输出平行线测试 end ----------------------- @@ -482,9 +473,9 @@ void RemoteControlOnDataReceive(const uint8_t *buffer, size_t size) } // 选择一个函数使用 // 使用结构体的成员方法 - RemoteControlInterruptMode22(&outputBuffer); + RemoteControlInterruptMode22(&outputBuffer); // 使用 switch case 模式 - // RemoteControlInterruptMode33(cmd, data, outputBuffer.LEFT_x, outputBuffer.LEFT_y, outputBuffer.RIGHT_x, outputBuffer.RIGHT_y); + // RemoteControlInterruptMode33(cmd, data, outputBuffer.LEFT_x, outputBuffer.LEFT_y, outputBuffer.RIGHT_x, outputBuffer.RIGHT_y); } // 获取摇杆数值 @@ -619,7 +610,11 @@ structRemoteControlFormat RemoteControlTimerMode(MODULE_SIZE RemoteControlLayout */ void test_serial_port_callback_enable(bool isEnable) { - SERIAL_PORT_CALLBACK_ENABLE = isEnable; + if (SERIAL_CALLBACK_FUNCTION_STATUS == isEnable) + { + SerialBT.onData(Uart_onDataReceive); + SERIAL_CALLBACK_FUNCTION_STATUS = false; + } } // 发送波形数据的函数例子 @@ -659,7 +654,11 @@ void test_oscilloscope_send_and_Receive() */ void test_remote_control_callback_enable(bool isEnable) { - REMOTE_CONTROL_CALLBACK_ENABLE = isEnable; + if (REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS == isEnable) + { + SerialBT.onData(RemoteControlOnDataReceive); + REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS = false; + } } /** diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h index e5c0c9d3..d99f2cf8 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h @@ -91,11 +91,10 @@ enum POLYTYPE JOYSTICKS = 0x11 // 0x11 }; - /************************* 蓝牙功能相关函数 *************************/ -// **初始化蓝牙模块**,设置设备名,默认为"mini_bot" -void BTinitialize(const String &deviceName = "mini_bot"); +// 初始化蓝牙模块 +void BlueTooth_initialization(const String blueTooth_name = "esp32_blueTooth"); // **发送浮点数**通过蓝牙 void BTsendFloatVia(const float *data); @@ -106,6 +105,10 @@ void BTsendStringVia(const char *stringData); // **获取蓝牙接收的字符串** char *BTgetReceivedData(); +void BTConfirmRequestCallback(uint32_t numVal); // 认证请求回调 +void BTAuthCompleteCallback(boolean success); // 认证结果回调函数 +void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); // 蓝牙事件回调函数 + /************************* 串口通讯辅助函数 ************************/ // 串口数据接收回调,当有数据通过UART接收到时调用 @@ -165,7 +168,6 @@ void test_remote_control_callback_enable(bool isEnable = false); // 遥控器定时模式下的功能 void test_remote_control_Timer_function(); - /************************* 功能测试,适配中... ************************/ // // 遥控器数据处理测试函数 diff --git a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp index fcaf54c4..95c04424 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp @@ -1,2 +1,100 @@ #include "myWiFi.h" +// 定义常量替代硬编码 +// constexpr char DIGIT_ZERO = 0x30; +// constexpr char DIGIT_ONE = 0x31; + +WiFi_Controller::WiFi_Controller(uint16_t port) : _port(port), _server(port) +{ + _port = port; + _server(port); + _receiveDataBuffer[100] = {}; + _SERIAL_CALLBACK_FUNCTION_STATUS = false; + _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS = false; +} + +void WiFi_Controller::begin(const char *wifi_name, const char *wifi_password) +{ + Serial.begin(115200); + + // 设置 ESP32 为 AP 模式 + WiFi.mode(WIFI_AP); + + // 启动 AP,并设置 SSID 和密码 + WiFi.softAP(wifi_name, wifi_password); + + _server.begin(); + // 设置服务器回调 + _server.onClient([this](void *arg, AsyncClient *client) + { this->_onClientEvent(arg, client); }, (void *)this); + + // 打印 IP 地址和端口号 + Serial.print("IP: "); + Serial.print(WiFi.softAPIP()); + Serial.printf(" port: %d\r\n", _port); +} + +void WiFi_Controller::onClientConnect(void *arg, AsyncClient *client) +{ + client->write("Connection\r\n"); + Serial.write("Connection\r\n"); +} + +void WiFi_Controller::onClientDisconnect(void *arg, AsyncClient *client) +{ + client->write("disconnected\r\n"); + Serial.write("disconnected\r\n"); +} + +void WiFi_Controller::onDataReceived(void *arg, AsyncClient *client, void *data, size_t len) +{ + if (_SERIAL_CALLBACK_FUNCTION_STATUS == true) + { + client->write((char *)data, len); + // Serial.write((char *)data, len); // 中文显示不了 + Serial.print((char *)data); + } + if (_REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS == true) + { + client->write("_remote_control 2233"); + Serial.print("_remote_control 2233"); + } +} + +void WiFi_Controller::_onDataReceived(void *arg, AsyncClient *client, void *data, size_t len) +{ + len = (len >= 100 ? 100 : len); // 限制数据长度 + + memset(_receiveDataBuffer, '\0', sizeof(_receiveDataBuffer)); // 清空缓冲区 + + memcpy(_receiveDataBuffer, data, len); // 将数据复制到缓冲区 +} + +void WiFi_Controller::_onClientEvent(void *arg, AsyncClient *client) +{ + client->onConnect([this](void *arg, AsyncClient *client) + { this->onClientConnect(arg, client); }); + client->onDisconnect([this](void *arg, AsyncClient *client) + { this->onClientDisconnect(arg, client); }); + client->onData([this](void *arg, AsyncClient *client, void *data, size_t len) + { this->onDataReceived(arg, client, data, len); }); + + client->onData([this](void *arg, AsyncClient *client, void *data, size_t len) + { this->_onDataReceived(arg, client, data, len); }); +} + +void WiFi_Controller::setSerialCallbackFunctionStatus(bool isEnable) +{ + if (_SERIAL_CALLBACK_FUNCTION_STATUS == !isEnable) + { + _SERIAL_CALLBACK_FUNCTION_STATUS = isEnable; + } +} + +void WiFi_Controller::setRemoteControlCallbackFunctionStatus(bool isEnable) +{ + if (_REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS == !isEnable) + { + _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS = isEnable; + } +} diff --git a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h index 8469d49a..ba2635e3 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h @@ -6,6 +6,30 @@ #include #include "FS.h" +class WiFi_Controller +{ +public: + WiFi_Controller(uint16_t port = 80); + void begin(const char *wifi_name = "esp32_wifi", const char *wifi_password = "esp32esp32"); -#endif + void setSerialCallbackFunctionStatus(bool isEnable = false); + void setRemoteControlCallbackFunctionStatus(bool isEnable = false); + + void onClientConnect(void *arg, AsyncClient *client); + void onClientDisconnect(void *arg, AsyncClient *client); + void onDataReceived(void *arg, AsyncClient *client, void *data, size_t len); + +private: + char _receiveDataBuffer[100]; + uint16_t _port; + AsyncServer _server; + bool _SERIAL_CALLBACK_FUNCTION_STATUS; + bool _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS; + +private: + void _onDataReceived(void *arg, AsyncClient *client, void *data, size_t len); + void _onClientEvent(void *arg, AsyncClient *client); +}; + +#endif diff --git a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp new file mode 100644 index 00000000..31a52721 --- /dev/null +++ b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp @@ -0,0 +1,14 @@ +#include "myXHZS.h" + +WiFi_Controller server(80); + +void XHZS_initialization(void) +{ + server.begin("esp32_wifi_ap", "esp32esp32"); +} + +void uart_EnableSerialFunction(bool isEnable) +{ + server.setSerialCallbackFunctionStatus(isEnable); + server.setRemoteControlCallbackFunctionStatus(isEnable); +} diff --git a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h new file mode 100644 index 00000000..fb9a9869 --- /dev/null +++ b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h @@ -0,0 +1,10 @@ +#ifndef _MYXHZS_H_ +#define _MYXHZS_H_ + +#include "myBlueTooth.h" +#include "myWiFi.h" + +void XHZS_initialization(); +void uart_EnableSerialFunction(bool isEnable); + +#endif diff --git a/PlatformIO_ESP32_oscilloscope/src/main.cpp b/PlatformIO_ESP32_oscilloscope/src/main.cpp index ca110dbb..831747bc 100644 --- a/PlatformIO_ESP32_oscilloscope/src/main.cpp +++ b/PlatformIO_ESP32_oscilloscope/src/main.cpp @@ -1,85 +1,64 @@ +#if true -#include -#include -#include -#include "FS.h" - -// 定义 Wi-Fi 热点的 SSID 和密码 -const char *ssid = "ESP32_AP"; -const char *password = "12345678"; -// 接收数据缓冲区 -char dataBuffer[64] = {}; +#include "myXHZS.h" -// AsyncTCP 服务器 -uint16_t ip_port = 80; -AsyncServer server(ip_port); -void onClientConnect(void *arg, AsyncClient *client) +void setup() { - IPAddress remoteIP = client->remoteIP(); - Serial.printf("Client connected from IP: %s, port: %u\n", remoteIP.toString().c_str(), client->remotePort()); + XHZS_initialization(); + uart_EnableSerialFunction(true); } -void onClientDisconnect(void *arg, AsyncClient *client) +void loop() { - IPAddress remoteIP = client->remoteIP(); - Serial.printf("Client disconnected from IP: %s, port: %u\n", remoteIP.toString().c_str(), client->remotePort()); + delay(1000); } -void onDataReceived(void *arg, AsyncClient *client, void *data, size_t len) -{ - len = (len >= 64 ? 64 : len); // 限制数据长度 +#endif - memset(dataBuffer, '\0', sizeof(dataBuffer)); // 清空缓冲区 +#if false +/* +选择工程环境:ESP32_WROVER_Kit +如需使用“学会助手”功能,请参阅myBlueTooth.h头文件中的相关说明 +“学会助手”的串口功能、示波器功能和遥控器功能现已可用 ^_^ +*/ - memcpy(dataBuffer, data, len); // 将数据复制到缓冲区 - - Serial.printf("%s", dataBuffer); // 在串行监视器中打印信息 - - client->write(dataBuffer, strlen(dataBuffer)); // 回发数据连接端 -} +#include +#include "myBlueTooth.h" -void onClientError(void *arg, AsyncClient *client, int8_t error) -{ - Serial.printf("Client error: %s\n", client->errorToString(error)); -} +BluetoothSerial SerialBlueTooth; // 定义蓝牙串口对象 -// 触发用户端事件回调函数 -void onClientEvent(void *arg, AsyncClient *client) -{ - client->onConnect(onClientConnect); - client->onDisconnect(onClientDisconnect); - client->onData(onDataReceived); - client->onError(onClientError); -} +// 串口回调函数和遥控器回调函数不能同时使用 +// 回调函数和示波器和遥控器功能不能同时使用 +// 串口回调函数和遥控器回调函数不能同时使用 +// 回调函数和示波器和遥控器功能不能同时使用 void setup() { - memset(dataBuffer, '\0', sizeof(dataBuffer)); - + // 初始化串口通信,设置波特率为115200 Serial.begin(115200); - // 设置 ESP32 为 AP 模式 - WiFi.mode(WIFI_AP); - - // 启动 AP,并设置 SSID 和密码 - WiFi.softAP(ssid, password); - Serial.println("AP started"); - - // 设置服务器回调 - server.onClient(onClientEvent, (void *)nullptr); + // 启用串口通信的回调功能 + test_serial_port_callback_enable(true); - server.begin(); - // 打印 IP 地址和端口号 - Serial.print("IP: "); - Serial.print(WiFi.softAPIP()); - Serial.printf(" port: %d\r\n", ip_port); + // 启用遥控器功能的回调 + test_remote_control_callback_enable(true); - Serial.println("Server started"); + // 初始化蓝牙模块,设置蓝牙名称为"mini_car" + BTinitialize("mini_car"); } void loop() { - // 主循环中不需要手动检查连接或数据,AsyncTCP 会自动处理 - delay(1000); -} \ No newline at end of file + // 测试发送波形数据到其他设备 + test_oscilloscope_send_wave(); + + // 执行示波器功能的数据发送和接收 + test_oscilloscope_send_and_Receive(); + + // 遥控器的定时功能演示 + test_remote_control_Timer_function(); + + delay(10); +} +#endif diff --git a/PlatformIO_ESP32_oscilloscope/src/main_BT.cpp b/PlatformIO_ESP32_oscilloscope/src/main_BT.cpp index be928de2..1e6d89ac 100644 --- a/PlatformIO_ESP32_oscilloscope/src/main_BT.cpp +++ b/PlatformIO_ESP32_oscilloscope/src/main_BT.cpp @@ -1,44 +1,46 @@ -// /* -// 选择工程环境:ESP32_WROVER_Kit -// 如需使用“学会助手”功能,请参阅myBlueTooth.h头文件中的相关说明 -// “学会助手”的串口功能、示波器功能和遥控器功能现已可用 ^_^ -// */ +# if false +/* +选择工程环境:ESP32_WROVER_Kit +如需使用“学会助手”功能,请参阅myBlueTooth.h头文件中的相关说明 +“学会助手”的串口功能、示波器功能和遥控器功能现已可用 ^_^ +*/ -// #include -// #include "myBlueTooth.h" +#include +#include "myBlueTooth.h" -// BluetoothSerial SerialBlueTooth; // 定义蓝牙串口对象 +BluetoothSerial SerialBlueTooth; // 定义蓝牙串口对象 -// // 串口回调函数和遥控器回调函数不能同时使用 -// // 回调函数和示波器和遥控器功能不能同时使用 +// 串口回调函数和遥控器回调函数不能同时使用 +// 回调函数和示波器和遥控器功能不能同时使用 -// // 串口回调函数和遥控器回调函数不能同时使用 -// // 回调函数和示波器和遥控器功能不能同时使用 -// void setup() -// { -// // 初始化串口通信,设置波特率为115200 -// Serial.begin(115200); +// 串口回调函数和遥控器回调函数不能同时使用 +// 回调函数和示波器和遥控器功能不能同时使用 +void setup_() +{ + // 初始化串口通信,设置波特率为115200 + Serial.begin(115200); -// // // 启用串口通信的回调功能 -// // test_serial_port_callback_enable(true); + // 启用串口通信的回调功能 + test_serial_port_callback_enable(true); -// // // 启用遥控器功能的回调 -// // test_remote_control_callback_enable(true); + // 启用遥控器功能的回调 + test_remote_control_callback_enable(true); -// // 初始化蓝牙模块,设置蓝牙名称为"mini_car" -// BTinitialize("mini_car"); -// } + // 初始化蓝牙模块,设置蓝牙名称为"mini_car" + BTinitialize("mini_car"); +} -// void loop() -// { -// // 测试发送波形数据到其他设备 -// test_oscilloscope_send_wave(); +void loop_() +{ + // 测试发送波形数据到其他设备 + test_oscilloscope_send_wave(); -// // // 执行示波器功能的数据发送和接收 -// // test_oscilloscope_send_and_Receive(); + // 执行示波器功能的数据发送和接收 + test_oscilloscope_send_and_Receive(); -// // // 遥控器的定时功能演示 -// // test_remote_control_Timer_function(); + // 遥控器的定时功能演示 + test_remote_control_Timer_function(); -// delay(10); -// } \ No newline at end of file + delay(10); +} +#endif -- Gitee From 77f83f80a2e3eb1f80eb3830ae3b6d3d03ec73c1 Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Tue, 20 Aug 2024 16:51:46 +0800 Subject: [PATCH 12/18] =?UTF-8?q?=E5=AF=B9=20wifi=20=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E9=87=8D=E6=96=B0=E5=B0=81=E8=A3=85=EF=BC=8C=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E5=92=8C=20blueTooth=20=E6=A8=A1=E5=9D=97=EF=BC=8C=E6=9C=89?= =?UTF-8?q?=E7=9B=B8=E5=90=8C=E5=87=BD=E6=95=B0=E5=90=8D=E7=A7=B0=E5=86=B2?= =?UTF-8?q?=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.vscode/c_cpp_properties.json | 4 +- .../lib/myWiFi/myWiFi.cpp | 158 +++++++++++++----- .../lib/myWiFi/myWiFi.h | 54 +++++- .../lib/myXHZS/myXHZS.cpp | 17 +- .../lib/myXHZS/myXHZS.h | 2 +- PlatformIO_ESP32_oscilloscope/src/main.cpp | 22 ++- 6 files changed, 195 insertions(+), 62 deletions(-) diff --git a/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json b/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json index d3822251..529d3627 100644 --- a/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json +++ b/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json @@ -13,7 +13,6 @@ "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myWiFi", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", - "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/FS/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/BluetoothSerial/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/AsyncTCP/src", @@ -224,6 +223,7 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ESPmDNS/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/Ethernet/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/FFat/src", + "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/FS/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/HTTPClient/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/HTTPUpdate/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/HTTPUpdateServer/src", @@ -255,7 +255,6 @@ "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myWiFi", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", - "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/FS/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/BluetoothSerial/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/AsyncTCP/src", @@ -466,6 +465,7 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ESPmDNS/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/Ethernet/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/FFat/src", + "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/FS/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/HTTPClient/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/HTTPUpdate/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/HTTPUpdateServer/src", diff --git a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp index 95c04424..bd44d127 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp @@ -1,39 +1,119 @@ -#include "myWiFi.h" -// 定义常量替代硬编码 -// constexpr char DIGIT_ZERO = 0x30; -// constexpr char DIGIT_ONE = 0x31; +// #include "myWiFi.h" + +// WiFi_Controller::WiFi_Controller(uint16_t port) +// : _port(port), _server(port), _SERIAL_CALLBACK_FUNCTION_STATUS(false), _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS(false) +// { +// memset(_receiveDataBuffer, 0, sizeof(_receiveDataBuffer)); +// } + +// void WiFi_Controller::begin(const char *wifi_name, const char *wifi_password) +// { +// Serial.begin(115200); + +// // 设置 ESP32 为 AP 模式 +// WiFi.mode(WIFI_AP); + +// // 启动 AP,并设置 SSID 和密码 +// WiFi.softAP(wifi_name, wifi_password); + +// _server.begin(); +// // 设置服务器回调 +// _server.onClient(std::bind(&WiFi_Controller::onClientEvent, this, std::placeholders::_1, std::placeholders::_2), (void *)this); + +// // 打印 IP 地址和端口号 +// Serial.print("IP: "); +// Serial.print(WiFi.softAPIP()); +// Serial.printf(" port: %d\r\n", _port); +// } + +// void WiFi_Controller::onClientEvent(void *arg, AsyncClient *client) +// { +// client->onConnect([this](void *arg, AsyncClient *client) +// { this->onClientConnect(arg, client); }); +// client->onDisconnect([this](void *arg, AsyncClient *client) +// { this->onClientDisconnect(arg, client); }); +// client->onData([this](void *arg, AsyncClient *client, void *data, size_t len) +// { this->_onDataReceived(arg, client, data, len), this->onDataReceived(arg, client, data, len); }); +// } + +// void WiFi_Controller::onClientConnect(void *arg, AsyncClient *client) +// { +// client->write("Connection\r\n"); +// Serial.write("Connection\r\n"); +// } + +// void WiFi_Controller::onClientDisconnect(void *arg, AsyncClient *client) +// { +// client->write("disconnected\r\n"); +// Serial.write("disconnected\r\n"); +// } + +// void WiFi_Controller::onDataReceived(void *arg, AsyncClient *client, void *data, size_t len) +// { +// if (_SERIAL_CALLBACK_FUNCTION_STATUS) +// { +// client->write((char *)data, len); +// Serial.write((char *)data, len); +// } +// if (_REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS) +// { +// client->write("_remote_control 2233"); +// Serial.print("_remote_control 2233"); +// } +// } + +// void WiFi_Controller::_onDataReceived(void *arg, AsyncClient *client, void *data, size_t len) +// { +// len = (len >= 100 ? 100 : len); // 限制数据长度 + +// memset(_receiveDataBuffer, 0, sizeof(_receiveDataBuffer)); // 清空缓冲区 + +// memcpy(_receiveDataBuffer, data, len); // 将数据复制到缓冲区 +// } + +// void WiFi_Controller::setSerialCallbackFunctionStatus(bool isEnable) +// { +// _SERIAL_CALLBACK_FUNCTION_STATUS = isEnable; +// } + +// void WiFi_Controller::setRemoteControlCallbackFunctionStatus(bool isEnable) +// { +// _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS = isEnable; +// } +#include "myWiFi.h" -WiFi_Controller::WiFi_Controller(uint16_t port) : _port(port), _server(port) +WiFi_Controller::WiFi_Controller(uint16_t port) + : _port(port), _server(port), _SERIAL_CALLBACK_FUNCTION_STATUS(false), _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS(false), _onDataReceivedCallback(nullptr) { - _port = port; - _server(port); - _receiveDataBuffer[100] = {}; - _SERIAL_CALLBACK_FUNCTION_STATUS = false; - _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS = false; + memset(_receiveDataBuffer, 0, sizeof(_receiveDataBuffer)); } void WiFi_Controller::begin(const char *wifi_name, const char *wifi_password) { Serial.begin(115200); - // 设置 ESP32 为 AP 模式 WiFi.mode(WIFI_AP); - - // 启动 AP,并设置 SSID 和密码 WiFi.softAP(wifi_name, wifi_password); _server.begin(); - // 设置服务器回调 - _server.onClient([this](void *arg, AsyncClient *client) - { this->_onClientEvent(arg, client); }, (void *)this); + _server.onClient(std::bind(&WiFi_Controller::onClientEvent, this, std::placeholders::_1, std::placeholders::_2), (void *)this); - // 打印 IP 地址和端口号 Serial.print("IP: "); Serial.print(WiFi.softAPIP()); Serial.printf(" port: %d\r\n", _port); } +void WiFi_Controller::onClientEvent(void *arg, AsyncClient *client) +{ + client->onData([this](void *arg, AsyncClient *client, void *data, size_t len) + { this->onDataReceived(arg, client, data, len); }); + client->onConnect([this](void *arg, AsyncClient *client) + { this->onClientConnect(arg, client); }); + client->onDisconnect([this](void *arg, AsyncClient *client) + { this->onClientDisconnect(arg, client); }); +} + void WiFi_Controller::onClientConnect(void *arg, AsyncClient *client) { client->write("Connection\r\n"); @@ -48,13 +128,17 @@ void WiFi_Controller::onClientDisconnect(void *arg, AsyncClient *client) void WiFi_Controller::onDataReceived(void *arg, AsyncClient *client, void *data, size_t len) { - if (_SERIAL_CALLBACK_FUNCTION_STATUS == true) + // 调用回调函数 + if (_onDataReceivedCallback != nullptr) + { + _onDataReceivedCallback(arg, client, data, len); + } + if (_SERIAL_CALLBACK_FUNCTION_STATUS) { client->write((char *)data, len); - // Serial.write((char *)data, len); // 中文显示不了 - Serial.print((char *)data); + Serial.write((char *)data, len); } - if (_REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS == true) + if (_REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS) { client->write("_remote_control 2233"); Serial.print("_remote_control 2233"); @@ -63,38 +147,24 @@ void WiFi_Controller::onDataReceived(void *arg, AsyncClient *client, void *data, void WiFi_Controller::_onDataReceived(void *arg, AsyncClient *client, void *data, size_t len) { - len = (len >= 100 ? 100 : len); // 限制数据长度 + len = (len >= 100 ? 100 : len); - memset(_receiveDataBuffer, '\0', sizeof(_receiveDataBuffer)); // 清空缓冲区 + memset(_receiveDataBuffer, 0, sizeof(_receiveDataBuffer)); - memcpy(_receiveDataBuffer, data, len); // 将数据复制到缓冲区 + memcpy(_receiveDataBuffer, data, len); } -void WiFi_Controller::_onClientEvent(void *arg, AsyncClient *client) +void WiFi_Controller::setSerialCallbackFunctionStatus(bool isEnable) { - client->onConnect([this](void *arg, AsyncClient *client) - { this->onClientConnect(arg, client); }); - client->onDisconnect([this](void *arg, AsyncClient *client) - { this->onClientDisconnect(arg, client); }); - client->onData([this](void *arg, AsyncClient *client, void *data, size_t len) - { this->onDataReceived(arg, client, data, len); }); - - client->onData([this](void *arg, AsyncClient *client, void *data, size_t len) - { this->_onDataReceived(arg, client, data, len); }); + _SERIAL_CALLBACK_FUNCTION_STATUS = isEnable; } -void WiFi_Controller::setSerialCallbackFunctionStatus(bool isEnable) +void WiFi_Controller::setRemoteControlCallbackFunctionStatus(bool isEnable) { - if (_SERIAL_CALLBACK_FUNCTION_STATUS == !isEnable) - { - _SERIAL_CALLBACK_FUNCTION_STATUS = isEnable; - } + _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS = isEnable; } -void WiFi_Controller::setRemoteControlCallbackFunctionStatus(bool isEnable) +void WiFi_Controller::setOnDataReceivedCallback(DataReceivedCallback callback) { - if (_REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS == !isEnable) - { - _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS = isEnable; - } + _onDataReceivedCallback = callback; } diff --git a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h index ba2635e3..b6a5a759 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h @@ -1,23 +1,63 @@ + +// #ifndef MYWIFI_H +// #define MYWIFI_H + +// #include +// #include +// #include + +// class WiFi_Controller +// { +// public: +// WiFi_Controller(uint16_t port = 80); +// void begin(const char *wifi_name, const char *wifi_password); + +// void setSerialCallbackFunctionStatus(bool isEnable); +// void setRemoteControlCallbackFunctionStatus(bool isEnable); + +// void onClientConnect(void *arg, AsyncClient *client); +// void onClientDisconnect(void *arg, AsyncClient *client); +// void onDataReceived(void *arg, AsyncClient *client, void *data, size_t len); +// void onClientEvent(void *arg, AsyncClient *client); + +// private: +// char _receiveDataBuffer[100]; +// uint16_t _port; +// AsyncServer _server; + +// bool _SERIAL_CALLBACK_FUNCTION_STATUS; +// bool _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS; + +// void _onDataReceived(void *arg, AsyncClient *client, void *data, size_t len); +// }; + +// #endif + #ifndef MYWIFI_H #define MYWIFI_H #include #include #include -#include "FS.h" + +// 定义回调函数的类型 +typedef void (*DataReceivedCallback)(void *arg, AsyncClient *client, void *data, size_t len); class WiFi_Controller { public: WiFi_Controller(uint16_t port = 80); - void begin(const char *wifi_name = "esp32_wifi", const char *wifi_password = "esp32esp32"); + void begin(const char *wifi_name, const char *wifi_password); - void setSerialCallbackFunctionStatus(bool isEnable = false); - void setRemoteControlCallbackFunctionStatus(bool isEnable = false); + void setSerialCallbackFunctionStatus(bool isEnable); + void setRemoteControlCallbackFunctionStatus(bool isEnable); + + void setOnDataReceivedCallback(DataReceivedCallback callback); void onClientConnect(void *arg, AsyncClient *client); void onClientDisconnect(void *arg, AsyncClient *client); void onDataReceived(void *arg, AsyncClient *client, void *data, size_t len); + void onClientEvent(void *arg, AsyncClient *client); private: char _receiveDataBuffer[100]; @@ -27,9 +67,9 @@ private: bool _SERIAL_CALLBACK_FUNCTION_STATUS; bool _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS; -private: + DataReceivedCallback _onDataReceivedCallback; // 新增的回调函数指针 + void _onDataReceived(void *arg, AsyncClient *client, void *data, size_t len); - void _onClientEvent(void *arg, AsyncClient *client); }; -#endif +#endif \ No newline at end of file diff --git a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp index 31a52721..78de362c 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp @@ -1,14 +1,21 @@ #include "myXHZS.h" -WiFi_Controller server(80); +WiFi_Controller server; -void XHZS_initialization(void) +void XHZS_begin(const char *wifi_name, const char *wifi_password) { - server.begin("esp32_wifi_ap", "esp32esp32"); + server.begin(wifi_name, wifi_password); +} + +void XHZS_onDataReceived(void *arg, AsyncClient *client, void *data, size_t len) +{ + Serial.print("xhzs callback"); } void uart_EnableSerialFunction(bool isEnable) { - server.setSerialCallbackFunctionStatus(isEnable); - server.setRemoteControlCallbackFunctionStatus(isEnable); + AsyncClient *client; + server.onClientEvent((void * )nullptr, client); + // server.setSerialCallbackFunctionStatus(isEnable); + // server.setRemoteControlCallbackFunctionStatus(isEnable); } diff --git a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h index fb9a9869..53930326 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h @@ -4,7 +4,7 @@ #include "myBlueTooth.h" #include "myWiFi.h" -void XHZS_initialization(); +void XHZS_begin(const char *wifi_name, const char *wifi_password); void uart_EnableSerialFunction(bool isEnable); #endif diff --git a/PlatformIO_ESP32_oscilloscope/src/main.cpp b/PlatformIO_ESP32_oscilloscope/src/main.cpp index 831747bc..0141eb16 100644 --- a/PlatformIO_ESP32_oscilloscope/src/main.cpp +++ b/PlatformIO_ESP32_oscilloscope/src/main.cpp @@ -1,12 +1,28 @@ #if true -#include "myXHZS.h" +// #include "myXHZS.h" +#include "myWiFi.h" +WiFi_Controller wifiController; + +// 自定义的回调函数 +void customDataReceivedCallback(void *arg, AsyncClient *client, void *data, size_t len) +{ + // Serial.printf("%d-%s\n", len, (char *)data); + client->write((char *)data, len); + Serial.write((char *)data, len); +} void setup() { - XHZS_initialization(); - uart_EnableSerialFunction(true); + // XHZS_begin("esp32_ap", "esp32esp32"); + // uart_EnableSerialFunction(true); + + // 设置回调函数 + wifiController.setOnDataReceivedCallback(customDataReceivedCallback); + + // 开始 WiFi 控制器 + wifiController.begin("MyAP", "esp32esp32"); } void loop() -- Gitee From 5f77b4ccf65ff1d503f728ea80c6b4a58820ee91 Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Wed, 21 Aug 2024 00:06:19 +0800 Subject: [PATCH 13/18] =?UTF-8?q?=E5=AF=B9=20blueTooth=20and=20wifi=20?= =?UTF-8?q?=E5=88=86=E5=88=AB=E8=BF=9B=E8=A1=8C=E7=B1=BB=E5=B0=81=E8=A3=85?= =?UTF-8?q?=EF=BC=8C=E4=B8=A4=E4=B8=AA=E7=B1=BB=E5=8F=AF=E4=BB=A5=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=20serial=20port=20ouput?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.vscode/c_cpp_properties.json | 4 +- .../lib/myBlueTooth/myBlueTooth.cpp | 213 ++++++++++++++---- .../lib/myBlueTooth/myBlueTooth.h | 128 ++++++++++- .../lib/myWiFi/myWiFi.cpp | 110 +-------- .../lib/myWiFi/myWiFi.h | 41 +--- .../lib/myXHZS/myXHZS.cpp | 26 ++- PlatformIO_ESP32_oscilloscope/src/main.cpp | 23 +- 7 files changed, 332 insertions(+), 213 deletions(-) diff --git a/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json b/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json index 529d3627..4a65618f 100644 --- a/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json +++ b/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json @@ -10,7 +10,6 @@ "includePath": [ "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/include", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/src", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myWiFi", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", @@ -213,6 +212,7 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/cores/esp32", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/variants/esp32", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/Interrupts_and_timers", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/Unity/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ArduinoOTA/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src", @@ -252,7 +252,6 @@ "path": [ "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/include", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/src", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myWiFi", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", @@ -455,6 +454,7 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/cores/esp32", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/variants/esp32", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/Interrupts_and_timers", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/Unity/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ArduinoOTA/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src", diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp index cb0259a4..f73d52bb 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -1,7 +1,8 @@ #include "myBlueTooth.h" + +#if false /************************* blueTooth part *************************/ -BluetoothSerial SerialBT; // 蓝牙类实例化 static char blueToothBuffer[100]; // 接收缓冲区 /************************* XHZS part *************************/ @@ -21,14 +22,14 @@ void BlueTooth_initialization(const String blueTooth_name) { Serial.begin(115200); // 蓝牙 spp 配对 begin - SerialBT.enableSSP(); - SerialBT.onConfirmRequest(BTConfirmRequestCallback); - SerialBT.onAuthComplete(BTAuthCompleteCallback); + _serialBT.enableSSP(); + _serialBT.onConfirmRequest(BTConfirmRequestCallback); + _serialBT.onAuthComplete(BTAuthCompleteCallback); // 蓝牙 spp 配对 end // 设置事件回调函数 连接 断开 发送 接收 - SerialBT.register_callback(Bluetooth_Event); - SerialBT.begin(blueTooth_name); + _serialBT.register_callback(Bluetooth_Event); + _serialBT.begin(blueTooth_name); Serial.printf("Bluetooth name: %s\r\n", blueTooth_name); } @@ -36,15 +37,15 @@ void BlueTooth_initialization(const String blueTooth_name) /*蓝牙发送浮点数据,转为字符串发送*/ void BTsendFloatVia(const float *data) { - // 非字符串不要使用 SerialBT.write(),转换后会乱码 - SerialBT.printf("%.2f", *data); + // 非字符串不要使用 _serialBT.write(),转换后会乱码 + _serialBT.printf("%.2f", *data); } /*蓝牙发送字符串*/ void BTsendStringVia(const char *stringData) { // 在字符串中表现良好 - SerialBT.write((uint8_t *)stringData, strlen(stringData)); + _serialBT.write((uint8_t *)stringData, strlen(stringData)); } /*获取蓝牙发送内容*/ @@ -79,9 +80,9 @@ void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) // 蓝 case ESP_SPP_DATA_IND_EVT: { memset(blueToothBuffer, '\0', sizeof(blueToothBuffer)); - while (SerialBT.available()) // 等待接收完成 + while (_serialBT.available()) // 等待接收完成 { - blueToothBuffer[index++] = SerialBT.read(); // 按字节存储 + blueToothBuffer[index++] = _serialBT.read(); // 按字节存储 } break; @@ -104,8 +105,8 @@ void BTConfirmRequestCallback(uint32_t numVal) // 这里要对这个识别码进行判断,是否和主机一样或是是否是我们从机内置的密码 // 然后再判断是否确定连接,我们这里直接确认了 - SerialBT.confirmReply(true); - // SerialBT.confirmReply(false); // 如果要拒绝就用这句 + _serialBT.confirmReply(true); + // _serialBT.confirmReply(false); // 如果要拒绝就用这句 } // 认证结果回调函数 @@ -128,7 +129,7 @@ void BTAuthCompleteCallback(boolean success) */ void Uart_onDataReceive(const uint8_t *dataBuffer, size_t dataSize) { - SerialBT.write(dataBuffer, dataSize); // 输出到连接的设备 + _serialBT.write(dataBuffer, dataSize); // 输出到连接的设备 Serial.write(dataBuffer, dataSize); // 输出到串行监视器 } @@ -141,7 +142,7 @@ char *Uart_getReceiveData() // 发送一个字节 void Uart_SendByte(const uint8_t *byte) { - SerialBT.write(*byte); + _serialBT.write(*byte); } // 发送数组 @@ -153,7 +154,7 @@ void Uart_SendArray(const uint8_t *array, size_t arraySize) } else { - SerialBT.write(array, arraySize); // 数组发生是个耗时操作,建议DMA优化 + _serialBT.write(array, arraySize); // 数组发生是个耗时操作,建议DMA优化 } } @@ -339,44 +340,44 @@ void RemoteControlInterruptMode33(uint8_t *cmd, uint8_t *data, short left_X, sho // 指令 cmd[0] 数据 data[0] // 逻辑代码区 begin - SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); + _serialBT.printf("%X-%X\r\n", cmd[0], data[0]); // 逻辑代码区 end break; case INTERRUPT_LEFT_DOWN: - SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); + _serialBT.printf("%X-%X\r\n", cmd[0], data[0]); break; case INTERRUPT_LEFT_LEFT: - SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); + _serialBT.printf("%X-%X\r\n", cmd[0], data[0]); break; case INTERRUPT_LEFT_RIGHT: - SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); + _serialBT.printf("%X-%X\r\n", cmd[0], data[0]); break; case INTERRUPT_RIGHT_UP: - SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); + _serialBT.printf("%X-%X\r\n", cmd[0], data[0]); break; case INTERRUPT_RIGHT_DOWN: - SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); + _serialBT.printf("%X-%X\r\n", cmd[0], data[0]); break; case INTERRUPT_RIGHT_LEFT: - SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); + _serialBT.printf("%X-%X\r\n", cmd[0], data[0]); break; case INTERRUPT_RIGHT_RIGHT: - SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); + _serialBT.printf("%X-%X\r\n", cmd[0], data[0]); break; case INTERRUPT_LEFT_X: // 摇杆数值在 left_X and left_Y // 逻辑代码区 begin - SerialBT.printf("x: %d y: %d\n", left_X, left_Y); + _serialBT.printf("x: %d y: %d\n", left_X, left_Y); // 逻辑代码区 end break; @@ -384,33 +385,33 @@ void RemoteControlInterruptMode33(uint8_t *cmd, uint8_t *data, short left_X, sho case INTERRUPT_RIGHT_X: // 逻辑代码区 begin - SerialBT.printf("x: %d y: %d\n", right_X, right_Y); + _serialBT.printf("x: %d y: %d\n", right_X, right_Y); // 逻辑代码区 end break; case INTERRUPT_BUTTON_A: - SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); + _serialBT.printf("%X-%X\r\n", cmd[0], data[0]); break; case INTERRUPT_BUTTON_B: - SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); + _serialBT.printf("%X-%X\r\n", cmd[0], data[0]); break; case INTERRUPT_BUTTON_C: - SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); + _serialBT.printf("%X-%X\r\n", cmd[0], data[0]); break; case INTERRUPT_BUTTON_D: - SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); + _serialBT.printf("%X-%X\r\n", cmd[0], data[0]); break; case INTERRUPT_BUTTON_E: - SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); + _serialBT.printf("%X-%X\r\n", cmd[0], data[0]); break; case INTERRUPT_BUTTON_F: - SerialBT.printf("%X-%X\r\n", cmd[0], data[0]); + _serialBT.printf("%X-%X\r\n", cmd[0], data[0]); break; } } @@ -557,14 +558,14 @@ structRemoteControlFormat RemoteControlTimerMode(MODULE_SIZE RemoteControlLayout // substring(tempBufferCopy, tempBuffer, 4, 4); // 截取字符串 // tempBufferCopy[0] = (char)strtol(tempBufferCopy, &end, 16); // 字符串转换十六进制 - // // SerialBT.printf("%s-%d\n", "pad", tempBufferCopy[0]); + // // _serialBT.printf("%s-%d\n", "pad", tempBufferCopy[0]); // if (tempBufferCopy[0] == 0x00 || tempBufferCopy[0] == 0x01 || tempBufferCopy[0] == 0x10 || tempBufferCopy[0] == 0x11) // { // // handle = *tempBufferCopy; // handle = tempBufferCopy[0]; // tempBufferCopy[0] = 100; - // SerialBT.printf("%s-%d\n", "ok", tempBufferCopy[0]); + // _serialBT.printf("%s-%d\n", "ok", tempBufferCopy[0]); // } // } @@ -612,7 +613,7 @@ void test_serial_port_callback_enable(bool isEnable) { if (SERIAL_CALLBACK_FUNCTION_STATUS == isEnable) { - SerialBT.onData(Uart_onDataReceive); + _serialBT.onData(Uart_onDataReceive); SERIAL_CALLBACK_FUNCTION_STATUS = false; } } @@ -643,7 +644,7 @@ void test_oscilloscope_send_and_Receive() OscGetValue(charData, "c13", &floatValue3); OscGetValue(charData, "c14", &floatValue4); - SerialBT.printf("w:%0.2f,%0.2f,%0.2f,%0.2f\r\n", floatValue1, floatValue2, floatValue3, floatValue4); + _serialBT.printf("w:%0.2f,%0.2f,%0.2f,%0.2f\r\n", floatValue1, floatValue2, floatValue3, floatValue4); delay(50); // 延时函数,可以注释掉 } @@ -656,7 +657,7 @@ void test_remote_control_callback_enable(bool isEnable) { if (REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS == isEnable) { - SerialBT.onData(RemoteControlOnDataReceive); + _serialBT.onData(RemoteControlOnDataReceive); REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS = false; } } @@ -768,4 +769,140 @@ void test_struct_move() } #endif -/************************* test end *************************/ +#endif + +// #include "myBlueTooth.h" +BlueTooth_Controller::BlueTooth_Controller() : _serialBT(), DIGIT_ZERO(0x30), DIGIT_ONE(0x31) +{ + memset(_receiveBuffer, 0, sizeof(_receiveBuffer)); +} + +BlueTooth_Controller &BlueTooth_Controller::getInstance() +{ + static BlueTooth_Controller instance; // 静态局部变量 + return instance; +} + +void BlueTooth_Controller::begin(const char *BlueTooth_name) +{ + Serial.begin(115200); + // 蓝牙 spp 配对 begin + _serialBT.enableSSP(); + _serialBT.onConfirmRequest([this](uint32_t num_val) + { this->ConfirmRequestCallback(num_val); }); + _serialBT.onAuthComplete([this](boolean success) + { this->AuthCompleteCallback(success); }); + + // 蓝牙 spp 配对 end + + // 注册回调 + _serialBT.register_callback(&BlueTooth_Controller::Bluetooth_Event_Wrapper); + _serialBT.onData([this](const uint8_t *buffer, size_t size) + { this->onDataReceived(buffer, size); }); + _serialBT.begin(BlueTooth_name); + + Serial.printf("Bluetooth name: %s\r\n", BlueTooth_name); +}; + +void BlueTooth_Controller::sendFloatVia(const float *data) +{ + // 非字符串不要使用 _serialBT.write(),转换后会乱码 + _serialBT.printf("%.2f", *data); +} + +void BlueTooth_Controller::sendStringVia(const uint8_t *stringData) +{ + _serialBT.write(stringData, strlen((char *)stringData)); +} + +void BlueTooth_Controller::sendUint8_t(const uint8_t *buffer, size_t size) +{ + _serialBT.write(buffer, size); +} + +char *BlueTooth_Controller::getReceivedData() +{ + return _receiveBuffer; +} + +void BlueTooth_Controller::onDataCallback(BT_DataReceivedCallback callback) +{ + _onDataReceivedCallback = callback; +} + +void BlueTooth_Controller::onDataReceived(const uint8_t *buffer, size_t size) +{ + if (_onDataReceivedCallback != nullptr) + { + _onDataReceivedCallback(buffer, size); + } +} + +void BlueTooth_Controller::Bluetooth_Event_Wrapper(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) +{ + // 直接调用实例的方法 + getInstance().Bluetooth_Event(event, param); +} + +// 设置事件回调函数 连接 断开 发送 接收 +void BlueTooth_Controller::Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) +{ + switch (event) + { + // 蓝牙连接成功标志 + case ESP_SPP_SRV_OPEN_EVT: + case ESP_SPP_OPEN_EVT: + { + uint8_t connection[] = "connection successful!\r\n"; + sendStringVia(connection); + break; + } + // 蓝牙断开连接标志 + case ESP_SPP_CLOSE_EVT: + { + uint8_t disconnect[] = "disconnect successful!\r\n"; + sendStringVia(disconnect); + break; + } + // 数据接收标志 + case ESP_SPP_DATA_IND_EVT: + { + int index = 0; + memset(_receiveBuffer, '\0', sizeof(_receiveBuffer)); + while (_serialBT.available()) // 等待接收完成 + { + _receiveBuffer[index++] = _serialBT.read(); // 按字节存储 + } + + break; + } + // 数据发送标志 + case ESP_SPP_WRITE_EVT: + { + Serial.write("send OK!\r\n"); + break; + } + } +} + +// 认证请求回调 +void BlueTooth_Controller::ConfirmRequestCallback(uint32_t numVal) +{ + // numVal是主机发来的识别码 + Serial.printf("recv pin: %d \r\n", numVal); + + // 这里要对这个识别码进行判断,是否和主机一样或是是否是我们从机内置的密码 + // 然后再判断是否确定连接,我们这里直接确认了 + + _serialBT.confirmReply(true); + // _serialBT.confirmReply(false); // 如果要拒绝就用这句 +} + +// 认证结果回调函数 +void BlueTooth_Controller::AuthCompleteCallback(boolean success) +{ + if (success) + Serial.println("Pairing success!!"); + else + Serial.println("Pairing failed, rejected by user!!"); +} diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h index d99f2cf8..f6893350 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h @@ -1,10 +1,3 @@ -/* - * 文件: myBlueTooth.cpp 和 myBlueTooth.h - * - * 功能概述: 此代码提供了ESP32蓝牙操作的简易封装库,专为从机模式设计。 - * 实现了浮点数、字符串的蓝牙发送功能,并利用中断接收数据。 - */ - #ifndef MYBLUETOOTH_H_ #define MYBLUETOOTH_H_ @@ -82,6 +75,127 @@ typedef enum MODULE_SIZE } INTERFACE_SIZE; +// 遥控器的方向按钮和摇杆组合 +enum POLYTYPE +{ + DIRECTIONAL_PAD = 0x00, + DIRECTIONAL_PAD_AND_JOYSTICK = 0x01, + JOYSTICK_AND_DIRECTIONAL_PAD = 0x10, + JOYSTICKS = 0x11 +}; + +typedef void (*BT_DataReceivedCallback)(const uint8_t *buffer, size_t size); + +class BlueTooth_Controller +{ +public: + static BlueTooth_Controller &getInstance(); // 单例获取方法 + BlueTooth_Controller(); + void begin(const char *BlueTooth_name); + + void sendFloatVia(const float *data); + void sendStringVia(const uint8_t *stringData); + void sendUint8_t(const uint8_t *buffer, size_t size); + char *getReceivedData(); + + void onDataCallback(BT_DataReceivedCallback callback); // 新增的设置回调函数 + void onDataReceived(const uint8_t *buffer, size_t size); + + static void Bluetooth_Event_Wrapper(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); + void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); + +private: + char _receiveBuffer[100]; + BluetoothSerial _serialBT; + + char DIGIT_ZERO; + char DIGIT_ONE; + + BT_DataReceivedCallback _onDataReceivedCallback; // 新增的回调函数指针 + +private: + void ConfirmRequestCallback(uint32_t numVal); + void AuthCompleteCallback(boolean success); +}; + +#endif + +#if falae + +#include +#include +#include + +// 定义遥控器输出结构体 +struct structRemoteControlFormat +{ + short begin; + + short LEFT_up; // 向上 // 1 + short LEFT_down; // 向下 + short LEFT_left; // 向左 + short LEFT_right; // 向右 + + short RIGHT_up; // 向上 // 5 + short RIGHT_down; // 向下 + short RIGHT_left; // 向左 + short RIGHT_right; // 向右 + + short middle; + + short A; // 按钮A // 10 + short B; // 按钮B + short C; // 按钮C + short D; // 按钮D + short E; // 按钮E + short F; // 按钮F + + short LEFT_x; // 左摇杆X轴 // 16 + short LEFT_y; // 左摇杆Y轴 + short RIGHT_x; // 右摇杆X轴 + short RIGHT_y; // 右摇杆Y轴 + + short end; // 结束标志位 // 20 +}; + +// 遥控器中断指令枚举定义 +enum INTERRUPT_MODE_ORDER +{ + INTERRUPT_BUTTON_RELEASE = 0X00, // 按键释放指令 + INTERRUPT_BUTTON_PRESS = 0X01, // 按键按下指令 + + INTERRUPT_LEFT_UP = 0X10, // 左侧方向键上 + INTERRUPT_LEFT_DOWN = 0X20, // 左侧方向键下 + INTERRUPT_LEFT_LEFT = 0X30, // 左侧方向键左 + INTERRUPT_LEFT_RIGHT = 0X40, // 左侧方向键右 + + INTERRUPT_RIGHT_UP = 0X50, // 右侧方向键上 + INTERRUPT_RIGHT_DOWN = 0X60, // 右侧方向键下 + INTERRUPT_RIGHT_LEFT = 0X70, // 右侧方向键左 + INTERRUPT_RIGHT_RIGHT = 0X80, // 右侧方向键右 + + INTERRUPT_LEFT_X = 0X9A, // 摇杆的指令 + INTERRUPT_LEFT_Y = 0X9B, + INTERRUPT_RIGHT_X = 0X9C, + INTERRUPT_RIGHT_Y = 0X9D, + + INTERRUPT_BUTTON_A = 0XA0, // 左侧A功能键 + INTERRUPT_BUTTON_B = 0XB0, // 左侧B功能键 + INTERRUPT_BUTTON_C = 0XC0, // 右侧C功能键 + INTERRUPT_BUTTON_D = 0XD0, // 右侧D功能键 + INTERRUPT_BUTTON_E = 0XE0, // 中间开关E + INTERRUPT_BUTTON_F = 0XF0 // 中间开关F +}; + +// 遥控器的模块大小 +typedef enum MODULE_SIZE +{ + JOYSTICK_SIZE = 6, // 手柄摇杆,占位6 + DIRECTIONAL_PAD_SIZE = 4, // 方向按钮,占位4 + FUNCTION_BUTTON_SIZE = 6, // 功能按键,占位6 + +} INTERFACE_SIZE; + // 遥控器的方向按钮和摇杆组合 enum POLYTYPE { diff --git a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp index bd44d127..0de9ecd2 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp @@ -1,92 +1,10 @@ - -// #include "myWiFi.h" - -// WiFi_Controller::WiFi_Controller(uint16_t port) -// : _port(port), _server(port), _SERIAL_CALLBACK_FUNCTION_STATUS(false), _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS(false) -// { -// memset(_receiveDataBuffer, 0, sizeof(_receiveDataBuffer)); -// } - -// void WiFi_Controller::begin(const char *wifi_name, const char *wifi_password) -// { -// Serial.begin(115200); - -// // 设置 ESP32 为 AP 模式 -// WiFi.mode(WIFI_AP); - -// // 启动 AP,并设置 SSID 和密码 -// WiFi.softAP(wifi_name, wifi_password); - -// _server.begin(); -// // 设置服务器回调 -// _server.onClient(std::bind(&WiFi_Controller::onClientEvent, this, std::placeholders::_1, std::placeholders::_2), (void *)this); - -// // 打印 IP 地址和端口号 -// Serial.print("IP: "); -// Serial.print(WiFi.softAPIP()); -// Serial.printf(" port: %d\r\n", _port); -// } - -// void WiFi_Controller::onClientEvent(void *arg, AsyncClient *client) -// { -// client->onConnect([this](void *arg, AsyncClient *client) -// { this->onClientConnect(arg, client); }); -// client->onDisconnect([this](void *arg, AsyncClient *client) -// { this->onClientDisconnect(arg, client); }); -// client->onData([this](void *arg, AsyncClient *client, void *data, size_t len) -// { this->_onDataReceived(arg, client, data, len), this->onDataReceived(arg, client, data, len); }); -// } - -// void WiFi_Controller::onClientConnect(void *arg, AsyncClient *client) -// { -// client->write("Connection\r\n"); -// Serial.write("Connection\r\n"); -// } - -// void WiFi_Controller::onClientDisconnect(void *arg, AsyncClient *client) -// { -// client->write("disconnected\r\n"); -// Serial.write("disconnected\r\n"); -// } - -// void WiFi_Controller::onDataReceived(void *arg, AsyncClient *client, void *data, size_t len) -// { -// if (_SERIAL_CALLBACK_FUNCTION_STATUS) -// { -// client->write((char *)data, len); -// Serial.write((char *)data, len); -// } -// if (_REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS) -// { -// client->write("_remote_control 2233"); -// Serial.print("_remote_control 2233"); -// } -// } - -// void WiFi_Controller::_onDataReceived(void *arg, AsyncClient *client, void *data, size_t len) -// { -// len = (len >= 100 ? 100 : len); // 限制数据长度 - -// memset(_receiveDataBuffer, 0, sizeof(_receiveDataBuffer)); // 清空缓冲区 - -// memcpy(_receiveDataBuffer, data, len); // 将数据复制到缓冲区 -// } - -// void WiFi_Controller::setSerialCallbackFunctionStatus(bool isEnable) -// { -// _SERIAL_CALLBACK_FUNCTION_STATUS = isEnable; -// } - -// void WiFi_Controller::setRemoteControlCallbackFunctionStatus(bool isEnable) -// { -// _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS = isEnable; -// } #include "myWiFi.h" + WiFi_Controller::WiFi_Controller(uint16_t port) : _port(port), _server(port), _SERIAL_CALLBACK_FUNCTION_STATUS(false), _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS(false), _onDataReceivedCallback(nullptr) { - memset(_receiveDataBuffer, 0, sizeof(_receiveDataBuffer)); + memset(_receiveBuffer, 0, sizeof(_receiveBuffer)); } void WiFi_Controller::begin(const char *wifi_name, const char *wifi_password) @@ -97,7 +15,9 @@ void WiFi_Controller::begin(const char *wifi_name, const char *wifi_password) WiFi.softAP(wifi_name, wifi_password); _server.begin(); - _server.onClient(std::bind(&WiFi_Controller::onClientEvent, this, std::placeholders::_1, std::placeholders::_2), (void *)this); + + _server.onClient([this](void *arg, AsyncClient *client) + { this->onClientEvent(arg, client); }, (void *)this); Serial.print("IP: "); Serial.print(WiFi.softAPIP()); @@ -107,7 +27,7 @@ void WiFi_Controller::begin(const char *wifi_name, const char *wifi_password) void WiFi_Controller::onClientEvent(void *arg, AsyncClient *client) { client->onData([this](void *arg, AsyncClient *client, void *data, size_t len) - { this->onDataReceived(arg, client, data, len); }); + { this->onDataReceived(arg, client, data, len), this->_onDataReceived(arg, client, data, len); }); client->onConnect([this](void *arg, AsyncClient *client) { this->onClientConnect(arg, client); }); client->onDisconnect([this](void *arg, AsyncClient *client) @@ -133,25 +53,17 @@ void WiFi_Controller::onDataReceived(void *arg, AsyncClient *client, void *data, { _onDataReceivedCallback(arg, client, data, len); } - if (_SERIAL_CALLBACK_FUNCTION_STATUS) - { - client->write((char *)data, len); - Serial.write((char *)data, len); - } - if (_REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS) - { - client->write("_remote_control 2233"); - Serial.print("_remote_control 2233"); - } } void WiFi_Controller::_onDataReceived(void *arg, AsyncClient *client, void *data, size_t len) { len = (len >= 100 ? 100 : len); - memset(_receiveDataBuffer, 0, sizeof(_receiveDataBuffer)); + memset(_receiveBuffer, 0, sizeof(_receiveBuffer)); + + memcpy(_receiveBuffer, data, len); - memcpy(_receiveDataBuffer, data, len); + Serial.println("_onData"); } void WiFi_Controller::setSerialCallbackFunctionStatus(bool isEnable) @@ -164,7 +76,7 @@ void WiFi_Controller::setRemoteControlCallbackFunctionStatus(bool isEnable) _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS = isEnable; } -void WiFi_Controller::setOnDataReceivedCallback(DataReceivedCallback callback) +void WiFi_Controller::onDataCallback(DataReceivedCallback callback) { _onDataReceivedCallback = callback; } diff --git a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h index b6a5a759..04a92d1f 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h @@ -1,38 +1,3 @@ - -// #ifndef MYWIFI_H -// #define MYWIFI_H - -// #include -// #include -// #include - -// class WiFi_Controller -// { -// public: -// WiFi_Controller(uint16_t port = 80); -// void begin(const char *wifi_name, const char *wifi_password); - -// void setSerialCallbackFunctionStatus(bool isEnable); -// void setRemoteControlCallbackFunctionStatus(bool isEnable); - -// void onClientConnect(void *arg, AsyncClient *client); -// void onClientDisconnect(void *arg, AsyncClient *client); -// void onDataReceived(void *arg, AsyncClient *client, void *data, size_t len); -// void onClientEvent(void *arg, AsyncClient *client); - -// private: -// char _receiveDataBuffer[100]; -// uint16_t _port; -// AsyncServer _server; - -// bool _SERIAL_CALLBACK_FUNCTION_STATUS; -// bool _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS; - -// void _onDataReceived(void *arg, AsyncClient *client, void *data, size_t len); -// }; - -// #endif - #ifndef MYWIFI_H #define MYWIFI_H @@ -52,15 +17,15 @@ public: void setSerialCallbackFunctionStatus(bool isEnable); void setRemoteControlCallbackFunctionStatus(bool isEnable); - void setOnDataReceivedCallback(DataReceivedCallback callback); + void onDataCallback(DataReceivedCallback callback); // 新增的设置回调函数 + void onDataReceived(void *arg, AsyncClient *client, void *data, size_t len); void onClientConnect(void *arg, AsyncClient *client); void onClientDisconnect(void *arg, AsyncClient *client); - void onDataReceived(void *arg, AsyncClient *client, void *data, size_t len); void onClientEvent(void *arg, AsyncClient *client); private: - char _receiveDataBuffer[100]; + char _receiveBuffer[100]; uint16_t _port; AsyncServer _server; diff --git a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp index 78de362c..62f6504a 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp @@ -1,21 +1,29 @@ #include "myXHZS.h" -WiFi_Controller server; +WiFi_Controller espWifi; +BlueTooth_Controller espBT; -void XHZS_begin(const char *wifi_name, const char *wifi_password) +void wifi_onData(void *arg, AsyncClient *client, void *data, size_t len) +{ + client->write((char *)data, len); + Serial.write((char *)data, len); +} + +void blueTooth_onData(const uint8_t *buffer, size_t size) { - server.begin(wifi_name, wifi_password); + espBT.sendUint8_t(buffer, size); + Serial.write(buffer, size); } -void XHZS_onDataReceived(void *arg, AsyncClient *client, void *data, size_t len) +void XHZS_begin(const char *wifi_name, const char *wifi_password) { - Serial.print("xhzs callback"); + // esp_wifi.begin(wifi_name, wifi_password); + espBT.begin(wifi_name); } void uart_EnableSerialFunction(bool isEnable) { - AsyncClient *client; - server.onClientEvent((void * )nullptr, client); - // server.setSerialCallbackFunctionStatus(isEnable); - // server.setRemoteControlCallbackFunctionStatus(isEnable); + // esp_wifi.onDataCallback(wifi_onData); + + espBT.onDataCallback(blueTooth_onData); } diff --git a/PlatformIO_ESP32_oscilloscope/src/main.cpp b/PlatformIO_ESP32_oscilloscope/src/main.cpp index 0141eb16..96f5c05a 100644 --- a/PlatformIO_ESP32_oscilloscope/src/main.cpp +++ b/PlatformIO_ESP32_oscilloscope/src/main.cpp @@ -1,28 +1,11 @@ #if true -// #include "myXHZS.h" -#include "myWiFi.h" - -WiFi_Controller wifiController; - -// 自定义的回调函数 -void customDataReceivedCallback(void *arg, AsyncClient *client, void *data, size_t len) -{ - // Serial.printf("%d-%s\n", len, (char *)data); - client->write((char *)data, len); - Serial.write((char *)data, len); -} +#include "myXHZS.h" void setup() { - // XHZS_begin("esp32_ap", "esp32esp32"); - // uart_EnableSerialFunction(true); - - // 设置回调函数 - wifiController.setOnDataReceivedCallback(customDataReceivedCallback); - - // 开始 WiFi 控制器 - wifiController.begin("MyAP", "esp32esp32"); + XHZS_begin("XHZS", "12345678"); + uart_EnableSerialFunction(true); } void loop() -- Gitee From 0118e8e31f0eba75f5131c3c44b9d5bd2ff01b0b Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Thu, 22 Aug 2024 00:45:01 +0800 Subject: [PATCH 14/18] =?UTF-8?q?=E5=AF=B9=E8=93=9D=E7=89=99=E5=92=8C?= =?UTF-8?q?=E5=AD=A6=E4=BC=9A=E5=8A=A9=E6=89=8B=E7=9A=84=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E5=88=86=E7=A6=BB=EF=BC=8C=E4=B9=8B=E5=89=8D=E6=98=AF=E6=B7=B7?= =?UTF-8?q?=E5=90=88=E5=9C=A8=E4=B8=80=E5=9D=97=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.vscode/c_cpp_properties.json | 4 +- .../.vscode/settings.json | 11 +- .../lib/myBlueTooth/myBlueTooth.cpp | 137 ++++------------ .../lib/myBlueTooth/myBlueTooth.h | 129 ++------------- .../lib/myWiFi/myWiFi.cpp | 116 ++++++++++---- .../lib/myWiFi/myWiFi.h | 46 +++--- .../lib/myXHZS/myXHZS.cpp | 20 ++- .../lib/myXHZS/myXHZS.h | 1 + PlatformIO_ESP32_oscilloscope/src/main.cpp | 147 +++++++++++++++++- 9 files changed, 335 insertions(+), 276 deletions(-) diff --git a/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json b/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json index 4a65618f..529d3627 100644 --- a/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json +++ b/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json @@ -10,6 +10,7 @@ "includePath": [ "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/include", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/src", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myWiFi", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", @@ -212,7 +213,6 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/cores/esp32", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/variants/esp32", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/Interrupts_and_timers", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/Unity/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ArduinoOTA/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src", @@ -252,6 +252,7 @@ "path": [ "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/include", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/src", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myWiFi", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", @@ -454,7 +455,6 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/cores/esp32", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/variants/esp32", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/Interrupts_and_timers", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/Unity/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ArduinoOTA/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src", diff --git a/PlatformIO_ESP32_oscilloscope/.vscode/settings.json b/PlatformIO_ESP32_oscilloscope/.vscode/settings.json index 98a26258..4ba537ed 100644 --- a/PlatformIO_ESP32_oscilloscope/.vscode/settings.json +++ b/PlatformIO_ESP32_oscilloscope/.vscode/settings.json @@ -20,7 +20,16 @@ "deque": "cpp", "unordered_map": "cpp", "unordered_set": "cpp", - "vector": "cpp" + "vector": "cpp", + "random": "cpp", + "cstddef": "cpp", + "optional": "cpp", + "istream": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "typeinfo": "cpp", + "cmath": "cpp", + "cstring": "cpp" }, "C_Cpp.errorSquiggles": "disabled" } \ No newline at end of file diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp index f73d52bb..daab6116 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -771,138 +771,63 @@ void test_struct_move() #endif #endif -// #include "myBlueTooth.h" -BlueTooth_Controller::BlueTooth_Controller() : _serialBT(), DIGIT_ZERO(0x30), DIGIT_ONE(0x31) -{ - memset(_receiveBuffer, 0, sizeof(_receiveBuffer)); -} +#include "myBlueTooth.h" -BlueTooth_Controller &BlueTooth_Controller::getInstance() +Bluetooth_Controller::Bluetooth_Controller() : _isConnected(false) { - static BlueTooth_Controller instance; // 静态局部变量 - return instance; } -void BlueTooth_Controller::begin(const char *BlueTooth_name) +void Bluetooth_Controller::begin(const char *deviceName) { Serial.begin(115200); - // 蓝牙 spp 配对 begin - _serialBT.enableSSP(); - _serialBT.onConfirmRequest([this](uint32_t num_val) - { this->ConfirmRequestCallback(num_val); }); - _serialBT.onAuthComplete([this](boolean success) - { this->AuthCompleteCallback(success); }); - - // 蓝牙 spp 配对 end - - // 注册回调 - _serialBT.register_callback(&BlueTooth_Controller::Bluetooth_Event_Wrapper); - _serialBT.onData([this](const uint8_t *buffer, size_t size) - { this->onDataReceived(buffer, size); }); - _serialBT.begin(BlueTooth_name); - - Serial.printf("Bluetooth name: %s\r\n", BlueTooth_name); -}; -void BlueTooth_Controller::sendFloatVia(const float *data) -{ - // 非字符串不要使用 _serialBT.write(),转换后会乱码 - _serialBT.printf("%.2f", *data); -} + // 设置蓝牙名称 + _serialBT.begin(deviceName); + Serial.printf("Bluetooth device name: %s\n", deviceName); -void BlueTooth_Controller::sendStringVia(const uint8_t *stringData) -{ - _serialBT.write(stringData, strlen((char *)stringData)); -} - -void BlueTooth_Controller::sendUint8_t(const uint8_t *buffer, size_t size) -{ - _serialBT.write(buffer, size); + // 注册蓝牙事件回调 + _serialBT.register_callback(bluetoothEventCallback); } -char *BlueTooth_Controller::getReceivedData() +Bluetooth_Controller *getBluetoothControllerInstance() { - return _receiveBuffer; + static Bluetooth_Controller controller; + return &controller; } -void BlueTooth_Controller::onDataCallback(BT_DataReceivedCallback callback) +void Bluetooth_Controller::bluetoothEventCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) { - _onDataReceivedCallback = callback; -} - -void BlueTooth_Controller::onDataReceived(const uint8_t *buffer, size_t size) -{ - if (_onDataReceivedCallback != nullptr) + Bluetooth_Controller *controller = getBluetoothControllerInstance(); + if (controller) { - _onDataReceivedCallback(buffer, size); + controller->handleBluetoothEvent(event, param); } } -void BlueTooth_Controller::Bluetooth_Event_Wrapper(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) -{ - // 直接调用实例的方法 - getInstance().Bluetooth_Event(event, param); -} - -// 设置事件回调函数 连接 断开 发送 接收 -void BlueTooth_Controller::Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) +void Bluetooth_Controller::handleBluetoothEvent(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) { switch (event) { - // 蓝牙连接成功标志 case ESP_SPP_SRV_OPEN_EVT: - case ESP_SPP_OPEN_EVT: - { - uint8_t connection[] = "connection successful!\r\n"; - sendStringVia(connection); - break; - } - // 蓝牙断开连接标志 - case ESP_SPP_CLOSE_EVT: - { - uint8_t disconnect[] = "disconnect successful!\r\n"; - sendStringVia(disconnect); - break; - } - // 数据接收标志 - case ESP_SPP_DATA_IND_EVT: - { - int index = 0; - memset(_receiveBuffer, '\0', sizeof(_receiveBuffer)); - while (_serialBT.available()) // 等待接收完成 + if (!_isConnected) { - _receiveBuffer[index++] = _serialBT.read(); // 按字节存储 + _isConnected = true; + Serial.println("Client connected!"); } + else + { + Serial.println("Connection rejected: already connected!"); + // 断开新连接 + _serialBT.disconnect(); + } + break; + case ESP_SPP_CLOSE_EVT: + _isConnected = false; + Serial.println("Client disconnected!"); break; - } - // 数据发送标志 - case ESP_SPP_WRITE_EVT: - { - Serial.write("send OK!\r\n"); + + default: break; } - } -} - -// 认证请求回调 -void BlueTooth_Controller::ConfirmRequestCallback(uint32_t numVal) -{ - // numVal是主机发来的识别码 - Serial.printf("recv pin: %d \r\n", numVal); - - // 这里要对这个识别码进行判断,是否和主机一样或是是否是我们从机内置的密码 - // 然后再判断是否确定连接,我们这里直接确认了 - - _serialBT.confirmReply(true); - // _serialBT.confirmReply(false); // 如果要拒绝就用这句 -} - -// 认证结果回调函数 -void BlueTooth_Controller::AuthCompleteCallback(boolean success) -{ - if (success) - Serial.println("Pairing success!!"); - else - Serial.println("Pairing failed, rejected by user!!"); } diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h index f6893350..5fd6b4ab 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h @@ -2,124 +2,25 @@ #define MYBLUETOOTH_H_ #include -#include #include -// 定义遥控器输出结构体 -struct structRemoteControlFormat -{ - short begin; - - short LEFT_up; // 向上 // 1 - short LEFT_down; // 向下 - short LEFT_left; // 向左 - short LEFT_right; // 向右 - - short RIGHT_up; // 向上 // 5 - short RIGHT_down; // 向下 - short RIGHT_left; // 向左 - short RIGHT_right; // 向右 - - short middle; - - short A; // 按钮A // 10 - short B; // 按钮B - short C; // 按钮C - short D; // 按钮D - short E; // 按钮E - short F; // 按钮F - - short LEFT_x; // 左摇杆X轴 // 16 - short LEFT_y; // 左摇杆Y轴 - short RIGHT_x; // 右摇杆X轴 - short RIGHT_y; // 右摇杆Y轴 - - short end; // 结束标志位 // 20 -}; - -// 遥控器中断指令枚举定义 -enum INTERRUPT_MODE_ORDER -{ - INTERRUPT_BUTTON_RELEASE = 0X00, // 按键释放指令 - INTERRUPT_BUTTON_PRESS = 0X01, // 按键按下指令 - - INTERRUPT_LEFT_UP = 0X10, // 左侧方向键上 - INTERRUPT_LEFT_DOWN = 0X20, // 左侧方向键下 - INTERRUPT_LEFT_LEFT = 0X30, // 左侧方向键左 - INTERRUPT_LEFT_RIGHT = 0X40, // 左侧方向键右 - - INTERRUPT_RIGHT_UP = 0X50, // 右侧方向键上 - INTERRUPT_RIGHT_DOWN = 0X60, // 右侧方向键下 - INTERRUPT_RIGHT_LEFT = 0X70, // 右侧方向键左 - INTERRUPT_RIGHT_RIGHT = 0X80, // 右侧方向键右 - - INTERRUPT_LEFT_X = 0X9A, // 摇杆的指令 - INTERRUPT_LEFT_Y = 0X9B, - INTERRUPT_RIGHT_X = 0X9C, - INTERRUPT_RIGHT_Y = 0X9D, - - INTERRUPT_BUTTON_A = 0XA0, // 左侧A功能键 - INTERRUPT_BUTTON_B = 0XB0, // 左侧B功能键 - INTERRUPT_BUTTON_C = 0XC0, // 右侧C功能键 - INTERRUPT_BUTTON_D = 0XD0, // 右侧D功能键 - INTERRUPT_BUTTON_E = 0XE0, // 中间开关E - INTERRUPT_BUTTON_F = 0XF0 // 中间开关F -}; - -// 遥控器的模块大小 -typedef enum MODULE_SIZE -{ - JOYSTICK_SIZE = 6, // 手柄摇杆,占位6 - DIRECTIONAL_PAD_SIZE = 4, // 方向按钮,占位4 - FUNCTION_BUTTON_SIZE = 6, // 功能按键,占位6 - -} INTERFACE_SIZE; - -// 遥控器的方向按钮和摇杆组合 -enum POLYTYPE -{ - DIRECTIONAL_PAD = 0x00, - DIRECTIONAL_PAD_AND_JOYSTICK = 0x01, - JOYSTICK_AND_DIRECTIONAL_PAD = 0x10, - JOYSTICKS = 0x11 -}; - -typedef void (*BT_DataReceivedCallback)(const uint8_t *buffer, size_t size); - -class BlueTooth_Controller +class Bluetooth_Controller { public: - static BlueTooth_Controller &getInstance(); // 单例获取方法 - BlueTooth_Controller(); - void begin(const char *BlueTooth_name); - - void sendFloatVia(const float *data); - void sendStringVia(const uint8_t *stringData); - void sendUint8_t(const uint8_t *buffer, size_t size); - char *getReceivedData(); - - void onDataCallback(BT_DataReceivedCallback callback); // 新增的设置回调函数 - void onDataReceived(const uint8_t *buffer, size_t size); - - static void Bluetooth_Event_Wrapper(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); - void Bluetooth_Event(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); + Bluetooth_Controller(); + void begin(const char *deviceName); private: - char _receiveBuffer[100]; BluetoothSerial _serialBT; + bool _isConnected; // 用于跟踪是否有连接 - char DIGIT_ZERO; - char DIGIT_ONE; - - BT_DataReceivedCallback _onDataReceivedCallback; // 新增的回调函数指针 - -private: - void ConfirmRequestCallback(uint32_t numVal); - void AuthCompleteCallback(boolean success); + static void bluetoothEventCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); + void handleBluetoothEvent(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); }; #endif + #if falae #include @@ -284,16 +185,16 @@ void test_remote_control_Timer_function(); /************************* 功能测试,适配中... ************************/ -// // 遥控器数据处理测试函数 -// void test_remote_control(const uint8_t *buffer, size_t size); +// 遥控器数据处理测试函数 +void test_remote_control(const uint8_t *buffer, size_t size); -// // 测试遥控器中断模式下的功能 -// void XHZS_RemoteControlInterruptMode(); +// 测试遥控器中断模式下的功能 +void XHZS_RemoteControlInterruptMode(); -// // 结构体数组的使用示例 -// void test_struct_array(); +// 结构体数组的使用示例 +void test_struct_array(); -// // 结构体移动示例 -// void test_struct_move(); +// 结构体移动示例 +void test_struct_move(); #endif diff --git a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp index 0de9ecd2..1f6d0c1a 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp @@ -1,10 +1,15 @@ #include "myWiFi.h" - -WiFi_Controller::WiFi_Controller(uint16_t port) - : _port(port), _server(port), _SERIAL_CALLBACK_FUNCTION_STATUS(false), _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS(false), _onDataReceivedCallback(nullptr) +WiFi_Controller::WiFi_Controller(uint16_t port, char maxClients) + : _port(port), _server(port), MAX_CLIENTS(maxClients), _clientCount(0) { memset(_receiveBuffer, 0, sizeof(_receiveBuffer)); + _clients = new ClientInfo[MAX_CLIENTS]; // 动态分配数组 +} + +WiFi_Controller::~WiFi_Controller() +{ + delete[] _clients; // 释放内存 } void WiFi_Controller::begin(const char *wifi_name, const char *wifi_password) @@ -19,31 +24,30 @@ void WiFi_Controller::begin(const char *wifi_name, const char *wifi_password) _server.onClient([this](void *arg, AsyncClient *client) { this->onClientEvent(arg, client); }, (void *)this); - Serial.print("IP: "); + Serial.print("AP IP address: "); Serial.print(WiFi.softAPIP()); Serial.printf(" port: %d\r\n", _port); } void WiFi_Controller::onClientEvent(void *arg, AsyncClient *client) { - client->onData([this](void *arg, AsyncClient *client, void *data, size_t len) - { this->onDataReceived(arg, client, data, len), this->_onDataReceived(arg, client, data, len); }); - client->onConnect([this](void *arg, AsyncClient *client) - { this->onClientConnect(arg, client); }); + onClientConnected(arg, client); + client->onDisconnect([this](void *arg, AsyncClient *client) - { this->onClientDisconnect(arg, client); }); + { this->onClientDisconnected(arg, client); }); + + client->onData([this](void *arg, AsyncClient *client, void *data, size_t len) + { this->onDataReceived(arg, client, data, len), this->onDataBuffer(arg, client, data, len); }); } -void WiFi_Controller::onClientConnect(void *arg, AsyncClient *client) +void WiFi_Controller::onDataCallback(WiFi_DataReceivedCallback callback) { - client->write("Connection\r\n"); - Serial.write("Connection\r\n"); + _onDataReceivedCallback = callback; } -void WiFi_Controller::onClientDisconnect(void *arg, AsyncClient *client) +char *WiFi_Controller::getReceiveBuffer() { - client->write("disconnected\r\n"); - Serial.write("disconnected\r\n"); + return _receiveBuffer; } void WiFi_Controller::onDataReceived(void *arg, AsyncClient *client, void *data, size_t len) @@ -55,28 +59,88 @@ void WiFi_Controller::onDataReceived(void *arg, AsyncClient *client, void *data, } } -void WiFi_Controller::_onDataReceived(void *arg, AsyncClient *client, void *data, size_t len) +void WiFi_Controller::onDataBuffer(void *arg, AsyncClient *client, void *data, size_t len) { + char index = 0; len = (len >= 100 ? 100 : len); - - memset(_receiveBuffer, 0, sizeof(_receiveBuffer)); - memcpy(_receiveBuffer, data, len); +} - Serial.println("_onData"); +void WiFi_Controller::onClientConnected(void *arg, AsyncClient *client) +{ + if (_clientCount < MAX_CLIENTS) + { + _clients[_clientCount].client = client; + _clients[_clientCount].ip = client->remoteIP(); + _clients[_clientCount].port = client->remotePort(); + _clientCount++; + + Serial.print("Client connected: "); + Serial.print(client->remoteIP()); + Serial.print(":"); + Serial.println(client->remotePort()); + } + else + { + Serial.println("Max clients reached, connection rejected."); + client->close(); + } } -void WiFi_Controller::setSerialCallbackFunctionStatus(bool isEnable) +void WiFi_Controller::onClientDisconnected(void *arg, AsyncClient *client) { - _SERIAL_CALLBACK_FUNCTION_STATUS = isEnable; + for (int i = 0; i < _clientCount; i++) + { + if (_clients[i].client == client) + { + Serial.print("Client disconnected: "); + Serial.print(_clients[i].ip); + Serial.print(":"); + Serial.println(_clients[i].port); + + // 移除客户端信息 + for (int j = i; j < _clientCount - 1; j++) + { + _clients[j] = _clients[j + 1]; + } + _clientCount--; + break; + } + } } -void WiFi_Controller::setRemoteControlCallbackFunctionStatus(bool isEnable) +void WiFi_Controller::checkClients() { - _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS = isEnable; + for (int i = 0; i < _clientCount; i++) + { + if (_clients[i].client->connected()) + { + Serial.print("Client "); + Serial.print(_clients[i].ip); + Serial.print(":"); + Serial.print(_clients[i].port); + Serial.println(" is connected."); + } + else + { + Serial.print("Client "); + Serial.print(_clients[i].ip); + Serial.print(":"); + Serial.print(_clients[i].port); + Serial.println(" is disconnected."); + } + } } -void WiFi_Controller::onDataCallback(DataReceivedCallback callback) +void WiFi_Controller::sendDataToAllClients(const char *data, size_t len) { - _onDataReceivedCallback = callback; + for (int i = 0; i < _clientCount; i++) + { + AsyncClient *client = _clients[i].client; + if (client->space() > len && client->canSend()) + { + client->add(data, len); + client->send(); + } + } } diff --git a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h index 04a92d1f..d9692c71 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h @@ -6,35 +6,43 @@ #include // 定义回调函数的类型 -typedef void (*DataReceivedCallback)(void *arg, AsyncClient *client, void *data, size_t len); +typedef void (*WiFi_DataReceivedCallback)(void *arg, AsyncClient *client, void *data, size_t len); + +struct ClientInfo +{ + AsyncClient *client; + IPAddress ip; + uint16_t port; +}; class WiFi_Controller { public: - WiFi_Controller(uint16_t port = 80); - void begin(const char *wifi_name, const char *wifi_password); + WiFi_Controller(uint16_t port = 80, char maxClients = 2); + ~WiFi_Controller(); - void setSerialCallbackFunctionStatus(bool isEnable); - void setRemoteControlCallbackFunctionStatus(bool isEnable); - - void onDataCallback(DataReceivedCallback callback); // 新增的设置回调函数 + void begin(const char *wifi_name, const char *wifi_password); + void checkClients(); + void sendDataToAllClients(const char *data, size_t len); + void onDataCallback(WiFi_DataReceivedCallback callback); // 设置回调函数 - void onDataReceived(void *arg, AsyncClient *client, void *data, size_t len); - void onClientConnect(void *arg, AsyncClient *client); - void onClientDisconnect(void *arg, AsyncClient *client); - void onClientEvent(void *arg, AsyncClient *client); + char *getReceiveBuffer(); // 获取接收缓存区指针 private: - char _receiveBuffer[100]; uint16_t _port; - AsyncServer _server; - - bool _SERIAL_CALLBACK_FUNCTION_STATUS; - bool _REMOTE_CONTROL_CALLBACK_FUNCTION_STATUS; + char _receiveBuffer[100]; + char MAX_CLIENTS; + char _clientCount; - DataReceivedCallback _onDataReceivedCallback; // 新增的回调函数指针 + ClientInfo *_clients; + AsyncServer _server; + WiFi_DataReceivedCallback _onDataReceivedCallback; // 定义回调函数指针 - void _onDataReceived(void *arg, AsyncClient *client, void *data, size_t len); + void onDataReceived(void *arg, AsyncClient *client, void *data, size_t len); // 接收数据回调函数 + void onDataBuffer(void *arg, AsyncClient *client, void *data, size_t len); + void onClientConnected(void *arg, AsyncClient *client); + void onClientDisconnected(void *arg, AsyncClient *client); + void onClientEvent(void *arg, AsyncClient *client); }; -#endif \ No newline at end of file +#endif diff --git a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp index 62f6504a..1ae19eae 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp @@ -1,7 +1,7 @@ #include "myXHZS.h" WiFi_Controller espWifi; -BlueTooth_Controller espBT; +// BlueTooth_Controller espBT; void wifi_onData(void *arg, AsyncClient *client, void *data, size_t len) { @@ -11,19 +11,25 @@ void wifi_onData(void *arg, AsyncClient *client, void *data, size_t len) void blueTooth_onData(const uint8_t *buffer, size_t size) { - espBT.sendUint8_t(buffer, size); - Serial.write(buffer, size); + // espBT.send(buffer, size); + // Serial.write(buffer, size); } void XHZS_begin(const char *wifi_name, const char *wifi_password) { - // esp_wifi.begin(wifi_name, wifi_password); - espBT.begin(wifi_name); + espWifi.begin(wifi_name, wifi_password); + + // espBT.begin(wifi_name); } void uart_EnableSerialFunction(bool isEnable) { - // esp_wifi.onDataCallback(wifi_onData); + // espWifi.onDataCallback(wifi_onData); + + // espBT.onDataCallback(blueTooth_onData); +} + +void status_update() +{ - espBT.onDataCallback(blueTooth_onData); } diff --git a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h index 53930326..6b12c3ac 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h @@ -6,5 +6,6 @@ void XHZS_begin(const char *wifi_name, const char *wifi_password); void uart_EnableSerialFunction(bool isEnable); +void status_update(); #endif diff --git a/PlatformIO_ESP32_oscilloscope/src/main.cpp b/PlatformIO_ESP32_oscilloscope/src/main.cpp index 96f5c05a..20209440 100644 --- a/PlatformIO_ESP32_oscilloscope/src/main.cpp +++ b/PlatformIO_ESP32_oscilloscope/src/main.cpp @@ -1,4 +1,4 @@ -#if true +#if false #include "myXHZS.h" @@ -10,6 +10,7 @@ void setup() void loop() { + // status_update(); delay(1000); } @@ -61,3 +62,147 @@ void loop() delay(10); } #endif + +#if false +#include +#include +#include + +// 定义热点名称和密码 +const char *ssid = "MyESP32AP"; +const char *password = "password123"; + +// 定义客户端信息结构体 +struct ClientInfo +{ + AsyncClient *client; + IPAddress ip; + uint16_t port; +}; + +// 存储客户端信息的数组,限制为三个客户端 +ClientInfo clients[2]; +int clientCount = 0; + +// 定义回调函数 +void onClientConnected(void *arg, AsyncClient *client) +{ + if (clientCount < 3) + { + clients[clientCount].client = client; + clients[clientCount].ip = client->remoteIP(); + clients[clientCount].port = client->remotePort(); + clientCount++; + + Serial.print("Client connected: "); + Serial.print(client->remoteIP()); + Serial.print(":"); + Serial.println(client->remotePort()); + } + else + { + Serial.println("Max clients reached, connection rejected."); + client->close(); + } +} + +void onClientDisconnected(void *arg, AsyncClient *client) +{ + for (int i = 0; i < clientCount; i++) + { + if (clients[i].client == client) + { + Serial.print("Client disconnected: "); + Serial.print(clients[i].ip); + Serial.print(":"); + Serial.println(clients[i].port); + + // 移除客户端信息 + for (int j = i; j < clientCount - 1; j++) + { + clients[j] = clients[j + 1]; + } + clientCount--; + break; + } + } + Serial.print("Client connected: "); + Serial.print(client->getRemoteAddress()); + Serial.print(":"); + Serial.println(client->getRemotePort()); +} + +void checkClients() +{ + for (int i = 0; i < clientCount; i++) + { + if (clients[i].client->connected()) + { + Serial.print("Client "); + Serial.print(clients[i].ip); + Serial.print(":"); + Serial.print(clients[i].port); + Serial.println(" is connected."); + } + else + { + Serial.print("Client "); + Serial.print(clients[i].ip); + Serial.print(":"); + Serial.print(clients[i].port); + Serial.println(" is disconnected."); + } + } +} + + +void setup() +{ + // 初始化串口 + Serial.begin(115200); + + // 设置 ESP32 为热点模式 + WiFi.softAP(ssid, password); + + // 打印热点 IP 地址 + Serial.print("AP IP address: "); + Serial.println(WiFi.softAPIP()); + + // 创建 AsyncServer 实例 + static AsyncServer server(80); + + // 设置客户端连接和断开的回调函数 + server.onClient([](void *arg, AsyncClient *client) + { + onClientConnected(arg, client); + client->onDisconnect([](void* arg, AsyncClient* client) { + onClientDisconnected(arg, client); + delete client; // 释放客户端资源 + }); }, nullptr); + + // 启动服务器 + server.begin(); + + +} + +void loop() +{ + delay(3000); +} +#endif + +#include +#include + +Bluetooth_Controller btController; + +void setup() +{ + btController.begin("ESP32_BT_Hotspot"); +} + +void loop() +{ + // 主循环代码 +} -- Gitee From 3311e5b2bf4ec6464e398af57328e9eebd48ff15 Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Fri, 23 Aug 2024 00:57:00 +0800 Subject: [PATCH 15/18] =?UTF-8?q?=E5=AF=B9=20wifi=20=E5=92=8C=20bluetooth?= =?UTF-8?q?=20=E4=B8=A4=E4=B8=AA=E7=B1=BB=E8=BF=9B=E8=A1=8C=E6=95=B4?= =?UTF-8?q?=E7=90=86=EF=BC=8C=E7=BB=9F=E4=B8=80=E6=8E=A5=E5=8F=A3=EF=BC=8C?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=EF=BC=9A1.=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E6=8E=A5=E6=94=B6=E6=95=B0=E6=8D=AE=202.=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E6=95=B0=E6=8D=AE=203.=E5=9C=A8=E4=B8=BB?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E4=B8=AD=E5=8F=91=E9=80=81=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.vscode/c_cpp_properties.json | 4 +- .../lib/myBlueTooth/myBlueTooth.cpp | 335 ++++++++++++++---- .../lib/myBlueTooth/myBlueTooth.h | 65 +++- .../lib/myWiFi/myWiFi.cpp | 231 +++++++++++- .../lib/myWiFi/myWiFi.h | 99 +++++- .../lib/myXHZS/myXHZS.cpp | 6 +- .../lib/myXHZS/myXHZS.h | 1 - PlatformIO_ESP32_oscilloscope/src/main.cpp | 74 ++-- 8 files changed, 702 insertions(+), 113 deletions(-) diff --git a/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json b/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json index 529d3627..4a65618f 100644 --- a/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json +++ b/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json @@ -10,7 +10,6 @@ "includePath": [ "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/include", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/src", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myWiFi", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", @@ -213,6 +212,7 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/cores/esp32", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/variants/esp32", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/Interrupts_and_timers", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/Unity/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ArduinoOTA/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src", @@ -252,7 +252,6 @@ "path": [ "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/include", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/src", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myWiFi", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", @@ -455,6 +454,7 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/cores/esp32", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/variants/esp32", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/Interrupts_and_timers", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/Unity/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ArduinoOTA/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src", diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp index daab6116..2daf033d 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -1,5 +1,279 @@ + +#include "myBlueTooth.h" + +BluetoothController::BluetoothController() : _isConnected(false) +{ + memset(receiveBuffer, 0, sizeof(receiveBuffer)); +} + +void BluetoothController::begin(const char *bluetoothName) +{ + Serial.begin(115200); + // 蓝牙 spp 配对 begin + _serialBT.enableSSP(); + + _serialBT.onConfirmRequest([this](uint32_t numVal) + { this->_confirmRequestCallback(numVal); }); + + _serialBT.onAuthComplete([this](boolean success) + { this->_authCompleteCallback(true); }); + // 蓝牙 spp 配对 end + + // 注册蓝牙事件回调 + _serialBT.register_callback(_bluetoothEventCallback); + + _serialBT.onData([this](const uint8_t *buffer, size_t size) + { this->_storeReceivedData(buffer, size); this->_processReceivedData(buffer, size); }); + + // 设置蓝牙名称 + _serialBT.begin(bluetoothName); + Serial.printf("Bluetooth name: %s\n", bluetoothName); +} + +void BluetoothController::setDataReceivedCallback(BlueToothDataReceivedCallback callback) +{ + _dataReceivedCallback = callback; +} + +void BluetoothController::_processReceivedData(const uint8_t *buffer, size_t size) +{ + if (_dataReceivedCallback != nullptr) + { + _dataReceivedCallback(buffer, size); + } +} + +void BluetoothController::_storeReceivedData(const uint8_t *buffer, size_t size) +{ + size = (size >= sizeof(receiveBuffer) ? (sizeof(receiveBuffer) - 1) : size); + memcpy(receiveBuffer, buffer, size); + receiveBuffer[size] = '\0'; // 确保字符串以 null 结尾 +} + +void BluetoothController::sendData(const uint8_t *buffer, size_t size) +{ + _serialBT.write(buffer, size); +} + +BluetoothController *getBluetoothControllerInstance() +{ + static BluetoothController controller; + return &controller; +} + +void BluetoothController::_bluetoothEventCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) +{ + BluetoothController *controller = getBluetoothControllerInstance(); + + if (controller) + { + controller->_handleBluetoothEvent(event, param); + } +} + +void BluetoothController::_handleBluetoothEvent(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) +{ + switch (event) + { + case ESP_SPP_SRV_OPEN_EVT: + if (!_isConnected) + { + _isConnected = true; + Serial.println("Client connected!"); + } + else + { + Serial.println("Connection rejected: already connected!"); + // 断开新连接 + _serialBT.disconnect(); + } + break; + + case ESP_SPP_CLOSE_EVT: + _isConnected = false; + Serial.println("Client disconnected!"); + break; + + case ESP_SPP_WRITE_EVT: + // Serial.write("send OK!\r\n"); + break; + } +} + +void BluetoothController::_confirmRequestCallback(uint32_t numVal) +{ + Serial.printf("Received pin: %d \r\n", numVal); + _serialBT.confirmReply(true); +} + +void BluetoothController::_authCompleteCallback(boolean success) +{ + if (success) + Serial.println("Pairing success!!"); + else + Serial.println("Pairing failed, rejected by user!!"); +} + +#if false #include "myBlueTooth.h" +Bluetooth_Controller::Bluetooth_Controller() : _isConnected(false) +{ + memset(receiveBuffer, 0, sizeof(receiveBuffer)); +} + +void Bluetooth_Controller::begin(const char *blueTooth_name) +{ + Serial.begin(115200); + // 蓝牙 spp 配对 begin + _serialBT.enableSSP(); + + _serialBT.onConfirmRequest([this](uint32_t numVal) + { this->BTConfirmRequestCallback(numVal); }); + + _serialBT.onAuthComplete([this](boolean success) + { this->BTAuthCompleteCallback(true); }); + // 蓝牙 spp 配对 end + + // 注册蓝牙事件回调 + _serialBT.register_callback(_bluetoothEventCallback); + + _serialBT.onData([this](const uint8_t *buffer, size_t size) + { this->onDataReceivedBuffer(buffer, size), this->onDataReceived(buffer, size); }); + + // _serialBT.onData([this](const uint8_t *buffer, size_t size) + // { this->onDataReceived(buffer, size); }); + + // 设置蓝牙名称 + _serialBT.begin(blueTooth_name); + Serial.printf("Bluetooth name: %s\n", blueTooth_name); +} + +void Bluetooth_Controller::onReceivedCallback(BlueTooth_DataReceivedCallback callback) +{ + _onDataReceivedCallback = callback; +} + +void Bluetooth_Controller::onDataReceived(const uint8_t *buffer, size_t size) +{ + if (_onDataReceivedCallback != nullptr) + { + _onDataReceivedCallback(buffer, size); + } +} + +void Bluetooth_Controller::onDataReceivedBuffer(const uint8_t *buffer, size_t size) +{ + size = (size >= sizeof(receiveBuffer) ? (sizeof(receiveBuffer) - 1) : size); + memcpy(receiveBuffer, buffer, size); + receiveBuffer[size] = '\0'; // 确保字符串以 null 结尾 +} + +void Bluetooth_Controller::write(const uint8_t *buffer, size_t size) +{ + _serialBT.write(buffer, size); +} + +// char *Bluetooth_Controller::getReceiveBuffer() +// { +// return receiveBuffer; +// } + +// +Bluetooth_Controller *getBluetoothControllerInstance() +{ + static Bluetooth_Controller controller; + return &controller; +} + +void Bluetooth_Controller::_bluetoothEventCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) +{ + Bluetooth_Controller *controller = getBluetoothControllerInstance(); + + if (controller) + { + controller->_processReceivedData(event, param); + } +} + +void Bluetooth_Controller::_processReceivedData(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) +{ + uint8_t index = 0; + + switch (event) + { + case ESP_SPP_SRV_OPEN_EVT: + { + if (!_isConnected) + { + _isConnected = true; + Serial.println("Client connected!"); + } + else + { + Serial.println("Connection rejected: already connected!"); + // 断开新连接 + _serialBT.disconnect(); + } + break; + } + + case ESP_SPP_CLOSE_EVT: + { + _isConnected = false; + Serial.println("Client disconnected!"); + break; + } + + // case ESP_SPP_DATA_IND_EVT: + // { + // esp_spp_cb_param_t *spp_param = (esp_spp_cb_param_t *)param; + // uint8_t *data = spp_param->data_ind.data; + // size_t len = spp_param->data_ind.len; + + // // 将接收到的数据存储到 receiveBuffer 中 + // len = (len >= sizeof(receiveBuffer) ? sizeof(receiveBuffer) - 1 : len); + // memcpy(receiveBuffer, data, len); + // receiveBuffer[len] = '\0'; // 确保字符串以 null 结尾 + + // Serial.printf("Received data: %d-%s\n", len, receiveBuffer); + // receiveBuffer[0] = 0; // 清空接收缓存 + // break; + // } + + case ESP_SPP_WRITE_EVT: + { + // Serial.write("send OK!\r\n"); + break; + } + } +} + +// 认证请求回调 +void Bluetooth_Controller::BTConfirmRequestCallback(uint32_t numVal) +{ + + // numVal是主机发来的识别码 + Serial.printf("recv pin: %d \r\n", numVal); + + // 这里要对这个识别码进行判断,是否和主机一样或是是否是我们从机内置的密码 + // 然后再判断是否确定连接,我们这里直接确认了 + + _serialBT.confirmReply(true); + // _serialBT.confirmReply(false); // 如果要拒绝就用这句 +} + +// 认证结果回调函数 +void Bluetooth_Controller::BTAuthCompleteCallback(boolean success) +{ + if (success) + Serial.println("Pairing success!!"); + else + Serial.println("Pairing failed, rejected by user!!"); +} + +#endif +// #endif #if false /************************* blueTooth part *************************/ @@ -770,64 +1044,3 @@ void test_struct_move() #endif #endif - -#include "myBlueTooth.h" - -Bluetooth_Controller::Bluetooth_Controller() : _isConnected(false) -{ -} - -void Bluetooth_Controller::begin(const char *deviceName) -{ - Serial.begin(115200); - - // 设置蓝牙名称 - _serialBT.begin(deviceName); - Serial.printf("Bluetooth device name: %s\n", deviceName); - - // 注册蓝牙事件回调 - _serialBT.register_callback(bluetoothEventCallback); -} - -Bluetooth_Controller *getBluetoothControllerInstance() -{ - static Bluetooth_Controller controller; - return &controller; -} - -void Bluetooth_Controller::bluetoothEventCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) -{ - Bluetooth_Controller *controller = getBluetoothControllerInstance(); - if (controller) - { - controller->handleBluetoothEvent(event, param); - } -} - -void Bluetooth_Controller::handleBluetoothEvent(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) -{ - switch (event) - { - case ESP_SPP_SRV_OPEN_EVT: - if (!_isConnected) - { - _isConnected = true; - Serial.println("Client connected!"); - } - else - { - Serial.println("Connection rejected: already connected!"); - // 断开新连接 - _serialBT.disconnect(); - } - break; - - case ESP_SPP_CLOSE_EVT: - _isConnected = false; - Serial.println("Client disconnected!"); - break; - - default: - break; - } -} diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h index 5fd6b4ab..ab1bc6a7 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h @@ -4,22 +4,77 @@ #include #include +typedef void (*BlueToothDataReceivedCallback)(const uint8_t *buffer, size_t size); + +class BluetoothController +{ +public: + BluetoothController(); + + void begin(const char *bluetoothName); + void setDataReceivedCallback(BlueToothDataReceivedCallback callback); + + void sendData(const uint8_t *buffer, size_t size); + void write(const uint8_t *buffer, size_t size); + char receiveBuffer[100]; + +private: + bool _isConnected; // 用于跟踪是否有连接 + + BluetoothSerial _serialBT; + BlueToothDataReceivedCallback _dataReceivedCallback; + + static void _bluetoothEventCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); + void _handleBluetoothEvent(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); + + void _processReceivedData(const uint8_t *buffer, size_t size); + void _storeReceivedData(const uint8_t *buffer, size_t size); + + void _confirmRequestCallback(uint32_t numVal); + void _authCompleteCallback(boolean success); +}; + +#endif + + +#if false +#ifndef MYBLUETOOTH_H_ +#define MYBLUETOOTH_H_ + +#include +#include + +typedef void (*BlueTooth_DataReceivedCallback)(const uint8_t *buffer, size_t size); + class Bluetooth_Controller { public: Bluetooth_Controller(); - void begin(const char *deviceName); + + void begin(const char *blueTooth_name); + void onReceivedCallback(BlueTooth_DataReceivedCallback callback); + + void write(const uint8_t *buffer, size_t size); + char receiveBuffer[100]; private: - BluetoothSerial _serialBT; bool _isConnected; // 用于跟踪是否有连接 + + BluetoothSerial _serialBT; + BlueTooth_DataReceivedCallback _onDataReceivedCallback; + + static void _bluetoothEventCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); + void _handleBluetoothEvent(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); - static void bluetoothEventCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); - void handleBluetoothEvent(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); + void onDataReceived(const uint8_t *buffer, size_t size); + void onDataReceivedBuffer(const uint8_t *buffer, size_t size); + + void BTConfirmRequestCallback(uint32_t numVal); + void BTAuthCompleteCallback(boolean success); }; #endif - +#endif #if falae diff --git a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp index 1f6d0c1a..090eb455 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp @@ -1,5 +1,152 @@ #include "myWiFi.h" +WiFiController::WiFiController(uint16_t port, char maxClients) + : _port(port), _server(port), _maxClients(maxClients), _clientCount(0) +{ + memset(receiveBuffer, 0, sizeof(receiveBuffer)); + _clients = new ClientInfo[_maxClients]; // 动态分配数组 +} + +WiFiController::~WiFiController() +{ + delete[] _clients; // 释放内存 +} + +void WiFiController::begin(const char *ssid, const char *password) +{ + Serial.begin(115200); + + WiFi.mode(WIFI_AP); + WiFi.softAP(ssid, password); + + _server.begin(); + + _server.onClient([this](void *arg, AsyncClient *client) + { this->_handleClientLinkEvent(arg, client); }, (void *)this); + + Serial.print("AP IP address: "); + Serial.print(WiFi.softAPIP()); + Serial.printf(" port: %d\r\n", _port); +} + +void WiFiController::_handleClientLinkEvent(void *arg, AsyncClient *client) +{ + client->onData([this](void *arg, AsyncClient *client, void *data, size_t len) + { + this->_storeReceivedData(arg, client, data, len); + this->_processReceivedData(arg, client, data, len); }, arg); + + this->_handleClientConnected(arg, client); + + client->onDisconnect([this](void *arg, AsyncClient *client) + { this->_handleClientDisconnected(arg, client); }, arg); +} + +void WiFiController::setDataReceivedCallback(WiFiDataReceivedCallback callback) +{ + _dataReceivedCallback = callback; +} + +void WiFiController::_processReceivedData(void *arg, AsyncClient *client, void *data, size_t len) +{ + // 调用回调函数 + if (_dataReceivedCallback != nullptr) + { + _dataReceivedCallback(arg, client, data, len); + } +} + +void WiFiController::_storeReceivedData(void *arg, AsyncClient *client, void *data, size_t len) +{ + len = (len >= sizeof(receiveBuffer) ? (sizeof(receiveBuffer) - 1) : len); + memcpy(receiveBuffer, data, len); + receiveBuffer[len] = '\0'; // 确保字符串以 null 结尾 + + Serial.printf("Data received: %d-%s\r\n", len, receiveBuffer); +} + +void WiFiController::_handleClientConnected(void *arg, AsyncClient *client) +{ + if (_clientCount < _maxClients) + { + _clients[_clientCount].client = client; + _clients[_clientCount].ip = client->remoteIP(); + _clients[_clientCount].port = client->remotePort(); + _clientCount++; + + Serial.print("Client connected: "); + Serial.print(client->remoteIP()); + Serial.print(":"); + Serial.println(client->remotePort()); + } + else + { + Serial.println("Max clients reached, connection rejected."); + client->close(); + } +} + +void WiFiController::_handleClientDisconnected(void *arg, AsyncClient *client) +{ + for (int i = 0; i < _clientCount; i++) + { + if (_clients[i].client == client) + { + Serial.print("Client disconnected: "); + Serial.print(_clients[i].ip); + Serial.print(":"); + Serial.println(_clients[i].port); + + // 移除客户端信息 + for (int j = i; j < _clientCount - 1; j++) + { + _clients[j] = _clients[j + 1]; + } + _clientCount--; + break; + } + } +} + +void WiFiController::checkClients() +{ + for (int i = 0; i < _clientCount; i++) + { + if (_clients[i].client->connected()) + { + Serial.print("Client "); + Serial.print(_clients[i].ip); + Serial.print(":"); + Serial.print(_clients[i].port); + Serial.println(" is connected."); + } + else + { + Serial.print("Client "); + Serial.print(_clients[i].ip); + Serial.print(":"); + Serial.print(_clients[i].port); + Serial.println(" is disconnected."); + } + } +} + +void WiFiController::sendDataToAllClients(const char *data, size_t len) +{ + for (int i = 0; i < _clientCount; i++) + { + AsyncClient *client = _clients[i].client; + if (client->space() > len && client->canSend()) + { + client->add(data, len); + client->send(); + } + } +} + +#if false +#include "myWiFi.h" + WiFi_Controller::WiFi_Controller(uint16_t port, char maxClients) : _port(port), _server(port), MAX_CLIENTS(maxClients), _clientCount(0) { @@ -22,22 +169,22 @@ void WiFi_Controller::begin(const char *wifi_name, const char *wifi_password) _server.begin(); _server.onClient([this](void *arg, AsyncClient *client) - { this->onClientEvent(arg, client); }, (void *)this); + { this->onClientLinkEvent(arg, client); }, (void *)this); Serial.print("AP IP address: "); Serial.print(WiFi.softAPIP()); Serial.printf(" port: %d\r\n", _port); } -void WiFi_Controller::onClientEvent(void *arg, AsyncClient *client) +void WiFi_Controller::onClientLinkEvent(void *arg, AsyncClient *client) { - onClientConnected(arg, client); + client->onData([this](void *arg, AsyncClient *client, void *data, size_t len) + { this->onDataBuffer(arg, client, data, len), this->onDataReceived(arg, client, data, len); }, arg); - client->onDisconnect([this](void *arg, AsyncClient *client) - { this->onClientDisconnected(arg, client); }); + this->onClientConnected(arg, client); - client->onData([this](void *arg, AsyncClient *client, void *data, size_t len) - { this->onDataReceived(arg, client, data, len), this->onDataBuffer(arg, client, data, len); }); + client->onDisconnect([this](void *arg, AsyncClient *client) + { this->onClientDisconnected(arg, client); }, arg); } void WiFi_Controller::onDataCallback(WiFi_DataReceivedCallback callback) @@ -45,10 +192,6 @@ void WiFi_Controller::onDataCallback(WiFi_DataReceivedCallback callback) _onDataReceivedCallback = callback; } -char *WiFi_Controller::getReceiveBuffer() -{ - return _receiveBuffer; -} void WiFi_Controller::onDataReceived(void *arg, AsyncClient *client, void *data, size_t len) { @@ -61,9 +204,11 @@ void WiFi_Controller::onDataReceived(void *arg, AsyncClient *client, void *data, void WiFi_Controller::onDataBuffer(void *arg, AsyncClient *client, void *data, size_t len) { - char index = 0; - len = (len >= 100 ? 100 : len); + len = (len >= sizeof(_receiveBuffer) ? (sizeof(_receiveBuffer) - 1) : len); memcpy(_receiveBuffer, data, len); + _receiveBuffer[len] = '\0'; // 确保字符串以 null 结尾 + + Serial.printf("onData: %d-%s\r\n", len, _receiveBuffer); } void WiFi_Controller::onClientConnected(void *arg, AsyncClient *client) @@ -144,3 +289,63 @@ void WiFi_Controller::sendDataToAllClients(const char *data, size_t len) } } } +#endif +#if false +#include "myWiFi.h" + +WiFi_Controller::WiFi_Controller() : _server(80), _port(80) +{ + memset(_receiveBuffer, 0, sizeof(_receiveBuffer)); +} + +void WiFi_Controller::begin(const char *wifi_name, const char *wifi_password) +{ + Serial.begin(115200); + + WiFi.mode(WIFI_AP); + WiFi.softAP(wifi_name, wifi_password); + + _server.begin(); + + _server.onClient([this](void *arg, AsyncClient *client) + { this->onClientLinkEvent_(arg, client); }, (void *)this); + + Serial.print("AP IP address: "); + Serial.print(WiFi.softAPIP()); + Serial.printf(" port: %d\r\n", _port); +} + +void WiFi_Controller::onDataCallback_(WiFi_DataReceivedCallback callback) +{ + _onDataReceivedCallback = callback; +} + +void WiFi_Controller::onClientLinkEvent_(void *arg, AsyncClient *client) +{ + Serial.println("Client connected"); + + client->onData([this](void *arg, AsyncClient *client, void *data, size_t len) + { this->onClientBuffer_(arg, client, data, len), this->onClientData_(arg, client, data, len); }, (void *)this); + + // client->onData([this](void *arg, AsyncClient *client, void *data, size_t len) + // { this->onClientData_(arg, client, data, len); }, (void *)this); +} + +void WiFi_Controller::onClientBuffer_(void *arg, AsyncClient *client, void *data, size_t len) +{ + len = (len >= sizeof(_receiveBuffer) ? (sizeof(_receiveBuffer) - 1) : len); + memcpy(_receiveBuffer, data, len); + _receiveBuffer[len] = '\0'; // 确保字符串以 null 结尾 + + Serial.print("Data received: "); + Serial.write((uint8_t *)data, len); + Serial.println(); +} +void WiFi_Controller::onClientData_(void *arg, AsyncClient *client, void *data, size_t len) +{ + if (_onDataReceivedCallback != nullptr) + { + _onDataReceivedCallback(arg, client, data, len); + } +} +#endif diff --git a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h index d9692c71..6894b9a5 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h @@ -1,3 +1,57 @@ + +#ifndef MYWIFI_H +#define MYWIFI_H + +#include +#include +#include + +// 定义回调函数的类型 +typedef void (*WiFiDataReceivedCallback)(void *arg, AsyncClient *client, void *data, size_t len); + +struct ClientInfo +{ + AsyncClient *client; + IPAddress ip; + uint16_t port; +}; + +class WiFiController +{ +public: + WiFiController(uint16_t port = 80, char maxClients = 2); + ~WiFiController(); + + void begin(const char *ssid, const char *password); + void setDataReceivedCallback(WiFiDataReceivedCallback callback); // 设置回调函数 + + void sendDataToAllClients(const char *data, size_t len); + + char receiveBuffer[100]; + + void checkClients(); + +private: + char _maxClients; + char _clientCount; + + uint16_t _port; + AsyncServer _server; + ClientInfo *_clients; + WiFiDataReceivedCallback _dataReceivedCallback; // 定义回调函数指针 + + void _processReceivedData(void *arg, AsyncClient *client, void *data, size_t len); // 接收数据回调函数 + void _storeReceivedData(void *arg, AsyncClient *client, void *data, size_t len); + + void _handleClientConnected(void *arg, AsyncClient *client); + void _handleClientDisconnected(void *arg, AsyncClient *client); + void _handleClientLinkEvent(void *arg, AsyncClient *client); +}; +// processReceivedData +// storeReceivedData +#endif + +#if false #ifndef MYWIFI_H #define MYWIFI_H @@ -23,26 +77,59 @@ public: void begin(const char *wifi_name, const char *wifi_password); void checkClients(); - void sendDataToAllClients(const char *data, size_t len); void onDataCallback(WiFi_DataReceivedCallback callback); // 设置回调函数 - char *getReceiveBuffer(); // 获取接收缓存区指针 + void sendDataToAllClients(const char *data, size_t len); + char _receiveBuffer[100]; private: - uint16_t _port; - char _receiveBuffer[100]; char MAX_CLIENTS; char _clientCount; - ClientInfo *_clients; + uint16_t _port; AsyncServer _server; + ClientInfo *_clients; + WiFi_DataReceivedCallback _onDataReceivedCallback; // 定义回调函数指针 void onDataReceived(void *arg, AsyncClient *client, void *data, size_t len); // 接收数据回调函数 void onDataBuffer(void *arg, AsyncClient *client, void *data, size_t len); + void onClientConnected(void *arg, AsyncClient *client); void onClientDisconnected(void *arg, AsyncClient *client); - void onClientEvent(void *arg, AsyncClient *client); + void onClientLinkEvent(void *arg, AsyncClient *client); }; #endif +#endif + +#if false +#ifndef MYWIFI_H_ +#define MYWIFI_H_ + +#include +#include + +// 定义回调函数的类型 +typedef void (*WiFi_DataReceivedCallback)(void *arg, AsyncClient *client, void *data, size_t len); + +class WiFi_Controller +{ +public: + WiFi_Controller(); + void begin(const char *wifi_name, const char *wifi_password); + char _receiveBuffer[100]; + void onDataCallback_(WiFi_DataReceivedCallback callback); // 设置回调函数 + +private: + uint16_t _port; + AsyncServer _server; + WiFi_DataReceivedCallback _onDataReceivedCallback; // 定义回调函数指针 + + void onClientLinkEvent_(void *arg, AsyncClient *client); + void onClientBuffer_(void *arg, AsyncClient *client, void *data, size_t len); + void onClientData_(void *arg, AsyncClient *client, void *data, size_t len); +}; + +#endif +#endif diff --git a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp index 1ae19eae..49084e97 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp @@ -1,7 +1,7 @@ #include "myXHZS.h" WiFi_Controller espWifi; -// BlueTooth_Controller espBT; +Bluetooth_Controller espBT; void wifi_onData(void *arg, AsyncClient *client, void *data, size_t len) { @@ -11,8 +11,8 @@ void wifi_onData(void *arg, AsyncClient *client, void *data, size_t len) void blueTooth_onData(const uint8_t *buffer, size_t size) { - // espBT.send(buffer, size); - // Serial.write(buffer, size); + espBT.send(buffer, size); + Serial.write(buffer, size); } void XHZS_begin(const char *wifi_name, const char *wifi_password) diff --git a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h index 6b12c3ac..53930326 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h @@ -6,6 +6,5 @@ void XHZS_begin(const char *wifi_name, const char *wifi_password); void uart_EnableSerialFunction(bool isEnable); -void status_update(); #endif diff --git a/PlatformIO_ESP32_oscilloscope/src/main.cpp b/PlatformIO_ESP32_oscilloscope/src/main.cpp index 20209440..bd191493 100644 --- a/PlatformIO_ESP32_oscilloscope/src/main.cpp +++ b/PlatformIO_ESP32_oscilloscope/src/main.cpp @@ -1,20 +1,65 @@ -#if false +#include +#include +#include + +WiFiController wifi; +BluetoothController bluetooth; -#include "myXHZS.h" +void WiFi_callback(void *arg, AsyncClient *client, void *data, size_t len) +{ + // client->write((char *)data, len); + // Serial.write((char *)data, len); + + char charBuffer[] = "can"; + client->write(charBuffer, strlen(charBuffer)); +} + +void BT_callback(const uint8_t *data, size_t len) +{ + // bluetooth.write(data, len); + // Serial.write(data, len); + + char charBuffer[] = "can"; + bluetooth.write((uint8_t *)charBuffer, strlen(charBuffer)); +} void setup() { - XHZS_begin("XHZS", "12345678"); - uart_EnableSerialFunction(true); + Serial.begin(115200); + wifi.begin("ESP32_AP", "12345678"); + wifi.setDataReceivedCallback(WiFi_callback); } void loop() { - // status_update(); - delay(1000); + // 主循环代码 + delay(2000); + char *buf = wifi.receiveBuffer; + // 检查缓冲区是否有数据 + if (buf[0] != '\0') + { + Serial.printf("buf: %s\n", buf); + } } -#endif +// void setup() +// { +// Serial.begin(115200); +// bluetooth.begin("esp32_BT"); +// bluetooth.onDataCallback(BT_callback); +// } + +// void loop() +// { +// // 主循环代码 +// delay(1000); +// char *buf = bluetooth._receiveBuffer; +// // 检查缓冲区是否有数据 +// if (buf[0] != '\0') +// { +// Serial.printf("buf: %s\n", buf); +// } +// } #if false /* @@ -191,18 +236,3 @@ void loop() delay(3000); } #endif - -#include -#include - -Bluetooth_Controller btController; - -void setup() -{ - btController.begin("ESP32_BT_Hotspot"); -} - -void loop() -{ - // 主循环代码 -} -- Gitee From dbd6692619cdb5696e38f9fa4b5fab22d27941bb Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Fri, 23 Aug 2024 15:47:17 +0800 Subject: [PATCH 16/18] =?UTF-8?q?=E8=93=9D=E7=89=99=E7=B1=BB=E5=92=8CWi-Fi?= =?UTF-8?q?=E7=B1=BB=E6=B5=8B=E8=AF=95=E5=B0=81=E8=A3=85=E6=88=90=E5=8A=9F?= =?UTF-8?q?=EF=BC=8C=E5=8F=AF=E4=BB=A5=E8=BF=90=E8=A1=8C=E7=A4=BA=E6=B3=A2?= =?UTF-8?q?=E5=99=A8=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/myBlueTooth/myBlueTooth.cpp | 22 +- .../lib/myBlueTooth/myBlueTooth.h | 10 +- .../lib/myWiFi/myWiFi.cpp | 17 +- .../lib/myWiFi/myWiFi.h | 5 +- .../lib/myXHZS/myXHZS.cpp | 405 +++++++++++++++++- .../lib/myXHZS/myXHZS.h | 8 +- PlatformIO_ESP32_oscilloscope/src/main.cpp | 34 +- 7 files changed, 487 insertions(+), 14 deletions(-) diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp index 2daf033d..31dda747 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -13,17 +13,17 @@ void BluetoothController::begin(const char *bluetoothName) _serialBT.enableSSP(); _serialBT.onConfirmRequest([this](uint32_t numVal) - { this->_confirmRequestCallback(numVal); }); + { this->_confirmRequestCallback(numVal); }); _serialBT.onAuthComplete([this](boolean success) - { this->_authCompleteCallback(true); }); + { this->_authCompleteCallback(true); }); // 蓝牙 spp 配对 end // 注册蓝牙事件回调 _serialBT.register_callback(_bluetoothEventCallback); _serialBT.onData([this](const uint8_t *buffer, size_t size) - { this->_storeReceivedData(buffer, size); this->_processReceivedData(buffer, size); }); + { this->_storeReceivedData(buffer, size); this->_processReceivedData(buffer, size); }); // 设置蓝牙名称 _serialBT.begin(bluetoothName); @@ -50,6 +50,22 @@ void BluetoothController::_storeReceivedData(const uint8_t *buffer, size_t size) receiveBuffer[size] = '\0'; // 确保字符串以 null 结尾 } +void BluetoothController::printf(const char *format, ...) +{ + va_list args; + va_start(args, format); + vsnprintf(printfBuffer, sizeof(printfBuffer), format, args); + va_end(args); + + // 假设这里将数据发送到蓝牙 + sendData((const uint8_t *)printfBuffer, strlen(printfBuffer)); +} + +void BluetoothController::sendArray(const uint8_t *buffer, size_t size) +{ + sendData(buffer, size); +} + void BluetoothController::sendData(const uint8_t *buffer, size_t size) { _serialBT.write(buffer, size); diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h index ab1bc6a7..7b28d579 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h @@ -14,13 +14,18 @@ public: void begin(const char *bluetoothName); void setDataReceivedCallback(BlueToothDataReceivedCallback callback); + void printf(const char *format, ...); + void sendArray(const uint8_t *buffer, size_t size); + + void sendData(const uint8_t *buffer, size_t size); void write(const uint8_t *buffer, size_t size); char receiveBuffer[100]; private: + char printfBuffer[100]; bool _isConnected; // 用于跟踪是否有连接 - + BluetoothSerial _serialBT; BlueToothDataReceivedCallback _dataReceivedCallback; @@ -34,8 +39,7 @@ private: void _authCompleteCallback(boolean success); }; -#endif - +#endif #if false #ifndef MYBLUETOOTH_H_ diff --git a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp index 090eb455..a3a9d110 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.cpp @@ -62,7 +62,7 @@ void WiFiController::_storeReceivedData(void *arg, AsyncClient *client, void *da memcpy(receiveBuffer, data, len); receiveBuffer[len] = '\0'; // 确保字符串以 null 结尾 - Serial.printf("Data received: %d-%s\r\n", len, receiveBuffer); + // Serial.printf("Data received: %d-%s\r\n", len, receiveBuffer); } void WiFiController::_handleClientConnected(void *arg, AsyncClient *client) @@ -131,6 +131,21 @@ void WiFiController::checkClients() } } +void WiFiController::printf(const char *format, ...) +{ + va_list args; + va_start(args, format); + vsnprintf(printfBuffer, sizeof(printfBuffer), format, args); + va_end(args); + + sendDataToAllClients(printfBuffer, strlen(printfBuffer)); +} + +void WiFiController::sendArray(const uint8_t *data, size_t len) +{ + sendDataToAllClients((char *)data, len); +} + void WiFiController::sendDataToAllClients(const char *data, size_t len) { for (int i = 0; i < _clientCount; i++) diff --git a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h index 6894b9a5..022f2d7d 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h @@ -25,13 +25,16 @@ public: void begin(const char *ssid, const char *password); void setDataReceivedCallback(WiFiDataReceivedCallback callback); // 设置回调函数 - void sendDataToAllClients(const char *data, size_t len); + void printf(const char *format, ...); + void sendArray(const uint8_t *data, size_t len); char receiveBuffer[100]; + void sendDataToAllClients(const char *data, size_t len); void checkClients(); private: +char printfBuffer[100]; char _maxClients; char _clientCount; diff --git a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp index 49084e97..6b8f5cbb 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp @@ -1,8 +1,236 @@ #include "myXHZS.h" -WiFi_Controller espWifi; -Bluetooth_Controller espBT; +WiFiController espWifi; +BluetoothController espBT; +enum WIRELESS_DEVICE +{ + EMPTY, + BLUETOOTH_DEVICE, + WIFI_DEVICE +}; + +enum WIRELESS_DEVICE wireless_device = WIFI_DEVICE; + +void XHZS_begin(const char *device_name, const char *device_password) +{ + switch (wireless_device) + { + case BLUETOOTH_DEVICE: + espBT.begin(device_name); + break; + + case WIFI_DEVICE: + espWifi.begin(device_name, device_password); + break; + + case EMPTY: + default: + Serial.printf("begin error, no wireless device.\r\n"); + break; + } +} + +void uart_printf(const char *format, ...) +{ + char printfBuffer[100]; + va_list args; + va_start(args, format); + vsnprintf(printfBuffer, sizeof(printfBuffer), format, args); + va_end(args); + + Uart_SendArray((uint8_t *)printfBuffer, strlen(printfBuffer)); +} + +// 接收数据缓冲区 +char *uart_receiveBuffer() +{ + switch (wireless_device) + { + case BLUETOOTH_DEVICE: + return espBT.receiveBuffer; + break; + + case WIFI_DEVICE: + return espWifi.receiveBuffer; + break; + + case EMPTY: + default: + Serial.printf("receiveBuffer error, no wireless device.\r\n"); + return nullptr; + break; + } +} + +// 发送一个字节 +void Uart_SendByte(uint8_t *dataByte) +{ + Uart_SendArray(dataByte, 1); +} + +// 发送数组 +void Uart_SendArray(uint8_t *dataArray, size_t dataSize) +{ + switch (wireless_device) + { + + case BLUETOOTH_DEVICE: + espBT.sendArray(dataArray, dataSize); + break; + + case WIFI_DEVICE: + espWifi.sendArray(dataArray, dataSize); + break; + + case EMPTY: + default: + Serial.printf("No wireless device, please connect to WiFi or Bluetooth.\r\n"); + break; + } +} + +/************************************************************** + *函数名称:XHZS_SendWave + *简 介:学会助手APP虚拟示波器下位机程序(兼容山外) + *输 入:wareaddr:数据数组起始地址 waresize:数组大小 + *输 出:无 + *注意事项:无 + **************************************************************/ +void XHZS_SendWave(uint8_t *waveAddr, uint16_t waveSize) +{ + uint8_t cmdf[2] = {0x03, 0xFC}; // 帧头 + uint8_t cmdr[2] = {0xFC, 0x03}; // 帧尾 + + Uart_SendArray(cmdf, sizeof(cmdf)); // 发送帧头 + Uart_SendArray(waveAddr, waveSize); // 发送数据 + Uart_SendArray(cmdr, sizeof(cmdr)); // 发送帧尾 +} +/************************************************************** + *函数名称: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((uint8_t *)Wave, sizeof(Wave)); + + delay(50); + } +} + +/************************************************************** + *函数名称:OscGetFloat + *简 介:将字符转为浮点数 + *输 入:p:字符串;value:用于获取输出值 + *输 出:(==0)转成功;(!=0)转失败 + *注意事项:虚拟示波器调参用,以','为数据分隔符 + **************************************************************/ +char OscGetFloat(const char *p, float *value) +{ + int i; + int negative_flag = 1; + int float_flag = 0; + float tmp_value = 0; + for (i = 0; i < 16; i++) + { + if (*p == ',') // 检测到分隔符,返回值 + { + tmp_value *= negative_flag; + *value = tmp_value; + return 0; + } + else if (*p == '-') // 检测到负号 + { + negative_flag = -1; + } + else if (*p == '.') // 检测到小数点 + { + float_flag = -1; + } + else if (*p >= '0' && *p <= '9') + { + if (float_flag == 0) + { + tmp_value = tmp_value * 10 + (*p - 0x30); + } + else + { + tmp_value += (*p - 0x30) * pow(10, float_flag); + float_flag--; + } + } + else + { + return 1; + } + p++; + } + return 2; +} +/************************************************************** + *函数名称:OscGetValue + *简 介:从字符串中获取指定参数数值 + *输 入:p:字符串,以'\0'结尾,否则将出错 + * name:指定参数名称,如"c12"获取第1组第2个参数; + * value:用于获取输出值; + *输 出:(==0)转成功;(!=0)转失败 + *注意事项:虚拟示波器调参用,字符串中以'\0'为结束符 + **************************************************************/ +char OscGetValue(const char *p, const char *name, float *value) +{ + float tmp_value; + int feedback; + p = strstr(p, name); + if (p == NULL) + { + return 1; + } + p += 4; + feedback = OscGetFloat(p, &tmp_value); + if (feedback == 0) + { + *value = tmp_value; + return 0; + } + return 2; +} + +/** + * @brief 示波器的发送数据和接收数据 + * + */ +void OscilloscopeSendsAndReceivesData() +{ + float value1, value2, value3, value4; + char *USART_RX_BUF = uart_receiveBuffer(); + + while (true) + { + OscGetValue(USART_RX_BUF, "c11", &value1); + OscGetValue(USART_RX_BUF, "c12", &value2); + OscGetValue(USART_RX_BUF, "c13", &value3); + OscGetValue(USART_RX_BUF, "c14", &value4); + + // 处理数据 + uart_printf("w:%.2f,%.2f,%.2f,%.2f\r\n", value1, value2, value3, value4); + delay(50); + } +} + +#if false void wifi_onData(void *arg, AsyncClient *client, void *data, size_t len) { client->write((char *)data, len); @@ -31,5 +259,178 @@ void uart_EnableSerialFunction(bool isEnable) void status_update() { +} +#endif + +#if false + +#include "stm32f10x.h" +#include "sys.h" +#include "led.h" +#include "delay.h" +#include "usart.h" +#include "math.h" +#include "string.h" + +// 发送一个字节 +void Uart_SendByte(USART_TypeDef *UARTx, u8 byte) +{ + USART_SendData(UARTx, byte); + while ((UARTx->SR & 0X40) == 0) + ; // 等待发送结束 +} +// 发送数组 +void Uart_SendArray(USART_TypeDef *UARTx, uint8_t *array, uint32_t num) +{ + while (num--) + { + Uart_SendByte(UARTx, *array); + array++; + } +} + +/************************************************************** + *函数名称:XHZS_SendWave + *简 介:学会助手APP虚拟示波器下位机程序(兼容山外) + *输 入:wareaddr:数据数组起始地址 waresize:数组大小 + *输 出:无 + *注意事项:无 + **************************************************************/ +void XHZS_SendWave(uint8_t *waveaddr, uint16_t wavesize) +{ + uint8_t cmdf[2] = {0x03, 0xFC}; // 帧头 + uint8_t cmdr[2] = {0xFC, 0x03}; // 帧尾 + + Uart_SendArray(USART1, cmdf, sizeof(cmdf)); // 发送帧头 + Uart_SendArray(USART1, waveaddr, wavesize); // 发送数据 + Uart_SendArray(USART1, cmdr, sizeof(cmdr)); // 发送帧尾 +} +/************************************************************** + *函数名称: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((uint8_t *)Wave, sizeof(Wave)); + + delay_ms(20); + } +} + +/************************************************************** + *函数名称:OscGetFloat + *简 介:将字符转为浮点数 + *输 入:p:字符串;value:用于获取输出值 + *输 出:(==0)转成功;(!=0)转失败 + *注意事项:虚拟示波器调参用,以','为数据分隔符 + **************************************************************/ +char OscGetFloat(const char *p, float *value) +{ + int i; + int negative_flag = 1; + int float_flag = 0; + float tmp_value = 0; + for (i = 0; i < 16; i++) + { + if (*p == ',') // 检测到分隔符,返回值 + { + tmp_value *= negative_flag; + *value = tmp_value; + return 0; + } + else if (*p == '-') // 检测到负号 + { + negative_flag = -1; + } + else if (*p == '.') // 检测到小数点 + { + float_flag = -1; + } + else if (*p >= '0' && *p <= '9') + { + if (float_flag == 0) + { + tmp_value = tmp_value * 10 + (*p - 0x30); + } + else + { + tmp_value += (*p - 0x30) * pow(10, float_flag); + float_flag--; + } + } + else + { + return 1; + } + p++; + } + return 2; } +/************************************************************** + *函数名称:OscGetValue + *简 介:从字符串中获取指定参数数值 + *输 入:p:字符串,以'\0'结尾,否则将出错 + * name:指定参数名称,如"c12"获取第1组第2个参数; + * value:用于获取输出值; + *输 出:(==0)转成功;(!=0)转失败 + *注意事项:虚拟示波器调参用,字符串中以'\0'为结束符 + **************************************************************/ +char OscGetValue(const char *p, const char *name, float *value) +{ + float tmp_value; + int feedback; + p = strstr(p, name); + if (p == NULL) + { + return 1; + } + p += 4; + feedback = OscGetFloat(p, &tmp_value); + if (feedback == 0) + { + *value = tmp_value; + return 0; + } + return 2; +} + +int main(void) +{ + float v1 = 0, v2 = 0, v3 = 0, v4 = 0; + NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); + LED_Init(); + delay_init(); + uart_init(115200); + + while (1) + { + if (USART_RX_STA == 2) + { + USART_RX_BUF[USART_RX_NUM] = '\0'; + OscGetValue((const char *)USART_RX_BUF, "c11", &v1); + OscGetValue((const char *)USART_RX_BUF, "c12", &v2); + OscGetValue((const char *)USART_RX_BUF, "c13", &v3); + OscGetValue((const char *)USART_RX_BUF, "c14", &v4); + + printf("w:%.2f,%.2f,%.2f,%.2f\r\n", v1, v2, v3, v4); + USART_RX_NUM = 0; + USART_RX_STA = 0; + } + delay_ms(50); + } +} + +#endif \ No newline at end of file diff --git a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h index 53930326..c560da8f 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h @@ -4,7 +4,11 @@ #include "myBlueTooth.h" #include "myWiFi.h" -void XHZS_begin(const char *wifi_name, const char *wifi_password); -void uart_EnableSerialFunction(bool isEnable); +void XHZS_begin(const char *device_name, const char *device_password); + +void uart_printf(const char *format, ...); +void Uart_SendArray(uint8_t *dataArray, size_t dataSize); + +void OscilloscopeSendsAndReceivesData(); #endif diff --git a/PlatformIO_ESP32_oscilloscope/src/main.cpp b/PlatformIO_ESP32_oscilloscope/src/main.cpp index bd191493..bcd7c19b 100644 --- a/PlatformIO_ESP32_oscilloscope/src/main.cpp +++ b/PlatformIO_ESP32_oscilloscope/src/main.cpp @@ -1,6 +1,34 @@ #include -#include -#include +#include "myXHZS.h" + + +void setup() +{ + XHZS_begin("esp32_BT", "12345678"); + + OscilloscopeSendsAndReceivesData(); +} + +void loop() +{ + delay(3000); +} + + + + + + + + + + + + + + +#if false +#include WiFiController wifi; BluetoothController bluetooth; @@ -61,6 +89,8 @@ void loop() // } // } +#endif + #if false /* 选择工程环境:ESP32_WROVER_Kit -- Gitee From f876d22242b0af20b6db61c7581026b89ea112df Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Fri, 23 Aug 2024 16:52:16 +0800 Subject: [PATCH 17/18] =?UTF-8?q?Wi-Fi=20and=20bluetooth=20=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=EF=BC=8C=E4=B8=B2=E5=8F=A3=20ok?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.vscode/c_cpp_properties.json | 4 +- .../lib/myBlueTooth/myBlueTooth.cpp | 5 ++ .../lib/myBlueTooth/myBlueTooth.h | 6 +-- .../lib/myWiFi/myWiFi.h | 6 ++- .../lib/myXHZS/myXHZS.cpp | 47 +++++++++++++++---- .../lib/myXHZS/myXHZS.h | 26 ++++++++++ PlatformIO_ESP32_oscilloscope/src/main.cpp | 5 +- 7 files changed, 82 insertions(+), 17 deletions(-) diff --git a/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json b/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json index 4a65618f..529d3627 100644 --- a/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json +++ b/PlatformIO_ESP32_oscilloscope/.vscode/c_cpp_properties.json @@ -10,6 +10,7 @@ "includePath": [ "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/include", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/src", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myWiFi", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", @@ -212,7 +213,6 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/cores/esp32", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/variants/esp32", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/Interrupts_and_timers", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/Unity/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ArduinoOTA/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src", @@ -252,6 +252,7 @@ "path": [ "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/include", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/src", + "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myWiFi", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth", @@ -454,7 +455,6 @@ "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/cores/esp32", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/variants/esp32", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/Interrupts_and_timers", - "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/lib/myXHZS", "E:/PlatformIO_vscode/git_XHZS/esp32_oscilloscope/PlatformIO_ESP32_oscilloscope/.pio/libdeps/esp32dev/Unity/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/ArduinoOTA/src", "C:/Users/Sword/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src", diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp index 31dda747..e8e94ba9 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -50,6 +50,11 @@ void BluetoothController::_storeReceivedData(const uint8_t *buffer, size_t size) receiveBuffer[size] = '\0'; // 确保字符串以 null 结尾 } +void BluetoothController::write(const uint8_t *buffer, size_t size) +{ + sendData(buffer, size); +} + void BluetoothController::printf(const char *format, ...) { va_list args; diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h index 7b28d579..0caf3b92 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.h @@ -14,15 +14,15 @@ public: void begin(const char *bluetoothName); void setDataReceivedCallback(BlueToothDataReceivedCallback callback); + void write(const uint8_t *buffer, size_t size); void printf(const char *format, ...); void sendArray(const uint8_t *buffer, size_t size); - - void sendData(const uint8_t *buffer, size_t size); - void write(const uint8_t *buffer, size_t size); char receiveBuffer[100]; private: + void sendData(const uint8_t *buffer, size_t size); + char printfBuffer[100]; bool _isConnected; // 用于跟踪是否有连接 diff --git a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h index 022f2d7d..4e95fcd0 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myWiFi/myWiFi.h @@ -25,16 +25,18 @@ public: void begin(const char *ssid, const char *password); void setDataReceivedCallback(WiFiDataReceivedCallback callback); // 设置回调函数 + // void write(const char *data, size_t len); void printf(const char *format, ...); void sendArray(const uint8_t *data, size_t len); char receiveBuffer[100]; - void sendDataToAllClients(const char *data, size_t len); void checkClients(); private: -char printfBuffer[100]; + void sendDataToAllClients(const char *data, size_t len); + + char printfBuffer[100]; char _maxClients; char _clientCount; diff --git a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp index 6b8f5cbb..70404265 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp @@ -3,14 +3,7 @@ WiFiController espWifi; BluetoothController espBT; -enum WIRELESS_DEVICE -{ - EMPTY, - BLUETOOTH_DEVICE, - WIFI_DEVICE -}; - -enum WIRELESS_DEVICE wireless_device = WIFI_DEVICE; +enum WIRELESS_DEVICE wireless_device = BLUETOOTH_DEVICE; void XHZS_begin(const char *device_name, const char *device_password) { @@ -63,6 +56,42 @@ char *uart_receiveBuffer() } } +/** + * @brief 蓝牙串口接收数据回调函数 + * + * 需要提前准备好,一个函数用于回调函数和处理接收到的数据 + */ +void uart_EnableSerialCallback() +{ + switch (wireless_device) + { + case BLUETOOTH_DEVICE: + espBT.setDataReceivedCallback(bluetooth_SerialReceiveCallback); + break; + + case WIFI_DEVICE: + espWifi.setDataReceivedCallback(wifi_SerialReceiveCallback); + break; + + case EMPTY: + default: + Serial.printf("begin error, no wireless device.\r\n"); + break; + } +} + +void bluetooth_SerialReceiveCallback(const uint8_t *buffer, size_t size) +{ + espBT.write(buffer, size); + Serial.write(buffer, size); +} + +void wifi_SerialReceiveCallback(void *arg, AsyncClient *client, void *data, size_t len) +{ + client->write((char *)data, len); + Serial.write((char *)data, len); +} + // 发送一个字节 void Uart_SendByte(uint8_t *dataByte) { @@ -210,7 +239,7 @@ char OscGetValue(const char *p, const char *name, float *value) /** * @brief 示波器的发送数据和接收数据 - * + * */ void OscilloscopeSendsAndReceivesData() { diff --git a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h index c560da8f..b79d5b0b 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h @@ -4,11 +4,37 @@ #include "myBlueTooth.h" #include "myWiFi.h" +enum WIRELESS_DEVICE +{ + EMPTY, + BLUETOOTH_DEVICE, + WIFI_DEVICE +}; + void XHZS_begin(const char *device_name, const char *device_password); void uart_printf(const char *format, ...); +char *uart_receiveBuffer(); + +void Uart_SendByte(uint8_t *dataByte); void Uart_SendArray(uint8_t *dataArray, size_t dataSize); +// 串口功能 start +void uart_EnableSerialCallback(); +void bluetooth_SerialReceiveCallback(const uint8_t *buffer, size_t size); +void wifi_SerialReceiveCallback(void *arg, AsyncClient *client, void *data, size_t len); +// 串口功能 start + +// 示波器功能 start +void XHZS_SendWave(uint8_t *waveAddr, uint16_t waveSize); +char OscGetFloat(const char *p, float *value); +char OscGetValue(const char *p, const char *name, float *value); + +void SendWave(); void OscilloscopeSendsAndReceivesData(); +// 示波器功能 end + +// 遥控器功能 start +// 遥控器功能 end #endif diff --git a/PlatformIO_ESP32_oscilloscope/src/main.cpp b/PlatformIO_ESP32_oscilloscope/src/main.cpp index bcd7c19b..b5cf9a9d 100644 --- a/PlatformIO_ESP32_oscilloscope/src/main.cpp +++ b/PlatformIO_ESP32_oscilloscope/src/main.cpp @@ -6,7 +6,10 @@ void setup() { XHZS_begin("esp32_BT", "12345678"); - OscilloscopeSendsAndReceivesData(); + uart_EnableSerialCallback(); + + // OscilloscopeSendsAndReceivesData(); + } void loop() -- Gitee From 66c3fb3b39b43f420a29792734a5bcd81e4d6fa4 Mon Sep 17 00:00:00 2001 From: klein1412 <1148878282@qq.com> Date: Sun, 25 Aug 2024 00:52:11 +0800 Subject: [PATCH 18/18] =?UTF-8?q?wifi=20and=20bluetooth,=20=E5=86=99?= =?UTF-8?q?=E9=81=A5=E6=8E=A7=E5=99=A8=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: klein1412 <1148878282@qq.com> --- .../lib/myBlueTooth/myBlueTooth.cpp | 13 +- .../lib/myXHZS/myXHZS.cpp | 439 +++++++++--------- .../lib/myXHZS/myXHZS.h | 97 +++- PlatformIO_ESP32_oscilloscope/src/main.cpp | 21 +- 4 files changed, 334 insertions(+), 236 deletions(-) diff --git a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp index e8e94ba9..9ee84d14 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myBlueTooth/myBlueTooth.cpp @@ -771,7 +771,7 @@ void RemoteControlOnDataReceive(const uint8_t *buffer, size_t size) // 使用结构体的成员方法 RemoteControlInterruptMode22(&outputBuffer); // 使用 switch case 模式 - // RemoteControlInterruptMode33(cmd, data, outputBuffer.LEFT_x, outputBuffer.LEFT_y, outputBuffer.RIGHT_x, outputBuffer.RIGHT_y); + RemoteControlInterruptMode33(cmd, data, outputBuffer.LEFT_x, outputBuffer.LEFT_y, outputBuffer.RIGHT_x, outputBuffer.RIGHT_y); } // 获取摇杆数值 @@ -996,6 +996,17 @@ struct myStruct char e; }; +void struct_number_size() +{ + student stu = {}; // 修正拼写错误 + int mySize = sizeof(stu); // 使用已声明的变量 stu + int numberSize = sizeof(stu.begin); // 使用已声明的变量 stu + int size = mySize / numberSize; // 计算剩余字节数 + Serial.printf("%d / %d = %d\r\n", mySize, numberSize, size); +} + + + /* 如何把结构体像数组一样使用?可以做到,前提条件时是结构体成员是单一类型时,结构体内部就像一个数组, 获取结构体的首地址,在基础上进行偏移,可以像数组一样访问了 diff --git a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp index 70404265..e0ff8513 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp +++ b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.cpp @@ -3,7 +3,7 @@ WiFiController espWifi; BluetoothController espBT; -enum WIRELESS_DEVICE wireless_device = BLUETOOTH_DEVICE; +enum enumWirelessDevice wireless_device = BLUETOOTH_DEVICE; void XHZS_begin(const char *device_name, const char *device_password) { @@ -24,6 +24,27 @@ void XHZS_begin(const char *device_name, const char *device_password) } } +// 接收数据缓冲区 +char *uart_receiveBuffer() +{ + switch (wireless_device) + { + case BLUETOOTH_DEVICE: + return espBT.receiveBuffer; + break; + + case WIFI_DEVICE: + return espWifi.receiveBuffer; + break; + + case EMPTY: + default: + Serial.printf("receiveBuffer error, no wireless device.\r\n"); + return nullptr; + break; + } +} + void uart_printf(const char *format, ...) { char printfBuffer[100]; @@ -35,42 +56,48 @@ void uart_printf(const char *format, ...) Uart_SendArray((uint8_t *)printfBuffer, strlen(printfBuffer)); } -// 接收数据缓冲区 -char *uart_receiveBuffer() +// 发送一个字节 +void Uart_SendByte(uint8_t *dataByte) +{ + Uart_SendArray(dataByte, 1); +} + +// 发送数组 +void Uart_SendArray(uint8_t *dataArray, size_t dataSize) { switch (wireless_device) { + case BLUETOOTH_DEVICE: - return espBT.receiveBuffer; + espBT.sendArray(dataArray, dataSize); break; case WIFI_DEVICE: - return espWifi.receiveBuffer; + espWifi.sendArray(dataArray, dataSize); break; case EMPTY: default: - Serial.printf("receiveBuffer error, no wireless device.\r\n"); - return nullptr; + Serial.printf("No wireless device, please connect to WiFi or Bluetooth.\r\n"); break; } } /** * @brief 蓝牙串口接收数据回调函数 - * - * 需要提前准备好,一个函数用于回调函数和处理接收到的数据 + * + * 需要提前准备好一个函数,用于回调函数的接收和逻辑处理 */ void uart_EnableSerialCallback() { switch (wireless_device) { case BLUETOOTH_DEVICE: - espBT.setDataReceivedCallback(bluetooth_SerialReceiveCallback); + espBT.setDataReceivedCallback(uart_bluetoothDataReceivedCallback); break; case WIFI_DEVICE: - espWifi.setDataReceivedCallback(wifi_SerialReceiveCallback); + espWifi.setDataReceivedCallback(uart_wifiDataReceivedCallback); break; case EMPTY: @@ -80,45 +107,18 @@ void uart_EnableSerialCallback() } } -void bluetooth_SerialReceiveCallback(const uint8_t *buffer, size_t size) +void uart_bluetoothDataReceivedCallback(const uint8_t *buffer, size_t size) { espBT.write(buffer, size); Serial.write(buffer, size); } -void wifi_SerialReceiveCallback(void *arg, AsyncClient *client, void *data, size_t len) +void uart_wifiDataReceivedCallback(void *arg, AsyncClient *client, void *data, size_t len) { client->write((char *)data, len); Serial.write((char *)data, len); } -// 发送一个字节 -void Uart_SendByte(uint8_t *dataByte) -{ - Uart_SendArray(dataByte, 1); -} - -// 发送数组 -void Uart_SendArray(uint8_t *dataArray, size_t dataSize) -{ - switch (wireless_device) - { - - case BLUETOOTH_DEVICE: - espBT.sendArray(dataArray, dataSize); - break; - - case WIFI_DEVICE: - espWifi.sendArray(dataArray, dataSize); - break; - - case EMPTY: - default: - Serial.printf("No wireless device, please connect to WiFi or Bluetooth.\r\n"); - break; - } -} - /************************************************************** *函数名称:XHZS_SendWave *简 介:学会助手APP虚拟示波器下位机程序(兼容山外) @@ -241,7 +241,7 @@ char OscGetValue(const char *p, const char *name, float *value) * @brief 示波器的发送数据和接收数据 * */ -void OscilloscopeSendsAndReceivesData() +void Oscilloscope_SendsAndReceivesData() { float value1, value2, value3, value4; char *USART_RX_BUF = uart_receiveBuffer(); @@ -259,207 +259,232 @@ void OscilloscopeSendsAndReceivesData() } } -#if false -void wifi_onData(void *arg, AsyncClient *client, void *data, size_t len) +void RemoteControl_EnableCallbackReceive() { - client->write((char *)data, len); - Serial.write((char *)data, len); -} + switch (wireless_device) + { + case BLUETOOTH_DEVICE: + espBT.setDataReceivedCallback(RemoteControl_BluetoothCallbackReceive); + break; -void blueTooth_onData(const uint8_t *buffer, size_t size) -{ - espBT.send(buffer, size); - Serial.write(buffer, size); + case WIFI_DEVICE: + espWifi.setDataReceivedCallback(RemoteControl_WifiCallbackReceive); + break; + + case EMPTY: + default: + Serial.printf("begin error, no wireless device.\r\n"); + break; + } } -void XHZS_begin(const char *wifi_name, const char *wifi_password) +void RemoteControl_BluetoothCallbackReceive(const uint8_t *buffer, size_t size) { - espWifi.begin(wifi_name, wifi_password); - - // espBT.begin(wifi_name); + RemoteControl_CallbackReceiveAnalysis(buffer, size); } -void uart_EnableSerialFunction(bool isEnable) +void RemoteControl_WifiCallbackReceive(void *arg, AsyncClient *client, void *data, size_t len) { - // espWifi.onDataCallback(wifi_onData); - - // espBT.onDataCallback(blueTooth_onData); + RemoteControl_CallbackReceiveAnalysis((uint8_t *)data, len); } -void status_update() +void RemoteControl_CallbackReceiveAnalysis(const uint8_t *buffer, size_t size) { -} -#endif + structRemoteControlDataFormat buttonBuffer = {}; // 输出的缓冲区 + buttonBuffer.end = sizeof(buttonBuffer) / sizeof(buttonBuffer.start); + short *ptr = &buttonBuffer.start; -#if false + uint8_t cmd[4] = {}; // 指令 + uint8_t data[4] = {}; // 数据 -#include "stm32f10x.h" -#include "sys.h" -#include "led.h" -#include "delay.h" -#include "usart.h" -#include "math.h" -#include "string.h" + char orderBuffer[4] = {}; + memcpy(orderBuffer, buffer, size); -// 发送一个字节 -void Uart_SendByte(USART_TypeDef *UARTx, u8 byte) -{ - USART_SendData(UARTx, byte); - while ((UARTx->SR & 0X40) == 0) - ; // 等待发送结束 -} -// 发送数组 -void Uart_SendArray(USART_TypeDef *UARTx, uint8_t *array, uint32_t num) -{ - while (num--) + // 判断是否摇杆数据,在进行解释 + switch (size) { - Uart_SendByte(UARTx, *array); - array++; + case 4: + if ((orderBuffer[0] & 0xF0) == 0x10) + { + cmd[0] = 0x9A; + RemoteControl_getJoystickValueXY(orderBuffer, buttonBuffer.LEFT_x_axis, buttonBuffer.LEFT_y_axis); + } + else if ((orderBuffer[0] & 0xF0) == 0x50) + { + cmd[0] = 0x9C; + RemoteControl_getJoystickValueXY(orderBuffer, buttonBuffer.RIGHT_x_axis, buttonBuffer.RIGHT_y_axis); + } + break; + + case 1: + + cmd[0] = (orderBuffer[0] & 0xF0) / 16; + data[0] = (orderBuffer[0] & 0x0F); + + // 把结构体当成数组 + ptr[cmd[0]] = data[0]; + break; } +#if true + RemoteControl_StructureArrayProcessingData(buttonBuffer); +#else + cmd[0] = cmd[0] * 16; + RemoteControl_SwitchCaseProcessingData(&buttonBuffer, cmd[0], data[0]); +#endif } -/************************************************************** - *函数名称:XHZS_SendWave - *简 介:学会助手APP虚拟示波器下位机程序(兼容山外) - *输 入:wareaddr:数据数组起始地址 waresize:数组大小 - *输 出:无 - *注意事项:无 - **************************************************************/ -void XHZS_SendWave(uint8_t *waveaddr, uint16_t wavesize) +void RemoteControl_getJoystickValueXY(char (&inputButtonData)[4], short &output_x_axis, short &output_y_axis) { - uint8_t cmdf[2] = {0x03, 0xFC}; // 帧头 - uint8_t cmdr[2] = {0xFC, 0x03}; // 帧尾 + // 摇杆接收格式: 1A 23 31 46 + uint8_t data[4] = {0}; + short x, y; - Uart_SendArray(USART1, cmdf, sizeof(cmdf)); // 发送帧头 - Uart_SendArray(USART1, waveaddr, wavesize); // 发送数据 - Uart_SendArray(USART1, cmdr, sizeof(cmdr)); // 发送帧尾 -} -/************************************************************** - *函数名称:SendWave - *简 介:发送数据到学会助手APP虚拟示波器 - *输 入:无 - *输 出:无 - *注意事项:将要发送的数据赋值给Wave数组即可,通道依次对应 - **************************************************************/ -void SendWave() -{ - float i; - float Wave[4] = {0}; // 注意数据类型;数组大小即为通道数量,最多四条通道 + data[0] = inputButtonData[0] & 0x0F; + data[1] = inputButtonData[1] & 0x0F; - // 正弦波测试 - 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)); + data[2] = inputButtonData[2] & 0x0F; + data[3] = inputButtonData[3] & 0x0F; - delay_ms(20); - } + // 对默认数值修改,中心数值为0, left 100~0~100 right + x = (data[0] * 16 + data[1]) - 100; + output_x_axis = ((x > 0) ? x : -x); + + y = (data[2] * 16 + data[3]) - 100; + output_y_axis = ((y > 0) ? y : -y); } -/************************************************************** - *函数名称:OscGetFloat - *简 介:将字符转为浮点数 - *输 入:p:字符串;value:用于获取输出值 - *输 出:(==0)转成功;(!=0)转失败 - *注意事项:虚拟示波器调参用,以','为数据分隔符 - **************************************************************/ -char OscGetFloat(const char *p, float *value) +void RemoteControl_StructureArrayProcessingData(structRemoteControlDataFormat &remoteControlData) { - int i; - int negative_flag = 1; - int float_flag = 0; - float tmp_value = 0; - for (i = 0; i < 16; i++) + short *ptr = &remoteControlData.start; // 获取结构体首地址 + for (size_t i = 0; i <= remoteControlData.end; i++) { - if (*p == ',') // 检测到分隔符,返回值 - { - tmp_value *= negative_flag; - *value = tmp_value; - return 0; - } - else if (*p == '-') // 检测到负号 - { - negative_flag = -1; - } - else if (*p == '.') // 检测到小数点 - { - float_flag = -1; - } - else if (*p >= '0' && *p <= '9') - { - if (float_flag == 0) - { - tmp_value = tmp_value * 10 + (*p - 0x30); - } - else - { - tmp_value += (*p - 0x30) * pow(10, float_flag); - float_flag--; - } - } - else - { - return 1; - } - p++; + Serial.printf("%d-%d ", i, ptr[i]); } - return 2; + Serial.println(); } -/************************************************************** - *函数名称:OscGetValue - *简 介:从字符串中获取指定参数数值 - *输 入:p:字符串,以'\0'结尾,否则将出错 - * name:指定参数名称,如"c12"获取第1组第2个参数; - * value:用于获取输出值; - *输 出:(==0)转成功;(!=0)转失败 - *注意事项:虚拟示波器调参用,字符串中以'\0'为结束符 - **************************************************************/ -char OscGetValue(const char *p, const char *name, float *value) + +void RemoteControl_SwitchCaseProcessingData(structRemoteControlDataFormat &remoteControlData, uint8_t &cmd, uint8_t &value) { - float tmp_value; - int feedback; - p = strstr(p, name); - if (p == NULL) - { - return 1; - } - p += 4; - feedback = OscGetFloat(p, &tmp_value); - if (feedback == 0) + switch (cmd) { - *value = tmp_value; - return 0; + case LEFT_UP: + uart_printf("LEFT_UP: %d\r\n", value); + break; + case LEFT_DOWN: + uart_printf("LEFT_DOWN: %d\r\n", value); + break; + case LEFT_LEFT: + uart_printf("LEFT_LEFT: %d\r\n", value); + break; + case LEFT_RIGHT: + uart_printf("LEFT_RIGHT: %d\r\n", value); + break; + + case RIGHT_UP: + uart_printf("RIGHT_UP: %d\r\n", value); + break; + case RIGHT_DOWN: + uart_printf("RIGHT_DOWN: %d\r\n", value); + break; + case RIGHT_LEFT: + uart_printf("RIGHT_LEFT: %d\r\n", value); + break; + case RIGHT_RIGHT: + uart_printf("RIGHT_RIGHT: %d\r\n", value); + break; + + case LEFT_X_AXIS: + uart_printf("x: %d y: %d\r\n", remoteControlData.LEFT_x_axis, remoteControlData.LEFT_y_axis); + break; + + case RIGHT_X_AXIS: + uart_printf("x: %d y: %d\r\n", remoteControlData.RIGHT_x_axis, remoteControlData.RIGHT_y_axis); + break; + + case A_BUTTON: + uart_printf("A_BUTTON: %d\r\n", value); + break; + case B_BUTTON: + uart_printf("B_BUTTON: %d\r\n", value); + break; + case C_BUTTON: + uart_printf("C_BUTTON: %d\r\n", value); + break; + case D_BUTTON: + uart_printf("D_BUTTON: %d\r\n", value); + break; + case E_BUTTON: + uart_printf("E_BUTTON: %d\r\n", value); + break; + case F_BUTTON: + uart_printf("F_BUTTON: %d\r\n", value); + break; } - return 2; } -int main(void) -{ - float v1 = 0, v2 = 0, v3 = 0, v4 = 0; - NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); - LED_Init(); - delay_init(); - uart_init(115200); +#if false - while (1) +void RemoteControl_TimedReceive() +{ + structRemoteControlDataFormat buttonBuffer = {}; // 输出的缓冲区 + buttonBuffer.start = 0; + buttonBuffer.end = 20; + char tempBuffer[20] = {0}; // 临时缓冲区 + + // 判断手柄的方向按钮和摇杆的组合 + char handle = (RemoteControlLayoutLeft == JOYSTICK_SIZE ? JOYSTICK_AND_DIRECTIONAL_PAD : DIRECTIONAL_PAD) + + (RemoteControlLayoutRight == JOYSTICK_SIZE ? DIRECTIONAL_PAD_AND_JOYSTICK : DIRECTIONAL_PAD); + + size_t data_len = strlen(Uart_getReceiveData()); // 获取长度用于复制 + memcpy(tempBuffer, Uart_getReceiveData(), data_len); // 内存复制 + + //// 尝试使用 cmd 串口改变,手柄的组合,失败 + // if (tempBuffer[0] == 's' && tempBuffer[1] == 'e' && tempBuffer[2] == 't') + // { + // char *end; + // char tempBufferCopy[data_len]; + + // substring(tempBufferCopy, tempBuffer, 4, 4); // 截取字符串 + // tempBufferCopy[0] = (char)strtol(tempBufferCopy, &end, 16); // 字符串转换十六进制 + // // _serialBT.printf("%s-%d\n", "pad", tempBufferCopy[0]); + + // if (tempBufferCopy[0] == 0x00 || tempBufferCopy[0] == 0x01 || tempBufferCopy[0] == 0x10 || tempBufferCopy[0] == 0x11) + // { + // // handle = *tempBufferCopy; + // handle = tempBufferCopy[0]; + // tempBufferCopy[0] = 100; + // _serialBT.printf("%s-%d\n", "ok", tempBufferCopy[0]); + // } + // } + + // 确定,方向按钮和摇杆组合方式 + switch (handle) { - if (USART_RX_STA == 2) - { - USART_RX_BUF[USART_RX_NUM] = '\0'; - OscGetValue((const char *)USART_RX_BUF, "c11", &v1); - OscGetValue((const char *)USART_RX_BUF, "c12", &v2); - OscGetValue((const char *)USART_RX_BUF, "c13", &v3); - OscGetValue((const char *)USART_RX_BUF, "c14", &v4); - - printf("w:%.2f,%.2f,%.2f,%.2f\r\n", v1, v2, v3, v4); - USART_RX_NUM = 0; - USART_RX_STA = 0; - } - delay_ms(50); + case DIRECTIONAL_PAD: + // 获取结构体的首地址,在基础上移动到指定位置,进行赋值或者读取操作 + getDirectionalPad(&outputBuffer.begin, 1, tempBuffer, 0); + getDirectionalPad(&outputBuffer.begin, 5, tempBuffer, 4); + getFunctionButtonValue(&outputBuffer.begin, 10, tempBuffer, 8); + break; + + case DIRECTIONAL_PAD_AND_JOYSTICK: + getDirectionalPad(&outputBuffer.begin, 1, tempBuffer, 0); + getJoystickValue(&outputBuffer.begin, 18, tempBuffer, 4); + getFunctionButtonValue(&outputBuffer.begin, 10, tempBuffer, 10); + break; + + case JOYSTICK_AND_DIRECTIONAL_PAD: + getJoystickValue(&outputBuffer.begin, 16, tempBuffer, 0); + getDirectionalPad(&outputBuffer.begin, 5, tempBuffer, 6); + getFunctionButtonValue(&outputBuffer.begin, 10, tempBuffer, 10); + break; + + case JOYSTICKS: + getJoystickValue(&outputBuffer.begin, 16, tempBuffer, 0); + getJoystickValue(&outputBuffer.begin, 18, tempBuffer, 6); + getFunctionButtonValue(&outputBuffer.begin, 10, tempBuffer, 12); + break; } + return outputBuffer; } - #endif \ No newline at end of file diff --git a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h index b79d5b0b..ef7e722e 100644 --- a/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h +++ b/PlatformIO_ESP32_oscilloscope/lib/myXHZS/myXHZS.h @@ -4,37 +4,112 @@ #include "myBlueTooth.h" #include "myWiFi.h" -enum WIRELESS_DEVICE +// 无线设备类型 +enum enumWirelessDevice { EMPTY, BLUETOOTH_DEVICE, WIFI_DEVICE }; +// 遥控器数据格式 +struct structRemoteControlDataFormat +{ + short start; // 起始标志 + + short LEFT_up; // 向上 // 1 + short LEFT_down; // 向下 + short LEFT_left; // 向左 + short LEFT_right; // 向右 + + short RIGHT_up; // 向上 // 5 + short RIGHT_down; // 向下 + short RIGHT_left; // 向左 + short RIGHT_right; // 向右 + + short middle; + + short A_button; // 按钮A // 10 + short B_button; // 按钮B + short C_button; // 按钮C + short D_button; // 按钮D + short E_button; // 按钮E + short F_button; // 按钮F + + short LEFT_x_axis; // 左摇杆X轴 // 16 + short LEFT_y_axis; // 左摇杆Y轴 + short RIGHT_x_axis; // 右摇杆X轴 + short RIGHT_y_axis; // 右摇杆Y轴 + + short end; // 结束标志位 // 20 +}; + +enum enumRemoteControlOrderFormat +{ + LEFT_UP = 0X10, // 左侧方向键上 + LEFT_DOWN = 0X20, // 左侧方向键下 + LEFT_LEFT = 0X30, // 左侧方向键左 + LEFT_RIGHT = 0X40, // + + RIGHT_UP = 0X50, // 右侧方向键上 + RIGHT_DOWN = 0X60, // 右侧方向键下 + RIGHT_LEFT = 0X70, // 右侧方向键左 + RIGHT_RIGHT = 0X80, // + + LEFT_X_AXIS = 0X9A, // 左侧摇杆的指令 + LEFT_Y_AXIS = 0X9B, + RIGHT_X_AXIS = 0X9C, // 右侧摇杆的指令 + RIGHT_Y_AXIS = 0X9D, + + A_BUTTON = 0XA0, // 左侧A功能键 + B_BUTTON = 0XB0, // 左侧B功能键 + C_BUTTON = 0XC0, // 右侧C功能键 + D_BUTTON = 0XD0, // 右侧D功能键 + E_BUTTON = 0XE0, // 中间开关E + F_BUTTON = 0XF0 // 中间开关F +}; + +// struct structJoystickDataFormat +// { +// short x_axis; // 左右摇杆X轴 +// short y_axis; // 左右摇杆Y轴 +// }; + +// 基础输入输出函数 start -------------------- void XHZS_begin(const char *device_name, const char *device_password); -void uart_printf(const char *format, ...); char *uart_receiveBuffer(); - +void uart_printf(const char *format, ...); void Uart_SendByte(uint8_t *dataByte); void Uart_SendArray(uint8_t *dataArray, size_t dataSize); +// 基础输入输出函数 end -------------------- // 串口功能 start void uart_EnableSerialCallback(); -void bluetooth_SerialReceiveCallback(const uint8_t *buffer, size_t size); -void wifi_SerialReceiveCallback(void *arg, AsyncClient *client, void *data, size_t len); -// 串口功能 start +void uart_bluetoothDataReceivedCallback(const uint8_t *buffer, size_t size); +void uart_wifiDataReceivedCallback(void *arg, AsyncClient *client, void *data, size_t len); +// 串口功能 start -------------------- -// 示波器功能 start +// 示波器功能 start -------------------- void XHZS_SendWave(uint8_t *waveAddr, uint16_t waveSize); char OscGetFloat(const char *p, float *value); char OscGetValue(const char *p, const char *name, float *value); void SendWave(); -void OscilloscopeSendsAndReceivesData(); -// 示波器功能 end +void Oscilloscope_SendsAndReceivesData(); +// 示波器功能 end -------------------- + +// 遥控器功能 start -------------------- +void RemoteControl_EnableCallbackReceive(); +void RemoteControl_BluetoothCallbackReceive(const uint8_t *buffer, size_t size); +void RemoteControl_WifiCallbackReceive(void *arg, AsyncClient *client, void *data, size_t len); +void RemoteControl_CallbackReceiveAnalysis(const uint8_t *buffer, size_t size); +void RemoteControl_getJoystickValueXY(char (&inputButtonData)[4], short &output_x_axis, short &output_y_axis); +void RemoteControl_StructureArrayProcessingData(structRemoteControlDataFormat &remoteControlData); +void RemoteControl_SwitchCaseProcessingData(structRemoteControlDataFormat &remoteControlData, uint8_t &cmd, uint8_t &value); + +void RemoteControl_TimedReceive(); -// 遥控器功能 start +// 遥控器功能 end -------------------- -// 遥控器功能 end #endif diff --git a/PlatformIO_ESP32_oscilloscope/src/main.cpp b/PlatformIO_ESP32_oscilloscope/src/main.cpp index b5cf9a9d..f30ab0d4 100644 --- a/PlatformIO_ESP32_oscilloscope/src/main.cpp +++ b/PlatformIO_ESP32_oscilloscope/src/main.cpp @@ -1,15 +1,15 @@ #include #include "myXHZS.h" - void setup() { + Serial.begin(115200); + XHZS_begin("esp32_BT", "12345678"); - uart_EnableSerialCallback(); - - // OscilloscopeSendsAndReceivesData(); + // uart_EnableSerialCallback(); + // OscilloscopeSendsAndReceivesData(); } void loop() @@ -17,19 +17,6 @@ void loop() delay(3000); } - - - - - - - - - - - - - #if false #include -- Gitee