Skip to content

Commit

Permalink
Merge pull request #25 from Fish-in-a-Barrel/gen_values
Browse files Browse the repository at this point in the history
Added methods to quickly generate common series
  • Loading branch information
Finomnis authored Mar 18, 2024
2 parents e36336c + a062d6d commit cde1afc
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions LabelGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,57 @@ def render_outlines(c: Canvas, layout: PaperConfig) -> None:
c.roundRect(rect.left, rect.bottom, rect.width, rect.height, rect.corner)


# Resistor values typically come from an "E series of preferred numbers". This is why apparently
# random values such as 22, 39, or 47 are common, but some round numbers like 25, 40, or 50
# are rarely seen. 10% resistors usually come from the E12 series, while 5% resistors usually
# come from an abridged version of the E24 series.
#
# The list constants below can be used with the generate_values function to quickly create sets of
# common resistor values.
E12_VALUES = [1.0, 1.2, 1.5, 1.8, 2.2, 2.7, 3.3, 3.9, 4.7, 5.6, 6.8, 8.2]
E24_COMMON_VALUES = [1.0, 1.2, 1.5, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0,
3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1]
E24_ALL_VALUES = [1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0,
3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1]


# Scales a list of values by a power of 10. The list may be one of the E constants above, or your
# own list of values.
#
# Examples:
# scale_values([1.0, 4.7, 7.5], 2) -> [100, 470, 750]
#
# scale_values(E12_VALUES, 1) -> [ 10, 12, 15, 18, 22, 27, 33, 39, 47, 56, 68, 82 ]
def scale_values(
series: List[float], # the base series of resistor values
power: int # the power of 10 to scale the series by
) -> List[float]:
scalar = 10 ** power
return [scalar * x for x in series]


# Expands a list of values into several lists, each scaled by increasing powers of 10. The list may
# be one of the E constants above, or your own list of values.
#
# Examples:
# generate_values([1.0, 4.7, 7.5], 0, 2) -> [
# [ 1.0, 4.7, 7.5 ], # 10 ** 0 -> x1
# [ 10, 47, 75 ], # 10 ** 1 -> x10
# [ 100, 470, 750 ] # 10 ** 2 -> x100
# ]
#
# generate_values(E12_VALUES, 1, 2) -> [
# [ 10, 12, 15, 18, 22, 27, 33, 39, 47, 56, 68, 82 ], # 10 ** 1 -> x10
# [ 100, 120, 150, 180, 220, 270, 330, 390, 470, 560, 680, 820 ] # 10 ** 2 -> x100
# ]
def generate_values(
series: List[float], # the base series of resistor values
first_power: int, # the first power of 10 to scale the series by
last_power: int # the last power of 10 to scale the series by
) -> List[List[float]]:
return [scale_values(series, x) for x in range(first_power, last_power)]


def main() -> None:

# ############################################################################
Expand Down Expand Up @@ -632,6 +683,12 @@ def main() -> None:
[9100000000, 9200000000, 3300000000],
]

# ############################################################################
# Alternatively, a set of common resistor values can be generated by the
# generate_values function.
# ############################################################################
# resistor_values: ResistorList = [ 0, 0.01 ] + generate_values(E24_COMMON_VALUES, 0, 6)

# ############################################################################
# Further configuration options
#
Expand Down

0 comments on commit cde1afc

Please sign in to comment.