Skip to content

Commit b3f2b9a

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 e213d17 commit b3f2b9a

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
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

@@ -886,32 +890,48 @@ def _build_highlight(self, ref: str, info: PlacedComponentInfo,
886890
self._plotter.append_highlight_element(h)
887891

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

910+
print(ref, res, tolerance)
911+
912+
# if more than 2%, then a 4 color band. otherwise a 5 color band
913+
res_orig = res
896914
if float(tolerance[:-1]) > 2:
897915
power = math.floor(res.log10())
898-
if res >= 10:
899-
power -= 1
900916
res = Decimal(int(float(res) / 10 ** power))
917+
if res < 10:
918+
power -= 1
919+
res = Decimal(int(float(res_orig) / 10 ** power))
901920
res = "{:02f}".format(res)
902921
resistor_colors = [
903922
self._plotter.get_style("tht-resistor-band-colors", int(str(res)[0])),
904923
self._plotter.get_style("tht-resistor-band-colors", int(str(res)[1])),
905924
self._plotter.get_style("tht-resistor-band-colors", int(power))
906925
]
907-
r_band = '4'
926+
r_band = '' # the svg doesn't have a prefix to keep backwards compatibility
908927
else:
909928
power = math.floor(res.log10())
910-
if res >= 100:
929+
res = Decimal(int(float(res) / 10 ** power))
930+
if res < 10:
911931
power -= 2
912-
elif res >= 10:
932+
elif res < 100:
913933
power -= 1
914-
res = Decimal(int(float(res) / 10 ** power))
934+
res = int(float(res_orig / Decimal(10 ** power)))
915935
res = "{:03f}".format(res)
916936
resistor_colors = [
917937
self._plotter.get_style("tht-resistor-band-colors", int(str(res)[0])),
@@ -921,6 +941,9 @@ def _apply_resistor_code(self, root: etree.Element, id_prefix: str, ref: str, va
921941
]
922942
r_band = '5'
923943

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

@@ -948,6 +971,7 @@ def _apply_resistor_code(self, root: etree.Element, id_prefix: str, ref: str, va
948971
def _get_resistance_from_value(self, value: str, properties: Dict[str, str]) -> Tuple[Decimal, str]:
949972
res, tolerance = None, "5%"
950973
value_l = value.split(" ", maxsplit=1)
974+
print(value)
951975
try:
952976
res = read_resistance(value_l[0])
953977
except ValueError:
@@ -974,7 +998,7 @@ def _get_resistance_from_value(self, value: str, properties: Dict[str, str]) ->
974998
if not isinstance(s, dict):
975999
raise RuntimeError(f"Invalid style specified, tht-resistor-band-colors should be dictionary, got {type(s)}")
9761000
if t_string.strip() not in s:
977-
raise UserWarning(f"Invalid resistor tolerance {value_l[1]}")
1001+
raise UserWarning(f"Tolerance does not exist in style: {t_string}")
9781002
tolerance = t_string
9791003

9801004
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

0 commit comments

Comments
 (0)