diff options
author | Thomas Van Iseghem <[email protected]> | 2023-09-13 21:04:35 +0200 |
---|---|---|
committer | Thomas Van Iseghem <[email protected]> | 2023-09-13 21:04:35 +0200 |
commit | 8f20b973a8b0dbe5cfbe021548c9fb5d0787dd05 (patch) | |
tree | b77e07036badc97b4a05959aaf93deb3bd22f3a3 | |
parent | c602895995e5ba88dd3f29c6a984b86bd9a4f140 (diff) | |
download | OpenCortex-8f20b973a8b0dbe5cfbe021548c9fb5d0787dd05.tar.gz OpenCortex-8f20b973a8b0dbe5cfbe021548c9fb5d0787dd05.zip |
Updated read_stomp and added write_stomp proof of concept
-rw-r--r-- | Stomps/README.md | 2 | ||||
-rw-r--r-- | Stomps/read_stomp.py | 28 | ||||
-rw-r--r-- | Stomps/write_stomp.py | 76 |
3 files changed, 102 insertions, 4 deletions
diff --git a/Stomps/README.md b/Stomps/README.md index 53e5d1b..da4fe92 100644 --- a/Stomps/README.md +++ b/Stomps/README.md @@ -25,7 +25,7 @@ It seems that a 32 bytes message is divided into 2 parts of 16 bytes. ### Part 1 (first 16 bytes) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | |---|---|---|---|---|---|---|---|---|---|--------|--------|-------|---------|---------|---------| -| time | time | time | time | time | time | time | padding? | action | action (not really used) | action | action | value | padding | padding | padding | +| time | time | time | time | ? | ? | ? | padding? | action | action (not really used) | action | action | value | padding | padding | padding | ### Part 1 (last 16 bytes) - 0 -> 6 are duplicates diff --git a/Stomps/read_stomp.py b/Stomps/read_stomp.py index 516db24..65dcdae 100644 --- a/Stomps/read_stomp.py +++ b/Stomps/read_stomp.py @@ -1,8 +1,15 @@ +# Quick and simple Python 2 script to read the raw bytes, +# from a stomp device on the Quad Cortex. + +from datetime import datetime + class StompEvent: def __init__(self, raw_bytes): self.raw_bytes = raw_bytes self.action = "unknown" self.value = "unknown" + self.system_epoch = datetime.now().strftime("%s") + self.system_datetime = datetime.now() self.decode_bytes() def print_bytes(self): @@ -13,15 +20,20 @@ class StompEvent: print '---'*32 def decode_bytes(self): + self.decode_action() + self.decode_value() + self.decode_time() + + def decode_action(self): action_byte_1 = self.raw_bytes[10].encode('hex') action_byte_2 = self.raw_bytes[11].encode('hex') - value_byte = self.raw_bytes[12].encode('hex') - if(action_byte_1 == "11" and action_byte_2 == "01"): self.action = "button" elif(action_byte_1 == "08" and action_byte_2 == "00"): self.action = "rotary" + def decode_value(self): + value_byte = self.raw_bytes[12].encode('hex') if(value_byte == "00"): self.value = "released" elif(value_byte == "01"): @@ -32,8 +44,18 @@ class StompEvent: elif(value_byte == "ff"): self.value = "counter clock" + def decode_time(self): + # the first 4 bytes form the time + time_bytes = self.raw_bytes[0:4] + time_bytes.reverse() + time_hex = "" + for i in range(len(time_bytes)): + time_hex = time_hex + time_bytes[i].encode('hex') + self.epoch = int(time_hex, 16) + self.date_time = datetime.fromtimestamp(self.epoch) + def __str__(self): - return "Action: " + self.action + " | Value: " + self.value + return "Action: " + self.action + "\nValue: " + self.value + "\nEpoch: " + str(self.epoch) + "\nDatetime: " + str(self.date_time) with open('/dev/zencoder/knob_stomp2', 'rb') as file: byte_buffer = [] diff --git a/Stomps/write_stomp.py b/Stomps/write_stomp.py new file mode 100644 index 0000000..002acd4 --- /dev/null +++ b/Stomps/write_stomp.py @@ -0,0 +1,76 @@ +# Quick and simple Python 2 script to write the raw bytes, +# to a stomp device on the Quad Cortex. + +from datetime import datetime + +class StompCommand: + def __init__(self, action, value): + self.action = action + self.value = value + self.raw_bytes = bytearray(32) + self.encode_bytes() + + def encode_bytes(self): + self.encode_time() + self.encode_action() + self.encode_value() + self.build_last_16_bytes() + + def encode_time(self): + epoch = datetime.now().strftime("%s") + epoch_hex = hex(int(epoch))[2:] + epoch_bytes = bytearray.fromhex(epoch_hex) + epoch_bytes.reverse() + for i in range(4): + self.raw_bytes[i] = epoch_bytes[i] + + def encode_action(self): + self.raw_bytes[9] = 0x00 + if(self.action == "button"): + self.raw_bytes[8] = 0x01 + self.raw_bytes[10] = 0x11 + self.raw_bytes[11] = 0x01 + elif(self.action == "rotary"): + self.raw_bytes[8] = 0x01 + self.raw_bytes[10] = 0x08 + self.raw_bytes[11] = 0x00 + + def encode_value(self): + if(self.value == "pressed"): + self.raw_bytes[12] = 0x01 + elif(self.value == "released"): + self.raw_bytes[12] = 0x00 + elif(self.value == "clock wise"): + self.raw_bytes[12] = 0x01 + elif(self.value == "counter clock"): + self.raw_bytes[12] = 0xff + + def build_last_16_bytes(self): + # build the last 16 bytes + for i in range(6): + self.raw_bytes[16+i] = self.raw_bytes[i] + + def bytes_to_hex(self): + bytes_str = "" + for i in range(len(self.raw_bytes)): + bytes_str = bytes_str + str(self.raw_bytes[i]) + ' ' + return bytes_str + + def __str__(self): + return "Action: " + self.action + "\nValue: " + self.value + "\nBytes: " + self.bytes_to_hex() + + +with open('/dev/zencoder/knob_stomp2', 'wb') as file: + # write a button press + command = StompCommand("button", "pressed") + print command + file.write(command.raw_bytes) + + print '' + + # write a button release + command = StompCommand("button", "released") + print command + file.write(command.raw_bytes) + # close the file + file.close()
\ No newline at end of file |