Arduino + Cython !/?

1,007 views
Skip to first unread message

Kyle Lawlor

unread,
Nov 6, 2015, 12:41:01 AM11/6/15
to cython-users
Hi, all.

I am working on project involving Arduino and Cython, purely for fun. My goal is to have a tool that allows me to write code in a Python-like way in order to upload a program onto the Arduino (or really any micro-controller). There are a couple of projects similar to this idea, but they involve a serial protocol called Firmata. As of now my approach is attempting to wrap the C/C++ code that Arduino uses.

For those who don't know, a typical Arduino IDE session goes like: write some C-like code which contains a setup(){} block and a loop(){} block. Then using the Java IDE you click to upload the sketch onto board (which behind the scenes converts to actual C/C++, creates a hex (machine language) file and is uploaded to the micro-controller via avrdude).

I only started learning C a week ago. So there is a lot that I do not understand about the language. I have been working with Python for about a year now. I just started learning how to work with Cython also about week ago. I would like to know what some more experienced Cython-users think about my project. I would like to use the Arduino "C-like" syntax, but in a Python environment. I think that Cython could be used directly to generate this sort of C++ code (LED blink):

#include <Arduino.h>

void setup(){
    pinMode
(13, output);
}

void loop(){
    digitalWrite
(13, HIGH);
    delay
(1000);
    digitalWrite
(13, LOW);
    delay
(1000);
}

Overall I like the Arduino syntax. Below is a rough sketch of what I see being a Blink.py.

What I would like to get from responses are ideas for how I can implement this. Would it be best to wrap some of the C-code from the Arduino libraries? Or am I better off just calling Cython from Python to write out a C-file? Or I am better off doing something completely different?

I think I have been over-thinking this, so any advice and comments will be appreciated. I will go into more detail about what I have tried so far, based off of what I hear back. I don't want to start getting into details unless it will be useful.

Looking forward to hearing some responses.

Kyle
       
import arduino #import the library

def setup():
    arduino
.pin_mode(13, arduino.output)

def loop():
    arduino
.digital_write(13, arduino.high)
    arduino
.delay(1000)
    arduino
.digital_write(13, arduino.low)
    arduino
.delay(1000)

arduino
.upload() #write a file to C, probably using Cython in the background and upload via avrdude.


















Robert Bradshaw

unread,
Nov 6, 2015, 1:07:36 AM11/6/15
to cython...@googlegroups.com
From what I understand, you want your input to be Python source code,
and your output to be (C-ish) Arduino source code that you can upload
(after a compile/assemble/etc. step) to the board. I don't think that
Cython will help much with this--the C code it produces is not close
to being "restricted C," is probably on the large side, but most
importantly relies on a full Python interpreter that it can call in
(which you probably don't have on arduino, or you'd just be running
Python code natively there).

Best of luck hacking though!

- Robert

Michael Wilber

unread,
Nov 6, 2015, 11:12:35 AM11/6/15
to cython-users
If you just want a nice language to use, have you considered Lua at all? The interpreter is much more lightweight and integrating with C is a snap.

(Disclaimer: I'm not sure if it's possible to embed Lua on an Arduino)

Kyle Lawlor

unread,
Nov 6, 2015, 11:12:40 AM11/6/15
to cython-users
Hi, Robert.

Thanks for the response. I will carefully consider your advice.

What do you think of wrapping the Arduino C/C++ code? I've tried to wrap this file called Arduino.h, which defines the basic arduino syntax. See here for Arduino.h. Here is my pyx file. I just wanted to do the bare minimum, hence the `pass` statement in the extern block (to figure out how to handle the #include statements throughout). Here is the setup.py I use to build. Here are the errors I am getting after trying to build. The main error seems to be "error: conflicting types for ‘intptr_t’". Given my background, I'm pretty stumped by this.

Anyway, my premise for wrapping the library is that if I can import parts of the library in python, it will be easier to write out a C++ file to upload, from Blink.py for example.

Best, Kyle



On Friday, November 6, 2015 at 1:07:36 AM UTC-5, Robert Bradshaw wrote:

Robert Bradshaw

