summaryrefslogtreecommitdiffstats
path: root/example_avr_double/encode_double.c
blob: cd532d4659c06761001415057b817bcb57d94721 (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
/* Encodes a float value into a double on the wire.
 * Used to emit doubles from AVR code, which doesn't support double directly.
 */

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

int main()
{
    AVRDoubleMessage message = {
        float_to_double(1234.5678f),
        float_to_double(0.00001f)
    };
    
    uint8_t buffer[32];
    pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
    
    pb_encode(&stream, AVRDoubleMessage_fields, &message);
    fwrite(buffer, 1, stream.bytes_written, stdout);

    return 0;
}