Class library for a Nextion graphics LCD to provide basic graphics and touch functions.

Dependents:   ProjectCardDisplay ProjectCardDisplay TUTORIA_GARCIA_ACOSTA

Committer:
grantphillips
Date:
Sun Feb 04 20:43:57 2018 +0000
Revision:
2:ea278c2b9437
Parent:
1:3b96605ca27a
Child:
3:72f3dadfab71
1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
grantphillips 0:ad3c413532ad 1 /* NextionLCD Library v1.0
grantphillips 0:ad3c413532ad 2 * Copyright (c) 2018 Grant Phillips
grantphillips 0:ad3c413532ad 3 * grant.phillips@mandela.ac.za
grantphillips 0:ad3c413532ad 4 *
grantphillips 0:ad3c413532ad 5 *
grantphillips 0:ad3c413532ad 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
grantphillips 0:ad3c413532ad 7 * of this software and associated documentation files (the "Software"), to deal
grantphillips 0:ad3c413532ad 8 * in the Software without restriction, including without limitation the rights
grantphillips 0:ad3c413532ad 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
grantphillips 0:ad3c413532ad 10 * copies of the Software, and to permit persons to whom the Software is
grantphillips 0:ad3c413532ad 11 * furnished to do so, subject to the following conditions:
grantphillips 0:ad3c413532ad 12 *
grantphillips 0:ad3c413532ad 13 * The above copyright notice and this permission notice shall be included in
grantphillips 0:ad3c413532ad 14 * all copies or substantial portions of the Software.
grantphillips 0:ad3c413532ad 15 *
grantphillips 0:ad3c413532ad 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
grantphillips 0:ad3c413532ad 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
grantphillips 0:ad3c413532ad 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
grantphillips 0:ad3c413532ad 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
grantphillips 0:ad3c413532ad 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
grantphillips 0:ad3c413532ad 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
grantphillips 0:ad3c413532ad 22 * THE SOFTWARE.
grantphillips 0:ad3c413532ad 23 */
grantphillips 0:ad3c413532ad 24
grantphillips 0:ad3c413532ad 25 #ifndef NEXTIONLCD_H
grantphillips 0:ad3c413532ad 26 #define NEXTIONLCD_H
grantphillips 0:ad3c413532ad 27
grantphillips 0:ad3c413532ad 28 #include "mbed.h"
grantphillips 0:ad3c413532ad 29
grantphillips 0:ad3c413532ad 30 #define BLACK 0
grantphillips 0:ad3c413532ad 31 #define BLUE 31
grantphillips 0:ad3c413532ad 32 #define BROWN 48192
grantphillips 0:ad3c413532ad 33 #define GREEN 2016
grantphillips 0:ad3c413532ad 34 #define YELLOW 65504
grantphillips 0:ad3c413532ad 35 #define RED 63488
grantphillips 0:ad3c413532ad 36 #define GRAY 33840
grantphillips 0:ad3c413532ad 37 #define WHITE 65535
grantphillips 0:ad3c413532ad 38
grantphillips 0:ad3c413532ad 39
grantphillips 1:3b96605ca27a 40 /** Class library for a Nextion graphics LCD to provide basic graphics and touch functions.
grantphillips 0:ad3c413532ad 41 * This library DOES NOT provide the HMI functionality that is included with Nextion LCDs.
grantphillips 0:ad3c413532ad 42 *
grantphillips 0:ad3c413532ad 43 * Example:
grantphillips 0:ad3c413532ad 44 * @code
grantphillips 0:ad3c413532ad 45 * #include "mbed.h"
grantphillips 2:ea278c2b9437 46 * #include "NextionLCD.h"
grantphillips 2:ea278c2b9437 47 *
grantphillips 2:ea278c2b9437 48 * NextionLCD lcd(PB_6, PB_7); //Tx, Rx
grantphillips 0:ad3c413532ad 49 *
grantphillips 2:ea278c2b9437 50 * int main()
grantphillips 2:ea278c2b9437 51 * {
grantphillips 2:ea278c2b9437 52 * char str[20];
grantphillips 2:ea278c2b9437 53 *
grantphillips 2:ea278c2b9437 54 * lcd.ClrScr(BLACK); //clear the display and fill it with BLACK pixels
grantphillips 2:ea278c2b9437 55 *
grantphillips 2:ea278c2b9437 56 * //draw some shapes
grantphillips 2:ea278c2b9437 57 * lcd.FillCircle(100,100,50,RED);
grantphillips 2:ea278c2b9437 58 * lcd.DrawPixel(100,100,WHITE);
grantphillips 2:ea278c2b9437 59 * lcd.DrawRectangle(50,50,100,100,BLUE);
grantphillips 2:ea278c2b9437 60 * lcd.DrawLine(50,50,100,150,YELLOW);
grantphillips 0:ad3c413532ad 61 *
grantphillips 2:ea278c2b9437 62 * while(1)
grantphillips 2:ea278c2b9437 63 * {
grantphillips 2:ea278c2b9437 64 * if (lcd.Touch()) //test if the lcd was touched
grantphillips 2:ea278c2b9437 65 * {
grantphillips 2:ea278c2b9437 66 * sprintf(str, "Touched: %d,%d", lcd.TouchX(), lcd.TouchY()); //print to string buffer
grantphillips 2:ea278c2b9437 67 * lcd.DrawString(0,0,200,30,0,GREEN,GRAY,0,0,str); //display string on LCD:
grantphillips 2:ea278c2b9437 68 * // x,y : 0,0
grantphillips 2:ea278c2b9437 69 * // width, height: 200, 300
grantphillips 2:ea278c2b9437 70 * // font : 0
grantphillips 2:ea278c2b9437 71 * // fontcolor : GREEN
grantphillips 2:ea278c2b9437 72 * // backcolor : GRAY
grantphillips 2:ea278c2b9437 73 * // x alignment : 0 (left)
grantphillips 2:ea278c2b9437 74 * // y alignment : 0 (top)
grantphillips 2:ea278c2b9437 75 * }
grantphillips 2:ea278c2b9437 76 * else
grantphillips 2:ea278c2b9437 77 * {
grantphillips 2:ea278c2b9437 78 * lcd.DrawString(0,0,200,30,0,BLACK,BLACK,0,0,""); //blank display area when not touched
grantphillips 2:ea278c2b9437 79 * }
grantphillips 2:ea278c2b9437 80 * }
grantphillips 0:ad3c413532ad 81 * }
grantphillips 0:ad3c413532ad 82 * @endcode
grantphillips 0:ad3c413532ad 83 */
grantphillips 0:ad3c413532ad 84
grantphillips 0:ad3c413532ad 85 class NextionLCD {
grantphillips 0:ad3c413532ad 86 public:
grantphillips 0:ad3c413532ad 87 /** Create a NextionLCD object for a graphics LCD connected to the specified pins.
grantphillips 0:ad3c413532ad 88 * @param Tx USART TX pin used to connect to Nextion LCD's RX pin
grantphillips 0:ad3c413532ad 89 * @param Rx USART RX pin used to connect to Nextion LCD's TX pin
grantphillips 0:ad3c413532ad 90 */
grantphillips 0:ad3c413532ad 91 NextionLCD(PinName Tx, PinName Rx);
grantphillips 0:ad3c413532ad 92
grantphillips 0:ad3c413532ad 93 /** Clears the lcd by filling it with the specified color pixels.
grantphillips 0:ad3c413532ad 94 * @param Color Integer value (0 to 65535) to represent the color to fill the screen with - represented in 16-bit 565 color format
grantphillips 0:ad3c413532ad 95 */
grantphillips 0:ad3c413532ad 96 void ClrScr(uint16_t color);
grantphillips 0:ad3c413532ad 97
grantphillips 0:ad3c413532ad 98 /** Draws a string on the lcd at the specified xy position.
grantphillips 0:ad3c413532ad 99 * @param x x position.
grantphillips 0:ad3c413532ad 100 * @param y y position.
grantphillips 0:ad3c413532ad 101 * @param w Width of string area.
grantphillips 0:ad3c413532ad 102 * @param h Height of string area.
grantphillips 0:ad3c413532ad 103 * @param font Font ID to use (0: 8x16; 1: 12x24; 2: 16x32).
grantphillips 0:ad3c413532ad 104 * @param fontcolor Color Integer value (0 to 65535) to represent the font color of the string - represented in 16-bit 565 color format.
grantphillips 0:ad3c413532ad 105 * @param backcolor Color Integer value (0 to 65535) to represent the background color of the string - represented in 16-bit 565 color format.
grantphillips 0:ad3c413532ad 106 * @param xcenter Horizontal alignment (0: left-aligned, 1: entered, 2: right-aligned).
grantphillips 0:ad3c413532ad 107 * @param ycenter Vertical alignment (0: upper-aligned, 1: entered, 2: lower-aligned).
grantphillips 0:ad3c413532ad 108 * @param str String content.
grantphillips 0:ad3c413532ad 109 */
grantphillips 0:ad3c413532ad 110 void DrawString(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t font, uint16_t fontcolor, uint16_t backcolor, uint8_t xcenter, uint8_t ycenter, char *str);
grantphillips 0:ad3c413532ad 111
grantphillips 0:ad3c413532ad 112 /** Draws a pixel on the lcd at the specified xy position.
grantphillips 0:ad3c413532ad 113 * @param x x position
grantphillips 0:ad3c413532ad 114 * @param y y position
grantphillips 0:ad3c413532ad 115 * @param color Color of the pixel (0 to 65535) - represented in 16-bit 565 color format.
grantphillips 0:ad3c413532ad 116 */
grantphillips 0:ad3c413532ad 117 void DrawPixel(uint16_t x, uint16_t y, uint16_t color);
grantphillips 0:ad3c413532ad 118
grantphillips 0:ad3c413532ad 119 /** Draws a line on the lcd from x1,y1 to x2,y2.
grantphillips 0:ad3c413532ad 120 * @param x1 x coordinate starting position
grantphillips 0:ad3c413532ad 121 * @param y1 y coordinate starting position
grantphillips 0:ad3c413532ad 122 * @param x2 x coordinate ending position
grantphillips 0:ad3c413532ad 123 * @param y2 y coordinate ending position
grantphillips 0:ad3c413532ad 124 * @param color Color of the line (0 to 65535) - represented in 16-bit 565 color format.
grantphillips 0:ad3c413532ad 125 */
grantphillips 0:ad3c413532ad 126 void DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);
grantphillips 0:ad3c413532ad 127
grantphillips 0:ad3c413532ad 128 /** Draws a rectangle on the lcd from x,y with width w and height h.
grantphillips 0:ad3c413532ad 129 * @param x x coordinate of top-left corner position
grantphillips 0:ad3c413532ad 130 * @param y y coordinate of top-left corner position
grantphillips 0:ad3c413532ad 131 * @param w Width of the rectangle
grantphillips 0:ad3c413532ad 132 * @param h Height of the rectangle
grantphillips 0:ad3c413532ad 133 * @param color Color of the rectangle (0 to 65535) - represented in 16-bit 565 color format.
grantphillips 0:ad3c413532ad 134 */
grantphillips 0:ad3c413532ad 135 void DrawRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color);
grantphillips 0:ad3c413532ad 136
grantphillips 0:ad3c413532ad 137 /** Draws a filled rectangle on the lcd from x,y with width w and height h.
grantphillips 0:ad3c413532ad 138 * @param x x coordinate of top-left corner position
grantphillips 0:ad3c413532ad 139 * @param y y coordinate of top-left corner position
grantphillips 0:ad3c413532ad 140 * @param w Width of the rectangle
grantphillips 0:ad3c413532ad 141 * @param h Height of the rectangle
grantphillips 0:ad3c413532ad 142 * @param color Color of the rectangle (0 to 65535) - represented in 16-bit 565 color format.
grantphillips 0:ad3c413532ad 143 */
grantphillips 0:ad3c413532ad 144 void FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color);
grantphillips 0:ad3c413532ad 145
grantphillips 0:ad3c413532ad 146 /** Draws a circle on the lcd at x,y with a radius of r.
grantphillips 0:ad3c413532ad 147 * @param x x coordinate position
grantphillips 0:ad3c413532ad 148 * @param y y coordinate position
grantphillips 0:ad3c413532ad 149 * @param r Radius of the circle
grantphillips 0:ad3c413532ad 150 * @param color Color of the circle (0 to 65535) - represented in 16-bit 565 color format.
grantphillips 0:ad3c413532ad 151 */
grantphillips 0:ad3c413532ad 152 void DrawCircle(uint16_t x, uint16_t y, uint16_t r, uint16_t color);
grantphillips 0:ad3c413532ad 153
grantphillips 0:ad3c413532ad 154 /** Draws a filled circle on the lcd at x,y with a radius of r.
grantphillips 0:ad3c413532ad 155 * @param x x coordinate position
grantphillips 0:ad3c413532ad 156 * @param y y coordinate position
grantphillips 0:ad3c413532ad 157 * @param r Radius of the circle
grantphillips 0:ad3c413532ad 158 * @param color Color of the circle (0 to 65535) - represented in 16-bit 565 color format.
grantphillips 0:ad3c413532ad 159 */
grantphillips 0:ad3c413532ad 160 void FillCircle(uint16_t x, uint16_t y, uint16_t r, uint16_t color);
grantphillips 0:ad3c413532ad 161
grantphillips 0:ad3c413532ad 162 /** Determines if the touchscreen is being touched or not.
grantphillips 0:ad3c413532ad 163 *
grantphillips 0:ad3c413532ad 164 * @retval true LCD is being touched.
grantphillips 0:ad3c413532ad 165 * @retval false LCD is not being touched.
grantphillips 0:ad3c413532ad 166 */
grantphillips 0:ad3c413532ad 167 bool Touch(void);
grantphillips 0:ad3c413532ad 168
grantphillips 0:ad3c413532ad 169 /** Get the X coordinate of the last touch on the touchscreen.
grantphillips 0:ad3c413532ad 170 *
grantphillips 0:ad3c413532ad 171 * @retval Integer indicating the X coordinate of the last touch.
grantphillips 0:ad3c413532ad 172 */
grantphillips 0:ad3c413532ad 173 int TouchX(void);
grantphillips 0:ad3c413532ad 174
grantphillips 0:ad3c413532ad 175 /** Get the Y coordinate of the last touch on the touchscreen.
grantphillips 0:ad3c413532ad 176 *
grantphillips 0:ad3c413532ad 177 * @retval Integer indicating the X coordinate of the last touch.
grantphillips 0:ad3c413532ad 178 */
grantphillips 0:ad3c413532ad 179 int TouchY(void);
grantphillips 0:ad3c413532ad 180
grantphillips 0:ad3c413532ad 181 private:
grantphillips 0:ad3c413532ad 182 RawSerial lcd; //Serial object for connecting to Nextion LCD
grantphillips 0:ad3c413532ad 183 //Serial pc;
grantphillips 0:ad3c413532ad 184 bool mTouch;
grantphillips 0:ad3c413532ad 185 int mTouchX, mTouchY;
grantphillips 0:ad3c413532ad 186 char mRxMsg[40];
grantphillips 0:ad3c413532ad 187 int mRxIdx;
grantphillips 0:ad3c413532ad 188 void RxInterrupt(void); //Rx Interrupt
grantphillips 0:ad3c413532ad 189 };
grantphillips 0:ad3c413532ad 190
grantphillips 0:ad3c413532ad 191 #endif