C Coding (Hybrid)
This feature supercharges the functionality of Smowcode allowing users to write codes in C
Creating a C file
Click on the C+ button. A new tab gets created with a text editor.
Note: You would have noticed a header guard at the start of your C file. If it does not exist for you or you accidentally removed it, add it back to prevent build errors.
Creating a reusable node using C
Add the smow_extern keyword in front of the node you want to expose to be reused. Once this is done, a node gets created in the palette that can be used throughout your codebase.
Using existing node in C file
Simply drag and drop the nodes from the palette in the file. All the boilerplate code is generated and inserted in the file.
Advanced Usage
Taking user input as arguments and returning outputs in a reusable node
While creating a reusable node, you can take in arguments just like any other nodes when you are creating a flow. We call these arguments, config props. Similarly, you can also return outputs (known as output props).
To take in config props, simply add the arguments in the function declaration. Similary, adding a pointer as an argument considered an output prop (unless there is x_len property next to it, as we will see in one of the examples below)
Example 1
Let us create a digitalWrite node that takes in the pin_number and pin_level as input
- Create a c file and paste in the following code definition
void digitalWrite(int pin, int level) {
}
- While the cursor is inside the function, search for "write" in the palette and drag the pin write node into the function. This should result in a similar looking code (after formatting and renaming if needed)
void digitalWrite(int pin, int level) {
smow_gpio_write_config_t pin_write_config = {.pin_number = 2, .level = 1};
smow_gpio_write_init( &pin_write_config );
smow_gpio_write( &pin_write_config, NULL );
}
- Update the pin_write_config struct to take pin_number and level from the arguments passed in the function
void digitalWrite(int pin, int level) {
smow_gpio_write_config_t pin_write_config = {.pin_number = pin, .level = level};
smow_gpio_write_init( &pin_write_config );
smow_gpio_write( &pin_write_config, NULL );
}
-
Add the smow_extern keyword in front of the function to make it available in the palette
-
Create a flow in the main tab as follows
-
Double clicking on the node on canvas will open the side tray where the config props will be displayed
-
You can try out this template by clicking here: C Coding in Smowcode
Example 2
Let us create a arrSum node takes an array and length as input and calculates the sum of all elements
- Create a c file and paste in the following code definition
void arrSum(){
}
- Since we are going to take two inputs: arr and len. We will add them as arguments
void arrSum(int32_t* arr, uint32_t len){
}
- As mentioned earlier, pointers represent output for us. So, for Smowcode to interpret arr as a config prop of data type array_int32, we need to add an additional argument, arr_len of any numeric datatype (e.g. uint32_t <arr_name>_len) right next to where arr is declared
void arrSum(int32_t* arr, uint32_t arr_len, uint32_t len){
}
- To add an output prop sum we add an int pointer as argument in the function
void arrSum(int32_t* arr, uint32_t arr_len, uint32_t len, int32_t *sum){
}
- Update the code to actually compute the sum and give an output
void arrSum(int32_t* arr, uint32_t arr_len, uint32_t len, int32_t *sum){
*sum = 0;
for (uint32_t i = 0; i < len; i++){
*sum += arr[i];
}
}
-
Add the smow_extern keyword in front of the function to make it available in the palette
-
Create a flow in the main tab as follows
-
Double clicking on the node on canvas will open the side tray where the config props will be displayed
Note: After updating the code and parameters, if the new config props are not updated while using the nodes, simply remove the smow_extern keyword from the function and add it back. This will reload the reusable node with the updated config and output props.
Setting default values
You might have observed that, whenever a reusable node is created, it has a pencil icon floating over it.
Clicking on it, opens up the lib node editor. Here you can add default values for your config props and output props.
Supported data types
Currently, we support the following data types as config and output props:
- *int*_t
- bool
- text ( char* )
- array_* (e.g. uint8_t*, char**)
Using the Smow Exposer tab
You might have observed the Smow Exposer tab opening up when you drag and drop any palette nodes into the code. This tab helps Smowcode to fetch dependencies so that the necessary libraries and functions are available to your code.
In case you no longer need to use a node, you can simply delete it from the tab
Similarly, in case you accidentally remove a node, dragging and dropping the node from the palette to your code editor (and then remove the code) will add the node to the tab.