blob: fb1f034255329621ad13c1fa4984edadf6748f19 [file] [log] [blame]
zongqiang.zhang0c6a0882019-08-07 14:48:21 +08001/**
2 \file graphs.c
3 \brief Functions relating to graphs. e.g bar graphs etc.
4 \author Andy Gock
5 */
6
7/*
8 Copyright (c) 2012, Andy Gock
9
10 All rights reserved.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in the
18 documentation and/or other materials provided with the distribution.
19 * Neither the name of Andy Gock nor the
20 names of its contributors may be used to endorse or promote products
21 derived from this software without specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL ANDY GOCK BE LIABLE FOR ANY
27 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*/
34
35#include "glcd.h"
36
37static uint8_t glcd_map(uint8_t x1, uint8_t x2, uint8_t x);
38
39void glcd_bar_graph_horizontal(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t val)
40{
41 if (height < 3) {
42 return;
43 }
44 glcd_draw_rect(x, y, width, height, BLACK);
45 glcd_fill_rect(x+1, y+1, glcd_map(0,width-2,val), height-2 , BLACK);
46}
47
48void glcd_bar_graph_horizontal_no_border(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t val)
49{
50 if (height < 3) {
51 return;
52 }
53 glcd_fill_rect(x, y, glcd_map(0,width,val), height , BLACK);
54}
55
56void glcd_bar_graph_vertical(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t val)
57{
58 glcd_draw_rect(x, y, width, height, BLACK);
59 glcd_fill_rect(x+1, y+1+glcd_map(0,height-2,255-val), width-2, height-2-glcd_map(0,height-2,255-val), BLACK);
60}
61
62void glcd_bar_graph_vertical_no_border(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t val)
63{
64 glcd_fill_rect(x, y+glcd_map(0,height-2,255-val), width, height-2-glcd_map(0,height-2,255-val), BLACK);
65}
66
67void glcd_scrolling_bar_graph(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t val)
68{
69 uint8_t nx, ny;
70 uint8_t color;
71
72 /* Draw border of graph */
73 glcd_draw_rect(x,y,width,height,BLACK);
74
75 /* Scroll inner contents left by one pixel width */
76 for (ny = 1; ny <= (height-2); ny++) {
77 /* Redraw each horizontal line */
78 for (nx = 1; nx <= (width-2); nx += 1) {
79 color = glcd_get_pixel(x+nx+1,y+ny);
80 glcd_set_pixel(x+nx,y+ny,color);
81 }
82 }
83
84 val = val * (height-3) / 255;
85
86 /* Make sure we're not exceeding the size of box interior */
87 if (val > (height-3)) {
88 val = height - 3;
89 }
90
91 /* Draw new bar - both black and white portions*/
92 glcd_draw_line(x+width-2,y+height-2,x+width-2,y+height-2-val,BLACK);
93 glcd_draw_line(x+width-2,y+height-3-val,x+width-2,y+1,WHITE);
94
95 /* Write to display */
96 glcd_write();
97}
98
99void glcd_scrolling_bar_graph_timing(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t val, uint8_t line_width, uint16_t delay)
100{
101 uint8_t n;
102 if (line_width == 0) {
103 line_width = 1;
104 }
105
106 /* Adjust graph line's width by just running glcd_scrolling_bar_graph() x number of times */
107 /* \todo This should be done differently! */
108 for (n=0; n<line_width; n++) {
109 glcd_scrolling_bar_graph(x,y,width,height,val);
110 }
111
112 if (delay) {
113 delay_ms(delay);
114 }
115}
116
117static uint8_t glcd_map(uint8_t x1, uint8_t x2, uint8_t x)
118{
119 return x1+(x2-x1)*x/255;
120}