Shop Learn
Sunday, Fun Day: Port Arduino Library to Spin — Parallax Forums

Sunday, Fun Day: Port Arduino Library to Spin

JonnyMacJonnyMac Posts: 7,588
edited 2019-12-02 07:49 in Propeller 1
I'm often asked by friends who tinker with the Arduino family, "Can my code run on the Propeller?"

I have ported lots of Arduino code to Spin, and as a project for a book I'm working on, decided to port the LedControl library (for MAX7219s) this afternoon. I already have a low-level library for the MAX7219 (jm_max7219.spin), but LedControl supports chained devices and I really wanted to do this as an exercise.

It turned out to go pretty smoothly. I started with a near line-for-line translation of Arduino C to Spin. In a few areas I used things like bytemove() and bytefill() for efficiency, along with left-shift for power-of-2 multiplication (which the original author chose not to do). Once done and tested I made a couple small modifications and additions because I think the library needs them.

So... if you're looking for a multi-device MAX7219 library, I hope you'll give this a try and let me know if you find any problems.


  • Jon,
    Thanks for this interesting post.
    Arduino has many more device libraries than Prop1/SPIN, and I have often thought about doing conversions from Arduino to SPIN.
    Do you have any tips or ideas to share when doing these conversions, or is it better to just jump in there and get going line-code by line-code?

  • Jon,

    This looks great! Will give it a whirl tomorrow night.

    Can you give us a hint on the book?

  • JonnyMacJonnyMac Posts: 7,588
    edited 2019-12-02 17:28
    Do you have any tips or ideas to share when doing these conversions, or is it better to just jump in there and get going line-code by line-code?
    I always say, "Jump in!" because you will miss out on some fun if you don't!

    In this particular case there was no feature of a typical Arduino that the Propeller cannot do (without support hardware, e.g., analog in), so that made it easy. I opened the source files in GitHub and created a shell outline with matching names, using the .h file as a guide. In C++, objects instances are created differently than with Spin, so the method handling the instantiation is called start() by Spin convention. In the original code, the spi_transfer() function is not available through the interface so that became a pri(vate) method this object. There is no built-in shiftout() function in Spin so I coded one that matched the behavior of the Arduino.

    After that I did do a line-for-line translation (from the .cpp file), replacing some C/C++ code with Spin functionality that is more efficient. Here are some examples:
    ... in C becomes:
      longmove(@mosi, @dpin, 3)
    ... in Spin. Remember that Spin parameters are always longs.

    Here's another:
      for(int i=0;i<64;i++) 
    ... translates to:
      bytefill(@status, 0, 64)

    Once everything was working I looked for opportunities to make the code more readable. For example
      spiTransfer(addr, digit+1,v);
    The +1 is not obvious to a person that doesn't yet have detailed knowledge of the MAX7219. In my version that line is:
      spi_transfer(addr, OP_DIGIT0+digit, bits)
    The +1 is for the start of the digit registers and there was a named constant for it -- I simply put it to use to make the code a little easier to understand.

    Finally, I added the fill() method and then connected the original clear() method to it. I also updated the character maps table to make it easier to read and modify, as well as expand the number of characters available. Of course, one cannot do many characters on 7-segment displays, but the original code would not let me spell out "OPEn" and "CLOSEd" completely (as I could with my jm_max7219 object).

    I don't think that anything I did violates the spirit or intent of the original code, and, of course, I credited the original author and provided links to the original source. We should all give credit where it's due, especially when so many work very hard providing MIT-licensed code for the rest of us.

    Have fun! (I always do)
Sign In or Register to comment.