Last active
March 30, 2017 16:52
-
-
Save jsjolund/69fe7eef3a59c139fc9546fbbf20889a to your computer and use it in GitHub Desktop.
Hantek DSO5000P oscilloscope screenshot using python PIL
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 python | |
from PIL import Image | |
import array | |
import usb | |
import time | |
png_path = "/tmp/" | |
png_name = "DSO_Screenshot_" + time.strftime('%Y-%m-%d_%H-%M-%S') + ".png" | |
dev = usb.core.find(idVendor=0x049f, idProduct=0x505a) | |
if dev is None: | |
print("Hantek DSO5000P not found. Did you connect it via USB?") | |
exit() | |
interface = 0 | |
cfg = dev.get_active_configuration() | |
intf = cfg[(0, 0)] | |
outbound = intf[1] # 0x81 | |
inbound = intf[0] # 0x2 | |
if dev.is_kernel_driver_active(interface): | |
dev.detach_kernel_driver(interface) | |
usb.util.claim_interface(dev, interface) | |
cmd = [0x53, 0x02, 0x00, 0x20] # screenshot cmd | |
checksum = 0x75 | |
dev.write(inbound.bEndpointAddress, cmd + [checksum]) | |
num_pkts = 0 | |
total_data_size = 0 | |
rgb565 = array.array('B', [0]) | |
try: | |
while True: | |
pkt = array.array('B', [0]) * 0xF000 | |
len_read = dev.read(outbound.bEndpointAddress, pkt) | |
pkt_marker = pkt[0] | |
pkt_len = pkt[1] + (pkt[2] << 8) | |
pkt_cmd = pkt[3] | |
pkt_subcmd = pkt[4] | |
pkt_data = pkt[5:len_read - 1] | |
pkt_chksum = pkt[len_read - 1] | |
num_pkts += 1 | |
# print('mark=0x{:02X} len={} cmd=0x{:02X} subcmd=0x{:02X} chksum=0x{:02X}'.format(pkt_marker, pkt_len, pkt_cmd, pkt_subcmd, pkt_chksum)) | |
if pkt_subcmd == 0x02: | |
break | |
if pkt_cmd == 0xA0: | |
rgb565 += pkt_data | |
except usb.core.USBError as e: | |
print(e.args) | |
usb.util.release_interface(dev, interface) | |
print("received " + str(num_pkts) + " packets, " + str(len(rgb565)) + " bytes image data") | |
size = (800, 480) | |
img = Image.new("RGB", size) | |
for i in range(0, 768000, 2): | |
# convert rgb565 to rgb888 | |
color = (rgb565[i] << 8) | rgb565[i + 1] | |
r = ((((color >> 11) & 0x1F) * 527) + 23) >> 6 | |
g = ((((color >> 5) & 0x3F) * 259) + 33) >> 6 | |
b = (((color & 0x1F) * 527) + 23) >> 6 | |
j = int(i/2) | |
x = int(j % size[0]) | |
y = int((j - x) / size[0]) | |
x = size[0] - 1 if x == 0 else x - 1 # weird offset | |
img.putpixel((x, y), (r, g, b)) | |
img.show() | |
img.save(png_path + png_name) | |
print("saved to " + png_path + png_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment