ccNOos  v0.0.0
Build Portable Microcontroller Applications!
console_menu.h
Go to the documentation of this file.
1 /** \file console_menu.h
2 * \brief <a href="https://www.inmechasol.org/" target="_blank">IMS</a>:
3  <a href="https://github.com/InMechaSol/ccNOos" target="_blank">ccNOos</a>,
4  Declarations for straight C and C++
5 
6  Copyright 2021 <a href="https://www.inmechasol.org/" target="_blank">InMechaSol, Inc</a>
7 
8  Licensed under the Apache License, Version 2.0(the "License");
9  you may not use this file except in compliance with the License.
10  You may obtain a copy of the License at
11 
12  http://www.apache.org/licenses/LICENSE-2.0
13 
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License.
19 
20 Notes:
21  (.c includes .h) - for straight C or
22  (.cpp includes .c which includes .h) - for C++ wrapped straight C
23  *Always compiled to a single compilation unit, either C or CPP, not both
24 
25 */
26 
27 #ifndef __CONSOLE_MENU__
28 #define __CONSOLE_MENU__
29 
30 
31 #include "compute_module.h"
32 #include "io_device.h"
33 
34 #ifdef __USINGCONSOLEMENU
35 
36 
37 
38 // Console UI Data Structure
39 struct uiStruct
40 {
41  struct devicedatastruct* devptr;
42  int lines2print, linesprinted;
43  int currentMenuIndex, currentUserLevel, cursorIndex, viewFormatIndex;
44  int parseIndex, readIndex;
45  UI_8 clearScreen, showHelp, showPrompt;
46 };
47 struct uiStruct createuiStruct();
48 
49 
50 // Logging Interface Data Structure
51 struct logStruct
52 {
53  struct devicedatastruct* devptr;
54 };
55 struct logStruct createlogStruct();
56 
57 
58 // Config Interface Data Structure
59 struct configStruct
60 {
61  struct devicedatastruct* devptr;
62 };
63 struct configStruct createconfigStruct();
64 
65 
66 // Cross-platfrom implementations of common ASCII string functions
67 UI_8 isASCIIchar(char inChar);
68 UI_8 isLetterchar(char inChar);
69 UI_8 isNumberchar(char inChar);
70 UI_8 isIntegerchar(char inChar);
71 UI_8 isUnsignedIntegerchar(char inChar);
72 UI_8 isDelimiterchar(char inChar);
73 UI_8 isTerminatorchar(char inChar);
74 UI_8 isASCIIString(char* inStringPtr);
75 UI_8 isLetterString(char* inStringPtr);
76 UI_8 isNumberString(char* inStringPtr);
77 UI_8 isIntegerString(char* inStringPtr);
78 UI_8 isUnsignedIntegerString(char* inStringPtr);
79 UI_8 stringMatchCaseSensitive(char* inStringPtr, const char* matchString);
80 void stringInit(char* stringPtr, const char* initString);
81 UI_16 stringLength(const char* stringPtr);
82 const char* cursorString(UI_8 showCursor);
83 const char* terminalClearString();
84 
85 void writeUIHelpString(struct uiStruct* uiStructPtrIn);
86 const char* terminalPromptString(int userLevelIndex);
87 const char* terminalSlashes();
88 
89 void WriteMenuLine(struct uiStruct* uiStructPtrin); // rely on 0x00 termination? safer with length parameter
90 void GetMenuChars(struct uiStruct* uiStructPtrin);
91 
92 void WriteLogLine(struct logStruct* logStructPtrin);
93 void ReadConfigLine(struct configStruct* configStructPtrin);
94 
95 
96 // when possible, simply return snprintf() from std lib
97 int SN_PrintF(char* str, unsigned int size, const char* format, ...);
98 UI_8 ATO_F(const char* str, float* val);
99 UI_8 ATO_D(const char* str, double* val);
100 
101 UI_8 ATO_I8(const char* str, I_8* val);
102 UI_8 ATO_I16(const char* str, I_16* val);
103 UI_8 ATO_I32(const char* str, I_32* val);
104 UI_8 ATO_I64(const char* str, I_64* val);
105 UI_8 ATO_U8(const char* str, UI_8* val);
106 UI_8 ATO_U16(const char* str, UI_16* val);
107 UI_8 ATO_U32(const char* str, UI_32* val);
108 UI_8 ATO_U64(const char* str, UI_64* val);
109 
110 #define PlatformAPIFuncsTemplate(SizePlusOne) \
111 int SN_PrintF(char* str, unsigned int size, const char* format, ...)\
112 {\
113  va_list argptr;\
114  va_start(argptr, format);\
115  int chars = vsnprintf(str, SizePlusOne, format, argptr);\
116  va_end(argptr);\
117  return chars;\
118 }\
119 UI_8 ATO_F(const char* str, float* val)\
120 {\
121  if (isNumberString((char*)str))\
122  {\
123  *val = (float)atof(str);\
124  return ui8TRUE;\
125  }\
126  else\
127  return ui8FALSE;\
128 }\
129 UI_8 ATO_D(const char* str, double* val)\
130 {\
131  if (isNumberString((char*)str))\
132  {\
133  *val = atof(str);\
134  return ui8TRUE;\
135  }\
136  else\
137  return ui8FALSE;\
138 }\
139 UI_8 ATO_I8(const char* str, I_8* val)\
140 {\
141  if (isIntegerString((char*)str))\
142  {\
143  *val = (I_8)atoi(str);\
144  return ui8TRUE;\
145  }\
146  else\
147  return ui8FALSE;\
148 }\
149 UI_8 ATO_I16(const char* str, I_16* val)\
150 {\
151  if (isIntegerString((char*)str))\
152  {\
153  *val = (I_16)atoi(str);\
154  return ui8TRUE;\
155  }\
156  else\
157  return ui8FALSE;\
158 }\
159 UI_8 ATO_I32(const char* str, I_32* val)\
160 {\
161  if (isIntegerString((char*)str))\
162  {\
163  *val = (I_32)atol(str);\
164  return ui8TRUE;\
165  }\
166  else\
167  return ui8FALSE;\
168 }\
169 UI_8 ATO_I64(const char* str, I_64* val)\
170 {\
171  if (isIntegerString((char*)str))\
172  {\
173  *val = (I_64)atol(str);\
174  return ui8TRUE;\
175  }\
176  else\
177  return ui8FALSE;\
178 }\
179 UI_8 ATO_U8(const char* str, UI_8* val)\
180 {\
181  if (isUnsignedIntegerString((char*)str))\
182  {\
183  *val = (UI_8)atol(str);\
184  return ui8TRUE;\
185  }\
186  else\
187  return ui8FALSE;\
188 }\
189 UI_8 ATO_U16(const char* str, UI_16* val)\
190 {\
191  if (isUnsignedIntegerString((char*)str))\
192  {\
193  *val = (UI_16)atol(str);\
194  return ui8TRUE;\
195  }\
196  else\
197  return ui8FALSE;\
198 }\
199 UI_8 ATO_U32(const char* str, UI_32* val)\
200 {\
201  if (isUnsignedIntegerString((char*)str))\
202  {\
203  *val = (UI_32)atol(str);\
204  return ui8TRUE;\
205  }\
206  else\
207  return ui8FALSE;\
208 }\
209 UI_8 ATO_U64(const char* str, UI_64* val)\
210 {\
211  if (isUnsignedIntegerString((char*)str))\
212  {\
213  *val = (UI_64)atol(str);\
214  return ui8TRUE;\
215  }\
216  else\
217  return ui8FALSE;\
218 }
219 
220 
221 // ASCII Constants for Communication Interfaces
222 #define ASCII_space 32
223 #define ASCII_A 65
224 #define ASCII_Z 90
225 #define ASCII_a 97
226 #define ASCII_z 122
227 #define ASCII_0 48
228 #define ASCII_9 57
229 #define ASCII_plus 43
230 #define ASCII_minus 45
231 #define ASCII_dot 46
232 #define ASCII_colon 58
233 #define ASCII_semicolon 59
234 #define ASCII_tilda 126
235 #define ASCII_lf 10
236 #define ASCII_cr 13
237 #define ASCII_tab 9
238 
239 #define __INIT_MENU_VARS(CHARS_PER_LINE, CHAR_PTR) char * linebuff = CHAR_PTR; \
240  int charsWritten = 0; \
241  int maxChars = CHARS_PER_LINE
242 
243 #define INIT_MENU_VARS(CHARS_PER_LINE, CHAR_PTR) __INIT_MENU_VARS(CHARS_PER_LINE, CHAR_PTR)
244 
245 #define __PRINT_MENU_LN SN_PrintF( linebuff, maxChars,
246 #define PRINT_MENU_LN __PRINT_MENU_LN
247 
248 #define __END_MENU_LN ); break
249 #define END_MENU_LN __END_MENU_LN
250 
251 #define __OPENSWITCH(NAMEEE) INIT_MENU_VARS(charBuffMax, &NAMEEE->devptr->outbuff.charbuff[0]); \
252  do { \
253  switch (NAMEEE->linesprinted){
254 
255 
256 #define OPENSWITCH(NAMEEE) __OPENSWITCH(NAMEEE)
257 
258 #define __CLOSESWITCH(NAMEEE) \
259  NAMEEE->lines2print = 0;\
260 break;\
261  }\
262 if (NAMEEE->lines2print > 0)\
263 {\
264  WriteMenuLine(NAMEEE);\
265  NAMEEE->linesprinted++;\
266 }\
267  } while (NAMEEE->lines2print > 0);
268 
269 
270 
271 #define CLOSESWITCH(NAMEEE) __CLOSESWITCH(NAMEEE)
272 
273 
274 #define __OPENIF(tNAME, mNAME) \
275 if (uiStructPtrIn->devptr->newDataReadIn) \
276 { \
277  if (stringMatchCaseSensitive(&uiStructPtrIn->devptr->inbuff.charbuff[uiStructPtrIn->parseIndex], tNAME) == ui8TRUE) \
278  { \
279  uiStructPtrIn->parseIndex += stringLength( tNAME ); \
280  if (uiStructPtrIn->devptr->inbuff.charbuff[uiStructPtrIn->parseIndex] == ';') \
281  { \
282  uiStructPtrIn->currentMenuIndex = mNAME; \
283  } \
284  else if (uiStructPtrIn->devptr->inbuff.charbuff[uiStructPtrIn->parseIndex] == ':') \
285  { \
286  uiStructPtrIn->parseIndex++;
287 
288 #define OPENIF(tNAME, mNAME) __OPENIF(tNAME, mNAME)
289 
290 
291 #define __CLOSEIF(tNAME, mNAME) \
292  } \
293  uiStructPtrIn->devptr->newDataReadIn = ui8FALSE; \
294  } \
295 }
296 
297 #define CLOSEIF(tNAME, mNAME) __CLOSEIF(tNAME, mNAME)
298 
299 
300 
301 #define __KEYIF(tNAME, mNAME) \
302 if (uiStructPtrIn->devptr->newDataReadIn) \
303 { \
304  if (stringMatchCaseSensitive(&uiStructPtrIn->devptr->inbuff.charbuff[uiStructPtrIn->parseIndex], tNAME) == ui8TRUE) \
305  { \
306  uiStructPtrIn->parseIndex++; \
307  theCurrentAction = mNAME; \
308  uiStructPtrIn->devptr->newDataReadIn = ui8FALSE; \
309  } \
310 }
311 
312 #define KEYIF(tNAME, mNAME) __KEYIF(tNAME, mNAME)
313 
314 
315 #ifdef __cplusplus
316 
317 
318 
319 class consoleMenuClass // declaration of console menu class
320 {
321 
322 };
323 
324 
325 #endif // !__cplusplus
326 #endif // !__USINGCONSOLEMENU
327 #endif // ! __CONSOLE_MENU__
GetMenuChars
void GetMenuChars(struct uiStruct *uiStructPtrin)
Definition: Application_Platform_Main.c:111
io_device.h
IMS: ccNOos, Declarations for straight C and C++
str
#define str(s)
Definition: version_config.h:51
WriteLogLine
void WriteLogLine(struct logStruct *logStructPtrin)
Definition: Application_Platform_Main.c:167
ReadConfigLine
void ReadConfigLine(struct configStruct *configStructPtrin)
Definition: Application_Platform_Main.c:172
terminalPromptString
const char * terminalPromptString(int userLevelIndex)
Definition: MenuAPI.c:61
WriteMenuLine
void WriteMenuLine(struct uiStruct *uiStructPtrin)
Definition: Application_Platform_Main.c:144
ATO_F
UI_8 ATO_F(const char *str, float *val)
compute_module.h
IMS: ccNOos, Declarations for straight C and C++
writeUIHelpString
void writeUIHelpString(struct uiStruct *uiStructPtrIn)
Definition: MenuAPI.c:29
devicedatastruct
the common data struct of io devices
Definition: io_device.h:62