SaltyCrane Blog — Notes on JavaScript and web development

How to switch from the GNU to the Diab compiler in VxWorks / Tornado 2.2

 I was using the GNU compiler for aVxWorks / Tornado project and wanted to switch to the Diab compiler. Here is how to do it:

1. In the "Workspace" window, click on the "Builds" tab.
2. Right-click on the project that you want to change and select "New Build..."
3. Select the "Default Build Spec for" option and select the Diab toolchain for your hardware.
4. Click "OK". The Diab compiler should now be the "Active Build".
5. To switch back to the GNU compiler, click on the "+" sign to expand the branch, then right-click on the GNU build, and select "Set 'XXXXXXgnu' as Active Build".

How to use Eclipse and CDT to edit C source files

Eclipse is a good integrated development environment rivaling Microsoft Visual Studio 2005. I have Visual Studio 2005 installed on my computer, though for some reason, I don't care to use it. Eclipse originally was used for Java development but now includes plugins for C/C++, Python, Perl, and many others. I use the Pydev plugin for Python development and it is good.

I am using Tornado 2.2 to develop C code for VxWorks. The editor in Tornado isn't the greatest, so I want to edit my code in Eclipse with the CDT plugin. I will still be using Tornado to build the projects, so instead of using a Standard or Managed Make Project, I just created a folder which linked to my source files. Note that you may get messages in Tornado that say "This file has been changed outside the source editor. Do you want to reload it?" Just make sure you don't have any unsaved changes from Tornado and click "Yes". Or just make sure all your source code windows are closed in Tornado. I know this isn't the most elegant solution, but it will have to do unless we upgrade to VxWorks 6 and Workbench.

Update 2/13/07: I've found a better way to use Eclipse with Tornado projects. See my post How to use Tornado GNU tools with Eclipse/CDT for how to use Eclipse to edit *and build* your Tornado project.

Here are the steps:
  1. Start with your .c and .h files in a directory called "c:\sofeng\proj\stuff"
  2. Download and install eclipse-SDK-3.2.1-win32.zip from http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops/R-3.2.1-200609210945/eclipse-SDK-3.2.1-win32.zip
  3. Download and install org.eclipse.cdt-3.1.1-win32.x86.zip from http://download.eclipse.org/tools/cdt/releases/callisto/dist/3.1.1/
  4. Run "eclipse.exe"
    5. From the menu: "Window" -> "Open Perspective" -> "Other..." -> "C/C++" -> "OK"
  5. From the menu: "File" -> "New" -> "Other..."
  6. Under "General", select "Project"
  7. Click "Next"
  8. In the "Project name:" field, type "dummy". Leave the "Use default location" box checked.
  9. Click "Finish"
  10. From the menu: "File" -> "New" -> "Folder"
  11. In the "Enter or select the parent folder:" enter or select the "dummy" project you just created.
  12. Click the ">> Advanced" button
  13. Click the "Link to folder in the file system" checkbox and enter "c:\sofeng\proj\stuff" or "Browse..." to that location.
  14. Click "Finish"
NOTE: If you do not see the ">> Advanced" button, follow these steps:
  1. From the "Window" menu, select "Preferences..."
  2. Go to "General" > "Workspace" > "Linked Resources" and check "Enable linked resources"
  3. Click "OK"

How to create a downloadable vxworks project in Tornado 2.2

Start with your .c and .h files in a directory called "c:\sofeng\proj\myproj"

TORNADO DOWNLOADABLE PROJECT
1. Install Tornado 2.2
2. Run Tornado ("Start" -> "All Programs" -> "Tornado 2.2" -> "Tornado")
3. If the "Create Project in New/Existing Workspace" dialog is up, click on "New". If not, go to "File" -> "New Project..."
4. Click "Create downloadable application modules for VxWorks", click "OK"
5. In the "Name:" field, enter "myproj"
6. In the "Location:" field, enter "c:\sofeng\proj\myproj"
7. In the "Add to a New or Existing Workspace" field, enter "c:\sofeng\proj\Workspace0.wsp"
8. Click "Next"
9. Choose the "A toolchain" option
10. From the dropdown, choose "SIMNTgnu" if you are using the simulator. Otherwise choose the proper toolchain for your processor.
11. Click "Next" then click "Finish"
12. In the "Workspace:Workspace0" window, make sure the "Files" tab is selected. Make sure the "Workspace0" branch is expanded.
12. Right-click on "myproj Files" and select "Add files..."
13. Navigate to c:\sofeng\proj\myproj
14. Select all the files. (Click the first file, then while holding the "Shift" key, click the last file.)
15. Click "Add"
16. To build your project, right-click on "myproj Files"in the "Workspace:Workspace0" window and select "Build 'myproj.out'"

