Advertisement
orborbson

sortowanie kodów 433 mhz, klasa

Sep 28th, 2024 (edited)
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.05 KB | Source Code | 0 0
  1. import esp32, machine, time, math, kody
  2.  
  3. class signals:
  4.    
  5.     def __init__(self, nums, min_signals = 30, threshold = 1):
  6.         self._nums = nums[0]
  7.         self._initial_state = nums[1]
  8.         self._nums_size = len(nums[0])
  9.         self._threshold = threshold
  10.         self._min_signals = min_signals
  11.         self._split_sig = []
  12.         self._dict_sig = {}
  13.        
  14.     def _average(self):
  15.         return sum(self._nums) / self._nums_size
  16.  
  17.     def _standard_deviation(self):
  18.         avg = self._average()
  19.         variance = sum((x - avg) ** 2 for x in self._nums) / self._nums_size
  20.         return math.sqrt(variance)
  21.    
  22.     def _detect_outliers(self):
  23.         avg = self._average()
  24.         sd = self._standard_deviation()
  25.         return [index for index, var in enumerate(self._nums) if abs(var - avg) > self._threshold * sd]
  26.    
  27.     def _split_signals(self):
  28.         spikes = self._detect_outliers()
  29.         start_idx = 0
  30.         for idx in spikes:
  31.             signal_length = idx - start_idx
  32.             #print("%i, " % signal_length)
  33.             if signal_length >= self._min_signals:
  34.                 self._split_sig.append(self._nums[start_idx:idx])
  35.             start_idx = idx
  36.         if start_idx + self._min_signals < self._nums_size:
  37.             self._split_sig.append(self._nums[start_idx:])
  38.    
  39.     def _compare_lists(self, list1, list2, tolerance_in_percent = 50):
  40.         if len(list1) != len(list2):
  41.             return False
  42.         tp = tolerance_in_percent / 100
  43.         for a, b in zip(list1, list2):
  44.             if abs(a - b) > a * tp:
  45.                 return False
  46.         return True
  47.        
  48.     def _group_and_average(self):
  49.         self._split_signals()
  50.         groups = {}
  51.         group_id = 0
  52.         for i, list1 in enumerate(self._split_sig):
  53.             found_group = False
  54.             for group_key, group_indices in groups.items():
  55.                 if self._compare_lists(list1, self._split_sig[group_indices[0]]):
  56.                     groups[group_key].append(i)
  57.                     found_group = True
  58.                     break
  59.             if not found_group:
  60.                 groups[group_id] = [i]
  61.                 group_id += 1
  62.         result = {}
  63.         for group_key, group_indices in groups.items():
  64.             group_lists = [self._split_sig[i] for i in group_indices]
  65.             averages = [sum(values) // len(values) for values in zip(*group_lists)]
  66.             self._dict_sig[group_key] = averages
  67.            
  68.     def return_unique_signals(self):
  69.         self._group_and_average()
  70.         return self._dict_sig
  71.          
  72. if __name__ == "__main__":
  73.     sig_on = signals(kody.remote_code["ON"])
  74.     ret_on = sig_on.return_unique_signals()
  75.    
  76.     sig_off = signals(kody.remote_code["OFF"])
  77.     ret_off = sig_off.return_unique_signals()
  78.    
  79.     transmit = esp32.RMT(0, pin = machine.Pin(23, machine.Pin.OUT), clock_div = 80, idle_level = False)
  80.    
  81.     print("Wyłączam przekaźnik ...")
  82.     transmit.write_pulses(kody.remote_code["OFF"][0], kody.remote_code["OFF"][1])
  83.     time.sleep(1)
  84.    
  85.     on_dobry = off_dobry = None
  86.    
  87.     print("\n<<< Testuję kody WŁączające przekaźnik >>>")
  88.     time.sleep(4)
  89.    
  90.     for i in ret_on:
  91.         print("\n[ON]: testuję kod nr %i ... " % i, end = "")
  92.         time.sleep(1)
  93.         transmit.write_pulses(ret_on[i] * 4, False)
  94.         time.sleep(1)
  95.         if input("czy WŁĄCZYŁ przekaźnik? (t/n): ") == "t":
  96.             on_dobry = i
  97.             break
  98.    
  99.     print("Włączam przekaźnik ...")
  100.     transmit.write_pulses(kody.remote_code["ON"][0], kody.remote_code["ON"][1])
  101.     print("\n<<< Testuję kody WYŁączające przekaźnik >>>")
  102.     time.sleep(4)
  103.    
  104.     for i in ret_off:
  105.         print("\n[OFF]: testuję kod nr %i ... " % i, end = "")
  106.         time.sleep(1)
  107.         transmit.write_pulses(ret_off[i] * 4, False)
  108.         time.sleep(1)
  109.         if input("czy WYŁĄCZYŁ przekaźnik (t/n): ") == "t":
  110.             off_dobry = i
  111.             break
  112.    
  113.     print("\nON: %r\nOFF: %r" % (ret_on[on_dobry], ret_off[off_dobry]))
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement