From a0be3febef2aab84a3bc6abf49c337ee6e484118 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Mon, 15 Jul 2024 17:27:43 +0800
Subject: [PATCH] Yocto:IGKBoard-All: Add rtl8188fu and rtl8723du USB WiFi module driver
---
drivers/test-apps/ds18b20.c | 38 +++++++++++++-------------------------
1 files changed, 13 insertions(+), 25 deletions(-)
diff --git a/drivers/test-apps/ds18b20.c b/drivers/test-apps/ds18b20.c
index c9da5f3..caf8933 100644
--- a/drivers/test-apps/ds18b20.c
+++ b/drivers/test-apps/ds18b20.c
@@ -23,7 +23,6 @@
*
********************************************************************************/
-/* 在C语言编程时,一般系统的头文件用<xxx.h>,我们自己写的头文件则用"zzz.h" */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@@ -33,17 +32,12 @@
#include <time.h>
#include <errno.h>
-/* 在C语言编程中,函数应该先定义再使用,如果函数的定义在函数调用后面,应该前向声明。*/
int ds18b20_get_temperature(float *temp);
int main(int argc, char *argv[])
{
float temp; /* 温度值有小数位,所以使用浮点数 */
- /* 1,在Linux下做C语言编程时,函数返回值一般是0表示成功,<0表示失败,我们也遵循这个规约;
- * 2,但函数调用只能有一个返回值,所以这里的采样函数只能通过指针来返回采样的温度值;
- * 3,因为要在ds18b20_get_temperature()函数中修改main()中temp的值,所以这里传&temp;
- */
if( ds18b20_get_temperature(&temp) < 0 )
{
printf("ERROR: ds18b20 get temprature failure\n");
@@ -64,15 +58,15 @@
int ds18b20_get_temperature(float *temp)
{
const char *w1_path = "/sys/bus/w1/devices/";
- char ds_path[50]; /* DS18B20 采样文件路径 */
- char chip[20]; /* DS18B20 芯片序列号文件名 */
- char buf[128]; /* read() 读数据存储 buffer */
- DIR *dirp; /* opendir()打开的文件夹句柄 */
- struct dirent *direntp; /* readdir()读文件夹内容时的目录项*/
- int fd =-1; /* open()打开文件的文件描述符 */
- char *ptr; /* 一个字符指针,用来字符串处理 */
- int found = 0; /* 是否找到DS18B20的标志,默认设置为没找到(0) */
- int rv = 0; /* 函数返回值,默认设置为成功返回(0) */
+ char ds_path[50];
+ char chip[20];
+ char buf[128];
+ DIR *dirp;
+ struct dirent *direntp;
+ int fd =-1;
+ char *ptr;
+ int found = 0;
+ int rv = 0;
/* 在C语言编程时,进入函数的第一件事应该进行函数参数的合法性检测,检查参数非法输入。
@@ -110,21 +104,17 @@
/* 文件夹打开用完后,要记得第一时间关闭 */
closedir(dirp);
- /* found在定义时初始化为0,如果上面的代码没有找到 "28-" 文件名则其值依然为0,否则将会被
- * 设置为1。如果 found 的值为0的话,则打印错误信息并返回相应的错误码-3.
- */
+ /* found在定义时初始化为0,如果上面没有找到 "28-" 文件则其值依然为0,否则将被置为1 */
if( !found )
{
printf("Can not find ds18b20 in %s\n", w1_path);
return -3;
}
- /* 使用snprintf()函数生成完整路径/sys/bus/w1/devices/28-xxxxx/w1_slave
- * 并保存到 ds_path 中。
- */
+ /* 使用snprintf() 生成完整路径/sys/bus/w1/devices/28-xxxxx/w1_slave */
snprintf(ds_path, sizeof(ds_path), "%s/%s/w1_slave", w1_path, chip);
- /* 接下来打开 DS18B20 的采样文件,如果失败则返回相应的错误码-4。 */
+ /* 接下来打开 DS18B20 的采样文件 */
if( (fd=open(ds_path, O_RDONLY)) < 0 )
{
printf("open %s error: %s\n", ds_path, strerror(errno));
@@ -136,13 +126,11 @@
{
printf("read %s error: %s\n", ds_path, strerror(errno));
- /* 1, 这里不能直接调用 return -5 直接返回,否则的话前面open()打开的文件描述符就没有关闭。
+ /* 1, 这里不能直接调用 return直接返回,否则的话前面open()打开的文件描述符就没有关闭。
* 这里设置 rv 为错误码-5,通过 goto 语句跳转到函数后面统一进行错误处理。
*
* 2, 在C语言编程时我们应该慎用goto语句进行"随意"的跳转,因为它会降低代码的可读性。但这里是
* goto语句的一个非常典型应用,我们经常会用它来对错误进行统一的处理。
- *
- * 3,goto后面的cleanup为标号,它在下面的代码中定义。
*/
rv = -5;
goto cleanup;
--
Gitblit v1.9.1