See also VxWorks/Tornado bootable project tutorial

Standard Make vs. Managed Make in CDT in Eclipse

From this presentation, here are differences between Standard Make and Managed Make in CDT in Eclipse.


Standard Make
  • Re-uses existing makefiles
  • Simple integration with arbitrary build systems
  • Parsing of toolchain output to generate error markers
Managed Make
  • Manages compiles and toolchain directly
  • No makefile editing
  • Fine control over compile, link settings
Also, see "When do I use the standard make feature" from the CDT FAQ.

How to install Easy Install for Python

Update 2009-03-31:
How to install Easy Install on Ubuntu Linux
$ sudo apt-get install python-setuptools python-dev build-essential

How to install Easy Install on Windows
  1. Go to http://peak.telecommunity.com/DevCenter/EasyInstall#installing-easy-install
  2. Right click on "ez_setup.py" and save the file to "c:\temp"
  3. Open a cmd.exe prompt
  4. "cd c:\temp"
  5. "python ez_setup.py"

How to install Easy Install on Cygwin
  1. $ cd /tmp
  2. $ wget http://peak.telecommunity.com/dist/ez_setup.py
  3. $ python ez_setup.py

How to draw a simple line using python and the matplotlib API -

I'm continuing to learn the low level object oriented matplotlib API. My goal is to create very customizable, perfect plots. Here is how to draw a simple line. First create a figure that is 4 inches by 4 inches. Then create some axes with a 10% margin around each edge. Then add the axes to the figure. Then create a line from (0,0) to (1,1). Then add the line to the axes. Then create a canvase. Then create the .png file. Looks like good object oriented python fun to me...


""" line_ex.py                                             
"""
from matplotlib.figure import Figure
from matplotlib.axes import Axes
from matplotlib.lines import Line2D
from matplotlib.backends.backend_agg import FigureCanvasAgg

fig = Figure(figsize=[4,4])
ax = Axes(fig, [.1,.1,.8,.8])
fig.add_axes(ax)
l = Line2D([0,1],[0,1])
ax.add_line(l)

canvas = FigureCanvasAgg(fig)
canvas.print_figure("line_ex.png")

How to use the pylab API vs. the matplotlib API

This article has a good description of the 2 API's in matplotlib: the pylab API and the matplotlib API. I've been using the pylab interface because it is easier, especially coming from a matlab background. But I wanted to get direct access to the matplotlib classes so I needed to use the matplotlib API. Here is a simple example that creates a .png figure using the 2 different API's.

Here is the example using the pylab API:
""" api_pylab.py          
"""
from pylab import *

figure(figsize=[4,4])
axes([.1,.1,.8,.8])
scatter([1,2],[3,4])
savefig('api_pylab.png')

Here is the example using the matplotlib API:
""" api_matplotlib.py                                        
"""
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg

fig = Figure(figsize=[4,4])
ax = fig.add_axes([.1,.1,.8,.8])
ax.scatter([1,2], [3,4])
canvas = FigureCanvasAgg(fig)
canvas.print_figure("api_matplotlib.png")

How to create some derived arrow classes with matplotlib and python

Here is an example of how to create some derived arrow classes with matplotlib and python. The arrow() function inmatplotlib accepts origin and delta x and delta y inputs. I changed this to polor coordinates so Arrow2 accepts the x and y coordinates of the origin, the length, and the angle. Then I created 4 classes derived from Arrow2 called ArrowRight, ArrowLeft, ArrowUp, and ArrowDown. These just set the angle for you to 0, 180, 90, and 270 respectively.Notice too that the **kwargs can be passed down so you can still set all the other parameters.
""" arrow_ex2.py """ 
from pylab import *  

def main(): 
    figure() 
    axes() 
    Arrow2(.5,.5,.2,45) 
    ArrowRight(.5,.5,.2) 
    ArrowLeft(.5,.5,.2) 
    ArrowUp(.5,.5,.2) 
    ArrowDown(.5,.5,.2) 
    show() 

