More of a console, approaching standard commandset for Chipcon port.
[goodfet] / client / GoodFETConsole.py
1 #!/usr/bin/env python
2 # GoodFET Client Library
3
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # This code is being rewritten and refactored.  You've been warned!
7
8 import sys, os;
9 import binascii;
10
11 from GoodFET import GoodFET;
12 from intelhex import IntelHex;
13
14 class GoodFETConsole():
15     """An interactive goodfet driver."""
16     
17     def __init__(self, client):
18         self.client=client;
19         client.serInit();
20         client.setup();
21         client.start();
22     def handle(self, str):
23         """Handle a command string.  First word is command."""
24         #Lines beginning with # are comments.
25         if(str[0]=="#"):  return;
26         #Lines beginning with ! are Python.
27         if(str[0]=="!"):
28             try:
29                 exec(str.lstrip('!'));
30             except:
31                 print sys.exc_info()[0];
32             return;
33         #Backtick (`) indicates shell commands.
34         if(str[0]=='`'):
35             os.system(str.lstrip('`'));
36             return;
37         #By this point, we're looking at a GoodFET command.
38         args=str.split();
39         if len(args)==0:
40             return;
41         try:
42             eval("self.CMD%s(args)" % args[0])
43         except:
44             print sys.exc_info()[0];
45             #print "Unknown command '%s'." % args[0];
46     def CMDinfo(self,args):
47         print self.client.infostring()
48     def CMDlock(self,args):
49         print "Locking.";
50         self.client.lock();
51     def CMDerase(self,args):
52         print "Erasing.";
53         self.client.erase();
54     def CMDtest(self,args):
55         self.client.test();
56         return;
57     def CMDstatus(self,args):
58         print self.client.status();
59         return;
60     def CMDpeek(self,args):
61         adr=eval(args[1]);
62         memory="vn";
63         if(len(args)>2):
64             memory=args[2];
65         print "0x%08x:= 0x%04x" % (adr, self.client.peek16(adr,memory));
66     def CMDflash(self,args):
67         file=args[1];
68         self.client.flash(self.expandfilename(file));
69     def expandfilename(self,filename):
70         if(filename[0]=='~'):
71             return "%s%s" % (os.environ.get("HOME"),filename.lstrip('~'));
72         return filename;
73     
74