mirror of
https://github.com/janishutz/BiogasControllerApp.git
synced 2025-11-25 05:44:23 +00:00
Refactor for some name changes of libraries
This commit is contained in:
24
util/decoder.py
Normal file
24
util/decoder.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import struct
|
||||
|
||||
|
||||
# Decoder to decode various sent values from the microcontroller
|
||||
class Decoder:
|
||||
# Decode an ascii character
|
||||
def decode_ascii(self, value: bytes) -> str:
|
||||
try:
|
||||
return value.decode()
|
||||
except:
|
||||
return "Error"
|
||||
|
||||
# Decode a float (6 bits)
|
||||
def decode_float(self, value: bytes) -> float:
|
||||
return struct.unpack(">f", bytes.fromhex(str(value, "ascii") + "00"))[0]
|
||||
|
||||
# Decode a float, but with additional offsets
|
||||
def decode_float_long(self, value: bytes) -> float:
|
||||
return struct.unpack(">f", bytes.fromhex(str(value, "ascii") + "0000"))[0]
|
||||
|
||||
# Decode an int
|
||||
def decode_int(self, value: bytes) -> int:
|
||||
# return int.from_bytes(value, 'big')
|
||||
return int(value, base=16)
|
||||
Reference in New Issue
Block a user