Zerynth provides a very powerful feature, which is mixed C and Python programming environment for embedded development.
You might be thinking why should I use this hybrid programming environment?
Well, If you are a developer, you are well aware of the hassle you face when you program a simple peripheral driver, handling different kinds of architectures and registers and datasheets.You also lack the high-level features of python programming language, such as objects and readability and easiness of syntax.
Zerynth hybrid programming environment provides the best features of those two programming languages.You can code your high-level logic in python, utilizing the power of Python programming language, while also code some parts in C, which enables you to also use the speed and efficiency of C programming language. Furthermore, most of the drivers of the peripheral are already made for you to use in python or C programming languages.
Here is an example of blinking an LED in Zerynth using Python and C. This example uses the interrupts on the on-board button, written in C programming language as an example of using this hybrid environment.
In reality, you can do anything in Python like starting a web server or handling multiple threads, while also handling some peripherals in C as PWM or interrupts or anything really!.That’s why I chose to edit an existing project “C Language interface ” example and added “vhalPinAttachInterrupt” to show you how you can access the hardware in an independent way, Also from C.
This project can be made on any of the supported devices by Zerynth, so I picked the DOIT ESP32 DEVKIT V1 Board.
Hardware ConnectionWe are going to use the on-board LED and button.
For more info on the Pin-map of the device, click here:
There is a step-by-step guideline of the tutorial in this Video:
Steps1. Zerynth Setup, Connect and virtualize the device:
If this is your first time using Zerynth studio, Follow this documentation on getting started with Zerynth studio IDE.
2. To give you a brief example of C/Python programming, open the examples bar, open the “C language interface example” then clone it.
3. Once you cloned the example, run the example and open the serial terminal to see the output.The code simply divides to random numbers, the division function is implemented in C in the C-file “cdiv.c”
This simple project illustrates how easy it is to connect your C codes to you python and pass parameters between them easily.
4. Back to our tutorial, create a new project.
5. Copy and paste the sample code provided below into the "main.py" file.
import streamsstreams.serial()# define a Python function decorated with c_native.# This function has no body and will instead call# the C function specified in the decorator.# The source file(s) where to find the C function must be given (cdiv.c)
def blink_led():
pinToggle(LED0)
@c_native("init_interrupt_c",["cdiv.c"],[])
def init_interrupt():
pass#we use @c_native to tell the compiler that we are calling a c function in another file.
init_interrupt() #init. of the interrupt in the C function in "cdic.c" file
pinMode(LED0,OUTPUT_PUSHPULL) #defining the pin of the LED to be output
while True:
try:
print("Blinking LED")
blink_led()
except Exception as e:
print(e)
sleep(500)
6. Create a new file and name it: “cdiv.c”
7. Then copy and paste the sample code provided below into the "cdiv.c" file.
#define ZERYNTH_PRINTF //we define this macro to use "printf"function
#include "zerynth.h" //this is mandatory
void btn_press_callback(int slot, int dir) //this header and the paramters of the function mmust be defined like that according to the docs.
{ //https://docs.zerynth.com/latest/official/core.zerynth.stdlib/docs/official_core.zerynth.stdlib___common_vhal_h.html?highlight=vhalpinattachinterrupt#c.vhalPinAttachInterrupt
printf("interrupt triggered\n"); //just a print to know when the interrupt is triggered.
}
C_NATIVE(init_interrupt_c){
C_NATIVE_UNWARN(); //must be included at the start of a called c function.
vhalPinAttachInterrupt(BTN0,PINMODE_EXT_BOTH,btn_press_callback,TIME_U(0,MILLIS)); //enabling the interrupt for BTN0 on both rising and falling edge, a
printf("Interrupt initialized on BTN0\n"); // the service routine is the function btn_press_callback defined above.
return ERR_OK; //<-- execution ok //the timeout is given as 0 millis.
}
8. Then you have to compile(verify) and Uplink the script into the DOIT ESP32 DEVKIT V1. To verify and uplink the script, press the "up arrow" icon found below:
9. Once the uplink is finished, you will receive the message "Uplink done" in the terminal log of the Zerynth Studio.
Open the console to debug the status of the request; press the "terminal" icon found below to open the console:
Starting from the main python file “main.py”, We are importing the streams library, then defining two functions; “blink led” to blink the led, coded in python, and “init_interrupt” coded in python and calls another C function in “cdiv.c” file.We enable the interrupt on the on-board button on both rising and falling edge, and on triggering it goes to the service routine “btn_press_callback”.
Then in the infinite loop we are blinking the LED each half a second.We have already initialized the interrupt so when the interrupt is triggered it would go to its service routine in the C file “cdiv.c” called “btn_press_callback” function.The documentation has all the information needed regarding the python/C interface.
Although in this tutorial the interrupt routine is impemented in C code, it could be used in python as well. For instance:
defpressed(): print("touched!")
digitalWrite(ledPin, HIGH) # just blink the LED for 100 millisec when the button is pressed sleep(100) digitalWrite(ledPin, LOW)
onPinFall(buttonPin, pressed)
This line in python calls the interrupt routine on falling edge and setting the service routine function to “pressed” function.
What is important is that the low-level interrupt driver is already implemented and ready for you to use in C or in Python!
ConclusionIn just a few lines of code, you have full capability of Zerynth Python/C programming, by which you can use the high level features of Python while also using the efficiency and speed of C programming language.
Happy hacking!
Comments
Please log in or sign up to comment.