COSA
An Object-Oriented Platform for Arduino Programming
Canvas Class Referenceabstract

#include <Canvas.hh>

Inheritance diagram for Canvas:
Inheritance graph
Collaboration diagram for Canvas:
Collaboration graph

Classes

struct  circle16_t
 
union  color16_t
 
class  Context
 
class  Element
 
class  Image
 
struct  pos16_t
 
struct  rect16_t
 

Public Types

enum  {
  BLACK = 0x0000U, WHITE = 0xFFFFU, RED = 0xF800U, GREEN = 0x07E0U,
  BLUE = 0x001FU, YELLOW = RED + GREEN, CYAN = GREEN + BLUE, MAGENTA = RED + BLUE
}
 
enum  { PORTRAIT = 0, LANDSCAPE = 1 }
 
enum  {
  END_SCRIPT = 0, CALL_SCRIPT, SET_CANVAS_COLOR, SET_PEN_COLOR,
  SET_TEXT_COLOR, SET_TEXT_SCALE, SET_TEXT_FONT, SET_CURSOR,
  MOVE_CURSOR, DRAW_BITMAP, DRAW_ICON, DRAW_PIXEL,
  DRAW_LINE, DRAW_POLY, DRAW_STROKE, DRAW_RECT,
  FILL_RECT, DRAW_ROUNDRECT, FILL_ROUNDRECT, DRAW_CIRCLE,
  FILL_CIRCLE, DRAW_CHAR, DRAW_STRING, FILL_SCREEN
}
 

Public Member Functions

 Canvas (uint16_t width, uint16_t height, Context *context=&Canvas::context)
 
virtual bool begin ()=0
 
Contextget_context () const
 
Contextset_context (Context *context)
 
color16_t get_canvas_color () const
 
color16_t set_canvas_color (color16_t color)
 
color16_t get_pen_color () const
 
color16_t set_pen_color (color16_t color)
 
color16_t get_text_color () const
 
color16_t set_text_color (color16_t color)
 
Fontget_text_font () const
 
Fontset_text_font (Font *font)
 
uint8_t get_text_scale () const
 
uint8_t set_text_scale (uint8_t scale)
 
void get_cursor (uint16_t &x, uint16_t &y) const
 
void set_cursor (uint16_t x, uint16_t y)
 
void move_cursor (int16_t dx, int16_t dy)
 
color16_t color (uint8_t red, uint8_t green, uint8_t blue)
 
virtual uint8_t get_orientation ()
 
virtual uint8_t set_orientation (uint8_t direction)
 
virtual void draw_pixel (uint16_t x, uint16_t y)
 
void draw_pixel ()
 
virtual void draw_bitmap (uint16_t x, uint16_t y, const uint8_t *bp, uint16_t width, uint16_t height, uint8_t scale=1)
 
void draw_bitmap (const uint8_t *bp, uint16_t width, uint16_t height, uint8_t scale=1)
 
virtual void draw_icon (uint16_t x, uint16_t y, const uint8_t *bp, uint16_t width, uint16_t height, uint8_t scale=1)
 
virtual void draw_icon (uint16_t x, uint16_t y, const uint8_t *bp, uint8_t scale=1)
 
void draw_icon (const uint8_t *bp, uint8_t scale=1)
 
virtual void draw_image (uint16_t x, uint16_t y, Image *image)
 
void draw_image (Image *image)
 
