Last active
December 20, 2019 22:20
-
-
Save tedyapo/bd92df6dc3712386ef2aa6f3b8906ccc to your computer and use it in GitHub Desktop.
Tektronix 11801 Screen Capture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import re | |
import datetime | |
from PIL import Image | |
import serial | |
import subprocess | |
device = '/dev/ttyUSB0' | |
speed = 19200 | |
colors = [ | |
bytes.fromhex('000000'), | |
bytes.fromhex('ffffff'), | |
bytes.fromhex('00ff00'), | |
bytes.fromhex('00ffff'), | |
bytes.fromhex('ff00ff'), | |
bytes.fromhex('0000ff'), | |
bytes.fromhex('555555'), | |
bytes.fromhex('ff0000') | |
] | |
def parse11801Stream(stream): | |
data = bytearray() | |
title = stream.readline() | |
m = re.search(b'date: (..-...-..) time: (..:..:..)', title) | |
if not m: | |
return | |
timestamp = datetime.datetime.strptime(m.group(1).decode().strip() + 'T' + | |
m.group(2).decode().strip(), | |
'%d-%b-%yT%H:%M:%S') | |
filename = timestamp.isoformat() + '.png' | |
print('decoding : ' + filename) | |
cols = int(stream.readline().decode('utf-8')) | |
rows = int(stream.readline().decode('utf-8')) | |
byte = stream.read(1) | |
byte = stream.read(1) | |
while byte: | |
val = int.from_bytes(byte, 'little') | |
if (val & 0xc0) == 0x40: | |
count = 1 | |
if (val & 0xc0) == 0x80: | |
count = 2 | |
if (val & 0xc0) == 0xc0: | |
count = 3 | |
if (val & 0xc0) == 0x00: | |
byte = stream.read(1) | |
val2 = int.from_bytes(byte, 'little') | |
if val2 >= 4: | |
count = val2 | |
else: | |
byte = stream.read(1) | |
val3 = int.from_bytes(byte, 'little') | |
count = (val2<<8) | val3 | |
for i in range(count): | |
data += colors[val & 0x7] | |
data += colors[(val & 0x38)>>3] | |
if 3*rows*cols == len(data): | |
break | |
byte = stream.read(1) | |
if 3*rows*cols != len(data): | |
print(title, | |
'length mismatch({:d} != {:d})'.format(3*rows*cols, | |
len(data))) | |
try: | |
image = Image.frombytes('RGB', (cols, rows), bytes(data)) | |
image = image.transpose(Image.ROTATE_90) | |
print('writing ' + filename) | |
image.save(filename) | |
print('transferring ' + filename) | |
subprocess.Popen(['scp', './' + filename, | |
'tyapo@192.168.1.14:Documents/11801_images/']).wait() | |
except: | |
pass | |
port = serial.Serial(device, speed) | |
while True: | |
parse11801Stream(port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment