Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DispTools.c « menu « ble « STM32_WPAN « ST « Middlewares - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0a7e92a81ac9a5c548e11d49f8ac1cf15f8af053 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
/**
  ******************************************************************************
  * @file    DispTools.c
  * @author  MCD Application Team
  * @brief   This file contains functions for creating MMI on a terminal.
  ******************************************************************************
  * @attention
 *
 * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under Ultimate Liberty license
 * SLA0044, the "License"; You may not use this file except in compliance with
 * the License. You may obtain a copy of the License at:
 *                             www.st.com/SLA0044
 *
 ******************************************************************************
 */



/* *****************************************************************************
 * *****    Include Files declaration.                                     *****
 * ****************************************************************************/
#include "ble_common.h"
#include "stm_queue.h"
#include "DispTools.h"
#include "app_conf.h"


/* *****************************************************************************
 * *****    Define & Macro declaration.                                    *****
 * ****************************************************************************/

#define RX_BUFFER_SIZE                                                        64


/* -- Definition of Lines in DisplayTitle() function -- */
#define LINE_START                      0       /* Case of Test Name is in Bold. */
#define LINE_TESTNAME                   1
#define LINE_COMPANY                    2
#define LINE_CHIP_DEMUX                 3
#define LINE_SOFT_PROTO                 4
#define LINE_END                        5

#define MAX_X                          80
#define MAX_Y                          24

/* -- Macro for Menu() function. -- */
#define skip_next_inactive_item(ItemNumb)    \
  /* skip not active item */                 \
  while ( (menu[ItemNumb].Type == TITLE)     \
       || (menu[ItemNumb].Type == VAL16)     \
       || (menu[ItemNumb].Type == VAL32)     \
       || (menu[ItemNumb].Type == SVAL32)    \
       || (menu[ItemNumb].Type == STRTAB)    \
       || (menu[ItemNumb].Type == NULL)      \
       || (*menu[ItemNumb].Item == '\t' ) )  \
  {                                          \
    ItemNumb++;                              \
    if(*menu[ItemNumb].Item == '\0' )        \
      ItemNumb=0;   /* Roll over */          \
  }

/* -- Macro for Menu() function. -- */
#define skip_previous_inactive_item(ItemNumb)        \
  /* skip not active item  */                        \
  while ( (menu[ItemNumb].Type == TITLE)             \
               || (menu[ItemNumb].Type == VAL16)     \
               || (menu[ItemNumb].Type == VAL32)     \
               || (menu[ItemNumb].Type == SVAL32)    \
               || (menu[ItemNumb].Type == STRTAB)    \
               || (menu[ItemNumb].Type == NULL)      \
               || (*menu[ItemNumb].Item == '\t' ) )  \
  {                                                  \
    if (ItemNumb == 0)                               \
      ItemNumb=k;   /* max item */                   \
    else                                             \
      ItemNumb--;                                    \
  }

/* -- Macro for Menu() function. -- */
#define skip_next_inactive_item2(ItemNumb, MenuCtrlEntry)  \
  /* skip not active item */                               \
  while (MenuCtrlEntry.ItemsPos[ItemNumb].Selectable == 0) \
  {                                                        \
    ItemNumb++;                                            \
    if(MenuCtrlEntry.nbEntries == ItemNumb )               \
      ItemNumb=0;   /* Roll over */                        \
  }
        

#define skip_previous_inactive_item2(ItemNumb, MenuCtrlEntry) \
while (MenuCtrlEntry.ItemsPos[ItemNumb].Selectable == 0)      \
{                                                             \
  if (ItemNumb == 0)                                          \
   ItemNumb = MenuCtrlEntry.nbEntries;   /* max item */       \
  else                                                        \
   ItemNumb--;                                                \
}


typedef struct
{
  uint8_t x;
  uint8_t y;
} Pos_t;


typedef struct
{
  Pos_t WinPosUpperLeft ;
  Pos_t WinPosDownRigth;
} WinPosSize_t;

typedef struct
{
  uint8_t Field_x;
  uint8_t Cursor_x;
  uint8_t NbLines;
} ColInfo_t;

typedef struct
{
  Pos_t ItemPos;
  uint8_t ColIndex;
  uint8_t Selectable;
} ItemPos_t;

typedef struct
{
  uint8_t created;            /* indicate if the menu has been created */
  STRUCTMENU_T *MenuItems;    /* point on the Menu description provide by Caller at creation time */
  ItemPos_t ItemsPos[MAXITEMNB];
  ColInfo_t ColInfo[MAXCOLNB+2];
  WinPosSize_t WinPos;
  uint8_t NbCol;
  uint8_t ItemSelectable;
  uint8_t nbItems;
  uint8_t nbEntries;
} MenuCtrlData_t;


/* *****************************************************************************
 * *****    Global Variables                                               *****
 * ****************************************************************************/
uint8_t BusyFlag = FREE_FLAG;
#ifdef CFG_CONSOLE_MENU
static uint8_t InputCharFromUart; 
static uint8_t RxQueueBuffer[RX_BUFFER_SIZE];  
#endif
static queue_t RxQueue;
static MenuCtrlData_t MenuCtrlData[MAX_MENU];
static char str[133], strDisplay[133];


static uint16_t NbDigit (int32_t * ptTabNber, uint16_t * NbElements);
static void GetNumberBox_Range (char *szFmt, STRUCTCAPTUREVAL_T * stRangeVal,
       uint8_t iX, uint8_t iY);
static uint16_t SelectBox (char *szMenu[], uint8_t iLineNumb, uint8_t iX,
        uint8_t iY, uint8_t mode);
static uint16_t SelectNumberBox (int32_t * ptTabNber, uint8_t iLineNumb,
        uint8_t iX, uint8_t iY, uint8_t mode);
static uint16_t CheckVaribleType (char *fmtStr);
static int8_t AToHex (char *szBuffer, uint32_t * plNumber);
static int8_t AToDec (const char *szString, int32_t *piNumber);
uint16_t StringLength (const char *szString);
static uint8_t IsSpace (int8_t cCar);
static uint8_t IsDigit (int8_t cCar);
void ClearLines (uint8_t iStart_Line, uint8_t iNumber_Of_Line);
static uint16_t DisplaySTMVersion (uint8_t iX, uint8_t iY);
int8_t ReadPcStr (uint8_t x, uint8_t y, char buffer[], int8_t nbDigit,
      int8_t pad, int8_t deflt, int8_t mode);
void PosCur (uint8_t iX, uint8_t iY);
void PrintPcCrt (uint8_t x, uint8_t y, char *szFormat, ...);
void PrintCrt (char *szFormat, ...);
char UartReadChar(uint8_t blocking);
#ifdef CFG_CONSOLE_MENU
static void ConsoleMenu_UART_RxCpltCallback( void );
void SaveCursorPos(void);
void RestoreCursorPos(void);
void DummyFunc(__IO uint32_t * lpUart_Base, int8_t * pcFormat, ...);
static void _DisplayMenu(int8_t MenuHandle, uint8_t flag);
#endif


/**
  * @brief  This function initialize the console
  * @param  None
  * @retval None
  */
void ConsoleMenu_Init(void)
{
#ifdef CFG_CONSOLE_MENU
  HW_UART_Init(CFG_CONSOLE_MENU);

  CircularQueue_Init(&RxQueue, RxQueueBuffer, RX_BUFFER_SIZE, 1, CIRCULAR_QUEUE_NO_WRAP_FLAG);
  
  HW_UART_Receive_IT(CFG_CONSOLE_MENU, &InputCharFromUart, 1, ConsoleMenu_UART_RxCpltCallback);

  return;
#endif
}


#ifdef CFG_CONSOLE_MENU
/**
  * @brief  This function handles USARTx interrupt request.
  * @param  None
  * @retval None
  */
void ConsoleMenu_UART_RxCpltCallback( void )
{
  CircularQueue_Add(&RxQueue, &InputCharFromUart,1,1);

  HW_UART_Receive_IT(CFG_CONSOLE_MENU, &InputCharFromUart, 1, ConsoleMenu_UART_RxCpltCallback);

  return;
 }
#endif


uint16_t UartTransmitStr(char *StrToDisp)
{
#ifdef CFG_CONSOLE_MENU
  uint16_t jj;

  jj = 0;
  while (StrToDisp[jj] != 0)
  {
    HW_UART_Transmit(CFG_CONSOLE_MENU, (uint8_t*)&StrToDisp[jj++], 1, 0xFFFFFFFF);
  }
  return jj;
#else
  return 0;
#endif
}


/**
 * *****************************************************************************
 * @fn    char UartReadChar(uint8_t blocking)
 * @author  -
 * @brief   Wait until the receipt of a character from an Uart or JTAG,
 *          then convert it into ASCII and return the character
 * @param   blocking : blocking mode
 * @return  Return the character received from the serial link or the JTAG.
 * *****************************************************************************
 */
