You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2020/07/13 14:29:47 UTC

[plc4x] branch feature/knx-read-write-support updated: - Implemented the functionality to read floating point valies in half, full and double precision.

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch feature/knx-read-write-support
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/feature/knx-read-write-support by this push:
     new 42e648c  - Implemented the functionality to read floating point valies in half, full and double precision.
42e648c is described below

commit 42e648c3c7e23084308703f12b8c5e6e1180683d
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Mon Jul 13 16:29:40 2020 +0200

    - Implemented the functionality to read floating point valies in half, full and double precision.
---
 sandbox/plc4c/spi/src/read_buffer.c       | 54 +++++++++++++++++++++++++++-
 sandbox/plc4c/spi/test/read_buffer_test.c | 59 +++++++++++++++++++++++++++++++
 2 files changed, 112 insertions(+), 1 deletion(-)

diff --git a/sandbox/plc4c/spi/src/read_buffer.c b/sandbox/plc4c/spi/src/read_buffer.c
index 8c87f9a..1d0cc28 100644
--- a/sandbox/plc4c/spi/src/read_buffer.c
+++ b/sandbox/plc4c/spi/src/read_buffer.c
@@ -19,6 +19,7 @@
 
 #include <plc4c/spi/read_buffer.h>
 #include <string.h>
+#include <math.h>
 
 // As we're doing some operations where byte-order is important, we need this
 // little helper to find out if we're on a big- or little-endian machine.
