Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <MI0283QT2.h>
- #include <ADS7846.h>
- #include <fonts.h>
- #include "lcdGUI.h"
- #ifdef BUTTON
- #ifndef lcdGUI_BUTTON
- #define lcdGUI_BUTTON
- void Gui::addButton(uint16_t _left, uint16_t _top, uint16_t _width, uint16_t _height, char * _caption, uint8_t _size,uint8_t _id)
- {
- Button * btn = (Button*)malloc(sizeof(Button));
- if(btn != NULL)
- {
- btn->next = NULL;
- btn->x = _left;
- btn->y = _top;
- btn->width = _width;
- btn->height = _height;
- btn->size = _size;
- if (strlen(_caption) == 0)
- btn->text = NULL;
- else
- {
- btn->text = (char *)malloc(strlen(_caption) + 1);
- if (btn->text != NULL)
- strcpy(btn->text, _caption);
- }
- btn->id = _id;
- if (btnPtr == NULL)
- btnPtr = btn;
- else
- lastBtnPtr->next = btn;
- lastBtnPtr = btn;
- }
- btn->draw(lcd, false);
- }
- #endif
- #endif
- #ifndef lcdGUI_cpp
- #define lcdGUI_cpp
- void buttonPressed(Button*);
- void buttonReleased(Button*);
- void sliderValueChanged(Slider*);
- Gui::Gui(lcdPtr _lcd, tpPtr _tp)
- {
- lcd = _lcd;
- tp = _tp;
- }
- void Gui::addTextbox(uint16_t X, uint16_t Y, uint16_t W, char * T, char * DFT, uint8_t S, uint8_t ID)
- {
- TextBox * txtBox = (TextBox*)malloc(sizeof(TextBox));
- if(txtBox != NULL)
- {
- txtBox->x = X;
- txtBox->y = Y;
- txtBox->width = W;
- txtBox->id = ID;
- txtBox->size = S;
- txtBox->next = NULL;
- }
- if(strlen(T) == 0)
- txtBox->text = NULL;
- else
- {
- txtBox->text = (char*)malloc(strlen(T)+1);
- if(txtBox->text != NULL)
- strcpy(txtBox->text, T);
- }
- if(strlen(DFT) == 0)
- txtBox->defaultText = NULL;
- else
- {
- txtBox->defaultText = (char*)malloc(strlen(DFT)+1);
- if(txtBox->defaultText != NULL)
- strcpy(txtBox->defaultText, DFT);
- }
- if(txtPtr == NULL)
- txtPtr = txtBox;
- else
- lastTxtPtr->next = txtBox;
- lastTxtPtr = txtBox;
- txtBox->draw(lcd, false);
- }
- void Gui::addSlider(uint16_t X, uint16_t Y, uint16_t W, uint16_t H, float V, uint8_t ID)
- {
- Slider * sld = (Slider*)malloc(sizeof(Slider));
- if(sld != NULL)
- {
- sld->x = X;
- sld->y = Y;
- sld->width = W;
- sld->height = H;
- sld->value = V;
- sld->id = ID;
- sld->next = NULL;
- }
- if(sldPtr == NULL)
- sldPtr = sld;
- else
- lastSldPtr->next = sld;
- lastSldPtr = sld;
- sld->draw(lcd, false);
- }
- void Gui::addStatusBar(char * T, uint8_t S)
- {
- if(statBarPtr == NULL)
- {
- StatusBar * stbr = (StatusBar*)malloc(sizeof(StatusBar));
- if(stbr != NULL)
- {
- if(strlen(T) == NULL)
- return;
- else
- {
- stbr->size = S;
- stbr->text = (char*)malloc(strlen(T)+1);
- if(stbr->text != NULL)
- strcpy(stbr->text, T);
- }
- }
- statBarPtr = stbr;
- stbr->draw(lcd);
- }
- }
- void Gui::addCheckbox(uint16_t X, uint16_t Y, char * T, bool A, uint8_t ID)
- {
- CheckBox * chk = (CheckBox*)malloc(sizeof(CheckBox));
- if(chk != NULL)
- {
- chk->x = X;
- chk->y = Y;
- chk->isActive = A;
- chk->next = NULL;
- chk->id = ID;
- if(strlen(T) == 0)
- chk->text = NULL;
- else
- {
- chk->text = (char*)malloc(strlen(T)+1);
- if(chk->text != NULL)
- strcpy(chk->text, T);
- }
- if(chkPtr == NULL)
- chkPtr = chk;
- else
- lastChkPtr->next = chk;
- lastChkPtr = chk;
- chk->draw(lcd, false);
- }
- }
- void Gui::service(bool draw)
- {
- if(draw)
- {
- lcd->clear(COLOR_BG);
- //draw statusbar
- if(statBarPtr != NULL)
- statBarPtr->draw(lcd);
- //draw buttons
- Button * tmpBtnPtr = btnPtr;
- while(tmpBtnPtr != NULL)
- {
- tmpBtnPtr->draw(lcd, false);
- tmpBtnPtr = tmpBtnPtr->next;
- }
- //draw textboxes
- TextBox * tmpTxtPtr = txtPtr;
- while(tmpTxtPtr != NULL)
- {
- tmpTxtPtr->draw(lcd, false);
- tmpTxtPtr = tmpTxtPtr->next;
- }
- //draw sliders
- Slider * tmpSldPtr = sldPtr;
- while(tmpSldPtr != NULL)
- {
- tmpSldPtr->draw(lcd, false);
- tmpSldPtr = tmpSldPtr->next;
- }
- //draw checkboxes
- CheckBox * tmpChkPtr = chkPtr;
- while(tmpChkPtr != NULL)
- {
- tmpChkPtr->draw(lcd, false);
- tmpChkPtr = tmpChkPtr->next;
- }
- }
- //check buttons
- tp->service();
- uint8_t currPress = tp->getPressure();
- if(currPress > MIN_PRESSURE)
- {
- //check buttons
- Button * btn = buttonFromPos(tp->getX(), tp->getY());
- if(lastBtnPressed != btn && lastBtnPressed != NULL)
- {
- lastBtnPressed->draw(lcd, false);
- if(lastBtnPressed == buttonFromPos(tp->getX(), tp->getY()))
- buttonReleased(lastBtnPressed);
- lastBtnPressed = NULL;
- }
- if(btn != NULL && btn != lastBtnPressed)
- {
- btn->draw(lcd, true);
- lastBtnPressed = btn;
- buttonPressed(btn);
- }
- //check textboxes
- TextBox * txt = textboxFromPos(tp->getX(), tp->getY());
- if(lastTxtPressed != txt && lastTxtPressed != NULL)
- {
- lastTxtPressed->draw(lcd, false);
- lastTxtPressed = NULL;
- }
- if(txt != NULL && txt != lastTxtPressed)
- {
- txt->draw(lcd, true);
- lastTxtPressed = txt;
- }
- //check sliders
- Slider * sld = sliderFromPos(tp->getX(), tp->getY());
- if(sld != NULL)
- {
- float oldVal = sld->value;
- sld->value = (float)(tp->getX()-sld->x)/sld->width*1024;
- if(oldVal != sld->value)
- sliderValueChanged(sld);
- }
- if(lastSldPressed != sld && lastSldPressed != NULL)
- {
- lastSldPressed->draw(lcd, false);
- lastSldPressed = NULL;
- }
- if(sld != NULL && sld != lastSldPressed)
- {
- sld->draw(lcd, true);
- lastSldPressed = sld;
- }
- if(sld != NULL && sld == lastSldPressed)
- sld->draw(lcd, true);
- //check checkboxes
- CheckBox * chk = checkboxFromPos(tp->getX(), tp->getY());
- if(lastChkPressed != chk && lastChkPressed != NULL)
- {
- lastChkPressed->draw(lcd, false);
- lastChkPressed = NULL;
- }
- if(chk != NULL && chk != lastChkPressed)
- {
- chk->isActive = !chk->isActive;
- chk->draw(lcd, true);
- lastChkPressed = chk;
- }
- }
- else
- {
- //deselect selected buttons
- if(lastBtnPressed != NULL)
- {
- buttonReleased(lastBtnPressed);
- lastBtnPressed->draw(lcd, false);
- lastBtnPressed = NULL;
- }
- //deselect selected sliders
- if(lastSldPressed != NULL)
- {
- lastSldPressed->draw(lcd, false);
- lastSldPressed = NULL;
- }
- if(lastChkPressed != NULL)
- {
- lastChkPressed->draw(lcd, false);
- lastChkPressed = NULL;
- }
- }
- }
- void Gui::service()
- {
- service(false);
- }
- void Gui::refresh()
- {
- service(true);
- }
- void Gui::deleteButton(Button * _button)
- {
- if (_button != NULL)
- {
- lcd->fillRect(_button->x, _button->y, _button->x + _button->width, _button->y + _button->height, COLOR_BG);
- Button * btn = btnPtr;
- Button * prev = NULL;
- while (btn != NULL)
- {
- if (btn == _button)
- {
- if (prev == NULL)
- btnPtr = btn->next;
- else
- prev->next = btn->next;
- if (btn->text != NULL)
- free(btn->text);
- free(btn);
- return;
- }
- prev = btn;
- btn = btn->next;
- }
- }
- }
- void Gui::deleteTextbox(TextBox * txt)
- {
- if (txt != NULL)
- {
- lcd->fillRect(txt->x, txt->y, txt->x + txt->width, txt->y + FONT_HEIGHT+4, COLOR_BG);
- TextBox * tmpTxtPtr = txtPtr;
- TextBox * prev = NULL;
- while (tmpTxtPtr != NULL)
- {
- if (tmpTxtPtr == txt)
- {
- if (prev == NULL)
- txtPtr = tmpTxtPtr->next;
- else
- prev->next = tmpTxtPtr->next;
- if (tmpTxtPtr->text != NULL)
- free(tmpTxtPtr->text);
- if(tmpTxtPtr->defaultText != NULL)
- free(tmpTxtPtr->defaultText);
- free(tmpTxtPtr);
- return;
- }
- prev = tmpTxtPtr;
- tmpTxtPtr = tmpTxtPtr->next;
- }
- }
- }
- void Gui::deleteSlider(Slider * sld)
- {
- if (sld != NULL)
- {
- lcd->fillRect(sld->x, sld->y, sld->x + sld->width, sld->y + sld->height, COLOR_BG);
- Slider * tmpSldPtr = sldPtr;
- Slider * prev = NULL;
- while (tmpSldPtr != NULL)
- {
- if (tmpSldPtr == sld)
- {
- if (prev == NULL)
- sldPtr = tmpSldPtr->next;
- else
- prev->next = tmpSldPtr->next;
- free(tmpSldPtr);
- return;
- }
- prev = tmpSldPtr;
- tmpSldPtr = tmpSldPtr->next;
- }
- }
- }
- void Gui::deleteStatusbar()
- {
- if(statBarPtr != NULL)
- {
- lcd->fillRect(0,0,lcd->getWidth(),statBarPtr->size*FONT_HEIGHT+4,COLOR_BG);
- free(statBarPtr->text);
- free(statBarPtr);
- statBarPtr = NULL;
- }
- }
- void Gui::deleteCheckbox(CheckBox * chk)
- {
- if (chk != NULL)
- {
- lcd->fillRect(chk->x, chk->y, chk->x + strlen(chk->text)*FONT_WIDTH+FONT_HEIGHT+10, chk->y + FONT_HEIGHT+4, COLOR_BG);
- CheckBox * tmpChkPtr = chkPtr;
- CheckBox * prev = NULL;
- while (tmpChkPtr != NULL)
- {
- if (tmpChkPtr == chk)
- {
- if (prev == NULL)
- chkPtr = tmpChkPtr->next;
- else
- prev->next = tmpChkPtr->next;
- if (tmpChkPtr->text != NULL)
- free(tmpChkPtr->text);
- free(tmpChkPtr);
- return;
- }
- prev = tmpChkPtr;
- tmpChkPtr = tmpChkPtr->next;
- }
- }
- }
- Button * Gui::buttonFromId(uint8_t _id)
- {
- Button * btn = btnPtr;
- while (btn != NULL)
- {
- if (btn->id == _id)
- return btn;
- btn = (Button*)btn->next;
- }
- return NULL;
- }
- TextBox * Gui::textboxFromId(uint8_t _id)
- {
- TextBox * txt = txtPtr;
- while (txt != NULL)
- {
- if (txt->id == _id)
- return txt;
- txt = (TextBox*)txt->next;
- }
- return NULL;
- }
- Slider * Gui::sliderFromId(uint8_t _id)
- {
- Slider * sld = sldPtr;
- while (sld != NULL)
- {
- if (sld->id == _id)
- return sld;
- sld = (Slider*)sld->next;
- }
- return NULL;
- }
- CheckBox * Gui::checkboxFromId(uint8_t _id)
- {
- CheckBox * chk = chkPtr;
- while (chk != NULL)
- {
- if (chk->id == _id)
- return chk;
- chk = (CheckBox*)chk->next;
- }
- return NULL;
- }
- Button * Gui::buttonFromPos(uint16_t _x, uint16_t _y)
- {
- Button * btn = (Button*)btnPtr;
- while (btn != NULL)
- {
- if ((_x >= btn->x) && (_x < btn->x + btn->width) &&
- (_y >= btn->y) && (_y < btn->y + btn->height))
- {
- return btn;
- }
- btn = (Button*)btn->next;
- }
- return NULL;
- }
- TextBox * Gui::textboxFromPos(uint16_t _x, uint16_t _y)
- {
- TextBox * txt = (TextBox*)txtPtr;
- while (txt != NULL)
- {
- uint16_t height = FONT_HEIGHT*txt->size+4;
- if ((_x >= txt->x) && (_x < txt->x + txt->width) &&
- (_y >= txt->y) && (_y < txt->y + height))
- {
- return txt;
- }
- txt = (TextBox*)txt->next;
- }
- return NULL;
- }
- Slider * Gui::sliderFromPos(uint16_t _x, uint16_t _y)
- {
- Slider * sld = sldPtr;
- while (sld != NULL)
- {
- if ((_x >= sld->x) && (_x < sld->x + sld->width) &&
- (_y >= sld->y) && (_y < sld->y + sld->height))
- {
- return sld;
- }
- sld = sld->next;
- }
- return NULL;
- }
- CheckBox * Gui::checkboxFromPos(uint16_t _x, uint16_t _y)
- {
- CheckBox * chk = chkPtr;
- while (chk != NULL)
- {
- if ((_x >= chk->x) && (_x < chk->x + strlen(chk->text)*FONT_WIDTH+FONT_HEIGHT+10) &&
- (_y >= chk->y) && (_y < chk->y + FONT_HEIGHT+2))
- {
- return chk;
- }
- chk = chk->next;
- }
- return NULL;
- }
- /*
- --------------------------------------------
- BUTTON
- --------------------------------------------
- */
- void Button::draw(lcdPtr lcd, bool isDown)
- {
- uint16_t clr = (isDown) ? COLOR_BUTTON_PRESSED : COLOR_BUTTON;
- uint16_t clrUpLine = (isDown) ? COLOR_BUTTON_PRESSED_UPPERLINE : COLOR_BUTTON_UPPERLINE;
- uint16_t clrLwLine = (isDown) ? COLOR_BUTTON_PRESSED_LOWERLINE : COLOR_BUTTON_LOWERLINE;
- lcd->fillRect(x, y+1, x + width, y + height-1, clr);
- lcd->drawLine(x+1,y,x+width-1,y,clrUpLine);
- lcd->drawLine(x+1,y+height,x+width-1,y+height,clrLwLine);
- lcd->drawText(x + (width-strlen(text)*FONT_WIDTH*size)/2, y + (height-FONT_HEIGHT*size)/2, text, size, COLOR_BUTTON_TEXT, clr);
- }
- /*
- --------------------------------------------
- TEXTBOX
- --------------------------------------------
- */
- void TextBox::draw(lcdPtr lcd, bool isDown)
- {
- uint16_t clr = (isDown) ? COLOR_TEXTBOX_PRESSED : COLOR_TEXTBOX;
- uint8_t height = FONT_HEIGHT*size+4;
- lcd->drawLine(x,y+height-3,x,y+height,clr);
- lcd->drawLine(x+width,y+height-3,x+width,y+height,clr);
- lcd->drawLine(x,y+height-1,x+width,y+height-1,clr);
- if(isDown)
- lcd->drawLine(x,y+height,x+width,y+height,clr);
- else
- lcd->drawLine(x,y+height,x+width,y+height,COLOR_BG);
- char * content = (text == NULL) ? defaultText : text;
- char * tmpTxt = "";
- for(int i=0; i<width/(FONT_WIDTH*size); i++)
- strcat(tmpTxt, " ");
- content = (isDown) ? tmpTxt : content;
- uint16_t clrTxt = (text == NULL) ? COLOR_TEXTBOX_TEXT_DEFAULT : COLOR_TEXTBOX_TEXT;
- if(strlen(content)*FONT_WIDTH*size < width)
- lcd->drawText(x+4, y+2, content, size, clrTxt, COLOR_BG);
- else
- {
- uint8_t i = 0;
- while(i*FONT_WIDTH*size < width - 4*FONT_WIDTH*size)
- {
- lcd->drawChar(x+4+i*FONT_WIDTH,y+2,content[i++],size,clrTxt,COLOR_BG);
- }
- lcd->drawText(x+4+i*FONT_WIDTH,y+2,"...",size,clrTxt,COLOR_BG);
- }
- }
- void TextBox::changeText(lcdPtr lcd, char * T)
- {
- if(T != NULL)
- {
- if(strlen(T) == 0)
- text = NULL;
- else
- {
- free(text);
- text = (char*)malloc(strlen(T)+1);
- if(text != NULL)
- strcpy(text, T);
- }
- draw(lcd, false);
- }
- }
- void TextBox::addText(lcdPtr lcd, char * T)
- {
- if(strlen(T) != NULL)
- {
- if(text != NULL)
- {
- char * tmpTxt = (char*)malloc(strlen(text)+strlen(T)+1);
- if(tmpTxt != NULL)
- {
- strcpy(tmpTxt, text);
- strcat(tmpTxt, T);
- free(text);
- text = tmpTxt;
- draw(lcd, false);
- }
- }
- }
- }
- /*
- --------------------------------------------
- SLIDER
- --------------------------------------------
- */
- void Slider::draw(lcdPtr lcd, bool isDown)
- {
- if(value>1023)
- value = 1023;
- if(value<0)
- value = 0;
- float valToX = (float)value/1024*width;
- lcd->fillRect(x,y+(height-2)/2,x+valToX,y+height/2,COLOR_SLIDER_VALUE);
- lcd->fillRect(x+valToX,y+(height-2)/2,x+width,y+height/2,COLOR_SLIDER);
- lcd->fillRect(x-10,y,x+width+10,y+(height-2)/2-1,COLOR_BG);
- lcd->fillRect(x-10,y+height/2+1,x+width+10,y+height,COLOR_BG);
- lcd->fillRect(x-10,y,x-1,y+height,COLOR_BG);
- lcd->fillRect(x+width+1,y,x+width+10,y+height,COLOR_BG);
- lcd->fillCircle(x+valToX,y+height/2,height/2,COLOR_SLIDER_CIRCLE);
- lcd->fillCircle(x+valToX,y+height/2,2,COLOR_SLIDER_VALUE);
- lcd->drawCircle(x+valToX,y+height/2,height/2,COLOR_SLIDER_VALUE);
- }
- /*
- --------------------------------------------
- STATUSBAR
- --------------------------------------------
- */
- void StatusBar::draw(lcdPtr lcd)
- {
- lcd->fillRect(0,0,lcd->getWidth(), size*FONT_HEIGHT+4,COLOR_STATUSBAR);
- lcd->drawText(2,2,text,size,COLOR_STATUSBAR_TEXT,COLOR_STATUSBAR);
- }
- /*
- --------------------------------------------
- CHECKBOX
- --------------------------------------------
- */
- void CheckBox::draw(lcdPtr lcd, bool isDown)
- {
- uint16_t clr = (isDown) ? COLOR_CHECK_PRESSED : COLOR_CHECK ;
- if(!isActive && !isDown)
- {
- lcd->fillRect(x,y,x+FONT_HEIGHT,y+FONT_HEIGHT,clr);
- lcd->fillRect(x+1,y+1,x+FONT_HEIGHT-1,y+FONT_HEIGHT-1,COLOR_BG);
- lcd->fillRect(x+FONT_HEIGHT,y,x+FONT_HEIGHT+2,y+FONT_HEIGHT,COLOR_BG);
- }
- lcd->drawRect(x,y,x+FONT_HEIGHT,y+FONT_HEIGHT,clr);
- lcd->drawText(x+FONT_HEIGHT+8,y+1,text,1,COLOR_CHECK_TEXT,COLOR_BG);
- if(isActive)
- {
- //line 1 (upper)
- lcd->drawLine(x+2,y+6,x+6,y+10,COLOR_CHECK_PRESSED);
- lcd->drawLine(x+7,y+10,x+FONT_HEIGHT+2,y+3,COLOR_CHECK_PRESSED);
- //line 2 (middle)
- lcd->drawLine(x+2,y+5,x+6,y+9,COLOR_CHECK_PRESSED);
- lcd->drawLine(x+7,y+9,x+FONT_HEIGHT+2,y+2,COLOR_CHECK_PRESSED);
- //line 3 (middle)
- lcd->drawLine(x+3,y+5,x+6,y+8,COLOR_CHECK_PRESSED);
- lcd->drawLine(x+7,y+8,x+FONT_HEIGHT+1,y+2,COLOR_CHECK_PRESSED);
- //line 4 (lower)
- lcd->drawLine(x+3,y+4,x+6,y+7,COLOR_CHECK_PRESSED);
- lcd->drawLine(x+7,y+7,x+FONT_HEIGHT+1,y+1,COLOR_CHECK_PRESSED);
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement