zephyr/samples/sensor/tmp112/src/main.c
Keith Packard 0b90fd5adf samples, tests, boards: Switch main return type from void to int
As both C and C++ standards require applications running under an OS to
return 'int', adapt that for Zephyr to align with those standard. This also
eliminates errors when building with clang when not using -ffreestanding,
and reduces the need for compiler flags to silence warnings for both clang
and gcc.

Most of these changes were automated using coccinelle with the following
script:

@@
@@
- void
+ int
main(...) {
	...
-	return;
+	return 0;
	...
}

Approximately 40 files had to be edited by hand as coccinelle was unable to
fix them.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-14 07:49:41 +09:00

68 lines
1.4 KiB
C

/*
* Copyright (c) 2016 Firmwave
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/__assert.h>
static void do_main(const struct device *dev)
{
int ret;
struct sensor_value temp_value;
struct sensor_value attr;
attr.val1 = 150;
attr.val2 = 0;
ret = sensor_attr_set(dev, SENSOR_CHAN_AMBIENT_TEMP,
SENSOR_ATTR_FULL_SCALE, &attr);
if (ret) {
printk("sensor_attr_set failed ret %d\n", ret);
return;
}
attr.val1 = 8;
attr.val2 = 0;
ret = sensor_attr_set(dev, SENSOR_CHAN_AMBIENT_TEMP,
SENSOR_ATTR_SAMPLING_FREQUENCY, &attr);
if (ret) {
printk("sensor_attr_set failed ret %d\n", ret);
return;
}
while (1) {
ret = sensor_sample_fetch(dev);
if (ret) {
printk("sensor_sample_fetch failed ret %d\n", ret);
return;
}
ret = sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp_value);
if (ret) {
printk("sensor_channel_get failed ret %d\n", ret);
return;
}
printk("temp is %d (%d micro)\n", temp_value.val1,
temp_value.val2);
k_sleep(K_MSEC(1000));
}
}
int main(void)
{
const struct device *const dev = DEVICE_DT_GET_ANY(ti_tmp112);
__ASSERT(dev != NULL, "Failed to get device binding");
__ASSERT(device_is_ready(dev), "Device %s is not ready", dev->name);
printk("device is %p, name is %s\n", dev, dev->name);
do_main(dev);
return 0;
}