Created
July 28, 2022 18:15
-
-
Save paucoma/57080d2845ba4b21b980b90842c38eb1 to your computer and use it in GitHub Desktop.
Python Script to convert Flipper RAW .sub file to a Basic VCD Value Change Dump format
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 | |
# Flipper RAW .sub format | |
# | |
# Having a look at some of the code in lib/subghz/protocols/raw.c | |
# - Decoded data is stored as a line starting with the text "Raw_Data: " followed by space delimited integers. | |
# - Each line stores a maximum number of integers limited to SUBGHZ_DOWNLOAD_MAX_SIZE ,defined as 512. Then a new line is written. | |
# - The sign of each integer represents a decoded signal logic level, positive / negative , logic level 1 / 0 | |
# - The absolute value of each integer represents the signal level duration. I am guessing this is in micro-seconds | |
# From a captured Flipper RAW .sub file to a Basic VCD Value Change Dump | |
# this format can then be opened and viewed in | |
# - GTKWave : http://gtkwave.sourceforge.net/ | |
# - Sigroks PulseView : https://sigrok.org/wiki/PulseView#Download | |
# | |
# Usage : ./sub2vcd.py input.sub > output.vcd | |
# You can "trim" decoded section by modifying starttime and endtime variables they are in micro-seconds | |
# | |
# Inspired by jinscho's gist : | |
# https://gist.github.com/jinschoi/8396f25a4cb7ac7986a7d881026ae950 | |
# 2022/07/28 : Initial version with plenty of room for improvement | |
import re | |
import sys | |
import math | |
filename = sys.argv[1] | |
starttime = 0 | |
endtime = -1 | |
myaccutime = 0 | |
myiv = -1 | |
def initwrite(ival): | |
print("$date\r\n" | |
" Date text. For example: November 11, 2009.\r\n" | |
"$end\r\n" | |
"$version\r\n" | |
" VCD generator tool version info text.\r\n" | |
"$end\r\n" | |
"$comment\r\n" | |
" Any comment text.\r\n" | |
"$end\r\n" | |
"$timescale 1 us $end\r\n" | |
"$scope module logic $end\r\n" | |
"$var wire 1 * rawdata $end\r\n" | |
"$upscope $end\r\n" | |
"$enddefinitions $end\r\n" | |
"$dumpvars\r\n" | |
"x*\r\n" | |
"$end\r\n" | |
"#0") | |
def addtime(seg): | |
global myaccutime | |
myaccutime += abs(seg) | |
return myaccutime | |
segs = [] | |
with open(filename, 'r') as f: | |
for line in f: | |
m = re.match(r'RAW_Data:\s*([-0-9 ]+)\s*$', line) | |
if m: | |
mysegs = m[1].split(r' ') | |
if (myiv == -1): | |
myiv = 0 if int(mysegs[0])<0 else 1 | |
if endtime > 0: | |
myiv = abs(myiv-1) | |
initwrite(myiv) | |
for seg in mysegs: | |
myte = addtime(int(seg)) | |
if endtime > 0: | |
if myaccutime < endtime and myaccutime > starttime: | |
segs.extend([myte-starttime]) | |
else: | |
myiv = abs(myiv-1) | |
else: | |
segs.extend([myte]) | |
if m and endtime > 0: | |
segs.extend([endtime-starttime]) | |
full = [] | |
for seg in segs: | |
if myiv == 1: | |
full.append('1*\r\n') | |
myiv = 0 | |
else: | |
full.append('0*\r\n') | |
myiv = 1 | |
full.append('#'+str(seg)+'\r\n') | |
full = ''.join(full) | |
print(full) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Attempting to run this with Python3, kicks the following error:
user@host sub2vcd % ./sub2vcd.py sample.sub > out.sub
Traceback (most recent call last):
File "./sub2vcd.py", line 66, in
mysegs = m[1].split(r' ')
TypeError: '_sre.SRE_Match' object has no attribute 'getitem'
Sorry regex is not my forte.