6. Adding Custom Commands

CodeLess offers the option to not write firmware for the DA14531/DA14585/DA14586 platform, but you may want to add your own commands or modify existing ones to better serve your application. This section will demonstrate how to add your own flavor to CodeLess.

Before jumping into that, you need two things

6.1. Overview of implementation

With all the necessary understanding, we will see how to get going with creating very own Custom Commands.

Overview:

  • We will implement a command that can serve as a really primitive calculator (Supports only addition of two to five operands – all integers)
  • The response to the command will be the sum of the two to five operands or an error if the number of arguments is less than 2 or more than 5
  • We will allow input of operands in hexadecimal or decimal format. The result will be provided only as decimal in the response
  • We will name our new commandAT+ADD

An example of the new command: AT+ADD=1,2,3,4 (should return 10)

With the design specification in place we can start implementation.

6.2. Implementation

Open the folder containing the CodeLess SDK and go to the location -> v_6.380.x.x -> projects -> target_apps -> codeless -> codeless_5xx ->Keil_5, double-click on codeless_585.uvproj to open the project in Keil development environment.

We will add our command right after the USE_AT_BAUD command implementation. The implementation is simplified to be able to add custom commands by writing code in two files only, namelyuser_at_commands.handuser_at_commands.cas shown below.

1. user_at_commands.h

定义USE_AT_ADD启用的使用custom addition. You can include the code at the end of all the other definitions, i.e, after #define USE_AT_BAUD

...#define USE_AT_BAUD//Already part of the SDK#define USE_AT_ADD//CUSTOM

Also include in the Set of supported CodeLess at command set.

...#ifdef USE_AT_BAUD//Already part of the SDKUSE_AT_BAUD,/**< Set or request the current pin code */#endif#ifdef USE_AT_ADDAT_ADD,/**< Addition of up to 5 numbers *///CUSTOM#endif

2. user_at_commands.c

Now lets add our custom AT command in the AT commands jumptable as shown below.

...#ifdef USE_AT_BAUD{AT_BAUD,"BAUD",4,user_at_baud,0,1,RPLY_MAX_SIZE},#endif#ifdef USE_AT_ADD{AT_ADD,"ADD",3,user_at_add,2,5,RPLY_MAX_SIZE},//CUSTOM#endif

The description of the syntax is depicted in the below figure. Each custom command you create need to follow this syntax.

_images/atparam.png

Figure 39Syntax of the AT command

Lets define the function that does the job of addition for us, right after the USE_AT_PIN definition.

_images/customadd.png

Figure 40Adding Custom Add AT code

#ifdef USE_AT_ADDvoiduser_at_add(结构体at_cmd_params_t*arg,char*reply_string){// Initialize the sumuint32_tsum=0;for(uint8_ti=0;i<arg->arg_count;i++)sum+=ahtoi32(&arg->cmd_buffer[arg->arg_index[i]]);arch_sprintf(reply_string,"%d",sum);arg->success_flag=true;}#endif

6.3. Execution

The steps to execute the build, selecting the target and running the code is explained inhere. We can now build the code, upload it to target, and test the new command using any terminal.

_images/testrun.png

Figure 41Example output in terminal