aboutsummaryrefslogtreecommitdiffstats
path: root/example_avr_double/decode_double.c
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2013-01-29 22:10:37 +0200
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2013-01-29 22:10:37 +0200
commite4b55179d1f53c42916f5e62fb83789973bf4b01 (patch)
tree4505321c4c14bdca6362b1e1bb1054c3588a4543 /example_avr_double/decode_double.c
parent2392d255749715ad337d3f5e23d3de7f2065e3dd (diff)
Add an example of handling doubles on AVR platform.
Diffstat (limited to 'example_avr_double/decode_double.c')
-rw-r--r--example_avr_double/decode_double.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/example_avr_double/decode_double.c b/example_avr_double/decode_double.c
new file mode 100644
index 0000000..5802eca
--- /dev/null
+++ b/example_avr_double/decode_double.c
@@ -0,0 +1,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;
+ }
+}