Imported Upstream version 3.2.2
[debian/gnuradio] / usrp / host / lib / legacy / gen_usrp_dbid.py
1 #!/usr/bin/env python
2
3 import sys
4 import os
5 import os.path
6 import re
7 from optparse import OptionParser
8
9 def write_header(f, comment_char):
10     f.write(comment_char); f.write('\n')
11     f.write(comment_char); f.write(' Machine generated by gen_usrp_dbid.py from usrp_dbid.dat\n')
12     f.write(comment_char); f.write(' Do not edit by hand.  All edits will be overwritten.\n')
13     f.write(comment_char); f.write('\n')
14     f.write('\n')
15
16 def gen_dbid_py(r):
17     f = open('usrp_dbid.py', 'w')
18     comment_char = '#'
19     write_header(f, comment_char)
20     f.write(comment_char); f.write('\n')
21     f.write(comment_char); f.write(" USRP Daughterboard ID's\n")
22     f.write(comment_char); f.write('\n')
23     f.write('\n')
24     for x in r:
25         f.write('%-16s = %s\n' % (x[1], x[2]))
26
27 def gen_dbid_h(r):
28     f = open('usrp_dbid.h', 'w')
29     comment_char = '//'
30     write_header(f, comment_char)
31     f.write(comment_char); f.write('\n')
32     f.write(comment_char); f.write(" USRP Daughterboard ID's\n")
33     f.write(comment_char); f.write('\n')
34     f.write('\n')
35     f.write('#ifndef INCLUDED_USRP_DBID_H\n')
36     f.write('#define INCLUDED_USRP_DBID_H\n')
37     f.write('\n')
38     for x in r:
39         f.write('#define %-25s %s\n' % ('USRP_DBID_' + x[1], x[2]))
40     f.write('\n')
41     f.write('#endif /* INCLUDED_USRP_DBID_H */\n')
42
43 def gen_dbid_cc(r):
44     f = open('usrp_dbid.cc', 'w')
45     write_header(f, '//')
46     head = '''/*
47  * Copyright 2005 Free Software Foundation, Inc.
48  * 
49  * This file is part of GNU Radio
50  * 
51  * GNU Radio is free software; you can redistribute it and/or modify
52  * it under the terms of the GNU General Public License as published by
53  * the Free Software Foundation; either version 3, or (at your option)
54  * any later version.
55  * 
56  * GNU Radio is distributed in the hope that it will be useful,
57  * but WITHOUT ANY WARRANTY; without even the implied warranty of
58  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
59  * GNU General Public License for more details.
60  * 
61  * You should have received a copy of the GNU General Public License
62  * along with GNU Radio; see the file COPYING.  If not, write to
63  * the Free Software Foundation, Inc., 51 Franklin Street,
64  * Boston, MA 02110-1301, USA.
65  */
66
67 #include <usrp_prims.h>
68 #include <usrp_dbid.h>
69 #include <stdio.h>
70
71 #define NELEM(x) sizeof(x)/sizeof(x[0])
72
73 static struct {
74   unsigned short        dbid;
75   const char           *name;
76 } dbid_map[] = {
77 '''
78     
79     tail = '''};
80
81 const std::string
82 usrp_dbid_to_string (int dbid)
83 {
84   if (dbid == -1)
85     return "<none>";
86
87   if (dbid == -2)
88     return "<invalid EEPROM contents>";
89
90   for (unsigned i = 0; i < NELEM (dbid_map); i++)
91     if (dbid == dbid_map[i].dbid)
92       return dbid_map[i].name;
93
94   char tmp[64];
95   snprintf (tmp, sizeof (tmp), "Unknown (0x%04x)", dbid);
96   return tmp;
97 }
98 '''
99     f.write(head)
100     for x in r:
101         f.write('  { %-27s "%s" },\n' % (
102             'USRP_DBID_' + x[1] + ',', x[0]))
103     f.write(tail)
104
105 def gen_all(src_filename):
106     src_file = open(src_filename, 'r')
107     r = []
108     for line in src_file:
109         line = line.strip()
110         line = re.sub(r'\s*#.*$','', line)
111         if len(line) == 0:
112             continue
113         mo = re.match('"([^"]+)"\s*(0x[0-9a-fA-F]+)', line)
114         if mo:
115             str_name = mo.group(1)
116             id_name = str_name.upper().replace(' ', '_')
117             id_val = mo.group(2)
118             r.append((str_name, id_name, id_val))
119             #sys.stdout.write('%-16s\t%-16s\t%s\n' % ('"'+str_name+'"', id_name, id_val))
120
121     gen_dbid_h(r)
122     gen_dbid_py(r)
123     gen_dbid_cc(r)
124     
125
126 def main():
127     usage = "usage: %prog [options] usrp_dbid.dat"
128     parser = OptionParser(usage=usage)
129     (options, args) = parser.parse_args()
130     if len(args) != 1:
131         parser.print_help()
132         sys.exit(1)
133
134     gen_all(args[0])
135
136 if __name__ == '__main__':
137     main()