Plant Monitor SAM3X8E
Loading...
Searching...
No Matches
format.c File Reference

Formatting functions for sensor measurements. More...

#include <stdlib.h>
#include <stdint.h>

Go to the source code of this file.

Functions

void format_humidity (uint32_t val, char *buffer)
 Formats BME280 humidity reading for display.
void format_pressure (uint32_t val, char *buffer)
 Formats BME280 pressure reading for display.
void format_temp (int32_t val, char *buffer)
 Formats BME280 temperature reading for display.

Detailed Description

Formatting functions for sensor measurements.

Contains implementations of functions for producing display-ready strings from raw sensor readings.

Author
Vladimir Zhelev

Definition in file format.c.

Function Documentation

◆ format_humidity()

void format_humidity ( uint32_t val,
char * buffer )

Formats BME280 humidity reading for display.

Converts compensated humidity value into a percentage string.

Parameters
valCompensated humidity measurement in units of 1/1024RH.
bufferDestination buffer for formatted string.

Definition at line 13 of file format.c.

14{
15 uint32_t exponent = (val / 1024);
16 uint32_t mantissa = ((val % 1024) * 100) / 1024;
17
18 size_t i = 0;
19 size_t j = 0;
20 char temp[16];
21
22 while (i < 2)
23 {
24 temp[i++] = (mantissa % 10) + '0';
25 mantissa /= 10;
26 }
27
28 temp[i++] = '.';
29
30 do {
31 temp[i++] = (exponent % 10) + '0';
32 exponent /= 10;
33 } while (exponent > 0);
34
35 while (i > 0)
36 buffer[j++] = temp[--i];
37
38 buffer[j++] = '%';
39 buffer[j++] = 'R';
40 buffer[j++] = 'H';
41 buffer[j] = '\0';
42}

◆ format_pressure()

void format_pressure ( uint32_t val,
char * buffer )

Formats BME280 pressure reading for display.

Converts compensated pressure value into a string containing pressure in hectopascals.

Parameters
valCompensated pressure measurement from BME280.
bufferDestination buffer for formatted string.

Definition at line 44 of file format.c.

45{
46 uint32_t exponent = (val / 25600);
47 uint32_t mantissa = (val % 25600) / 256;
48
49 size_t i = 0;
50 size_t j = 0;
51 char temp[16];
52
53 while (i < 2)
54 {
55 temp[i++] = (mantissa % 10) + '0';
56 mantissa /= 10;
57 }
58
59 temp[i++] = '.';
60
61 do {
62 temp[i++] = (exponent % 10) + '0';
63 exponent /= 10;
64 } while (exponent > 0);
65
66 while (i > 0)
67 buffer[j++] = temp[--i];
68
69 buffer[j++] = 'h';
70 buffer[j++] = 'P';
71 buffer[j++] = 'a';
72 buffer[j] = '\0';
73}

◆ format_temp()

void format_temp ( int32_t val,
char * buffer )

Formats BME280 temperature reading for display.

Converts compensated temperature value into a string containing temperature in degrees Celsius.

Parameters
valCompensated temperature measurement in units of 0.01°C.
bufferDestination buffer for formatted string.

Definition at line 75 of file format.c.

76{
77 uint32_t magnitude = (val < 0)
78 ? (uint32_t)(-(int64_t)val)
79 : (uint32_t)val;
80 size_t i = 0;
81 size_t j = 0;
82 char temp[16];
83
84 while (i < 2)
85 {
86 temp[i++] = (magnitude % 10) + '0';
87 magnitude /= 10;
88 }
89
90 temp[i++] = '.';
91
92 do {
93 temp[i++] = (magnitude % 10) + '0';
94 magnitude /= 10;
95 } while (magnitude > 0);
96
97 /* Attach sign */
98 temp[i++] = (val < 0)
99 ? '-'
100 : '+';
101
102 /* Fill buffer in reverse with assorted string */
103 while (i > 0)
104 buffer[j++] = temp[--i];
105
106 buffer[j++] = 'C';
107 buffer[j] = '\0';
108}