My Top 5 Python Videos

The Python community continues to fascinate me. The amount of work (code) that is being shared and the breadth of the applications supported  keeps expanding. To try and learn something new every day I read quasi-regularly the postings on Planet.Python.org which re-publishes blog posts from a large collection of authors world wide.

Recently I added to my browser reading list the pyvideo.org link. This is a large archive offering free access to thousands of videos from all major Python-related conferences world wide since at least 2009. You can search videos by keyword (as tagged by the authors/presenters) or by the author/presenter own name.

I can spend hours browsing through this material just as I do with TED ! In fact I wish they had a  playlist feature (much like TED’s web site)  so that different curricula could be suggested and common topics would be grouped by users/experts to guide us through the maze.

Here is my little contribution: my Top 5 video on  “Python Under the Hoods”, understanding how the VM works, byte codes, and introspective powers of the language:

Posted in Python | Comments Off on My Top 5 Python Videos

Compiling Python Code with PyPy

PyPy

I have been watching recently some interesting videos recorded from the last few editions of the PYCON US conference. In particular my passion for (programming) languages  attracted me to those from Alex Gaynor ( a VERY young and talented programmer / presenter) and other members of the PyPy project.  

Now what I had previously understood about PyPy was that it is basically a fast interpreter and JIT compiler for Python code, written in Python. This was intriguing in itself, the idea of using the advanced features of Python to write an entire interpreter/compiler with it  is quite brilliant, but Alex literally opened my eyes with this presentation (video) where in the short span of 45 minutes he develops  a  (stripped down) Javascript ( could be any generic algol-like language) virtual machine.

In  order, Alex develops a lexer, a parser (based on a simple bnf grammar description provided in a txt file), an AST traversal byte code generator (compiler), an interpreter (VM) and he runs it too.

The cleanness of the code and simplicity with which Alex proceeds slide after slide is remarkable. In fact I have no difficulty to compare his work with the most inspiring chapters from Nicklaus Wirth book “Algorithms + Data Structures = Programs”, where he develops a simplified compiler in PASCAL (link). Sign of the times, Alex work is available in video and his code is on Bitbucket rather than on paper.

Most importantly perhaps, Alex brought home the point that Python and the PyPy toolchain (all the libraries developed by the team to create the new Python compiler) are not limited to Python, but can be used very effectively to create fast virtual machines (and JIT compilers) for other languages too. The team has already developed  Ruby(!), Schema, Javascript(full), Prolog, SmallTalk, Io and even attempted a “Whole System”  virtual machine (aka an emulator) for the GameBoy gaming platform.

So the PyPy tools could help us develop (rapidly) a Python compiler that generates (JIT) code for any processor ( x86 was the main target but other architectures are in development) including possibly a MIPS or perhaps even a more humble PIC core!?

I am (day) dreaming about a world of efficient embedded applications written with the clarity and expressive power of the highest level programming languages… (brought to you by PyPy).

Posted in Python | Comments Off on Compiling Python Code with PyPy

M-Stack – An open source USB stack for PIC16, PIC18, PIC24 and PIC32

M-Stack (link) is a new, well-documented, free and open source implementation of a USB stack for Microchip PIC platforms developed by Signal 11. That is Alan Ott’s creation, a name that many of you will immediately associate to the excellent cross platform HIDAPI libraries.

The software is dual-licensed under the LGPL version 3 and the Apache License version 2.0. It may be used without royalty payment in both commercial and open hardware projects so long as the conditions of either the LGPL or the Apache License can be met.

(Commercial licenses are available for purchase for companies and projects which cannot or wish to not comply with the terms of either the LGPL or the Apache License.)

 

Posted in PIC24, PIC32 | Comments Off on M-Stack – An open source USB stack for PIC16, PIC18, PIC24 and PIC32

An Open Source Toolchain How-To Guide for the PIC32

gnu logo

I have always known that the MPLAB C32 compiler first and MPLAB  XC32 compiler later were both projects based on the GCC platform and therefore shared a common Open Source DNA.

What I could not imagine was how easy it can be to take the latest and greatest revision of the  original GCC compiler and target it for the PIC32 (mips-elf) to obtain a brand new (fully open source) compiler.

What changed my mind is the work of Bruno Basseto on his freshly posted blog/wiki site:

