Prototype
Math2Var_I32 (r1, r2, funcId)
Description
Math on two int32 variables.
r1 = operation(r1, r2);
Return
Nothing
Code Example
The following code example uses the Math2Var_I32 function.
The SMK900.evi file used for this example can be downloaded at the bottom of this page.
/* Example Code
* Math2Var_I32
* Calculator for atan2: https://planetcalc.com/7954/
*
*
* get buffer indexes r1, r2 and funcId
* set buffer in memory
* operate
* if params used
* return result of funcId on 32 bits
* else
* return 0x00 (operation not ok) or 0x01 (operation ok) on 8 bits
*
* example VMExecuteCmd:
* cmd: |r1 |r2 |funcId|buffer
* 0xfb 0x0e 0x00 0x0c 0x00 0x0e [0x?? 0x?? 0x?? = macAdress] 0x00 0x00 0x04 0x00 0x00 0xFF 0xFF 0xFF 0xFF 0x0A 0x00 0x00 0x00
* rsp: 0xfb 0x08 0x00 0x2d 0x00 0xe1 0x1e 0x09 0x00 0x00 0x00
*/
#include "SMK900.evi"
#define SENSORCODE 0x01
function exec_aircmd(){
local rxLen;
local useParams;
local r1,r2,funcId,val0,val1,val2,val3;
local i;
local result;
rxLen=GetAirBuf(0, 0, 20);
if(rxLen>=14){ // 1 byte for paketID + 13 bytes of payload (5 bytes for params and 8 bytes for buffer)
useParams=true;
r1=GetBuffer_16(1);
r2=GetBuffer_16(3);
funcId=GetBuffer_U8(5);
for(i=6;i<rxLen;i++){
SetBuffer(i-6,GetBuffer_S8(i),1);
}
}else{
useParams=false;
r1=0;
r2=4;
funcId=0; // add
val0=0xFFFF; // -1
val1=0xFFFF;
val2=0x000a; // 10
val3=0x0000;
SetBuffer_16(0,val0);
SetBuffer_16(2,val1);
SetBuffer_16(4,val2);
SetBuffer_16(6,val3);
}
Math2Var_I32(r1,r2,funcId);
if(useParams){
SetBuffer_16(0,GetBuffer_16(r1));
SetBuffer_16(2,GetBuffer_16(r1+2));
Send(4);
}else{
result = 0;
if(GetBuffer_16(2)==0x0000&&GetBuffer_16(0)==0x0009){ //add(-1,10)
SetBuffer_16(0,val0);SetBuffer_16(2,val1);
SetBuffer_16(4,val2);SetBuffer_16(6,val3);
funcId=1;
Math2Var_I32(0,4,funcId); // substract
if(GetBuffer_16(2)==0xFFFF&&GetBuffer_16(0)==0xFFF5){ //substract(-1,10)
SetBuffer_16(0,val0);SetBuffer_16(2,val1);
SetBuffer_16(4,val2);SetBuffer_16(6,val3);
funcId=2;
Math2Var_I32(0,4,funcId); // multiply
if(GetBuffer_16(2)==0xFFFF&&GetBuffer_16(0)==0xFFF6){ //multiply(-1,10)
SetBuffer_16(0,0xFFEC);SetBuffer_16(2,0xFFFF);
SetBuffer_16(4,val2);SetBuffer_16(6,val3);
funcId=3;
Math2Var_I32(0,4,funcId); // divide
if(GetBuffer_16(2)==0xFFFF&&GetBuffer_16(0)==0xFFFE){ //divide(-20,10)
SetBuffer_16(0,val2);SetBuffer_16(2,val3);
SetBuffer_16(4,0xFFFD);SetBuffer_16(6,0xFFFF);
funcId=4;
Math2Var_I32(0,4,funcId); // modulo 0x00 0x00 0x04 0x00 0x04 0x0a 0x00 0x00 0x00 0xFD 0xFF 0xFF 0xFF
if(GetBuffer_16(2)==0x0000&&GetBuffer_16(0)==0x0001){ //modulo(10,-3)
SetBuffer_16(0,val0);SetBuffer_16(2,val1);
SetBuffer_16(4,0x0002);SetBuffer_16(6,0x0000);
funcId=5;
Math2Var_I32(0,4,funcId); // shiftleft 0x00 0x00 0x04 0x00 0x05 0xFF 0xFF 0xFF 0xFF 0x02 0x00 0x00 0x00
if(GetBuffer_16(2)==0xFFFF&&GetBuffer_16(0)==0xFFFC){ //shiftleft(-1,2)
SetBuffer_16(0,0xFFEC);SetBuffer_16(2,0xFFFF);
SetBuffer_16(4,val2);SetBuffer_16(6,val3);
funcId=6;
Math2Var_I32(0,4,funcId); // atan2 0x00 0x00 0x04 0x00 0x06 0xEC 0xFF 0xFF 0xFF 0x0A 0x00 0x00 0x00
if(GetBuffer_16(2)==0xFFFF&&GetBuffer_16(0)==0xFFFF){ //atan2(-20,10)
result = 1;
SetBuffer(0,result,1);
Send(1);
/*
SetBuffer(4,result,1);
Send(5);
*/
}
}
}
}
}
}
}
if(result==0){
SetBuffer(0,result,1);
SetBuffer(1,funcId,1);
Send(2);
/*
SetBuffer(4,result,1);
SetBuffer(5,funcId,1);
Send(6);
*/
}
}
}
function main()
{
local execType;
execType = GetExecType();
if(execType==MESHEXECTYPE_AIRCMD_bm){
exec_aircmd();
}
}