Unimplemented standard commands now print warnings.
[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 prompt(self):
23         sys.stdout.write("gf% ");
24         sys.stdout.flush();
25     def run(self):
26         self.prompt();
27         #for cmd in sys.stdin:
28         while 1:
29             cmd=sys.stdin.readline();
30             if not cmd: break;
31             if(cmd.strip()!=""):
32                 self.handle(cmd);
33             self.prompt();
34     def handle(self, str):
35         """Handle a command string.  First word is command."""
36         #Lines beginning with # are comments.
37         if(str[0]=="#"):  return;
38         #Lines beginning with ! are Python.
39         if(str[0]=="!"):
40             try:
41                 exec(str.lstrip('!'));
42             except:
43                 print sys.exc_info()[0];
44             return;
45         #Backtick (`) indicates shell commands.
46         if(str[0]=='`'):
47             os.system(str.lstrip('`'));
48             return;
49         #By this point, we're looking at a GoodFET command.
50         args=str.split();
51         if len(args)==0:
52             return;
53         try:
54             eval("self.CMD%s(args)" % args[0])
55         except:
56             print sys.exc_info()[0];
57             #print "Unknown command '%s'." % args[0];
58     def CMDinfo(self,args):
59         print self.client.infostring()
60     def CMDlock(self,args):
61         print "Locking.";
62         self.client.lock();
63     def CMDerase(self,args):
64         print "Erasing.";
65         self.client.erase();
66     def CMDtest(self,args):
67         self.client.test();
68         return;
69     def CMDstatus(self,args):
70         print self.client.status();
71         return;
72     def CMDhalt(self,args):
73         print self.client.halt();
74     def CMDresume(self,args):
75         print self.client.resume();
76     def CMDpeek(self,args):
77         adr=eval(args[1]);
78         memory="vn";
79         if(len(args)>2):
80             memory=args[2];
81         print "0x%08x:= 0x%04x" % (adr, self.client.peek16(adr,memory));
82     def CMDflash(self,args):
83         file=args[1];
84         self.client.flash(self.expandfilename(file));
85     def CMDwhere(self,args):
86         pc=self.client.getpc();
87         print "PC=0x%04X" % pc;
88     def CMDchip(self,args):
89         cmd="self.client.CMD%s()" % args[1];
90         print cmd;
91         try:
92             eval(cmd);
93         except:
94             print sys.exc_info()[0];
95             print "Chip-specific command failed.";
96     def expandfilename(self,filename):
97         if(filename[0]=='~'):
98             return "%s%s" % (os.environ.get("HOME"),filename.lstrip('~'));
99         return filename;
100     
101