middleware.py 2.81 KB
Newer Older
1 2 3 4 5 6 7 8 9
#!/usr/bin/python3 -u

import os
import sys
import time
import struct
import serial
import subprocess

Sebastian Renner committed
10
DBG=''
11 12 13 14 15 16


ser = serial.Serial('/dev/ttyUSB0', 115200)

def flash():
        pipe = subprocess.PIPE
17 18
        p = subprocess.Popen(['JLinkExe', 'flash.jlink'], stdout=sys.stderr, stdin=pipe)
        #p = subprocess.Popen(['stm32flash', 'build/f7.bin', '0x8000000'], stdout=sys.stderr, stdin=pipe)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        stdout, stderr = p.communicate("")

def read(channel, l):
    if channel == 'ser':
        data = ser.read(l)
    elif channel == 'std':
        data = sys.stdin.buffer.read(l)
    else:
        raise Exception("read() complains: no sai channelino")
    if len(data) != l:
        raise Exception("could not read %d bytes of data (got %d)" % (l, len(data)))
    return data

def write(channel, data):
    if channel == 'ser':
        l = ser.write(data)
        if DBG:
            print("Data sent to serial: " + str(data) + "\n", file=sys.stderr)
    elif channel == 'std':
        l = sys.stdout.buffer.write(data)
    else:
        raise Exception("write() complains: no sai channelino")
    if len(data) != l:
        raise Exception("could not write %d bytes of data (put %d)" % (len(data), l))
            
def obtain(channel):
    l = read(channel, 4)
    (l, ) = struct.unpack("<I", l)
    if DBG:
        print("Trying to read %d bytes from %s\n" % (l, channel), file=sys.stderr)
    return read(channel, l)

def submit(channel, action, data):
    if channel == "std":
        h = struct.pack("<I", len(data))
    else:
        h = struct.pack("<BI", ord(action), len(data))
    write(channel, h)
    write(channel, data)
    time.sleep(0.1)

def main(argv):
    if DBG:
        print(argv[0], file=sys.stderr)
    script_dir = os.path.split(argv[0])[0]
    if len(script_dir ) > 0:
        os.chdir(script_dir)
    flash()
    print("Hello, World!")
    while(1):
        action = read('std', 1)
        if DBG:
            print("Read following action from stdin: %s\n" % action, file=sys.stderr)
        if action == b'c' or action == b'm' or action == b'a' or action == b'k' or action == b's' or action == b'p':
            data = obtain('std')
            submit('ser', action, data)

        elif action == b'e' or action == b'd':
            write('ser', action)

        elif action == b'C' or action == b'M' or action == b'A' or action == b'K' or action == b'S' or action == b'P':
            write('ser', action)
            data = obtain('ser')
            if DBG:
                print("Obtained following data from serial: %s\n" % data, file=sys.stderr)
            submit('std', action, data)
            if DBG:
                print("Submitted following data to stdout: %s\n" % data, file=sys.stderr)
        else:
            raise Exception("no capiche aczione %s" % (action))
                
if __name__ == "__main__":
    sys.exit(main(sys.argv))