Automate écran capacitif
Dependencies: BSP_DISCO_F469NI LCD_DISCO_F469NI SeeedStudioTFTv2 TFT_fonts mbed
Fork of DISCO-F469NI_LCD_demo by
F469_GUI/SeekBar.cpp@2:2182df2d7810, 2017-03-31 (annotated)
- Committer:
- SquirrelGod
- Date:
- Fri Mar 31 15:36:26 2017 +0000
- Revision:
- 2:2182df2d7810
Programme ?cran capacitif
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SquirrelGod | 2:2182df2d7810 | 1 | //----------------------------------------------------------- |
SquirrelGod | 2:2182df2d7810 | 2 | // SeekBar class |
SquirrelGod | 2:2182df2d7810 | 3 | // |
SquirrelGod | 2:2182df2d7810 | 4 | // 2016/07/12, Copyright (c) 2016 MIKAMI, Naoki |
SquirrelGod | 2:2182df2d7810 | 5 | //----------------------------------------------------------- |
SquirrelGod | 2:2182df2d7810 | 6 | |
SquirrelGod | 2:2182df2d7810 | 7 | #include "SeekBar.hpp" |
SquirrelGod | 2:2182df2d7810 | 8 | |
SquirrelGod | 2:2182df2d7810 | 9 | namespace Mikami |
SquirrelGod | 2:2182df2d7810 | 10 | { |
SquirrelGod | 2:2182df2d7810 | 11 | // Constructor with scale value (only horizontal) |
SquirrelGod | 2:2182df2d7810 | 12 | SeekBar::SeekBar(uint16_t x, uint16_t y, uint16_t length, |
SquirrelGod | 2:2182df2d7810 | 13 | float min, float max, float initialValue, |
SquirrelGod | 2:2182df2d7810 | 14 | string left, string center, string right, |
SquirrelGod | 2:2182df2d7810 | 15 | uint32_t thumbColor, |
SquirrelGod | 2:2182df2d7810 | 16 | uint16_t thumbSize, uint16_t width, |
SquirrelGod | 2:2182df2d7810 | 17 | uint32_t colorL, uint32_t colorH, |
SquirrelGod | 2:2182df2d7810 | 18 | uint32_t backColor) |
SquirrelGod | 2:2182df2d7810 | 19 | : GuiBase(x, y, Font12, GuiBase::ENUM_TEXT, backColor, thumbColor), |
SquirrelGod | 2:2182df2d7810 | 20 | L_(length), W_(width), |
SquirrelGod | 2:2182df2d7810 | 21 | SIZE_(thumbSize), COLOR_L_(colorL), COLOR_H_(colorH), |
SquirrelGod | 2:2182df2d7810 | 22 | MIN_(min), MAX_(max), ORIENT_(Holizontal), v_(initialValue), |
SquirrelGod | 2:2182df2d7810 | 23 | labelOn_(true), slided_(false), active_(true) |
SquirrelGod | 2:2182df2d7810 | 24 | { |
SquirrelGod | 2:2182df2d7810 | 25 | Draw(initialValue); |
SquirrelGod | 2:2182df2d7810 | 26 | uint16_t y0 = y - thumbSize/2 - 13; |
SquirrelGod | 2:2182df2d7810 | 27 | labelLCR_[0] = new Label(x, y0, left, Label::CENTER); |
SquirrelGod | 2:2182df2d7810 | 28 | labelLCR_[1] = new Label(x+length/2, y0, center, Label::CENTER); |
SquirrelGod | 2:2182df2d7810 | 29 | labelLCR_[2] = new Label(x+length, y0, right, Label::CENTER); |
SquirrelGod | 2:2182df2d7810 | 30 | } |
SquirrelGod | 2:2182df2d7810 | 31 | |
SquirrelGod | 2:2182df2d7810 | 32 | SeekBar::~SeekBar() |
SquirrelGod | 2:2182df2d7810 | 33 | { |
SquirrelGod | 2:2182df2d7810 | 34 | if (labelOn_) |
SquirrelGod | 2:2182df2d7810 | 35 | for (int n=0; n<3; n++) delete labelLCR_[n]; |
SquirrelGod | 2:2182df2d7810 | 36 | } |
SquirrelGod | 2:2182df2d7810 | 37 | |
SquirrelGod | 2:2182df2d7810 | 38 | // Slide thumb |
SquirrelGod | 2:2182df2d7810 | 39 | // If the thumb is not touched, return false |
SquirrelGod | 2:2182df2d7810 | 40 | bool SeekBar::Slide() |
SquirrelGod | 2:2182df2d7810 | 41 | { |
SquirrelGod | 2:2182df2d7810 | 42 | if (!active_) return false; |
SquirrelGod | 2:2182df2d7810 | 43 | |
SquirrelGod | 2:2182df2d7810 | 44 | if (!PanelTouched()) |
SquirrelGod | 2:2182df2d7810 | 45 | { |
SquirrelGod | 2:2182df2d7810 | 46 | if (slided_) Draw(v_); |
SquirrelGod | 2:2182df2d7810 | 47 | slided_ = false; |
SquirrelGod | 2:2182df2d7810 | 48 | return false; |
SquirrelGod | 2:2182df2d7810 | 49 | } |
SquirrelGod | 2:2182df2d7810 | 50 | |
SquirrelGod | 2:2182df2d7810 | 51 | uint16_t x, y; |
SquirrelGod | 2:2182df2d7810 | 52 | bool rtn = IsOnThumb(x, y); |
SquirrelGod | 2:2182df2d7810 | 53 | if (rtn || slided_) |
SquirrelGod | 2:2182df2d7810 | 54 | { |
SquirrelGod | 2:2182df2d7810 | 55 | if (rtn) v_ = ToValue(Point(x, y)); |
SquirrelGod | 2:2182df2d7810 | 56 | Draw(v_, rtn); |
SquirrelGod | 2:2182df2d7810 | 57 | slided_ = rtn; |
SquirrelGod | 2:2182df2d7810 | 58 | } |
SquirrelGod | 2:2182df2d7810 | 59 | return rtn; |
SquirrelGod | 2:2182df2d7810 | 60 | } |
SquirrelGod | 2:2182df2d7810 | 61 | |
SquirrelGod | 2:2182df2d7810 | 62 | void SeekBar::Activate() |
SquirrelGod | 2:2182df2d7810 | 63 | { |
SquirrelGod | 2:2182df2d7810 | 64 | active_ = true; |
SquirrelGod | 2:2182df2d7810 | 65 | Draw(v_); |
SquirrelGod | 2:2182df2d7810 | 66 | if (labelOn_) |
SquirrelGod | 2:2182df2d7810 | 67 | for (int n=0; n<3; n++) labelLCR_[n]->Draw(TEXT_COLOR_); |
SquirrelGod | 2:2182df2d7810 | 68 | } |
SquirrelGod | 2:2182df2d7810 | 69 | |
SquirrelGod | 2:2182df2d7810 | 70 | void SeekBar::Inactivate() |
SquirrelGod | 2:2182df2d7810 | 71 | { |
SquirrelGod | 2:2182df2d7810 | 72 | active_ = false; |
SquirrelGod | 2:2182df2d7810 | 73 | Draw(v_); |
SquirrelGod | 2:2182df2d7810 | 74 | if (labelOn_) |
SquirrelGod | 2:2182df2d7810 | 75 | for (int n=0; n<3; n++) labelLCR_[n]->Draw(INACTIVE_TEXT_COLOR_); |
SquirrelGod | 2:2182df2d7810 | 76 | } |
SquirrelGod | 2:2182df2d7810 | 77 | |
SquirrelGod | 2:2182df2d7810 | 78 | // If touched position is on the thumb, return true |
SquirrelGod | 2:2182df2d7810 | 79 | bool SeekBar::IsOnThumb(uint16_t &x, uint16_t &y) |
SquirrelGod | 2:2182df2d7810 | 80 | { |
SquirrelGod | 2:2182df2d7810 | 81 | x = state_.touchX[0]; |
SquirrelGod | 2:2182df2d7810 | 82 | y = state_.touchY[0]; |
SquirrelGod | 2:2182df2d7810 | 83 | |
SquirrelGod | 2:2182df2d7810 | 84 | uint16_t th = SIZE_/2; |
SquirrelGod | 2:2182df2d7810 | 85 | Point pt = ToPoint(v_); |
SquirrelGod | 2:2182df2d7810 | 86 | if (ORIENT_ == Holizontal) |
SquirrelGod | 2:2182df2d7810 | 87 | { |
SquirrelGod | 2:2182df2d7810 | 88 | if ( (pt.x-th <= x) && (x <= pt.x+th) && |
SquirrelGod | 2:2182df2d7810 | 89 | (pt.y-th <= y) && (y <= pt.y+th) ) return true; |
SquirrelGod | 2:2182df2d7810 | 90 | } |
SquirrelGod | 2:2182df2d7810 | 91 | else |
SquirrelGod | 2:2182df2d7810 | 92 | { |
SquirrelGod | 2:2182df2d7810 | 93 | if ( (pt.x-th <= x) && (x <= pt.x+th) && |
SquirrelGod | 2:2182df2d7810 | 94 | (pt.y-th <= y) && (y <= pt.y+th) ) return true; |
SquirrelGod | 2:2182df2d7810 | 95 | } |
SquirrelGod | 2:2182df2d7810 | 96 | |
SquirrelGod | 2:2182df2d7810 | 97 | return false; |
SquirrelGod | 2:2182df2d7810 | 98 | } |
SquirrelGod | 2:2182df2d7810 | 99 | |
SquirrelGod | 2:2182df2d7810 | 100 | // Draw seekbar |
SquirrelGod | 2:2182df2d7810 | 101 | void SeekBar::Draw(float value, bool fill) |
SquirrelGod | 2:2182df2d7810 | 102 | { |
SquirrelGod | 2:2182df2d7810 | 103 | uint16_t sizeS = (uint16_t)(SIZE_*0.6f); |
SquirrelGod | 2:2182df2d7810 | 104 | // Erase previous seekbar |
SquirrelGod | 2:2182df2d7810 | 105 | lcd_.SetTextColor(BACK_COLOR_); |
SquirrelGod | 2:2182df2d7810 | 106 | if (ORIENT_ == Holizontal) |
SquirrelGod | 2:2182df2d7810 | 107 | lcd_.FillRect(X_-sizeS/2, Y_-SIZE_/2, L_+sizeS+1, SIZE_+1); |
SquirrelGod | 2:2182df2d7810 | 108 | else |
SquirrelGod | 2:2182df2d7810 | 109 | lcd_.FillRect(X_-SIZE_/2, Y_-sizeS/2, SIZE_+1, L_+sizeS+1); |
SquirrelGod | 2:2182df2d7810 | 110 | |
SquirrelGod | 2:2182df2d7810 | 111 | v_ = Saturate(value); // current value |
SquirrelGod | 2:2182df2d7810 | 112 | Point pt = ToPoint(v_); // Position of thumb |
SquirrelGod | 2:2182df2d7810 | 113 | |
SquirrelGod | 2:2182df2d7810 | 114 | // Draw upper line |
SquirrelGod | 2:2182df2d7810 | 115 | if (active_) lcd_.SetTextColor(COLOR_H_); |
SquirrelGod | 2:2182df2d7810 | 116 | else lcd_.SetTextColor(INACTIVE_TEXT_COLOR_-0x404040); |
SquirrelGod | 2:2182df2d7810 | 117 | if ((ORIENT_ == Holizontal) && ((X_+L_-pt.x) > 0)) |
SquirrelGod | 2:2182df2d7810 | 118 | lcd_.FillRect(pt.x, Y_-W_/4, X_+L_-pt.x, W_/2); |
SquirrelGod | 2:2182df2d7810 | 119 | if ((ORIENT_ == Vertical) && ((pt.y-Y_) > 0)) |
SquirrelGod | 2:2182df2d7810 | 120 | lcd_.FillRect(X_-W_/4, Y_, W_/2, pt.y-Y_); |
SquirrelGod | 2:2182df2d7810 | 121 | |
SquirrelGod | 2:2182df2d7810 | 122 | // Draw lower line |
SquirrelGod | 2:2182df2d7810 | 123 | if (active_) lcd_.SetTextColor(COLOR_L_); |
SquirrelGod | 2:2182df2d7810 | 124 | else lcd_.SetTextColor(INACTIVE_TEXT_COLOR_-0x202020); |
SquirrelGod | 2:2182df2d7810 | 125 | if ((ORIENT_ == Holizontal) && ((pt.x-X_) > 0)) |
SquirrelGod | 2:2182df2d7810 | 126 | lcd_.FillRect(X_, Y_-W_/2, pt.x-X_, W_); |
SquirrelGod | 2:2182df2d7810 | 127 | if ((ORIENT_ == Vertical) && ((Y_+L_-pt.y) > 0)) |
SquirrelGod | 2:2182df2d7810 | 128 | lcd_.FillRect(X_-W_/2, pt.y, W_, Y_+L_-pt.y); |
SquirrelGod | 2:2182df2d7810 | 129 | |
SquirrelGod | 2:2182df2d7810 | 130 | // Draw thumb |
SquirrelGod | 2:2182df2d7810 | 131 | if (active_) lcd_.SetTextColor(CREATED_COLOR_); |
SquirrelGod | 2:2182df2d7810 | 132 | else lcd_.SetTextColor(INACTIVE_TEXT_COLOR_); |
SquirrelGod | 2:2182df2d7810 | 133 | uint16_t width = SIZE_; |
SquirrelGod | 2:2182df2d7810 | 134 | uint16_t height = SIZE_; |
SquirrelGod | 2:2182df2d7810 | 135 | if (ORIENT_ == Holizontal) width = sizeS; |
SquirrelGod | 2:2182df2d7810 | 136 | else height = sizeS; |
SquirrelGod | 2:2182df2d7810 | 137 | uint16_t xPos = pt.x - width/2; |
SquirrelGod | 2:2182df2d7810 | 138 | uint16_t yPos = pt.y - height/2; |
SquirrelGod | 2:2182df2d7810 | 139 | |
SquirrelGod | 2:2182df2d7810 | 140 | if (fill) |
SquirrelGod | 2:2182df2d7810 | 141 | lcd_.FillRect(xPos, yPos, width+1, height+1); |
SquirrelGod | 2:2182df2d7810 | 142 | else |
SquirrelGod | 2:2182df2d7810 | 143 | { |
SquirrelGod | 2:2182df2d7810 | 144 | lcd_.DrawRect(xPos, yPos, width, height); |
SquirrelGod | 2:2182df2d7810 | 145 | lcd_.DrawHLine(pt.x+width/2, pt.y+height/2, 1); |
SquirrelGod | 2:2182df2d7810 | 146 | lcd_.DrawRect(xPos+1, yPos+1, width-2, height-2); |
SquirrelGod | 2:2182df2d7810 | 147 | lcd_.DrawHLine(pt.x+width/2-1, pt.y+height/2-1, 1); |
SquirrelGod | 2:2182df2d7810 | 148 | if (ORIENT_ == Holizontal) |
SquirrelGod | 2:2182df2d7810 | 149 | lcd_.DrawVLine(pt.x, yPos+4, SIZE_-7); |
SquirrelGod | 2:2182df2d7810 | 150 | else |
SquirrelGod | 2:2182df2d7810 | 151 | lcd_.DrawHLine(xPos+4, pt.y, SIZE_-7); |
SquirrelGod | 2:2182df2d7810 | 152 | } |
SquirrelGod | 2:2182df2d7810 | 153 | } |
SquirrelGod | 2:2182df2d7810 | 154 | |
SquirrelGod | 2:2182df2d7810 | 155 | void SeekBar::Redraw(bool fill) |
SquirrelGod | 2:2182df2d7810 | 156 | { |
SquirrelGod | 2:2182df2d7810 | 157 | Draw(GetValue(), fill); |
SquirrelGod | 2:2182df2d7810 | 158 | if (labelOn_) |
SquirrelGod | 2:2182df2d7810 | 159 | for (int n=0; n<3; n++) labelLCR_[n]->Draw(TEXT_COLOR_); |
SquirrelGod | 2:2182df2d7810 | 160 | } |
SquirrelGod | 2:2182df2d7810 | 161 | |
SquirrelGod | 2:2182df2d7810 | 162 | SeekBar::Point SeekBar::ToPoint(float value) |
SquirrelGod | 2:2182df2d7810 | 163 | { |
SquirrelGod | 2:2182df2d7810 | 164 | if (ORIENT_ == Holizontal) |
SquirrelGod | 2:2182df2d7810 | 165 | return Point(X_ + Round(L_*(value - MIN_)/(MAX_ - MIN_)), Y_); |
SquirrelGod | 2:2182df2d7810 | 166 | else |
SquirrelGod | 2:2182df2d7810 | 167 | return Point(X_, Y_ + L_ - Round(L_*(value - MIN_)/(MAX_ - MIN_))); |
SquirrelGod | 2:2182df2d7810 | 168 | } |
SquirrelGod | 2:2182df2d7810 | 169 | |
SquirrelGod | 2:2182df2d7810 | 170 | float SeekBar::ToValue(Point pt) |
SquirrelGod | 2:2182df2d7810 | 171 | { |
SquirrelGod | 2:2182df2d7810 | 172 | float value; |
SquirrelGod | 2:2182df2d7810 | 173 | if (ORIENT_ == Holizontal) |
SquirrelGod | 2:2182df2d7810 | 174 | value = (pt.x - X_)*(MAX_ - MIN_)/(float)L_ + MIN_; |
SquirrelGod | 2:2182df2d7810 | 175 | else |
SquirrelGod | 2:2182df2d7810 | 176 | value = -(pt.y - Y_ - L_)*(MAX_ - MIN_)/(float)L_ + MIN_; |
SquirrelGod | 2:2182df2d7810 | 177 | return Saturate(value); |
SquirrelGod | 2:2182df2d7810 | 178 | } |
SquirrelGod | 2:2182df2d7810 | 179 | |
SquirrelGod | 2:2182df2d7810 | 180 | float SeekBar::Saturate(float value) |
SquirrelGod | 2:2182df2d7810 | 181 | { |
SquirrelGod | 2:2182df2d7810 | 182 | if (value < MIN_) value = MIN_; |
SquirrelGod | 2:2182df2d7810 | 183 | if (value > MAX_) value = MAX_; |
SquirrelGod | 2:2182df2d7810 | 184 | return value; |
SquirrelGod | 2:2182df2d7810 | 185 | } |
SquirrelGod | 2:2182df2d7810 | 186 | } |
SquirrelGod | 2:2182df2d7810 | 187 |