unread,
Nov 6, 2015, 1:47:14 PM11/6/15
to cython...@googlegroups.com
On Fri, Nov 6, 2015 at 7:21 AM, Kyle Lawlor <klawl...@gmail.com> wrote:
> Hi, Robert.
>
> Thanks for the response. I will carefully consider your advice.
>
> What do you think of wrapping the Arduino C/C++ code? I've tried to wrap
> this file called Arduino.h, which defines the basic arduino syntax. See here
> for Arduino.h. Here is my pyx file. I just wanted to do the bare minimum,
> hence the `pass` statement in the extern block (to figure out how to handle
> the #include statements throughout). Here is the setup.py I use to build.
> Here are the errors I am getting after trying to build. The main error seems
> to be "error: conflicting types for ‘intptr_t’". Given my background, I'm
> pretty stumped by this.

You're including multiple files that define intptr_t, You'd have to
look at the generated code to see where.

> Anyway, my premise for wrapping the library is that if I can import parts of
> the library in python, it will be easier to write out a C++ file to upload,
> from Blink.py for example.

If the code compiled what would you do with the resulting .so file (or
.dll on windows)? You can't actually run it on Arduino without
building the Python interpreter itself to run on Arduino, and Cython
extensions can't be used without that, and I'm willing to bet the
"micro-Python" projects to run Python on embedded devices are not
fully CPython compatible. And as I understand it the C/C++ dialect
supported by Arduino is somewhat restricted.

If your function uses absolutely no Python (e.g. cdef nogil) you could
possibly run Cython on it and try copying the relevant snippet of C
out of the generated C file and plugging that into an Arduino "C" file
and run from there, but I just don't think Cython is the right tool
for the job unless/until CPython also runs on these chips (at which
point it will be quite useful).
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cython-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Chris Barker - NOAA Federal

unread,
Nov 6, 2015, 6:51:33 PM11/6/15
to cython...@googlegroups.com
I'm willing to bet the
> "micro-Python" projects to run Python on embedded devices are not
> fully CPython compatible.

Yup.

If you want to write Python for a microcontroler, why not get a micro
Python board and get native Python?

-Chris

Kyle Lawlor

unread,
Nov 9, 2015, 9:13:14 AM11/9/15
to cython-users
If you want to write Python for a microcontroler, why not get a micro
Python board and get native Python?

I plan to.

You're including multiple files that define intptr_t, You'd have to
look at the generated code to see where.

Thanks for the suggestions. I might try debugging this further.

If the code compiled what would you do with the resulting .so file (or
.dll on windows)?

I realized as well this was not going to be very useful. I think for the tool I am looking for with Arduino, I am probably better off just building a helper class, i.e. piping the upload process.

If your function uses absolutely no Python (e.g. cdef nogil) you could
possibly run Cython on it and try copying the relevant snippet of C
out of the generated C file and plugging that into an Arduino "C" file
and run from there, but I just don't think Cython is the right tool
for the job unless/until CPython also runs on these chips (at which
point it will be quite useful).

So Cython could be useful on these micro-python boards. Presumably it is CPython that is being run on them. I will need to check into this some more. In which case, it might be fun and useful to wrap the Arduino library.

Any further comments, suggestions, corrections are appreciated.

-Kyle

Chris Barker

unread,
Nov 9, 2015, 5:59:38 PM11/9/15
to cython-users
On Mon, Nov 9, 2015 at 6:13 AM, Kyle Lawlor <klawl...@gmail.com> wrote:
 I just don't think Cython is the right tool 
for the job unless/until CPython also runs on these chips (at which
point it will be quite useful).

So Cython could be useful on these micro-python boards. Presumably it is CPython that is being run on them.

nope. microPython:


is a its own implementation of python -- written in C, yes, and for all I know borrowing code from the "normal" implementation, but it is its own thing, and I don't think Cython will work with it. 

But as far as I can tell, it can use "native" simple data types (integers anyway), and get pretty fast code for those non-dynamic bits.

On the other hand, if you want to use  general purpose little machine like the Raspberry Pi -- Cython could be very nice there.

-CHB

 

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris....@noaa.gov

Kyle Lawlor

unread,
Nov 9, 2015, 10:48:12 PM11/9/15
to cython...@googlegroups.com
is a its own implementation of python -- written in C, yes, and for all I know borrowing code from the "normal" implementation, but it is its own thing, and I don't think Cython will work with it.

I see, thanks for clarifying.


 On the other hand, if you want to use  general purpose little machine like the Raspberry Pi -- Cython could be very nice there.

Do you have something in mind? Or do you mean just as a way to speed things up ?

--

---
You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/fQlrO_4B3UI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.

Chris Barker - NOAA Federal

unread,
Nov 10, 2015, 11:49:07 AM11/10/15
to cython...@googlegroups.com

 On the other hand, if you want to use  general purpose little machine like the Raspberry Pi -- Cython could be very nice there.

Do you have something in mind? Or do you mean just as a way to speed things up ?

That's it. The Pi provides a full OS and regular old cPython interpreter. But it's still pretty constrained, so Cython could be helpful there.

CHB

You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages