From 14da473a06ecd5b3a792b06adcee7641eb082c59 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Mon, 22 Apr 2024 00:07:29 +0800
Subject: [PATCH] Apps:IGKBoard-All: Add DS18B20 test app support
---
drivers/test-apps/ds18b20.c | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 175 insertions(+), 0 deletions(-)
diff --git a/drivers/test-apps/ds18b20.c b/drivers/test-apps/ds18b20.c
new file mode 100644
index 0000000..c9da5f3
--- /dev/null
+++ b/drivers/test-apps/ds18b20.c
@@ -0,0 +1,175 @@
+/*********************************************************************************
+ * Copyright: (C) 2023 LingYun IoT System Studio
+ * All rights reserved.
+ *
+ * Filename: ds18b20.c
+ * Description: This file is temperature sensor DS18B20 source code
+ *
+ * Version: 1.0.0(2023/8/10)
+ * Author: Guo Wenxue <guowenxue@gmail.com>
+ * ChangeLog: 1, Release initial version on "2023/8/10 12:13:26"
+ *
+ * Pin connection:
+ *
+ * DS18B20 Module IGKBoard
+ * VCC <-----> 3.3V
+ * DQ <-----> #Pin7(GPIO1_IO18)
+ * GND <-----> GND
+ *
+ * /run/media/mmcblk1p1/config.txt:
+ *
+ * # Eanble 1-Wire overlay
+ * dtoverlay_w1=yes
+ *
+ ********************************************************************************/
+
+/* 在C语言编程时,一般系统的头文件用<xxx.h>,我们自己写的头文件则用"zzz.h" */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <dirent.h>
+#include <string.h>
+#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");
+ return 1;
+ }
+
+ /* 打印DS18B20的采样温度值,因为℃是非ASCII打印字符,所以这里用 'C 代替 */
+ printf("DS18B20 get temperature: %f 'C\n", temp);
+
+ return 0;
+}
+
+/*
+ * 函数说明: 该函数用来使用 DS18B20 温度传感器采样获取当前的温度值;
+ * 参数说明: $temp: 通过指针返回DS18B20的采样温度
+ * 返回说明: ==0 表示成功, <0 表示失败
+ */
+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) */
+
+
+ /* 在C语言编程时,进入函数的第一件事应该进行函数参数的合法性检测,检查参数非法输入。
+ * 否则调用者"不小心"通过 $temp 传入一个空指针,下面的代码就有可能出现段错误。
+ */
+ if( !temp )
+ {
+ return -1;
+ }
+
+ /* 打开 "/sys/bus/w1/devices/" 文件夹,如果打开失败则打印错误信息并退出。 */
+ if((dirp = opendir(w1_path)) == NULL)
+ {
+ printf("opendir error: %s\n", strerror(errno));
+ return -2;
+ }
+
+ /* 1, 因为文件夹下可能有很多文件,所以这里使用while()循环读取/sys/bus/w1/devices/
+ * 文件夹下的所有目录项,其中 direntp->d_name 就是目录里的每个文件/文件夹的文件名。
+ *
+ * 2, 接下来我们使用 strstr() 函数判断文件名中是否包含 "28-",如果找到则将完整的
+ * 文件名通过strcpy()函数保存到 chip 中;并设置 found 标志为1,跳出循环。
+ */
+ while((direntp = readdir(dirp)) != NULL)
+ {
+ if(strstr(direntp->d_name,"28-"))
+ {
+ /* find and get the chipset SN filename */
+ strcpy(chip,direntp->d_name);
+ found = 1;
+ break;
+ }
+ }
+
+ /* 文件夹打开用完后,要记得第一时间关闭 */
+ closedir(dirp);
+
+ /* found在定义时初始化为0,如果上面的代码没有找到 "28-" 文件名则其值依然为0,否则将会被
+ * 设置为1。如果 found 的值为0的话,则打印错误信息并返回相应的错误码-3.
+ */
+ 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(ds_path, sizeof(ds_path), "%s/%s/w1_slave", w1_path, chip);
+
+ /* 接下来打开 DS18B20 的采样文件,如果失败则返回相应的错误码-4。 */
+ if( (fd=open(ds_path, O_RDONLY)) < 0 )
+ {
+ printf("open %s error: %s\n", ds_path, strerror(errno));
+ return -4;
+ }
+
+ /* 读取文件中的内容将会触发 DS18B20温度传感器采样,这里读取文件内容保存到buf中 */
+ if(read(fd, buf, sizeof(buf)) < 0)
+ {
+ printf("read %s error: %s\n", ds_path, strerror(errno));
+
+ /* 1, 这里不能直接调用 return -5 直接返回,否则的话前面open()打开的文件描述符就没有关闭。
+ * 这里设置 rv 为错误码-5,通过 goto 语句跳转到函数后面统一进行错误处理。
+ *
+ * 2, 在C语言编程时我们应该慎用goto语句进行"随意"的跳转,因为它会降低代码的可读性。但这里是
+ * goto语句的一个非常典型应用,我们经常会用它来对错误进行统一的处理。
+ *
+ * 3,goto后面的cleanup为标号,它在下面的代码中定义。
+ */
+ rv = -5;
+ goto cleanup;
+ }
+
+ /* 采样温度值是在字符串"t="后面,这里我们从buf中找到"t="字符串的位置并保存到ptr指针中 */
+ ptr = strstr(buf, "t=");
+ if( !ptr )
+ {
+ printf("ERROR: Can not get temperature\n");
+ rv = -6;
+ goto cleanup;
+ }
+
+ /* 因为此时ptr是指向 "t="字符串的地址(即't'的地址),那跳过2个字节(t=)后面的就是采样温度值 */
+ ptr+=2;
+
+ /* 接下来我们使用 atof() 函数将采样温度值字符串形式,转化成 float 类型。*/
+ *temp = atof(ptr)/1000;
+
+ /* 1,在这里我们对函数返回进行集中处理,其中 cleanup 为 goto 语句的标号;
+ * 2,在函数退出时,我们应该考虑清楚在前面的代码中做了哪些事,这些事是否需要进行反向操作。如
+ * 打开的文件或文件夹是否需要关闭,malloc()分配的内存是否需要free()等。
+ * 3, 在最开始我们定义rv并赋初值为0(表示成功)是有原因的,如果前面的代码任何一个地方出现错误,
+ * 则会将rv的值修改为相应的错误码,否则rv的值将始终为0(即没有错误发生),这里将统一返回。
+ */
+cleanup:
+ close(fd);
+ return rv;
+}
--
Gitblit v1.9.1