graph.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * https://wiki.ugfx.io/index.php/Graph
  3. *
  4. * Copyright (c) 2012, 2013, Joel Bodenmann aka Tectu <joel@unormal.org>
  5. * Copyright (c) 2012, 2013, Andrew Hannam aka inmarket
  6. *
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * * Neither the name of the <organization> nor the
  17. * names of its contributors may be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  27. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "gfx.h"
  32. #include "math.h"
  33. // window
  34. #define WINDOW_X 1
  35. #define WINDOW_Y 123
  36. #define WINDOW_W 318
  37. #define WINDOW_H 118
  38. #define MIN_U 0
  39. #define MIN_I 0
  40. #define MAX_U 15000
  41. #define MAX_I 5000
  42. // from Arduino
  43. static long map(long x, long in_min, long in_max, long out_min, long out_max) {
  44. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  45. }
  46. // A graph styling
  47. static GGraphStyle GraphStyle1 = {
  48. { GGRAPH_POINT_DOT, 0, GFX_BLUE }, // Point
  49. { GGRAPH_LINE_NONE, 2, GFX_GRAY }, // Line
  50. { GGRAPH_LINE_SOLID, 0, GFX_WHITE }, // X axis
  51. { GGRAPH_LINE_SOLID, 0, GFX_WHITE }, // Y axis
  52. { GGRAPH_LINE_DASH, 5, GFX_GRAY, 50 }, // X grid
  53. { GGRAPH_LINE_DOT, 7, GFX_YELLOW, 50 }, // Y grid
  54. GWIN_GRAPH_STYLE_POSITIVE_AXIS_ARROWS // Flags
  55. };
  56. static GHandle gh;
  57. static gCoord pos_X;
  58. void graph_Init(void) {
  59. // Create the graph window
  60. {
  61. GWindowInit wi;
  62. wi.show = gTrue;
  63. wi.x = WINDOW_X;
  64. wi.y = WINDOW_Y;
  65. wi.width = WINDOW_W;
  66. wi.height = WINDOW_H;
  67. gh = gwinGraphCreate(0, &wi);
  68. }
  69. // clear area
  70. gdispFillArea(WINDOW_X, WINDOW_Y, WINDOW_W, WINDOW_H, GFX_BLACK);
  71. // Set the graph origin and style
  72. gwinGraphSetOrigin(gh, gwinGetWidth(gh)/2, gwinGetHeight(gh)/2);
  73. gwinGraphSetStyle(gh, &GraphStyle1);
  74. gwinGraphDrawAxis(gh);
  75. pos_X = WINDOW_X;
  76. }
  77. void graph_Draw(uint32_t u_value, uint32_t i_value) {
  78. gCoord pos_Y;
  79. // Modify the style
  80. gwinGraphStartSet(gh);
  81. GraphStyle1.point.color = GFX_GREEN;
  82. gwinGraphSetStyle(gh, &GraphStyle1);
  83. // Draw voltage point
  84. pos_Y = map(u_value, MIN_U, MAX_U, WINDOW_Y, WINDOW_H);
  85. gwinGraphDrawPoint(gh, pos_X, pos_Y);
  86. // Modify the style
  87. gwinGraphStartSet(gh);
  88. GraphStyle1.point.color = GFX_RED;
  89. gwinGraphSetStyle(gh, &GraphStyle1);
  90. // Draw current point
  91. pos_Y = map(i_value, MIN_I, MAX_I, WINDOW_Y, WINDOW_H);
  92. gwinGraphDrawPoint(gh, pos_X, pos_Y);
  93. pos_X ++;
  94. if (pos_X >= WINDOW_W) {
  95. pos_X = WINDOW_X;
  96. }
  97. }