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:
0:ad3c413532ad
1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
grantphillips 0:ad3c413532ad 1 #include "NextionLCD.h"
grantphillips 0:ad3c413532ad 2 #include "mbed.h"
grantphillips 0:ad3c413532ad 3
grantphillips 0:ad3c413532ad 4
grantphillips 0:ad3c413532ad 5 /* ***************************************** Public Functions ***************************************** */
grantphillips 0:ad3c413532ad 6
grantphillips 0:ad3c413532ad 7 NextionLCD::NextionLCD(PinName Tx, PinName Rx) : lcd(Tx, Rx) {
grantphillips 0:ad3c413532ad 8 lcd.baud(9600);
grantphillips 0:ad3c413532ad 9 mTouch=false;
grantphillips 0:ad3c413532ad 10 mRxIdx=0;
grantphillips 0:ad3c413532ad 11 lcd.printf("rest%c%c%c", 0xff, 0xff, 0xff);
grantphillips 0:ad3c413532ad 12 lcd.printf("rest%c%c%c", 0xff, 0xff, 0xff);
grantphillips 0:ad3c413532ad 13 wait(1.0);
grantphillips 0:ad3c413532ad 14 lcd.printf("sendxy=1%c%c%c", 0xff, 0xff, 0xff);
grantphillips 0:ad3c413532ad 15 lcd.attach(this,&NextionLCD::RxInterrupt,Serial::RxIrq);
grantphillips 0:ad3c413532ad 16 }
grantphillips 0:ad3c413532ad 17
grantphillips 0:ad3c413532ad 18 void NextionLCD::ClrScr(uint16_t color) {
grantphillips 0:ad3c413532ad 19 lcd.printf("cls %d%c%c%c", color, 0xff, 0xff, 0xff);
grantphillips 0:ad3c413532ad 20 }
grantphillips 0:ad3c413532ad 21
grantphillips 0:ad3c413532ad 22 void NextionLCD::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 23 lcd.printf("xstr %d,%d,%d,%d,%d,%d,%d,%d,%d,1,\"%s\"%c%c%c", x, y, w, h, font, fontcolor, backcolor, xcenter, ycenter, str,0xff, 0xff, 0xff);
grantphillips 0:ad3c413532ad 24 }
grantphillips 0:ad3c413532ad 25
grantphillips 0:ad3c413532ad 26 void NextionLCD::DrawPixel(uint16_t x, uint16_t y, uint16_t color) {
grantphillips 0:ad3c413532ad 27 lcd.printf("fill %d,%d,1,1,%d%c%c%c", x, y, color, 0xff, 0xff, 0xff);
grantphillips 0:ad3c413532ad 28 }
grantphillips 0:ad3c413532ad 29
grantphillips 0:ad3c413532ad 30 void NextionLCD::DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) {
grantphillips 0:ad3c413532ad 31 lcd.printf("line %d,%d,%d,%d,%d%c%c%c", x1, y1, x2, y2, color, 0xff, 0xff, 0xff);
grantphillips 0:ad3c413532ad 32 }
grantphillips 0:ad3c413532ad 33
grantphillips 0:ad3c413532ad 34 void NextionLCD::DrawRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) {
grantphillips 0:ad3c413532ad 35 lcd.printf("draw %d,%d,%d,%d,%d%c%c%c", x, y, x+w, y+h, color, 0xff, 0xff, 0xff);
grantphillips 0:ad3c413532ad 36 }
grantphillips 0:ad3c413532ad 37
grantphillips 0:ad3c413532ad 38 void NextionLCD::FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) {
grantphillips 0:ad3c413532ad 39 lcd.printf("fill %d,%d,%d,%d,%d%c%c%c", x, y, w, h, color, 0xff, 0xff, 0xff);
grantphillips 0:ad3c413532ad 40 }
grantphillips 0:ad3c413532ad 41
grantphillips 0:ad3c413532ad 42 void NextionLCD::DrawCircle(uint16_t x, uint16_t y, uint16_t r, uint16_t color) {
grantphillips 0:ad3c413532ad 43 lcd.printf("cir %d,%d,%d,%d%c%c%c", x, y, r, color, 0xff, 0xff, 0xff);
grantphillips 0:ad3c413532ad 44 }
grantphillips 0:ad3c413532ad 45
grantphillips 0:ad3c413532ad 46 void NextionLCD::FillCircle(uint16_t x, uint16_t y, uint16_t r, uint16_t color) {
grantphillips 0:ad3c413532ad 47 lcd.printf("cirs %d,%d,%d,%d%c%c%c", x, y, r, color, 0xff, 0xff, 0xff);
grantphillips 0:ad3c413532ad 48 }
grantphillips 0:ad3c413532ad 49
grantphillips 0:ad3c413532ad 50 bool NextionLCD::Touch(void) {
grantphillips 0:ad3c413532ad 51 return mTouch;
grantphillips 0:ad3c413532ad 52 }
grantphillips 0:ad3c413532ad 53
grantphillips 0:ad3c413532ad 54 int NextionLCD::TouchX(void) {
grantphillips 0:ad3c413532ad 55 return mTouchX;
grantphillips 0:ad3c413532ad 56 }
grantphillips 0:ad3c413532ad 57
grantphillips 0:ad3c413532ad 58 int NextionLCD::TouchY(void) {
grantphillips 0:ad3c413532ad 59 return mTouchY;
grantphillips 0:ad3c413532ad 60 }
grantphillips 0:ad3c413532ad 61
grantphillips 0:ad3c413532ad 62
grantphillips 0:ad3c413532ad 63
grantphillips 0:ad3c413532ad 64 /* ***************************************** Private Functions ***************************************** */
grantphillips 0:ad3c413532ad 65
grantphillips 0:ad3c413532ad 66
grantphillips 0:ad3c413532ad 67
grantphillips 0:ad3c413532ad 68
grantphillips 0:ad3c413532ad 69 /************************* Private Callback Functions *************************/
grantphillips 0:ad3c413532ad 70 void NextionLCD::RxInterrupt(void) {
grantphillips 0:ad3c413532ad 71 char c;
grantphillips 0:ad3c413532ad 72
grantphillips 0:ad3c413532ad 73 if(lcd.readable()) {
grantphillips 0:ad3c413532ad 74 c = lcd.getc();
grantphillips 0:ad3c413532ad 75 mRxMsg[mRxIdx] = c;
grantphillips 0:ad3c413532ad 76 mRxIdx++;
grantphillips 0:ad3c413532ad 77 if ((mRxIdx >= 3) && (mRxMsg[mRxIdx-1] == 0xff) && (mRxMsg[mRxIdx-2] == 0xff) && (mRxMsg[mRxIdx-3] == 0xff)) { //valid rx message
grantphillips 0:ad3c413532ad 78 mRxIdx=0;
grantphillips 0:ad3c413532ad 79 if(mRxMsg[0] == 0x67) { //Touch coordinate data return
grantphillips 0:ad3c413532ad 80 mTouchX = (mRxMsg[1]*256) + mRxMsg[2];
grantphillips 0:ad3c413532ad 81 mTouchY = (mRxMsg[3]*256) + mRxMsg[4];
grantphillips 0:ad3c413532ad 82 if(mRxMsg[5] == 0x01)
grantphillips 0:ad3c413532ad 83 mTouch = true;
grantphillips 0:ad3c413532ad 84 else
grantphillips 0:ad3c413532ad 85 mTouch = false;
grantphillips 0:ad3c413532ad 86 }
grantphillips 0:ad3c413532ad 87 }
grantphillips 0:ad3c413532ad 88 }
grantphillips 0:ad3c413532ad 89 }