Initial import of OsmocomBB into git repository
[osmocom-bb.git] / src / target_dsp / calypso / bin2cfile.py
1 #!/usr/bin/env python
2
3 import struct
4 import sys
5
6 def group_by_n(s, n, do_join=True):
7         return ( ''.join(x) for x in zip(*[s[i::n] for i in range(n)]) )
8         
9
10 def main(pn, filename):
11         # Get all bytes
12         f = open(filename, 'r')
13         d = f.read()
14         f.close()
15
16         # Get the data
17         ops = ''.join([
18                 '0x%04x,%c' % (
19                         struct.unpack('=H', x)[0],
20                         '\n' if (i&3==3) else ' '
21                 )
22                 for i, x
23                 in enumerate(group_by_n(d, 2))
24         ])[:-1]
25
26         # Header / footer
27         print """
28 #define _SA_DECL (const uint16_t *)&(const uint16_t [])
29
30 static const struct dsp_section dsp_xxx[] = {
31         {
32                 .addr = 0x,
33                 .size = 0x%04x,
34                 .data = _SA_DECL {
35 %s
36                 },
37         },
38         { /* Guard */
39                 .addr = 0,
40                 .size = 0,
41                 .data = NULL,
42         },
43 };
44
45 #undef _SA_DECL
46 """ % (len(d)/2, ops)
47
48
49 if __name__ == "__main__":
50         main(*sys.argv)