COSA
An Object-Oriented Platform for Arduino Programming
Canvas.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_CANVAS_HH
22 #define COSA_CANVAS_HH
23 
24 #include "Cosa/Types.h"
25 
26 // Forward declaration of default system font
27 class Font;
28 class System5x7;
29 extern System5x7 system5x7;
30 
44 class Canvas {
45 public:
49  union color16_t {
50  uint16_t rgb;
51  struct {
52  unsigned int blue:5;
53  unsigned int green:6;
54  unsigned int red:5;
55  };
56 
61  {
62  rgb = 0;
63  }
64 
69  color16_t(uint16_t color)
70  {
71  rgb = color;
72  }
73 
81  color16_t(uint8_t r, uint8_t g, uint8_t b)
82  {
83  red = r >> 3;
84  green = g >> 2;
85  blue = b >> 3;
86  }
87 
91  operator uint16_t()
92  {
93  return (rgb);
94  }
95  };
96 
100  enum {
101  BLACK = 0x0000U,
102  WHITE = 0xFFFFU,
103  RED = 0xF800U,
104  GREEN = 0x07E0U,
105  BLUE = 0x001FU,
107  CYAN = GREEN + BLUE,
108  MAGENTA = RED + BLUE
109  };
110 
114  struct pos16_t {
115  uint16_t x;
116  uint16_t y;
117  };
118 
122  struct rect16_t {
123  uint16_t x;
124  uint16_t y;
125  uint16_t width;
126  uint16_t height;
127  };
128 
132  struct circle16_t {
133  uint16_t x;
134  uint16_t y;
135  uint16_t radius;
136  };
137 
141  class Context {
142  public:
150  Context(Font* font = (Font*) &system5x7) :
151  m_pen_color(BLACK),
152  m_canvas_color(WHITE),
153  m_text_color(BLACK),
154  m_text_scale(1),
155  m_font(font)
156  {
157  set_cursor(0, 0);
158  }
159 
165  {
166  return (m_canvas_color);
167  }
168 
175  {
176  color16_t previous = m_canvas_color;
177  m_canvas_color = color;
178  return (previous);
179  }
180 
186  {
187  return (m_pen_color);
188  }
189 
196  {
197  color16_t previous = m_pen_color;
198  m_pen_color = color;
199  return (previous);
200  }
201 
207  {
208  return (m_text_color);
209  }
210 
217  {
218  color16_t previous = m_text_color;
219  m_text_color = color;
220  return (previous);
221  }
222 
227  {
228  return (m_font);
229  }
230 
238  {
239  Font* previous = m_font;
240  m_font = font;
241  return (previous);
242  }
243 
248  uint8_t get_text_scale() const
249  {
250  return (m_text_scale);
251  }
252 
259  uint8_t set_text_scale(uint8_t scale)
260  {
261  uint8_t previous = m_text_scale;
262  m_text_scale = (scale > 0 ? scale : 1);
263  return (previous);
264  }
265 
271  void get_cursor(uint16_t& x, uint16_t& y) const
272  {
273  x = m_cursor.x;
274  y = m_cursor.y;
275  }
276 
282  void set_cursor(uint16_t x, uint16_t y)
283  {
284  m_cursor.x = x;
285  m_cursor.y = y;
286  }
287 
293  void move_cursor(int16_t dx, int16_t dy)
294  {
295  m_cursor.x += dx;
296  m_cursor.y += dy;
297  }
298 
299  protected:
303  uint8_t m_text_scale;
306  };
307 
312  class Element : public Context {
313  public:
320  Element(Canvas* canvas, Font* font = (Font*) &system5x7) :
321  Context(font),
322  m_canvas(canvas)
323  {
324  }
325  protected:
327  };
328 
334  class Image {
335  public:
337  uint16_t WIDTH;
338 
340  uint16_t HEIGHT;
341 
347  Image(uint16_t width = 0, uint16_t height = 0) :
348  WIDTH(width),
349  HEIGHT(height)
350  {}
351 
360  virtual bool read(color16_t* buf, size_t count) = 0;
361 
363  static const size_t BUFFER_MAX = 32;
364  };
365 
369  uint16_t WIDTH;
370  uint16_t HEIGHT;
371  enum {
372  PORTRAIT = 0,
374  } __attribute__((packed));
375 
382  Canvas(uint16_t width, uint16_t height, Context* context = &Canvas::context) :
383  WIDTH(width),
384  HEIGHT(height),
387  {
388  }
389 
395  virtual bool begin() = 0;
396 
402  {
403  return (m_context);
404  }
405 
412  {
413  Context* previous = m_context;
414  m_context = context;
415  return (previous);
416  }
417 
423  {
424  return (m_context->get_canvas_color());
425  }
426 
433  {
434  return (m_context->set_canvas_color(color));
435  }
436 
442  {
443  return (m_context->get_pen_color());
444  }
445 
452  {
453  return (m_context->set_pen_color(color));
454  }
455 
461  {
462  return (m_context->get_text_color());
463  }
464 
471  {
472  return (m_context->set_text_color(color));
473  }
474 
479  {
480  return (m_context->get_text_font());;
481  }
482 
489  {
490  return (m_context->set_text_font(font));
491  }
492 
497  uint8_t get_text_scale() const
498  {
499  return (m_context->get_text_scale());
500  }
501 
507  uint8_t set_text_scale(uint8_t scale)
508  {
509  return (m_context->set_text_scale(scale));
510  }
511 
517  void get_cursor(uint16_t& x, uint16_t& y) const
518  {
519  m_context->get_cursor(x, y);
520  }
521 
527  void set_cursor(uint16_t x, uint16_t y)
528  {
529  m_context->set_cursor(x, y);
530  }
531 
537  void move_cursor(int16_t dx, int16_t dy)
538  {
539  m_context->move_cursor(dx, dy);
540  }
541 
549  color16_t color(uint8_t red, uint8_t green, uint8_t blue)
550  {
551  return (color16_t(red, green, blue));
552  }
553 
560  static color16_t shade(color16_t color, uint8_t scale);
561 
568  static color16_t blend(color16_t c1, color16_t c2);
569 
575  virtual uint8_t get_orientation();
576 
583  virtual uint8_t set_orientation(uint8_t direction);
584 
591  virtual void draw_pixel(uint16_t x, uint16_t y);
592 
596  void draw_pixel()
597  {
598  uint16_t x, y;
599  get_cursor(x, y);
600  draw_pixel(x, y);
601  }
602 
614  virtual void draw_bitmap(uint16_t x, uint16_t y, const uint8_t* bp,
615  uint16_t width, uint16_t height,
616  uint8_t scale = 1);
617 
626  void draw_bitmap(const uint8_t* bp,
627  uint16_t width, uint16_t height,
628  uint8_t scale = 1)
629  {
630  uint16_t x, y;
631  get_cursor(x, y);
632  draw_bitmap(x, y, bp, width, height, scale);
633  }
634 
646  virtual void draw_icon(uint16_t x, uint16_t y, const uint8_t* bp,
647  uint16_t width, uint16_t height,
648  uint8_t scale = 1);
649 
659  virtual void draw_icon(uint16_t x, uint16_t y, const uint8_t* bp,
660  uint8_t scale = 1);
661 
668  void draw_icon(const uint8_t* bp, uint8_t scale = 1)
669  {
670  uint16_t width = pgm_read_byte(bp++);
671  uint16_t height = pgm_read_byte(bp++);
672  uint16_t x, y;
673  get_cursor(x, y);
674  draw_icon(x, y, bp, width, height, scale);
675  }
676 
684  virtual void draw_image(uint16_t x, uint16_t y, Image* image);
685 
691  void draw_image(Image* image)
692  {
693  uint16_t x, y;
694  get_cursor(x, y);
695  draw_image(x, y, image);
696  }
697 
706  virtual void draw_line(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1);
707 
714  void draw_line(uint16_t x1, uint16_t y1)
715  {
716  uint16_t x0, y0;
717  get_cursor(x0, y0);
718  draw_line(x0, y0, x1, y1);
719  set_cursor(x1, y1);
720  }
721 
729  virtual void draw_vertical_line(uint16_t x, uint16_t y, uint16_t length);
730 
736  void draw_vertical_line(uint16_t length)
737  {
738  uint16_t x, y;
739  get_cursor(x, y);
740  draw_line(x, y + length);
741  }
742 
750  virtual void draw_horizontal_line(uint16_t x, uint16_t y, uint16_t length);
751 
757  void draw_horizontal_line(uint16_t length)
758  {
759  uint16_t x, y;
760  get_cursor(x, y);
761  draw_line(x + length, y);
762  }
763 
771  virtual void draw_poly_P(const int8_t* poly, uint8_t scale = 1);
772 
782  virtual void draw_stroke_P(const int8_t* stroke, uint8_t scale = 1);
783 
792  virtual void draw_rect(uint16_t x, uint16_t y, uint16_t width, uint16_t height);
793 
799  void draw_rect(uint16_t width, uint16_t height)
800  {
801  uint16_t x, y;
802  get_cursor(x, y);
803  draw_rect(x, y, width, height);
804  }
805 
814  virtual void fill_rect(uint16_t x, uint16_t y, uint16_t width, uint16_t height);
815 
821  void fill_rect(uint16_t width, uint16_t height)
822  {
823  uint16_t x, y;
824  get_cursor(x, y);
825  fill_rect(x, y, width, height);
826  }
827 
837  virtual void draw_roundrect(uint16_t x, uint16_t y,
838  uint16_t width, uint16_t height,
839  uint16_t radius);
840 
847  void draw_roundrect(uint16_t width, uint16_t height, uint16_t radius)
848  {
849  uint16_t x, y;
850  get_cursor(x, y);
851  draw_roundrect(x, y, width, height, radius);
852  }
853 
863  virtual void fill_roundrect(uint16_t x, uint16_t y,
864  uint16_t width, uint16_t height,
865  uint16_t radius);
866 
873  void fill_roundrect(uint16_t width, uint16_t height, uint16_t radius)
874  {
875  uint16_t x, y;
876  get_cursor(x, y);
877  fill_roundrect(x, y, width, height, radius);
878  }
879 
887  virtual void draw_circle(uint16_t x, uint16_t y, uint16_t radius);
888 
893  void draw_circle(uint16_t radius)
894  {
895  uint16_t x, y;
896  get_cursor(x, y);
897  draw_circle(x, y, radius);
898  }
899 
907  virtual void fill_circle(uint16_t x, uint16_t y, uint16_t radius);
908 
913  void fill_circle(uint16_t radius)
914  {
915  uint16_t x, y;
916  get_cursor(x, y);
917  fill_circle(x, y, radius);
918  }
919 
927  virtual void draw_char(uint16_t x, uint16_t y, char c);
928 
933  void draw_char(char c)
934  {
935  uint16_t x, y;
936  get_cursor(x, y);
937  draw_char(x, y, c);
938  }
939 
945  virtual void draw_string(char* s);
946 
952  virtual void draw_string(str_P s);
953 
958  virtual void fill_screen();
959 
965  virtual bool end() = 0;
966 
970  enum {
995  } __attribute__((packed));
996 
1005  void run(uint8_t ix, const void_P* tab, uint8_t max);
1006 
1007 protected:
1010 
1013 
1015  uint8_t m_direction;
1016 };
1017 
1021 #define CANVAS_BEGIN_SCRIPT(name) const uint8_t name[] __PROGMEM = {
1022 #define CANVAS_CALL_SCRIPT(ix) Canvas::CALL_SCRIPT, ix,
1023 #define CANVAS_SET_CANVAS_COLOR(r, g, b) Canvas::SET_CANVAS_COLOR, r, g, b,
1024 #define CANVAS_SET_PEN_COLOR(r, g, b) Canvas::SET_PEN_COLOR, r, g, b,
1025 #define CANVAS_SET_TEXT_COLOR(r, g, b) Canvas::SET_TEXT_COLOR, r, g, b,
1026 #define CANVAS_SET_TEXT_SCALE(s) Canvas::SET_TEXT_SCALE, s,
1027 #define CANVAS_SET_TEXT_PORT(x, y, w, h) Canvas::SET_TEXT_PORT, x, y, w, h,
1028 #define CANVAS_SET_TEXT_FONT(ix) Canvas::SET_TEXT_FONT, ix,
1029 #define CANVAS_SET_CURSOR(x, y) Canvas::SET_CURSOR, x, y,
1030 #define CANVAS_MOVE_CURSOR(dx, dy) Canvas::MOVE_CURSOR, (uint8_t) dx, (uint8_t) dy,
1031 #define CANVAS_DRAW_BITMAP(ix, w, h, s) Canvas::DRAW_BITMAP, ix, w, h, s,
1032 #define CANVAS_DRAW_ICON(ix, s) Canvas::DRAW_ICON, ix, s,
1033 #define CANVAS_DRAW_PIXEL() Canvas::DRAW_PIXEL,
1034 #define CANVAS_DRAW_LINE(x, y) Canvas::DRAW_LINE, x, y,
1035 #define CANVAS_DRAW_POLY(ix, s) Canvas::DRAW_POLY, ix, s,
1036 #define CANVAS_DRAW_STROKE(ix, s) Canvas::DRAW_STROKE, ix, s,
1037 #define CANVAS_DRAW_RECT(w, h) Canvas::DRAW_RECT, w, h,
1038 #define CANVAS_FILL_RECT(w, h) Canvas::FILL_RECT, w, h,
1039 #define CANVAS_DRAW_ROUNDRECT(w, h, r) Canvas::DRAW_ROUNDRECT, w, h, r,
1040 #define CANVAS_FILL_ROUNDRECT(w, h, r) Canvas::FILL_ROUNDRECT, w, h, r,
1041 #define CANVAS_DRAW_CIRCLE(r) Canvas::DRAW_CIRCLE, r,
1042 #define CANVAS_FILL_CIRCLE(r) Canvas::FILL_CIRCLE, r,
1043 #define CANVAS_DRAW_CHAR(c) Canvas::DRAW_CHAR, c,
1044 #define CANVAS_DRAW_STRING(ix) Canvas::DRAW_STRING, ix,
1045 #define CANVAS_FILL_SCREEN() Canvas::FILL_SCREEN,
1046 #define CANVAS_END_SCRIPT Canvas::END_SCRIPT };
1047 
1048 #endif
color16_t set_text_color(color16_t color)
Definition: Canvas.hh:470
virtual uint8_t set_orientation(uint8_t direction)
Definition: Canvas.cpp:55
color16_t get_pen_color() const
Definition: Canvas.hh:185
color16_t(uint16_t color)
Definition: Canvas.hh:69
void fill_circle(uint16_t radius)
Definition: Canvas.hh:913
uint16_t width
Definition: Canvas.hh:125
Font * m_font
Current font.
Definition: Canvas.hh:304
color16_t m_canvas_color
Current background color.
Definition: Canvas.hh:301
uint16_t y
Definition: Canvas.hh:124
Definition: Font.hh:30
void set_cursor(uint16_t x, uint16_t y)
Definition: Canvas.hh:527
virtual uint8_t get_orientation()
Definition: Canvas.cpp:49
pos16_t m_cursor
Current cursor position.
Definition: Canvas.hh:305
virtual void draw_char(uint16_t x, uint16_t y, char c)
Definition: Canvas.cpp:378
uint16_t y
Definition: Canvas.hh:116
unsigned int green
Definition: Canvas.hh:53
void fill_rect(uint16_t width, uint16_t height)
Definition: Canvas.hh:821
uint8_t set_text_scale(uint8_t scale)
Definition: Canvas.hh:259
color16_t set_text_color(color16_t color)
Definition: Canvas.hh:216
uint8_t get_text_scale() const
Definition: Canvas.hh:248
virtual void draw_circle(uint16_t x, uint16_t y, uint16_t radius)
Definition: Canvas.cpp:247
void fill_roundrect(uint16_t width, uint16_t height, uint16_t radius)
Definition: Canvas.hh:873
virtual void draw_image(uint16_t x, uint16_t y, Image *image)
Definition: Canvas.cpp:121
void draw_image(Image *image)
Definition: Canvas.hh:691
color16_t color(uint8_t red, uint8_t green, uint8_t blue)
Definition: Canvas.hh:549
void draw_rect(uint16_t width, uint16_t height)
Definition: Canvas.hh:799
color16_t set_pen_color(color16_t color)
Definition: Canvas.hh:451
IOStream & tab(IOStream &outs)
Definition: IOStream.hh:805
color16_t set_canvas_color(color16_t color)
Definition: Canvas.hh:432
Canvas(uint16_t width, uint16_t height, Context *context=&Canvas::context)
Definition: Canvas.hh:382
color16_t get_canvas_color() const
Definition: Canvas.hh:422
color16_t get_pen_color() const
Definition: Canvas.hh:441
virtual void draw_icon(uint16_t x, uint16_t y, const uint8_t *bp, uint16_t width, uint16_t height, uint8_t scale=1)
Definition: Canvas.cpp:105
uint16_t x
Definition: Canvas.hh:123
void run(uint8_t ix, const void_P *tab, uint8_t max)
Definition: Canvas.cpp:414
Font * set_text_font(Font *font)
Definition: Canvas.hh:488
System5x7 system5x7
Definition: System5x7.cpp:24
Image(uint16_t width=0, uint16_t height=0)
Definition: Canvas.hh:347
uint8_t get_text_scale() const
Definition: Canvas.hh:497
uint8_t set_text_scale(uint8_t scale)
Definition: Canvas.hh:507
Context(Font *font=(Font *)&system5x7)
Definition: Canvas.hh:150
Context * get_context() const
Definition: Canvas.hh:401
void move_cursor(int16_t dx, int16_t dy)
Definition: Canvas.hh:537
unsigned int blue
Definition: Canvas.hh:52
virtual void fill_circle(uint16_t x, uint16_t y, uint16_t radius)
Definition: Canvas.cpp:280
virtual void fill_rect(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
Definition: Canvas.cpp:151
void draw_circle(uint16_t radius)
Definition: Canvas.hh:893
uint16_t rgb
Definition: Canvas.hh:50
color16_t get_text_color() const
Definition: Canvas.hh:460
const class prog_str * str_P
Definition: Types.h:187
void draw_vertical_line(uint16_t length)
Definition: Canvas.hh:736
Context * set_context(Context *context)
Definition: Canvas.hh:411
virtual bool begin()=0
color16_t m_pen_color
Current foreground color.
Definition: Canvas.hh:300
uint16_t HEIGHT
Definition: Canvas.hh:340
color16_t m_text_color
Current text color.
Definition: Canvas.hh:302
void draw_roundrect(uint16_t width, uint16_t height, uint16_t radius)
Definition: Canvas.hh:847
void draw_icon(const uint8_t *bp, uint8_t scale=1)
Definition: Canvas.hh:668
void get_cursor(uint16_t &x, uint16_t &y) const
Definition: Canvas.hh:517
void draw_bitmap(const uint8_t *bp, uint16_t width, uint16_t height, uint8_t scale=1)
Definition: Canvas.hh:626
Canvas * m_canvas
Definition: Canvas.hh:326
Font * get_text_font() const
Definition: Canvas.hh:478
virtual void fill_roundrect(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t radius)
Definition: Canvas.cpp:346
virtual bool end()=0
uint16_t radius
Definition: Canvas.hh:135
virtual void fill_screen()
Definition: Canvas.cpp:406
uint8_t m_text_scale
Current text scale.
Definition: Canvas.hh:303
color16_t get_text_color() const
Definition: Canvas.hh:206
virtual void draw_string(char *s)
Definition: Canvas.cpp:389
void draw_horizontal_line(uint16_t length)
Definition: Canvas.hh:757
Font * set_text_font(Font *font)
Definition: Canvas.hh:237
static Context context
Definition: Canvas.hh:1009
color16_t get_canvas_color() const
Definition: Canvas.hh:164
uint16_t WIDTH
Definition: Canvas.hh:337
static color16_t shade(color16_t color, uint8_t scale)
Definition: Canvas.cpp:28
void move_cursor(int16_t dx, int16_t dy)
Definition: Canvas.hh:293
color16_t set_pen_color(color16_t color)
Definition: Canvas.hh:195
virtual void draw_poly_P(const int8_t *poly, uint8_t scale=1)
Definition: Canvas.cpp:210
uint16_t x
Definition: Canvas.hh:115
virtual void draw_line(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
Definition: Canvas.cpp:167
virtual void draw_vertical_line(uint16_t x, uint16_t y, uint16_t length)
Definition: Canvas.cpp:198
const void * void_P
Definition: Types.h:260
uint16_t HEIGHT
Definition: Canvas.hh:370
virtual void draw_roundrect(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t radius)
Definition: Canvas.cpp:301
unsigned int red
Definition: Canvas.hh:54
virtual void draw_bitmap(uint16_t x, uint16_t y, const uint8_t *bp, uint16_t width, uint16_t height, uint8_t scale=1)
Definition: Canvas.cpp:69
Context * m_context
Definition: Canvas.hh:1012
void draw_line(uint16_t x1, uint16_t y1)
Definition: Canvas.hh:714
void draw_pixel()
Definition: Canvas.hh:596
void get_cursor(uint16_t &x, uint16_t &y) const
Definition: Canvas.hh:271
color16_t set_canvas_color(color16_t color)
Definition: Canvas.hh:174
Definition: Canvas.hh:44
void set_cursor(uint16_t x, uint16_t y)
Definition: Canvas.hh:282
Element(Canvas *canvas, Font *font=(Font *)&system5x7)
Definition: Canvas.hh:320
virtual void draw_stroke_P(const int8_t *stroke, uint8_t scale=1)
Definition: Canvas.cpp:226
uint16_t WIDTH
Definition: Canvas.hh:369
virtual void draw_horizontal_line(uint16_t x, uint16_t y, uint16_t length)
Definition: Canvas.cpp:204
void draw_char(char c)
Definition: Canvas.hh:933
uint8_t m_direction
Definition: Canvas.hh:1015
color16_t(uint8_t r, uint8_t g, uint8_t b)
Definition: Canvas.hh:81
uint16_t height
Definition: Canvas.hh:126
static color16_t blend(color16_t c1, color16_t c2)
Definition: Canvas.cpp:39
Font * get_text_font() const
Definition: Canvas.hh:226
virtual void draw_rect(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
Definition: Canvas.cpp:142