|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 CNRS, France |
| 3 | + * |
| 4 | + * This file is subject to the terms and conditions of the GNU Lesser |
| 5 | + * General Public License v2.1. See the file LICENSE in the top level |
| 6 | + * directory for more details. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * @ingroup tests |
| 11 | + * @{ |
| 12 | + * |
| 13 | + * @file |
| 14 | + * @brief Test application for the Honeywell ABP2 series |
| 15 | + * pressure and temperature sensor driver. |
| 16 | + * |
| 17 | + * @author David Picard <[email protected]> |
| 18 | + * @} |
| 19 | + */ |
| 20 | + |
| 21 | +#include <stdio.h> |
| 22 | +#include <string.h> |
| 23 | + |
| 24 | +#include "ztimer.h" |
| 25 | +#include "timex.h" |
| 26 | +#include "phydat.h" |
| 27 | +#include "abp2.h" |
| 28 | +#include "abp2_params.h" |
| 29 | +#include "saul_reg.h" |
| 30 | + |
| 31 | +#define STATUS_SLEEP_MS (1) |
| 32 | +#define MEASUREMENT_SLEEP_MS (1000) |
| 33 | +#define MAX_LOOPS_STATUS (10) |
| 34 | +#define MAX_LOOPS_MEAS (3) |
| 35 | + |
| 36 | +/* supported acquisition modes */ |
| 37 | +enum { |
| 38 | + NON_BLOCKING, |
| 39 | + BLOCKING, |
| 40 | + SAUL |
| 41 | +} acqmode = NON_BLOCKING; |
| 42 | + |
| 43 | +static abp2_t dev; |
| 44 | + |
| 45 | +int main(void) |
| 46 | +{ |
| 47 | + int32_t press = 0; |
| 48 | + int32_t temp = 0; |
| 49 | + abp2_params_t prms; |
| 50 | + phydat_t phyPress; |
| 51 | + phydat_t phyTemp; |
| 52 | + void *ptr; |
| 53 | + int cntMeas = 0; |
| 54 | + saul_reg_t *saulDev = saul_reg; |
| 55 | + phydat_t saulData; |
| 56 | + |
| 57 | + puts("ABP2 Honeywell series pressure and temperature sensor\n"); |
| 58 | + |
| 59 | + if (saulDev == NULL) { |
| 60 | + puts("ERROR: no SAUL devices detected"); |
| 61 | + } |
| 62 | + |
| 63 | + ptr = memcpy(&prms, &abp2_params, sizeof(abp2_params)); |
| 64 | + if (!ptr) |
| 65 | + { |
| 66 | + puts("main() >> ERROR: memcpy() failed"); |
| 67 | + return 1; |
| 68 | + } |
| 69 | + |
| 70 | + printf("Initializing SPI bus %d\n", prms.spi); |
| 71 | + spi_init(prms.spi); |
| 72 | + |
| 73 | + printf("Initializing ABP2 at SPI_DEV(%i)... ", prms.spi); |
| 74 | + |
| 75 | + if (abp2_init(&dev, &prms) == 0) |
| 76 | + { |
| 77 | + puts("[OK]"); |
| 78 | + } |
| 79 | + else { |
| 80 | + puts("[Failed]"); |
| 81 | + return -1; |
| 82 | + } |
| 83 | + |
| 84 | + puts("========================="); |
| 85 | + puts(" Measuring"); |
| 86 | + puts("========================="); |
| 87 | + |
| 88 | + printf("Pressure range = %d .. %d\n", (int)prms.rangeMin, (int)prms.rangeMax); |
| 89 | + |
| 90 | + while (1) { |
| 91 | + int res; |
| 92 | + |
| 93 | + ztimer_sleep(ZTIMER_MSEC, MEASUREMENT_SLEEP_MS); |
| 94 | + switch (acqmode) { |
| 95 | + case NON_BLOCKING: |
| 96 | + res = abp2_read_nb(&dev, &press, &temp); |
| 97 | + if (res) |
| 98 | + { |
| 99 | + printf("abp2_read_nb() >> ERROR errno = %d", res); |
| 100 | + continue; |
| 101 | + } |
| 102 | + break; |
| 103 | + case BLOCKING: |
| 104 | + res = abp2_read(&dev, &press, &temp); |
| 105 | + if (res) |
| 106 | + { |
| 107 | + printf("abp2_read() >> ERROR errno = %d", res); |
| 108 | + continue; |
| 109 | + } |
| 110 | + break; |
| 111 | + case SAUL: |
| 112 | + while (saulDev) { |
| 113 | + int dim = saul_reg_read(saulDev, &saulData); |
| 114 | + printf("\nDev: %s\tType: %s" "\n", saulDev->name, |
| 115 | + saul_class_to_str(saulDev->driver->type)); |
| 116 | + phydat_dump(&saulData, dim); |
| 117 | + saulDev = saulDev->next; |
| 118 | + } |
| 119 | + saulDev = saul_reg; /* reset pointer for next read */ |
| 120 | + break; |
| 121 | + default: |
| 122 | + acqmode = 0; |
| 123 | + } |
| 124 | + |
| 125 | + /* display data retrieved in blocking and non-blocking modes: */ |
| 126 | + if (acqmode != SAUL) { |
| 127 | + phyPress.val[0] = press / 100; /* let int32_t fit into int16_t */ |
| 128 | + phyPress.scale = -4; /* and set the exponent accordingly */ |
| 129 | + phyPress.unit = UNIT_BAR; /* set the unit */ |
| 130 | + phydat_dump(&phyPress, 1); /* print the value in a pretty format */ |
| 131 | + |
| 132 | + phyTemp.val[0] = temp; /* temp is already in mdeg C */ |
| 133 | + phyTemp.scale = -3; /* 1 mdegC = 1e-03 degC */ |
| 134 | + phyTemp.unit = UNIT_TEMP_C; /* set the unit */ |
| 135 | + phydat_dump(&phyTemp, 1); /* print the value in a pretty format */ |
| 136 | + } |
| 137 | + |
| 138 | + /* Switch modes periodically: */ |
| 139 | + cntMeas++; |
| 140 | + if (cntMeas == MAX_LOOPS_MEAS) { |
| 141 | + cntMeas = 0; |
| 142 | + acqmode++; |
| 143 | + if (acqmode > SAUL) { |
| 144 | + acqmode = 0; |
| 145 | + } |
| 146 | + puts("------------------------------"); |
| 147 | + switch (acqmode) { |
| 148 | + case NON_BLOCKING: |
| 149 | + puts("Switch to non-blocking mode"); |
| 150 | + break; |
| 151 | + case BLOCKING: |
| 152 | + puts("Switch to blocking mode"); |
| 153 | + break; |
| 154 | + case SAUL: |
| 155 | + puts("Switch to SAUL mode"); |
| 156 | + break; |
| 157 | + default: |
| 158 | + acqmode = 0; |
| 159 | + puts("ERROR: unsupported mode"); |
| 160 | + } |
| 161 | + puts("------------------------------"); |
| 162 | + } |
| 163 | + } |
| 164 | + return 0; |
| 165 | +} |
0 commit comments