|
| 1 | +#!/usr/bin/python3 |
| 2 | +"""Automaticly get data from ESP8266's Raw data output and calculating average time for zero, one and space""" |
| 3 | +# |
| 4 | +# Copyright 2024 Andrey Kravchenko (StellaLupus) |
| 5 | +import serial |
| 6 | +import serial.tools |
| 7 | +import serial.tools.list_ports |
| 8 | + |
| 9 | + |
| 10 | +def get_port(): |
| 11 | + """Allow user to select com port to connect device""" |
| 12 | + ports = sorted(serial.tools.list_ports.comports()) |
| 13 | + print("Available ports to listing:") |
| 14 | + for id, port_info in enumerate(ports): |
| 15 | + print("{}. - {}: {} [{}]".format(id, port_info.device, |
| 16 | + port_info.description, |
| 17 | + port_info.hwid)) |
| 18 | + print("Select port: ", end="") |
| 19 | + select_id = int(input()) |
| 20 | + if select_id < len(ports) and select_id >= 0: |
| 21 | + return ports[select_id] |
| 22 | + else: |
| 23 | + print("Unrecognized port number") |
| 24 | + return get_port() |
| 25 | + |
| 26 | + |
| 27 | +ZERO_T = 756 |
| 28 | +ONE_T = 2149 |
| 29 | +SPACE_T = 752 |
| 30 | +PRECISION = 0.25 |
| 31 | + |
| 32 | + |
| 33 | +def get_bit_from_interv(value: int): |
| 34 | + """Returning zero or one from value or 2 if not recognized""" |
| 35 | + if value > ZERO_T - ZERO_T * PRECISION and value < ZERO_T + ZERO_T * PRECISION: |
| 36 | + return 0 |
| 37 | + elif value > ONE_T - ONE_T * PRECISION and value < ONE_T + ONE_T * PRECISION: |
| 38 | + return 1 |
| 39 | + else: |
| 40 | + return 2 |
| 41 | + |
| 42 | + |
| 43 | +def bit_list_to_int(bit_list): |
| 44 | + """Convert list of bits to int""" |
| 45 | + out = 0 |
| 46 | + for bit in bit_list: |
| 47 | + out = (out << 1) | bit |
| 48 | + return out |
| 49 | + |
| 50 | + |
| 51 | +def main(): |
| 52 | + """Main method""" |
| 53 | + port = get_port() |
| 54 | + print("Selected port:" + port.device) |
| 55 | + |
| 56 | + ser = serial.Serial( |
| 57 | + port=port.device, |
| 58 | + baudrate=115200, |
| 59 | + ) |
| 60 | + |
| 61 | + ZERO_SUM = 0 |
| 62 | + ZERO_COUNT = 0 |
| 63 | + ONE_SUM = 0 |
| 64 | + ONE_CNT = 0 |
| 65 | + SPACE_SUM = 0 |
| 66 | + SPACE_CNT = 0 |
| 67 | + |
| 68 | + while True: |
| 69 | + try: |
| 70 | + data_str = ser.readline().decode() |
| 71 | + except: |
| 72 | + continue |
| 73 | + if "uint16_t rawData" in data_str: |
| 74 | + data_str_array = str(data_str[data_str.index("{") + |
| 75 | + 1:data_str.index("}")]).split(",") |
| 76 | + data = [int(i.strip()) for i in data_str_array] |
| 77 | + data = data[2:] |
| 78 | + clear_data = [i for idi, i in enumerate(data) if idi % 2 == 1] |
| 79 | + bit_data = [get_bit_from_interv(i) for i in data] |
| 80 | + clear_bit_data = [get_bit_from_interv(i) for i in clear_data] |
| 81 | + |
| 82 | + for idd, d in enumerate(data): |
| 83 | + if idd % 2 == 0: |
| 84 | + SPACE_SUM += d |
| 85 | + SPACE_CNT += 1 |
| 86 | + else: |
| 87 | + if get_bit_from_interv(d) == 0: |
| 88 | + ZERO_SUM += d |
| 89 | + ZERO_COUNT += 1 |
| 90 | + elif get_bit_from_interv(d) == 1: |
| 91 | + ONE_SUM += d |
| 92 | + ONE_CNT += 1 |
| 93 | + |
| 94 | + print("Bit data = " + "".join([str(i) for i in bit_data])) |
| 95 | + print("Clear bit data = " + |
| 96 | + "".join([str(i) for i in clear_bit_data])) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + main() |
0 commit comments