http://wise-ware.org/wiki

His explanations are crystal clear and reduce the potentially treacherous subject into a small number of steps that I can almost pretend to understand.

I did try his method and in a matter of 20 minutes I had a brand new  compiler (on my MAC) cranking out mips code. (Windows and Linux users will find dedicated instructions as well).

Now, the compiler is not everything. You will still need linker scripts, start up code  and a set of header files to handle your desired target PIC32 device and its peripherals, but that seems all to be easy if not fun work, when you follow Bruno’s lead that is…

Clearly following this path means entering totally new and uncharted territories, but isn’t that what I was looking for when Exploring the PIC32?

Highly entertaining, highly educational, thanks Bruno!

Posted in PIC32 | Comments Off on An Open Source Toolchain How-To Guide for the PIC32

Eclipse for the PIC32

Another couple of interesting links for the PIC32:

Using Eclipse for PIC32: http://blog.hjf.com.ar/2013/01/developing-for-pic32mx-using-eclipse/

GDB Proxy for Pickit2 and PIC32: https://code.google.com/p/ejtagproxy/wiki/About

 

Enjoy

Posted in PIC32 | Comments Off on Eclipse for the PIC32

Unix for PIC32

BSD

Here is a link (http://retrobsd.org/wiki/doku.php) to a true port of the Unix (BSD) OS to the PIC32 (MIPS)!

This code will work on a humble MIPS M4K core that you can find inside the PIC32MX series devices. It will work with 128K of RAM which means we are talking PIC32MX7  at least.

Lots of common hardware development boards (Explorer 16, Chipkit…) supported too..

Posted in PIC32 | Comments Off on Unix for PIC32

About Distributed Version Control Systems

Working on the PyMite project recently, I have been exposed for the first  time to  Mercurial a distributed version control system.

That is when I was just starting to feel confident that I could handle a more traditional VCS such as Subversion.

The paradigm shift is considerable, and I must admit I struggled at first to get my mind around it, but it is clear, so much of modern Open Source development today revolves necessarily  around DVCSs.

Be it Mercurial or Git and their respective hubs (BitBucket, GitHub…), there is no point resisting it.

Only this morning I stumbled onto an excellent post on Dr.Dobbs’ web site (link) that helped me put things in perspective,  highly recommended…

P.S. Mercurial is supported directly by MPLAB X project manager

Posted in Python, Tools | Comments Off on About Distributed Version Control Systems

New Mikromedia, new (better) Audio Player (VS1053)

VS1053

A short while ago I wrote about a small module that I wrote to support the Audio Codec chip present on the Mikromedia (PIC24) boards (link).  What I had not noticed though was that I had been playing at home with a beta version of the board (provided very early on by the Mikroe team) while the production release rev 1.1 had departed just a little bit from the original design and used only one different component. Do you want to try and  guess which one? Of course, the audio codec!

The new Mikromedia boards use the much improved, omni-capable, universal… VS1053 from VLSI technology. You can now play not only MP3 files, but also WMA, WAV and even Ogg Vorbis files, no sweat!

The device data sheet, states that the communication interface is absolutely backward compatible so when I discovered the change I felt absolutely confident it was going to be a minor update if any was required at all!  Yeah right…

To spare you the excruciating 6 hours of debugging that followed, here are the key lessons (re-)learned:

Continue reading

Posted in PIC24, PIC32, Tools | Tagged | Comments Off on New Mikromedia, new (better) Audio Player (VS1053)

Unexpected (Positive) Side Effects of using Python: Cross Platform Simulation

Yes or No dialog box emulated

When talking of side effects, more often than not, we are referring to the bad ones. This is especially true when they are involuntary and unexpected. But here is the story of a good one, a side effect of using Python to script a Graphical User Interface in embedded control applications!

As per my previous post, I have re-written from scratch some kind of replacement for the GOL lib (Graphical  Object Library) using Python from the primitives up. The new module, baptised EZ.py, appears to run on a PIC32MX7 board (PIC32MX7-MMB from Mikroe) just as fast and fluid as the original (C) library. This is not too much of a surprise as eventually all the heavy lifting (graphical primitives) are exactly the same ones (MAL Graphics Lib) and Python is doing just what it is design to, scripting, that is chaining activities together to produce the same presentation and responses to user input.

This is all nice and dandy, but the real fun begins when you realise that the same exact code “could” ran on any PC, MAC or Linux box producing the same exact output. The distance between “could” and “will” is actually very small in this case, as all we need is suitable “replacement” for the embedded primitives (MAL) with something more standard in the python world. And by “replacement” I mean, binding, that is wrapping some code around another library, something that, I am starting to appreciate, is very easy to do in Python.

My alternative primitives of  choice: pygame.

Once more the effort was extremely limited in time (and number of code lines, less than a hundred) and voila’:

4x3 keypad emulated 5x4 keypad emulated

These are screen captures from my Mac where a 320×240 (QVGA) size window is now used to emulate the screen of the PIC32MX7-MMB board. Since I don’t have a touch screen capable model, I used pygame primitives to replace the mouse for the touch screen input.

This is great for prototyping a user interface and debugging it. In fact it is so easy and addicting that I wonder if it does not make a tool such as GDD unnecessary  or simply obsolete!?

 

 

Posted in Languages, PIC32 | Tagged , , | Comments Off on Unexpected (Positive) Side Effects of using Python: Cross Platform Simulation

Python Expressiveness

While learning a new language you will notice how the often enthusiastic literature available dispenses adjectives with quite some generosity. Apparently every new language out there claims to be more “expressive” than the previous. This is an easy claim when comparing to plain C, which nowadays ranks among the high level languages barely above the level of raw assembly and just a notch higher than hand coded binary.

Few have made a real effort to verify such claims and tried to establish a metric (link, link) but there is nothing like trying yourself, using possibly a test case that truly matters to you.

In my recent experimentations with Python (on a PIC), I witnessed with my eyes  what Python expressiveness truly can be.

I have been playing with the MX7 Multimedia Board from Mikroelektronika and wanting to take advantage of the QVGA display and touch screen, I was looking into porting the well known GOL libraries (a GUI development library part of the larger free graphics package from Microchip, aka MLA Graphics).

After a quick attempt at binding the existing library to Python, I realised that it was an intrinsically lost cause. Graphical user interfaces scream for object oriented design and that is exactly what the MLA-GOL lib attempts to deliver  albeit sticking to an artfull, yet plain, C coding technique reminiscent of Windows 3.1 (circa 1995) style. This has the enormous advantage of being portable across all microcontroller architectures (8/16 and 32-bit) using a multitude of C compilers (at least five at the last count) and covering a product portfolio of approximately 500 devices or so… short of a miracle!

But once I had made the leap to Python, and the larger processors (RAM rich) required to run it, all that value was lost, in fact it had become a dead weight. A complete re-write was in order, using Python from the ground up ( meaning from the MLA Graphcis primitives up).

EZ.py

Off I went to develop a small Python module that I decided to call EZ.py, inspired by somewhat similar efforts in the Python community better known as EZGUI and … friends.

Long story short, there are two numbers that summarise well the outcome of my effort:

  1. It took me only a couple of nights work to flesh out the basic functionality
  2. The total code size turned out to be ridiculously small

While I struggle to quantify the first, sorry I don’t keep a fine count of my free time allocation, especially the dark hours, but my son goes to bed at around 9pm and I never work past midnight …

I can measure the second one very well: that is 300 lines of code in Python.

By comparison the basic set of GOL widgets does cover some 8,700 lines of code (semicolons, excluding header files and widgets that don’t have a parallel in my code)

That would set the expressiveness meter at a 20:1 ratio or more but the case is admittedly an extreme one. Here some of the reasons that I could think of, that could help explain the extraordinary performance:

  • Python does memory management and handles “lists” natively
  • Python Object Oriented features allow for inheritance mechanisms that reduce enormously code repetition in a GUI widget design
  • Python has dynamic typing which eliminates a lot of the “type casting” clutter present in GOL

Quiz: How many lines of code does it take to implement the below dialog box using GOL and respectively Python?

Yes or No

Answer: 1 line of code in Python (using the EZ module), lost the count in C using GOL (guessing a couple hundreds, probably you are going to need the GDD too in order to help you place elements around in a nice and proportional way)

 

Posted in PIC32 | Tagged , , | Comments Off on Python Expressiveness