Skip to content

Instantly share code, notes, and snippets.

@hexagon5un
Created Nov 15, 2018
Embed
What would you like to do?
Quick demo code for exploring the Supplyfrrame Cube
import serial
import os
s = serial.Serial("/dev/ttyUSB0", baudrate=115200, timeout=0.1)
# timeout: the cube seems to respond in 0.3 sec to "mtu" command
def send(command, s=s):
s.write(command + "\r")
def receive(s=s):
"""Returns ascii data if it's there, else None"""
count = 0
output = None
while count < 15: # arbitrary
lookahead = s.readline().strip()
if lookahead == "+":
return output
if "error" in lookahead:
return None
output = lookahead
count = count + 1
def do(command, s=s):
send(command)
response = receive()
return response
def toBytes(asciiString):
asciiList = asciiString.split()
byteList = [chr(int(x)) for x in asciiList]
return "".join(byteList)
def toASCII(byteString):
output = ""
for c in byteString:
output = output + " %03d" % ord(c)
return output
def pad(asciiString):
while len(asciiString) < 512:
asciiString = asciiString + "\x00"
return asciiString
def unpad(asciiString):
return asciiString.strip("\x00")
def help(s=s):
send("?")
print s.read(4000).strip()
def accelerometer(s=s):
return [int(x) for x in do("mau").split()][0:3]
if __name__ == "__main__":
## Initialize encryption: store 8 blocks of random data in block 32
do("w64")
do("mtf8")
## Test first block of random secret
do("r64")
do("mfu") # looks random to me. :)
## Copy message over into buffer B
superSecret = "Hello World!"
send("mub")
send(pad(superSecret))
receive() # cube acknowledges with a "+" -- this clears it out
receive() # Why twice?
print toBytes(do("mbu")) # verify
## Encrypt with key in 32
do("r64")
encrypted = do("xbu")
print encrypted
## Decrypt
send("muc")
send(toBytes(encrypted))
receive()
receive()
encryptedC = do("mcu") # verify encrypted in buffer C
do("r64")
decoded = do("xcu")
print unpad(toBytes(decoded))
## Decrypting with wrong key
## Key read/write pointers update on each "move" command, so it'll be right
## on the second try.
print do("r62")
print do("xcu")
print do("xcu")
print do("xcu")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment