Blame view

RIOT/tests/driver_hih6130/main.c 2.02 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  /*
   * Copyright (C) 2015 Eistec AB
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup tests
   * @{
   *
   * @file
   * @brief       Test application for the HIH6130 sensor driver
   *
   * @author      Joakim Nohlgård <joakim.nohlgard@eistec.se>
   *
   * @}
   */
  
  #ifndef TEST_HIH6130_I2C
  #error "TEST_HIH6130_I2C not defined"
  #endif
  #ifndef TEST_HIH6130_ADDR
  #error "TEST_HIH6130_ADDR not defined"
  #endif
  
  #include <stdio.h>
  #include <stdlib.h>
  #include <math.h>
  
  #include "xtimer.h"
  #include "hih6130.h"
  
  #define SLEEP    (100 * 1000U)
  
  int main(void)
  {
      hih6130_t dev;
  
      puts("HIH6130 sensor driver test application\n");
      printf("Initializing I2C_%i... ", TEST_HIH6130_I2C);
      if (i2c_init_master(TEST_HIH6130_I2C, I2C_SPEED_FAST) < 0) {
          puts("[Failed]");
          return -1;
      }
      puts("[OK]");
  
      printf("Initializing HIH6130 sensor at I2C_%i, address 0x%02x... ",
          TEST_HIH6130_I2C, TEST_HIH6130_ADDR);
      hih6130_init(&dev, TEST_HIH6130_I2C, TEST_HIH6130_ADDR);
      puts("[OK]");
  
      while (1) {
          float hum = 0.f;
          float temp = 0.f;
          int status;
          float integral = 0.f;
          float fractional;
  
          xtimer_usleep(SLEEP);
  
          status = hih6130_get_humidity_temperature_float(&dev, &hum, &temp);
          if (status < 0) {
              printf("Communication error: %d\n", status);
              continue;
          } else if (status == 1) {
              puts("Stale values");
          }
          /* Several platforms usually build with nano.specs, (without float printf) */
          /* Split value into two integer parts for printing. */
          fractional = modff(hum, &integral);
          printf("humidity: %4d.%04u %%",
              (int)integral, (unsigned int)abs(fractional * 10000.f));
          fractional = modff(temp, &integral);
          printf("  temperature: %4d.%04u C\n",
              (int)integral, (unsigned int)abs(fractional * 10000.f));
      }
  
      return 0;
  }