A new and upcoming project is emerging and it has fangs. Ever wanted an easy to use, high level, multi-threaded language on their micro-controller? Well it is now a reality thanks to the good people at Viper . What makes it even more exciting is that is runs on the Flip n click like they were born to be together.
Installing Python on the Flip n click
Example
Let's look at the reasons one would want to run something other than C on their micro-controller:
- Python is multi-threaded - if you want multiple tasks running at the same time with C you will need an RTOS or scheduler
- Python is easy to learn - Look ma, no pointers
- Python has memory management built-in - Ah, you mean I can't use the memory manager?
- Python can access C files - but C can't access Python
To prove its case, here is an example of running multiple tasks at the same time.
################################################################################ # Threaded LEDs # # Created: 2016-01-30 23:19:09.376430 # ################################################################################ import streams # create a serial port with default parameters streams.serial() LEDA=38 LEDB=37 LEDC=39 LEDD=40 pinMode(LEDA, OUTPUT) pinMode(LEDB, OUTPUT) pinMode(LEDC, OUTPUT) pinMode(LEDD, OUTPUT) # Define a function to be used in the various threads. # Parameters are passed to the function when the thread is created and then used in the thread loop. # To be continuously executed by a thread a function requires an infinite loop, # otherwise when the function terminates the thread is closed def threadfn(number, delay, led): while True: led_state = digitalRead(led) if led_state is 0: led_state = 1 else: led_state = 0 digitalWrite(led, led_state) print("I am the thread", number) print("I run every:", delay, "msec") print() # just add an empty line for console output readability sleep(delay) # create the various threads using the same function but passing different parameters thread(threadfn, 1, 500, LEDA) thread(threadfn, 2, 1000, LEDB) thread(threadfn, 3, 1300, LEDC) thread(threadfn, 4, 800, LEDD)
We start by importing the streams library that will be used to communicate through the serial port. streams.serial()
Then we configure the Flip n clicks LED pins to be outputs:
LEDA=38 LEDB=37 LEDC=39 LEDD=40 pinMode(LEDA, OUTPUT) pinMode(LEDB, OUTPUT) pinMode(LEDC, OUTPUT) pinMode(LEDD, OUTPUT)
Then create a function that will be used in each of the threads. In this one we are going to be toggling an led that was passed to it as well as output text on the serial stream:
def threadfn(number, delay, led): while True: led_state = digitalRead(led) if led_state is 0: led_state = 1 else: led_state = 0 digitalWrite(led, led_state) print("I am the thread", number) print("I run every:", delay, "msec") print() # just add an empty line for console output readability sleep(delay)
The sleep(delay) at the end of the function will put the thread to sleep for the requested time in ms.
Last step is to create 4 separate threads, 1 for each of the LEDs.
# create the various threads using the same function but passing different parameters thread(threadfn, 1, 500, LEDA) thread(threadfn, 2, 1000, LEDB) thread(threadfn, 3, 1300, LEDC) thread(threadfn, 4, 800, LEDD)
Each thread is given a function, a ID number, a delay, and the LED that we want to toggle.
Summary
While the area of running python on an embedded system is still emerging, there is no doubt to its benefit. Ease of use, multi-threaded, real-time, and memory management are just a few of the arguments in favor of python. Some of the things that needs to be addressed are libraries and number of processors it will run on. Even with that, Viper is ready for your Flip n click and is ready for your project with 4 click sockets available for expansion.