libnxter  0.1
Screen.nxc
Go to the documentation of this file.
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  Screen.nxc
4  Copyright (C) 2008 Naba Kumar <naba@gnome.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
26 #ifndef __SCREEN_H_
27 #define __SCREEN_H_
28 
29 #include "Vector.nxc"
30 
31 #define NXT_SCREEN_ROWS 8
32 #define NXT_SCREEN_COLS 11
33 #define NXT_CHAR_HEIGHT 8
34 #define NXT_CHAR_LENGTH 8
35 #define NXT_SCREEN_X 110
36 #define NXT_SCREEN_Y 64
37 
38 #define PLOT_TYPE_LINE 1
39 #define PLOT_TYPE_POINT 2
40 #define PLOT_TYPE_CIRCLE 3
41 #define PLOT_TYPE_SQUARE 4
42 
43 struct Plotter
44 {
45  long lastX;
46  long lastY;
47  long minX;
48  long maxX;
49  long minY;
50  long maxY;
51  int plotType;
52 };
53 
54 mutex screenMutex;
55 bool screenInitialized = 0;
56 int screen_row = 0;
57 string screen_TextBuffer[];
58 
59 /* Private function. Initializes the screen on first screen function call */
60 void ScreenInit()
61 {
62  Acquire(screenMutex);
63  ClearScreen();
64  ArrayInit(screen_TextBuffer, " ", NXT_SCREEN_ROWS);
65  screenInitialized = 1;
66  Release(screenMutex);
67 }
68 
73 {
74  Acquire(screenMutex);
75  ClearScreen();
76  screen_row = 0;
77  Release(screenMutex);
78 }
79 
80 /* Private function. Scrolls the the screen by one line */
81 void ScreenScroll()
82 {
83  Acquire(screenMutex);
84  while (screen_row >= NXT_SCREEN_ROWS)
85  {
86  /* Scroll */
87  ClearScreen();
88  for (int i = 1; i < NXT_SCREEN_ROWS; i++)
89  {
90  int j = i - 1;
91  string data = screen_TextBuffer[i];
92  screen_TextBuffer[j] = data;
93  int y = (NXT_SCREEN_ROWS - j - 1) * NXT_CHAR_HEIGHT;
94  TextOut(0, y, data, false);
95  }
96  screen_row--;
97  }
98  Release(screenMutex);
99 }
100 
104 void PrintText(string text, int waitTime)
105 {
106  Acquire(screenMutex);
107  if (screenInitialized == 0) ScreenInit();
108  if (screen_row >= NXT_SCREEN_ROWS) ScreenScroll();
109  screen_TextBuffer[screen_row] = text;
110  int y = ((NXT_SCREEN_ROWS - 1 - screen_row) * NXT_CHAR_HEIGHT);
111  TextOut(0, y, text, false);
112  screen_row++;
113  if (waitTime > 0) Wait(waitTime);
114  Release(screenMutex);
115 }
116 
120 void PrintNum(long num, int waitTime)
121 {
122  Acquire(screenMutex);
123  if (screenInitialized == 0) ScreenInit();
124  if (screen_row >= NXT_SCREEN_ROWS) ScreenScroll();
125  screen_TextBuffer[screen_row] = NumToStr(num);
126  int y = ((NXT_SCREEN_ROWS - 1 - screen_row) * NXT_CHAR_HEIGHT);
127  NumOut(0, y, num, false);
128  screen_row++;
129  if (waitTime > 0) Wait(waitTime);
130  Release(screenMutex);
131 }
132 
137 void Print(string format, long num, int waitTime)
138 {
139  Acquire(screenMutex);
140  if (screenInitialized == 0) ScreenInit();
141  if (screen_row >= NXT_SCREEN_ROWS) ScreenScroll();
142  screen_TextBuffer[screen_row] = formatNum(format, num);
143  int y = ((NXT_SCREEN_ROWS - 1 - screen_row) * NXT_CHAR_HEIGHT);
144  TextOut(0, y, screen_TextBuffer[screen_row], false);
145  screen_row++;
146  if (waitTime > 0) Wait(waitTime);
147  Release(screenMutex);
148 }
149 
154 void PlotterInit(Plotter &plotter, int plotType,
155  long minX, long maxX, long minY, long maxY)
156 {
157  plotter.minX = minX;
158  plotter.maxX = maxX;
159  plotter.minY = minY;
160  plotter.maxY = maxY;
161  plotter.lastX = 0;
162  plotter.lastY = 0;
163  plotter.plotType = plotType;
164 }
165 
169 void PlotterPlot(Plotter &plotter, long x, long y)
170 {
171  Acquire(screenMutex);
172  x = (x - plotter.minX) * NXT_SCREEN_X / (plotter.maxX - plotter.minX);
173  y = (y - plotter.minY) * NXT_SCREEN_Y / (plotter.maxY - plotter.minY);
174 
175  if (plotter.plotType != PLOT_TYPE_LINE)
176  {
177  if (x < NXT_SCREEN_X && y > 0)
178  {
179  if (plotter.plotType == PLOT_TYPE_CIRCLE)
180  CircleOut(x, y, 2, false);
181  else if (plotter.plotType == PLOT_TYPE_POINT)
182  PointOut(x, y, false);
183  else if (plotter.plotType == PLOT_TYPE_SQUARE)
184  RectOut(x, y, 2, 2, false);
185  }
186  }
187  if (x > NXT_SCREEN_X) x = NXT_SCREEN_X;
188  if (y < 0) y = 0;
189 
190  if (plotter.plotType == PLOT_TYPE_LINE && plotter.lastX != 0 && plotter.lastY != 0)
191  LineOut(plotter.lastX, plotter.lastY, x, y, false);
192 
193  plotter.lastX = x;
194  plotter.lastY = y;
195  Release(screenMutex);
196 }
197 
201 void PlotterPlotVector(Plotter &plotter, Vector &vec)
202 {
203  PlotterPlot(plotter, vec.x, vec.y);
204 }
205 
206 #ifdef ENABLE_TEST
207 
208 void TestScreen()
209 {
210  PrintText("some text1", 500);
211  PrintText("some text2", 500);
212  PrintText("some text3", 500);
213  PrintText("some text4", 500);
214  PrintText("some text5", 500);
215  PrintText("some text6", 500);
216  PrintText("some text7", 500);
217  PrintText("some text8", 500);
218  PrintText("some text9", 500);
219  PrintText("some text10", 500);
220  PrintText("some text11", 500);
221  PrintNum(2341, 500);
222  PrintNum(2342, 500);
223  PrintNum(2343, 500);
224  PrintNum(2344, 500);
225  PrintNum(2345, 500);
226  Print("Format: %d", 501, 500);
227  Print("Format: %d", 502, 500);
228  Print("Format: %d", 503, 500);
229  Print("Format: %d", 504, 500);
230  Wait(2000);
231 }
232 
233 /*
234 task main()
235 {
236  TestScreen();
237 }
238 */
239 
240 #endif /* ENABLE_TEST */
241 #endif /* __SCREEN_H_ */
242 
void PlotterPlotVector(Plotter &plotter, Vector &vec)
Plots the given vector (x,y) using the plotter on screen.
Definition: Screen.nxc:201
A vector that represents a 2D point by x-y coordinates and a direction angle.
Definition: Vector.nxc:43
void PrintText(string text, int waitTime)
Prints a line of text. It scrolls the screen if there no line left.
Definition: Screen.nxc:104
A simple 3-components vector implementation (2D point + direction)
void PlotterPlot(Plotter &plotter, long x, long y)
Plots the given point using the plotter on screen.
Definition: Screen.nxc:169
void PrintNum(long num, int waitTime)
Prints a number. It scrolls the screen if there no line left.
Definition: Screen.nxc:120
void PlotterInit(Plotter &plotter, int plotType, long minX, long maxX, long minY, long maxY)
Initializes a plotter. There can be multiple plotters (for each graph) active simultaneous to draw on...
Definition: Screen.nxc:154
long y
Definition: Vector.nxc:46
long x
Definition: Vector.nxc:45
void ScreenClear()
Clears screen.
Definition: Screen.nxc:72
void Print(string format, long num, int waitTime)
Prints a formated string with a number. It scrolls the screen if there no line left.
Definition: Screen.nxc:137