summaryrefslogtreecommitdiffstats
path: root/libs/nanopb/examples/using_double_on_avr/decode_double.c
blob: 5802eca79e9ea617c4a992bd23a9503b8f23ad3f (plain)
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
/* Decodes a double value into a float variable.
 * Used to read double values with AVR code, which doesn't support double directly.
 */

#include <stdio.h>
#include <pb_decode.h>
#include "double_conversion.h"
#include "doubleproto.pb.h"

int main()
{
    uint8_t buffer[32];
    size_t count = fread(buffer, 1, sizeof(buffer), stdin);
    pb_istream_t stream = pb_istream_from_buffer(buffer, count);
    
    AVRDoubleMessage message;
    pb_decode(&stream, AVRDoubleMessage_fields, &message);
    
    float v1 = double_to_float(message.field1);
    float v2 = double_to_float(message.field2);

    printf("Values: %f %f\n", v1, v2);
    
    if (v1 == 1234.5678f &&
        v2 == 0.00001f)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}