Skip to content

Commit

Permalink
Merge pull request #1 from willianchanlovegithub/WillianChan
Browse files Browse the repository at this point in the history
First Commit
  • Loading branch information
willianchanlovegithub authored Jul 24, 2019
2 parents 3829a96 + 8e11ac7 commit 0f53a2b
Show file tree
Hide file tree
Showing 4 changed files with 411 additions and 0 deletions.
20 changes: 20 additions & 0 deletions SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from building import *
Import('rtconfig')

src = []
cwd = GetCurrentDir()

# add ds18b20 src files.
if GetDepend('PKG_USING_DS18B20'):
src += Glob('src/sensor_dallas_ds18b20.c')

if GetDepend('PKG_USING_DS18B20_SAMPLE'):
src += Glob('sample/ds18b20_sample.c')

# add ds18b20 include path.
path = [cwd + '/inc']

# add src and include to group.
group = DefineGroup('ds18b20', src, depend = ['PKG_USING_DS18B20'], CPPPATH = path)

Return('group')
36 changes: 36 additions & 0 deletions inc/sensor_dallas_ds18b20.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-07-15 WillianChan the first version.
*
*/

#ifndef __DS18B20_H__
#define __DS18B20_H__

#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include "sensor.h"

#define CONNECT_SUCCESS 0
#define CONNECT_FAILED 1

struct ds18b20_device
{
rt_base_t pin;
rt_mutex_t lock;
};
typedef struct ds18b20_device *ds18b20_device_t;

uint8_t ds18b20_init(rt_base_t pin);
int32_t ds18b20_get_temperature(rt_base_t pin);
int rt_hw_ds18b20_init(const char *name, struct rt_sensor_config *cfg);

#endif /* __DS18B20_H_ */


97 changes: 97 additions & 0 deletions sample/ds18b20_sample.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-07-24 WillianChan the first version
*/

#include <stdlib.h>
#include <rtthread.h>
#include "sensor.h"
#include "sensor_dallas_ds18b20.h"

/* Modify this pin according to the actual wiring situation */
#define DS18B20_DATA_PIN GET_PIN(G, 9)

static void read_temp_entry(void *parameter)
{
rt_device_t dev = RT_NULL;
struct rt_sensor_data sensor_data;
rt_size_t res;

dev = rt_device_find(parameter);
if (dev == RT_NULL)
{
rt_kprintf("Can't find device:%s\n", parameter);
return;
}

if (rt_device_open(dev, RT_DEVICE_FLAG_RDWR) != RT_EOK)
{
rt_kprintf("open device failed!\n");
return;
}
rt_device_control(dev, RT_SENSOR_CTRL_SET_ODR, (void *)100);

while (1)
{
res = rt_device_read(dev, 0, &sensor_data, 1);
if (res != 1)
{
rt_kprintf("read data failed!size is %d\n", res);
rt_device_close(dev);
return;
}
else
{
if (sensor_data.data.temp >= 0)
{
rt_kprintf("temp:%3d.%dC, timestamp:%5d\n",
sensor_data.data.temp / 10,
sensor_data.data.temp % 10,
sensor_data.timestamp);
}
else
{
rt_kprintf("temp:-%2d.%dC, timestamp:%5d\n",
abs(sensor_data.data.temp / 10),
abs(sensor_data.data.temp % 10),
sensor_data.timestamp);
}
}
rt_thread_mdelay(100);
}
}

static int ds18b20_read_temp_sample(void)
{
rt_thread_t ds18b20_thread;

ds18b20_thread = rt_thread_create("18b20tem",
read_temp_entry,
"temp_ds18b20",
1024,
RT_THREAD_PRIORITY_MAX / 2,
20);
if (ds18b20_thread != RT_NULL)
{
rt_thread_startup(ds18b20_thread);
}

return RT_EOK;
}
INIT_APP_EXPORT(ds18b20_read_temp_sample);

static int rt_hw_ds18b20_port(void)
{
struct rt_sensor_config cfg;

cfg.intf.user_data = (void *)DS18B20_DATA_PIN;
rt_hw_ds18b20_init("ds18b20", &cfg);

return RT_EOK;
}
INIT_COMPONENT_EXPORT(rt_hw_ds18b20_port);
Loading

0 comments on commit 0f53a2b

Please sign in to comment.