Skip to content

Commit f5bd13f

Browse files
committed
Some code simplification
1 parent 0d1e527 commit f5bd13f

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

adafruit_emc2101/emc2101_lut.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ def __repr__(self):
127127
def __str__(self):
128128
"""return the official string representation of the LUT"""
129129
value_strs = []
130-
lut_keys = list(self.lut_values.keys())
131-
lut_keys.sort()
130+
lut_keys = list(sorted(self.lut_values.keys()))
132131
for temp in lut_keys:
133132
fan_drive = self.lut_values[temp]
134133
value_strs.append("%d deg C => %.1f%% duty cycle" % (temp, fan_drive))
@@ -143,31 +142,26 @@ def __len__(self):
143142
# temperature first
144143

145144
def _set_lut(self, lut_dict):
146-
lut_keys = list(lut_dict.keys())
147-
lut_size = len(lut_dict)
148145
# Make sure we're not going to try to set more entries than we have slots
149-
if lut_size > 8:
146+
if len(lut_dict) > 8:
150147
raise AttributeError("LUT can only contain a maximum of 8 items")
151148

152-
# we want to assign the lowest temperature to the lowest LUT slot, so we sort the keys/temps
153-
for k in lut_keys:
154-
# Verify that the value is a correct amount
155-
lut_value = lut_dict[k]
156-
if lut_value > 100.0 or lut_value < 0:
149+
# Verify that the value is a correct amount
150+
for speed in lut_dict.values():
151+
if speed > 100.0 or speed < 0:
157152
raise AttributeError("LUT values must be a fan speed from 0-100%")
158153

159-
# add the current temp/speed to our internal representation
160-
self.lut_values[k] = lut_value
154+
# Copy to our internal representation
155+
self.lut_values = {k: v for k, v in lut_dict.items()}
161156
current_mode = self.emc_fan.lut_enabled
162157

163158
# Disable the lut to allow it to be updated
164159
self.emc_fan.lut_enabled = False
165160

161+
# we want to assign the lowest temperature to the lowest LUT slot, so we sort the keys/temps
166162
# get and sort the new lut keys so that we can assign them in order
167-
lut_keys = list(self.lut_values.keys())
168-
lut_keys.sort()
169-
for idx in range(lut_size):
170-
current_temp = lut_keys[idx]
163+
flat_list = []
164+
for idx, current_temp in enumerate(sorted(self.lut_values.keys())):
171165
current_speed = _speed_to_lsb(self.lut_values[current_temp])
172166
getattr(self, "_fan_lut_t%d" % (idx + 1)).__set__(self, current_temp)
173167
getattr(self, "_fan_lut_s%d" % (idx + 1)).__set__(self, current_speed)
@@ -176,7 +170,7 @@ def _set_lut(self, lut_dict):
176170
# self.emc_fan._lut_speed_setters[idx].__set__(self.emc_fan, current_speed)
177171

178172
# Set the remaining LUT entries to the default (Temp/Speed = max value)
179-
for idx in range(8)[lut_size:]:
173+
for idx in range(8)[len(self.lut_values):]:
180174
getattr(self, "_fan_lut_t%d" % (idx + 1)).__set__(self, MAX_LUT_TEMP)
181175
getattr(self, "_fan_lut_s%d" % (idx + 1)).__set__(self, MAX_LUT_SPEED)
182176
self.emc_fan.lut_enabled = current_mode

0 commit comments

Comments
 (0)