middleware.py 2.71 KB
Newer Older
1
#!/usr/bin/python3 -u
2 3 4

import os
import sys
5
import time
6 7
import struct
import serial
8
import subprocess
9

10 11
DBG='1'

12

Sebastian Renner committed
13 14
ser = serial.Serial('/dev/ttyUSB0', 115200)

15
def flash():
16 17 18
        pipe = subprocess.PIPE
        p = subprocess.Popen(['JLinkExe', 'flash.jlink'], stdout=sys.stderr, stdin=pipe)
        stdout, stderr = p.communicate("")
19 20 21 22 23

def read(channel, l):
    if channel == 'ser':
        data = ser.read(l)
    elif channel == 'std':
24
        data = sys.stdin.buffer.read(l)
25 26 27 28 29 30
    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

Sebastian Renner committed
31
def write(channel, data):
32 33
    if channel == 'ser':
        l = ser.write(data)
34 35
        if DBG:
            print("Data sent to serial: " + str(data) + "\n", file=sys.stderr)
36
    elif channel == 'std':
37
        l = sys.stdout.buffer.write(data)
38 39 40 41 42 43 44 45
    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)
46 47
    if DBG:
        print("Trying to read %d bytes from %s\n" % (l, channel), file=sys.stderr)
48 49 50
    return read(channel, l)

def submit(channel, action, data):
51 52 53 54
    if channel == "std":
        h = struct.pack("<I", len(data))
    else:
        h = struct.pack("<BI", ord(action), len(data))
55
    write(channel, h)
56 57
    write(channel, data)
    time.sleep(0.1)
58

59
def main(argv):
60 61
    if DBG:
        print(argv[0], file=sys.stderr)
62 63 64 65
    script_dir = os.path.split(argv[0])[0]
    if len(script_dir ) > 0:
        os.chdir(script_dir)
    flash()
66
    print("Hello, World!")
67 68
    while(1):
        action = read('std', 1)
69 70
        if DBG:
            print("Read following action from stdin: %s\n" % action, file=sys.stderr)
71
        if action == b'c' or action == b'm' or action == b'a' or action == b'k' or action == b's' or action == b'p':
72 73
            data = obtain('std')
            submit('ser', action, data)
74

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

78 79
        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)
80
            data = obtain('ser')
81 82
            if DBG:
                print("Obtained following data from serial: %s\n" % data, file=sys.stderr)
83
            submit('std', action, data)
84 85
            if DBG:
                print("Submitted following data to stdout: %s\n" % data, file=sys.stderr)
86 87
        else:
            raise Exception("no capiche aczione %s" % (action))
88 89
                
if __name__ == "__main__":
90
    sys.exit(main(sys.argv))