PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
GraphicsTest.c
Go to the documentation of this file.
1 
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <propeller.h>
10 
11 #include "tv.h"
12 #include "Graphics.h"
13 
14 void Start(void);
15 void grTest(void);
16 
20 int main(void)
21 {
22  Start();
23  grTest(); // never returns
24  return 0;
25 }
26 
27 #define X_TILES 12
28 #define Y_TILES 10
29 #define X_EXPANSION 14
30 #define X_ORIGIN 96
31 #define Y_ORIGIN 80
32 #define LINES 4
33 #define THICKNESS 3
34 #define MAX_COLORS 64
35 
36 /*
37  * TV and Display control variables
38  */
39 HUBDATA TV_t gTV;
40 
41 HUBDATA volatile uint16_t gScreen[X_TILES * Y_TILES];
42 HUBDATA volatile uint32_t gColors[MAX_COLORS];
43 
44 HUBDATA volatile uint32_t gDisplay[(X_TILES * Y_TILES << 4)+128];
45 HUBDATA volatile uint32_t gBitmap [(X_TILES * Y_TILES << 4)+128];
46 
47 #define BITMAP_BASE (((int)gBitmap +256) & ~127) // page align
48 #define DISPLY_BASE (((int)gDisplay+256) & ~127)
49 
50 /*
51  * demo lines and sprites
52  */
53 uint8_t xa[LINES];
54 uint8_t ya[LINES];
55 uint8_t xsa[LINES];
56 uint8_t ysa[LINES];
57 
58 
59 /*
60  * triangle
61  */
62 uint16_t vecdef[] =
63 {
64  0x4000+0x2000/3*0, 50,
65  0x8000+0x2000/3*1+1, 50,
66  0x8000+0x2000/3*2-1, 50,
67  0x8000+0x2000/3*0, 50,
68  0
69 };
70 
71 /*
72  * star
73  */
74 uint16_t vecdef2[] =
75 {
76  0x4000+0x2000/12*0, 50,
77  0x8000+0x2000/12*1, 20,
78  0x8000+0x2000/12*2, 50,
79  0x8000+0x2000/12*3, 20,
80  0x8000+0x2000/12*4, 50,
81  0x8000+0x2000/12*5, 20,
82  0x8000+0x2000/12*6, 50,
83  0x8000+0x2000/12*7, 20,
84  0x8000+0x2000/12*8, 50,
85  0x8000+0x2000/12*9, 20,
86  0x8000+0x2000/12*10, 50,
87  0x8000+0x2000/12*11, 20,
88  0x8000+0x2000/12*0, 50,
89  0
90 };
91 
92 /*
93  * crosshair
94  */
95 uint16_t pixdef[] =
96 {
97  0x0702, 0x0000, //byte 2,7,3,3 endianness ?
98  0x0fc0, 0x0000, //word %%00333000,%%00000000
99  0x3230, 0x0000, //word %%03020300,%%00000000
100  0xc20c, 0x0000, //word %%30020030,%%00000000
101  0xeaac, 0x0000, //word %%32222230,%%00000000
102  0xc20c, 0x2000, //word %%30020030,%%02000000
103  0x3230, 0xa800, //word %%03020300,%%22200000
104  0x0fc0, 0x2000 //word %%00333000,%%02000000
105 };
106 
107 /*
108  * dog
109  */
110 uint16_t pixdef2[] =
111 {
112  0x0401,
113  0x0003, //byte 1,4,0,3
114  0x800a, //word %%20000022
115  0x2aaa, //word %%02222222
116  0x2aa0, //word %%02222200
117  0x2020 //word %%02000200
118 };
119 
120 /*
121  * start tv and graphics cogs
122  */
123 static int tvstart(int pin)
124 {
125  int n, dx, dy;
126  int cog;
127  TV_t* tp = &gTV;
128  tp->enable = 1;
129  tp->pins = TV_BASEBAND_PIN(pin);
130  tp->mode = 0;
131  tp->screen = (uint32_t) &gScreen[0];
132  tp->colors = (uint32_t) &gColors[0];
133  tp->ht = X_TILES;
134  tp->vt = Y_TILES;
135  tp->hx = X_EXPANSION;
136  tp->vx = 1;
137  tp->ho = 0;
138  tp->vo = 0;
139  tp->broadcast = 0;
140  tp->auralcog = 0;
141 
142  cog = TV_start(tp);
143 
144  //setColorPalette(&gPalette[0]);
145 
146  // init colors
147  for(n = 0; n < MAX_COLORS; n++)
148  gColors[n] = 0x1010 * ((n+4) & 0xF) + 0x2B060C02;
149 
150  // init tile screen
151  for (dx = 0; dx < tp->ht; dx++)
152  for (dy = 0; dy < tp->vt; dy++)
153  gScreen[dy * tp->ht + dx] = (DISPLY_BASE >> 6) + dy + dx * tp->vt + ((dy & 0x3F) << 10);
154 
155  return cog;
156 }
157 
158 
159 /*
160  * Graphics test function
161  */
162 char gstr[100];
163 #define MAXV 50
164 
165 void grTest()
166 {
167  int n = 0;
168  int numx = 0;
169  int numchr = '0';
170  int k = 8776434;
171  int t = 0;
172  int limit = Y_ORIGIN;
173  int mousex = 0, mousey = 0;
174 
175  srand(k);
176  for(n = 0; n < LINES; n++) {
177  xa[n] = (rand() % 32) - 16;
178  ya[n] = (rand() % 32) - 16;
179  xsa[n] = (rand() % 16) - 8;
180  ysa[n] = (rand() % 16) - 8;
181  }
182 
183  for(;;)
184  {
185 #if 0
186  int delay = 100;
187  waitcnt(((CLKFREQ*delay)/1000)+CNT);
188  DIRA |= (1 << 15);
189  OUTA ^= (1 << 15);
190 #endif
191  Graph_clear();
192 
193  Graph_colorwidth(2,0);
194  Graph_textmode(1,1,5,0);
195  //sprintf(gstr,"Base %X Display %X", BITMAP_BASE, DISPLY_BASE);
196  Graph_text(-limit*2 + t++ % (limit*3), limit-15, gstr);
197 
198  Graph_colorwidth(2,k>>3);
199  if(!(k % 4)) {
200  mousex = rand()%(limit/4);
201  mousey = rand()%(limit/4);
202  }
203  Graph_pix(mousex, mousey, k>>4 & 0x7, pixdef);
204 
205  //draw spinning triangles
206  Graph_colorwidth(3,0);
207  for(n = 1; n <= 8; n++)
208  Graph_vec(0, 0, ((k & 0x7F) << 3) + (n << 4), (k << 6) + (n << 8), vecdef);
209 
210  // init bouncing lines ... not even close to graphics.spin elegance
211  if (!(k % 4)) {
212  for(n = 0; n < LINES; n++) {
213  if(rand()%100 > 80)
214  xsa[n] = -xsa[n];
215  if(rand()%100 > 80)
216  ysa[n] = -ysa[n];
217  xa[n] += xsa[n] % 10;
218  ya[n] += ysa[n] % 10;
219  }
220  }
221 
222  Graph_colorwidth(1,THICKNESS);
223  Graph_plot((xa[0]%MAXV)-(MAXV>>1), (ya[0]%MAXV)-(MAXV>>1));
224  for(n = 1; n < LINES; n++)
225  Graph_line((xa[n]%MAXV)-(MAXV>>1),(ya[n]%MAXV)-(MAXV>>1));
226  Graph_line((xa[0]%MAXV)-(MAXV>>1), (ya[0]%MAXV)-(MAXV>>1));
227 
228  //draw small box with text
229  Graph_colorwidth(1,14);
230  Graph_box(6,-limit+3,53,20);
231  Graph_colorwidth(2,0);
232  Graph_textmode(1,1,5,0);
233  Graph_text(10,-limit+5,"Propeller");
234 
235  // draw axis
236  Graph_colorwidth(2,0);
237  Graph_plot(0, 0);
238  Graph_line(0, limit);
239  Graph_plot(0, 0);
240  Graph_line(limit, 0);
241 
242  Graph_plot(-limit, 0);
243  Graph_line(limit, 0);
244  Graph_plot(0, -limit);
245  Graph_line(0, limit);
246 
247  Graph_colorwidth(4,0);
248  Graph_pix(-limit + k%(limit*2), 2 + k%2, 0, pixdef2);
249 
250  //draw spinning stars and revolving crosshairs and dogs
251  Graph_colorwidth(2,0);
252  for(n = 0; n < 8; n++) {
253  Graph_vecarc(limit/2, limit/2, 20, 20, -((n << 10) + (k << 6)), 0x40, -(k<<7), vecdef2);
254  Graph_pixarc(-limit/2, -limit/2, 20, 20, (n << 10) + (k << 6), k<<4, pixdef2);
255  Graph_pixarc(-limit/2, -limit/2, 10, 10, -((n << 10) + (k << 6)), k<<4, pixdef);
256  }
257 
258  // draw expanding pixel halo
259  Graph_colorwidth(1,k);
260  Graph_arc(0,0,limit,20,-k<<5,0x2000/9,9,0);
261 
262  // draw incrementing digit
263  if((++numx % 7) == 0)
264  numchr++;
265  if(numchr < '0' || numchr > '9')
266  numchr = '0';
267  //Graph_textmode(8,8,6,5);
268  Graph_textmode(3,3,5,0);
269  Graph_colorwidth(1,20);
270  Graph_char(-limit+5 + t % (limit*2), t*t % (limit/3) - limit/2, numchr);
271 
272  // copy bitmap to display
273  if(BITMAP_BASE != DISPLY_BASE)
274  Graph_copy(DISPLY_BASE);
275 
276  k++;
277  }
278 }
279 
280 void Start()
281 {
282  tvstart(12);
283  Graph_start();
284  Graph_setup(X_TILES, Y_TILES, X_ORIGIN, Y_ORIGIN, BITMAP_BASE);
285 }
286 
287 /*
288 +------------------------------------------------------------------------------------------------------------------------------+
289 ¦ TERMS OF USE: MIT License ¦
290 +------------------------------------------------------------------------------------------------------------------------------¦
291 ¦Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation ¦
292 ¦files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, ¦
293 ¦modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software¦
294 ¦is furnished to do so, subject to the following conditions: ¦
295 ¦ ¦
296 ¦The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.¦
297 ¦ ¦
298 ¦THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE ¦
299 ¦WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR ¦
300 ¦COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ¦
301 ¦ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ¦
302 +------------------------------------------------------------------------------------------------------------------------------+
303 */
304 
OUTA
#define OUTA
Use to set output pin states when corresponding DIRA bits are 1.
Definition: propeller1.h:157
Graph_vecarc
void Graph_vecarc(uint32_t x, uint32_t y, uint32_t xr, uint32_t yr, int angle, int vecscale, int vecangle, uint16_t *vecdef_ptr)
Definition: Graphics.c:299
Graph_line
void Graph_line(uint32_t x, uint32_t y)
Definition: Graphics.c:257
Graph_colorwidth
void Graph_colorwidth(uint32_t c, uint32_t w)
Definition: Graphics.c:236
Graph_pixarc
void Graph_pixarc(uint32_t x, uint32_t y, uint32_t xr, uint32_t yr, int angle, int pixrot, uint16_t *pixdef_ptr)
Definition: Graphics.c:329
Graph_plot
void Graph_plot(uint32_t x, uint32_t y)
Definition: Graphics.c:246
Graph_textmode
void Graph_textmode(uint32_t x_scale, uint32_t y_scale, uint32_t spacing, uint32_t justification)
Definition: Graphics.c:393
Graph_char
void Graph_char(uint32_t x, uint32_t y, char ch)
Definition: Graphics.c:345
Graph_text
void Graph_text(uint32_t x, uint32_t y, char *string_ptr)
Definition: Graphics.c:356
Graph_start
int Graph_start(void)
Definition: Graphics.c:73
Graph_copy
void Graph_copy(uint32_t dest_ptr)
Definition: Graphics.c:188
Graph_pix
void Graph_pix(uint32_t x, uint32_t y, int pixrot, uint16_t *pixdef_ptr)
Definition: Graphics.c:316
main
int main(void)
Definition: GraphicsTest.c:20
Graph_vec
void Graph_vec(uint32_t x, uint32_t y, int vecscale, int vecangle, uint16_t *vecdef_ptr)
Definition: Graphics.c:285
CLKFREQ
#define CLKFREQ
Returns the current clock frequency.
Definition: propeller.h:46
TV_start
int TV_start(TV_t *tvp)
Definition: tv.c:17
HUBDATA
#define HUBDATA
HUBDATA tells compiler to put data into HUB RAM section. This is mostly useful in XMM modes where dat...
Definition: propeller.h:32
waitcnt
#define waitcnt(a)
Wait until system counter reaches a value.
Definition: propeller.h:176
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
Graph_setup
void Graph_setup(uint32_t x_tiles, uint32_t y_tiles, uint32_t x_origin, uint32_t y_origin, uint32_t base_ptr)
Definition: Graphics.c:144
tv.h
TV_t
volatile struct TV_struct TV_t
Graph_arc
void Graph_arc(uint32_t x, uint32_t y, uint32_t xr, uint32_t yr, int angle, int anglestep, int steps, int arcmode)
Definition: Graphics.c:268
Graph_box
void Graph_box(int x, int y, int box_width, int box_height)
Definition: Graphics.c:407
TV_BASEBAND_PIN
#define TV_BASEBAND_PIN(pin)
Definition: tv.h:35
Graph_clear
void Graph_clear(void)
Definition: Graphics.c:178
DIRA
#define DIRA
Use to set pins to input (0) or output (1).
Definition: propeller1.h:161
Graphics.h