char UartReadChar(uint8_t blocking)
{
  uint16_t size;
  uint8_t *c;
  static uint8_t  ESC_Seq = 0;
  uint8_t leave = 0;
  char retc = (char)EOF;
  static uint16_t ESC_Timeout = 0;
    
  while (!leave)
  {
    c = CircularQueue_Remove(&RxQueue, &size);
    if (!c) 
    { 
     if (ESC_Seq == 1)
     {
       if (--ESC_Timeout == 0) 
       {
         retc = ESC;
         ESC_Seq = 0;
         break; /* Assume ESC have been hit */ 
       }
     }
     if (!blocking) leave=1;
    }
    else if (c) /* I got a char */
    {
      retc = *c;
      if ((*c == ESC) && (ESC_Seq == 0))  /* ESC sequence */
      {
        ESC_Timeout = 5000;  /* Maybe better to arm a timer */
        ESC_Seq = 1;          /* If ESC AND Esc_seq ==> error case, here we just restart ESC sequence */
        retc = 0;
      }
      else if (ESC_Seq == 1)  /* check second char in ESC sequence */
      {
        if (*c == '[')  /* True ESC sequence */
        {
          /* wait for next char in ESC seq */
          ESC_Seq++;   
          retc = 0;
          ESC_Timeout = 0;
        }
        else          /* Unexpected char follwing ESC, so ESC hit [no ESC sequence ] */
        {
          leave = 1;
          ESC_Seq = 0;
          retc = ESC;
          ESC_Timeout = 0;
        }
      }
      else if (ESC_Seq == 2)
      {
        leave = 1;
        ESC_Seq = 0;
        retc = *c | 0x80;  
      }
      else
      {
        leave = 1;
      }
    }  
  }
  
  return retc;       
}


/**
 *******************************************************************************
 * @fn    int8_t ReadPcStr(int32_t x, int32_t y, int8_t buffer[],int8_t nbDigit, 
 *        int8_t pad, int8_t deflt, int8_t mode)
 * @author  BINI Jean-Claude
 * @brief Take the characters from the 'UART1_BASE' or the keypad, translate 
 * them into ASCII, and place them into the buffer. The characters will be 
 * accepted until the ENTER character is received. LEFT, RIGHT, BACK SPACE, 
 * DEL functions are available.
 * When nbDigit characters are received, the function does not accept additional 
 * characters.
 *
 * @param   x, y : Column & Line position.
 * @param   buffer : Address of the buffer where data will be placed
 * @param   nbDigit : Number max of characters accepted
 * @param   pad : Is the padding string. If pad = '.' and nbdigit = 6 the 
 *          function will display : ......
 * @param   deflt : If TRUE the function displays the default value contained 
 *                  into the string buffer.
 *                  If FALSE the string buffer is considered as empty
 * @param   mode :  If mode = INS means that the character capture uses the 
 *          insert mode.
 *          If mode = OVERSTK means that the function uses the overstrike mode.
 *
 * @return  Return the number of character read
 *******************************************************************************
 */
int8_t ReadPcStr(uint8_t x, uint8_t y, char buffer[], int8_t nbDigit,
                 int8_t pad, int8_t deflt, int8_t mode)
{
  uint8_t idx, nbChar, i;
  char data;
  int8_t valid;
  static char padding[2];

  padding[0] = pad;
  padding[1] = 0;
  PosCur(x, y);
  /*---- If a default value is required it is printed and nbChar 
         is initiated ---*/
  if (deflt == TRUE)
  {
    PrintCrt("%s", buffer);
    nbChar = (uint8_t) StringLength(buffer);
    for (i = nbChar; i < nbDigit; i++)
    {
      /* print padding pattern */
      PrintCrt(padding);

    }
  }
  else
  {
    for (i = 0; i < nbDigit; i++)
    {
      /* print padding pattern */
      PrintCrt(padding);

    }
    nbChar = 0;
  }
  PosCur(x, y);
  idx = 0;
  valid = FALSE;
  while (valid == FALSE)
  {
    data = UartReadChar(TRUE);
    switch (data)
    {
      case ENTER:
        valid = TRUE;
        break;

      case DEL:
        if (idx < nbChar)
        {
          for (i = idx; i < nbChar; i++)
          {
            buffer[i] = buffer[i + 1];
          }
          PosCur(x, y);
          for (i = 0; i < nbDigit; i++)
            PrintCrt(padding);

          PrintPcCrt(x, y, "%s", buffer);
          PosCur(x + idx, y);
          nbChar--;
        }
        break;

      case BKSP:
        if (idx != 0)
        {
          for (i = idx; i <= nbChar; i++)
          {
            buffer[i - 1] = buffer[i];
          }
          idx--;
          PosCur(x, y);
          for (i = 0; i < nbDigit; i++)
          {
            PrintCrt(padding);
          }
          PrintPcCrt(x, y, "%s", buffer);
          PosCur(x + idx, y);
          nbChar--;
        }
        break;

      case LEFT:
        if (idx != 0)
        {
          idx--;
          PosCur(x + idx, y);
        }
        break;

      case RIGHT:
        if (idx < nbChar)
        {
          idx++;
          PosCur(x + idx, y);
        }
        break;

      default:
        if (mode == INS)
        {
          if ((data > 0x1f) && (data < 0x80))
          {
            if (nbChar < nbDigit)
            {
              i = nbChar;
              do
              {
                buffer[i + 1] = buffer[i];
              }
              while (i-- != idx);

              nbChar++;
              buffer[idx] = data;
              PrintPcCrt(x, y, "%s", buffer);
              idx++;
              PosCur(x + idx, y);
            }
          }
        }
        else
        {
          if ((data > 0x1f) && (data < 0x80))
          {
            if (idx < nbDigit)
            {
              buffer[idx] = data;
              buffer[idx + 1] = 0;
              for (i = idx + 1; i <= nbDigit; i++)
              {
                PrintCrt(padding);
              }
              if (idx == nbChar)
                nbChar++;

              PrintPcCrt(x, y, "%s", buffer);
              idx++;
              PosCur(x + idx, y);
            }
          }
        }
        break;
    }
  }
  return nbChar;
}


/*
 * ****************************************************************
 *      MANAGE TERMINAL (DISPLAY & CURSOR)  FUNCTIONS             *
 *                                                                *
 *****************************************************************/

/**
 *******************************************************************************
 * @fn      void ClearLines(uint16_t iStart_Line, uint16_t iNumber_Of_Line)
 * @author  -
 * @brief Clear the number of line specified on terminal.
 *
 * @param USARTx : Is the base address of the serial interface.
 * @param iStart_Line : Location of the first line to clear
 * @param iNumber_Of_Line : Number of line to clear
 *
 * @return: none.
 *******************************************************************************
 **/
void ClearLines(uint8_t iStart_Line, uint8_t iNumber_Of_Line)
{
  uint16_t iCpt;

  for (iCpt = 0; iCpt < iNumber_Of_Line; iCpt++)
  {
    PosCur(1, iStart_Line + iCpt);
    UartTransmitStr("\033[K");
  }
}

/**
 *******************************************************************************
 * @fn      void ClearScreen()
 * @author  -
 * @brief Clear the full screen on terminal
 * @param None
 * @return  none.
 *******************************************************************************
 **/
void ClearScreen()
{
  UartTransmitStr("\033[2J");
  PosCur(1, 1);
}


/**
 *******************************************************************************
 * @fn      void PosCur(uint16_t iX, uint16_t iY)
 * @author  -
 * @brief Locate cursor at the position X & Y
 * @param iX, iY : Column & Line cursor position.
 * @return none.
 *******************************************************************************
 **/
void PosCur(uint8_t iX, uint8_t iY)
{
  char szCtrl[30] = "\33[01;01H";

  szCtrl[3] = (int8_t) ((iY % 10) + '0');
  szCtrl[2] = (int8_t) ((iY / 10) + '0');
  szCtrl[6] = (int8_t) ((iX % 10) + '0');
  szCtrl[5] = (int8_t) ((iX / 10) + '0');

  UartTransmitStr(szCtrl);
}

/**
 *******************************************************************************
 * @fn      void SaveCursorPos()
 * @author  -
 * @brief Save the current position of the cursor.
 * @param None
 * @return  none.
 *******************************************************************************
 **/
void SaveCursorPos(void)
{
  PrintCrt("%c7", 27);
}


/**
 *******************************************************************************
 * @fn      void RestoreCursorPos()
 * @author  -
 * @brief Restore the last value of the cursor saved with SaveCursorPos() function.
 * @param  none
 * @return  none.
 *******************************************************************************
 **/
void RestoreCursorPos(void)
{
  PrintCrt("%c8", 27);
}


/*
 * ****************************************************************
 *          STRING MANIPULATIONS  FUNCTIONS                       *
 *                                                                *
 *****************************************************************/

