GUIDEMO_Graph.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*********************************************************************
  2. * Portions COPYRIGHT 2016 STMicroelectronics *
  3. * Portions SEGGER Microcontroller GmbH & Co. KG *
  4. * Solutions for real time microcontroller applications *
  5. **********************************************************************
  6. * *
  7. * (c) 1996 - 2015 SEGGER Microcontroller GmbH & Co. KG *
  8. * *
  9. * Internet: www.segger.com Support: support@segger.com *
  10. * *
  11. **********************************************************************
  12. ** emWin V5.28 - Graphical user interface for embedded applications **
  13. All Intellectual Property rights in the Software belongs to SEGGER.
  14. emWin is protected by international copyright laws. Knowledge of the
  15. source code may not be used to write a similar product. This file may
  16. only be used in accordance with the following terms:
  17. The software has been licensed to STMicroelectronics International
  18. N.V. a Dutch company with a Swiss branch and its headquarters in Plan-
  19. les-Ouates, Geneva, 39 Chemin du Champ des Filles, Switzerland for the
  20. purposes of creating libraries for ARM Cortex-M-based 32-bit microcon_
  21. troller products commercialized by Licensee only, sublicensed and dis_
  22. tributed under the terms and conditions of the End User License Agree_
  23. ment supplied by STMicroelectronics International N.V.
  24. Full source code is available at: www.segger.com
  25. We appreciate your understanding and fairness.
  26. ----------------------------------------------------------------------
  27. File : GUIDEMO_Graph.c
  28. Purpose : Several GUIDEMO routines
  29. ----------------------------------------------------------------------
  30. */
  31. /**
  32. ******************************************************************************
  33. * @file GUIDEMO_Graph.c
  34. * @author MCD Application Team
  35. * @brief Several GUIDEMO routines
  36. ******************************************************************************
  37. * @attention
  38. *
  39. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics International N.V.
  40. * All rights reserved.</center></h2>
  41. *
  42. * Redistribution and use in source and binary forms, with or without
  43. * modification, are permitted, provided that the following conditions are met:
  44. *
  45. * 1. Redistribution of source code must retain the above copyright notice,
  46. * this list of conditions and the following disclaimer.
  47. * 2. Redistributions in binary form must reproduce the above copyright notice,
  48. * this list of conditions and the following disclaimer in the documentation
  49. * and/or other materials provided with the distribution.
  50. * 3. Neither the name of STMicroelectronics nor the names of other
  51. * contributors to this software may be used to endorse or promote products
  52. * derived from this software without specific written permission.
  53. * 4. This software, including modifications and/or derivative works of this
  54. * software, must execute solely and exclusively on microcontroller or
  55. * microprocessor devices manufactured by or for STMicroelectronics.
  56. * 5. Redistribution and use of this software other than as permitted under
  57. * this license is void and will automatically terminate your rights under
  58. * this license.
  59. *
  60. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  61. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  62. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  63. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  64. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  65. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  66. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  67. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  68. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  69. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  70. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  71. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  72. *
  73. ******************************************************************************
  74. */
  75. #include "GUIDEMO.h"
  76. #if (SHOW_GUIDEMO_GRAPH && GUI_SUPPORT_MEMDEV)
  77. /*********************************************************************
  78. *
  79. * Configuration defines
  80. *
  81. **********************************************************************
  82. */
  83. #define MAX_GRAPH_SIZE_X 440
  84. #define MAX_GRAPH_SIZE_Y (MAX_GRAPH_SIZE_X * 11) / 20
  85. #define DIST_TO_BORDER 10
  86. #define DIST_TO_WIN 5
  87. #define BORDER_TOP 0
  88. #define BORDER_BOTTOM 9
  89. #define BORDER_LEFT 19
  90. #define BORDER_RIGHT 0
  91. #define COLOR_BK GUI_DARKGRAY
  92. #define COLOR_BORDER BK_COLOR_1
  93. #define COLOR_FRAME GUI_BLACK
  94. #define COLOR_GRID GUI_GRAY
  95. #define SCALE_H_HEIGHT 4
  96. #define TICK_DIST_H 25
  97. #define TICK_DIST_V 20
  98. #define TIME_RUN 5000
  99. #define TIME_STEP 15
  100. #define HEARTBEAT_TIME 1000
  101. #define MAX_NUM_DATA_OBJ 5
  102. #define GRAPH_DIV 9 // (2^9 = 512) If this value is changed _aWaves[] need to be changed too!
  103. #define GRID_DIST_X 25
  104. #define GRID_DIST_Y 10
  105. #define GRID_OFF_Y 25
  106. /*********************************************************************
  107. *
  108. * Typedef / Data
  109. *
  110. **********************************************************************
  111. */
  112. typedef struct {
  113. char * Name;
  114. int ScaleVOff;
  115. int DataVOff;
  116. int GridVOff;
  117. void (* pfAddData)(GRAPH_DATA_Handle hData, int DataID);
  118. int NumWaves;
  119. } GRAPH_WAVE;
  120. static int _HeartBeat[] = {
  121. 2, 4, 6, 8, 10, 6, 2, 0, 0, 0,
  122. -8, 16, 40, 64, 88, 58, 28, -2, -32, -30,
  123. -20, -10, 0, 2, 2, 4, 4, 6, 6, 8,
  124. 8, 10, 12, 14, 16, 18, 20, 16, 12, 8,
  125. 4, 2, 2, 0, 0, 0, 0, 0, 0, 0,
  126. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  127. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  128. };
  129. static GUI_COLOR _aColorData[MAX_NUM_DATA_OBJ] = {
  130. 0x50C0FF,
  131. 0xFFC050,
  132. 0x50FFC0,
  133. 0x800000,
  134. 0x000080
  135. };
  136. GRAPH_SCALE_Handle _hScaleH, _hScaleV;
  137. static int _DataAdjust;
  138. /*********************************************************************
  139. *
  140. * Static functions
  141. *
  142. **********************************************************************
  143. */
  144. /*********************************************************************
  145. *
  146. * _AddData_Sine
  147. */
  148. static void _AddData_Sine(GRAPH_DATA_Handle hData, int DataID) {
  149. static int x1000[MAX_NUM_DATA_OBJ];
  150. I32 SinHQ;
  151. int Multi, Step;
  152. switch (DataID) {
  153. case 0:
  154. Multi = 70;
  155. Step = 3;
  156. break;
  157. case 1:
  158. Multi = 50;
  159. Step = 1;
  160. break;
  161. case 2:
  162. Multi = 30;
  163. Step = 7;
  164. break;
  165. default:
  166. return;
  167. }
  168. SinHQ = GUI__SinHQ(x1000[DataID]);
  169. x1000[DataID] += 1000 * Step;
  170. GRAPH_DATA_YT_AddValue(hData, ((SinHQ * Multi) >> 16) + _DataAdjust);
  171. }
  172. /*********************************************************************
  173. *
  174. * _AddData_Heartbeat
  175. */
  176. static void _AddData_Heartbeat(GRAPH_DATA_Handle hData, int DataID) {
  177. static int Index;
  178. GUI_USE_PARA(DataID);
  179. GRAPH_DATA_YT_AddValue(hData, (_HeartBeat[Index]) + _DataAdjust);
  180. if (++Index == GUI_COUNTOF(_HeartBeat)) {
  181. Index = 0;
  182. }
  183. }
  184. /*********************************************************************
  185. *
  186. * DATA _aWave - Keep below _AddData-functions
  187. */
  188. GRAPH_WAVE _aWave[] = {
  189. { "Heartbeat", // Name
  190. 157, // Vertical scale offset in relation to GRAPH_DIV
  191. 152, // Vertical data offset in relation to GRAPH_DIV
  192. 21, // Vertical grid offset in relation to GRAPH_DIV
  193. _AddData_Heartbeat, // Pointer to specific AddData function
  194. 1 // Number of waves
  195. },
  196. { "Sine Waves",
  197. 265,
  198. 253,
  199. 23,
  200. _AddData_Sine,
  201. 3
  202. },
  203. 0
  204. };
  205. /*********************************************************************
  206. *
  207. * _ShowGraph
  208. */
  209. static void _ShowGraph(GRAPH_Handle hGraph, GRAPH_DATA_Handle hData[], int DataCount, void (* pfAddData)(GRAPH_DATA_Handle hData, int DataID)) {
  210. int Count, Data_xSize, xSize;
  211. int TimeStart, TimeDiff, TimeStep;
  212. int NextState, i, Flag;
  213. xSize = LCD_GetXSize();
  214. Data_xSize = xSize - (DIST_TO_BORDER << 1) - (BORDER_LEFT + BORDER_RIGHT);
  215. Count = 0;
  216. //
  217. // Attach data objects
  218. //
  219. for (i = 0; i < DataCount; i++) {
  220. GRAPH_AttachData(hGraph, hData[i]);
  221. }
  222. //
  223. // Add values before GRAPH is displayed
  224. //
  225. while (Count < Data_xSize) {
  226. for (i = 0; i < DataCount; i++) {
  227. pfAddData(hData[i], i);
  228. }
  229. Count++;
  230. }
  231. //
  232. // Add values depending on time
  233. //
  234. TimeStart = GUIDEMO_GetTime();
  235. Flag = 1;
  236. do {
  237. TimeDiff = GUIDEMO_GetTime() - TimeStart;
  238. for (i = 0; i < DataCount; i++) {
  239. pfAddData(hData[i], i);
  240. }
  241. if (Flag) {
  242. Flag = 0;
  243. GUI_Exec();
  244. GRAPH_DetachScale(hGraph, _hScaleH);
  245. GRAPH_DetachScale(hGraph, _hScaleV);
  246. WM_ValidateWindow(hGraph);
  247. }
  248. NextState = GUIDEMO_CheckCancel();
  249. TimeStep = GUIDEMO_GetTime() - TimeStart;
  250. if ((TimeStep - TimeDiff) < TIME_STEP) {
  251. GUI_Delay(TIME_STEP - (TimeStep - TimeDiff));
  252. }
  253. } while ((TimeDiff < TIME_RUN) && (NextState == 0));
  254. for (i = 0; i < DataCount; i++) {
  255. GRAPH_DetachData(hGraph, hData[i]);
  256. }
  257. GUIDEMO_NotifyStartNext();
  258. }
  259. /*********************************************************************
  260. *
  261. * _cbBk
  262. */
  263. static void _cbBk(WM_MESSAGE * pMsg) {
  264. switch (pMsg->MsgId) {
  265. case WM_PAINT:
  266. GUIDEMO_DrawBk(1);
  267. break;
  268. default:
  269. WM_DefaultProc(pMsg);
  270. }
  271. }
  272. /*********************************************************************
  273. *
  274. * _GraphDemo
  275. */
  276. static void _GraphDemo(void) {
  277. const WIDGET_EFFECT * pEffectOld;
  278. GRAPH_Handle hGraph;
  279. GRAPH_DATA_Handle hData[MAX_NUM_DATA_OBJ];
  280. int xSize, ySize, i;
  281. int Graph_xSize, Graph_ySize, Data_ySize;
  282. int Graph_xPos, Graph_yPos;
  283. xSize = LCD_GetXSize();
  284. ySize = LCD_GetYSize();
  285. pEffectOld = WIDGET_SetDefaultEffect(&WIDGET_Effect_Simple);
  286. //
  287. // Set Callback function for background window
  288. //
  289. WM_SetCallback(WM_HBKWIN, _cbBk);
  290. //
  291. // Determine size of GRAPH
  292. //
  293. Graph_xSize = xSize - (DIST_TO_BORDER << 1); // xsize = Screen size subtracting twice the distance to the border of the screen
  294. Graph_ySize = ySize - INFO_SIZE_Y - (DIST_TO_WIN << 1); // ysize = Screen size subtracting the window sizes and twice the distance to the windows
  295. if (Graph_ySize > MAX_GRAPH_SIZE_Y) {
  296. Graph_ySize = MAX_GRAPH_SIZE_Y;
  297. if (Graph_xSize > (Graph_ySize * 20) / 11) {
  298. Graph_xSize = (Graph_ySize * 20) / 11;
  299. }
  300. }
  301. //
  302. // Create and configure GRAPH_WIDGET
  303. //
  304. Graph_xPos = (xSize - Graph_xSize) >> 1;
  305. Graph_yPos = (ySize - Graph_ySize) >> 1;
  306. if (Graph_yPos < INFO_SIZE_Y + DIST_TO_WIN) {
  307. Graph_yPos = INFO_SIZE_Y + DIST_TO_WIN;
  308. }
  309. hGraph = GRAPH_CreateEx(Graph_xPos, Graph_yPos, Graph_xSize, Graph_ySize, WM_HBKWIN, WM_CF_SHOW | WM_CF_CONST_OUTLINE, 0, 0);
  310. GRAPH_SetBorder(hGraph, BORDER_LEFT, BORDER_TOP, BORDER_RIGHT, BORDER_BOTTOM);
  311. WM_SetHasTrans (hGraph);
  312. GRAPH_SetColor (hGraph, COLOR_BK, GRAPH_CI_BK);
  313. GRAPH_SetColor (hGraph, COLOR_BORDER, GRAPH_CI_BORDER);
  314. GRAPH_SetColor (hGraph, COLOR_FRAME, GRAPH_CI_FRAME);
  315. GRAPH_SetColor (hGraph, COLOR_GRID, GRAPH_CI_GRID);
  316. //
  317. // Adjust grid
  318. //
  319. GRAPH_SetGridVis (hGraph, 1);
  320. GRAPH_SetGridDistX(hGraph, GRID_DIST_X);
  321. GRAPH_SetGridDistY(hGraph, GRID_DIST_Y);
  322. WM_BringToBottom (hGraph);
  323. //
  324. // Create and configure GRAPH_DATA_YT object
  325. //
  326. for (i = 0; i < MAX_NUM_DATA_OBJ; i++) {
  327. hData[i] = GRAPH_DATA_YT_Create(_aColorData[i], xSize - (DIST_TO_BORDER << 1) - BORDER_LEFT, 0, 0);
  328. }
  329. Data_ySize = Graph_ySize - BORDER_BOTTOM;
  330. //
  331. // Create and configure GRAPH_SCALE objects
  332. //
  333. _hScaleH = GRAPH_SCALE_Create(BORDER_BOTTOM >> 1, GUI_TA_VCENTER, GRAPH_SCALE_CF_HORIZONTAL, TICK_DIST_H);
  334. _hScaleV = GRAPH_SCALE_Create(BORDER_LEFT >> 1, GUI_TA_HCENTER, GRAPH_SCALE_CF_VERTICAL, TICK_DIST_V);
  335. GRAPH_SCALE_SetPos(_hScaleH, Graph_ySize - SCALE_H_HEIGHT);
  336. GRAPH_SCALE_SetOff(_hScaleH, -5);
  337. //
  338. // Show some graphs
  339. //
  340. i = 0;
  341. while (_aWave[i].pfAddData != 0) {
  342. GUIDEMO_ShowInfo(_aWave[i].Name);
  343. GRAPH_AttachScale(hGraph, _hScaleH);
  344. GRAPH_AttachScale(hGraph, _hScaleV);
  345. _DataAdjust = (Data_ySize * _aWave[i].DataVOff) >> GRAPH_DIV;
  346. GRAPH_SetGridOffY (hGraph, (Data_ySize * _aWave[i].GridVOff) >> GRAPH_DIV);
  347. GRAPH_SCALE_SetOff(_hScaleV, (((Data_ySize - BORDER_BOTTOM) * _aWave[i].ScaleVOff) >> GRAPH_DIV));
  348. _ShowGraph(hGraph, hData, _aWave[i].NumWaves, _aWave[i].pfAddData);
  349. i++;
  350. }
  351. //
  352. // Clean up
  353. //
  354. GRAPH_DetachScale(hGraph, _hScaleH);
  355. GRAPH_DetachScale(hGraph, _hScaleV);
  356. GRAPH_SCALE_Delete(_hScaleH);
  357. GRAPH_SCALE_Delete(_hScaleV);
  358. for (i = 0; i < MAX_NUM_DATA_OBJ; i++) {
  359. GRAPH_DATA_YT_Delete(hData[i]);
  360. }
  361. WM_DeleteWindow(hGraph);
  362. WIDGET_SetDefaultEffect(pEffectOld);
  363. }
  364. /*********************************************************************
  365. *
  366. * Public functions
  367. *
  368. **********************************************************************
  369. */
  370. void GUIDEMO_Graph(void) {
  371. GUIDEMO_ShowIntro("Drawing a graph",
  372. "Optimized drawing routine\n"
  373. "for drawing graph data");
  374. GUIDEMO_ShowInfoWin();
  375. _GraphDemo();
  376. GUIDEMO_NotifyStartNext();
  377. }
  378. #else
  379. void GUIDEMO_Graph(void) {}
  380. #endif
  381. /*************************** End of file ****************************/