76 lines
1.8 KiB
C
Raw Normal View History

2021-10-01 00:21:50 +08:00
/* this demo shows the usage of string arg in method */
#include "BaseObj.h"
#include <stdio.h>
void sendMethod(PikaObj *self, Args *args)
{
char *data = args_getStr(args, "data");
/* send to com1 */
printf("[com1]: %s\r\n", data);
}
PikaObj *New_USART(Args *args)
{
/* Derive from the tiny object class.
Tiny object can not import sub object.
Tiny object is the smallest object. */
2021-10-18 11:33:11 +08:00
PikaObj *self = TinyObj(args);
2021-10-01 00:21:50 +08:00
/* bind the method */
class_defineMethod(self, "send(data:str)", sendMethod);
/* return the object */
return self;
}
PikaObj *New_MYROOT(Args *args)
{
/* Derive from the base object class .
BaseObj is the smallest object that can
import sub object. */
2021-10-18 11:33:11 +08:00
PikaObj *self = BaseObj(args);
2021-10-01 00:21:50 +08:00
/* import LED class */
obj_import(self, "USART", New_USART);
/* new led object bellow root object */
obj_newObjFromClassLoader(self, "usart", "USART");
2021-10-01 00:21:50 +08:00
/* return the object */
return self;
}
int32_t main()
{
/* new root object */
PikaObj *root = newRootObj("root", New_MYROOT);
/* user input buff */
char inputBuff[256] = {0};
/* run the script with check*/
obj_run(root, "res = usart.send('hello world')");
printf("memory used max = %0.2f kB\r\n", pikaMemMax() / 1024.0);
printf("memory used now = %0.2f kB\r\n", pikaMemNow() / 1024.0);
while (1)
{
/* get user input */
fgets(inputBuff, sizeof(inputBuff), stdin);
/* run PikaScript and get res */
Args *resArgs = obj_runDirect(root, inputBuff);
/* get system output of PikaScript*/
char *sysOut = args_getSysOut(resArgs);;
if (NULL != sysOut)
{
/* print32_t out the system output */
printf("%s\r\n", sysOut);
}
/* deinit the res */
args_deinit(resArgs);
}
}