menu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*************************************************************************
  2. *
  3. * Used with ICCARM and AARM.
  4. *
  5. * (c) Copyright IAR Systems 2003
  6. *
  7. * File name : main.c
  8. * Description : Define main module
  9. *
  10. * History :
  11. * 1. Data : Feb 10, 2005
  12. * Author : Stanimir Bonev
  13. * Description : Create
  14. *
  15. * $Revision: 1.1 $
  16. **************************************************************************/
  17. //#include "user_func.h"
  18. #include "includes.h"
  19. #include "menu_table.c"
  20. typedef struct
  21. {
  22. MENU_EVENT_TYPE_DEF * Push;
  23. MENU_EVENT_TYPE_DEF * Pop;
  24. MENU_EVENT_TYPE_DEF EventStackBuffer [MENU_EVENT_STACK_SIZE]; /* Hold enevts */
  25. } EVENT_STACK_DEF;
  26. #define MENU_EVET_STACK_EMPTY (LPC_BOOL)0 /* Event stack is empty */
  27. #define MENU_EVET_STACK_DATA_VALID (LPC_BOOL)1 /* Event stack data valid */
  28. EVENT_STACK_DEF EventStack;
  29. const MENU_VISUAL_DEF *const * LanguageBaseAdd;
  30. MEMU_IND_DEF MenuInd;
  31. MEMU_TIME_OUT_DEF MenuTO;
  32. /*************************************************************************
  33. * Function Name: MenuStackInit
  34. * Parameters: None
  35. * Return: none
  36. * Description: Initialize menu event stack structure
  37. *
  38. *************************************************************************/
  39. void MenuStackInit ()
  40. {
  41. EventStack.Push = EventStack.Pop = EventStack.EventStackBuffer;
  42. }
  43. /*************************************************************************
  44. * Function Name: MenuShow
  45. * Parameters: MEMU_STING_DEF *pVarData
  46. * Return: MENU_ERROR_CODE_DEF
  47. * MENU_OK 0: sucess
  48. * MENU_LANG_ERROR 1: fail not valid language
  49. * MENU_IND_ERROR 2: fail not valid menu index
  50. * MENU_SHOW_OUT_OF_VISUAL 4: fail String out of visual space
  51. * MENU_SHOW_ERROR 5: fail Error into LCD driver module
  52. * Description: Show current visual chain to LCD
  53. *
  54. *************************************************************************/
  55. MENU_ERROR_CODE_DEF MenuShow (MEMU_STING_DEF *pVarData)
  56. {
  57. MENU_VISUAL_DEF * pData = (MENU_VISUAL_DEF *)*(LanguageBaseAdd+MenuInd);
  58. MEMU_STING_DEF DataStr[MENU_MAX_STR_LENG];
  59. MEMU_STING_DEF * pDataHold = NULL;
  60. MEMU_STING_DEF * pDestData;
  61. const MEMU_STING_DEF * pSourceData;
  62. LPC_INT8U Result = MENU_OK;
  63. if (MenuInd > MENU_MAX_INDEX-1)
  64. {
  65. return MENU_IND_ERROR;
  66. }
  67. else if (pVarData == NULL)
  68. {
  69. return (MENU_ERROR_CODE_DEF) Result;
  70. }
  71. else
  72. {
  73. while (pData->MenuText) {
  74. pSourceData = pData->MenuText;
  75. pDestData = DataStr;
  76. while(*pSourceData || (pDataHold != NULL))
  77. {
  78. switch (*pSourceData)
  79. {
  80. case END_OF_STR:
  81. pVarData = (char *)++pSourceData;
  82. pSourceData = pDataHold;
  83. pDataHold = NULL;
  84. break;
  85. case VARIABLE_DATA:
  86. if((pVarData != NULL) && (*pVarData))
  87. {
  88. pDataHold = (char *)++pSourceData;
  89. pSourceData = pVarData;
  90. }
  91. else
  92. {
  93. pVarData = NULL;
  94. }
  95. break;
  96. case END_OF_VAR_DATA:
  97. if(pDataHold != NULL)
  98. {
  99. pVarData = (char *)++pSourceData;
  100. pSourceData = pDataHold;
  101. pDataHold = NULL;
  102. }
  103. else
  104. {
  105. ++pSourceData;
  106. }
  107. break;
  108. default:
  109. if((pDestData - DataStr) >= MENU_MAX_STR_LENG-1) break;
  110. *pDestData = *pSourceData;
  111. ++pDestData;++pSourceData;
  112. break;
  113. }
  114. }
  115. *pDestData = 0;
  116. Result = HD44780_StrShow(pData->X,pData->Y,(LPC_INT8S const *)DataStr);
  117. switch (Result)
  118. {
  119. case HD44780_BUSY_TO_ERROR:
  120. case HD44780_ERROR:
  121. return MENU_SHOW_ERROR;
  122. case HD44780_OUT_OF_VISUAL:
  123. Result = MENU_SHOW_OUT_OF_VISUAL;
  124. }
  125. ++pData;
  126. }
  127. }
  128. return (MENU_ERROR_CODE_DEF) Result;
  129. }
  130. /*************************************************************************
  131. * Function Name: MenuInit
  132. * Parameters: MENU_LANG_TYPE_DEF Language,
  133. * MEMU_IND_DEF MainMenuInd,
  134. * MEMU_STING_DEF *pVarData,
  135. * MEMU_TIME_OUT_DEF MenuTO_Init
  136. * Return: MENU_ERROR_CODE_DEF
  137. * MENU_OK 0: sucess
  138. * MENU_LANG_ERROR 1: fail not valid language
  139. * MENU_IND_ERROR 2: fail not valid menu index
  140. * MENU_SHOW_OUT_OF_VISUAL 4: fail String out of visual space
  141. * MENU_SHOW_ERROR 5: fail Error into LCD driver module
  142. * MENU_TO_ERROR 6: fail Menu tome out is to long
  143. * Description: Initialize Menu
  144. *
  145. *************************************************************************/
  146. MENU_ERROR_CODE_DEF MenuInit (MENU_LANG_TYPE_DEF Language,
  147. MEMU_IND_DEF MainMenuInd, MEMU_STING_DEF *pVarData, MEMU_TIME_OUT_DEF MenuTO_Init)
  148. {
  149. /* Init Menu Events stack */
  150. MenuStackInit();
  151. if (Language > MENU_LANGUAGE_NUMBER-1)
  152. {
  153. return MENU_LANG_ERROR;
  154. }
  155. LanguageBaseAdd = MenuLanguageBaseAddTable[Language];
  156. if (MainMenuInd > MENU_MAX_INDEX-1)
  157. {
  158. return MENU_IND_ERROR;
  159. }
  160. MenuInd = MainMenuInd;
  161. if (MenuTO_Init > MEMU_MAX_TIME_OUT)
  162. {
  163. return MENU_TO_ERROR;
  164. }
  165. MenuTO = MenuTO_Init;
  166. return MenuShow(pVarData);
  167. }
  168. /*************************************************************************
  169. * Function Name: MenuGetEvent
  170. * Parameters: MENU_EVENT_TYPE_DEF * MenuEvent
  171. * Return: LPC_BOOL
  172. * MENU_EVET_STACK_EMPTY 0: Event stack is empty not valid data
  173. * MENU_EVET_STACK_DATA_VALID 1: Event stack is not empty valid data
  174. * Description: Pop menu event from stack
  175. *
  176. *************************************************************************/
  177. LPC_BOOL MenuGetEvent (MENU_EVENT_TYPE_DEF * MenuEvent)
  178. {
  179. if (EventStack.Pop == EventStack.Push)
  180. {
  181. return MENU_EVET_STACK_EMPTY;
  182. }
  183. /* Function MenuSetEvent is reentrant */
  184. *MenuEvent = *EventStack.Pop;
  185. __disable_interrupt();
  186. if (++EventStack.Pop == EventStack.Push)
  187. {
  188. MenuStackInit();
  189. }
  190. __enable_interrupt();
  191. return MENU_EVET_STACK_DATA_VALID;
  192. }
  193. /*************************************************************************
  194. * Function Name: MenuSetEvent
  195. * Parameters: MENU_EVENT_TYPE_DEF MenuEvent
  196. * Return: MENU_ERROR_CODE_DEF
  197. * MENU_OK 0: Push Event to stack
  198. * MENU_EVET_STACK_OVERFLOW 3: Stack overflow
  199. * Description: Push menu event to stack
  200. *
  201. *************************************************************************/
  202. MENU_ERROR_CODE_DEF MenuSetEvent (MENU_EVENT_TYPE_DEF MenuEvent)
  203. {
  204. if (EventStack.Push == &EventStack.EventStackBuffer[MENU_EVENT_STACK_SIZE])
  205. {
  206. return MENU_EVET_STACK_OVERFLOW;
  207. }
  208. *EventStack.Push = MenuEvent;
  209. ++EventStack.Push;
  210. return MENU_OK;
  211. }
  212. /*************************************************************************
  213. * Function Name: Menu
  214. * Parameters: none
  215. * Return: MENU_ERROR_CODE_DEF
  216. * MENU_OK 0: Push Event to stack
  217. * MENU_EVET_STACK_OVERFLOW 3: Stack overflow
  218. * Description: Implenet menu function, pop menu event and determinate actions,
  219. * set new visual chain, call to User function ..
  220. *
  221. *************************************************************************/
  222. MENU_ERROR_CODE_DEF Menu ()
  223. {
  224. MENU_EVENT_TYPE_DEF MenuEvent;
  225. MENU_ACTION_SHAIN_DEF * pMenuActionChain;
  226. MEMU_STING_DEF DataStr[MENU_MAX_STR_LENG];
  227. MEMU_STING_DEF * pDestData = DataStr;
  228. const MEMU_STING_DEF * pSourceData;
  229. LPC_BOOL ShowUpdate = FALSE;
  230. MENU_ERROR_CODE_DEF Result = MENU_OK;
  231. if(MenuGetEvent(&MenuEvent) == MENU_EVET_STACK_DATA_VALID)
  232. {
  233. /* Menu time out implement */
  234. if (MenuEvent == MENU_TICK_EVENT)
  235. {
  236. if (MenuTO)
  237. {
  238. if(--MenuTO)
  239. {
  240. return Result;
  241. }
  242. }
  243. else
  244. {
  245. return Result;
  246. }
  247. }
  248. /* Menu action time out implement */
  249. pMenuActionChain = (MENU_ACTION_SHAIN_DEF *)MenuActionChainAdd[MenuInd];
  250. while(pMenuActionChain->EventType)
  251. {
  252. if(pMenuActionChain->EventType != MenuEvent)
  253. {
  254. ++pMenuActionChain;
  255. }
  256. else
  257. {
  258. break;
  259. }
  260. }
  261. if (pMenuActionChain->MenuIndex)
  262. {
  263. if (MenuInd != pMenuActionChain->MenuIndex)
  264. {
  265. ShowUpdate = TRUE;
  266. MenuInd = pMenuActionChain->MenuIndex;
  267. }
  268. }
  269. if(pMenuActionChain->pVarArg != NULL)
  270. {
  271. ShowUpdate = TRUE;
  272. pSourceData = pMenuActionChain->pVarArg;
  273. while(*pSourceData)
  274. {
  275. *pDestData=*pSourceData;
  276. ++pSourceData;++pDestData;
  277. }
  278. }
  279. if (pMenuActionChain->MenuTO != MENU_TIME_OUT_NOT_UPADATE)
  280. {
  281. MenuTO = pMenuActionChain->MenuTO;
  282. }
  283. if(pMenuActionChain->UserFunc != NULL)
  284. {
  285. ShowUpdate = TRUE;
  286. pMenuActionChain->UserFunc(DataStr,&MenuInd,&MenuTO);
  287. }
  288. if (ShowUpdate)
  289. {
  290. Result = MenuShow(DataStr);
  291. if(pMenuActionChain->UserFunc1 != NULL)
  292. {
  293. pMenuActionChain->UserFunc1(DataStr,&MenuInd,&MenuTO);
  294. }
  295. }
  296. }
  297. return Result;
  298. }
  299. /*************************************************************************
  300. * Function Name: MenuGetInd
  301. * Parameters: none
  302. * Return: MENU_EVENT_TYPE_DEF
  303. *
  304. * Description: Return current menu index
  305. *
  306. *************************************************************************/
  307. MENU_EVENT_TYPE_DEF MenuGetInd(void)
  308. {
  309. return MenuInd;
  310. }
  311. /*************************************************************************
  312. * Function Name: MenuGetActionChain
  313. * Parameters: MEMU_IND_DEF MenuInd
  314. * Return: MENU_ACTION_SHAIN_DEF *
  315. *
  316. * Description: Return pointer to stucture of action chain
  317. *
  318. *************************************************************************/
  319. MENU_ACTION_SHAIN_DEF * MenuGetActionChain(MEMU_IND_DEF MenuInd)
  320. {
  321. if (MenuInd > MENU_MAX_INDEX-1)
  322. {
  323. return (void *)0;
  324. }
  325. return (MENU_ACTION_SHAIN_DEF *)MenuActionChainAdd[MenuInd];
  326. }
  327. /*************************************************************************
  328. * Function Name: MenuGetVisualChain
  329. * Parameters: MENU_LANG_TYPE_DEF Language,
  330. * MEMU_IND_DEF MenuInd
  331. * Return: MENU_VISUAL_DEF *
  332. *
  333. * Description: Return pointer to stucture of visual chain
  334. *
  335. *************************************************************************/
  336. MENU_VISUAL_DEF * MenuGetVisualChain(MENU_LANG_TYPE_DEF Language,MEMU_IND_DEF MenuInd)
  337. {
  338. if ((MenuInd < MENU_MAX_INDEX) && (Language < MENU_LANGUAGE_NUMBER))
  339. {
  340. return (MENU_VISUAL_DEF *)(*MenuLanguageBaseAddTable[Language]+MenuInd);
  341. }
  342. return (MENU_VISUAL_DEF *)0;
  343. }