Display Simple Graphics library  v0.5
Library containing shape, text, and bitmap drawing functions called by various display drivers
simplegfx.h
Go to the documentation of this file.
1 /*
2  * @file simplegfx.h
3  *
4  * @author Matthew Matz
5  *
6  * @version 0.5
7  *
8  * @copyright Copyright (C) Parallax, Inc. 2019. See end of file for
9  * terms of use (MIT License).
10  *
11  * @brief This is a driver that allows the Propeller Multicore Microcontroller to
12  * draw text, shapes, and bitmap files on various screens and displays.
13  *
14  * @detail This helper-library allows the Propeller Multicore Microcontroller to
15  * draw pixels, lines, circles, recatngles, rounded rectagles, triangles, formatted text
16  * in multiple fonts, bitmap images stored on an SD card on various screens and displays.
17  * At Parallax, we would like to thank Adafruit Industries as parts of this library
18  * were dervied from the Adafruit GFX library for Arduino. Please submit bug reports,
19  * suggestions, and improvements to this code to editor@parallax.com.
20  *
21  * @note If fonts are installed, they occupy EEPROM addresses 40576 to 63359.
22  */
23 
24 #ifndef _SCREEN_GFX_
25 #define _SCREEN_GFX_
26 
27 #if defined(__cplusplus) // If compiling for C++
28 extern "C" { // Compile for C
29 #endif
30 
31 
32 #include "simpletools.h"
33 
34 
35 // Font size and face
36 #ifndef SMALL
37  #define SMALL 1
38 #endif
39 
40 #ifndef MEDIUM
41  #define MEDIUM 2
42 #endif
43 
44 #ifndef LARGE
45  #define LARGE 3
46 #endif
47 
48 
49 #ifndef FONT_SANS
50  #define FONT_SANS 0
51 #endif
52 
53 #ifndef FONT_SERIF
54  #define FONT_SERIF 1
55 #endif
56 
57 #ifndef FONT_SCRIPT
58  #define FONT_SCRIPT 2
59 #endif
60 
61 #ifndef FONT_BUBBLE
62  #define FONT_BUBBLE 3
63 #endif
64 
65 
66 #ifndef INTF_SPI_NO_BUFFER
67  #define INTF_SPI_NO_BUFFER 0b00
68 #endif
69 
70 #ifndef INTF_SPI_WITH_BUFFER
71  #define INTF_SPI_WITH_BUFFER 0b10
72 #endif
73 
74 #ifndef INTF_I2C_NO_BUFFER
75  #define INTF_I2C_NO_BUFFER 0b01
76 #endif
77 
78 #ifndef INTF_I2C_WITH_BUFFER
79  #define INTF_I2C_WITH_BUFFER 0b11
80 #endif
81 
82 
83 #ifndef gfx_swap
84  #define gfx_swap(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b))) // No-temp-var swap operation
85 #endif
86 
87 #ifndef absv
88  #define absv(x) ((x)<0 ? -(x) : (x))
89 #endif
90 
91 
92 // Radix Constants
93 #ifndef HEX
94  #define HEX -16
95 #endif
96 
97 #ifndef OCT
98  #define OCT -8
99 #endif
100 
101 #ifndef BIN
102  #define BIN -2
103 #endif
104 
105 #ifndef DEC
106  #define DEC -10
107 #endif
108 
109 
110 
111 typedef struct screen_st {
113  int width;
114 
116  int height;
117 
119  int rotation;
120 
122  int cursor_x;
123 
125  int cursor_y;
126 
128  int font[5];
129 
132 
134  char text_size;
135 
137  char text_wrap;
138 
141 
143  int bg_color;
144 
147 
148 
150  i2c* eepromBus;
151 
152 
154  i2c* deviceBus;
155 
158 
159 
161  unsigned char dev_id;
162 
165 
167  char sdi_pin;
168 
170  char rst_pin;
171 
173  char clk_pin;
174 
176  char dc_pin;
177 
178 
181 
183  char* image_ptr;
184 
185 
188 
191 
194 
195 
197  char (*deviceWriteLock) (void);
198 
200  void (*deviceWriteLockSet) (struct screen_st*);
201 
203  void (*deviceWriteLockClear) (struct screen_st*);
204 
205 
207  void (*deviceDrawPixel) (struct screen_st*, int x, int y, int color);
208 
210  int (*deviceGetPixel) (struct screen_st*, int x, int y);
211 
213  void (*deviceDrawLine) (struct screen_st*, int x, int y, int w, int h, int color);
214 
216  void (*deviceDrawFastHLine) (struct screen_st*, int x, int y, int w, int color);
217 
219  void (*deviceDrawFastVLine) (struct screen_st*, int x, int y, int w, int color);
220 
222  void (*deviceFillRect) (struct screen_st*, int x, int y, int w, int h, int color);
223 
225  void (*deviceCopyRect) (struct screen_st*, int x, int y, int x0, int y0, int x1, int y1);
226 
227 
229  void (*deviceClearDisplay) (struct screen_st*);
230 
232  void (*deviceUpdateDisplay) (struct screen_st*);
233 
235  void (*deviceResetDisplay) (struct screen_st*);
236 
238  void (*deviceInvertDisplay) (struct screen_st*, char i);
239 
241  void (*deviceSleepWakeDisplay)(struct screen_st*, char i);
242 
244  void (*deviceScrollDisplay) (struct screen_st*, int h, int v);
245 
246 } screen_t;
247 
248 typedef screen_t screen;
249 
250 
251 
252 
253 
254 
260 void clearDisplay(screen_t *dev);
261 
268 void updateDisplay(screen_t *dev);
269 
275 void resetDisplay(screen_t *dev);
276 
288 void drawPixel(screen_t *dev, int x, int y, int color);
289 
303 void drawFastHLine(screen_t *dev, int x, int y, int w, int color);
304 
318 void drawFastVLine(screen_t *dev, int x, int y, int h, int color);
319 
335 void drawLine(screen_t *dev, int x0, int y0, int x1, int y1, int color);
336 
350 void drawCircle(screen_t *dev, int x0, int y0, int r, int color);
351 
367 void drawCircleHelper(screen_t *dev, int x0, int y0, int r, char cornername, int color);
368 
382 void fillCircle(screen_t *dev, int x0, int y0, int r, int color);
383 
402 void fillCircleHelper(screen_t *dev, int x0, int y0, int r, char cornername, int delta, int color);
403 
419 void drawRect(screen_t *dev, int x, int y, int w, int h, int color);
420 
436 void fillRect(screen_t *dev, int x, int y, int w, int h, int color);
437 
455 void drawRoundRect(screen_t *dev, int x, int y, int w, int h, int r, int color);
456 
474 void fillRoundRect(screen_t *dev, int x, int y, int w, int h, int r, int color);
475 
495 void drawTriangle(screen_t *dev, int x0, int y0, int x1, int y1, int x2, int y2, int color);
496 
516 void fillTriangle(screen_t *dev, int x0, int y0, int x1, int y1, int x2, int y2, int color);
517 
535 void drawCopy(screen_t *dev, int x, int y, int w, int h, int x1, int y1);
536 
547 void setTextSize(screen_t *dev, char s);
548 
558 void setTextFont(screen_t *dev, char f);
559 
569 void setTextColor(screen_t *dev, int color);
570 
580 void setBgColor(screen_t *dev, int color);
581 
589 void setTextWrap(screen_t *dev, char w);
590 
605 void setCursor(screen_t *dev, int x, int y, char size);
606 
614 int drawPrint(screen_t *dev, const char *fmt, ...);
615 
627 void drawNumber(screen_t *dev, float d, int r);
628 
636 void drawText(screen_t *dev, char *myString);
637 
645 void drawChar(screen_t *dev, char c);
646 
654 int getCursorX(screen_t *dev);
655 
663 int getCursorY(screen_t *dev);
664 
675 void setDisplayRotation(screen_t *dev, char r);
676 
687 char getDisplayRotation(screen_t *dev);
688 
694 int getDisplayWidth(screen_t *dev);
695 
701 int getDisplayHeight(screen_t *dev);
702 
712 void loadFonts(screen_t *dev, i2c *eeBus);
713 
721 void drawCharSmall(screen_t *dev, unsigned char c);
722 
730 void drawCharMedium(screen_t *dev, unsigned char c);
731 
739 void drawCharLarge(screen_t *dev, unsigned char c);
740 
750 void invertDisplay(screen_t *dev, char i);
751 
759 void sleepWakeDisplay(screen_t *dev, char i);
760 
775 void scrollDisplay(screen_t *dev, int h, int v);
776 
785 char isInverted(screen_t *dev);
786 
794 char isAsleep(screen_t *dev);
795 
804 unsigned char getScrollVertical(screen_t *dev);
805 
814 unsigned char getScrollHorizontal(screen_t *dev);
815 
828 void drawBitmap(screen_t *dev, char *imgdir, int x, int y);
829 
830 
831 #if defined(__cplusplus)
832 } // End compile for C block
833 #endif
834 /* __cplusplus */
835 
836 
837 #endif // _SCREEN_GFX_
838 
839 
840 /*
841  * Because this library is bsed in part on the Adafruit GFX library, this
842  * notice must be included with this distribution:
843  * --------------------------------------------------------------------------
844  * This is the core graphics library for all our displays, providing a common
845  * set of graphics primitives (points, lines, circles, etc.). It needs to be
846  * paired with a hardware-specific library for each display device we carry
847  * (to handle the lower-level functions).
848  *
849  * Adafruit invests time and resources providing this open source code, please
850  * support Adafruit & open-source hardware by purchasing products from Adafruit!
851  *
852  * Copyright (c) 2013 Adafruit Industries. All rights reserved.
853  *
854  * Redistribution and use in source and binary forms, with or without
855  * modification, are permitted provided that the following conditions are met:
856  * - Redistributions of source code must retain the above copyright notice,
857  * this list of conditions and the following disclaimer.
858  * - Redistributions in binary form must reproduce the above copyright notice,
859  * this list of conditions and the following disclaimer in the documentation
860  * and/or other materials provided with the distribution.
861  *
862  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
863  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
864  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
865  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
866  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
867  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
868  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
869  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
870  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
871  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
872  * POSSIBILITY OF SUCH DAMAGE.
873  * --------------------------------------------------------------------------
874  */
875 
char * customCharSmall
Definition: simplegfx.h:187
void drawCircle(screen_t *dev, int x0, int y0, int r, int color)
Draws a circle in the specified color.
Definition: drawCircle.c:29
void setTextWrap(screen_t *dev, char w)
Toggles automatic wrapping of text printed to the screen.
Definition: setText.c:56
int cursor_y
Definition: simplegfx.h:125
void drawFastVLine(screen_t *dev, int x, int y, int h, int color)
Draws a vertical line in the specified color.
Definition: drawLine.c:199
char * customCharLarge
Definition: simplegfx.h:193
void(* deviceDrawPixel)(struct screen_st *, int x, int y, int color)
Definition: simplegfx.h:207
void drawCharMedium(screen_t *dev, unsigned char c)
Low-level helper function for printing single characters to the screen in the medium (11x15) font...
Definition: drawChar.c:149
int display_mode
Definition: simplegfx.h:146
void(* deviceUpdateDisplay)(struct screen_st *)
Definition: simplegfx.h:232
int image_size
Definition: simplegfx.h:180
void(* deviceDrawLine)(struct screen_st *, int x, int y, int w, int h, int color)
Definition: simplegfx.h:213
void drawLine(screen_t *dev, int x0, int y0, int x1, int y1, int color)
Draws a line in the specified color.
Definition: drawLine.c:43
int bg_color
Definition: simplegfx.h:143
char(* deviceWriteLock)(void)
Definition: simplegfx.h:197
char color_depth
Definition: simplegfx.h:131
void fillCircle(screen_t *dev, int x0, int y0, int r, int color)
Draws a filled circle in the specified color.
Definition: fillCircle.c:27
int getCursorX(screen_t *dev)
Returns the current horizontal position of the cursor, measured from the left side of the screen in p...
char dc_pin
Definition: simplegfx.h:176
i2c * deviceBus
Definition: simplegfx.h:154
int rotation
Definition: simplegfx.h:119
unsigned char getScrollVertical(screen_t *dev)
Returns a value representing the status or speed of the display&#39;s vertical scroll.
Definition: displayControl.c:95
void drawRect(screen_t *dev, int x, int y, int w, int h, int color)
Draws a rectangle in the specified color.
Definition: drawRect.c:29
void(* deviceWriteLockSet)(struct screen_st *)
Definition: simplegfx.h:200
void drawCharSmall(screen_t *dev, unsigned char c)
Low-level helper function for printing single characters to the screen in the small (5x7) font...
Definition: drawChar.c:127
char isInverted(screen_t *dev)
Returns the status of the screen&#39;s colors - normal or inverted.
Definition: displayControl.c:83
void(* deviceInvertDisplay)(struct screen_st *, char i)
Definition: simplegfx.h:238
void(* deviceSleepWakeDisplay)(struct screen_st *, char i)
Definition: simplegfx.h:241
void drawFastHLine(screen_t *dev, int x, int y, int w, int color)
Draws a horizontal line in the specified color.
Definition: drawLine.c:164
void sleepWakeDisplay(screen_t *dev, char i)
Toggle the display&#39;s sleep/wake mode. Not supported by all displays.
Definition: displayControl.c:70
Definition: simplegfx.h:111
void resetDisplay(screen_t *dev)
Resets the display.
Definition: displayControl.c:36
void(* deviceWriteLockClear)(struct screen_st *)
Definition: simplegfx.h:203
void setTextSize(screen_t *dev, char s)
Sets the size of the font to be used. Range is from 1 to 3. Size (1) is 5x7 (6x8 spacing) pixels...
Definition: setText.c:37
screen_t screen
Definition: simplegfx.h:248
void drawCircleHelper(screen_t *dev, int x0, int y0, int r, char cornername, int color)
Helper function used to help draw circles and rectangles with rounded corners.
Definition: drawCircleHelper.c:27
int width
Definition: simplegfx.h:113
void drawNumber(screen_t *dev, float d, int r)
Prints a number to the screen starting at the cursor position. Output is limited to 64 bytes...
Definition: drawNumber.c:31
void drawRoundRect(screen_t *dev, int x, int y, int w, int h, int r, int color)
Draws a rectangle with rounded corners in the specified color.
Definition: drawRoundRect.c:27
char getDisplayRotation(screen_t *dev)
Returns the screen&#39;s orientation.
Definition: displayControl.c:44
void setBgColor(screen_t *dev, int color)
Sets the color of the font and the color of the background (highlighting) to be used. Setting the text color and text background to the same color will make the background color transparent.
Definition: setText.c:52
void drawCharLarge(screen_t *dev, unsigned char c)
Low-level helper function for printing single characters to the screen in the large (17x23) font...
Definition: drawChar.c:177
char deviceInterface
Definition: simplegfx.h:157
int(* deviceGetPixel)(struct screen_st *, int x, int y)
Definition: simplegfx.h:210
void drawPixel(screen_t *dev, int x, int y, int color)
Draws a single pixel in the specified color.
Definition: drawPixel.c:30
void drawBitmap(screen_t *dev, char *imgdir, int x, int y)
Draw an image (bitmap) at the specified (x,y) position. Requires an SD card to be mounted...
Definition: drawBitmap.c:30
char isAsleep(screen_t *dev)
Returns whether the screen is currently in a sleep mode or awake.
Definition: displayControl.c:87
int getCursorY(screen_t *dev)
Returns the current vertical position of the cursor, measured from the top of the screen in pixels...
void fillCircleHelper(screen_t *dev, int x0, int y0, int r, char cornername, int delta, int color)
Helper function used to draw filled circles and rectangles with rounded corners.
Definition: fillCircleHelper.c:29
int getDisplayHeight(screen_t *dev)
Returns the height of the display in pixels with screen rotation taken into account.
Definition: displayControl.c:56
void(* deviceScrollDisplay)(struct screen_st *, int h, int v)
Definition: simplegfx.h:244
unsigned char getScrollHorizontal(screen_t *dev)
Returns a value representing the status or speed of the display&#39;s horizontal scroll.
Definition: displayControl.c:91
void fillRoundRect(screen_t *dev, int x, int y, int w, int h, int r, int color)
Draws a filled rectangle with rounded corners in the specified color.
Definition: fillRoundRect.c:28
char rst_pin
Definition: simplegfx.h:170
int font[5]
Definition: simplegfx.h:128
void(* deviceFillRect)(struct screen_st *, int x, int y, int w, int h, int color)
Definition: simplegfx.h:222
void loadFonts(screen_t *dev, i2c *eeBus)
Internal function used to load the EEPROM addresses of the bitmapped fonts to the device&#39;s structure...
Definition: loadSetFonts.c:27
void(* deviceClearDisplay)(struct screen_st *)
Definition: simplegfx.h:229
void invertDisplay(screen_t *dev, char i)
Toggles the invert/normal display modes for the display. Depending on the display type it may only in...
Definition: displayControl.c:64
void setDisplayRotation(screen_t *dev, char r)
Sets the screen&#39;s orientation.
Definition: displayControl.c:40
void drawText(screen_t *dev, char *myString)
Prints a string of text to the screen starting at the cursor position. Output is limited to 64 bytes...
Definition: drawText.c:28
unsigned char dev_id
Definition: simplegfx.h:161
void(* deviceDrawFastVLine)(struct screen_st *, int x, int y, int w, int color)
Definition: simplegfx.h:219
char status_pin
Definition: simplegfx.h:164
void setTextFont(screen_t *dev, char f)
Sets the font face to be used. Range is from 0 to 3.
Definition: loadSetFonts.c:47
void(* deviceCopyRect)(struct screen_st *, int x, int y, int x0, int y0, int x1, int y1)
Definition: simplegfx.h:225
void(* deviceDrawFastHLine)(struct screen_st *, int x, int y, int w, int color)
Definition: simplegfx.h:216
void drawCopy(screen_t *dev, int x, int y, int w, int h, int x1, int y1)
Copies a rectagle and draws it at another location on the screen.
Definition: drawCopy.c:29
int text_color
Definition: simplegfx.h:140
void drawChar(screen_t *dev, char c)
Prints single ASCII-encoded characters to the screen. Characters 32 (space) to 126 (~) are rendered...
Definition: drawText.c:35
void setCursor(screen_t *dev, int x, int y, char size)
Sets the cursor position based on the size parameter.
Definition: setText.c:27
char clk_pin
Definition: simplegfx.h:173
char text_size
Definition: simplegfx.h:134
void fillRect(screen_t *dev, int x, int y, int w, int h, int color)
Draws a filled rectangle in the specified color.
Definition: fillRect.c:29
void clearDisplay(screen_t *dev)
Clears (sets to the native background color of the screen, usually black) the entire screen...
Definition: displayControl.c:28
int drawPrint(screen_t *dev, const char *fmt,...)
Print format "..." args to the screen. The output is limited to 128 bytes.
Definition: drawPrint.c:29
struct screen_st screen_t
void fillTriangle(screen_t *dev, int x0, int y0, int x1, int y1, int x2, int y2, int color)
Draws a filled triangle with in the specified color.
Definition: fillTriangle.c:27
char * customCharMed
Definition: simplegfx.h:190
void scrollDisplay(screen_t *dev, int h, int v)
Set the display&#39;s vertical or hrizontal scrolling functions. Not supported by all displays...
Definition: displayControl.c:76
int height
Definition: simplegfx.h:116
void setTextColor(screen_t *dev, int color)
Sets the color of the font and the color of the background (highlighting) to be used. Setting the text color and text background to the same color will make the background color transparent.
Definition: setText.c:48
char text_wrap
Definition: simplegfx.h:137
void updateDisplay(screen_t *dev)
Updates the display by writing the image buffer to it. Not necessary for screens that have their own ...
Definition: displayControl.c:32
char * image_ptr
Definition: simplegfx.h:183
void drawTriangle(screen_t *dev, int x0, int y0, int x1, int y1, int x2, int y2, int color)
Draws a triangle with in the specified color.
Definition: drawTriangle.c:27
void(* deviceResetDisplay)(struct screen_st *)
Definition: simplegfx.h:235
int getDisplayWidth(screen_t *dev)
Returns the width of the display in pixels with screen rotation taken into account.
Definition: displayControl.c:48
int cursor_x
Definition: simplegfx.h:122
char sdi_pin
Definition: simplegfx.h:167
i2c * eepromBus
Definition: simplegfx.h:150