/**
 *******************************************************************************
 * @fn      uint8_t IsDigit(int8_t cCar)
 * @author  -
 * @brief Test if Character cCar is an ASCII value between '0' and '9' including.
 * @param   cCar : Character to test.
 * @return  TRUE if the ASCII value of cCar equals '0'or'1'or...'9', else FALSE.
 *******************************************************************************
 **/
uint8_t IsDigit(int8_t cCar)
{
  if ((cCar < '0') || (cCar > '9'))
  {
    return (FALSE);
  }
  else
  {
    return (TRUE);
  }
}


/**
 *******************************************************************************
 * @fn      uint8_t IsSpace(int8_t cCar)
 * @author  -
 * @brief Test if Character cCar is the ASCII value ' ' (Space).
 * @param   cCar : Character to test.
 * @return  TRUE if the ASCII value of cCar equals ' ', else FALSE.
 *******************************************************************************
 **/
uint8_t IsSpace(int8_t cCar)
{
  if (cCar == ' ')
  {
    return (TRUE);
  }

  return (FALSE);
}


/**
 *******************************************************************************
 * @fn    uint16_t StringLength(const int8_t * szString)
 * @author  -
 * @brief   Return the lenght of a string.
 * @param   szString : String pointer
 * @return  Length of the string without the '\0' char
 *******************************************************************************
 */
uint16_t StringLength(const char *szString)
{
  uint16_t iLength;

  iLength = 0;
  while (*szString++ != '\0')
  {
    if (++iLength == 0xFFFF)
    {
      break;
    }
  }

  return (iLength);
}


/**
 *******************************************************************************
 * @fn      boolean AToDec(const int8_t *szString, int32_t *piNumber)
 * @author  -
 * @brief   Conversion of an ASCII number to an integer.
 *
 * @param   szString : Is the address of a string containing the DEC number to 
 *                     convert
 * @param   piNumber : Address where the converted number can be put
 * @return  TRUE when the number is correctly converted, else FALSE.
 *******************************************************************************
 */
static int8_t AToDec(const char *szString, int32_t * piNumber)
{
  int8_t *pcChar = (int8_t *) szString;
  int32_t iValue = 0, iSign;

  while (IsSpace(*pcChar))
  {
    pcChar++;
  };

  if ((*pcChar) == '-')
  {
    iSign = -1;
    pcChar++;
  }
  else
  {
    if ((*pcChar) == '+')
    {
      pcChar++;
    }
    iSign = 1;
  };

  while ((*pcChar != 0) && (*pcChar != ' '))    /* space(s) at the end */
  {
    if (IsDigit(*pcChar))
    {
      iValue = 10 * iValue + (*pcChar - '0');
      pcChar++;
    }
    else
    {
      return (FALSE);
    }
  }

  *piNumber = iSign * iValue;
  return (TRUE);
}


/**
 *******************************************************************************
 * @fn      boolean AToHex(int8_t * szBuffer, uint32_t *plNumber)
 * @author  -
 * @brief   Conversion of an ASCII number to a long.
 *
 * @param   szBuffer : Is the address of a string containing the Hexa number to 
 *                     convert
 * @param   plNumber : Address where the converted number can be put
 * @return  TRUE when the number is correctly converted, else FALSE.
 *******************************************************************************
 */
int8_t AToHex(char *szBuffer, uint32_t * plNumber)
{
  uint8_t cCar;
  uint32_t lNumber;

  while (IsSpace(*szBuffer))
  {
    szBuffer++;
  }

  lNumber = 0;
  while ((*szBuffer != 0) && (*szBuffer != ' '))        /* Space(s) at the end */
  {
    cCar = *szBuffer;

    if (IsDigit(cCar))
    {
      cCar = (cCar - '0');
    }
    else
    {
      if ((cCar >= 'a') && (cCar <= 'f'))
      {
        cCar = (cCar - 'a' + 10);
      }
      else
      {
        if ((cCar >= 'A') && (cCar <= 'F'))
        {
          cCar = (cCar - 'A' + 10);
        }
        else
        {
          return (FALSE);
        }
      }
    }
    lNumber = (lNumber << 4) | cCar;
    szBuffer++;
  }

  *plNumber = lNumber;
  return (TRUE);
}


/*
 * ****************************************************************
 *          STRING DISPLAY FUNCTIONS                              *
 *                                                                *
 *****************************************************************/

/**
 *******************************************************************************
 * @fn      void DummyFunc(__IO uint32_t *lpUart_Base, int8_t *pcFormat, ...)
 * @author  -
 * @brief This function replace the PrintCrt() when no display is required.
 *
 * @param   lpUart_Base : is the base address of the serial interface
 * @param   pcFormat : Represent the string including the format of the data
 *             to be displayed. %d, %x, %s. %c, %u (uint32_t) are supported.
 *
 * @return  TRUE of Test OK, else FALSE.
 *******************************************************************************
 **/
void DummyFunc(__IO uint32_t * lpUart_Base, int8_t * pcFormat, ...)
{
}


/**
 *******************************************************************************
 * @fn      void PrintCrt(int8_t* szFormat, ...)
 * @author  -
 * @brief   This function print messages into the Display.
 * @param   szFormat : Represent the string including the format of the data
 *             to be displayed. %d, %x, %s. %c, %u (uint32_t) are supported.
 * @return  none
 *******************************************************************************
 */
void PrintCrt(char *szFormat, ...)
{
  uint8_t i;
  uint32_t ulval;
  int8_t *sval;
  char fmtDetect, *pt;
  int32_t slval;
  va_list ptArg;

  /* -------   Execute the print if BusyFlag = FREE_FLAG  -------- */
  if (BusyFlag == FREE_FLAG)
  {
    BusyFlag = BUSY_FLAG;
    /* -------  This is for multi-thread */
    fmtDetect = FALSE;
    va_start(ptArg, szFormat);
    i = 0;
    for (pt = szFormat; *pt != 0; pt++)
    {
      str[i++] = *pt;
      if (fmtDetect == FALSE)
      {
        if (*pt == '%')
        {
          fmtDetect = TRUE;
        }
      }
      else
      {
        switch (*pt)
        {
        case 'd':
        case 'c':
          fmtDetect = FALSE;
          str[i++] = 0;
          slval = va_arg(ptArg, long int);

          sprintf((char *) strDisplay, (char *) str, slval);
          i = 0;
          UartTransmitStr(strDisplay);
          break;

        case 'X':
        case 'x':
        case 'u':
          fmtDetect = FALSE;
          str[i++] = 0;
          ulval = va_arg(ptArg, unsigned long int);

          sprintf((char *) strDisplay, (char *) str, ulval);
          i = 0;
          UartTransmitStr(strDisplay);
          break;

        case 's':
          fmtDetect = FALSE;
          str[i++] = 0;
          sval = va_arg(ptArg, int8_t *);
          sprintf((char *) strDisplay, (char *) str, sval);
          i = 0;
          UartTransmitStr(strDisplay);
          break;
        }
      }
    }
    str[i++] = 0;
    UartTransmitStr(str);
    va_end(ptArg);
    BusyFlag = FREE_FLAG;
  }
}


/**
 *******************************************************************************
 * @fn      void PrintPcCrt(int32_t x, int32_t y, int8_t* szFormat, ...)
 * @author  -
 * @brief   This function print messages into the Display at the x,y position.
 * @param x, y   : column & line position.
 * @param   szFormat : Represent the string including the format of the data
 *             to be displayed. %d, %x, %s. %c, %u (unsigned int32_t) are supported.
 * @return  none
 *******************************************************************************
 */
void PrintPcCrt(uint8_t x, uint8_t y, char *szFormat, ...)
{
  uint8_t i;
  uint32_t ulval;
  int8_t *sval;
  char fmtDetect, *pt;
  int32_t slval;
  va_list ptArg;

  /* -------   Execute the print if BusyFlag = FREE_FLAG  -------- */
  if (BusyFlag == FREE_FLAG)
  {
    BusyFlag = BUSY_FLAG;
    /* -------  This is for multi-thread */
    PosCur(x, y);
    fmtDetect = FALSE;
    va_start(ptArg, szFormat);
    i = 0;

    for (pt = szFormat; *pt != 0; pt++)
    {
      str[i++] = *pt;
      if (fmtDetect == FALSE)
      {
        if (*pt == '%')
        {
          fmtDetect = TRUE;
        }
      }
      else
      {
        switch (*pt)
        {
        case 'd':
        case 'c':
          fmtDetect = FALSE;
          str[i++] = 0;
          slval = va_arg(ptArg, long int);

          sprintf((char *) strDisplay, (char *) str, slval);
          i = 0;
          UartTransmitStr(strDisplay);
          break;

        case 'X':
        case 'x':
        case 'u':
          fmtDetect = FALSE;
          str[i++] = 0;
          ulval = va_arg(ptArg, unsigned long int);

          sprintf((char *) strDisplay, (char *) str, ulval);
          i = 0;
          UartTransmitStr(strDisplay);
          break;

        case 's':
          fmtDetect = FALSE;
          str[i++] = 0;
          sval = va_arg(ptArg, int8_t *);
          sprintf((char *) strDisplay, (char *) str, sval);
          i = 0;
          UartTransmitStr(strDisplay);
          break;
        }
      }
    }
    str[i++] = 0;
    UartTransmitStr(str);
    va_end(ptArg);
    BusyFlag = FREE_FLAG;       /* allow another printf to be executed */
  }
}


