Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import esp32, machine, time, math, kody
- class signals:
- def __init__(self, nums, min_signals = 30, threshold = 1):
- self._nums = nums[0]
- self._initial_state = nums[1]
- self._nums_size = len(nums[0])
- self._threshold = threshold
- self._min_signals = min_signals
- self._split_sig = []
- self._dict_sig = {}
- def _average(self):
- return sum(self._nums) / self._nums_size
- def _standard_deviation(self):
- avg = self._average()
- variance = sum((x - avg) ** 2 for x in self._nums) / self._nums_size
- return math.sqrt(variance)
- def _detect_outliers(self):
- avg = self._average()
- sd = self._standard_deviation()
- return [index for index, var in enumerate(self._nums) if abs(var - avg) > self._threshold * sd]
- def _split_signals(self):
- spikes = self._detect_outliers()
- start_idx = 0
- for idx in spikes:
- signal_length = idx - start_idx
- #print("%i, " % signal_length)
- if signal_length >= self._min_signals:
- self._split_sig.append(self._nums[start_idx:idx])
- start_idx = idx
- if start_idx + self._min_signals < self._nums_size:
- self._split_sig.append(self._nums[start_idx:])
- def _compare_lists(self, list1, list2, tolerance_in_percent = 50):
- if len(list1) != len(list2):
- return False
- tp = tolerance_in_percent / 100
- for a, b in zip(list1, list2):
- if abs(a - b) > a * tp:
- return False
- return True
- def _group_and_average(self):
- self._split_signals()
- groups = {}
- group_id = 0
- for i, list1 in enumerate(self._split_sig):
- found_group = False
- for group_key, group_indices in groups.items():
- if self._compare_lists(list1, self._split_sig[group_indices[0]]):
- groups[group_key].append(i)
- found_group = True
- break
- if not found_group:
- groups[group_id] = [i]
- group_id += 1
- result = {}
- for group_key, group_indices in groups.items():
- group_lists = [self._split_sig[i] for i in group_indices]
- averages = [sum(values) // len(values) for values in zip(*group_lists)]
- self._dict_sig[group_key] = averages
- def return_unique_signals(self):
- self._group_and_average()
- return self._dict_sig
- if __name__ == "__main__":
- sig_on = signals(kody.remote_code["ON"])
- ret_on = sig_on.return_unique_signals()
- sig_off = signals(kody.remote_code["OFF"])
- ret_off = sig_off.return_unique_signals()
- transmit = esp32.RMT(0, pin = machine.Pin(23, machine.Pin.OUT), clock_div = 80, idle_level = False)
- print("Wyłączam przekaźnik ...")
- transmit.write_pulses(kody.remote_code["OFF"][0], kody.remote_code["OFF"][1])
- time.sleep(1)
- on_dobry = off_dobry = None
- print("\n<<< Testuję kody WŁączające przekaźnik >>>")
- time.sleep(4)
- for i in ret_on:
- print("\n[ON]: testuję kod nr %i ... " % i, end = "")
- time.sleep(1)
- transmit.write_pulses(ret_on[i] * 4, False)
- time.sleep(1)
- if input("czy WŁĄCZYŁ przekaźnik? (t/n): ") == "t":
- on_dobry = i
- break
- print("Włączam przekaźnik ...")
- transmit.write_pulses(kody.remote_code["ON"][0], kody.remote_code["ON"][1])
- print("\n<<< Testuję kody WYŁączające przekaźnik >>>")
- time.sleep(4)
- for i in ret_off:
- print("\n[OFF]: testuję kod nr %i ... " % i, end = "")
- time.sleep(1)
- transmit.write_pulses(ret_off[i] * 4, False)
- time.sleep(1)
- if input("czy WYŁĄCZYŁ przekaźnik (t/n): ") == "t":
- off_dobry = i
- break
- print("\nON: %r\nOFF: %r" % (ret_on[on_dobry], ret_off[off_dobry]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement