Skip to content

Commit 5a30b85

Browse files
committed
Fixed resistor band values set, and updated for latest model
Moved around resistor tolerance values, added 0.01% and 0.02% Fix bug where a value of `R` alone would count as 0 ohms Fixed lxml to ==4.9.4, as the newest 5.0.0 version breaks this tool
1 parent 3682bee commit 5a30b85

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

pcbdraw/plot.py

+36-12
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"highlight-padding": 1.5,
5858
"highlight-offset": 0,
5959
"tht-resistor-band-colors": {
60+
-3: '#d8a0a6',
6061
-2: '#d9d9d9',
6162
-1: '#ffc800',
6263
0: '#000000',
@@ -69,14 +70,17 @@
6970
7: '#cc00cc',
7071
8: '#666666',
7172
9: '#cccccc',
73+
# resistor tolerances
74+
'10%': '#d9d9d9',
75+
'5%': '#ffc800',
7276
'1%': '#805500',
7377
'2%': '#ff0000',
78+
'0.05%': '#ff8000',
79+
'0.02%': '#ffff00',
7480
'0.5%': '#00cc11',
7581
'0.25%': '#0000cc',
7682
'0.1%': '#cc00cc',
77-
'0.05%': '#666666',
78-
'5%': '#ffc800',
79-
'10%': '#d9d9d9',
83+
'0.01%': '#666666',
8084
}
8185
}
8286

@@ -884,32 +888,48 @@ def _build_highlight(self, ref: str, info: PlacedComponentInfo,
884888
self._plotter.append_highlight_element(h)
885889

886890
def _apply_resistor_code(self, root: etree.Element, id_prefix: str, ref: str, value: str, properties: Dict[str, str]) -> None:
887-
if root.find(f".//*[@id='{id_prefix}res_4band1']") is None:
891+
if root.find(f".//*[@id='{id_prefix}res_band1']") is None:
888892
return
889893
try:
890894
res, tolerance = self._get_resistance_from_value(value, properties)
895+
if res == 0: # if exactly 0, un-hide the zero band mark
896+
band = root.find(f".//*[@id='{id_prefix}res_zeroband']")
897+
s = band.attrib["style"].split(";")
898+
for i in range(len(s)):
899+
if s[i].startswith('display:'):
900+
s_split = s[i].split(':')
901+
s_split[1] = 'inline'
902+
s[i] = ':'.join(s_split)
903+
band.attrib["style"] = ";".join(s)
904+
return
891905
if res < 0.001:
892-
raise UserWarning("resistance too small to represent")
906+
raise UserWarning(f"resistance too small to represent ({res})")
893907

908+
print(ref, res, tolerance)
909+
910+
# if more than 2%, then a 4 color band. otherwise a 5 color band
911+
res_orig = res
894912
if float(tolerance[:-1]) > 2:
895913
power = math.floor(res.log10())
896-
if res >= 10:
897-
power -= 1
898914
res = Decimal(int(float(res) / 10 ** power))
915+
if res < 10:
916+
power -= 1
917+
res = Decimal(int(float(res_orig) / 10 ** power))
899918
res = "{:02f}".format(res)
900919
resistor_colors = [
901920
self._plotter.get_style("tht-resistor-band-colors", int(str(res)[0])),
902921
self._plotter.get_style("tht-resistor-band-colors", int(str(res)[1])),
903922
self._plotter.get_style("tht-resistor-band-colors", int(power))
904923
]
905-
r_band = '4'
924+
r_band = '' # the svg doesn't have a prefix to keep backwards compatibility
906925
else:
907926
power = math.floor(res.log10())
908-
if res >= 100:
927+
res = Decimal(int(float(res) / 10 ** power))
928+
if res < 10:
909929
power -= 2
910-
elif res >= 10:
930+
elif res < 100:
911931
power -= 1
912-
res = Decimal(int(float(res) / 10 ** power))
932+
res = int(float(res_orig / Decimal(10 ** power)))
913933
res = "{:03f}".format(res)
914934
resistor_colors = [
915935
self._plotter.get_style("tht-resistor-band-colors", int(str(res)[0])),
@@ -919,6 +939,9 @@ def _apply_resistor_code(self, root: etree.Element, id_prefix: str, ref: str, va
919939
]
920940
r_band = '5'
921941

942+
# safety check, to ensure the new resistor times power is the same as a truncated version of the original
943+
assert math.isclose(float(res_orig), float(res)*10**power, rel_tol=10**power)
944+
922945
if tolerance != '20%':
923946
resistor_colors += [self._plotter.get_style("tht-resistor-band-colors", tolerance)]
924947

@@ -946,6 +969,7 @@ def _apply_resistor_code(self, root: etree.Element, id_prefix: str, ref: str, va
946969
def _get_resistance_from_value(self, value: str, properties: Dict[str, str]) -> Tuple[Decimal, str]:
947970
res, tolerance = None, "5%"
948971
value_l = value.split(" ", maxsplit=1)
972+
print(value)
949973
try:
950974
res = read_resistance(value_l[0])
951975
except ValueError:
@@ -972,7 +996,7 @@ def _get_resistance_from_value(self, value: str, properties: Dict[str, str]) ->
972996
if not isinstance(s, dict):
973997
raise RuntimeError(f"Invalid style specified, tht-resistor-band-colors should be dictionary, got {type(s)}")
974998
if t_string.strip() not in s:
975-
raise UserWarning(f"Invalid resistor tolerance {value_l[1]}")
999+
raise UserWarning(f"Tolerance does not exist in style: {t_string}")
9761000
tolerance = t_string
9771001

9781002
return res, tolerance

pcbdraw/unit.py

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def read_resistance(value: str) -> Decimal:
3636
# Then each gets multiplied by the factor and added, so 4000 + 700
3737
# This method ensures that 4k7 and 4k700 for example yields the same result
3838
split = p_value.split(prefix)
39+
if len(split[0]) == 0 and len(split[1]) == 0:
40+
raise UserWarning("Empty resistance value after splitting by prefix")
3941
n_whole = Decimal(split[0]) if split[0] != "" else Decimal(0)
4042
n_dec = Decimal('.'+split[1]) if split[1] != "" else Decimal(0)
4143
numerical_value = n_whole * table + n_dec * table

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
],
2626
install_requires=[
2727
"numpy",
28-
"lxml",
28+
"lxml==4.9.4",
2929
"mistune>=2.0.2, <=2.0.5",
3030
"pybars3",
3131
"pyyaml",

0 commit comments

Comments
 (0)