PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Graphics.c
Go to the documentation of this file.
1 
8 #include <stdio.h>
9 #include <string.h>
10 #include <propeller.h>
11 #include "Graphics.h"
12 
34 extern uint16_t Graphics_Font[];
35 
40 
41 /*
42  * set command parameter array
43  */
44 HUBDATA uint32_t pa[GRAPH_MAX_ARGS];
45 
49 HUBDATA uint32_t Graph_colors[] =
50 {
51  0x00000000,
52  0x55555555,
53  0xAAAAAAAA,
54  0xFFFFFFFF
55 };
56 
60 HUBDATA uint8_t Graph_pixels[] =
61 {
62  0x00, 0x00, 0x00, 0x00, //0,1,2,3
63  0x00, 0x00, 0x02, 0x05, //4,5,6,7
64  0x0A, 0x0A, 0x1A, 0x1A, //8,9,A,B
65  0x34, 0x3a, 0x74, 0x74 //C,D,E,F
66 };
67 
68 
73 int Graph_start(void)
74 {
75  // spin uses fontptr := font. C has no such luxury
76  // to achieve the same result, we pass font in setup
77  int cog;
78  extern uint32_t binary_Graphics_dat_start[];
79 #if defined(__PROPELLER_XMM__)
80  extern uint32_t pasm[]; // defined in TV.c
81  extern uint32_t binary_Graphics_dat_end[];
82  int binsize = (binary_Graphics_dat_end - binary_Graphics_dat_start);
83  memcpy(pasm, binary_Graphics_dat_start, binsize*4);
84 #endif
85  gGraph.command = -1;
86 
87 // Debug Only.
88 #if 0 // defined(__PROPELLER_XMM__) || defined(__PROPELLER_XMMC__)
89  printf("Graph_start\n");
90  printf("GraphCmd_ST %x\n", &gGraph);
91  printf("%x\n", &gGraph.cog);
92  printf("%x\n", &gGraph.command);
93  printf("%x\n", &gGraph.pixel_width); //pixel data width
94  printf("%x\n", &gGraph.bitmap_ptr); //bitmap data
95  printf("%x\n", &gGraph.bitmap_len);
96  printf("%x\n", &gGraph.bases);
97  printf("%x\n", &gGraph.slices);
98  printf("%x\n", &gGraph.text_xs); //text data (these 4 must be contiguous)
99  printf("%x\n", &gGraph.text_ys);
100  printf("%x\n", &gGraph.text_sp);
101  printf("%x\n", &gGraph.text_just);
102  if(0) {
103  int n;
104  for(n = 0; n < 496; n++) {
105  if(!(n % 8))
106  printf("\n%03x ",n<<2);
107  printf("0x%08x ",pasm[n]);
108  }
109  printf("\n");
110  }
111  //for(;;);
112 #endif
113 #if defined(__PROPELLER_XMM__)
114  cog = cognew(pasm, (uint32_t)&gGraph.command)+1;
115 #else
116  cog = cognew(binary_Graphics_dat_start, (uint32_t)&gGraph.command)+1;
117 #endif
118  while(gGraph.command != 0)
119  ;
120  //printf("Graph_start COG %d started 0x%08x\n", cog, gGraph.command);
121  gGraph.cog = cog;
122 
123  return cog;
124 }
125 
130 void Graph_stop(void)
131 {
132  GraphCmd_ST* gp = &gGraph;
133  uint32_t cog = gp->cog-1;
134  if(gp->cog > 0) {
135  cogstop(cog);
136  gp->cog = 0;
137  }
138 }
139 
144 void Graph_setup(uint32_t x_tiles, uint32_t y_tiles, uint32_t x_origin, uint32_t y_origin, uint32_t base_ptr)
145 {
146  GraphCmd_ST* gp = &gGraph;
147  int n = 0;
148 
149  /* the simplest possible command */
150  sendCommand(GraphCmd_loop, 0);
151 
152  for(n = 0; n < x_tiles && n < 32; n++)
153  gp->bases[n] = base_ptr + ((n * y_tiles) << 6);
154 
155  y_tiles <<= 4;
156  y_origin = y_tiles - y_origin - 1;
157 
158  pa[0] = x_tiles;
159  pa[1] = y_tiles;
160  pa[2] = x_origin;
161  pa[3] = y_origin;
162  pa[4] = (uint32_t)gp->bases; // driver ignores arg4
163  pa[5] = (uint32_t)gp->bases;
164  pa[6] = (uint32_t)gp->slices;
165  pa[7] = (uint32_t)Graphics_Font;
166  //printf("Graph_setup %d\n", GraphCmd_setup);
167  sendCommand(GraphCmd_setup, pa);
168 
169  gp->bitmap_ptr = (uint32_t*)base_ptr;
170  gp->bitmap_len = x_tiles * y_tiles;
171  //printf("\nBP Longs 0x%x Bases 0x%x\n", gp->bitmap_ptr, gp->bases);
172 }
173 
178 void Graph_clear(void)
179 {
180  sendCommand(GraphCmd_loop, 0);
181  memset(gGraph.bitmap_ptr, 0, gGraph.bitmap_len<<2);
182 }
183 
188 void Graph_copy(uint32_t dest_ptr)
189 {
190  sendCommand(GraphCmd_loop, 0);
191  memcpy((void*)dest_ptr, (void*)gGraph.bitmap_ptr, gGraph.bitmap_len<<2);
192 }
193 
198 void Graph_color(uint32_t c)
199 {
200  sendCommand(GraphCmd_color, (uint32_t*)&Graph_colors[c & 3]);
201 }
202 
207 void Graph_width(uint32_t w)
208 {
209  int passes, r, n, pw;
210 
211  r = ~w & 0x10;
212  w &= 0xf;
213  gGraph.pixel_width = w;
214  passes = (w >> 1) + 1;
215 
216  memset(pa, 0, GRAPH_MAX_ARGS<<2);
217  pa[0] = w;
218  pa[1] = passes;
219 
220  sendCommand(GraphCmd_width, pa);
221 
222  pw = w ^ 0xf;
223  for(n = 0; n <= w >> 1; n++) {
224  gGraph.slices[n] = (uint32_t)(0xffffffff >> (pw << 1)) << (pw & 0xE);
225  if(r && (Graph_pixels[w] & (1 << n)))
226  pw += 2;
227  if(r && n == passes - 2)
228  pw += 2;
229  }
230 }
231 
236 void Graph_colorwidth(uint32_t c, uint32_t w)
237 {
238  Graph_color(c);
239  Graph_width(w);
240 }
241 
246 void Graph_plot(uint32_t x, uint32_t y)
247 {
248  pa[0] = x;
249  pa[1] = y;
250  sendCommand(GraphCmd_plot, pa);
251 }
252 
257 void Graph_line(uint32_t x, uint32_t y)
258 {
259  pa[0] = x;
260  pa[1] = y;
261  sendCommand(GraphCmd_line, pa);
262 }
263 
268 void Graph_arc(uint32_t x, uint32_t y, uint32_t xr, uint32_t yr, int angle, int anglestep, int steps, int arcmode)
269 {
270  pa[0] = x;
271  pa[1] = y;
272  pa[2] = xr;
273  pa[3] = yr;
274  pa[4] = angle;
275  pa[5] = anglestep;
276  pa[6] = steps;
277  pa[7] = arcmode;
278  sendCommand(GraphCmd_arc, pa);
279 }
280 
285 void Graph_vec(uint32_t x, uint32_t y, int vecscale, int vecangle, uint16_t* vecdef_ptr)
286 {
287  pa[0] = x;
288  pa[1] = y;
289  pa[2] = vecscale;
290  pa[3] = vecangle;
291  pa[4] = (uint32_t)vecdef_ptr;
292  sendCommand(GraphCmd_vec, pa);
293 }
294 
299 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)
300 {
301  pa[0] = x;
302  pa[1] = y;
303  pa[2] = xr;
304  pa[3] = yr;
305  pa[4] = angle;
306  pa[5] = vecscale;
307  pa[6] = vecangle;
308  pa[7] = (uint32_t)vecdef_ptr;
309  sendCommand(GraphCmd_vecarc, pa);
310 }
311 
316 void Graph_pix(uint32_t x, uint32_t y, int pixrot, uint16_t* pixdef_ptr)
317 {
318  pa[0] = x;
319  pa[1] = y;
320  pa[2] = pixrot;
321  pa[3] = (long)pixdef_ptr;
322  sendCommand(GraphCmd_pix, pa);
323 }
324 
329 void Graph_pixarc(uint32_t x, uint32_t y, uint32_t xr, uint32_t yr, int angle, int pixrot, uint16_t* pixdef_ptr)
330 {
331  pa[0] = x;
332  pa[1] = y;
333  pa[2] = xr;
334  pa[3] = yr;
335  pa[4] = angle;
336  pa[5] = pixrot;
337  pa[6] = (uint32_t)pixdef_ptr;
338  sendCommand(GraphCmd_pixarc, pa);
339 }
340 
345 void Graph_char(uint32_t x, uint32_t y, char ch)
346 {
347  char s[2] = { '\0', '\0' };
348  s[0] = ch;
349  Graph_text(x, y, s);
350 }
351 
356 void Graph_text(uint32_t x, uint32_t y, char* string_ptr)
357 {
358  uint32_t just[2];
359 
360  justify(string_ptr, just);
361  pa[0] = x;
362  pa[1] = y;
363  pa[2] = (uint32_t)string_ptr;
364  pa[3] = just[0];
365  pa[4] = just[1];
366  sendCommand(GraphCmd_text, pa);
367 }
368 
373 void Graph_textarc(uint32_t x, uint32_t y, uint32_t xr, uint32_t yr, int angle, char* string_ptr)
374 {
375  uint32_t just[2];
376 
377  justify(string_ptr, just);
378  pa[0] = x;
379  pa[1] = y;
380  pa[2] = xr;
381  pa[3] = yr;
382  pa[4] = angle;
383  pa[5] = (uint32_t)string_ptr;
384  pa[6] = just[0];
385  pa[7] = just[1];
386  sendCommand(GraphCmd_textarc, pa);
387 }
388 
393 void Graph_textmode(uint32_t x_scale, uint32_t y_scale, uint32_t spacing, uint32_t justification)
394 {
395  GraphCmd_ST* gp = &gGraph;
396  pa[0] = gp->text_xs = x_scale;
397  pa[1] = gp->text_ys = y_scale;
398  pa[2] = gp->text_sp = spacing;
399  pa[3] = gp->text_just = justification;
400  sendCommand(GraphCmd_textmode, pa);
401 }
402 
407 void Graph_box(int x, int y, int box_width, int box_height)
408 {
409  uint32_t x2, y2, pmin, pmax;
410  int width = gGraph.pixel_width;
411 
412  if((box_width > width) && (box_height > width))
413  {
414  pmin = width >> 1;
415  pmax = width - pmin;
416  x += pmin;
417  y += pmin;
418  x2 = x + box_width - 1 - width;
419  y2 = y + box_height - 1 - width;
420 
421  Graph_plot(x,y);
422  Graph_plot(x,y2);
423  Graph_plot(x2,y);
424  Graph_plot(x2,y2);
425 
426  fill(x, y2 + pmax, 0, (x2 - x) << 16, 0, 0, pmax); // fill gaps
427  fill(x, y, 0, (x2 - x) << 16, 0, 0, pmin);
428  fill(x - pmin, y2, 0, (x2 - x + width) << 16, 0, 0, y2 - y);
429  }
430 }
431 
436 void Graph_quad(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t x3, uint32_t y3, uint32_t x4, uint32_t y4)
437 {
438  Graph_tri(x1, y1, x2, y2, x3, y3);
439  Graph_tri(x3, y3, x4, y4, x1, y1);
440 }
441 
442 void Graph_tri(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t x3, uint32_t y3)
443 {
444  int xy[2];
445  int mycase = ((y1 >= y2) ? 4 : 0) | ((y2 >= y3) ? 2 : 0) | ((y1 >= y3) ? 1 : 0);
446 
447  switch(mycase) {
448  case 0:
449  xy[0] = x1;
450  xy[1] = y1; // 2 longs
451  x1 = x3;
452  y1 = y3; // 2 longs
453  x3 = xy[0];
454  y3 = xy[1]; // 2 longs
455  break;
456  case 2:
457  xy[0] = x1;
458  xy[1] = y1; // 2 longs
459  x1 = x2;
460  y1 = y2;
461  x2 = x3;
462  y2 = y3; // 4 longs
463  x3 = xy[0];
464  y3 = xy[1]; // 2 longs
465  break;
466  case 3:
467  xy[0] = x1;
468  xy[1] = y1; // 2 longs
469  x1 = x2;
470  y1 = y2; // 2 longs
471  x2 = xy[0];
472  y2 = xy[1]; // 2 longs
473  break;
474  case 4:
475  xy[0] = x3;
476  xy[1] = y3; // 2 longs
477  x2 = x1;
478  y2 = y1;
479  x3 = x2;
480  y3 = y2; // 4 longs
481  x1 = xy[0];
482  y1 = xy[1]; // 2 longs
483  break;
484  case 5:
485  xy[0] = x2;
486  xy[1] = y2; // 2 longs
487  x2 = x3;
488  y2 = y3; // 2 longs
489  x3 = xy[0];
490  y3 = xy[1]; // 2 longs
491  break;
492  default:
493  break;
494  }
495 
496  fill(x1, y1, ((x3 - x1) << 16) / (y1 - y3 + 1), ((x2 - x1) << 16) / (y1 - y2 + 1), ((x3 - x2) << 16) / (y2 - y3 + 1), y1 - y2, y1 - y3);
497 }
498 
502 void Graph_finish(void)
503 {
504  sendCommand(GraphCmd_loop, 0);
505 }
506 
507 
511 void fill(int x, int y, int da, int db, int db2, int linechange, int lines_minus_1)
512 {
513  pa[0] = x;
514  pa[1] = y;
515  pa[2] = da;
516  pa[3] = db;
517  pa[4] = db2;
518  pa[5] = linechange;
519  pa[6] = lines_minus_1;
520  sendCommand(GraphCmd_fill, pa);
521 }
522 
526 void justify(char* string_ptr, uint32_t* justptr)
527 {
528  GraphCmd_ST* gp = &gGraph;
529  uint32_t x = (strlen(string_ptr) - 1) * gp->text_xs * gp->text_sp + gp->text_xs * 5 - 1;
530  uint32_t ja1[4];
531  uint32_t ja2[4];
532 
533  // p1 is an alias of ja1
534  ja1[0] = 0;
535  ja1[1] = x >> 1;
536  ja1[2] = x;
537  ja1[3] = 0;
538  justptr[0] = -ja1[gp->text_just]; // -lookupz(text_just >> 2 & 3: 0, x >> 1, x, 0)
539 
540  // p2 is an alias of ja1
541  ja2[0] = 0;
542  ja2[1] = gp->text_ys << 3;
543  ja2[2] = gp->text_ys << 4;
544  ja2[3] = 0;
545  justptr[1] = -ja2[gp->text_just]; // -lookupz(text_just & 3: 0, text_ys << 3, text_ys << 4, 0)
546 }
547 
548 #if DEBUG_CMD
549 char* cmds[] = {
550  "None", "Setup",
551  "Color", "Width",
552  "Plot", "Line",
553  "Arc", "Vec", "VecArc",
554  "Pix", "PixArc",
555  "Text", "TextArc", "TextMode",
556  "Fill", "Loop",
557  0 };
558 #endif
559 
565 void sendCommand(GraphCmd_ET cmd, uint32_t* argptr)
566 {
567  //write command and pointer
568  gGraph.command = ((cmd << 16) | ((int)argptr & 0xffff));
569 #if DEBUG_CMD
570  printf("\nsendCommand(0x%x, 0x%x): 0x%x ... %s", cmd, (int)argptr, gGraph.command, cmds[cmd]);
571  fflush(stdout);
572 #endif
573  // wait for command to be cleared, signifying receipt
574  while(gGraph.command != 0)
575  ;
576 }
577 
578 
579 
580 /*
581  * Ported from Graphics.spin
582  *
583 ''***************************************
584 ''* Graphics Driver v1.0 *
585 ''* Author: Chip Gracey *
586 ''* Copyright (c) 2005 Parallax, Inc. *
587 ''* See end of file for terms of use. *
588 ''***************************************
589  *
590 +------------------------------------------------------------------------------------------------------------------------------+
591 ¦ TERMS OF USE: MIT License ¦
592 +------------------------------------------------------------------------------------------------------------------------------¦
593 ¦Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation ¦
594 ¦files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, ¦
595 ¦modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software¦
596 ¦is furnished to do so, subject to the following conditions: ¦
597 ¦ ¦
598 ¦The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.¦
599 ¦ ¦
600 ¦THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE ¦
601 ¦WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR ¦
602 ¦COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ¦
603 ¦ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ¦
604 +------------------------------------------------------------------------------------------------------------------------------+
605 */
Graph_colors
HUBDATA uint32_t Graph_colors[]
Definition: Graphics.c:49
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_MAX_ARGS
#define GRAPH_MAX_ARGS
Definition: Graphics.h:57
Graph_stop
void Graph_stop(void)
Definition: Graphics.c:130
Graph_line
void Graph_line(uint32_t x, uint32_t y)
Definition: Graphics.c:257
cognew
#define cognew(code, param)
Start a new Propeller PASM COG.
Definition: propeller.h:94
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
sendCommand
void sendCommand(GraphCmd_ET cmd, uint32_t *argptr)
Definition: Graphics.c:565
gGraph
HUBDATA GraphCmd_ST gGraph
Definition: Graphics.c:39
Graphics_Font
uint16_t Graphics_Font[]
Definition: GraphicsFont.c:13
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
GraphCmd_ET
enum Graph_enum GraphCmd_ET
Graph_text
void Graph_text(uint32_t x, uint32_t y, char *string_ptr)
Definition: Graphics.c:356
justify
void justify(char *string_ptr, uint32_t *justptr)
Definition: Graphics.c:526
Graph_start
int Graph_start(void)
Definition: Graphics.c:73
Graph_finish
void Graph_finish(void)
Definition: Graphics.c:502
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
Graph_textarc
void Graph_textarc(uint32_t x, uint32_t y, uint32_t xr, uint32_t yr, int angle, char *string_ptr)
Definition: Graphics.c:373
Graph_vec
void Graph_vec(uint32_t x, uint32_t y, int vecscale, int vecangle, uint16_t *vecdef_ptr)
Definition: Graphics.c:285
fill
void fill(int x, int y, int da, int db, int db2, int linechange, int lines_minus_1)
Definition: Graphics.c:511
x
int x
Definition: 07 Box and Lines.c:13
Graph_pixels
HUBDATA uint8_t Graph_pixels[]
Definition: Graphics.c:60
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
Graph_color
void Graph_color(uint32_t c)
Definition: Graphics.c:198
cogstop
#define cogstop(a)
Stop a COG.
Definition: propeller.h:100
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
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_quad
void Graph_quad(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t x3, uint32_t y3, uint32_t x4, uint32_t y4)
Definition: Graphics.c:436
Graph_box
void Graph_box(int x, int y, int box_width, int box_height)
Definition: Graphics.c:407
Graph_clear
void Graph_clear(void)
Definition: Graphics.c:178
Graph_tri
void Graph_tri(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t x3, uint32_t y3)
Definition: Graphics.c:442
GraphCmd_ST
volatile struct GraphCmd_struct GraphCmd_ST
Graphics.h
Graph_width
void Graph_width(uint32_t w)
Definition: Graphics.c:207