Automate écran capacitif
Dependencies: BSP_DISCO_F469NI LCD_DISCO_F469NI SeeedStudioTFTv2 TFT_fonts mbed
Fork of DISCO-F469NI_LCD_demo by
F469_GUI/ButtonGroup.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 | // ButtonGroup class |
SquirrelGod | 2:2182df2d7810 | 3 | // |
SquirrelGod | 2:2182df2d7810 | 4 | // 2016/07/03, Copyright (c) 2016 MIKAMI, Naoki |
SquirrelGod | 2:2182df2d7810 | 5 | //----------------------------------------------------------- |
SquirrelGod | 2:2182df2d7810 | 6 | |
SquirrelGod | 2:2182df2d7810 | 7 | #include "ButtonGroup.hpp" |
SquirrelGod | 2:2182df2d7810 | 8 | |
SquirrelGod | 2:2182df2d7810 | 9 | namespace Mikami |
SquirrelGod | 2:2182df2d7810 | 10 | { |
SquirrelGod | 2:2182df2d7810 | 11 | // Constructor |
SquirrelGod | 2:2182df2d7810 | 12 | ButtonGroup::ButtonGroup( |
SquirrelGod | 2:2182df2d7810 | 13 | uint16_t x0, uint16_t y0, |
SquirrelGod | 2:2182df2d7810 | 14 | uint16_t width, uint16_t height, |
SquirrelGod | 2:2182df2d7810 | 15 | uint16_t number, const string str[], |
SquirrelGod | 2:2182df2d7810 | 16 | uint16_t spaceX, uint16_t spaceY, |
SquirrelGod | 2:2182df2d7810 | 17 | uint16_t column, int touched, |
SquirrelGod | 2:2182df2d7810 | 18 | sFONT &fonts, |
SquirrelGod | 2:2182df2d7810 | 19 | uint32_t textColor, uint32_t backColor, |
SquirrelGod | 2:2182df2d7810 | 20 | uint32_t createdColor, uint32_t touchedColor, |
SquirrelGod | 2:2182df2d7810 | 21 | uint32_t inactiveColor, uint32_t inactiveTextColor) |
SquirrelGod | 2:2182df2d7810 | 22 | : GuiBase(x0, y0, fonts, textColor, backColor, |
SquirrelGod | 2:2182df2d7810 | 23 | createdColor, touchedColor, |
SquirrelGod | 2:2182df2d7810 | 24 | inactiveColor, inactiveTextColor), |
SquirrelGod | 2:2182df2d7810 | 25 | number_(number), prevNum_(touched) |
SquirrelGod | 2:2182df2d7810 | 26 | { |
SquirrelGod | 2:2182df2d7810 | 27 | buttons_ = new Button *[number]; |
SquirrelGod | 2:2182df2d7810 | 28 | for (int n=0; n<number; n++) |
SquirrelGod | 2:2182df2d7810 | 29 | { |
SquirrelGod | 2:2182df2d7810 | 30 | div_t u1 = div(n, column); |
SquirrelGod | 2:2182df2d7810 | 31 | uint16_t x = x0 + u1.rem*(width + spaceX); |
SquirrelGod | 2:2182df2d7810 | 32 | uint16_t y = y0 + u1.quot*(height + spaceY); |
SquirrelGod | 2:2182df2d7810 | 33 | buttons_[n] = |
SquirrelGod | 2:2182df2d7810 | 34 | new Button(x, y, width, height, str[n], fonts, |
SquirrelGod | 2:2182df2d7810 | 35 | TEXT_COLOR_, BACK_COLOR_, |
SquirrelGod | 2:2182df2d7810 | 36 | CREATED_COLOR_, TOUCHED_COLOR_, |
SquirrelGod | 2:2182df2d7810 | 37 | INACTIVE_COLOR_, INACTIVE_TEXT_COLOR_); |
SquirrelGod | 2:2182df2d7810 | 38 | } |
SquirrelGod | 2:2182df2d7810 | 39 | // On created, set touched color as needed |
SquirrelGod | 2:2182df2d7810 | 40 | if (touched >= 0) TouchedColor(touched); |
SquirrelGod | 2:2182df2d7810 | 41 | } |
SquirrelGod | 2:2182df2d7810 | 42 | |
SquirrelGod | 2:2182df2d7810 | 43 | // Destructor |
SquirrelGod | 2:2182df2d7810 | 44 | ButtonGroup::~ButtonGroup() |
SquirrelGod | 2:2182df2d7810 | 45 | { |
SquirrelGod | 2:2182df2d7810 | 46 | for (int n=0; n<number_; n++) delete buttons_[n]; |
SquirrelGod | 2:2182df2d7810 | 47 | delete[] buttons_; |
SquirrelGod | 2:2182df2d7810 | 48 | } |
SquirrelGod | 2:2182df2d7810 | 49 | |
SquirrelGod | 2:2182df2d7810 | 50 | // Draw button |
SquirrelGod | 2:2182df2d7810 | 51 | bool ButtonGroup::Draw(int num, uint32_t color, uint32_t textColor) |
SquirrelGod | 2:2182df2d7810 | 52 | { |
SquirrelGod | 2:2182df2d7810 | 53 | if (!Range(num)) return false; |
SquirrelGod | 2:2182df2d7810 | 54 | buttons_[num]->Draw(color, textColor); |
SquirrelGod | 2:2182df2d7810 | 55 | return true; |
SquirrelGod | 2:2182df2d7810 | 56 | } |
SquirrelGod | 2:2182df2d7810 | 57 | |
SquirrelGod | 2:2182df2d7810 | 58 | // Change to touched color |
SquirrelGod | 2:2182df2d7810 | 59 | bool ButtonGroup::TouchedColor(int num) |
SquirrelGod | 2:2182df2d7810 | 60 | { |
SquirrelGod | 2:2182df2d7810 | 61 | if (prevNum_ != num) prevNum_ = num; |
SquirrelGod | 2:2182df2d7810 | 62 | return Draw(num, TOUCHED_COLOR_, TEXT_COLOR_); |
SquirrelGod | 2:2182df2d7810 | 63 | } |
SquirrelGod | 2:2182df2d7810 | 64 | |
SquirrelGod | 2:2182df2d7810 | 65 | // Erase button |
SquirrelGod | 2:2182df2d7810 | 66 | bool ButtonGroup::Erase(int num) |
SquirrelGod | 2:2182df2d7810 | 67 | { |
SquirrelGod | 2:2182df2d7810 | 68 | if (!Range(num)) return false; |
SquirrelGod | 2:2182df2d7810 | 69 | buttons_[num]->Erase(); |
SquirrelGod | 2:2182df2d7810 | 70 | return true; |
SquirrelGod | 2:2182df2d7810 | 71 | } |
SquirrelGod | 2:2182df2d7810 | 72 | |
SquirrelGod | 2:2182df2d7810 | 73 | // Check touch detected for specified button |
SquirrelGod | 2:2182df2d7810 | 74 | bool ButtonGroup::Touched(int num) |
SquirrelGod | 2:2182df2d7810 | 75 | { |
SquirrelGod | 2:2182df2d7810 | 76 | if (!Range(num)) return false; |
SquirrelGod | 2:2182df2d7810 | 77 | if (!buttons_[num]->IsActive()) return false; |
SquirrelGod | 2:2182df2d7810 | 78 | int touched; |
SquirrelGod | 2:2182df2d7810 | 79 | if (!GetTouchedNumber(touched)) return false; |
SquirrelGod | 2:2182df2d7810 | 80 | bool rtn = (touched == num) ? true : false; |
SquirrelGod | 2:2182df2d7810 | 81 | return rtn; |
SquirrelGod | 2:2182df2d7810 | 82 | } |
SquirrelGod | 2:2182df2d7810 | 83 | |
SquirrelGod | 2:2182df2d7810 | 84 | // Get touched number |
SquirrelGod | 2:2182df2d7810 | 85 | bool ButtonGroup::GetTouchedNumber(int &num) |
SquirrelGod | 2:2182df2d7810 | 86 | { |
SquirrelGod | 2:2182df2d7810 | 87 | bool rtn = false; |
SquirrelGod | 2:2182df2d7810 | 88 | if (PanelTouched()) |
SquirrelGod | 2:2182df2d7810 | 89 | { |
SquirrelGod | 2:2182df2d7810 | 90 | for (int n=0; n<number_; n++) |
SquirrelGod | 2:2182df2d7810 | 91 | if (buttons_[n]->IsOnButton() && |
SquirrelGod | 2:2182df2d7810 | 92 | buttons_[n]->IsActive()) |
SquirrelGod | 2:2182df2d7810 | 93 | { |
SquirrelGod | 2:2182df2d7810 | 94 | num = n; |
SquirrelGod | 2:2182df2d7810 | 95 | rtn = true; |
SquirrelGod | 2:2182df2d7810 | 96 | } |
SquirrelGod | 2:2182df2d7810 | 97 | |
SquirrelGod | 2:2182df2d7810 | 98 | if (!rtn) return false; |
SquirrelGod | 2:2182df2d7810 | 99 | } |
SquirrelGod | 2:2182df2d7810 | 100 | else |
SquirrelGod | 2:2182df2d7810 | 101 | return false; |
SquirrelGod | 2:2182df2d7810 | 102 | |
SquirrelGod | 2:2182df2d7810 | 103 | buttons_[num]->Draw(TOUCHED_COLOR_); |
SquirrelGod | 2:2182df2d7810 | 104 | if ((prevNum_ >= 0) && (prevNum_ != num)) |
SquirrelGod | 2:2182df2d7810 | 105 | buttons_[prevNum_]->Draw(); |
SquirrelGod | 2:2182df2d7810 | 106 | if (prevNum_ != num) prevNum_ = num; |
SquirrelGod | 2:2182df2d7810 | 107 | return true; |
SquirrelGod | 2:2182df2d7810 | 108 | } |
SquirrelGod | 2:2182df2d7810 | 109 | |
SquirrelGod | 2:2182df2d7810 | 110 | // Activate and inactivate button(s) |
SquirrelGod | 2:2182df2d7810 | 111 | bool ButtonGroup::Activate(int num) |
SquirrelGod | 2:2182df2d7810 | 112 | { |
SquirrelGod | 2:2182df2d7810 | 113 | if (!Range(num)) return false; |
SquirrelGod | 2:2182df2d7810 | 114 | buttons_[num]->Activate(); |
SquirrelGod | 2:2182df2d7810 | 115 | return true; |
SquirrelGod | 2:2182df2d7810 | 116 | } |
SquirrelGod | 2:2182df2d7810 | 117 | bool ButtonGroup::Inactivate(int num) |
SquirrelGod | 2:2182df2d7810 | 118 | { |
SquirrelGod | 2:2182df2d7810 | 119 | if (!Range(num)) return false; |
SquirrelGod | 2:2182df2d7810 | 120 | buttons_[num]->Inactivate(); |
SquirrelGod | 2:2182df2d7810 | 121 | return true; |
SquirrelGod | 2:2182df2d7810 | 122 | } |
SquirrelGod | 2:2182df2d7810 | 123 | } |