Plant Monitor SAM3X8E
Loading...
Searching...
No Matches
format.c
Go to the documentation of this file.
1
9
10#include <stdlib.h>
11#include <stdint.h>
12
13void format_humidity(uint32_t val, char* buffer)
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}
43
44void format_pressure(uint32_t val, char* buffer)
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}
74
75void format_temp(int32_t val, char* buffer)
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}
void format_temp(int32_t val, char *buffer)
Formats BME280 temperature reading for display.
Definition format.c:75
void format_humidity(uint32_t val, char *buffer)
Formats BME280 humidity reading for display.
Definition format.c:13
void format_pressure(uint32_t val, char *buffer)
Formats BME280 pressure reading for display.
Definition format.c:44