Automate écran capacitif
Dependencies: BSP_DISCO_F469NI LCD_DISCO_F469NI SeeedStudioTFTv2 TFT_fonts mbed
Fork of DISCO-F469NI_LCD_demo by
F469_GUI/Button.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 | // Button class handling multi-touch |
SquirrelGod | 2:2182df2d7810 | 3 | // Multi-touch: Enabled (default) |
SquirrelGod | 2:2182df2d7810 | 4 | // |
SquirrelGod | 2:2182df2d7810 | 5 | // 2016/04/08, Copyright (c) 2016 MIKAMI, Naoki |
SquirrelGod | 2:2182df2d7810 | 6 | //----------------------------------------------------------- |
SquirrelGod | 2:2182df2d7810 | 7 | |
SquirrelGod | 2:2182df2d7810 | 8 | #include "Button.hpp" |
SquirrelGod | 2:2182df2d7810 | 9 | |
SquirrelGod | 2:2182df2d7810 | 10 | namespace Mikami |
SquirrelGod | 2:2182df2d7810 | 11 | { |
SquirrelGod | 2:2182df2d7810 | 12 | // Draw button |
SquirrelGod | 2:2182df2d7810 | 13 | void Button::Draw(uint32_t color, uint32_t textColor) |
SquirrelGod | 2:2182df2d7810 | 14 | { |
SquirrelGod | 2:2182df2d7810 | 15 | if (color == BACK_COLOR_) active_ = true; |
SquirrelGod | 2:2182df2d7810 | 16 | if (!active_) return; |
SquirrelGod | 2:2182df2d7810 | 17 | lcd_.SetTextColor(color); |
SquirrelGod | 2:2182df2d7810 | 18 | lcd_.FillRect(X_, Y_, W_, H_); |
SquirrelGod | 2:2182df2d7810 | 19 | |
SquirrelGod | 2:2182df2d7810 | 20 | if (STR_.length() != 0) |
SquirrelGod | 2:2182df2d7810 | 21 | { |
SquirrelGod | 2:2182df2d7810 | 22 | lcd_.SetFont(FONTS_); |
SquirrelGod | 2:2182df2d7810 | 23 | lcd_.SetBackColor(color); |
SquirrelGod | 2:2182df2d7810 | 24 | lcd_.SetTextColor(textColor); |
SquirrelGod | 2:2182df2d7810 | 25 | uint16_t x0 = X_ + (W_ - FONTS_->Width*(STR_.length()))/2; |
SquirrelGod | 2:2182df2d7810 | 26 | uint16_t y0 = Y_ + (H_ - FONTS_->Height)/2 + 1; |
SquirrelGod | 2:2182df2d7810 | 27 | DrawString(x0, y0, STR_); |
SquirrelGod | 2:2182df2d7810 | 28 | lcd_.SetBackColor(BACK_COLOR_); // recover back color |
SquirrelGod | 2:2182df2d7810 | 29 | } |
SquirrelGod | 2:2182df2d7810 | 30 | } |
SquirrelGod | 2:2182df2d7810 | 31 | |
SquirrelGod | 2:2182df2d7810 | 32 | // Check touch detected |
SquirrelGod | 2:2182df2d7810 | 33 | bool Button::Touched() |
SquirrelGod | 2:2182df2d7810 | 34 | { |
SquirrelGod | 2:2182df2d7810 | 35 | if (!active_) return false; |
SquirrelGod | 2:2182df2d7810 | 36 | if (!PanelTouched()) return false; |
SquirrelGod | 2:2182df2d7810 | 37 | if (!IsOnButton()) return false; |
SquirrelGod | 2:2182df2d7810 | 38 | Draw(TOUCHED_COLOR_, TEXT_COLOR_); |
SquirrelGod | 2:2182df2d7810 | 39 | return true; |
SquirrelGod | 2:2182df2d7810 | 40 | } |
SquirrelGod | 2:2182df2d7810 | 41 | |
SquirrelGod | 2:2182df2d7810 | 42 | // If touched position is on the button, return true |
SquirrelGod | 2:2182df2d7810 | 43 | bool Button::IsOnButton() |
SquirrelGod | 2:2182df2d7810 | 44 | { |
SquirrelGod | 2:2182df2d7810 | 45 | int nTouch = multiTouch_ ? state_.touchDetected : 1; |
SquirrelGod | 2:2182df2d7810 | 46 | for (int n=0; n<nTouch; n++) |
SquirrelGod | 2:2182df2d7810 | 47 | { |
SquirrelGod | 2:2182df2d7810 | 48 | uint16_t x = state_.touchX[n]; |
SquirrelGod | 2:2182df2d7810 | 49 | uint16_t y = state_.touchY[n]; |
SquirrelGod | 2:2182df2d7810 | 50 | if ( (X_ <= x) && (x <= X_+W_) && |
SquirrelGod | 2:2182df2d7810 | 51 | (Y_ <= y) && (y <= Y_+H_) ) return true; |
SquirrelGod | 2:2182df2d7810 | 52 | } |
SquirrelGod | 2:2182df2d7810 | 53 | return false; |
SquirrelGod | 2:2182df2d7810 | 54 | } |
SquirrelGod | 2:2182df2d7810 | 55 | |
SquirrelGod | 2:2182df2d7810 | 56 | void Button::Activate() |
SquirrelGod | 2:2182df2d7810 | 57 | { |
SquirrelGod | 2:2182df2d7810 | 58 | active_ = true; |
SquirrelGod | 2:2182df2d7810 | 59 | Draw(); |
SquirrelGod | 2:2182df2d7810 | 60 | } |
SquirrelGod | 2:2182df2d7810 | 61 | |
SquirrelGod | 2:2182df2d7810 | 62 | void Button::Inactivate() |
SquirrelGod | 2:2182df2d7810 | 63 | { |
SquirrelGod | 2:2182df2d7810 | 64 | Draw(INACTIVE_COLOR_, INACTIVE_TEXT_COLOR_); |
SquirrelGod | 2:2182df2d7810 | 65 | active_ = false; |
SquirrelGod | 2:2182df2d7810 | 66 | } |
SquirrelGod | 2:2182df2d7810 | 67 | } |