class Arrow2: 
    def __init__(self, x0, y0, length, angle=0.0, color='k', width=0.01, **kwargs):          
        dx = length*cos(angle*pi/180) 
        dy = length*sin(angle*pi/180) 
        arrow (x0, y0, dx, dy, 
               width=width, 
               edgecolor=color,  
               facecolor=color,  
               antialiased=True,  
               head_width=5*width, 
               head_length=7.5*width, 
               **kwargs)

class ArrowRight(Arrow2): 
    def __init__(self, x0, y0, length, **kwargs): 
        Arrow2.__init__(self, x0, y0, length, angle=0.0, **kwargs)

class ArrowLeft(Arrow2): 
    def __init__(self, x0, y0, length, **kwargs): 
        Arrow2.__init__(self, x0, y0, length, angle=180.0, **kwargs)                      

class ArrowUp(Arrow2): 
    def __init__(self, x0, y0, length, **kwargs): 
        Arrow2.__init__(self, x0, y0, length, angle=90.0, **kwargs) 

class ArrowDown(Arrow2): 
    def __init__(self, x0, y0, length, **kwargs): 
        Arrow2.__init__(self, x0, y0, length, angle=270.0, **kwargs) 

if __name__ == "__main__": 
    main() 

Example pie charts using python and matplotlib


I needed to make some pie charts and didn't like the results I got from Excel. It was too hard to customize the plots exactly the way I wanted them. I have used Matlab before and I preferred Matlab to Excel. However, Python is my favorite thing to use so I searched for python and matlab on Google and found matplotlib. Matplotlib is a matlab-like plotting library for Python. You can get matplotlib from http://matplotlib.sourceforge.net/, but it is also bundled with the Enthought version of Python so I got it from there. Update: I realized that the Enthought bundle didn't include the latest version of matplotlib so I installed the latest version of matplotlib and the required NumPy as well.

Step-by-step:
1. Download enthought version of Python 2.4.3 from http://code.enthought.com/enthon/ (Click on the "enthon-python2.4-1.0.0.exe" link is at the bottom of the page) and install it.
2. Download "numpy-1.0.1.win32-py2.4.exe" from http://sourceforge.net/project/showfiles.php?group_id=1369 and install it.
3. Download "matplotlib-0.87.7.win32-py2.4.exe" from http://sourceforge.net/projects/matplotlib and install it.
3. Open a text editor and type this inside:
#!/usr/bin/env python
"""
http://matplotlib.sf.net/matplotlib.pylab.html#-pie for the docstring.
"""
from pylab import *

# create figure
figwidth = 10.0    # inches
figheight = 3.5   # inches
figure(1, figsize=(figwidth, figheight))
rcParams['font.size'] = 12.0
rcParams['axes.titlesize'] = 16.0
rcParams['xtick.labelsize'] = 12.0
rcParams['legend.fontsize'] = 12.0
explode=(0.05, 0.0)
colors=('b','g')
Ncols = 3
plotheight = figwidth/Ncols
H = plotheight/figheight
W = 1.0 / Ncols
margin = 0.1
left = [W*margin, W*(1+margin), W*(2+margin)]
bottom = H*margin
width = W*(1-2*margin)
height = H*(1-2*margin)

# cpu utilization
utilized = 10.0
free = 100.0 - utilized
fracs = [utilized, free]
axes([left[0], bottom, width, height])
patches = pie(fracs, colors=colors, explode=explode, autopct='%1.f%%', shadow=True)
title('CPU Throughput')
legend((patches[0], patches[2]), ('Processing', 'Idle'), loc=(0,-.05))

# ROM utilization
utilized = 30.0
free = 100.0 - utilized
fracs = [utilized, free]
axes([left[1], bottom, width, height])
patches = pie(fracs, colors=colors, explode=explode, autopct='%1.f%%', shadow=True)
title('ROM Memory Usage')
legend((patches[0], patches[2]), ('Used', 'Unused'), loc=(0,-.05))

# RAM utilization
utilized = 15.0
free = 100.0 - utilized
fracs = [utilized, free]
axes([left[2], bottom, width, height])
patches = pie(fracs, colors=colors, explode=explode, autopct='%1.f%%', shadow=True)
title('RAM Memory Usage')
legend((patches[0], patches[2]), ('Used', 'Unused'), loc=(0,-.05))

savefig('utilization')
show()
4. Save the file as piechart.py in c:\temp
5. In Windows, go to Start -> All Programs -> Python 2.4 (Enthought Edition) -> IPython Shell
6. Type in "cd c:\temp"
7. Type "run piechart.py" and hit enter
Technorati tags: , ,