123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- /*
- * https://wiki.ugfx.io/index.php/Graph
- *
- * Copyright (c) 2012, 2013, Joel Bodenmann aka Tectu <joel@unormal.org>
- * Copyright (c) 2012, 2013, Andrew Hannam aka inmarket
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the <organization> nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "gfx.h"
- #include "math.h"
- // window
- #define WINDOW_X 1
- #define WINDOW_Y 123
- #define WINDOW_W 318
- #define WINDOW_H 118
- #define MIN_U 0
- #define MIN_I 0
- #define MAX_U 15000
- #define MAX_I 5000
- // from Arduino
- static long map(long x, long in_min, long in_max, long out_min, long out_max) {
- return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
- }
- // A graph styling
- static GGraphStyle GraphStyle1 = {
- { GGRAPH_POINT_DOT, 0, GFX_BLUE }, // Point
- { GGRAPH_LINE_NONE, 2, GFX_GRAY }, // Line
- { GGRAPH_LINE_SOLID, 0, GFX_WHITE }, // X axis
- { GGRAPH_LINE_SOLID, 0, GFX_WHITE }, // Y axis
- { GGRAPH_LINE_DASH, 5, GFX_GRAY, 50 }, // X grid
- { GGRAPH_LINE_DOT, 7, GFX_YELLOW, 50 }, // Y grid
- GWIN_GRAPH_STYLE_POSITIVE_AXIS_ARROWS // Flags
- };
- static GHandle gh;
- static gCoord pos_X;
- void graph_Init(void) {
- // Create the graph window
- {
- GWindowInit wi;
-
- wi.show = gTrue;
- wi.x = WINDOW_X;
- wi.y = WINDOW_Y;
- wi.width = WINDOW_W;
- wi.height = WINDOW_H;
- gh = gwinGraphCreate(0, &wi);
- }
- // clear area
- gdispFillArea(WINDOW_X, WINDOW_Y, WINDOW_W, WINDOW_H, GFX_BLACK);
- // Set the graph origin and style
- gwinGraphSetOrigin(gh, gwinGetWidth(gh)/2, gwinGetHeight(gh)/2);
- gwinGraphSetStyle(gh, &GraphStyle1);
- gwinGraphDrawAxis(gh);
- pos_X = WINDOW_X;
- }
- void graph_Draw(uint32_t u_value, uint32_t i_value) {
- gCoord pos_Y;
- // Modify the style
- gwinGraphStartSet(gh);
- GraphStyle1.point.color = GFX_GREEN;
- gwinGraphSetStyle(gh, &GraphStyle1);
- // Draw voltage point
- pos_Y = map(u_value, MIN_U, MAX_U, WINDOW_Y, WINDOW_H);
- gwinGraphDrawPoint(gh, pos_X, pos_Y);
- // Modify the style
- gwinGraphStartSet(gh);
- GraphStyle1.point.color = GFX_RED;
- gwinGraphSetStyle(gh, &GraphStyle1);
- // Draw current point
- pos_Y = map(i_value, MIN_I, MAX_I, WINDOW_Y, WINDOW_H);
- gwinGraphDrawPoint(gh, pos_X, pos_Y);
- pos_X ++;
- if (pos_X >= WINDOW_W) {
- pos_X = WINDOW_X;
- }
- }
|