Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ProjectCardDisplay ProjectCardDisplay TUTORIA_GARCIA_ACOSTA
NextionLCD.h@0:ad3c413532ad, 2018-02-04 (annotated)
- Committer:
- grantphillips
- Date:
- Sun Feb 04 20:36:03 2018 +0000
- Revision:
- 0:ad3c413532ad
- Child:
- 1:3b96605ca27a
Class library for a Nextion graphics LCD to provide basic graphics and touch functions.
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:ad3c413532ad | 40 | /** Class library for a Nextion LCD 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 | 0:ad3c413532ad | 46 | * #include "LM6029ACW.h" |
grantphillips | 0:ad3c413532ad | 47 | * |
grantphillips | 0:ad3c413532ad | 48 | * LM6029ACW lcd(PD_7, PE_2, PD_6, PD_3, PD_2, PC_15, PC_14, PC_13, PC_11, PC_9, PC_8, PC_6, PC_2); |
grantphillips | 0:ad3c413532ad | 49 | * // CS1 RES RS WR RD DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 |
grantphillips | 0:ad3c413532ad | 50 | * |
grantphillips | 0:ad3c413532ad | 51 | * DigitalOut backlight(PB_7); |
grantphillips | 0:ad3c413532ad | 52 | * |
grantphillips | 0:ad3c413532ad | 53 | * int main() { |
grantphillips | 0:ad3c413532ad | 54 | * backlight = 0; //turn on the lcd's backlight |
grantphillips | 0:ad3c413532ad | 55 | * while(1) { |
grantphillips | 0:ad3c413532ad | 56 | * lcd.GotoXY(4, 3); //move cursor to row, col |
grantphillips | 0:ad3c413532ad | 57 | * lcd.ClrScr(0); //clear all the pixels on the display |
grantphillips | 0:ad3c413532ad | 58 | * lcd.PutStr("Hello World!", 1); //print text in black pixels |
grantphillips | 0:ad3c413532ad | 59 | * wait(2.0); |
grantphillips | 0:ad3c413532ad | 60 | * |
grantphillips | 0:ad3c413532ad | 61 | * lcd.GotoXY(4, 3); //move cursor to row, col |
grantphillips | 0:ad3c413532ad | 62 | * lcd.ClrScr(1); //fill all the pixels on the display |
grantphillips | 0:ad3c413532ad | 63 | * lcd.PutStr("Hello World!", 0); //print text in clear pixels |
grantphillips | 0:ad3c413532ad | 64 | * wait(2.0); |
grantphillips | 0:ad3c413532ad | 65 | * } |
grantphillips | 0:ad3c413532ad | 66 | * } |
grantphillips | 0:ad3c413532ad | 67 | * @endcode |
grantphillips | 0:ad3c413532ad | 68 | */ |
grantphillips | 0:ad3c413532ad | 69 | |
grantphillips | 0:ad3c413532ad | 70 | class NextionLCD { |
grantphillips | 0:ad3c413532ad | 71 | public: |
grantphillips | 0:ad3c413532ad | 72 | /** Create a NextionLCD object for a graphics LCD connected to the specified pins. |
grantphillips | 0:ad3c413532ad | 73 | * @param Tx USART TX pin used to connect to Nextion LCD's RX pin |
grantphillips | 0:ad3c413532ad | 74 | * @param Rx USART RX pin used to connect to Nextion LCD's TX pin |
grantphillips | 0:ad3c413532ad | 75 | */ |
grantphillips | 0:ad3c413532ad | 76 | NextionLCD(PinName Tx, PinName Rx); |
grantphillips | 0:ad3c413532ad | 77 | |
grantphillips | 0:ad3c413532ad | 78 | /** Clears the lcd by filling it with the specified color pixels. |
grantphillips | 0:ad3c413532ad | 79 | * @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 | 80 | */ |
grantphillips | 0:ad3c413532ad | 81 | void ClrScr(uint16_t color); |
grantphillips | 0:ad3c413532ad | 82 | |
grantphillips | 0:ad3c413532ad | 83 | /** Draws a string on the lcd at the specified xy position. |
grantphillips | 0:ad3c413532ad | 84 | * @param x x position. |
grantphillips | 0:ad3c413532ad | 85 | * @param y y position. |
grantphillips | 0:ad3c413532ad | 86 | * @param w Width of string area. |
grantphillips | 0:ad3c413532ad | 87 | * @param h Height of string area. |
grantphillips | 0:ad3c413532ad | 88 | * @param font Font ID to use (0: 8x16; 1: 12x24; 2: 16x32). |
grantphillips | 0:ad3c413532ad | 89 | * @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 | 90 | * @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 | 91 | * @param xcenter Horizontal alignment (0: left-aligned, 1: entered, 2: right-aligned). |
grantphillips | 0:ad3c413532ad | 92 | * @param ycenter Vertical alignment (0: upper-aligned, 1: entered, 2: lower-aligned). |
grantphillips | 0:ad3c413532ad | 93 | * @param str String content. |
grantphillips | 0:ad3c413532ad | 94 | */ |
grantphillips | 0:ad3c413532ad | 95 | 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 | 96 | |
grantphillips | 0:ad3c413532ad | 97 | /** Draws a pixel on the lcd at the specified xy position. |
grantphillips | 0:ad3c413532ad | 98 | * @param x x position |
grantphillips | 0:ad3c413532ad | 99 | * @param y y position |
grantphillips | 0:ad3c413532ad | 100 | * @param color Color of the pixel (0 to 65535) - represented in 16-bit 565 color format. |
grantphillips | 0:ad3c413532ad | 101 | */ |
grantphillips | 0:ad3c413532ad | 102 | void DrawPixel(uint16_t x, uint16_t y, uint16_t color); |
grantphillips | 0:ad3c413532ad | 103 | |
grantphillips | 0:ad3c413532ad | 104 | /** Draws a line on the lcd from x1,y1 to x2,y2. |
grantphillips | 0:ad3c413532ad | 105 | * @param x1 x coordinate starting position |
grantphillips | 0:ad3c413532ad | 106 | * @param y1 y coordinate starting position |
grantphillips | 0:ad3c413532ad | 107 | * @param x2 x coordinate ending position |
grantphillips | 0:ad3c413532ad | 108 | * @param y2 y coordinate ending position |
grantphillips | 0:ad3c413532ad | 109 | * @param color Color of the line (0 to 65535) - represented in 16-bit 565 color format. |
grantphillips | 0:ad3c413532ad | 110 | */ |
grantphillips | 0:ad3c413532ad | 111 | void DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color); |
grantphillips | 0:ad3c413532ad | 112 | |
grantphillips | 0:ad3c413532ad | 113 | /** Draws a rectangle on the lcd from x,y with width w and height h. |
grantphillips | 0:ad3c413532ad | 114 | * @param x x coordinate of top-left corner position |
grantphillips | 0:ad3c413532ad | 115 | * @param y y coordinate of top-left corner position |
grantphillips | 0:ad3c413532ad | 116 | * @param w Width of the rectangle |
grantphillips | 0:ad3c413532ad | 117 | * @param h Height of the rectangle |
grantphillips | 0:ad3c413532ad | 118 | * @param color Color of the rectangle (0 to 65535) - represented in 16-bit 565 color format. |
grantphillips | 0:ad3c413532ad | 119 | */ |
grantphillips | 0:ad3c413532ad | 120 | void DrawRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color); |
grantphillips | 0:ad3c413532ad | 121 | |
grantphillips | 0:ad3c413532ad | 122 | /** Draws a filled rectangle on the lcd from x,y with width w and height h. |
grantphillips | 0:ad3c413532ad | 123 | * @param x x coordinate of top-left corner position |
grantphillips | 0:ad3c413532ad | 124 | * @param y y coordinate of top-left corner position |
grantphillips | 0:ad3c413532ad | 125 | * @param w Width of the rectangle |
grantphillips | 0:ad3c413532ad | 126 | * @param h Height of the rectangle |
grantphillips | 0:ad3c413532ad | 127 | * @param color Color of the rectangle (0 to 65535) - represented in 16-bit 565 color format. |
grantphillips | 0:ad3c413532ad | 128 | */ |
grantphillips | 0:ad3c413532ad | 129 | void FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color); |
grantphillips | 0:ad3c413532ad | 130 | |
grantphillips | 0:ad3c413532ad | 131 | /** Draws a circle on the lcd at x,y with a radius of r. |
grantphillips | 0:ad3c413532ad | 132 | * @param x x coordinate position |
grantphillips | 0:ad3c413532ad | 133 | * @param y y coordinate position |
grantphillips | 0:ad3c413532ad | 134 | * @param r Radius of the circle |
grantphillips | 0:ad3c413532ad | 135 | * @param color Color of the circle (0 to 65535) - represented in 16-bit 565 color format. |
grantphillips | 0:ad3c413532ad | 136 | */ |
grantphillips | 0:ad3c413532ad | 137 | void DrawCircle(uint16_t x, uint16_t y, uint16_t r, uint16_t color); |
grantphillips | 0:ad3c413532ad | 138 | |
grantphillips | 0:ad3c413532ad | 139 | /** Draws a filled circle on the lcd at x,y with a radius of r. |
grantphillips | 0:ad3c413532ad | 140 | * @param x x coordinate position |
grantphillips | 0:ad3c413532ad | 141 | * @param y y coordinate position |
grantphillips | 0:ad3c413532ad | 142 | * @param r Radius of the circle |
grantphillips | 0:ad3c413532ad | 143 | * @param color Color of the circle (0 to 65535) - represented in 16-bit 565 color format. |
grantphillips | 0:ad3c413532ad | 144 | */ |
grantphillips | 0:ad3c413532ad | 145 | void FillCircle(uint16_t x, uint16_t y, uint16_t r, uint16_t color); |
grantphillips | 0:ad3c413532ad | 146 | |
grantphillips | 0:ad3c413532ad | 147 | /** Determines if the touchscreen is being touched or not. |
grantphillips | 0:ad3c413532ad | 148 | * |
grantphillips | 0:ad3c413532ad | 149 | * @retval true LCD is being touched. |
grantphillips | 0:ad3c413532ad | 150 | * @retval false LCD is not being touched. |
grantphillips | 0:ad3c413532ad | 151 | */ |
grantphillips | 0:ad3c413532ad | 152 | bool Touch(void); |
grantphillips | 0:ad3c413532ad | 153 | |
grantphillips | 0:ad3c413532ad | 154 | /** Get the X coordinate of the last touch on the touchscreen. |
grantphillips | 0:ad3c413532ad | 155 | * |
grantphillips | 0:ad3c413532ad | 156 | * @retval Integer indicating the X coordinate of the last touch. |
grantphillips | 0:ad3c413532ad | 157 | */ |
grantphillips | 0:ad3c413532ad | 158 | int TouchX(void); |
grantphillips | 0:ad3c413532ad | 159 | |
grantphillips | 0:ad3c413532ad | 160 | /** Get the Y coordinate of the last touch on the touchscreen. |
grantphillips | 0:ad3c413532ad | 161 | * |
grantphillips | 0:ad3c413532ad | 162 | * @retval Integer indicating the X coordinate of the last touch. |
grantphillips | 0:ad3c413532ad | 163 | */ |
grantphillips | 0:ad3c413532ad | 164 | int TouchY(void); |
grantphillips | 0:ad3c413532ad | 165 | |
grantphillips | 0:ad3c413532ad | 166 | private: |
grantphillips | 0:ad3c413532ad | 167 | RawSerial lcd; //Serial object for connecting to Nextion LCD |
grantphillips | 0:ad3c413532ad | 168 | //Serial pc; |
grantphillips | 0:ad3c413532ad | 169 | bool mTouch; |
grantphillips | 0:ad3c413532ad | 170 | int mTouchX, mTouchY; |
grantphillips | 0:ad3c413532ad | 171 | char mRxMsg[40]; |
grantphillips | 0:ad3c413532ad | 172 | int mRxIdx; |
grantphillips | 0:ad3c413532ad | 173 | void RxInterrupt(void); //Rx Interrupt |
grantphillips | 0:ad3c413532ad | 174 | }; |
grantphillips | 0:ad3c413532ad | 175 | |
grantphillips | 0:ad3c413532ad | 176 | #endif |