Have you tried to develop a key input feature on your small embedded platform with a serial input? It is not so easy to implement correctly because the input methodology is using a complicated protocol known as VT100. In my recent project "Natural Tiny Shell (NT-Shell)" provides VT100 compatible terminal control features for small embedded systems. However, the middleware is still large for small embedded processors such as 8-bit MCUs, 16-bit MCUs and also for small 32-bit MCUs like Cortex-M0. This "MicroShell" middleware provides a minimal terminal control for the platforms.

Dependents:   MicroShellExample

Committer:
shintamainjp
Date:
Sun Feb 05 03:55:01 2017 +0000
Revision:
0:e91b984e285d
first commitment.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shintamainjp 0:e91b984e285d 1 /**
shintamainjp 0:e91b984e285d 2 * @file msopt.c
shintamainjp 0:e91b984e285d 3 * @author Shinichiro Nakamura (CuBeatSystems)
shintamainjp 0:e91b984e285d 4 * ===============================================================
shintamainjp 0:e91b984e285d 5 * MicroShell (Version 0.0.1)
shintamainjp 0:e91b984e285d 6 * Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems)
shintamainjp 0:e91b984e285d 7 * ===============================================================
shintamainjp 0:e91b984e285d 8 * The MIT License : https://opensource.org/licenses/MIT
shintamainjp 0:e91b984e285d 9 *
shintamainjp 0:e91b984e285d 10 * Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems)
shintamainjp 0:e91b984e285d 11 *
shintamainjp 0:e91b984e285d 12 * Permission is hereby granted, free of charge, to any person
shintamainjp 0:e91b984e285d 13 * obtaining a copy of this software and associated documentation
shintamainjp 0:e91b984e285d 14 * files (the "Software"), to deal in the Software without
shintamainjp 0:e91b984e285d 15 * restriction, including without limitation the rights to use,
shintamainjp 0:e91b984e285d 16 * copy, modify, merge, publish, distribute, sublicense, and/or
shintamainjp 0:e91b984e285d 17 * sell copies of the Software, and to permit persons to whom the
shintamainjp 0:e91b984e285d 18 * Software is furnished to do so, subject to the following
shintamainjp 0:e91b984e285d 19 * conditions:
shintamainjp 0:e91b984e285d 20 *
shintamainjp 0:e91b984e285d 21 * The above copyright notice and this permission notice shall be
shintamainjp 0:e91b984e285d 22 * included in all copies or substantial portions of the Software.
shintamainjp 0:e91b984e285d 23 *
shintamainjp 0:e91b984e285d 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
shintamainjp 0:e91b984e285d 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
shintamainjp 0:e91b984e285d 26 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
shintamainjp 0:e91b984e285d 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
shintamainjp 0:e91b984e285d 28 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
shintamainjp 0:e91b984e285d 29 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
shintamainjp 0:e91b984e285d 30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
shintamainjp 0:e91b984e285d 31 * OTHER DEALINGS IN THE SOFTWARE.
shintamainjp 0:e91b984e285d 32 */
shintamainjp 0:e91b984e285d 33
shintamainjp 0:e91b984e285d 34 #include "msopt.h"
shintamainjp 0:e91b984e285d 35
shintamainjp 0:e91b984e285d 36 #define INITCODE (0x248e97c0)
shintamainjp 0:e91b984e285d 37 #define IS_WHITE_SPACE(C) (((C) == ' ') || ((C) == '\t') || ((C) == '\r') || ((C) == '\n'))
shintamainjp 0:e91b984e285d 38
shintamainjp 0:e91b984e285d 39 static char *skip_white_space(char *text)
shintamainjp 0:e91b984e285d 40 {
shintamainjp 0:e91b984e285d 41 char *p = text;
shintamainjp 0:e91b984e285d 42 while (IS_WHITE_SPACE(*p)) {
shintamainjp 0:e91b984e285d 43 if (*p == 0) {
shintamainjp 0:e91b984e285d 44 break;
shintamainjp 0:e91b984e285d 45 }
shintamainjp 0:e91b984e285d 46 p++;
shintamainjp 0:e91b984e285d 47 }
shintamainjp 0:e91b984e285d 48 return p;
shintamainjp 0:e91b984e285d 49 }
shintamainjp 0:e91b984e285d 50
shintamainjp 0:e91b984e285d 51 static char *skip_text_string(char *text, int *length)
shintamainjp 0:e91b984e285d 52 {
shintamainjp 0:e91b984e285d 53 char *p = text;
shintamainjp 0:e91b984e285d 54 int count = 0;
shintamainjp 0:e91b984e285d 55 while (!IS_WHITE_SPACE(*p)) {
shintamainjp 0:e91b984e285d 56 if (*p == 0) {
shintamainjp 0:e91b984e285d 57 break;
shintamainjp 0:e91b984e285d 58 }
shintamainjp 0:e91b984e285d 59 p++;
shintamainjp 0:e91b984e285d 60 count++;
shintamainjp 0:e91b984e285d 61 }
shintamainjp 0:e91b984e285d 62 *length = count;
shintamainjp 0:e91b984e285d 63 return p;
shintamainjp 0:e91b984e285d 64 }
shintamainjp 0:e91b984e285d 65
shintamainjp 0:e91b984e285d 66 MSOPT_RESULT msopt_init(MSOPT *handle, char *text)
shintamainjp 0:e91b984e285d 67 {
shintamainjp 0:e91b984e285d 68 char *src = text;
shintamainjp 0:e91b984e285d 69 char *des = handle->argv;
shintamainjp 0:e91b984e285d 70 while (*src) {
shintamainjp 0:e91b984e285d 71 *des++ = *src++;
shintamainjp 0:e91b984e285d 72 }
shintamainjp 0:e91b984e285d 73 *des = 0;
shintamainjp 0:e91b984e285d 74 handle->argc = 0;
shintamainjp 0:e91b984e285d 75 char *p = handle->argv;
shintamainjp 0:e91b984e285d 76 while (1) {
shintamainjp 0:e91b984e285d 77 if (handle->argc >= MSCONF_MAX_INPUT_ARGS) {
shintamainjp 0:e91b984e285d 78 return MSOPT_RESULT_ERROR_TOO_MUCH_ARGUMENTS;
shintamainjp 0:e91b984e285d 79 }
shintamainjp 0:e91b984e285d 80 p = skip_white_space(p);
shintamainjp 0:e91b984e285d 81 if (*p == 0) {
shintamainjp 0:e91b984e285d 82 break;
shintamainjp 0:e91b984e285d 83 }
shintamainjp 0:e91b984e285d 84 handle->info[handle->argc].head = p;
shintamainjp 0:e91b984e285d 85 p = skip_text_string(p, &(handle->info[handle->argc].length));
shintamainjp 0:e91b984e285d 86 handle->argc++;
shintamainjp 0:e91b984e285d 87 }
shintamainjp 0:e91b984e285d 88 handle->initcode = INITCODE;
shintamainjp 0:e91b984e285d 89 return MSOPT_RESULT_OK;
shintamainjp 0:e91b984e285d 90 }
shintamainjp 0:e91b984e285d 91
shintamainjp 0:e91b984e285d 92 MSOPT_RESULT msopt_get_argc(MSOPT *handle, int *argc)
shintamainjp 0:e91b984e285d 93 {
shintamainjp 0:e91b984e285d 94 if (handle->initcode != INITCODE) {
shintamainjp 0:e91b984e285d 95 *argc = 0;
shintamainjp 0:e91b984e285d 96 return MSOPT_RESULT_ERROR_ILLEGAL_OBJECT;
shintamainjp 0:e91b984e285d 97 }
shintamainjp 0:e91b984e285d 98 *argc = handle->argc;
shintamainjp 0:e91b984e285d 99 return MSOPT_RESULT_OK;
shintamainjp 0:e91b984e285d 100 }
shintamainjp 0:e91b984e285d 101
shintamainjp 0:e91b984e285d 102 MSOPT_RESULT msopt_get_argv(MSOPT *handle, int index, char *bufptr, int bufsiz)
shintamainjp 0:e91b984e285d 103 {
shintamainjp 0:e91b984e285d 104 *bufptr = 0;
shintamainjp 0:e91b984e285d 105 if (handle->argc <= index) {
shintamainjp 0:e91b984e285d 106 return MSOPT_RESULT_ERROR_ILLEGAL_INDEX_NUMBER;
shintamainjp 0:e91b984e285d 107 }
shintamainjp 0:e91b984e285d 108 char *src = handle->info[index].head;
shintamainjp 0:e91b984e285d 109 char *des = bufptr;
shintamainjp 0:e91b984e285d 110 int i;
shintamainjp 0:e91b984e285d 111 for (i = 0; i < handle->info[index].length; i++) {
shintamainjp 0:e91b984e285d 112 if (i >= bufsiz - 1) {
shintamainjp 0:e91b984e285d 113 *des = 0;
shintamainjp 0:e91b984e285d 114 return MSOPT_RESULT_ERROR_BUFFER_SIZE;
shintamainjp 0:e91b984e285d 115 }
shintamainjp 0:e91b984e285d 116 *des++ = *src++;
shintamainjp 0:e91b984e285d 117 }
shintamainjp 0:e91b984e285d 118 *des = 0;
shintamainjp 0:e91b984e285d 119 return MSOPT_RESULT_OK;
shintamainjp 0:e91b984e285d 120 }
shintamainjp 0:e91b984e285d 121
shintamainjp 0:e91b984e285d 122