/**
 *******************************************************************************
* @fn      void DisplayTitle(int32_t iY, char * szTitle, char * szVersion, 
*                            char * szFclk)
* @author -
* @brief  Display the title, the version of the OneC on the uart pointed by 
*         USARTx, at the line indicated by iY. The title will take 7 lines.
*
* @param  iY        : line position when the display will start
* @param  szTitle   : Points to the string containing the title to display
* @param  szVersion : Points to the string containing the version number. 
*                     If the string is empty (""), the version number will not 
*                     be displayed.
* @param  szFclk    : ARM clk actual frequency value
*
* @return None
 *******************************************************************************
**/
void DisplayTitle(uint8_t iY, char *szTitle, char *szVersion)
{
  uint32_t DecVal, IntVal;

  IntVal = SystemCoreClock / 1000000;
  DecVal = ((SystemCoreClock - (IntVal * 1000000)) + 500) / 1000;
  PrintPcCrt(1, iY,
             "________________________________________________________________________________");

  PrintPcCrt(24, iY + 2, "%c[\033[34;01m%s %c[0m", 27, szTitle, 27);
  PrintPcCrt(29, iY + 3, "%c[\033[34;01mST Microelectronics %c[0m", 27, 27);

  if (*szVersion != 0)
  {
    PrintPcCrt(5, iY + 5, "Soft version : %s", szVersion);
    PrintPcCrt(50, iY + 5, "Freq. ArmClk : %d.%d MHz", IntVal, DecVal);
  }

  PrintPcCrt(1, iY + 6,
             "________________________________________________________________________________");
  PrintCrt("\r\n");
}


/**
 *******************************************************************************
 * @fn      void DisplaySubTitle(int y, int8_t *title, int8_t dispVer)
 * @author  -
 * @brief Display the title, the version of the OneC on the uart pointed by 
 *        USARTx, at the line indicated by y. The title will take 2 lines.
 *
 * @param   y   : line position when the display will start.
 * @param   title : Points to the string containing the title to display
 * @param   dispVer : When TRUE, the OneC version is display, else only the 
 *                    title will be displayed.
 *
 * @return  None
 *******************************************************************************
 */
void DisplaySubTitle(int y, char *title, char dispVer)
{
  int dx;
  uint16_t lg;

  lg = StringLength(title);
  if (lg > 38)
    dx = 1;
  else
    dx = (40 - lg) / 2;
  PrintPcCrt(dx, y, "%c#6%s", 27, title);

  if (dispVer == TRUE)
    DisplaySTMVersion(25, y + 1);
}


/**
 *******************************************************************************
 * @fn      uint16_t DisplaySTMVersion(USART_TypeDef* uint16_t iX, uint16_t iY)
 * @author  -
 * @brief Display the Version of the Chip on the terminal pointed by
      at the position indicated by x, y.
 * @param USARTx : Is the base address of the serial interface .
 * @param iX, iY : column & line position.
 * @return  Value read in the Chip Version Register
 *******************************************************************************
**/
uint16_t DisplaySTMVersion(uint8_t iX, uint8_t iY)
{
  PrintPcCrt(iX, iY, "STM Version : STM32Fxxx");

  return (00);
}


/*
 * ****************************************************************
 *          MANAGE DISPLAY CFG_MENU FUNCTIONS                         *
 *                                                                *
 *****************************************************************/

/**
 *******************************************************************************
 * @fn      uint16_t DisplayMenu(int8_t * menu[], uint16_t lineNumb, int32_t x, 
 *                               int32_t y)
 * @author  -
 * @brief Displays the menu pointed by the parameters menu[], and select the 
 *      line indicated by 'line' with UP, DOWN, and VALID keys (issued from the 
 *      UART or for the keypad), it is possible to select the desired option.
 *      key : 'UP'      Select the previous available line
 *      key : 'DOWN'    Select the next available line
 *      key : 'ENTER'   Validate the selected line
 *      key : 'ESC'     Exit from the menu
 *
 * @param   USARTx : Pointer indicating the address base of the uart to be used
 * @param   menu :  Pointer of a table containing the menu to be displayed.
 *          THE LAST LINE OF THE TABLE MUST BE : ""
 * @param   lineNumb : Line number to display.
 * @param   x, y : Define the Column & Line where will be displayed the upper 
 *                 left corner of the menu.
 *
 * @return  Return the line menu selected (lineNumb).
            If lineNumb = 0xFE ---> Home
            else               ---> Line menu selected
 *******************************************************************************
 **/
uint16_t DisplayMenu(char *menu[], uint8_t lineNumb, uint8_t x, uint8_t y)
{
  uint16_t valid, max, i, j, end, maxLength, length;
  char c;

  valid = FALSE;
  max = 1;
  maxLength = 0;
  /* Look for the max number of menu lines to control exit function */
  for (i = 0; i < 23; i++)
  {
    if (*menu[i] == '\0')
    {
      max = i - 1;
      break;
    }
        /*--- Search the max length of the strings ---*/
    length = StringLength(menu[i]);
    if (maxLength < length)
    {
      maxLength = length;
    }
  }
  PrintPcCrt(1, 24,
             "Use : 'UP', 'DOWN' to select an option ; 'RETURN' to validate, 'ESC' to exit");

        /*--- Display the menu, and erase the used area ---*/
  for (i = 0; i <= max; i++)
  {
    PrintPcCrt(x, y + i + 1, "    %s", menu[i]);
    end = maxLength - StringLength(menu[i]);
    for (j = 0; j < end; j++)
      PrintCrt(" ");
  }
        /*--- Do not accept a number of line greater than the number of line ---*/
  if (lineNumb > max)
    lineNumb = 0;

        /*--- init terminal display ---*/
  PrintPcCrt(x + 1, y + 1 + lineNumb, "->");
  while (!valid)
  {
    c = UartReadChar(TRUE);
    switch (c)
    {
                /*------- 'Home' or Escape key --------*/
    case ESC:
      ClearLines (24, 1);
      lineNumb = HOME;
      valid = TRUE;
      break;

                /*------- 'Down' key --------*/
    case '2':
    case DOWN:
      PrintPcCrt(x + 1, y + 1 + lineNumb, "  ");
      lineNumb++;
      if (*menu[lineNumb] == '\0')
        lineNumb = 0;

      PrintPcCrt(x + 1, y + 1 + lineNumb, "->");
      break;

                /*------- 'Up' key --------*/
    case '8':
    case UP:
      PrintPcCrt(x + 1, y + 1 + lineNumb, "  ");
      if (lineNumb == 0)
        lineNumb = max;
      else
        lineNumb--;

      PrintPcCrt(x + 1, y + 1 + lineNumb, "->");
      break;

                /*------- 'valid' key --------*/
    case ENTER:
      ClearLines (24, 1);
      valid = TRUE;
      break;
    }
  }
  return lineNumb;
}


