Simple version for outreach using https://os.mbed.com/users/wsteenberg/code/

Dependencies:   mbed LCD_DISCO_F469NI TS_DISCO_F469NI BSP_DISCO_F469NI

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_SLIDER class
wsteenberg 0:ebf3f36f3a64 4 //
wsteenberg 0:ebf3f36f3a64 5 //-----------------------------------------------------------
wsteenberg 0:ebf3f36f3a64 6
wsteenberg 0:ebf3f36f3a64 7 #include "F469_SLIDER.hpp"
wsteenberg 0:ebf3f36f3a64 8
wsteenberg 0:ebf3f36f3a64 9 extern uint16_t SliderDisplayValue_;
wsteenberg 0:ebf3f36f3a64 10
wsteenberg 0:ebf3f36f3a64 11 // Draw Slider
wsteenberg 0:ebf3f36f3a64 12 void Slider::Render()
wsteenberg 0:ebf3f36f3a64 13 {
wsteenberg 0:ebf3f36f3a64 14 active_ = true;
wsteenberg 0:ebf3f36f3a64 15 char min_[3]; //string Slider minimum;
wsteenberg 0:ebf3f36f3a64 16 char max_[3]; //string Slider maximum;
wsteenberg 0:ebf3f36f3a64 17
wsteenberg 0:ebf3f36f3a64 18 switch(STYLE_)
wsteenberg 0:ebf3f36f3a64 19 {
wsteenberg 0:ebf3f36f3a64 20 case 1: // Style 1 - Normal Slider
wsteenberg 0:ebf3f36f3a64 21 lcd_.SetTextColor(SLIDER_COLOUR_); // Slider rectangle
wsteenberg 0:ebf3f36f3a64 22 lcd_.FillRect(SliderX_, SliderY_, SliderW_, 40);
wsteenberg 0:ebf3f36f3a64 23 lcd_.SetTextColor(LCD_COLOR_GRAY); // Slider Control rectangle
wsteenberg 0:ebf3f36f3a64 24 uint16_t xC = (SliderW_/(Max_-Min_)* valueControl_); // Calculate initial value of Slider control
wsteenberg 0:ebf3f36f3a64 25 lcd_.FillRect(SliderX_+xC-20, SliderY_, 40, 40);
wsteenberg 0:ebf3f36f3a64 26 break;
wsteenberg 0:ebf3f36f3a64 27
wsteenberg 0:ebf3f36f3a64 28 case 2: // Style 2 - 3D Slider
wsteenberg 0:ebf3f36f3a64 29 lcd_.SetTextColor(LCD_COLOR_DARKGRAY); // Set Textcolour to shadow colour
wsteenberg 0:ebf3f36f3a64 30 lcd_.FillRect(SliderX_+2, SliderY_+2, SliderW_+3, 40+3); // Draws shadow
wsteenberg 0:ebf3f36f3a64 31 lcd_.SetTextColor(SLIDER_COLOUR_); // Slider rectangle
wsteenberg 0:ebf3f36f3a64 32 lcd_.FillRect(SliderX_, SliderY_, SliderW_, 40);
wsteenberg 0:ebf3f36f3a64 33 lcd_.SetTextColor(LCD_COLOR_GRAY); // Slider Control rectangle
wsteenberg 0:ebf3f36f3a64 34 xC = (SliderW_/(Max_-Min_)* valueControl_); // Calculate initial value of Slider control
wsteenberg 0:ebf3f36f3a64 35 lcd_.FillRect(SliderX_+xC-20, SliderY_, 40, 40);
wsteenberg 0:ebf3f36f3a64 36 break;
wsteenberg 0:ebf3f36f3a64 37 }
wsteenberg 0:ebf3f36f3a64 38
wsteenberg 0:ebf3f36f3a64 39 if (LABEL_.length() != 0)
wsteenberg 0:ebf3f36f3a64 40 {
wsteenberg 0:ebf3f36f3a64 41 lcd_.SetFont(FONTS_);
wsteenberg 0:ebf3f36f3a64 42 lcd_.SetBackColor(LCD_COLOR_WHITE);
wsteenberg 0:ebf3f36f3a64 43 lcd_.SetTextColor(LCD_COLOR_BLACK);
wsteenberg 0:ebf3f36f3a64 44
wsteenberg 0:ebf3f36f3a64 45 sprintf(min_, "%2d", (int) Min_); // Convert minimum value to string
wsteenberg 0:ebf3f36f3a64 46 lcd_.DisplayStringAt(SliderX_, SliderY_+50, (uint8_t *)min_, LEFT_MODE); // Display Slider minimum value
wsteenberg 0:ebf3f36f3a64 47
wsteenberg 0:ebf3f36f3a64 48 sprintf(max_, "%2d", (int) Max_); // Convert maximum value to string
wsteenberg 0:ebf3f36f3a64 49 lcd_.DisplayStringAt((SliderX_+SliderW_-40), (SliderY_+50), (uint8_t *)max_, LEFT_MODE); // Display Slider minimum value
wsteenberg 0:ebf3f36f3a64 50
wsteenberg 0:ebf3f36f3a64 51 uint16_t x0 = SliderX_ + (SliderW_ - FONT_WIDTH_*(LABEL_.length()))/2; // Calcukate middle x value for Slider name
wsteenberg 0:ebf3f36f3a64 52 uint16_t y0 = SliderY_ + (SliderH_ - FONT_HEIGHT_)/2 + 1; // Calcukate y value for Slider name
wsteenberg 0:ebf3f36f3a64 53 lcd_.DisplayStringAt(x0, y0-50, (uint8_t *)LABEL_.c_str(),
wsteenberg 0:ebf3f36f3a64 54 LEFT_MODE); // Write Slider name
wsteenberg 0:ebf3f36f3a64 55 }
wsteenberg 0:ebf3f36f3a64 56 }
wsteenberg 0:ebf3f36f3a64 57
wsteenberg 0:ebf3f36f3a64 58
wsteenberg 0:ebf3f36f3a64 59 // Service Slider
wsteenberg 0:ebf3f36f3a64 60 void Slider::Service()
wsteenberg 0:ebf3f36f3a64 61 {
wsteenberg 0:ebf3f36f3a64 62 uint16_t xC_; // X value of Control rectangle
wsteenberg 0:ebf3f36f3a64 63 uint16_t valueControl_; // Value of Slider control to display
wsteenberg 0:ebf3f36f3a64 64
wsteenberg 0:ebf3f36f3a64 65 switch(STYLE_)
wsteenberg 0:ebf3f36f3a64 66 {
wsteenberg 0:ebf3f36f3a64 67 case 1: // Style 1 - Normal button
wsteenberg 0:ebf3f36f3a64 68 lcd_.SetTextColor(SLIDER_COLOUR_); // Draw Slider rectangle
wsteenberg 0:ebf3f36f3a64 69 lcd_.FillRect(SliderX_, SliderY_, SliderW_, 40);
wsteenberg 0:ebf3f36f3a64 70 break;
wsteenberg 0:ebf3f36f3a64 71
wsteenberg 0:ebf3f36f3a64 72 case 2: // Style 2 - 3D button
wsteenberg 0:ebf3f36f3a64 73 lcd_.SetTextColor(LCD_COLOR_DARKGRAY); // Set Textcolour to shadow colour
wsteenberg 0:ebf3f36f3a64 74 lcd_.FillRect(SliderX_+2, SliderY_+2, SliderW_+3, 40+3);// Draws shadow
wsteenberg 0:ebf3f36f3a64 75 lcd_.SetTextColor(SLIDER_COLOUR_); // Slider rectangle
wsteenberg 0:ebf3f36f3a64 76 lcd_.FillRect(SliderX_, SliderY_, SliderW_, 40);
wsteenberg 0:ebf3f36f3a64 77 lcd_.SetTextColor(LCD_COLOR_GRAY); // Slider Control rectangle
wsteenberg 0:ebf3f36f3a64 78 break;
wsteenberg 0:ebf3f36f3a64 79 }
wsteenberg 0:ebf3f36f3a64 80
wsteenberg 0:ebf3f36f3a64 81 uint16_t newX = state_.touchX[0]; // Find new x value of touch
wsteenberg 0:ebf3f36f3a64 82
wsteenberg 0:ebf3f36f3a64 83 if ((newX >= SliderX_) and (newX <= SliderX_+ 40)) // if touch at beginning of Slider
wsteenberg 0:ebf3f36f3a64 84 {
wsteenberg 0:ebf3f36f3a64 85 { xC_ = SliderX_; } // Sets x value of Slider control to left x value defined for slider rectangle
wsteenberg 0:ebf3f36f3a64 86 }
wsteenberg 0:ebf3f36f3a64 87
wsteenberg 0:ebf3f36f3a64 88 else if ((newX >= SliderX_+SliderW_-40) and (newX <= SliderX_+ SliderW_)) // if touch at the end of Slider
wsteenberg 0:ebf3f36f3a64 89 {
wsteenberg 0:ebf3f36f3a64 90 xC_ = SliderX_+SliderW_-40; // Sets x value of Slider control to right x value defined for slider rectangle - width of slider control
wsteenberg 0:ebf3f36f3a64 91 }
wsteenberg 0:ebf3f36f3a64 92
wsteenberg 0:ebf3f36f3a64 93 else xC_ = newX - 20; // if touch inside Slider
wsteenberg 0:ebf3f36f3a64 94
wsteenberg 0:ebf3f36f3a64 95 lcd_.SetTextColor(LCD_COLOR_YELLOW); // Colour to change the control of the Slider when touched
wsteenberg 0:ebf3f36f3a64 96 lcd_.FillRect(xC_, SliderY_, 40, 40);
wsteenberg 0:ebf3f36f3a64 97
wsteenberg 0:ebf3f36f3a64 98 valueControl_ = (newX - SliderX_) * (Max_ - Min_) / SliderW_ + Min_; // Value to display of the control of the Slider
wsteenberg 0:ebf3f36f3a64 99 if (valueControl_== (Min_+1))valueControl_= Min_; // Set value value to display to minimum
wsteenberg 0:ebf3f36f3a64 100 if (valueControl_== (Max_-1))valueControl_= Max_; // Set value value to display to maximum
wsteenberg 0:ebf3f36f3a64 101
wsteenberg 0:ebf3f36f3a64 102
wsteenberg 0:ebf3f36f3a64 103 lcd_.SetTextColor(LCD_COLOR_BLACK);
wsteenberg 0:ebf3f36f3a64 104 SliderDisplayValue_ = valueControl_;
wsteenberg 0:ebf3f36f3a64 105 lcd_.SetTextColor(LCD_COLOR_YELLOW);
wsteenberg 0:ebf3f36f3a64 106 }
wsteenberg 0:ebf3f36f3a64 107
wsteenberg 0:ebf3f36f3a64 108
wsteenberg 0:ebf3f36f3a64 109
wsteenberg 0:ebf3f36f3a64 110 bool Slider::Moved() // Check touch detected
wsteenberg 0:ebf3f36f3a64 111 {
wsteenberg 0:ebf3f36f3a64 112 ts_.GetState(&state_); // Get state of touch
wsteenberg 0:ebf3f36f3a64 113 if (!state_.touchDetected) return false;
wsteenberg 0:ebf3f36f3a64 114 if (!active_) return false;
wsteenberg 0:ebf3f36f3a64 115 if (!SliderBoundaryCheck()) return false;
wsteenberg 0:ebf3f36f3a64 116 Service(); // Run Service routine
wsteenberg 0:ebf3f36f3a64 117 return SliderBoundaryCheck();
wsteenberg 0:ebf3f36f3a64 118 }
wsteenberg 0:ebf3f36f3a64 119
wsteenberg 0:ebf3f36f3a64 120
wsteenberg 0:ebf3f36f3a64 121 bool Slider::SliderBoundaryCheck() // Check if touch is within Slider boundaries
wsteenberg 0:ebf3f36f3a64 122 {
wsteenberg 0:ebf3f36f3a64 123 int nTouch = multiTouch ? state_.touchDetected : 1;
wsteenberg 0:ebf3f36f3a64 124 for (int n=0; n<nTouch; n++)
wsteenberg 0:ebf3f36f3a64 125 {
wsteenberg 0:ebf3f36f3a64 126 uint16_t x = state_.touchX[n]; // Find x value of touch
wsteenberg 0:ebf3f36f3a64 127 uint16_t y = state_.touchY[n]; // Find y value of touch
wsteenberg 0:ebf3f36f3a64 128
wsteenberg 0:ebf3f36f3a64 129 if ( (SliderX_ <= x) && (x <= SliderX_+SliderW_) &&
wsteenberg 0:ebf3f36f3a64 130 (SliderY_ <= y) && (y <= SliderY_+SliderH_) ) return true; // Check if touch is within Slider defined values
wsteenberg 0:ebf3f36f3a64 131 }
wsteenberg 0:ebf3f36f3a64 132 return false;
wsteenberg 0:ebf3f36f3a64 133 }
wsteenberg 0:ebf3f36f3a64 134
wsteenberg 0:ebf3f36f3a64 135 TS_StateTypeDef Slider::state_;
wsteenberg 0:ebf3f36f3a64 136 bool Slider::multiTouch = false; // Disable multitouch for Slider
wsteenberg 0:ebf3f36f3a64 137