1 #!/usr/bin/python3
3 import sys
4 import re
6 # Convert DMT pdf to txt:
7 # pdftotext -layout -f 18 -l 105 DMTr1\ v13.pdf DMT.txt
9 # Path to the text file
10 filepath = sys.argv[1]
12 m = {}
13 line = ""
15 def parsei(key, regex, base=10):
16 global m
17 global line
19 match = re.search(regex, line)
20 if match != None:
21 m[key] = int(match.group(1), base)
23 def parsef(key, regex, base=10):
24 global m
25 global line
27 match = re.search(regex, line)
28 if match != None:
29 m[key] = float(match.group(1))
31 for line in open(filepath, 'r'):
32 # each page starts with this
33 if "VESA MONITOR TIMING STANDARD" in line:
34 m = { }
36 # each page ends with this
37 if "VESA Display Monitor Timing Standard" in line:
38 print("// {:#x} - {}".format(m["dmt_id"], m["name"]))
40 flags = []
41 if m["ilace"]:
42 flags += [ "DRM_MODE_FLAG_INTERLACE" ]
44 if m["hsp"]:
45 flags += [ "DRM_MODE_FLAG_PHSYNC" ]
46 else:
47 flags += [ "DRM_MODE_FLAG_NHSYNC" ]
49 if m["vsp"]:
50 flags += [ "DRM_MODE_FLAG_PVSYNC" ]
51 else:
52 flags += [ "DRM_MODE_FLAG_NVSYNC" ]
54 print("DRM_MODE(\"{}\", {}, {}, {}, {}, {}, {}, {}, {}, {}, {}),".format(
55 m["name"],
56 int(m["pclk"] * 1000),
57 m["hact"], m["hfp"], m["hsw"], m["hbp"],
58 m["vact"], m["vfp"], m["vsw"], m["vbp"],
59 " | ".join(flags)
60 ))
62 match = re.search("Timing Name\s+=\s+([^;]+)", line)
63 if match != None:
64 m["name"] = str.strip(match.group(1))
66 parsei("dmt_id", "EDID ID:\s+DMT ID: ([0-9A-Fa-f]+)h", 16)
67 parsef("pclk", "Pixel Clock\s+=\s+(\d+\.\d+)")
69 parsei("hact", "Hor Pixels\s+=\s+(\d+)")
70 parsei("hfp", "H Front Porch.*\s(\d+) Pixels")
71 parsei("hsw", "Hor Sync Time.*\s(\d+) Pixels")
72 parsei("hbp", "H Back Porch.*\s(\d+) Pixels")
74 parsei("vact", "Ver Pixels\s+=\s+(\d+)")
75 parsei("vfp", "V Front Porch.*\s(\d+)\s+lines")
76 parsei("vsw", "Ver Sync Time.*\s(\d+)\s+lines")
77 parsei("vbp", "V Back Porch.*\s(\d+)\s+lines")
79 match = re.search("Scan Type\s+=\s+(\w+);", line)
80 if match != None:
81 if match.group(1) == "NONINTERLACED":
82 m["ilace"] = False
83 elif match.group(1) == "INTERLACED":
84 m["ilace"] = True
85 else:
86 print("Bad scan type")
87 exit(-1)
89 match = re.search("Hor Sync Polarity\s+=\s+(\w+)", line)
90 if match != None:
91 if match.group(1) == "POSITIVE":
92 m["hsp"] = True
93 elif match.group(1) == "NEGATIVE":
94 m["hsp"] = False
95 else:
96 print("Bad hsync polarity")
97 exit(-1)
99 match = re.search("Ver Sync Polarity\s+=\s+(\w+)", line)
100 if match != None:
101 if match.group(1) == "POSITIVE":
102 m["vsp"] = True
103 elif match.group(1) == "NEGATIVE":
104 m["vsp"] = False
105 else:
106 print("Bad vsync polarity")
107 exit(-1)