3.2. Interactive Run
Interactive operation requires a low-level interface to read user input bytes. This interface is a weak function. Users need to implement a __platform_getchar()
in their own code. to override this weak function. The weak function prototype is in PikaPlatform.c. If the user does not override it, an error will be reported when using the interactive runtime.
Users can directly implement a __platform_getchar()
in the main.c of the project. If the platform itself supports getchar()
, you can directly access the platform’s getchar()
.
/* main.c */
char __platform_getchar(){
return getchar();
}
If the platform does not support it, you need to implement it yourself, pay attention to implement a blocking getchar()
, that is, when there is no serial input character, you need to use __platform_getchar()
waits, and returns a character if there is input. E.g:
/* main.c */
char __platform_getchar(){
char res = 0;
while(rx_char == 0){
};
res = rx_char;
rx_char = 0;
return res;
}
3.2.1.2. Start PikaScript Shell and run pikaScriptShell() directly to start interactive operation.
pikaScriptShell()
The entry parameter is the root object of pika, and running pikaScriptInit()
will create a root object.
stm32g070cb: https://github.com/pikastech/pikascript/blob/master/bsp/stm32g070cb/Booter/main.c
rt-thread:
3.2.1.4. Precautions:
The obj_runChar
kernel API can specify an object to execute a script with one byte of input.
You need to run obj_runCharInit()
before you can use obj_runChar
.
Example code.
obj_runCharInit(pikaMain);
while(1){
char ch = my_get_char();
obj_runChar(pikaMain, ch);
}
Kernel version needs to be no less than v1.8.3
obj_run
kernel API can specify an object to execute a script, and use this API to execute a single-line or multi-line script. The following is an example of the interactive running driver of CH32. This interactive running support is written in the main loop of the firmware and starts to execute after the pikaScriptInit()
initialization script is executed.
PikaObj *PikaMain = pikaScriptInit();
printf(">>>");
while(1)
{
if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET)
{
is_Rx_start = 1;
t_start = rt_tick_get();
rxCh = USART_ReceiveData(USART1);
if(rxCh < 128){
RxBuffer[RxCnt++] = rxCh;
}
}
if( (is_Rx_start == 1) && (rt_tick_get() - t_start > 10) ){
{
USART_SendData(USART1, RxBuffer[i]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)== RESET);
}
obj_run(PikaMain, RxBuffer);
printf(">>>");
memset(RxBuffer, 0, 256);
RxCnt = 0;
}
}
3.2.3.1. Driven Content
Poll to receive characters and store them in the buffer.
Echo the received string after receiving.
Execute scripts using the
obj_run
kernel API. The specified object is the root object created by thepikaScriptInit()
init script, and the execution content is the received string.Clean up the receive buffer.
Kernel version needs to be at least v1.2.6
When executing a multi-line script, you need to pass in a complete code block For example: the following script is a complete code block, especially the 4th line, which needs to have an indent of 0 to mark the end of the code block. and the last line needs to have a blank line, which means
print('the end')
with a newline at the end of the script.
The following example is also possible
while a < 10:
a = a + 1
print(a)
The following example does not work
# Missing final newline
while a < 10:
print(a)