项目介绍
目标功能
读取这个光流动传感器的信息。
实现方式
- 环境搭建
我的环境是ubantu18.04 环境搭建参考。
因为我喜欢用vscode 开发,又下载一个C的插件,然后把克隆下来的整个文件包含进去就可以了。然后改一下编译器的位置就可以了。下面是我的设置 实现思路
这个移植感觉比较完善了,设备都可以直接用也有示例。所以照着iot_peripheral文件夹下的文件直接驱动UART2读取光流的数据,然后串口发出来就可以了。代码
#include <stdio.h>
#include <string.h>
#include "ohos_init.h"
#include "kernel/os/os.h"
#include "iot_uart.h"
#define UART_BUFFER_MAXSIZE 50
#define UART_RECEIVE_DATALEN 30
static OS_Thread_t g_main_thread;
static IotUartAttribute uart_param;
int GetLigSteamData(char buff[UART_BUFFER_MAXSIZE],int XY){
int flowX,flowY;
for(int i=0;i<UART_RECEIVE_DATALEN-9;i++){
int flag=0;
if(buff[i]==0xFE){
i++;
flowX=(int)(buff[i+1]<<8)|(buff[i]);
i=i+2;
flowY=(int)(buff[i+1]<<8)|(buff[i]);
}
//Read data from lightflow
}
if(XY==1) return flowX;
else return flowY;
}
static void MainThread(void *arg)
{
int ret;
unsigned int uart_id = 2;
unsigned int uart_len;
unsigned char uart_buffer[UART_BUFFER_MAXSIZE];
static IotUartAttribute uart_param = { 19200,
IOT_UART_DATA_BIT_8,
IOT_UART_STOP_BIT_1,
IOT_UART_PARITY_NONE,
0,
0,
0 };
printf("uart%d test start\r\n", uart_id);
IoTUartInit(uart_id, &uart_param);
while (1) {
memset((char *)uart_buffer, 0, UART_BUFFER_MAXSIZE);
ret = IoTUartRead(uart_id, uart_buffer, UART_RECEIVE_DATALEN);
if (ret > 0) {
//printf("recv len=%d, data:%02X\r\n", ret, uart_buffer[0]);
int x;
x=GetLigSteamData(uart_buffer,1);
printf("The flowX is %d\r\n",x);
memset(uart_buffer, 0, UART_BUFFER_MAXSIZE);
uart_len = snprintf((char *)uart_buffer,
UART_RECEIVE_DATALEN,
"uart%d send data\r\n", uart_id);
}
else{
printf("Dont read data");
}
}
IoTUartDeinit(uart_id);
printf("uart%d test end\r\n", uart_id);
}
void TESTMain(void)
{
printf("UART2 Data Test Start\n");
if (OS_ThreadCreate(&g_main_thread, "MainThread", MainThread, NULL,
OS_THREAD_PRIO_APP, 4 * 1024) != OS_OK) {
printf("[ERR] Create Main Thread is failed;");
}
}
SYS_RUN(TESTMain);
演示
linux上的烧录和编译我都是参考的这篇文章烧录串口助手参考
可以看到光流数据是会随着我的运动数据而变化的。