17ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/* Encodes a float value into a double on the wire.
27ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * Used to emit doubles from AVR code, which doesn't support double directly.
37ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen */
47ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen
57ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <stdio.h>
67ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <pb_encode.h>
77ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include "double_conversion.h"
87ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include "doubleproto.pb.h"
97ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen
107ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenint main()
117ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen{
127ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen    AVRDoubleMessage message = {
137ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen        float_to_double(1234.5678f),
147ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen        float_to_double(0.00001f)
157ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen    };
167ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen
177ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen    uint8_t buffer[32];
187ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen    pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
197ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen
207ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen    pb_encode(&stream, AVRDoubleMessage_fields, &message);
217ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen    fwrite(buffer, 1, stream.bytes_written, stdout);
227ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen
237ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen    return 0;
247ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen}
257ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen
26