Python tricks » History » Version 1
Alexis Jeandet, 30/03/2014 05:34 PM
1 | 1 | Alexis Jeandet | h1. Python tricks |
---|---|---|---|
2 | |||
3 | {{>toc}} |
||
4 | |||
5 | h2. Basic SocExplorer interaction |
||
6 | |||
7 | An interesting feature in SocExplorer is that you can drag and drop a python script in the terminal, it will execute it. First you can get some updated examples in the doc folder of SocExplorer "source code":https://jeandet@hephaistos.lpp.polytechnique.fr/rhodecode/HG_REPOSITORIES/LPP/INSTRUMENTATION/SocExplorer. |
||
8 | |||
9 | h3. Plugin related functions |
||
10 | |||
11 | * To load a plugin from python |
||
12 | |||
13 | The key object to load plugins is the proxy object, in SocExplorer console you can type proxy and then with the "tab" key you will get all the available methods. The ones you need are loadSysDriver() and loadSysDriverToParent(), you can get their definition by invoking them without any arguments. In most cases with SocExplorer if you don't know how to use a python method just call it and you will get its definition. |
||
14 | |||
15 | ** Load a root plugin |
||
16 | |||
17 | <pre><code class="python"> |
||
18 | |||
19 | #to load the AHBUART plugin |
||
20 | proxy.loadSysDriver("AHBUARTplugin") |
||
21 | # You can also specify an instance name |
||
22 | proxy.loadSysDriver("AHBUARTplugin","InstanceName") |
||
23 | # SocExplorer can also resolve the plugin from it file name |
||
24 | proxy.loadSysDriver("/home/your-path/.SocExplorer/plugins/libahbuartplugin.so") |
||
25 | # Even without the path |
||
26 | proxy.loadSysDriver("libahbuartplugin.so") |
||
27 | |||
28 | </code></pre> |
||
29 | |||
30 | |||
31 | ** Load a child plugin |
||
32 | |||
33 | A child plugin need a parent to be connected to. |
||
34 | |||
35 | <pre><code class="python"> |
||
36 | |||
37 | #to load the GenericRWplugin plugin and connect it to AHBUARTplugin0 instance |
||
38 | proxy.loadSysDriverToParent("GenericRWplugin", "AHBUARTplugin0") |
||
39 | # You can also specify an instance name for the child plugin |
||
40 | proxy.loadSysDriverToParent("GenericRWplugin","InstanceName", "AHBUARTplugin0") |
||
41 | #As for root plugin SocExplorer can resolve the plugin from it file name. |
||
42 | |||
43 | </code></pre> |
||
44 | |||
45 | ** Common plugin functions |
||
46 | |||
47 | All the plugins will provide you two methods to read or write, with root plugin it means read or write in the SOC bus. With child driver it can have different meaning depending on the plugin. Usually to read or write on your SOC just do: |
||
48 | |||
49 | <pre><code class="python"> |
||
50 | |||
51 | #rootplugin is your root plugin instance name, address is the address from where you want to read. |
||
52 | #count is the number of words you want to read /!\ usually it is 32 bits words. |
||
53 | #If count=2 you will read two 32 bits words. |
||
54 | data=rootplugin.Read(address, count) |
||
55 | |||
56 | #If you want to write just do |
||
57 | rootplugin.Write(address, [Word1,Word2,...]) |
||
58 | |||
59 | </code></pre> |
||
60 | |||
61 | h3. SOC related functions |
||
62 | |||
63 | * Find a peripheral address |
||
64 | |||
65 | With SOC design on FPGA, you can easily change the number of peripherals or their base address but you don't necessary want to update your python script each time you modify your SOC layout. One solution is to use plug and play feature such as the one provided by the Gaisler's GRLIB. SocExplorer is able to store and distribute the list of peripherals and their address, once a dedicated plugin did the scan for him such as the AMBA plugin. We suppose that the connection to the SOC is active, the scan is done and the root plugin instance name is "RootPlugin". |
||
66 | |||
67 | |||
68 | <pre><code class="python"> |
||
69 | |||
70 | #this function will return the base address of the first matching VID/PID device in the list |
||
71 | #by convention it is also the lower address one |
||
72 | baseAddress = SocExplorerEngine.getEnumDeviceBaseAddress("RootPlugin",VID,PID) |
||
73 | |||
74 | #you an also specify the index of the device, if you want the second |
||
75 | baseAddress = SocExplorerEngine.getEnumDeviceBaseAddress("RootPlugin",VID,PID,1) |
||
76 | |||
77 | </code></pre> |
||
78 | |||
79 | h2. SocExplorer provided objects |
||
80 | |||
81 | h3. SocExplorer plot |
||
82 | |||
83 | SocExplorerPlot is a wrapper to the "QCustomPlot":http://www.qcustomplot.com/ class, a simple and efficient plot widget. The following example should give you this result, please note that you will also need numpy library to run it. |
||
84 | |||
85 | p=. !{width: 80%}SocExplorerPlot.png(SocExplorerPlot example)! |
||
86 | |||
87 | <pre><code class="python"> |
||
88 | |||
89 | import numpy as np |
||
90 | freq1 = 30 |
||
91 | freq2 = 300 |
||
92 | time_step = 0.001 |
||
93 | |||
94 | t_ini = -50 * 1.0/(max(freq1,freq2)) |
||
95 | t_fin = -1 * t_ini |
||
96 | |||
97 | time_vec = np.arange(t_ini, t_fin, time_step) |
||
98 | |||
99 | #input signal |
||
100 | input_sig1 = np.sin(2 * np.pi * freq1 * time_vec) |
||
101 | input_sig2 = np.sin(2 * np.pi * freq2 * time_vec) |
||
102 | input_sig = input_sig1 + input_sig2 |
||
103 | |||
104 | |||
105 | plot=PySocExplorer.SocExplorerPlot() |
||
106 | plot.setTitle("demo") |
||
107 | plot.setXaxisLabel("Time(s)") |
||
108 | plot.setYaxisLabel("Values") |
||
109 | |||
110 | Courbe1=plot.addGraph() |
||
111 | Courbe2=plot.addGraph() |
||
112 | Courbe3=plot.addGraph() |
||
113 | |||
114 | plot.setGraphData(Courbe1,time_vec.tolist(),input_sig1.tolist()) |
||
115 | plot.setGraphData(Courbe2,time_vec.tolist(),input_sig2.tolist()) |
||
116 | plot.setGraphData(Courbe3,time_vec.tolist(),input_sig.tolist()) |
||
117 | |||
118 | pen=plot.getGraphPen(1) |
||
119 | pen.setWidth(1) |
||
120 | color=pen.color() |
||
121 | color.setRgb(0x00FF00) |
||
122 | pen.setColor(color) |
||
123 | plot.setGraphPen(1,pen) |
||
124 | |||
125 | pen=plot.getGraphPen(0) |
||
126 | pen.setWidth(1) |
||
127 | color=pen.color() |
||
128 | color.setRgb(0xFF0000) |
||
129 | pen.setColor(color) |
||
130 | plot.setGraphPen(2,pen) |
||
131 | |||
132 | plot.rescaleAxis() |
||
133 | |||
134 | </code></pre> |
||
135 | |||
136 | h3. TCP_Terminal_Client |
||
137 | |||
138 | Sometime you need to print some information while your python script is running, unfortunately SocExplorer isn't multi-threaded so you won't get any output until your script execution is finished. To solve this problem with SocExplorer setup you will get a small tcp terminal program which will run in a separated process. From one Python object you will be able to start the terminal process and to send it some data to print. Note that an other utilization of this terminal should be to deport the print outputs on a distant computer. |
||
139 | |||
140 | * Simple example 1 |
||
141 | |||
142 | <pre><code class="python"> |
||
143 | |||
144 | term=PySocExplorer.TCP_Terminal_Client() |
||
145 | term.startServer() |
||
146 | term.connectToServer() |
||
147 | term.sendText("hello") |
||
148 | |||
149 | </code></pre> |
||
150 | |||
151 | Should give you this result |
||
152 | |||
153 | p=. !{width: 40%}TCP_Terminal_Client1.png(SocExplorerPlot example)! |
||
154 | |||
155 | * Distant connection |
||
156 | |||
157 | <pre><code class="python"> |
||
158 | |||
159 | term=PySocExplorer.TCP_Terminal_Client() |
||
160 | #for a distant connection you can specify the distant address and port |
||
161 | #remember to check your firewall setings |
||
162 | term.connectToServer("192.168.1.10",2000) |
||
163 | term.sendText("hello") |
||
164 | |||
165 | </code></pre> |
||
166 | |||
167 | * Multi-terminal and HTML print |
||
168 | |||
169 | <pre><code class="python"> |
||
170 | |||
171 | import time |
||
172 | terminal=PySocExplorer.TCP_Terminal_Client() |
||
173 | terminal.startServer() |
||
174 | terminal2=PySocExplorer.TCP_Terminal_Client() |
||
175 | terminal2.startServer(2200) |
||
176 | terminal2.connectToServer("127.0.0.1",2200) |
||
177 | |||
178 | terminal.connectToServer() |
||
179 | terminal.sendText("<p><b> "+str(time.ctime())+": </b></p>"+"Hello World") |
||
180 | terminal2.sendText("<p><b> "+str(time.ctime())+": </b></p>"+"Hello World on terminal 2") |
||
181 | terminal.sendText("<p><b> "+str(time.ctime())+": </b></p>") |
||
182 | terminal.sendText("<p><b> "+str(time.ctime())+": </b></p>"+"<ul>HTML Items List Example:<LI>Item1</LI><LI>Item2</LI></ul>") |
||
183 | terminal.sendText("<p><b> "+str(time.ctime())+": </b></p>"+"<p style=\"color:#0000ff\" style=\"background-color:#00ff00\">hello</p>") |
||
184 | for i in range(0,100): |
||
185 | terminal.sendText("<p><b>"+str(time.ctime())+": </b></p>"+"<p style=\"color:#0000ff\" style=\"background-color:#00ff00\">hello "+str(i)+"</p>") |
||
186 | time.sleep(0.05) |
||
187 | |||
188 | </code></pre> |
||
189 | |||
190 | |||
191 | Should give you this result |
||
192 | |||
193 | p=. !{width: 70%}TCP_Terminal_Client2.png(SocExplorerPlot example)! |
||
194 | |||
195 | h3. QhexSpinBox |
||
196 | |||
197 | h3. QhexEdit |