zephyr/samples/sensor/grove_temperature/src/main.c
Andy Ross 32bb2395c2 timeout: Fix up API usage
Kernel timeouts have always been a 32 bit integer despite the
existence of generation macros, and existing code has been
inconsistent about using them.  Upcoming commits are going to make the
timeout arguments opaque, so fix things up to be rigorously correct.
Changes include:

+ Adding a K_TIMEOUT_EQ() macro for code that needs to compare timeout
  values for equality (e.g. with K_FOREVER or K_NO_WAIT).

+ Adding a k_msleep() synonym for k_sleep() which can continue to take
  integral arguments as k_sleep() moves away to timeout arguments.

+ Pervasively using the K_MSEC(), K_SECONDS(), et. al. macros to
  generate timeout arguments.

+ Removing the usage of K_NO_WAIT as the final argument to
  K_THREAD_DEFINE().  This is just a count of milliseconds and we need
  to use a zero.

This patch include no logic changes and should not affect generated
code at all.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-03-31 19:40:47 -04:00

85 lines
1.8 KiB
C

/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <init.h>
#include <stdio.h>
#include <drivers/sensor.h>
#ifdef CONFIG_GROVE_LCD_RGB
#include <display/grove_lcd.h>
#include <stdio.h>
#include <string.h>
#endif
#define SLEEP_TIME K_MSEC(1000)
void main(void)
{
struct device *dev = device_get_binding(DT_LABEL(DT_INST(0, grove_temperature)));
struct sensor_value temp;
int read;
if (dev == NULL) {
printf("device not found. aborting test.\n");
return;
}
#ifdef CONFIG_GROVE_LCD_RGB
struct device *glcd;
glcd = device_get_binding(GROVE_LCD_NAME);
if (glcd == NULL) {
printf("Failed to get Grove LCD\n");
return;
}
/* configure LCD */
glcd_function_set(glcd, GLCD_FS_ROWS_2 | GLCD_FS_DOT_SIZE_LITTLE |
GLCD_FS_8BIT_MODE);
glcd_display_state_set(glcd, GLCD_DS_DISPLAY_ON);
#endif
while (1) {
read = sensor_sample_fetch(dev);
if (read) {
printk("sample fetch error\n");
continue;
}
sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
#ifdef CONFIG_GROVE_LCD_RGB
char row[16];
/* clear LCD */
(void)memset(row, ' ', sizeof(row));
glcd_cursor_pos_set(glcd, 0, 0);
glcd_print(glcd, row, sizeof(row));
glcd_cursor_pos_set(glcd, 0, 1);
glcd_print(glcd, row, sizeof(row));
/* display temperature on LCD */
glcd_cursor_pos_set(glcd, 0, 0);
#ifdef CONFIG_NEWLIB_LIBC_FLOAT_PRINTF
sprintf(row, "T:%.2f%cC",
sensor_value_to_double(&temp),
223 /* degree symbol */);
#else
sprintf(row, "T:%d%cC", temp.val1,
223 /* degree symbol */);
#endif
glcd_print(glcd, row, strlen(row));
#endif
#ifdef CONFIG_NEWLIB_LIBC_FLOAT_PRINTF
printf("Temperature: %.2f C\n", sensor_value_to_double(&temp));
#else
printk("Temperature: %d\n", temp.val1);
#endif
k_sleep(SLEEP_TIME);
}
}