Add TXRSSI and TXLQI sensors to FrSky D8/D16/LBT when using MULTI (#7128)
[opentx.git] / radio / util / codecs.py
blobfdc88f4fe738cb5ce10e529c6ae8e0a44c2ea055
1 #!/bin/env python
3 from __future__ import print_function
5 SIGN_BIT = (0x80) # Sign bit for a A-law byte.
6 QUANT_MASK = (0xf) # Quantization field mask.
7 SEG_SHIFT = (4) # Left shift for segment number.
8 SEG_MASK = (0x70) # Segment field mask.
9 BIAS = (0x84) # Bias for linear code.
12 def alaw2linear(a_val):
13 a_val ^= 0x55
15 t = a_val & QUANT_MASK
16 seg = (a_val & SEG_MASK) >> SEG_SHIFT
17 if (seg):
18 t = (t + t + 1 + 32) << (seg + 2)
19 else:
20 t = (t + t + 1) << 3
22 if a_val & SIGN_BIT:
23 return t
24 else:
25 return -t
28 def ulaw2linear(u_val):
29 # Complement to obtain normal u-law value.
30 u_val = ~u_val
32 # Extract and bias the quantization bits. Then
33 # shift up by the segment number and subtract out the bias.
34 t = ((u_val & QUANT_MASK) << 3) + BIAS
35 t <<= (u_val & SEG_MASK) >> SEG_SHIFT
37 if u_val & SIGN_BIT:
38 return (BIAS - t)
39 else:
40 return (t - BIAS)
43 def pcmTable(fn):
44 result = []
45 for i in range(256):
46 result.append(fn(i))
47 return result
50 def tableToString(name, table):
51 result = 'const int16_t ' + name + '[256] = { '
52 result += ', '.join(str(i) for i in table)
53 result += ' };'
54 return result
57 print(tableToString('alawTable', pcmTable(alaw2linear)))
58 print(tableToString('ulawTable', pcmTable(ulaw2linear)))