You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by hu...@apache.org on 2023/05/12 04:06:10 UTC

[plc4x] branch develop updated: feat(plc4py): Add initial signed int

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

hutcheb pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new e14a3d7dc8 feat(plc4py): Add initial signed int
e14a3d7dc8 is described below

commit e14a3d7dc8bae8ead824f019d5e87767c4460adc
Author: Ben Hutcheson <be...@gmail.com>
AuthorDate: Fri May 12 06:01:06 2023 +0200

    feat(plc4py): Add initial signed int
---
 sandbox/plc4py/plc4py/spi/generation/WriteBuffer.py  | 20 ++++++++++++++------
 .../tests/unit/plc4py/spi/test_write_buffer.py       | 20 ++++++++++++++++++--
 2 files changed, 32 insertions(+), 8 deletions(-)

diff --git a/sandbox/plc4py/plc4py/spi/generation/WriteBuffer.py b/sandbox/plc4py/plc4py/spi/generation/WriteBuffer.py
index cb1d3cece1..3fca938f92 100644
--- a/sandbox/plc4py/plc4py/spi/generation/WriteBuffer.py
+++ b/sandbox/plc4py/plc4py/spi/generation/WriteBuffer.py
@@ -14,7 +14,8 @@
 #  KIND, either express or implied.  See the License for the
 #  specific language governing permissions and limitations
 #  under the License.
-from ctypes import c_bool, c_byte, c_ubyte, c_uint16, c_uint32, c_uint64, c_int16, c_int32, c_int64, c_float, c_double
+from ctypes import c_bool, c_byte, c_ubyte, c_uint16, c_uint32, c_uint64, c_int16, c_int32, c_int64, c_float, c_double, \
+    c_int8
 from dataclasses import dataclass
 from typing import List
 
@@ -65,7 +66,7 @@ class WriteBuffer(ByteOrderAware, PositionAware):
     def write_unsigned_long(self, value: c_uint64, bit_length: int = 64, logical_name: str = "", **kwargs) -> None:
         raise NotImplementedError
 
-    def write_signed_byte(self, value: c_byte, bit_length: int = 8, logical_name: str = "", **kwargs) -> None:
+    def write_signed_byte(self, value: c_int8, bit_length: int = 8, logical_name: str = "", **kwargs) -> None:
         raise NotImplementedError
 
     def write_short(self, value: c_int16, bit_length: int = 16, logical_name: str = "", **kwargs) -> None:
@@ -135,10 +136,7 @@ class WriteBufferByteBased(WriteBuffer):
         elif bit_length > 8:
             raise SerializationException("unsigned byte can only contain max 8 bits")
         else:
-            src = bitarray(endian=ByteOrder.get_short_name(self.byte_order))
-            src.frombytes(value)
-            self.bb[self.position:bit_length] = src[:bit_length]
-            self.position += bit_length
+            self._handle_integer_encoding(c_byte(value.value), bit_length, **kwargs)
 
     def write_unsigned_short(self, value: c_uint16, bit_length: int = 16, logical_name: str = "", **kwargs) -> None:
         if bit_length <= 0:
@@ -185,3 +183,13 @@ class WriteBufferByteBased(WriteBuffer):
             src.frombytes(value)
             self.bb[self.position:bit_length] = src[:bit_length]
             self.position += bit_length
+
+    def write_signed_byte(self, value: c_int8, bit_length: int = 8, logical_name: str = "", **kwargs) -> None:
+        if bit_length <= 0:
+            raise SerializationException("Signed byte must contain at least 1 bit")
+        elif bit_length > 8:
+            raise SerializationException("Signed byte can only contain max 8 bits")
+        src = bitarray(endian=ByteOrder.get_short_name(self.byte_order))
+        src.frombytes(value)
+        self.bb[self.position:bit_length] = src[:bit_length]
+        self.position += bit_length
diff --git a/sandbox/plc4py/tests/unit/plc4py/spi/test_write_buffer.py b/sandbox/plc4py/tests/unit/plc4py/spi/test_write_buffer.py
index 17d8479374..ab51bd85d2 100644
--- a/sandbox/plc4py/tests/unit/plc4py/spi/test_write_buffer.py
+++ b/sandbox/plc4py/tests/unit/plc4py/spi/test_write_buffer.py
@@ -16,7 +16,7 @@
 # specific language governing permissions and limitations
 # under the License.
 #
-from ctypes import c_bool, c_byte, c_uint16, c_uint64
+from ctypes import c_bool, c_byte, c_uint16, c_uint64, c_int8
 
 import pytest
 from bitarray import bitarray
@@ -215,8 +215,24 @@ def test_write_buffer_write_unsigned_long_ascii_encoding_little_endian(mocker) -
     assert (ba.obj == bitarray("10001100 10001100 10001100 10001100 10001100 10001100 10001100 10001100", endian="little"))
 
 
-def test_write_buffer_write_unsigned_short_ascii_encoding_big_endian(mocker) -> None:
+def test_write_buffer_write_unsigned_long_ascii_encoding_big_endian(mocker) -> None:
     wb: WriteBufferByteBased = WriteBufferByteBased(8, ByteOrder.BIG_ENDIAN)
     wb.write_unsigned_long(c_uint64(11111111), 64, "ASCII Value of 1111 1111 - 0x3131313131313131", encoding="ASCII")
     ba: memoryview = wb.get_bytes()
     assert (ba.obj == bitarray("00110001 00110001 00110001 00110001 00110001 00110001 00110001 00110001", endian="big"))
+
+
+def test_write_buffer_set_signed_byte(mocker) -> None:
+    wb: WriteBufferByteBased = WriteBufferByteBased(1, ByteOrder.LITTLE_ENDIAN)
+    wb.write_signed_byte(c_int8(-1), 8)
+    ba: memoryview = wb.get_bytes()
+    assert (ba.obj == bitarray("11111111",
+                               endian="little"))
+
+
+def test_write_buffer_set_signed_byte_three(mocker) -> None:
+    wb: WriteBufferByteBased = WriteBufferByteBased(1, ByteOrder.LITTLE_ENDIAN)
+    wb.write_signed_byte(c_int8(3), 8)
+    ba: memoryview = wb.get_bytes()
+    assert (ba.obj == bitarray("11000000",
+                               endian="little"))