Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
[bertos.git] / mware / charts.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 1999, 2000, 2001, 2003 Bernardo Innocenti <bernie@develer.com>
6  * This file is part of DevLib - See devlib/README for information.
7  * -->
8  *
9  * \brief Simple charts on top of mware/gfx routines (implementation).
10  *
11  * Sample usage:
12  *
13  * \code
14  *      bm = chart_init(0, ymax, N_POINTS_CURVE, ymin);
15  *
16  *      chart_drawCurve(bm, curve_y, curve_points + 1);
17  *      gfx_setViewRect(bm, xmin, ymax, xmax, ymin);
18  *      chart_drawDots(bm, samples_x, samples_y, samples_cnt);
19  *
20  *      print_bitmap(bm);
21  * \endcode
22  *
23  * \version $Id$
24  * \author Bernardo Innocenti <bernie@develer.com>
25  */
26
27 /*
28  * $Log$
29  * Revision 1.3  2004/08/11 19:39:12  bernie
30  * Use chart_x_t and chart_y_t for the input dataset.
31  *
32  * Revision 1.1  2004/08/04 03:16:30  bernie
33  * Import simple chart drawing code.
34  *
35  */
36
37 #include "charts.h"
38 #include <mware/gfx.h>
39
40
41 void chart_init(Bitmap *bm, vcoord_t xmin, vcoord_t ymin, vcoord_t xmax, vcoord_t ymax)
42 {
43         gfx_ClearBitmap(bm);
44         chart_drawAxis(bm);
45
46         gfx_SetClipRect(bm, CHART_BORDERLEFT, CHART_BORDERTOP,
47                 bm->width - CHART_BORDERRIGHT - 1, bm->height - CHART_BORDERBOTTOM - 1);
48
49         gfx_SetViewRect(bm, xmin, ymin, xmax, ymax);
50
51         //CHECK_WALL(wall_before_raster, WALL_SIZE);
52         //CHECK_WALL(wall_after_raster, WALL_SIZE);
53 }
54
55
56 /*!
57  * Draw the chart axes
58  */
59 void chart_drawAxis(Bitmap *bm)
60 {
61         /* Draw axis */
62         gfx_MoveTo(bm, CHART_BORDERLEFT, 4);
63         gfx_LineTo(bm, CHART_BORDERLEFT, CHART_BORDERTOP + CHART_HEIGHT - 1);
64         gfx_LineTo(bm, CHART_BORDERLEFT + CHART_WIDTH - 5, CHART_BORDERTOP + CHART_HEIGHT - 1);
65
66         /* Draw up arrow */
67         gfx_MoveTo(bm, CHART_BORDERLEFT - 2, 3);
68         gfx_LineTo(bm, CHART_BORDERLEFT + 2, 3);
69         gfx_LineTo(bm, CHART_BORDERLEFT, 0);
70         gfx_LineTo(bm, CHART_BORDERLEFT - 2, 3);
71
72         /* Draw right arrow */
73         gfx_MoveTo(bm, CHART_BORDERLEFT + CHART_WIDTH - 4, CHART_BORDERTOP + CHART_HEIGHT - 3);
74         gfx_LineTo(bm, CHART_BORDERLEFT + CHART_WIDTH - 4, CHART_BORDERTOP + CHART_HEIGHT + 1);
75         gfx_LineTo(bm, CHART_BORDERLEFT + CHART_WIDTH - 1, CHART_BORDERTOP + CHART_HEIGHT - 1);
76         gfx_LineTo(bm, CHART_BORDERLEFT + CHART_WIDTH - 4, CHART_BORDERTOP + CHART_HEIGHT - 3);
77
78         //CHECK_WALL(wall_before_raster, WALL_SIZE);
79         //CHECK_WALL(wall_after_raster, WALL_SIZE);
80 }
81
82
83 /*!
84  * Draw a set of \a curve_cnt connected segments, whose Y coordinates
85  * are identified by the \a curve_y array and X-coordinates are
86  * are evenly spaced by one virtual unit.
87  */
88 void chart_drawCurve(Bitmap *bm, const chart_y_t *curve_y, int curve_cnt)
89 {
90         int i;
91
92         gfx_MoveTo(bm, gfx_TransformX(bm, 0), gfx_TransformY(bm, curve_y[0]));
93
94         for (i = 1; i < curve_cnt; i++)
95                 gfx_LineTo(bm, gfx_TransformX(bm, i), gfx_TransformY(bm, curve_y[i]));
96
97         //CHECK_WALL(wall_before_raster, WALL_SIZE);
98         //CHECK_WALL(wall_after_raster, WALL_SIZE);
99 }
100
101
102 /*!
103  * Disegna dei dot in corrispondenza delle coppie (dotsx[i];dotsy[i])
104  * Se dotsx e' NULL, i punti vengono disegnati ad intervalli regolari.
105  */
106 void chart_drawDots(Bitmap *bm, const chart_x_t *dots_x, const chart_y_t *dots_y, int cnt)
107 {
108         int i;
109         coord_t x, y;
110
111         for (i = 0; i < cnt; i++)
112         {
113                 if (dots_x)
114                         x = gfx_TransformX(bm, dots_x[i]);
115                 else
116                         x = gfx_TransformX(bm, i);
117
118                 y = gfx_TransformY(bm, dots_y[i]);
119
120                 gfx_DrawRect(bm, x - 1, y - 1, x + 1, y + 1);
121
122                 /* Disegna ticks sull'asse X */
123                 gfx_DrawLine(bm, x, bm->height - 1, x, CHART_HEIGHT - 1);
124         }
125
126         //CHECK_WALL(wall_before_raster, WALL_SIZE);
127         //CHECK_WALL(wall_after_raster, WALL_SIZE);
128 }
129