static void _DisplayMenu(int8_t MenuHandle, uint8_t flag)
{
  uint8_t i;
  uint16_t value16;      /* for VAL16 and NUM16 types */
  int32_t svalue32;
  uint32_t uvalue32;
  uint16_t varType;
  int32_t *ptSvalue32;
  STRUCTCAPTUREVAL_T *ptStruct;
  char** ptr;
    
  i=0;
  while (*MenuCtrlData[MenuHandle].MenuItems[i].Item != '\0')
  {
    if (*MenuCtrlData[MenuHandle].MenuItems[i].Item == '\t') 
    {
      i++;
      continue;
    }
    if (flag & DISPLAY)
    {
      /* display the description */
      PrintPcCrt(MenuCtrlData[MenuHandle].ItemsPos[i].ItemPos.x, MenuCtrlData[MenuHandle].ItemsPos[i].ItemPos.y, "%s",  MenuCtrlData[MenuHandle].MenuItems[i].Item);
    }
    if ((flag & DISPLAY ) || (flag & REFRESH))
    {
      /* display the associated field */
      switch (MenuCtrlData[MenuHandle].MenuItems[i].Type)
      {
        case TOGGLE:
        case SEL_TXT:
        case STRTAB:
          /* get pointer value of pointer of string */
          ptr = (char **) MenuCtrlData[MenuHandle].MenuItems[i].Msg;
          value16 = *(uint16_t *) MenuCtrlData[MenuHandle].MenuItems[i].Var;            
          PrintPcCrt(MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[i].ColIndex].Field_x,
                      MenuCtrlData[MenuHandle].ItemsPos[i].ItemPos.y, ptr[value16]);
          break;
          
        case SEL_NUM:
          ptSvalue32 = (int32_t *) MenuCtrlData[MenuHandle].MenuItems[i].Msg;
          value16 = *(uint16_t *) MenuCtrlData[MenuHandle].MenuItems[i].Var;  /* get value */
          
          SelectNumberBox(ptSvalue32, value16, MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[i].ColIndex].Field_x,
                          MenuCtrlData[MenuHandle].ItemsPos[i].ItemPos.y, DISPLAY);
          break;
          
        case NUM32:
        case VAL32:
          ptStruct = (STRUCTCAPTUREVAL_T *) MenuCtrlData[MenuHandle].MenuItems[i].Var;
          varType = CheckVaribleType(MenuCtrlData[MenuHandle].MenuItems[i].Msg);
          if (varType == SNUM)
          {
            svalue32 = *(int32_t *) ptStruct->CapValue;
            PrintPcCrt(MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[i].ColIndex].Field_x, 
              MenuCtrlData[MenuHandle].ItemsPos[i].ItemPos.y, MenuCtrlData[MenuHandle].MenuItems[i].Msg, svalue32);
          }
          if (varType == UNUM)
          {
            uvalue32 = *(uint32_t *) ptStruct->CapValue;
            PrintPcCrt(MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[i].ColIndex].Field_x, 
                       MenuCtrlData[MenuHandle].ItemsPos[i].ItemPos.y, MenuCtrlData[MenuHandle].MenuItems[i].Msg, uvalue32);
          }
          break;
        case TITLE:
        case ACTION:
        case NULL:
          value16 = *(uint16_t *) MenuCtrlData[MenuHandle].MenuItems[i].Var;
          PrintPcCrt(MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[i].ColIndex].Field_x, 
                     MenuCtrlData[MenuHandle].ItemsPos[i].ItemPos.y, MenuCtrlData[MenuHandle].MenuItems[i].Msg, value16);
          break;
      
        case FUNCT:
          value16 = *(uint16_t *) MenuCtrlData[MenuHandle].MenuItems[i].Var;
          PrintPcCrt(MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[i].ColIndex].Field_x, MenuCtrlData[MenuHandle].ItemsPos[i].ItemPos.y, MenuCtrlData[MenuHandle].MenuItems[i].Msg);
          break;
         default:
          PrintPcCrt(MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[i].ColIndex].Field_x, MenuCtrlData[MenuHandle].ItemsPos[i].ItemPos.y, "???");
          break;
      }                       /* end switch */
    }
    i++;
  }
}


/* Create the menu intenal data */
int8_t CreateMenu(STRUCTMENU_T * menu, uint8_t x, uint8_t y, int8_t flag, int8_t MenuHandle)
{
  uint16_t length;
  uint8_t Handle = 0xFF;
  uint8_t i,j;
  char tempstr[20];
  uint16_t NbElem;
  char** ptr;

  uint8_t cur_x;
  uint8_t cur_y;
  uint8_t  x_win_offset , y_win_offset;
  uint8_t cur_col;
  uint8_t col_inter_space;
  uint8_t NbLine;
  

  uint8_t desc_max_length, cursor_offset, val_max_length;

  
  /* Look For free slot */

  if (!(flag & RECREATE))
  {
    for (i=0; i < MAX_MENU; i++)
    {
      if (!MenuCtrlData[i].created) break;
    }

    if (i == MAX_MENU)   /* No more room to create menu */
    {
      return -1;
    }
    Handle = i;
    
  }
  else
  {
    Handle = MenuHandle;
    menu = MenuCtrlData[Handle].MenuItems;
  }

  /* reset the data structure */
  memset (&MenuCtrlData[Handle], 0, sizeof(MenuCtrlData_t));
  MenuCtrlData[Handle].MenuItems = menu;
  /*------------------------------------------------------ */
  /* parse info from menu structure */
  
  /* ==== Look for the max number of menu lines to control exit function ==== */
  /* to be updated if WINDOW flag */

  if (flag & DRAW_WINDOW)
  {    
    x_win_offset = 1;
    y_win_offset = 1;
  }
  else
  {
    x_win_offset = 0;
    y_win_offset = 0;
  }

  MenuCtrlData[Handle].WinPos.WinPosUpperLeft.x = x;
  MenuCtrlData[Handle].WinPos.WinPosUpperLeft.y = y;

  MenuCtrlData[Handle].WinPos.WinPosDownRigth.x = x ;
  MenuCtrlData[Handle].WinPos.WinPosDownRigth.y = y;

      
  cur_x = x + x_win_offset;
  cur_y = y + y_win_offset;
  col_inter_space = 2;
  desc_max_length = 0;
  cursor_offset = 0;
  val_max_length = 0;
  cur_col = 0;
  NbLine = 0;

  
  for (i = 0; i < MAXITEMNB; i++)
  {
    /* end of Menu, leave */
    if (* MenuCtrlData[Handle].MenuItems[i].Item == '\0')   
    {
      if (i == 0)
      {
        return -1;  /* no Item ?? */
      }
      /* adjust Windows size anc check if fit in screen */      
      MenuCtrlData[Handle].WinPos.WinPosDownRigth.x += desc_max_length + cursor_offset + val_max_length + x_win_offset;

      /* Out of screen ? */
      if (MenuCtrlData[Handle].WinPos.WinPosDownRigth.x > MAX_X)
        return -1;

      if (MenuCtrlData[Handle].WinPos.WinPosDownRigth.y <= (cur_y + y_win_offset))
        MenuCtrlData[Handle].WinPos.WinPosDownRigth.y = cur_y + y_win_offset;

      /* Out of screen ? */
      if (MenuCtrlData[Handle].WinPos.WinPosDownRigth.y>= MAX_Y)
        return -1;

      /* set col info */   
      MenuCtrlData[Handle].ColInfo[cur_col].Cursor_x = cur_x +  desc_max_length;
      MenuCtrlData[Handle].ColInfo[cur_col].Field_x = cur_x + desc_max_length + cursor_offset;
      MenuCtrlData[Handle].ColInfo[cur_col].NbLines = NbLine;
      MenuCtrlData[Handle].NbCol = cur_col + 1;
      MenuCtrlData[Handle].created = 1; 
      
      break;
    }
    
    /* New colomn */
    if (* MenuCtrlData[Handle].MenuItems[i].Item == '\t')   
    {
      /* adjust Windows size  */      
      MenuCtrlData[Handle].WinPos.WinPosDownRigth.x += desc_max_length + cursor_offset + val_max_length + x_win_offset;


      if (MenuCtrlData[Handle].WinPos.WinPosDownRigth.y <= (cur_y + y_win_offset))
        MenuCtrlData[Handle].WinPos.WinPosDownRigth.y = cur_y + y_win_offset;

      /* set col info */   
      MenuCtrlData[Handle].ColInfo[cur_col].Cursor_x = cur_x +  desc_max_length;
      MenuCtrlData[Handle].ColInfo[cur_col].Field_x = cur_x + desc_max_length + cursor_offset;

      MenuCtrlData[Handle].ColInfo[cur_col].NbLines = NbLine;

      /* New col */
      cur_col++;

      /* store nb coluomns ? */
      if (cur_col > MAXCOLNB)
      {                         /* test if max reached */
        *menu[i].Item = '\0';   /* force end of menu instead of end of column */

        /* check Win Size */
        if (MenuCtrlData[Handle].WinPos.WinPosDownRigth.x > MAX_X)
         return -1;

        /* Out of screen ? */
        if (MenuCtrlData[Handle].WinPos.WinPosDownRigth.x >= MAX_Y)
         return -1;

        /* set col info */   
        MenuCtrlData[Handle].ColInfo[cur_col].Cursor_x = cur_x +  desc_max_length;
        MenuCtrlData[Handle].ColInfo[cur_col].Field_x = cur_x + desc_max_length + cursor_offset;
        MenuCtrlData[Handle].NbCol = cur_col;
        MenuCtrlData[Handle].created = 1; 

        break;
      }      
       
      MenuCtrlData[Handle].nbEntries++;  /* count Colum separator as entry */
      /* adjust cur_x */
      cur_x = cur_x + desc_max_length + cursor_offset + val_max_length +  col_inter_space; 

      /* reset cur_y */
      cur_y = y + y_win_offset;      

      cursor_offset = 1;
      desc_max_length = 0;
      val_max_length = 0;
      NbLine = 0;
    }
    else
    {
      if (i == MAXITEMNB)           /* table too long */
      {
        *menu[MAXITEMNB - 1].Item = '\0';   /* force end of menu */
        continue;
      }
       
      MenuCtrlData[Handle].nbItems++;
      MenuCtrlData[Handle].nbEntries++;
      MenuCtrlData[Handle].ItemsPos[i].ItemPos.x= cur_x;
      MenuCtrlData[Handle].ItemsPos[i].ItemPos.y= cur_y++; 
      MenuCtrlData[Handle].ItemsPos[i].ColIndex = cur_col;
      NbLine++;
      
      
      length = StringLength(menu[i].Item);
      desc_max_length = length > desc_max_length ?  length : desc_max_length;
      
     
      /* evaluate size of value */
      switch (menu[i].Type)
      {
        case TOGGLE:
        case SEL_TXT:
           MenuCtrlData[Handle].ItemsPos[i].Selectable = 1;
           MenuCtrlData[Handle].ItemSelectable = 1;
           cursor_offset = 3;
  
        case STRTAB:
          /* get pointer value of pointer of string */
          ptr = (char **) menu[i].Msg;
          j = 0;
          while (*ptr[j] != '\0') /* check all string */
          {
            length = StringLength(ptr[j++]);
            if ( val_max_length < length)
            {
              val_max_length = length;
            }
          }
          break;
  
        case ACTION:
          MenuCtrlData[Handle].ItemsPos[i].Selectable = 1;
          MenuCtrlData[Handle].ItemSelectable = 1;
          cursor_offset = 3;

  
        case TITLE:
          if (menu[i].Var != NULL)        /* if value to be displayed */
          {
            /* -- Test to print a value -- */
            sprintf((char *) tempstr, (char *) menu[i].Msg, 1);
            length = StringLength(tempstr);
          }
          else
          {
            length = StringLength(menu[i].Msg);
          }
  
          if ( val_max_length < length)
          {
            val_max_length = length;
          }
          break;
  
        case FUNCT:
          MenuCtrlData[Handle].ItemsPos[i].Selectable = 1;
          MenuCtrlData[Handle].ItemSelectable = 1;
          cursor_offset = 3;
          
          length = StringLength(menu[i].Msg);
          if (val_max_length < length)
          {
            val_max_length = length;
          }
          break;
  
        case SEL_NUM:
          MenuCtrlData[Handle].ItemsPos[i].Selectable = 1;
          MenuCtrlData[Handle].ItemSelectable = 1;
          cursor_offset = 3;
          
          /* compute the length */
          length = NbDigit((int32_t *) menu[i].Msg, &NbElem);
          if (val_max_length < length)
          {
            val_max_length = length;
          }
          break;
  
        case NUM32:
          MenuCtrlData[Handle].ItemsPos[i].Selectable = 1;
          MenuCtrlData[Handle].ItemSelectable = 1;          
          cursor_offset = 3;
  
        case VAL16:
        case SVAL32:
        case VAL32:
          /* -- Test to print a value -- */
          sprintf((char *) tempstr, (char *) menu[i].Msg, 1);
          length = StringLength(tempstr);
          if ( val_max_length < length)
          {
            val_max_length = length;
          }
          break;
  
        default:
          break;
       
      }
    }
  }
  
  /*---------------------------------------------------------------------------- */
  /*  -----                       Now Display                              ----- */
  /*---------------------------------------------------------------------------- */
  if (flag & DISPLAY)
    _DisplayMenu(Handle, flag);
  
  return Handle;
}


