- #include "stm32f10x.h"
- #include "Delay.h"
- #include "OLED.h"
- #include "Serial.h"
- #include "LD110.h"
- #include "stdlib.h"
- #include "string.h"
- int main(void)
- {
- Delay_ms(100);
- OLED_Init();
- Serial_Init();
- LD110_Init();
- LD110_Stop();
-
- // OLED初始化:清屏并显示欢迎信息
- OLED_Clear();
- OLED_ShowString(0, 0, "Bluetooth Draw", 16, 1);
- OLED_ShowString(0, 20, "Ready:", 16, 1);
- OLED_Refresh();
-
- while (1)
- {
- if (Serial_RxFlag == 1)
- {
- // 解析命令
- if (strncmp(Serial_RxPacket, "dot=", 4) == 0)
- {
- // dot=x,y
- int x, y;
- sscanf(Serial_RxPacket + 4, "%d,%d", &x, &y);
- if (x >= 0 && x < 128 && y >= 0 && y < 64)
- {
- OLED_DrawPoint(x, y, 1);
- OLED_Refresh();
- Serial_SendString("OK:dot\r\n");
- }
- else
- {
- Serial_SendString("ERR:out of range\r\n");
- }
- }
- else if (strncmp(Serial_RxPacket, "line=", 5) == 0)
- {
- // line=x1,y1,x2,y2
- int x1, y1, x2, y2;
- sscanf(Serial_RxPacket + 5, "%d,%d,%d,%d", &x1, &y1, &x2, &y2);
- if ((x1 >= 0 && x1 < 128 && y1 >= 0 && y1 < 64) &&
- (x2 >= 0 && x2 < 128 && y2 >= 0 && y2 < 64))
- {
- OLED_DrawLine(x1, y1, x2, y2, 1);
- OLED_Refresh();
- Serial_SendString("OK:line\r\n");
- }
- else
- {
- Serial_SendString("ERR:out of range\r\n");
- }
- }
- else if (strncmp(Serial_RxPacket, "rect=", 5) == 0)
- {
- // rect=x,y,w,h
- int x, y, w, h;
- sscanf(Serial_RxPacket + 5, "%d,%d,%d,%d", &x, &y, &w, &h);
- if (x >= 0 && x < 128 && y >= 0 && y < 64 &&
- (x + w) <= 128 && (y + h) <= 64)
- {
- // 画矩形:四条边
- OLED_DrawLine(x, y, x + w - 1, y, 1);
- OLED_DrawLine(x, y, x, y + h - 1, 1);
- OLED_DrawLine(x + w - 1, y, x + w - 1, y + h - 1, 1);
- OLED_DrawLine(x, y + h - 1, x + w - 1, y + h - 1, 1);
- OLED_Refresh();
- Serial_SendString("OK:rect\r\n");
- }
- else
- {
- Serial_SendString("ERR:out of range\r\n");
- }
- }
- else if (strncmp(Serial_RxPacket, "circle=", 7) == 0)
- {
- // circle=x,y,r
- int x, y, r;
- sscanf(Serial_RxPacket + 7, "%d,%d,%d", &x, &y, &r);
- if (x >= r && x + r < 128 && y >= r && y + r < 64)
- {
- OLED_DrawCircle(x, y, r);
- OLED_Refresh();
- Serial_SendString("OK:circle\r\n");
- }
- else
- {
- Serial_SendString("ERR:out of range\r\n");
- }
- }
- else if (strncmp(Serial_RxPacket, "cirde=", 6) == 0)
- {
- // 兼容拼写错误: cirde=x,y,r
- int x, y, r;
- sscanf(Serial_RxPacket + 6, "%d,%d,%d", &x, &y, &r);
- if (x >= r && x + r < 128 && y >= r && y + r < 64)
- {
- OLED_DrawCircle(x, y, r);
- OLED_Refresh();
- Serial_SendString("OK:circle\r\n");
- }
- else
- {
- Serial_SendString("ERR:out of range\r\n");
- }
- }
- else if (strcmp(Serial_RxPacket, "clear") == 0)
- {
- // clear: 清空画布
- OLED_Clear();
- OLED_ShowString(0, 0, "Bluetooth Draw", 16, 1);
- OLED_Refresh();
- Serial_SendString("OK:clear\r\n");
- }
- else
- {
- Serial_SendString("ERR:unknown command\r\n");
- }
-
- // 清除接收标志
- Serial_RxFlag = 0;
- }
- }
- }
复制代码
Keil代码下载:
蓝牙控制oled绘制.7z
(202.7 KB, 下载次数: 0)
|