'goodfet.cc term'
[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 run(self):
23         while 1:
24             sys.stdout.write("gf% ");
25             sys.stdout.flush();
26             cmd=sys.stdin.readline();
27             self.handle(cmd);
28
29     def handle(self, str):
30         """Handle a command string.  First word is command."""
31         #Lines beginning with # are comments.
32         if(str[0]=="#"):  return;
33         #Lines beginning with ! are Python.
34         if(str[0]=="!"):
35             try:
36                 exec(str.lstrip('!'));
37             except:
38                 print sys.exc_info()[0];
39             return;
40         #Backtick (`) indicates shell commands.
41         if(str[0]=='`'):
42             os.system(str.lstrip('`'));
43             return;
44         #By this point, we're looking at a GoodFET command.
45         args=str.split();
46         if len(args)==0:
47             return;
48         try:
49             eval("self.CMD%s(args)" % args[0])
50         except:
51             print sys.exc_info()[0];
52             #print "Unknown command '%s'." % args[0];
53     def CMDinfo(self,args):
54         print self.client.infostring()
55     def CMDlock(self,args):
56         print "Locking.";
57         self.client.lock();
58     def CMDerase(self,args):
59         print "Erasing.";
60         self.client.erase();
61     def CMDtest(self,args):
62         self.client.test();
63         return;
64     def CMDstatus(self,args):
65         print self.client.status();
66         return;
67     def CMDpeek(self,args):
68         adr=eval(args[1]);
69         memory="vn";
70         if(len(args)>2):
71             memory=args[2];
72         print "0x%08x:= 0x%04x" % (adr, self.client.peek16(adr,memory));
73     def CMDflash(self,args):
74         file=args[1];
75         self.client.flash(self.expandfilename(file));
76     def expandfilename(self,filename):
77         if(filename[0]=='~'):
78             return "%s%s" % (os.environ.get("HOME"),filename.lstrip('~'));
79         return filename;
80     
81