void DeleteMenu(int8_t MenuHandle)
{
  MenuCtrlData[MenuHandle].created = 0; 
}


/**
 *******************************************************************************
 * @fn      uint16_t Menu(STRUCTMENU_T * menu, uint16_t ItemNumb, int x, int y, 
 *                        int flag)
 * @author  -
 * @brief Displays the menu pointed by the parameters menu[], and select the 
 *      line indicated by 'line'
 *      with ARROW and VALID keys (issued from the UART or for the keypad), 
 *      it is possible to select the desired option.
 *      key : 'UP'      Select the previous available line
 *      key : 'DOWN'    Select the next available line
 *      key : 'LEFT'    Select the next available line to left with rollover to 
 *                      right
 *      key : 'RIGHT'   Select the next available line to right with rollover to 
 *                      left
 *      key : 'ENTER'   Validate the selected line with action depend on type
 *      key : 'ESC'     Exit from the menu
 *
 * @param menu :  Pointer of a structure containing the menu to be displayed.
 *          THE LAST LINE OF THE TABLE MUST BE : ""
 * @param ItemNumb : Item number to display.
 * @param x, y : Define the Column & Line where will be displayed the upper left
 *               corner of the menu
 * @param flag :   DISPLAY for full display only
 *                 CAPTURE for selection only
 *                 DISPLAY+CAPTURE for full display and selection
 *                 REFRESH to refresh variable values
 *
 * @return  ItemNumb : Return the Item selected.
 *          if ItemNumb = 0xFE ---> Home
 *          else               ---> ItemNumb selected
 *******************************************************************************
 **/
