A simple yet powerful library for controlling graphical displays. Multiple display controllers are supported using inheritance.

Dependents:   mbed_rifletool Hexi_Bubble_Game Hexi_Catch-the-dot_Game Hexi_Acceleromagnetic_Synth

NOTE: This library is in beta right now. As far as I know, everything here works, but there are many features that are lacking so far. Most notably containers, button handling, and display drivers other than the SSD1306.

Committer:
neilt6
Date:
Fri Mar 14 19:17:44 2014 +0000
Revision:
1:f7003ec66a51
Parent:
0:b876cf091464
Added rudimentary ILI9341 support, and SPI support for SSD1306

Who changed what in which revision?

UserRevisionLine numberNew contents of line
neilt6 0:b876cf091464 1 /* NeatGUI Library
neilt6 0:b876cf091464 2 * Copyright (c) 2013 Neil Thiessen
neilt6 0:b876cf091464 3 *
neilt6 0:b876cf091464 4 * Licensed under the Apache License, Version 2.0 (the "License");
neilt6 0:b876cf091464 5 * you may not use this file except in compliance with the License.
neilt6 0:b876cf091464 6 * You may obtain a copy of the License at
neilt6 0:b876cf091464 7 *
neilt6 0:b876cf091464 8 * http://www.apache.org/licenses/LICENSE-2.0
neilt6 0:b876cf091464 9 *
neilt6 0:b876cf091464 10 * Unless required by applicable law or agreed to in writing, software
neilt6 0:b876cf091464 11 * distributed under the License is distributed on an "AS IS" BASIS,
neilt6 0:b876cf091464 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
neilt6 0:b876cf091464 13 * See the License for the specific language governing permissions and
neilt6 0:b876cf091464 14 * limitations under the License.
neilt6 0:b876cf091464 15 */
neilt6 0:b876cf091464 16
neilt6 0:b876cf091464 17 #ifndef CANVAS_H
neilt6 0:b876cf091464 18 #define CANVAS_H
neilt6 0:b876cf091464 19
neilt6 0:b876cf091464 20 #include "mbed.h"
neilt6 0:b876cf091464 21 #include "Image.h"
neilt6 0:b876cf091464 22 #include "Font.h"
neilt6 0:b876cf091464 23
neilt6 0:b876cf091464 24 /** Canvas abstract class.
neilt6 0:b876cf091464 25 * Used as a base class for objects that provide 2D drawing capabilities.
neilt6 0:b876cf091464 26 */
neilt6 0:b876cf091464 27 class Canvas
neilt6 0:b876cf091464 28 {
neilt6 0:b876cf091464 29 public:
neilt6 0:b876cf091464 30 /** Create a Canvas object with the specified width and height
neilt6 0:b876cf091464 31 *
neilt6 0:b876cf091464 32 * @param w The canvas width.
neilt6 0:b876cf091464 33 * @param h The canvas height.
neilt6 0:b876cf091464 34 */
neilt6 0:b876cf091464 35 Canvas(int w, int h);
neilt6 0:b876cf091464 36
neilt6 0:b876cf091464 37 /** Draw a single pixel at the specified coordinates
neilt6 0:b876cf091464 38 *
neilt6 0:b876cf091464 39 * @param x The X coordinate.
neilt6 0:b876cf091464 40 * @param y The Y coordinate.
neilt6 0:b876cf091464 41 * @param c The color of the pixel as a 32-bit ARGB value.
neilt6 0:b876cf091464 42 */
neilt6 0:b876cf091464 43 virtual void drawPixel(int x, int y, unsigned int c) = 0;
neilt6 0:b876cf091464 44
neilt6 0:b876cf091464 45 /** Fill the entire canvas with the specified color using a filled rectangle
neilt6 0:b876cf091464 46 *
neilt6 0:b876cf091464 47 * @param c The color to fill with as a 32-bit ARGB value (black by default).
neilt6 0:b876cf091464 48 */
neilt6 0:b876cf091464 49 virtual void clear(unsigned int c = 0xFF000000);
neilt6 0:b876cf091464 50
neilt6 0:b876cf091464 51 /** Draw a line between the specified coordinates using Bresenham's line algorithm
neilt6 0:b876cf091464 52 *
neilt6 0:b876cf091464 53 * @param x0 The starting X coordinate.
neilt6 0:b876cf091464 54 * @param y0 The starting Y coordinate.
neilt6 0:b876cf091464 55 * @param x1 The ending X coordinate.
neilt6 0:b876cf091464 56 * @param y1 The ending Y coordinate.
neilt6 0:b876cf091464 57 * @param c The color of the line as a 32-bit ARGB value.
neilt6 0:b876cf091464 58 */
neilt6 0:b876cf091464 59 virtual void drawLine(int x0, int y0, int x1, int y1, unsigned int c);
neilt6 0:b876cf091464 60
neilt6 0:b876cf091464 61 /** Draw a fast horizontal line of the specified width, at the specified coordinates
neilt6 0:b876cf091464 62 *
neilt6 0:b876cf091464 63 * @param x The starting X coordinate.
neilt6 0:b876cf091464 64 * @param y The starting Y coordinate.
neilt6 0:b876cf091464 65 * @param w The width of the line.
neilt6 0:b876cf091464 66 * @param c The color of the line as a 32-bit ARGB value.
neilt6 0:b876cf091464 67 */
neilt6 0:b876cf091464 68 virtual void drawHLine(int x, int y, int w, unsigned int c);
neilt6 0:b876cf091464 69
neilt6 0:b876cf091464 70 /** Draw a fast vertical line of the specified height, at the specified coordinates
neilt6 0:b876cf091464 71 *
neilt6 0:b876cf091464 72 * @param x The starting X coordinate.
neilt6 0:b876cf091464 73 * @param y The starting Y coordinate.
neilt6 0:b876cf091464 74 * @param h The height of the line.
neilt6 0:b876cf091464 75 * @param c The color of the line as a 32-bit ARGB value.
neilt6 0:b876cf091464 76 */
neilt6 0:b876cf091464 77 virtual void drawVLine(int x, int y, int h, unsigned int c);
neilt6 0:b876cf091464 78
neilt6 0:b876cf091464 79 /** Draw an unfilled rectangle of the specified width and height, at the specified coordinates using fast lines
neilt6 0:b876cf091464 80 *
neilt6 0:b876cf091464 81 * @param x The starting X coordinate.
neilt6 0:b876cf091464 82 * @param y The starting Y coordinate.
neilt6 0:b876cf091464 83 * @param w The width of the rectangle.
neilt6 0:b876cf091464 84 * @param h The height of the rectangle.
neilt6 0:b876cf091464 85 * @param c The color of the rectangle as a 32-bit ARGB value.
neilt6 0:b876cf091464 86 */
neilt6 0:b876cf091464 87 virtual void drawRect(int x, int y, int w, int h, unsigned int c);
neilt6 0:b876cf091464 88
neilt6 0:b876cf091464 89 /** Draw a filled rectangle of the specified width and height, at the specified coordinates using fast lines
neilt6 0:b876cf091464 90 *
neilt6 0:b876cf091464 91 * @param x The starting X coordinate.
neilt6 0:b876cf091464 92 * @param y The starting Y coordinate.
neilt6 0:b876cf091464 93 * @param w The width of the rectangle.
neilt6 0:b876cf091464 94 * @param h The height of the rectangle.
neilt6 0:b876cf091464 95 * @param c The color of the rectangle as a 32-bit ARGB value.
neilt6 0:b876cf091464 96 */
neilt6 0:b876cf091464 97 virtual void fillRect(int x, int y, int w, int h, unsigned int c);
neilt6 0:b876cf091464 98
neilt6 0:b876cf091464 99 /** Draw an unfilled triangle with the specified vertices using lines
neilt6 0:b876cf091464 100 *
neilt6 0:b876cf091464 101 * @param x0 The first vertex X coordinate.
neilt6 0:b876cf091464 102 * @param y0 The first vertex Y coordinate.
neilt6 0:b876cf091464 103 * @param x1 The second vertex X coordinate.
neilt6 0:b876cf091464 104 * @param y1 The second vertex Y coordinate.
neilt6 0:b876cf091464 105 * @param x2 The third vertex X coordinate.
neilt6 0:b876cf091464 106 * @param y2 The third vertex Y coordinate.
neilt6 0:b876cf091464 107 * @param c The color of the triangle as a 32-bit ARGB value.
neilt6 0:b876cf091464 108 */
neilt6 0:b876cf091464 109 void drawTriangle(int x0, int y0, int x1, int y1, int x2, int y2, unsigned int c);
neilt6 0:b876cf091464 110
neilt6 0:b876cf091464 111 /** Draw a filled triangle with the specified vertices using Adafruit's algorithm
neilt6 0:b876cf091464 112 *
neilt6 0:b876cf091464 113 * @param x0 The first vertex X coordinate.
neilt6 0:b876cf091464 114 * @param y0 The first vertex Y coordinate.
neilt6 0:b876cf091464 115 * @param x1 The second vertex X coordinate.
neilt6 0:b876cf091464 116 * @param y1 The second vertex Y coordinate.
neilt6 0:b876cf091464 117 * @param x2 The third vertex X coordinate.
neilt6 0:b876cf091464 118 * @param y2 The third vertex Y coordinate.
neilt6 0:b876cf091464 119 * @param c The color of the triangle as a 32-bit ARGB value.
neilt6 0:b876cf091464 120 */
neilt6 0:b876cf091464 121 void fillTriangle(int x0, int y0, int x1, int y1, int x2, int y2, unsigned int c);
neilt6 0:b876cf091464 122
neilt6 0:b876cf091464 123 /** Draw an unfilled circle of the specified radius, at the specified coordinates using the midpoint circle algorithm
neilt6 0:b876cf091464 124 *
neilt6 0:b876cf091464 125 * @param x The center X coordinate.
neilt6 0:b876cf091464 126 * @param y The center Y coordinate.
neilt6 0:b876cf091464 127 * @param r The radius of the circle.
neilt6 0:b876cf091464 128 * @param c The color of the circle as a 32-bit ARGB value.
neilt6 0:b876cf091464 129 */
neilt6 0:b876cf091464 130 void drawCircle(int x, int y, int r, unsigned int c);
neilt6 0:b876cf091464 131
neilt6 0:b876cf091464 132 /** Draw a filled circle of the specified radius, at the specified coordinates using Adafruit's modified midpoint circle algorithm
neilt6 0:b876cf091464 133 *
neilt6 0:b876cf091464 134 * @param x The center X coordinate.
neilt6 0:b876cf091464 135 * @param y The center Y coordinate.
neilt6 0:b876cf091464 136 * @param r The radius of the circle.
neilt6 0:b876cf091464 137 * @param c The color of the circle as a 32-bit ARGB value.
neilt6 0:b876cf091464 138 */
neilt6 0:b876cf091464 139 void fillCircle(int x, int y, int r, unsigned int c);
neilt6 0:b876cf091464 140
neilt6 0:b876cf091464 141 /** Draw an unfilled rounded rectangle of the specified width, height, and corner radius, at the specified coordinates using Adafruit's algorithm
neilt6 0:b876cf091464 142 *
neilt6 0:b876cf091464 143 * @param x The starting X coordinate.
neilt6 0:b876cf091464 144 * @param y The starting Y coordinate.
neilt6 0:b876cf091464 145 * @param w The width of the rectangle.
neilt6 0:b876cf091464 146 * @param h The height of the rectangle.
neilt6 0:b876cf091464 147 * @param r The radius of the corners.
neilt6 0:b876cf091464 148 * @param c The color of the rectangle as a 32-bit ARGB value.
neilt6 0:b876cf091464 149 */
neilt6 0:b876cf091464 150 void drawRoundRect(int x, int y, int w, int h, int r, unsigned int c);
neilt6 0:b876cf091464 151
neilt6 0:b876cf091464 152 /** Draw a filled rounded rectangle of the specified width, height, and corner radius, at the specified coordinates using Adafruit's algorithm
neilt6 0:b876cf091464 153 *
neilt6 0:b876cf091464 154 * @param x The starting X coordinate.
neilt6 0:b876cf091464 155 * @param y The starting Y coordinate.
neilt6 0:b876cf091464 156 * @param w The width of the rectangle.
neilt6 0:b876cf091464 157 * @param h The height of the rectangle.
neilt6 0:b876cf091464 158 * @param r The radius of the corners.
neilt6 0:b876cf091464 159 * @param c The color of the rectangle as a 32-bit ARGB value.
neilt6 0:b876cf091464 160 */
neilt6 0:b876cf091464 161 void fillRoundRect(int x, int y, int w, int h, int r, unsigned int c);
neilt6 0:b876cf091464 162
neilt6 0:b876cf091464 163 /** Draw an Image object at the specified coordinates
neilt6 0:b876cf091464 164 *
neilt6 0:b876cf091464 165 * @param img Pointer to the image to draw.
neilt6 0:b876cf091464 166 * @param x The starting X coordinate.
neilt6 0:b876cf091464 167 * @param y The starting Y coordinate.
neilt6 0:b876cf091464 168 */
neilt6 1:f7003ec66a51 169 void drawImage(Image* img, int x, int y);
neilt6 0:b876cf091464 170
neilt6 0:b876cf091464 171 /** Draw a character at the specified coordinates without wrapping
neilt6 0:b876cf091464 172 *
neilt6 0:b876cf091464 173 * @param c The character to draw.
neilt6 0:b876cf091464 174 * @param fnt Pointer to the font to draw with.
neilt6 0:b876cf091464 175 * @param x The starting X coordinate.
neilt6 0:b876cf091464 176 * @param y The starting Y coordinate.
neilt6 0:b876cf091464 177 *
neilt6 0:b876cf091464 178 * @returns The width of the drawn character.
neilt6 0:b876cf091464 179 */
neilt6 1:f7003ec66a51 180 int drawChar(char c, Font* fnt, int x, int y);
neilt6 0:b876cf091464 181
neilt6 0:b876cf091464 182 /** Draw a string at the specified coordinates without wrapping
neilt6 0:b876cf091464 183 *
neilt6 0:b876cf091464 184 * @param str Pointer to the string to draw.
neilt6 0:b876cf091464 185 * @param fnt Pointer to the font to draw with.
neilt6 0:b876cf091464 186 * @param x The starting X coordinate.
neilt6 0:b876cf091464 187 * @param y The starting Y coordinate.
neilt6 0:b876cf091464 188 */
neilt6 1:f7003ec66a51 189 void drawString(const char* str, Font* fnt, int x, int y);
neilt6 0:b876cf091464 190
neilt6 0:b876cf091464 191 /** Draw a string within the specified rectangle with wrapping
neilt6 0:b876cf091464 192 *
neilt6 0:b876cf091464 193 * @param str Pointer to the string to draw.
neilt6 0:b876cf091464 194 * @param fnt Pointer to the font to draw with.
neilt6 0:b876cf091464 195 * @param x The starting X coordinate.
neilt6 0:b876cf091464 196 * @param y The starting Y coordinate.
neilt6 0:b876cf091464 197 * @param w The width of the bounding rectangle.
neilt6 0:b876cf091464 198 * @param h The height of the bounding rectangle.
neilt6 0:b876cf091464 199 */
neilt6 1:f7003ec66a51 200 void drawString(const char* str, Font* fnt, int x, int y, int w, int h);
neilt6 0:b876cf091464 201
neilt6 0:b876cf091464 202 /** Get the width of the canvas
neilt6 0:b876cf091464 203 *
neilt6 0:b876cf091464 204 * @returns The width of the canvas.
neilt6 0:b876cf091464 205 */
neilt6 1:f7003ec66a51 206 int width();
neilt6 0:b876cf091464 207
neilt6 0:b876cf091464 208 /** Get the height of the canvas
neilt6 0:b876cf091464 209 *
neilt6 0:b876cf091464 210 * @returns The height of the canvas.
neilt6 0:b876cf091464 211 */
neilt6 1:f7003ec66a51 212 int height();
neilt6 0:b876cf091464 213
neilt6 0:b876cf091464 214 protected:
neilt6 0:b876cf091464 215 //The canvas width/height
neilt6 0:b876cf091464 216 int m_Width, m_Height;
neilt6 0:b876cf091464 217
neilt6 0:b876cf091464 218 //Drawing helper functions
neilt6 0:b876cf091464 219 void drawCircleHelper(int x, int y, int r, unsigned int corner, unsigned int color);
neilt6 0:b876cf091464 220 void fillCircleHelper(int x, int y, int r, unsigned int corner, int delta, unsigned int color);
neilt6 0:b876cf091464 221 };
neilt6 0:b876cf091464 222
neilt6 0:b876cf091464 223 #endif