@@ -114,7 +115,7 @@ plc4c_return_code plc4c_spi_read_unsigned_bits_internal(
     uint8_t num_bytes_to_write = (num_bits / 8) + ((num_bits % 8 != 0) ? 1 : 0);
     // Do a quick range check for the input
     // (for the output, the calling function is responsible)
-    if ((buf->curPosByte + num_bytes_to_read) > buf->length) {
+    if ((buf->curPosByte + num_bytes_to_read - ((((buf->curPosBit + num_bits) % 8) == 0) ? 1 : 0)) > buf->length) {
       return OUT_OF_RANGE;
     }
 
@@ -522,11 +523,62 @@ plc4c_return_code plc4c_spi_read_signed_long(plc4c_spi_read_buffer* buf,
 
 plc4c_return_code plc4c_spi_read_float(plc4c_spi_read_buffer* buf,
                                        uint8_t num_bits, float* value) {
+  if(num_bits == 16) {
+      // https://en.wikipedia.org/wiki/Half-precision_floating-point_format
+      bool sign = false;
+      plc4c_return_code res = plc4c_spi_read_bit(buf, &sign);
+      if(res != OK) {
+        return res;
+      }
+      uint8_t exponent = 0;
+      res = plc4c_spi_read_unsigned_byte(buf, 5, &exponent);
+      if(res != OK) {
+        return res;
+      }
+      uint16_t fraction = 0;
+      res = plc4c_spi_read_unsigned_short(buf, 10, &fraction);
+      if(res != OK) {
+        return res;
+      }
+
+      if((exponent >= 1) && (exponent <= 30)) {
+        *value = (sign ? (float) 1 : (float) -1) * ((float) (2 ^ (exponent - 15))) * ((float) (1 + (fraction / 10)));
+      } else if(exponent == 0) {
+        if (fraction == 0) {
+          *value = 0.0f;
+        } else {
+          *value = (sign ? (float) 1 : (float) -1) * ((float) (2 ^ (-14))) * ((float) (fraction / 10));
+        }
+      } else if(exponent == 31) {
+        if (fraction == 0) {
+          *value = sign ? INFINITY : -INFINITY;
+        } else {
+          *value = NAN;
+        }
+      } else {
+        return INVALID_ARGUMENT;
+      }
+  } else if(num_bits == 32) {
+      plc4c_return_code res = plc4c_spi_read_unsigned_int(buf, 32, (uint32_t*) value);
+      if(res != OK) {
+          return res;
+      }
+  } else {
+      return INVALID_ARGUMENT;
+  }
   return OK;
 }
 
 plc4c_return_code plc4c_spi_read_double(plc4c_spi_read_buffer* buf,
                                         uint8_t num_bits, double* value) {
+  if(num_bits == 64) {
+    plc4c_return_code res = plc4c_spi_read_unsigned_long(buf, 64, (uint64_t*) value);
+    if(res != OK) {
+      return res;
+    }
+  } else {
+    return INVALID_ARGUMENT;
+  }
   return OK;
 }
 
diff --git a/sandbox/plc4c/spi/test/read_buffer_test.c b/sandbox/plc4c/spi/test/read_buffer_test.c
index 1fd3fe8..7f561f8 100644
--- a/sandbox/plc4c/spi/test/read_buffer_test.c
+++ b/sandbox/plc4c/spi/test/read_buffer_test.c
@@ -647,6 +647,62 @@ void test_plc4c_spi_read_signed_long(void) {
   test_plc4c_spi_read_signed_long_args("Simple 6 bit signed long", read_buffer, 6, OK, 22);
 }
 
+void test_plc4c_spi_read_float_args(char* message,
+                                    plc4c_spi_read_buffer* read_buffer, uint8_t num_bits,
+                                    plc4c_return_code expected_return_code, float expected_value) {
+    printf("Running read_buffer read_float test: %s", message);
+
+    float value = -1;
+    plc4c_return_code result =
+            plc4c_spi_read_float(read_buffer, num_bits, &value);
+
+    TEST_ASSERT_EQUAL_INT(expected_return_code, result);
+    TEST_ASSERT_EQUAL_INT(expected_value, value);
+
+    printf(" -> OK\n");
+}
+
+void test_plc4c_spi_read_float(void) {
+    // Prepare input data
+    uint8_t data[] = {0x40, 0x49, 0x0f, 0xdb};
+    plc4c_spi_read_buffer* read_buffer;
+    plc4c_spi_read_buffer_create(data, 4, &read_buffer);
+    // Run test
+    // Read all the full bytes
+    test_plc4c_spi_read_float_args("Simple 16 bit float 1", read_buffer, 16, OK, -24);
+    test_plc4c_spi_read_float_args("Simple 16 bit float 2", read_buffer, 16, OK, 990);
+    // Read the only part of a long (having to fill up 1s)
+    read_buffer->curPosByte = 0;
+    read_buffer->curPosBit = 0;
+    test_plc4c_spi_read_float_args("Simple 32 bit float", read_buffer, 32, OK, 3.14159274);
+    // Read the only part of a long (having to fill up 1s)
+}
+
+void test_plc4c_spi_read_double_args(char* message,
+                                    plc4c_spi_read_buffer* read_buffer, uint8_t num_bits,
+                                    plc4c_return_code expected_return_code, double expected_value) {
+    printf("Running read_buffer read_double test: %s", message);
+
+    double value = -1;
+    plc4c_return_code result =
+            plc4c_spi_read_double(read_buffer, num_bits, &value);
+
+    TEST_ASSERT_EQUAL_INT(expected_return_code, result);
+    TEST_ASSERT_EQUAL_INT(expected_value, value);
+
+    printf(" -> OK\n");
+}
+
+void test_plc4c_spi_read_double(void) {
+    // Prepare input data
+    uint8_t data[] = {0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18};
+    plc4c_spi_read_buffer* read_buffer;
+    plc4c_spi_read_buffer_create(data, 8, &read_buffer);
+    test_plc4c_spi_read_double_args("Simple 64 bit float", read_buffer, 64, OK, 3.1415926535897931);
+    // Read the only part of a long (having to fill up 1s)
+}
+
+
 void test_plc4c_spi_read_buffer(void) {
   test_plc4c_spi_read_buffer_create();
   test_plc4c_spi_read_get_total_bytes();
@@ -665,4 +721,7 @@ void test_plc4c_spi_read_buffer(void) {
   test_plc4c_spi_read_signed_short();
   test_plc4c_spi_read_signed_int();
   test_plc4c_spi_read_signed_long();
+
+  test_plc4c_spi_read_float();
+  test_plc4c_spi_read_double();
 }
\ No newline at end of file