uint16_t RunMenu(uint8_t MenuHandle, uint16_t ItemNumb, uint8_t x, uint8_t y,
              uint8_t flag)
{
  uint16_t valid;
  uint8_t ytemp;
  char c;
  char **ptr;            /* pointer of a string pointer */
  static void (*funcptr) (void);        /* function pointer */
  static uint16_t value16;      /* for VAL16 and NUM16 types */
  STRUCTCAPTUREVAL_T *ptStruct;  
  static int32_t *ptSvalue32;
  static uint8_t PrintActionInfo = TRUE;
  static uint8_t PrintCursor = TRUE;

  if (flag & RECREATE)
  {
    /* Recreate the menu already created, this is just to allow a kind of dynamic update of complete menu items and location */
    if (CreateMenu(NULL, x,y,DISPLAY,MenuHandle) == -1)
    {
      return 0xFFFF; 
    }   
  }
  else
     _DisplayMenu(MenuHandle, flag);
    

  if ((flag & CAPTURE) && (MenuCtrlData[MenuHandle].ItemSelectable))
  {
    if (PrintActionInfo)
    {
      PrintPcCrt(1, MAX_Y,
                "Use : ARROWS to select an option ; 'RETURN' to validate, 'ESC' to exit");
      PrintActionInfo = FALSE;
    }
  }
  
  if (!MenuCtrlData[MenuHandle].ItemSelectable)
  {
    return 0xFFFF;  /* nothing to select */
  }
 
  /*------------------------------------------------------ */
  /* Main loop , waiting key an do some action if valid pressed */
  /* clear the special bit ItemNumber */
  ItemNumb &= ~0xFF00;
  if (flag & CAPTURE)           /* only if capture data */
  {
    valid = 0;
    
       /*--- init terminal display ---*/
        /*--- Do not accept a number of line greater than the number of line ---*/
    if (ItemNumb > MenuCtrlData[MenuHandle].nbEntries)
      ItemNumb = 0;
    skip_next_inactive_item2(ItemNumb,MenuCtrlData[MenuHandle]);
    if (PrintCursor)
    {
      PrintPcCrt(MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ColIndex].Cursor_x, 
                 MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ItemPos.y, 
                 "->");
    }
    while (!valid)
    {      
      c = UartReadChar(!(flag & CAPTURE_NON_BLOCKING));
      switch (c)
      {
                /*------- 'Home' or Escape key --------*/
      case ESC:
        ClearLines (MAX_Y, 1);
        PrintActionInfo = TRUE;
        if ((flag & CAPTURE_NON_BLOCKING))
          /* add ESC flag to return item number */       
          ItemNumb |=  ESC_FLAG;  
        else
          ItemNumb = HOME;     
        
        valid = TRUE;
        break;

                /*------- 'Down' key --------*/
      case '2':
      case DOWN:

        PrintPcCrt(MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ColIndex].Cursor_x, MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ItemPos.y, "  ");
        ItemNumb++;
        if (*MenuCtrlData[MenuHandle].MenuItems[ItemNumb].Item == '\0')
          ItemNumb = 0;         /* Roll over */
        skip_next_inactive_item2(ItemNumb,MenuCtrlData[MenuHandle] );

        PrintPcCrt(MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ColIndex].Cursor_x, MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ItemPos.y, "->");
        break;

                /*------- 'Up' key --------*/
      case '8':
      case UP:
        PrintPcCrt(MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ColIndex].Cursor_x, MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ItemPos.y, "  ");
        if (ItemNumb == 0)
          ItemNumb = MenuCtrlData[MenuHandle].nbEntries - 1;            /* max item number */
        else
          ItemNumb--;
        skip_previous_inactive_item2(ItemNumb,MenuCtrlData[MenuHandle]);

        PrintPcCrt(MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ColIndex].Cursor_x, MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ItemPos.y, "->");
        break;

                /*------- 'Left' key --------*/
      case '4':
      case LEFT:
        PrintPcCrt(MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ColIndex].Cursor_x, MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ItemPos.y, "  ");
        ytemp  = MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ItemPos.y;
        /* loop while find same y coord. */
        do
        {
          if (ItemNumb == 0)
            ItemNumb = MenuCtrlData[MenuHandle].nbEntries - 1 ;       /* max item number */
          else
            ItemNumb--;          
          skip_previous_inactive_item2(ItemNumb, MenuCtrlData[MenuHandle]);
        }
        while (MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ItemPos.y != ytemp);
        PrintPcCrt(MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ColIndex].Cursor_x, MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ItemPos.y, "->");
        break;

                /*------- 'Right' key --------*/
      case '6':
      case RIGHT:
        PrintPcCrt(MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ColIndex].Cursor_x, MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ItemPos.y, "  ");
        ytemp = MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ItemPos.y;
        /* loop while find same y coord. */
        do
        {
          ItemNumb++;
          if (*MenuCtrlData[MenuHandle].MenuItems[ItemNumb].Item == '\0')
            ItemNumb = 0;       /* Roll over */
          skip_next_inactive_item2(ItemNumb,MenuCtrlData[MenuHandle]);

        }
        while (MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ItemPos.y != ytemp);

       PrintPcCrt(MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ColIndex].Cursor_x, MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ItemPos.y, "->");
        break;

                /*------- 'valid' key --------*/
      case ENTER:
        switch (MenuCtrlData[MenuHandle].MenuItems[ItemNumb].Type)
        {
        case TOGGLE:           /* TOGGLE Value and display message */
          value16 = *(uint16_t *) MenuCtrlData[MenuHandle].MenuItems[ItemNumb].Var;   /* get value */
          value16 = ~value16 & 1;       /* toggle value */
          *(uint16_t *) MenuCtrlData[MenuHandle].MenuItems[ItemNumb].Var = value16;   /* write back new value */
          ptr = (char **) MenuCtrlData[MenuHandle].MenuItems[ItemNumb].Msg;   /* get pointer value of pointer of string */
          PrintPcCrt(MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ColIndex].Field_x, MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ItemPos.y, ptr[value16]);
          break;

        case SEL_TXT:          /* SELECTBOX on Item */
          ptr = (char **) MenuCtrlData[MenuHandle].MenuItems[ItemNumb].Msg;   /* get pointer value of pointer of string */
          value16 = *(uint16_t *) MenuCtrlData[MenuHandle].MenuItems[ItemNumb].Var;   /* get value */
          value16 =
            SelectBox(ptr, value16,MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ColIndex].Field_x, MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ItemPos.y,
                      CAPTURE);
          *(uint16_t *) MenuCtrlData[MenuHandle].MenuItems[ItemNumb].Var = value16;   /* write back new value */
          break;

        case SEL_NUM:          /* SELECT NUMBER BOX on Item */
          ptSvalue32 = (int32_t *) MenuCtrlData[MenuHandle].MenuItems[ItemNumb].Msg;
          value16 = *(uint16_t *) MenuCtrlData[MenuHandle].MenuItems[ItemNumb].Var;   /* get value */
          value16 =
            SelectNumberBox(ptSvalue32, value16,
                            MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ColIndex].Field_x, MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ItemPos.y,
                            CAPTURE);
          *(uint16_t *) MenuCtrlData[MenuHandle].MenuItems[ItemNumb].Var = value16;   /* write back new value */
          break;

        case NUM32:            /* 32 bits number entry */
          ptStruct = (STRUCTCAPTUREVAL_T *) MenuCtrlData[MenuHandle].MenuItems[ItemNumb].Var;
          GetNumberBox_Range(MenuCtrlData[MenuHandle].MenuItems[ItemNumb].Msg, ptStruct,
                             MenuCtrlData[MenuHandle].ColInfo[MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ColIndex].Field_x, MenuCtrlData[MenuHandle].ItemsPos[ItemNumb].ItemPos.y);
          break;

        case ACTION:           /* Action is made in user routine */
          break;

        case FUNCT:            /* Run user function */
          funcptr = (void (*)()) MenuCtrlData[MenuHandle].MenuItems[ItemNumb].Var;    /* get pointer value of pointer of function */
          funcptr();
          break;

        default:
          break;
        }
        ClearLines (MAX_Y, 1);
        PrintActionInfo= TRUE;
        if ((flag & CAPTURE_NON_BLOCKING))
         ItemNumb |=  ENTER_FLAG;
        valid = TRUE;
        
        break;
      }  
      if (flag & CAPTURE_NON_BLOCKING)
        valid = TRUE;
        
    }                           /* end while !VALID */
  }                             /* end if CAPTURE */
  return ItemNumb;
}


uint16_t CheckVaribleType(char *fmtStr)
{
  char lpCaract;
  uint16_t ii;
  uint16_t step;

  ii = 0;
  step = 0;
  while (*(fmtStr + ii) != 0)
  {
    lpCaract = *(fmtStr + ii);
    /* search for '%' character */
    if ((lpCaract == '%') && (step == 0))
    {
      step = 1;
    }
    if ((lpCaract == 'd') && (step == 1))
    {
      return SNUM;
    }
    if ((lpCaract == 'u') && (step == 1))
    {
      return UNUM;
    }
    if ((lpCaract == 'i') && (step == 1))
    {
      return SNUM;
    }
    if ((lpCaract == 'x') && (step == 1))
    {
      return UNUM;
    }
    if ((lpCaract == 'X') && (step == 1))
    {
      return UNUM;
    }
    ii++;
  }
  return FALSE;
}


/**
 *******************************************************************************
 * @fn      uint16_t SelectBox(int8_t * szMenu[], uint16_t iLineNumb, 
 *                             uint16_t iX, uint16_t iY)
 * @author  -
 * @brief Displays the option pointed by the parameters menu[], and select the 
 *      line indicated by 'line'.
 *      With UP, DOWN, and VALID keys (issued from the UART or for the keypad), 
 *      it is possible toselect the desired option. ONLY THE SELECTED LINE IS 
 *      DISPLAYED.
 *
 * @param szMenu : Pointer of a table containing the menu to be displayed.
 *             THE LAST LINE OF THE TABLE MUST BE : ""
 * @param iLineNumb : Line number to display .
 * @param iX, iY : Column & Line position of Upper Left corner of the Select box.
 *
 * @return: Return the line menu selected.
 *******************************************************************************
 **/
uint16_t SelectBox(char *szMenu[], uint8_t iLineNumb, uint8_t iX,
                   uint8_t iY, uint8_t mode)
{
  uint16_t valid, max, i, length, maxLength, end;

  valid = FALSE;
  max = 1;
  maxLength = 0;

  PrintPcCrt(1, 24,
             "Select option using 'UP', 'DOWN' and 'RETURN' keys                         ");

  /* -- Look for the max number of menu lines to control exit function -- */
  for (i = 0; i < 64; i++)
  {
    if (*szMenu[i] == '\0')
    {
      max = i - 1;
      break;
    }
        /*--- Search the max length of the strings ---*/
    length = StringLength(szMenu[i]);
    if (maxLength < length)
    {
      maxLength = length;
    }
  }

  while (!valid)
  {
        /*--- Display the szMenu, and erase the used area ---*/
    PrintPcCrt(iX, iY, "%s", szMenu[iLineNumb]);
    end = maxLength - StringLength(szMenu[iLineNumb]);

    for (i = 0; i < end; i++)
    {
      PrintCrt(" ");
    }

    /* =====  Display only  ===== */
    if (mode != CAPTURE)
    {
      break;
    }

    PrintCrt("<");
    switch (UartReadChar(TRUE))
    {
                /*------- 'UP' key --------*/
    case '8':
    case UP:
      iLineNumb++;
      if (*szMenu[iLineNumb] == '\0')
      {
        iLineNumb = 0;
      }
      break;

                /*------- 'DOWN' key --------*/
    case '2':
    case DOWN:
      if (iLineNumb == 0)
      {
        iLineNumb = max;
      }
      else
      {
        iLineNumb--;
      }
      break;

                /*------- 'valid' or 'ENTER' key --------*/
    case ENTER:
      /* -- Erase the '<' -- */
      PrintPcCrt((iX + maxLength), iY, " ");
      ClearLines (24, 1);
      valid = TRUE;
      break;
    }
  }

  return (iLineNumb);
}


/**
 *******************************************************************************
 * @fn      uint16_t SelectNumberBox(int32_t * ptTabNber, uint16_t iLineNumb, 
 *                                   uint16_t iX, uint16_t iY)
 * @author  -
 * @brief Displays the option pointed by the parameters *ptTabNber, and select 
 *      the line indicated by 'iLineNumb'.
 *      With UP, DOWN, and VALID keys (issued from the UART or for the keypad), 
 *      it is possible to
 *      select the desired option. ONLY THE SELECTED LINE IS DISPLAYED.
 *
 * @param ptTabNber : Pointer of a table containing the menu to be displayed.
 *             THE LAST LINE OF THE TABLE IS DETECTED WHEN 2 CONSECUTIVE VALUES 
 *             ARE EQUAL
 * @param iLineNumb : Line number to display .
 * @param iX, iY : Column & Line position of Upper Left corner of the Select box.
 *
 * @return: Return the line menu selected.
 **************************************************************************
 **/
