![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
CQエレクトロニクス・セミナ「実習・マイコンを動かしながら学ぶディジタル・フィルタ」で使うプログラム.1次IIRフィルタの係数をターミナルから変更できる. http://seminar.cqpub.co.jp/ccm/ES18-0020
Dependencies: mbed Array_Matrix BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG
F746_Gui_New/SeekBar.cpp@2:aa11055138a2, 2017-11-08 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Wed Nov 08 11:10:56 2017 +0000
- Revision:
- 2:aa11055138a2
- Parent:
- 0:47718d3154d9
3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:47718d3154d9 | 1 | //----------------------------------------------------------- |
MikamiUitOpen | 0:47718d3154d9 | 2 | // SeekBar class |
MikamiUitOpen | 0:47718d3154d9 | 3 | // |
MikamiUitOpen | 0:47718d3154d9 | 4 | // 2017/01/25, Copyright (c) 2017 MIKAMI, Naoki |
MikamiUitOpen | 0:47718d3154d9 | 5 | //----------------------------------------------------------- |
MikamiUitOpen | 0:47718d3154d9 | 6 | |
MikamiUitOpen | 0:47718d3154d9 | 7 | #include "SeekBar.hpp" |
MikamiUitOpen | 0:47718d3154d9 | 8 | |
MikamiUitOpen | 0:47718d3154d9 | 9 | namespace Mikami |
MikamiUitOpen | 0:47718d3154d9 | 10 | { |
MikamiUitOpen | 0:47718d3154d9 | 11 | // Constructor with scale value (only horizontal) |
MikamiUitOpen | 0:47718d3154d9 | 12 | SeekBar::SeekBar(uint16_t x, uint16_t y, uint16_t length, |
MikamiUitOpen | 0:47718d3154d9 | 13 | float min, float max, float initialValue, |
MikamiUitOpen | 0:47718d3154d9 | 14 | string left, string center, string right, |
MikamiUitOpen | 0:47718d3154d9 | 15 | uint32_t thumbColor, |
MikamiUitOpen | 0:47718d3154d9 | 16 | uint16_t thumbSize, uint16_t width, |
MikamiUitOpen | 0:47718d3154d9 | 17 | uint32_t colorL, uint32_t colorH, |
MikamiUitOpen | 0:47718d3154d9 | 18 | uint32_t backColor) |
MikamiUitOpen | 0:47718d3154d9 | 19 | : GuiBase(x, y, Font12, GuiBase::ENUM_TEXT, backColor, thumbColor), |
MikamiUitOpen | 0:47718d3154d9 | 20 | L_(length), W_(width), |
MikamiUitOpen | 0:47718d3154d9 | 21 | SIZE_(thumbSize), COLOR_L_(colorL), COLOR_H_(colorH), |
MikamiUitOpen | 0:47718d3154d9 | 22 | MIN_(min), MAX_(max), ORIENT_(Holizontal), v_(initialValue), |
MikamiUitOpen | 0:47718d3154d9 | 23 | labelOn_(true), slided_(false), active_(true) |
MikamiUitOpen | 0:47718d3154d9 | 24 | { |
MikamiUitOpen | 0:47718d3154d9 | 25 | Draw(initialValue); |
MikamiUitOpen | 0:47718d3154d9 | 26 | uint16_t y0 = y - thumbSize/2 - 13; |
MikamiUitOpen | 0:47718d3154d9 | 27 | labelLCR_[0] = new Label(x, y0, left, Label::CENTER); |
MikamiUitOpen | 0:47718d3154d9 | 28 | labelLCR_[1] = new Label(x+length/2, y0, center, Label::CENTER); |
MikamiUitOpen | 0:47718d3154d9 | 29 | labelLCR_[2] = new Label(x+length, y0, right, Label::CENTER); |
MikamiUitOpen | 0:47718d3154d9 | 30 | } |
MikamiUitOpen | 0:47718d3154d9 | 31 | |
MikamiUitOpen | 0:47718d3154d9 | 32 | // Slide thumb |
MikamiUitOpen | 0:47718d3154d9 | 33 | // If the thumb is not touched, return false |
MikamiUitOpen | 0:47718d3154d9 | 34 | bool SeekBar::Slide() |
MikamiUitOpen | 0:47718d3154d9 | 35 | { |
MikamiUitOpen | 0:47718d3154d9 | 36 | if (!active_) return false; |
MikamiUitOpen | 0:47718d3154d9 | 37 | |
MikamiUitOpen | 0:47718d3154d9 | 38 | if (!PanelTouched()) |
MikamiUitOpen | 0:47718d3154d9 | 39 | { |
MikamiUitOpen | 0:47718d3154d9 | 40 | if (slided_) Draw(v_); |
MikamiUitOpen | 0:47718d3154d9 | 41 | slided_ = false; |
MikamiUitOpen | 0:47718d3154d9 | 42 | return false; |
MikamiUitOpen | 0:47718d3154d9 | 43 | } |
MikamiUitOpen | 0:47718d3154d9 | 44 | |
MikamiUitOpen | 0:47718d3154d9 | 45 | uint16_t x, y; |
MikamiUitOpen | 0:47718d3154d9 | 46 | bool rtn = IsOnThumb(x, y); |
MikamiUitOpen | 0:47718d3154d9 | 47 | if (rtn || slided_) |
MikamiUitOpen | 0:47718d3154d9 | 48 | { |
MikamiUitOpen | 0:47718d3154d9 | 49 | if (rtn) v_ = ToValue(Point(x, y)); |
MikamiUitOpen | 0:47718d3154d9 | 50 | Draw(v_, rtn); |
MikamiUitOpen | 0:47718d3154d9 | 51 | slided_ = rtn; |
MikamiUitOpen | 0:47718d3154d9 | 52 | } |
MikamiUitOpen | 0:47718d3154d9 | 53 | return rtn; |
MikamiUitOpen | 0:47718d3154d9 | 54 | } |
MikamiUitOpen | 0:47718d3154d9 | 55 | |
MikamiUitOpen | 0:47718d3154d9 | 56 | void SeekBar::Activate() |
MikamiUitOpen | 0:47718d3154d9 | 57 | { |
MikamiUitOpen | 0:47718d3154d9 | 58 | active_ = true; |
MikamiUitOpen | 0:47718d3154d9 | 59 | Draw(v_); |
MikamiUitOpen | 0:47718d3154d9 | 60 | if (labelOn_) |
MikamiUitOpen | 0:47718d3154d9 | 61 | for (int n=0; n<3; n++) labelLCR_[n]->Draw(TEXT_COLOR_); |
MikamiUitOpen | 0:47718d3154d9 | 62 | } |
MikamiUitOpen | 0:47718d3154d9 | 63 | |
MikamiUitOpen | 0:47718d3154d9 | 64 | void SeekBar::Inactivate() |
MikamiUitOpen | 0:47718d3154d9 | 65 | { |
MikamiUitOpen | 0:47718d3154d9 | 66 | active_ = false; |
MikamiUitOpen | 0:47718d3154d9 | 67 | Draw(v_); |
MikamiUitOpen | 0:47718d3154d9 | 68 | if (labelOn_) |
MikamiUitOpen | 0:47718d3154d9 | 69 | for (int n=0; n<3; n++) labelLCR_[n]->Draw(INACTIVE_TEXT_COLOR_); |
MikamiUitOpen | 0:47718d3154d9 | 70 | } |
MikamiUitOpen | 0:47718d3154d9 | 71 | |
MikamiUitOpen | 0:47718d3154d9 | 72 | // If touched position is on the thumb, return true |
MikamiUitOpen | 0:47718d3154d9 | 73 | bool SeekBar::IsOnThumb(uint16_t &x, uint16_t &y) |
MikamiUitOpen | 0:47718d3154d9 | 74 | { |
MikamiUitOpen | 0:47718d3154d9 | 75 | x = state_.touchX[0]; |
MikamiUitOpen | 0:47718d3154d9 | 76 | y = state_.touchY[0]; |
MikamiUitOpen | 0:47718d3154d9 | 77 | |
MikamiUitOpen | 0:47718d3154d9 | 78 | uint16_t th = SIZE_/2; |
MikamiUitOpen | 0:47718d3154d9 | 79 | Point pt = ToPoint(v_); |
MikamiUitOpen | 0:47718d3154d9 | 80 | if (ORIENT_ == Holizontal) |
MikamiUitOpen | 0:47718d3154d9 | 81 | { |
MikamiUitOpen | 0:47718d3154d9 | 82 | if ( (pt.x-th <= x) && (x <= pt.x+th) && |
MikamiUitOpen | 0:47718d3154d9 | 83 | (pt.y-th <= y) && (y <= pt.y+th) ) return true; |
MikamiUitOpen | 0:47718d3154d9 | 84 | } |
MikamiUitOpen | 0:47718d3154d9 | 85 | else |
MikamiUitOpen | 0:47718d3154d9 | 86 | { |
MikamiUitOpen | 0:47718d3154d9 | 87 | if ( (pt.x-th <= x) && (x <= pt.x+th) && |
MikamiUitOpen | 0:47718d3154d9 | 88 | (pt.y-th <= y) && (y <= pt.y+th) ) return true; |
MikamiUitOpen | 0:47718d3154d9 | 89 | } |
MikamiUitOpen | 0:47718d3154d9 | 90 | |
MikamiUitOpen | 0:47718d3154d9 | 91 | return false; |
MikamiUitOpen | 0:47718d3154d9 | 92 | } |
MikamiUitOpen | 0:47718d3154d9 | 93 | |
MikamiUitOpen | 0:47718d3154d9 | 94 | // Draw seekbar |
MikamiUitOpen | 0:47718d3154d9 | 95 | void SeekBar::Draw(float value, bool fill) |
MikamiUitOpen | 0:47718d3154d9 | 96 | { |
MikamiUitOpen | 0:47718d3154d9 | 97 | uint16_t sizeS = (uint16_t)(SIZE_*0.6f); |
MikamiUitOpen | 0:47718d3154d9 | 98 | // Erase previous seekbar |
MikamiUitOpen | 0:47718d3154d9 | 99 | lcd_.SetTextColor(BACK_COLOR_); |
MikamiUitOpen | 0:47718d3154d9 | 100 | if (ORIENT_ == Holizontal) |
MikamiUitOpen | 0:47718d3154d9 | 101 | lcd_.FillRect(X_-sizeS/2, Y_-SIZE_/2, L_+sizeS+1, SIZE_+1); |
MikamiUitOpen | 0:47718d3154d9 | 102 | else |
MikamiUitOpen | 0:47718d3154d9 | 103 | lcd_.FillRect(X_-SIZE_/2, Y_-sizeS/2, SIZE_+1, L_+sizeS+1); |
MikamiUitOpen | 0:47718d3154d9 | 104 | |
MikamiUitOpen | 0:47718d3154d9 | 105 | v_ = Saturate(value); // current value |
MikamiUitOpen | 0:47718d3154d9 | 106 | Point pt = ToPoint(v_); // Position of thumb |
MikamiUitOpen | 0:47718d3154d9 | 107 | |
MikamiUitOpen | 0:47718d3154d9 | 108 | // Draw upper line |
MikamiUitOpen | 0:47718d3154d9 | 109 | if (active_) lcd_.SetTextColor(COLOR_H_); |
MikamiUitOpen | 0:47718d3154d9 | 110 | else lcd_.SetTextColor(INACTIVE_TEXT_COLOR_-0x404040); |
MikamiUitOpen | 0:47718d3154d9 | 111 | if ((ORIENT_ == Holizontal) && ((X_+L_-pt.x) > 0)) |
MikamiUitOpen | 0:47718d3154d9 | 112 | lcd_.FillRect(pt.x, Y_-W_/4, X_+L_-pt.x, W_/2); |
MikamiUitOpen | 0:47718d3154d9 | 113 | if ((ORIENT_ == Vertical) && ((pt.y-Y_) > 0)) |
MikamiUitOpen | 0:47718d3154d9 | 114 | lcd_.FillRect(X_-W_/4, Y_, W_/2, pt.y-Y_); |
MikamiUitOpen | 0:47718d3154d9 | 115 | |
MikamiUitOpen | 0:47718d3154d9 | 116 | // Draw lower line |
MikamiUitOpen | 0:47718d3154d9 | 117 | if (active_) lcd_.SetTextColor(COLOR_L_); |
MikamiUitOpen | 0:47718d3154d9 | 118 | else lcd_.SetTextColor(INACTIVE_TEXT_COLOR_-0x202020); |
MikamiUitOpen | 0:47718d3154d9 | 119 | if ((ORIENT_ == Holizontal) && ((pt.x-X_) > 0)) |
MikamiUitOpen | 0:47718d3154d9 | 120 | lcd_.FillRect(X_, Y_-W_/2, pt.x-X_, W_); |
MikamiUitOpen | 0:47718d3154d9 | 121 | if ((ORIENT_ == Vertical) && ((Y_+L_-pt.y) > 0)) |
MikamiUitOpen | 0:47718d3154d9 | 122 | lcd_.FillRect(X_-W_/2, pt.y, W_, Y_+L_-pt.y); |
MikamiUitOpen | 0:47718d3154d9 | 123 | |
MikamiUitOpen | 0:47718d3154d9 | 124 | // Draw thumb |
MikamiUitOpen | 0:47718d3154d9 | 125 | if (active_) lcd_.SetTextColor(CREATED_COLOR_); |
MikamiUitOpen | 0:47718d3154d9 | 126 | else lcd_.SetTextColor(INACTIVE_TEXT_COLOR_); |
MikamiUitOpen | 0:47718d3154d9 | 127 | uint16_t width = SIZE_; |
MikamiUitOpen | 0:47718d3154d9 | 128 | uint16_t height = SIZE_; |
MikamiUitOpen | 0:47718d3154d9 | 129 | if (ORIENT_ == Holizontal) width = sizeS; |
MikamiUitOpen | 0:47718d3154d9 | 130 | else height = sizeS; |
MikamiUitOpen | 0:47718d3154d9 | 131 | uint16_t xPos = pt.x - width/2; |
MikamiUitOpen | 0:47718d3154d9 | 132 | uint16_t yPos = pt.y - height/2; |
MikamiUitOpen | 0:47718d3154d9 | 133 | |
MikamiUitOpen | 0:47718d3154d9 | 134 | if (fill) |
MikamiUitOpen | 0:47718d3154d9 | 135 | lcd_.FillRect(xPos, yPos, width+1, height+1); |
MikamiUitOpen | 0:47718d3154d9 | 136 | else |
MikamiUitOpen | 0:47718d3154d9 | 137 | { |
MikamiUitOpen | 0:47718d3154d9 | 138 | lcd_.DrawRect(xPos, yPos, width, height); |
MikamiUitOpen | 0:47718d3154d9 | 139 | lcd_.DrawHLine(pt.x+width/2, pt.y+height/2, 1); |
MikamiUitOpen | 0:47718d3154d9 | 140 | lcd_.DrawRect(xPos+1, yPos+1, width-2, height-2); |
MikamiUitOpen | 0:47718d3154d9 | 141 | lcd_.DrawHLine(pt.x+width/2-1, pt.y+height/2-1, 1); |
MikamiUitOpen | 0:47718d3154d9 | 142 | if (ORIENT_ == Holizontal) |
MikamiUitOpen | 0:47718d3154d9 | 143 | lcd_.DrawVLine(pt.x, yPos+4, SIZE_-7); |
MikamiUitOpen | 0:47718d3154d9 | 144 | else |
MikamiUitOpen | 0:47718d3154d9 | 145 | lcd_.DrawHLine(xPos+4, pt.y, SIZE_-7); |
MikamiUitOpen | 0:47718d3154d9 | 146 | } |
MikamiUitOpen | 0:47718d3154d9 | 147 | } |
MikamiUitOpen | 0:47718d3154d9 | 148 | |
MikamiUitOpen | 0:47718d3154d9 | 149 | void SeekBar::Redraw(bool fill) |
MikamiUitOpen | 0:47718d3154d9 | 150 | { |
MikamiUitOpen | 0:47718d3154d9 | 151 | Draw(GetValue(), fill); |
MikamiUitOpen | 0:47718d3154d9 | 152 | if (labelOn_) |
MikamiUitOpen | 0:47718d3154d9 | 153 | for (int n=0; n<3; n++) labelLCR_[n]->Draw(TEXT_COLOR_); |
MikamiUitOpen | 0:47718d3154d9 | 154 | } |
MikamiUitOpen | 0:47718d3154d9 | 155 | |
MikamiUitOpen | 0:47718d3154d9 | 156 | SeekBar::Point SeekBar::ToPoint(float value) |
MikamiUitOpen | 0:47718d3154d9 | 157 | { |
MikamiUitOpen | 0:47718d3154d9 | 158 | if (ORIENT_ == Holizontal) |
MikamiUitOpen | 0:47718d3154d9 | 159 | return Point(X_ + Round(L_*(value - MIN_)/(MAX_ - MIN_)), Y_); |
MikamiUitOpen | 0:47718d3154d9 | 160 | else |
MikamiUitOpen | 0:47718d3154d9 | 161 | return Point(X_, Y_ + L_ - Round(L_*(value - MIN_)/(MAX_ - MIN_))); |
MikamiUitOpen | 0:47718d3154d9 | 162 | } |
MikamiUitOpen | 0:47718d3154d9 | 163 | |
MikamiUitOpen | 0:47718d3154d9 | 164 | float SeekBar::ToValue(Point pt) |
MikamiUitOpen | 0:47718d3154d9 | 165 | { |
MikamiUitOpen | 0:47718d3154d9 | 166 | float value; |
MikamiUitOpen | 0:47718d3154d9 | 167 | if (ORIENT_ == Holizontal) |
MikamiUitOpen | 0:47718d3154d9 | 168 | value = (pt.x - X_)*(MAX_ - MIN_)/(float)L_ + MIN_; |
MikamiUitOpen | 0:47718d3154d9 | 169 | else |
MikamiUitOpen | 0:47718d3154d9 | 170 | value = -(pt.y - Y_ - L_)*(MAX_ - MIN_)/(float)L_ + MIN_; |
MikamiUitOpen | 0:47718d3154d9 | 171 | return Saturate(value); |
MikamiUitOpen | 0:47718d3154d9 | 172 | } |
MikamiUitOpen | 0:47718d3154d9 | 173 | |
MikamiUitOpen | 0:47718d3154d9 | 174 | float SeekBar::Saturate(float value) |
MikamiUitOpen | 0:47718d3154d9 | 175 | { |
MikamiUitOpen | 0:47718d3154d9 | 176 | if (value < MIN_) value = MIN_; |
MikamiUitOpen | 0:47718d3154d9 | 177 | if (value > MAX_) value = MAX_; |
MikamiUitOpen | 0:47718d3154d9 | 178 | return value; |
MikamiUitOpen | 0:47718d3154d9 | 179 | } |
MikamiUitOpen | 0:47718d3154d9 | 180 | } |
MikamiUitOpen | 0:47718d3154d9 | 181 | |
MikamiUitOpen | 0:47718d3154d9 | 182 |