Skip to content

Commit 0f53a2b

Browse files
Merge pull request #1 from willianchanlovegithub/WillianChan
First Commit
2 parents 3829a96 + 8e11ac7 commit 0f53a2b

File tree

4 files changed

+411
-0
lines changed

4 files changed

+411
-0
lines changed

SConscript

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from building import *
2+
Import('rtconfig')
3+
4+
src = []
5+
cwd = GetCurrentDir()
6+
7+
# add ds18b20 src files.
8+
if GetDepend('PKG_USING_DS18B20'):
9+
src += Glob('src/sensor_dallas_ds18b20.c')
10+
11+
if GetDepend('PKG_USING_DS18B20_SAMPLE'):
12+
src += Glob('sample/ds18b20_sample.c')
13+
14+
# add ds18b20 include path.
15+
path = [cwd + '/inc']
16+
17+
# add src and include to group.
18+
group = DefineGroup('ds18b20', src, depend = ['PKG_USING_DS18B20'], CPPPATH = path)
19+
20+
Return('group')

inc/sensor_dallas_ds18b20.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2019-07-15 WillianChan the first version.
9+
*
10+
*/
11+
12+
#ifndef __DS18B20_H__
13+
#define __DS18B20_H__
14+
15+
#include <rthw.h>
16+
#include <rtthread.h>
17+
#include <rtdevice.h>
18+
#include "sensor.h"
19+
20+
#define CONNECT_SUCCESS 0
21+
#define CONNECT_FAILED 1
22+
23+
struct ds18b20_device
24+
{
25+
rt_base_t pin;
26+
rt_mutex_t lock;
27+
};
28+
typedef struct ds18b20_device *ds18b20_device_t;
29+
30+
uint8_t ds18b20_init(rt_base_t pin);
31+
int32_t ds18b20_get_temperature(rt_base_t pin);
32+
int rt_hw_ds18b20_init(const char *name, struct rt_sensor_config *cfg);
33+
34+
#endif /* __DS18B20_H_ */
35+
36+

sample/ds18b20_sample.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2019-07-24 WillianChan the first version
9+
*/
10+
11+
#include <stdlib.h>
12+
#include <rtthread.h>
13+
#include "sensor.h"
14+
#include "sensor_dallas_ds18b20.h"
15+
16+
/* Modify this pin according to the actual wiring situation */
17+
#define DS18B20_DATA_PIN GET_PIN(G, 9)
18+
19+
static void read_temp_entry(void *parameter)
20+
{
21+
rt_device_t dev = RT_NULL;
22+
struct rt_sensor_data sensor_data;
23+
rt_size_t res;
24+
25+
dev = rt_device_find(parameter);
26+
if (dev == RT_NULL)
27+
{
28+
rt_kprintf("Can't find device:%s\n", parameter);
29+
return;
30+
}
31+
32+
if (rt_device_open(dev, RT_DEVICE_FLAG_RDWR) != RT_EOK)
33+
{
34+
rt_kprintf("open device failed!\n");
35+
return;
36+
}
37+
rt_device_control(dev, RT_SENSOR_CTRL_SET_ODR, (void *)100);
38+
39+
while (1)
40+
{
41+
res = rt_device_read(dev, 0, &sensor_data, 1);
42+
if (res != 1)
43+
{
44+
rt_kprintf("read data failed!size is %d\n", res);
45+
rt_device_close(dev);
46+
return;
47+
}
48+
else
49+
{
50+
if (sensor_data.data.temp >= 0)
51+
{
52+
rt_kprintf("temp:%3d.%dC, timestamp:%5d\n",
53+
sensor_data.data.temp / 10,
54+
sensor_data.data.temp % 10,
55+
sensor_data.timestamp);
56+
}
57+
else
58+
{
59+
rt_kprintf("temp:-%2d.%dC, timestamp:%5d\n",
60+
abs(sensor_data.data.temp / 10),
61+
abs(sensor_data.data.temp % 10),
62+
sensor_data.timestamp);
63+
}
64+
}
65+
rt_thread_mdelay(100);
66+
}
67+
}
68+
69+
static int ds18b20_read_temp_sample(void)
70+
{
71+
rt_thread_t ds18b20_thread;
72+
73+
ds18b20_thread = rt_thread_create("18b20tem",
74+
read_temp_entry,
75+
"temp_ds18b20",
76+
1024,
77+
RT_THREAD_PRIORITY_MAX / 2,
78+
20);
79+
if (ds18b20_thread != RT_NULL)
80+
{
81+
rt_thread_startup(ds18b20_thread);
82+
}
83+
84+
return RT_EOK;
85+
}
86+
INIT_APP_EXPORT(ds18b20_read_temp_sample);
87+
88+
static int rt_hw_ds18b20_port(void)
89+
{
90+
struct rt_sensor_config cfg;
91+
92+
cfg.intf.user_data = (void *)DS18B20_DATA_PIN;
93+
rt_hw_ds18b20_init("ds18b20", &cfg);
94+
95+
return RT_EOK;
96+
}
97+
INIT_COMPONENT_EXPORT(rt_hw_ds18b20_port);

0 commit comments

Comments
 (0)