uint16_t SelectNumberBox(int32_t * ptTabNber, uint8_t iLineNumb,
                         uint8_t iX, uint8_t iY, uint8_t mode)
{
  uint16_t valid, max, maxLength;

  char szFormat[10];

  szFormat[0] = '%';
  szFormat[2] = 'd';
  szFormat[3] = 0;

  valid = FALSE;
  max = 1;
  maxLength = 0;

  /* -- Look for the max number of menu lines to control exit function -- */
  maxLength = NbDigit(ptTabNber, &max);

  while (!valid)
  {
                                /*--- Display the szMenu, and erase the used area ---*/
    szFormat[1] = '0' + maxLength;

    /* =====  Display only  ===== */
    if (mode != CAPTURE)
    {
      PrintPcCrt(iX, iY, szFormat, *(ptTabNber + iLineNumb));
      break;
    }
    PrintPcCrt(1, 24,
               "Select option using 'UP', 'DOWN' and 'RETURN' keys                         ");
    PrintPcCrt(iX, iY, szFormat, *(ptTabNber + iLineNumb));
    PrintCrt("<");

    switch (UartReadChar(TRUE))
    {
                                                /*------- 'UP' key --------*/
    case '8':
    case UP:
      if (iLineNumb == max)
      {
        iLineNumb = 0;
      }
      else
      {
        iLineNumb++;
      }
      break;

                                                /*------- 'DOWN' key --------*/
    case '2':
    case DOWN:
      if (iLineNumb == 0)
      {
        iLineNumb = max;
      }
      else
      {
        iLineNumb--;
      }
      break;

                                                /*------- 'valid' or 'ENTER' key --------*/
    case ENTER:
      /* -- Erase the '<' -- */
      PrintPcCrt((iX + maxLength), iY, " ");
      ClearLines (24, 1);
      valid = TRUE;
      break;
    }
  }
  return (iLineNumb);
}

/**
 *******************************************************************************
 * @fn      void GetNumberBox_Range( char * szFmt, void * CaptureVal, 
 *                                   STRUCTRANGE * stRangeVal, uint16_t iX, 
 *                                   uint16_t iY)
 * @author  -
 * @brief This function allow displays on a terminal the text pointed by 
 *      "szFmt", to the positioniX, iY. If the string contains format characters
 *      such as "%d, %4d, %06d, %04x...;",
 *      the function will display the default value 'CaptureVal' and wait for an
 *      hexa number or decimal number. The number entried is validate by the 
 *      'ENTER' key. This function will return the converted number only when 
 *      the number was correctly decoded.
 *
 * @param szFmt : Points the formated string containing informations on the 
 *                message to display,
 *                and on the number to get.
 * @param CaptureVal : Is the default value to display
 * @param iX, iY : Column & Line position.
 *
 * @return: Return the binary value of the number captured.
 *******************************************************************************
 **/
void GetNumberBox_Range(char *szFmt, STRUCTCAPTUREVAL_T * stRangeVal,
                        uint8_t iX, uint8_t iY)
{
  uint8_t cLength, cStatus;
  uint16_t iCol, iCpt;
  char szFormat[10], szString[20];
  int8_t *lpCaract;
  uint8_t ValidRange;

  int32_t sNumber;
  uint32_t uNumber;

  cStatus = FALSE;
  while (cStatus == FALSE)
  {
    cStatus = TRUE;
    iCpt = 0;
    iCol = 0;
    PosCur(iX, iY);

    for (lpCaract = (int8_t *) szFmt; (*lpCaract != 0) && (cStatus != FALSE);
         lpCaract++)
    {
      if (*lpCaract != '%')
      {
        PrintCrt("%c", *lpCaract);
        iCol++;
        continue;
      }
      szFormat[iCpt++] = *lpCaract;
      lpCaract++;
      while ((*lpCaract == '+'))
      {
        lpCaract++;
      }

      cLength = 0;
      if ((*lpCaract == '0') || (*lpCaract == '-'))
      {
        szFormat[iCpt++] = *lpCaract;
        lpCaract++;
      }
      while (IsDigit(*lpCaract))
      {
        szFormat[iCpt++] = *lpCaract;
        cLength = cLength * 10 + (*lpCaract++ - '0');
      }
      if (*lpCaract == 'l')
      {
        lpCaract++;
      }

      switch (*lpCaract)
      {
      case 'd':
      case 'i':
        szFormat[iCpt++] = *lpCaract;
        szFormat[iCpt++] = 0;
        ValidRange = 0;
        sNumber = *((int32_t *) stRangeVal->CapValue);
        /* Display a message if range check is enabled */
        if (*((int32_t *) stRangeVal->MinValue) !=
            *((int32_t *) stRangeVal->MaxValue))
        {
          PrintPcCrt(1, 24,
                     "The number must be between %d and %d                                       ",
                     *((int32_t *) stRangeVal->MinValue),
                     *((int32_t *) stRangeVal->MaxValue));
        }
        while (ValidRange == 0)
        {
          sprintf((char *) szString, (char *) &szFormat[0], sNumber);
          if (cLength == 0)
          {
            cLength = 10;
          }
          ReadPcStr((iX + iCol), iY, szString, (int8_t) cLength, '.', TRUE,
                    OVERSTK);
          if (AToDec(szString, &sNumber) == FALSE)
          {
            cStatus = FALSE;
          }

          /* if captured number is between MinValue and  MaxValue */
          if ((sNumber >= *((int32_t *) stRangeVal->MinValue))
              && (sNumber <= *((int32_t *) stRangeVal->MaxValue)))
          {
            ValidRange = 1;
          }

          /* if MinValue = MaxValue, then no range is enabled */
          if (*((int32_t *) stRangeVal->MinValue) >=
              *((int32_t *) stRangeVal->MaxValue))
          {
            ValidRange = 1;
          }

        }
        /* value updated only when it is valid */
        *((int32_t *) stRangeVal->CapValue) = sNumber;
        iCol = iCol + cLength;
        break;

      case 'u':
      case 'x':
      case 'X':
        szFormat[iCpt++] = *lpCaract;
        szFormat[iCpt++] = 0;
        ValidRange = 0;
        uNumber = *((uint32_t *) stRangeVal->CapValue);
        /* Display a message if range check is enabled */
        if (*((uint32_t *) stRangeVal->MinValue) !=
            *((uint32_t *) stRangeVal->MaxValue))
        {
          PrintPcCrt(1, 24,
                     "The number must be between 0x%X and 0x%X                                       ",
                     *((uint32_t *) stRangeVal->MinValue),
                     *((uint32_t *) stRangeVal->MaxValue));
        }
        while (ValidRange == 0)
        {
          sprintf((char *) szString, (char *) &szFormat[0], uNumber);
          if (cLength == 0)
          {
            cLength = 8;
          }

          ReadPcStr((iX + iCol), iY, szString, (int8_t) cLength, '.', TRUE,
                    OVERSTK);
          if (AToHex(szString, &uNumber) == FALSE)
          {
            cStatus = FALSE;
          }

          /* if captured number is between MinValue and  MaxValue */
          if ((uNumber >= *((uint32_t *) stRangeVal->MinValue))
              && (uNumber <= *((uint32_t *) stRangeVal->MaxValue)))
          {
            ValidRange = 1;
          }

          /* if MinValue = MaxValue, then no range is enabled */
          if (*((uint32_t *) stRangeVal->MinValue) >=
              *((uint32_t *) stRangeVal->MaxValue))
          {
            ValidRange = 1;
          }
        }

        /* value updated only when it is valid */
        *((uint32_t *) stRangeVal->CapValue) = uNumber;
        iCol = iCol + cLength;
        break;
      }
    }
  }
  ClearLines (24, 1);
}

uint16_t NbDigit(int32_t * ptTabNber, uint16_t * NbElements)
{
  int32_t Prev;
  uint16_t ii;
  uint16_t kk;
  int32_t div;
  uint16_t maxLength;

  maxLength = 0;

  /* -- Look for the max number of menu lines to control exit function -- */
  Prev = *ptTabNber + 1;        /* shall be different from the fistr value */
  for (ii = 0; ii < 64; ii++)
  {
    /* consecutive duplicated value indicates the end of the list */
    if (*(ptTabNber + ii) == Prev)
    {
      break;
    }
        /*--- Search the max length of the strings ---*/
    div = 10;
    for (kk = 1; kk < 10; kk++)
    {
      if (*(ptTabNber + ii) / div == 0)
      {
        break;
      }
      div = div * 10;
    }
    if (*(ptTabNber + ii) < 0)
    {                           /* If the number is negative, a digital is added for '-' */
      kk++;
    }
    if (maxLength < kk)
    {
      maxLength = kk;
    }
    Prev = *(ptTabNber + ii);
  }
  *NbElements = ii - 1;
  return maxLength;
}