One way to customize the colorOS operating system is to add new commands. This feature is at the core of using colorOS in an IoT project, because it provides the ability to write C/C++ code to monitor and control the hardware (e.g., GPIO pins), and add that as new functionality to the rest of the operating system.
Writing New Commands
New commands are created by adding new functions within the file: /addon/addon.cpp
New command functions must adhere to the following prototype:
static bool {name}(COSShell* shell, const char *cmd, const char *opt, const char *param, const char *arg)
{name} - name of the new function
*shell - Pointer to the command shell that invoked the command
*cmd - Pointer to the command issued
*opt - Pointer to a command option (first argument after the command) upper-cased
*param - Pointer to a command parameter (second argument after the command)
*arg - Pointer to an additional parameter (third argument after the command)
If no arguments are provided, opt, param, and arg may all be zero length
Return true on success of the command, or false on failure of the command
Enabling New Commands
New command functions added to /addon/addon.cpp are linked to the command shell by editing /addon/commands.cpp and adding them to the function_map array.
For example, to create the new command GREETING, which says hello to a name passed as the first argument and returns an error if no argument is present, in /addon/addon.cpp add the code:
static bool Greeting(COSShell* shell, const char *cmd, const char *opt, const char *param, const char *arg)
{
CString Greeting;
if (strlen(opt) == 0) {
shell->Output("What is your name?\n");
return false;
} else {
Greeting.Format("Hello %s!\n", opt);
shell->Output(Greeting);
return true;
}
}
And then link the new GREETING command to the shell by adding it to function_map[] in /addon/comands.cpp:
function_map [] = {
{ "GREETING", Commands::Greeting }
};
Then rebuild the kernel image, add it to the SD card, reboot, and type the new command:
OK, greeting
What is your name?
ER!
OK, greeting dave
Hello DAVE!
OK,
Calling Operating System Functions in colorOS
This section describes how to call basic operating system functions in colorOS when writing new commands.
More details to come …