static void cliEnter(void) { cliMode = 1; beginSerialPortFunction(cliPort, FUNCTION_CLI); setPrintfSerialPort(cliPort); cliPrint("\r\nEntering CLI Mode, type 'exit' to return, or 'help'\r\n"); cliPrompt(); }
void cliInit(void) { serialPrint(cliHome); serialPrint(cliClear); sprintf(version, "%s.%d", VERSION, getBuildNumber()); cliFuncVer(0, 0); serialPrint("\r\nCLI ready.\r\n"); cliPrompt(); }
void cliTask(void * pdata) { char line[CLI_COMMAND_MAX_LEN+1]; int pos = 0; cliPrompt(); for (;;) { uint8_t c; while (!cliRxFifo.pop(c)) { CoTickDelay(10); // 20ms } if (c == 12) { // clear screen serialPrint("\033[2J\033[1;1H"); cliPrompt(); } else if (c == 127) { // backspace if (pos) { line[--pos] = '\0'; serialPutc(c); } } else if (c == '\r' || c == '\n') { // enter serialCrlf(); line[pos] = '\0'; cliExecLine(line); pos = 0; cliPrompt(); } else if (isascii(c) && pos < CLI_COMMAND_MAX_LEN) { line[pos++] = c; serialPutc(c); } } }
void cliProcess(void) { if (!cliMode) { cliMode = 1; cliPrint("\r\nEntering CLI Mode, type 'exit' to return, or 'help'\r\n"); cliPrompt(); } while (serialTotalBytesWaiting(core.mainport)) { uint8_t c = serialRead(core.mainport); if (c == '\t' || c == '?') { // do tab completion const clicmd_t *cmd, *pstart = NULL, *pend = NULL; unsigned int i = bufferIndex; for (cmd = cmdTable; cmd < cmdTable + CMD_COUNT; cmd++) { if (bufferIndex && (strncasecmp(cliBuffer, cmd->name, bufferIndex) != 0)) continue; if (!pstart) pstart = cmd; pend = cmd; } if (pstart) { /* Buffer matches one or more commands */ for (; ; bufferIndex++) { if (pstart->name[bufferIndex] != pend->name[bufferIndex]) break; if (!pstart->name[bufferIndex] && bufferIndex < sizeof(cliBuffer) - 2) { /* Unambiguous -- append a space */ cliBuffer[bufferIndex++] = ' '; cliBuffer[bufferIndex] = '\0'; break; } cliBuffer[bufferIndex] = pstart->name[bufferIndex]; } } if (!bufferIndex || pstart != pend) { /* Print list of ambiguous matches */ cliPrint("\r\033[K"); for (cmd = pstart; cmd <= pend; cmd++) { cliPrint(cmd->name); cliWrite('\t'); } cliPrompt(); i = 0; /* Redraw prompt */ } for (; i < bufferIndex; i++) cliWrite(cliBuffer[i]); } else if (!bufferIndex && c == 4) { cliExit(cliBuffer); return; } else if (c == 12) { // clear screen cliPrint("\033[2J\033[1;1H"); cliPrompt(); } else if (bufferIndex && (c == '\n' || c == '\r')) { // enter pressed clicmd_t *cmd = NULL; clicmd_t target; cliPrint("\r\n"); cliBuffer[bufferIndex] = 0; // null terminate target.name = cliBuffer; target.param = NULL; cmd = bsearch(&target, cmdTable, CMD_COUNT, sizeof cmdTable[0], cliCompare); if (cmd) cmd->func(cliBuffer + strlen(cmd->name) + 1); else cliPrint("ERR: Unknown command, try 'help'"); memset(cliBuffer, 0, sizeof(cliBuffer)); bufferIndex = 0; // 'exit' will reset this flag, so we don't need to print prompt again if (!cliMode) return; cliPrompt(); } else if (c == 127) { // backspace if (bufferIndex) { cliBuffer[--bufferIndex] = 0; cliPrint("\010 \010"); } } else if (bufferIndex < sizeof(cliBuffer) && c >= 32 && c <= 126) { if (!bufferIndex && c == 32) continue; cliBuffer[bufferIndex++] = c; cliWrite(c); } } }
void cliCheck(void) { cliCommand_t *cmd = NULL; if (cliTelemetry && !(runCount % cliTelemetry)) { maxAmps = (adcMaxAmps - adcAmpsOffset) * adcToAmps; serialPrint(cliHome); sprintf(tempBuf, "Telemetry @ %d Hz\r\n\n", RUN_FREQ/cliTelemetry); serialPrint(tempBuf); cliFuncStatus(cmd, ""); serialPrint("\n> "); serialPrint(cliBuf); serialPrint(cliClearEOL); } while (serialAvailable()) { char c = serialRead(); cliBuf[cliBufIndex++] = c; if (cliBufIndex == sizeof(cliBuf)) { cliBufIndex--; c = '\n'; } // EOL if (cliBufIndex && (c == '\n' || c == '\r')) { if (cliBufIndex > 1) { serialPrint("\r\n"); serialPrint(cliClearEOS); cliBuf[cliBufIndex-1] = 0; cmd = cliCommandGet(cliBuf); if (cmd) cmd->cmdFunc(cmd, cliBuf + strlen(cmd->name)); else serialPrint("Command not found"); if (commandMode != CLI_MODE) { cliBufIndex = 0; return; } } cliPrompt(); } // tab completion else if (c == CLI_TAB) { char *ret; cliBuf[--cliBufIndex] = 0; ret = cliTabComplete(cliBuf, cliBufIndex); if (ret) { cliBufIndex = strlen(ret); memcpy(cliBuf, ret, cliBufIndex); cliBuf[cliBufIndex++] = ' '; serialPrint("\r> "); serialPrint(cliBuf); serialPrint(cliClearEOL); } } // interrupt else if (c == CLI_INTR) { cliPrompt(); } // backspace else if (c == CLI_BS) { if (cliBufIndex > 1) { cliBuf[cliBufIndex-2] = 0; serialPrint("\r> "); serialPrint(cliBuf); serialWrite(' '); serialPrint("\r> "); serialPrint(cliBuf); cliBufIndex -= 2; } else { cliBufIndex--; } } // out of range character else if (c < 32 || c > 126) { serialWrite(CLI_BELL); cliBufIndex--; } else { serialWrite(c); } } }