DISCO-F469NI_Button_and_Slider_Library v1.0

Dependencies:   BSP_DISCO_F469NI LCD_DISCO_F469NI TS_DISCO_F469NI mbed

Committer:
wsteenberg
Date:
Tue Sep 25 12:38:48 2018 +0000
Revision:
0:ebf3f36f3a64
DISCO-F469NI_Button_and_Slider_Library v1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wsteenberg 0:ebf3f36f3a64 1 //-----------------------------------------------------------
wsteenberg 0:ebf3f36f3a64 2 //
wsteenberg 0:ebf3f36f3a64 3 // F469_BUTTON class
wsteenberg 0:ebf3f36f3a64 4 //
wsteenberg 0:ebf3f36f3a64 5 //-----------------------------------------------------------
wsteenberg 0:ebf3f36f3a64 6
wsteenberg 0:ebf3f36f3a64 7 #include "F469_BUTTON.hpp"
wsteenberg 0:ebf3f36f3a64 8
wsteenberg 0:ebf3f36f3a64 9 // Draw button
wsteenberg 0:ebf3f36f3a64 10 void Button::Render()
wsteenberg 0:ebf3f36f3a64 11 {
wsteenberg 0:ebf3f36f3a64 12 active_ = true;
wsteenberg 0:ebf3f36f3a64 13
wsteenberg 0:ebf3f36f3a64 14 switch(STYLE_)
wsteenberg 0:ebf3f36f3a64 15 {
wsteenberg 0:ebf3f36f3a64 16 case 1: // Style 1 - Normal button
wsteenberg 0:ebf3f36f3a64 17 lcd_.SetTextColor(BUTTON_COLOUR_); // Set Textcolour to original
wsteenberg 0:ebf3f36f3a64 18 lcd_.FillRect(ButtonX_, ButtonY_, ButtonW_, ButtonH_); // Draw button rectangle
wsteenberg 0:ebf3f36f3a64 19 break;
wsteenberg 0:ebf3f36f3a64 20
wsteenberg 0:ebf3f36f3a64 21 case 2: // Style 2 - 3D button
wsteenberg 0:ebf3f36f3a64 22 lcd_.SetTextColor(LCD_COLOR_DARKGRAY); // Set Textcolour to shadow colour
wsteenberg 0:ebf3f36f3a64 23 lcd_.FillRect(ButtonX_+1, ButtonY_+2, (ButtonW_+3), ButtonH_+3);// Draw shadow
wsteenberg 0:ebf3f36f3a64 24
wsteenberg 0:ebf3f36f3a64 25 lcd_.SetTextColor(BUTTON_COLOUR_); // Set Textcolour to original
wsteenberg 0:ebf3f36f3a64 26 lcd_.FillRect(ButtonX_, ButtonY_, ButtonW_, ButtonH_); // Draw button
wsteenberg 0:ebf3f36f3a64 27 break;
wsteenberg 0:ebf3f36f3a64 28 }
wsteenberg 0:ebf3f36f3a64 29 if (LABEL_.length() != 0) // If the label is larger than 0
wsteenberg 0:ebf3f36f3a64 30 {
wsteenberg 0:ebf3f36f3a64 31 lcd_.SetFont(FONTS_);
wsteenberg 0:ebf3f36f3a64 32 lcd_.SetBackColor(BUTTON_COLOUR_); // Set background colour
wsteenberg 0:ebf3f36f3a64 33 lcd_.SetTextColor(LCD_COLOR_WHITE); // Set textcolour
wsteenberg 0:ebf3f36f3a64 34
wsteenberg 0:ebf3f36f3a64 35 uint16_t x0 = ButtonX_ + (ButtonW_ - FONT_WIDTH_*(LABEL_.length()))/2; // Calcukate x value for Button name
wsteenberg 0:ebf3f36f3a64 36 uint16_t y0 = ButtonY_ + (ButtonH_ - FONT_HEIGHT_)/2 + 1; // Calcukate y value for Button name
wsteenberg 0:ebf3f36f3a64 37 lcd_.DisplayStringAt(x0, y0, (uint8_t *)LABEL_.c_str(),
wsteenberg 0:ebf3f36f3a64 38 LEFT_MODE); // Write button name
wsteenberg 0:ebf3f36f3a64 39 lcd_.SetBackColor(LCD_COLOR_WHITE); // Set back colour
wsteenberg 0:ebf3f36f3a64 40 }
wsteenberg 0:ebf3f36f3a64 41 }
wsteenberg 0:ebf3f36f3a64 42
wsteenberg 0:ebf3f36f3a64 43
wsteenberg 0:ebf3f36f3a64 44 void Button::Change() // Change colour of button when pressed
wsteenberg 0:ebf3f36f3a64 45 {
wsteenberg 0:ebf3f36f3a64 46 switch(STYLE_)
wsteenberg 0:ebf3f36f3a64 47 {
wsteenberg 0:ebf3f36f3a64 48 case 1: // Style 1 - Normal button
wsteenberg 0:ebf3f36f3a64 49 lcd_.SetTextColor(CHANGE_COLOUR_); // Set Textcolour to the change colour
wsteenberg 0:ebf3f36f3a64 50 lcd_.FillRect(ButtonX_, ButtonY_, ButtonW_, ButtonH_); // Draw button rectangle
wsteenberg 0:ebf3f36f3a64 51 break;
wsteenberg 0:ebf3f36f3a64 52
wsteenberg 0:ebf3f36f3a64 53 case 2: // Style 2 - 3D button
wsteenberg 0:ebf3f36f3a64 54 lcd_.SetTextColor(LCD_COLOR_WHITE); // Set Textcolour to background colour
wsteenberg 0:ebf3f36f3a64 55 lcd_.FillRect(ButtonX_, ButtonY_, (ButtonW_+3), ButtonH_+3); // Clears background
wsteenberg 0:ebf3f36f3a64 56
wsteenberg 0:ebf3f36f3a64 57 lcd_.SetTextColor(LCD_COLOR_GRAY); // Set Textcolour to shadow colour
wsteenberg 0:ebf3f36f3a64 58 lcd_.FillRect(ButtonX_+1, ButtonY_+2, (ButtonW_+3), ButtonH_+3);// Draws shadow
wsteenberg 0:ebf3f36f3a64 59
wsteenberg 0:ebf3f36f3a64 60 lcd_.SetTextColor(CHANGE_COLOUR_); // Indent button
wsteenberg 0:ebf3f36f3a64 61 lcd_.FillRect((ButtonX_+6), (ButtonY_+6), (ButtonW_-3), (ButtonH_-2)); // Draw indented button
wsteenberg 0:ebf3f36f3a64 62 break;
wsteenberg 0:ebf3f36f3a64 63 }
wsteenberg 0:ebf3f36f3a64 64
wsteenberg 0:ebf3f36f3a64 65 if (LABEL_.length() != 0)
wsteenberg 0:ebf3f36f3a64 66 {
wsteenberg 0:ebf3f36f3a64 67 lcd_.SetFont(FONTS_);
wsteenberg 0:ebf3f36f3a64 68 lcd_.SetBackColor(CHANGE_COLOUR_);
wsteenberg 0:ebf3f36f3a64 69 lcd_.SetTextColor(LCD_COLOR_BLACK);
wsteenberg 0:ebf3f36f3a64 70 uint16_t x0 = ButtonX_ + (ButtonW_ - FONT_WIDTH_*(LABEL_.length()))/2; // Calcukate x value for Button name
wsteenberg 0:ebf3f36f3a64 71 uint16_t y0 = ButtonY_ + (ButtonH_ - FONT_HEIGHT_)/2 + 1; // Calcukate y value for Button name
wsteenberg 0:ebf3f36f3a64 72 lcd_.DisplayStringAt(x0, y0, (uint8_t *)LABEL_.c_str(),
wsteenberg 0:ebf3f36f3a64 73 LEFT_MODE); // Write button name
wsteenberg 0:ebf3f36f3a64 74 lcd_.SetBackColor(LCD_COLOR_WHITE); // Set back colour
wsteenberg 0:ebf3f36f3a64 75 }
wsteenberg 0:ebf3f36f3a64 76 }
wsteenberg 0:ebf3f36f3a64 77
wsteenberg 0:ebf3f36f3a64 78
wsteenberg 0:ebf3f36f3a64 79
wsteenberg 0:ebf3f36f3a64 80 void Button::Hide() // Hide button
wsteenberg 0:ebf3f36f3a64 81 {
wsteenberg 0:ebf3f36f3a64 82 lcd_.SetTextColor(LCD_COLOR_WHITE); // Set textcolour to background colour
wsteenberg 0:ebf3f36f3a64 83 lcd_.FillRect(ButtonX_, ButtonY_, ButtonW_, ButtonH_); // Draw rectangle in background colour
wsteenberg 0:ebf3f36f3a64 84 active_ = false; // Set active boolean to false
wsteenberg 0:ebf3f36f3a64 85 }
wsteenberg 0:ebf3f36f3a64 86
wsteenberg 0:ebf3f36f3a64 87
wsteenberg 0:ebf3f36f3a64 88 bool Button::Press() // Check if touch detected
wsteenberg 0:ebf3f36f3a64 89 {
wsteenberg 0:ebf3f36f3a64 90 ts_.GetState(&state_);
wsteenberg 0:ebf3f36f3a64 91 if (!state_.touchDetected) return false;
wsteenberg 0:ebf3f36f3a64 92 if (!active_) return false;
wsteenberg 0:ebf3f36f3a64 93 if (!ButtonBoundaryCheck()) return false;
wsteenberg 0:ebf3f36f3a64 94 Change(); // Run the Change routine to change the colour of the button
wsteenberg 0:ebf3f36f3a64 95 wait(0.2); // Wait to allow the colour change to be visible
wsteenberg 0:ebf3f36f3a64 96 Render(); // Draw the original button
wsteenberg 0:ebf3f36f3a64 97 return true;
wsteenberg 0:ebf3f36f3a64 98 }
wsteenberg 0:ebf3f36f3a64 99
wsteenberg 0:ebf3f36f3a64 100 bool Button::ButtonBoundaryCheck() // Check if touch is within button boundaries
wsteenberg 0:ebf3f36f3a64 101 {
wsteenberg 0:ebf3f36f3a64 102 int nTouch = multiTouch ? state_.touchDetected : 1;
wsteenberg 0:ebf3f36f3a64 103 for (int n=0; n<nTouch; n++)
wsteenberg 0:ebf3f36f3a64 104 {
wsteenberg 0:ebf3f36f3a64 105 uint16_t x = state_.touchX[n]; // Assign touch x pos to x
wsteenberg 0:ebf3f36f3a64 106 uint16_t y = state_.touchY[n]; // Assign touch y pos to y
wsteenberg 0:ebf3f36f3a64 107
wsteenberg 0:ebf3f36f3a64 108 if ( (ButtonX_ <= x) && (x <= ButtonX_+ButtonW_) && // Check x value inside boundary
wsteenberg 0:ebf3f36f3a64 109 (ButtonY_ <= y) && (y <= ButtonY_+ButtonH_) ) return true; // Check y value inside boundary
wsteenberg 0:ebf3f36f3a64 110 }
wsteenberg 0:ebf3f36f3a64 111 return false;
wsteenberg 0:ebf3f36f3a64 112 }
wsteenberg 0:ebf3f36f3a64 113
wsteenberg 0:ebf3f36f3a64 114 TS_StateTypeDef Button::state_;
wsteenberg 0:ebf3f36f3a64 115 bool Button::multiTouch = false;
wsteenberg 0:ebf3f36f3a64 116