virtual void draw_line (uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
 
void draw_line (uint16_t x1, uint16_t y1)
 
virtual void draw_vertical_line (uint16_t x, uint16_t y, uint16_t length)
 
void draw_vertical_line (uint16_t length)
 
virtual void draw_horizontal_line (uint16_t x, uint16_t y, uint16_t length)
 
void draw_horizontal_line (uint16_t length)
 
virtual void draw_poly_P (const int8_t *poly, uint8_t scale=1)
 
virtual void draw_stroke_P (const int8_t *stroke, uint8_t scale=1)
 
virtual void draw_rect (uint16_t x, uint16_t y, uint16_t width, uint16_t height)
 
void draw_rect (uint16_t width, uint16_t height)
 
virtual void fill_rect (uint16_t x, uint16_t y, uint16_t width, uint16_t height)
 
void fill_rect (uint16_t width, uint16_t height)
 
virtual void draw_roundrect (uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t radius)
 
void draw_roundrect (uint16_t width, uint16_t height, uint16_t radius)
 
virtual void fill_roundrect (uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t radius)
 
void fill_roundrect (uint16_t width, uint16_t height, uint16_t radius)
 
virtual void draw_circle (uint16_t x, uint16_t y, uint16_t radius)
 
void draw_circle (uint16_t radius)
 
virtual void fill_circle (uint16_t x, uint16_t y, uint16_t radius)
 
void fill_circle (uint16_t radius)
 
virtual void draw_char (uint16_t x, uint16_t y, char c)
 
void draw_char (char c)
 
virtual void draw_string (char *s)
 
virtual void draw_string (str_P s)
 
virtual void fill_screen ()
 
virtual bool end ()=0
 
void run (uint8_t ix, const void_P *tab, uint8_t max)
 

Static Public Member Functions

static color16_t shade (color16_t color, uint8_t scale)
 
static color16_t blend (color16_t c1, color16_t c2)
 

Public Attributes

uint16_t WIDTH
 
uint16_t HEIGHT
 

Protected Attributes

Contextm_context
 
uint8_t m_direction
 

Static Protected Attributes

static Context context
 

Detailed Description

Virtual Canvas device; abstraction of small screens, LCD/TFT. Device drivers need to override at least begin(), fill_rect() and end(). See ST7735R and ILI9341 for examples of usage.

Limitations

Color model is 16-bit RGB<5,6,5>. Canvas size is max 64K square.

Acknowledgements

Inspired by GFX graphics library by ladyada/adafruit, the glcd library by Michael Margolis and Bill Perry, and scd library by Sungjune Lee.

Definition at line 44 of file Canvas.hh.

Member Enumeration Documentation

anonymous enum

Basic color palette.

Enumerator
BLACK 
WHITE 
RED 
GREEN 
BLUE 
YELLOW 
CYAN 
MAGENTA 

Definition at line 100 of file Canvas.hh.

anonymous enum
Enumerator
PORTRAIT 
LANDSCAPE 

Definition at line 371 of file Canvas.hh.

anonymous enum

Canvas script instructions. See macro set below for arguments.

Enumerator
END_SCRIPT 
CALL_SCRIPT 
SET_CANVAS_COLOR 
SET_PEN_COLOR 
SET_TEXT_COLOR 
SET_TEXT_SCALE 
SET_TEXT_FONT 
SET_CURSOR 
MOVE_CURSOR 
DRAW_BITMAP 
DRAW_ICON 
DRAW_PIXEL 
DRAW_LINE 
DRAW_POLY 
DRAW_STROKE 
DRAW_RECT 
FILL_RECT 
DRAW_ROUNDRECT 
FILL_ROUNDRECT 
DRAW_CIRCLE 
FILL_CIRCLE 
DRAW_CHAR 
DRAW_STRING 
FILL_SCREEN 

Definition at line 970 of file Canvas.hh.

Constructor & Destructor Documentation

Canvas::Canvas ( uint16_t  width,
uint16_t  height,
Context context = &Canvas::context 
)
inline

Construct canvas object and initiate.

Parameters
[in]widthscreen width.
[in]heightscreen height.
[in]contextcanvas state.

Definition at line 382 of file Canvas.hh.

Member Function Documentation

virtual bool Canvas::begin ( )
pure virtual

Start interaction with device. Must override.

Returns
true(1) if successful otherwise false(0).

Implemented in OffScreen< width, height >, and GDDRAM.

Canvas::color16_t Canvas::blend ( color16_t  c1,
color16_t  c2 
)
static

Blend the two colors.

Parameters
[in]c1.
[in]c2.
Returns
color blend.

Definition at line 39 of file Canvas.cpp.

color16_t Canvas::color ( uint8_t  red,
uint8_t  green,
uint8_t  blue 
)
inline

Create 16-bit color from primary colors (RGB).

Parameters
[in]red.
[in]green.
[in]blue.
Returns
color.

Definition at line 549 of file Canvas.hh.

void Canvas::draw_bitmap ( uint16_t  x,
uint16_t  y,
const uint8_t *  bp,
uint16_t  width,
uint16_t  height,
uint8_t  scale = 1 
)
virtual

Draw bitmap with current pen color. The bitmap must be stored in program memory.

Parameters
[in]x.
[in]y.
[in]bp.
[in]width.
[in]height.
[in]scale.

Definition at line 69 of file Canvas.cpp.

void Canvas::draw_bitmap ( const uint8_t *  bp,
uint16_t  width,
uint16_t  height,
uint8_t  scale = 1 
)
inline

Draw bitmap at cursor position with current pen color. The bitmap must be stored in program memory.

Parameters
[in]bp.
[in]width.
[in]height.
[in]scale.

Definition at line 626 of file Canvas.hh.

void Canvas::draw_char ( uint16_t  x,
uint16_t  y,
char  c 
)
virtual

Draw character with current text color, font and scale.

Parameters
[in]xposition.
[in]yposition.
[in]ccharacter.

Definition at line 378 of file Canvas.cpp.

void Canvas::draw_char ( char  c)
inline

Draw character with current text color, font and scale.

Parameters
[in]ccharacter.

Definition at line 933 of file Canvas.hh.

void Canvas::draw_circle ( uint16_t  x,
uint16_t  y,
uint16_t  radius 
)
virtual

Draw circle with current pen color.

Parameters
[in]x.
[in]y.
[in]radius.

Definition at line 247 of file Canvas.cpp.

void Canvas::draw_circle ( uint16_t  radius)
inline

Draw circle at cursor position with current pen color.

Parameters
[in]radius.

Definition at line 893 of file Canvas.hh.

void Canvas::draw_horizontal_line ( uint16_t  x,
uint16_t  y,
uint16_t  length 
)
virtual

Draw horizontal line with current pen color.

Parameters
[in]x.
[in]y.
[in]length.

Reimplemented in GDDRAM.

Definition at line 204 of file Canvas.cpp.

void Canvas::draw_horizontal_line ( uint16_t  length)
inline

Draw horizontal line with given length and current pen color. Update cursor to new position.

Parameters
[in]length.

Definition at line 757 of file Canvas.hh.

void Canvas::draw_icon ( uint16_t  x,
uint16_t  y,
const uint8_t *  bp,
uint16_t  width,
uint16_t  height,
uint8_t  scale = 1 
)
virtual

Draw icon at given position with current pen color. The icon must be stored in program memory.

Parameters
[in]x.
[in]y.
[in]bp.
[in]width.
[in]height.
[in]scale.

Definition at line 105 of file Canvas.cpp.

void Canvas::draw_icon ( uint16_t  x,
uint16_t  y,
const uint8_t *  bp,
uint8_t  scale = 1 
)
virtual

Draw icon at given position with current pen color. The icon must be stored in program memory.

Parameters
[in]x.
[in]y.
[in]bp.
[in]scale.

Definition at line 113 of file Canvas.cpp.

void Canvas::draw_icon ( const uint8_t *  bp,
uint8_t  scale = 1 
)
inline

Draw icon at cursor position with current pen color. The icon must be stored in program memory.

Parameters
[in]bp.
[in]scale.

Definition at line 668 of file Canvas.hh.

void Canvas::draw_image ( uint16_t  x,
uint16_t  y,
Image image 
)
virtual

Draw image on canvas at given position.

Parameters
[in]x.
[in]y.
[in]image.

Reimplemented in GDDRAM.

Definition at line 121 of file Canvas.cpp.

void Canvas::draw_image ( Image image)
inline

Draw image on canvas at current position.

Parameters
[in]image.

Definition at line 691 of file Canvas.hh.

void Canvas::draw_line ( uint16_t  x0,
uint16_t  y0,
uint16_t  x1,
uint16_t  y1 
)
virtual

Draw line with current pen color.

Parameters
[in]x0.
[in]y0.
[in]x1.
[in]y1.

Definition at line 167 of file Canvas.cpp.

void Canvas::draw_line ( uint16_t  x1,
uint16_t  y1 
)
inline

Draw line to given position with current color. Update cursor to new position.

Parameters
[in]x1.
[in]y1.

Definition at line 714 of file Canvas.hh.

void Canvas::draw_pixel ( uint16_t  x,
uint16_t  y 
)
virtual

Set pixel with current pen color.

Parameters
[in]x.
[in]y.

Reimplemented in OffScreen< width, height >, and GDDRAM.

Definition at line 63 of file Canvas.cpp.

void Canvas::draw_pixel ( )
inline

Set pixel at cursor position with current pen color.

Definition at line 596 of file Canvas.hh.

void Canvas::draw_poly_P ( const int8_t *  poly,
uint8_t  scale = 1 
)
virtual

Draw polygon from program memory with current pen color. Vector of delta positions, terminate with 0, 0. Update cursor to end position.

Parameters
[in]poly.
[in]scale.

Definition at line 210 of file Canvas.cpp.

void Canvas::draw_rect ( uint16_t  x,
uint16_t  y,
uint16_t  width,
uint16_t  height 
)
virtual

Draw rectangle with current pen color.

Parameters
[in]x.
[in]y.
[in]width.
[in]height.

Definition at line 142 of file Canvas.cpp.

void Canvas::draw_rect ( uint16_t  width,
uint16_t  height 
)
inline

Draw rectangle at cursor position with current pen color.

Parameters
[in]width.
[in]height.

Definition at line 799 of file Canvas.hh.

void Canvas::draw_roundrect ( uint16_t  x,
uint16_t  y,
uint16_t  width,
uint16_t  height,
uint16_t  radius 
)
virtual

Draw round corner rectangle with current pen color.

Parameters
[in]x.
[in]y.
[in]width.
[in]height.
[in]radius.

Definition at line 301 of file Canvas.cpp.

void Canvas::draw_roundrect ( uint16_t  width,
uint16_t  height,
uint16_t  radius 
)
inline

Draw round corner rectangle at cursor position with current pen color.

Parameters
[in]width.
[in]height.
[in]radius.

Definition at line 847 of file Canvas.hh.

void Canvas::draw_string ( char *  s)
virtual

Draw string in current text color, font and scale.

Parameters
[in]sstring.

Definition at line 389 of file Canvas.cpp.

void Canvas::draw_string ( str_P  s)
virtual

Draw string from program memory with current text color and font.

Parameters
[in]sstring in program memory (PSTR).

Definition at line 397 of file Canvas.cpp.

void Canvas::draw_stroke_P ( const int8_t *  stroke,
uint8_t  scale = 1 
)
virtual

Draw stroke from program memory with current pen color. Vector of delta positions, terminated with 0, 0. The cursor is moved for when both dx and dy are zero or negative. Update cursor to new position.

Parameters
[in]stroke.
[in]scale.

Definition at line 226 of file Canvas.cpp.

void Canvas::draw_vertical_line ( uint16_t  x,
uint16_t  y,
uint16_t  length 
)
virtual

Draw vertical line with current pen color.

Parameters
[in]x.
[in]y.
[in]length.

Reimplemented in GDDRAM.

Definition at line 198 of file Canvas.cpp.

void Canvas::draw_vertical_line ( uint16_t  length)
inline

Draw vertical line with given length and current pen color. Update cursor to new position.

Parameters
[in]length.

Definition at line 736 of file Canvas.hh.

virtual bool Canvas::end ( )
pure virtual

Stop sequence of interaction with device. Must override.

Returns
true(1) if successful otherwise false(0).

Implemented in GDDRAM, and OffScreen< width, height >.

void Canvas::fill_circle ( uint16_t  x,
uint16_t  y,
uint16_t  radius 
)
virtual

Fill circle with current pen color.

Parameters
[in]x.
[in]y.
[in]radius.

Definition at line 280 of file Canvas.cpp.

void Canvas::fill_circle ( uint16_t  radius)
inline

Fill circle at cursor position with current pen color.

Parameters
[in]radius.

Definition at line 913 of file Canvas.hh.

void Canvas::fill_rect ( uint16_t  x,
uint16_t  y,
uint16_t  width,
uint16_t  height 
)
virtual

Fill rectangle with current pen color. Must override.

Parameters
[in]x.
[in]y.
[in]width.
[in]height.

Reimplemented in GDDRAM.

Definition at line 151 of file Canvas.cpp.

void Canvas::fill_rect ( uint16_t  width,
uint16_t  height 
)
inline

Fill rectangle at cursor position with current pen color.

Parameters
[in]width.
[in]height.

Definition at line 821 of file Canvas.hh.

void Canvas::fill_roundrect ( uint16_t  x,
uint16_t  y,
uint16_t  width,
uint16_t  height,
uint16_t  radius 
)
virtual

Fill round corner rectangle with current pen color.

Parameters
[in]x.
[in]y.
[in]width.
[in]height.
[in]radius.

Definition at line 346 of file Canvas.cpp.

void Canvas::fill_roundrect ( uint16_t  width,
uint16_t  height,
uint16_t  radius 
)
inline

Fill round corner rectangle at cursor position with current pen color.

Parameters
[in]width.
[in]height.
[in]radius.

Definition at line 873 of file Canvas.hh.

void Canvas::fill_screen ( )
virtual

Fill screen with canvas color.

Reimplemented in OffScreen< width, height >.

Definition at line 406 of file Canvas.cpp.

color16_t Canvas::get_canvas_color ( ) const
inline

Get current canvas color.

Returns
color.

Definition at line 422 of file Canvas.hh.

Context* Canvas::get_context ( ) const
inline

Get current canvas context.

Returns
context.

Definition at line 401 of file Canvas.hh.

void Canvas::get_cursor ( uint16_t &  x,
uint16_t &  y 
) const
inline

Get current cursor position.

Parameters
[out]x.
[out]y.

Definition at line 517 of file Canvas.hh.

uint8_t Canvas::get_orientation ( )
virtual

Get screen orientation.

Returns
direction (Canvas::LANDSCAPE/PORTRAIT).

Definition at line 49 of file Canvas.cpp.

color16_t Canvas::get_pen_color ( ) const
inline

Get current drawing color.

Returns
color.

Definition at line 441 of file Canvas.hh.

color16_t Canvas::get_text_color ( ) const
inline

Get current text color.

Returns
color.

Definition at line 460 of file Canvas.hh.

Font* Canvas::get_text_font ( ) const
inline

Get current text font.

Definition at line 478 of file Canvas.hh.

uint8_t Canvas::get_text_scale ( ) const
inline

Get current text scale.

Returns
text scale.

Definition at line 497 of file Canvas.hh.

void Canvas::move_cursor ( int16_t  dx,
int16_t  dy 
)
inline

Move current cursor to delta position.

Parameters
[in]dx.
[in]dy.

Definition at line 537 of file Canvas.hh.

void Canvas::run ( uint8_t  ix,
const void_P tab,
uint8_t  max 
)

Run canvas drawing script. Table may contain sub-scripts, strings and icons. All should be in program memory. Use the support macro set to write scripts.

Parameters
[in]ixscript to run.
[in]tabscript table in program memory.
[in]maxsize of script table.

Definition at line 414 of file Canvas.cpp.

color16_t Canvas::set_canvas_color ( color16_t  color)
inline

Set current canvas color. Return previous color.

Parameters
[in]color.
Returns
previous color.

Definition at line 432 of file Canvas.hh.

Context* Canvas::set_context ( Context context)
inline

Set current canvas context. Return previous context.

Parameters
[in]context.
Returns
previous context.

Definition at line 411 of file Canvas.hh.

void Canvas::set_cursor ( uint16_t  x,
uint16_t  y 
)
inline

Set current cursor position.

Parameters
[in]x.
[in]y.

Definition at line 527 of file Canvas.hh.

uint8_t Canvas::set_orientation ( uint8_t  direction)
virtual

Set screen orientation. Return previous orientation.

Parameters
[in]direction(Canvas::LANDSCAPE/PORTRAIT).
Returns
previous orientation.

Reimplemented in ILI9163, ST7735, and GDDRAM.

Definition at line 55 of file Canvas.cpp.

color16_t Canvas::set_pen_color ( color16_t  color)
inline

Set current drawing color. Return previous color.

Parameters
[in]color.
Returns
previous color.

Definition at line 451 of file Canvas.hh.

color16_t Canvas::set_text_color ( color16_t  color)
inline

Set current text color. Return previous color.

Parameters
[in]color.
Returns
previous color.

Definition at line 470 of file Canvas.hh.

Font* Canvas::set_text_font ( Font font)
inline

Set current text font. Return previous font.

Parameters
[in]font.
Returns
previous font.

Definition at line 488 of file Canvas.hh.

uint8_t Canvas::set_text_scale ( uint8_t  scale)
inline

Set current text scale (1..n). Return previous scale.

Parameters
[in]scale.
Returns
previous scale.

Definition at line 507 of file Canvas.hh.

Canvas::color16_t Canvas::shade ( color16_t  color,
uint8_t  scale 
)
static

Create color shade (0..100%).

Parameters
[in]color.
[in]scale.
Returns
color shade.

Definition at line 28 of file Canvas.cpp.

Member Data Documentation

Canvas::Context Canvas::context
staticprotected

Default Canvas context (Factory pattern).

Definition at line 1009 of file Canvas.hh.

uint16_t Canvas::HEIGHT

Definition at line 370 of file Canvas.hh.

Context* Canvas::m_context
protected

Current Canvas context (Delegation pattern).

Definition at line 1012 of file Canvas.hh.

uint8_t Canvas::m_direction
protected

Canvas direction (LANDSCAPE/PORTRAIT).

Definition at line 1015 of file Canvas.hh.

uint16_t Canvas::WIDTH

Screen size; width/height and orientation.

Definition at line 369 of file Canvas.hh.


The documentation for this class was generated from the following files: