a b/python/maths2.py
1
import struct
2
3
values = "153 16 6 126 203 125 175 125 210 128 196 125 176 125 198 125 29 0 149 115 206 125 247 125 216 125 230 133 126 127 8 149"
4
values = values.split()
5
print(values)
6
values = [int(value) for value in values]
7
print(values)
8
sensors_16_bits = {
9
    'counter': [0, 1, 2, 3, 4, 5, 6, 7],
10
    'type': [8, 9, 10, 11, 12, 13, 14, 15],
11
12
    'F3': [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31],
13
    'FC5': [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47],
14
    'AF3': [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63],
15
    'F7': [64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
16
    'T7': [80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95],
17
    'P7': [96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111],
18
    'O1': [112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127],
19
    'QUALITY': [128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143],
20
    'O2': [144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159],
21
    'P8': [160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175],
22
    'T8': [176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191],
23
    'F8': [192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207],
24
    'AF4': [208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223],
25
    'FC6': [224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239],
26
    'F4': [240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]
27
    # 'F3':      [24,   25,  26,  27,  28,  29,  30,  31,  16,  17,  18,  19,  20,  21,  22,  23],
28
    # 'FC5':     [40,   41,  42,  43,  44,  45,  46,  47,  32,  33,  34,  35,  36,  37,  38,  39],
29
    # 'AF3':     [56,   57,  58,  59,  60,  61,  62,  63,  48,  49,  50,  51,  52,  53,  54,  55],
30
    # 'F7':      [72,   73,  74,  75,  76,  77,  78,  79,  64,  65,  66,  67,  68,  69,  70,  71],
31
    # 'T7':      [88,   89,  90,  91,  92,  93,  94,  95,  80,  81,  82,  83,  84,  85,  86,  87],
32
    # 'P7':      [104, 105, 106, 107, 108, 109, 110, 111,  96,  97,  98,  99, 100, 101, 102, 103],
33
    # 'O1':      [120, 121, 122, 123, 124, 125, 126, 127, 112, 113, 114, 115, 116, 117, 118, 119],
34
    # 'QUALITY': [136, 137, 138, 139, 140, 141, 142, 143, 128, 129, 130, 131, 132, 133, 134, 135],
35
    # 'O2':      [152, 153, 154, 155, 156, 157, 158, 159, 144, 145, 146, 147, 148, 149, 150, 151],
36
    # 'P8':      [168, 169, 170, 171, 172, 173, 174, 175, 160, 161, 162, 163, 164, 165, 166, 167],
37
    # 'T8':      [184, 185, 186, 187, 188, 189, 190, 191, 176, 177, 178, 179, 180, 181, 182, 183],
38
    # 'F8':      [200, 201, 202, 203, 204, 205, 206, 207, 192, 193, 194, 195, 196, 197, 198, 199],
39
    # 'AF4':     [216, 217, 218, 219, 220, 221, 222, 223, 208, 209, 210, 211, 212, 213, 214, 215],
40
    # 'FC6':     [232, 233, 234, 235, 236, 237, 238, 239, 224, 225, 226, 227, 228, 229, 230, 231],
41
    # 'F4':      [248, 249, 250, 251, 252, 253, 254, 255, 240, 241, 242, 243, 244, 245, 246, 247]
42
}
43
pos = 0
44
print(pos)
45
max_values = len(values) * 8
46
print(max_values)
47
this_value = struct.unpack('h', ''.join([chr(values[2]), chr(values[3])]))
48
print(this_value[0] / 8)
49
print(this_value[0] / 8 + abs(this_value[0]) * 0.0013131313)
50
this_value = struct.unpack('h', ''.join([chr(values[3]), chr(values[2])]))
51
print(this_value[0] / 8)
52
print(this_value[0] / 8 + abs(this_value[0]) * 0.0013131313)
53
this_value = struct.unpack('h', ''.join([chr(values[4]), chr(values[5])]))
54
print(this_value[0] / 8)
55
print(this_value[0] / 8 + abs(this_value[0]) * 0.0013131313)
56
this_value = struct.unpack('h', ''.join([chr(values[6]), chr(values[7])]))
57
print(this_value[0] / 8)
58
print(this_value[0] / 8 + abs(this_value[0]) * 0.0013131313)
59
this_value = struct.unpack('h', ''.join([chr(values[8]), chr(values[9])]))
60
print(this_value[0] / 8)
61
print(this_value[0] / 8 + abs(this_value[0]) * 0.0013131313)
62
this_value = struct.unpack('h', ''.join([chr(values[10]), chr(values[11])]))
63
print(this_value[0] / 8)
64
print(this_value[0] / 8 + abs(this_value[0]) * 0.0013131313)
65
this_value = struct.unpack('h', ''.join([chr(values[12]), chr(values[13])]))
66
print(this_value[0] / 8)
67
print(this_value[0] / 8 + abs(this_value[0]) * 0.0013131313)
68
this_value = struct.unpack('h', ''.join([chr(values[14]), chr(values[15])]))
69
print(this_value[0] / 8)
70
print(this_value[0] / 8 + abs(this_value[0]) * 0.0013131313)
71
this_value = struct.unpack('h', ''.join([chr(values[16]), chr(values[17])]))
72
print(this_value[0] / 8)
73
print(this_value[0] / 8 + abs(this_value[0]) * 0.0013131313)
74
this_value = struct.unpack('h', ''.join([chr(values[18]), chr(values[19])]))
75
print(this_value[0] / 8)
76
print(this_value[0] / 8 + abs(this_value[0]) * 0.0013131313)
77
this_value = struct.unpack('h', ''.join([chr(values[20]), chr(values[21])]))
78
print(this_value[0] / 8)
79
print(this_value[0] / 8 + abs(this_value[0]) * 0.0013131313)
80
this_value = struct.unpack('h', ''.join([chr(values[22]), chr(values[23])]))
81
print(this_value[0] / 8)
82
print(abs(this_value[0]) / 8 + abs(this_value[0]) * 0.0013131313)
83
this_value = struct.unpack('h', ''.join([chr(values[24]), chr(values[25])]))
84
print(this_value[0] / 8)
85
print(abs(this_value[0]) / 8 + abs(this_value[0]) * 0.0013131313)
86
this_value = struct.unpack('h', ''.join([chr(values[26]), chr(values[27])]))
87
print(this_value[0] / 8)
88
print(abs(this_value[0]) / 8 + abs(this_value[0]) * 0.0013131313)
89
this_value = struct.unpack('h', ''.join([chr(values[28]), chr(values[29])]))
90
print(this_value[0] / 8)
91
print(abs(this_value[0]) / 8 + abs(this_value[0]) * 0.0013131313)
92
this_value = struct.unpack('h', ''.join([chr(values[30]), chr(values[31])]))
93
print(this_value[0] / 8)
94
print(abs(this_value[0]) / 8 + abs(this_value[0]) * 0.0013131313)
95
bits = []
96
97
level = 0
98
all_list = []
99
bit_list = []
100
print(bit_list)
101
for byte in values:
102
    level = 0
103
    bit_list = []
104
    for i in range(7, -1, -1):
105
        level <<= 1
106
        bit_list.append(byte >> i & 1)
107
        level |= byte >> i & 1
108
    print(level)
109
    print(bit_list)
110
    all_list.append(bit_list)
111
112
print()
113
print("Sensor values")
114
for sensor, bits in sensors_16_bits.items():
115
    if len(bits) == 16:
116
        level = 0
117
        bit_list = []
118
        value = 0
119
        for i in range(16):
120
            # print(i)
121
            level <<= 1
122
            b = (bits[i] // 8)
123
            o = bits[i] % 8
124
            # print("B: {}, O: {}".format(b, o))
125
            # print("Level before shift: {}".format(level))
126
            level <<= 1
127
128
            # print("Level after shift: {}".format(level))
129
            bit_list.append(values[b] >> o & 1)
130
            if i == 6:
131
                value = abs(level - 4096) * 0.13
132
                # level = level - 4096
133
134
            level |= values[b] >> o & 1
135
            # print("Level after add: {}".format(level))
136
        # print(len(bit_list))
137
        value += 4096
138
        value = str(value) + str(abs(level))
139
        # print(value)
140
        # print(struct.unpack('>d', ''.join(chr(char) for char in bit_list[:8])))
141
        # print(level)
142
        print(bit_list)
143
        # print(struct.unpack('>l', ''.join(chr(char) for char in bit_list[-8:])))