Class library: Button class and ButtonGroup class for DISCO-F746NG. クラスライブラリ: DISCO-F746NG 用の,Button クラス,ButtonGroup クラス.
Dependents: F746_SpectralAnalysis_NoPhoto F746_Fourier_series_of_square_wave_01 F746_ButtonGroup_Demo F746_Mandelbrot ... more
Revision 0:d3038879fed6, committed 2015-11-22
- Comitter:
- MikamiUitOpen
- Date:
- Sun Nov 22 10:07:55 2015 +0000
- Child:
- 1:57fe493e8db2
- Commit message:
- 1
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/button.cpp Sun Nov 22 10:07:55 2015 +0000 @@ -0,0 +1,58 @@ +//----------------------------------------------------------- +// Button class +// +// 2015/11/22, Copyright (c) 2015 MIKAMI, Naoki +//----------------------------------------------------------- + +#include "button.hpp" + +namespace Mikami +{ + // Draw button + void Button::Draw(uint32_t color, uint32_t textColor) + { + lcd_.SetTextColor(color); + lcd_.FillRect(X_, Y_, W_, H_); + + if (STR_.length() != 0) + { + lcd_.SetFont(FONTS_); + lcd_.SetBackColor(color); + lcd_.SetTextColor(textColor); + + sFONT *font = lcd_.GetFont(); + uint16_t x0 = X_ + (W_ - (font->Width)*(STR_.length()))/2; + uint16_t y0 = Y_ + (H_ - font->Height)/2; + + lcd_.DisplayStringAt(x0, y0, (uint8_t *)STR_.c_str(), LEFT_MODE); + + lcd_.SetBackColor(BACK_COLOR_); // recover back color + } + } + + // Check touch detected + bool Button::Touched() + { + TS_StateTypeDef state; + ts_.GetState(&state); + + if (state.touchDetected) + { + uint16_t x = state.touchX[0]; + uint16_t y = state.touchY[0]; + + return ( (X_ <= x) && (x <= X_+W_) && + (Y_ <= y) && (y <= Y_+H_) ) ? true : false; + } + else + return false; + } + + // Check touch detected and redraw button + bool Button::Touched(uint32_t color, uint32_t textColor) + { + bool rtn = Touched(); + if (rtn) Draw(color, textColor); + return rtn; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/button.hpp Sun Nov 22 10:07:55 2015 +0000 @@ -0,0 +1,65 @@ +//----------------------------------------------------------- +// Button class -- Header +// +// 2015/11/22, Copyright (c) 2015 MIKAMI, Naoki +//----------------------------------------------------------- + +#ifndef F746_BUTTON_HPP +#define F746_BUTTON_HPP + +#include "mbed.h" +#include <string> +#include "TS_DISCO_F746NG.h" +#include "LCD_DISCO_F746NG.h" + +namespace Mikami +{ + class Button + { + public: + // Constructor + Button(LCD_DISCO_F746NG &lcd, TS_DISCO_F746NG &ts, + uint16_t x, uint16_t y, uint16_t width, uint16_t height, + uint32_t color, uint32_t backColor, + string str = "", sFONT &fonts = Font12, + uint32_t textColor = LCD_COLOR_WHITE) + : lcd_(lcd), ts_(ts), X_(x), Y_(y), W_(width), H_(height), + COLOR_(color), BACK_COLOR_(backColor), + STR_(str), FONTS_(&fonts) + { Draw(color, textColor); } + + // Draw button + void Draw(uint32_t color, uint32_t textColor = LCD_COLOR_WHITE); + + // Redraw button with original color + void Redraw(uint32_t textColor = LCD_COLOR_WHITE) + { Draw(COLOR_, textColor); } + + // Erase button with selected color + void Erase() + { Draw(BACK_COLOR_, BACK_COLOR_); } + + // Check touch detected + bool Touched(); + + // Check touch detected and redraw button + bool Touched(uint32_t color, uint32_t textColor = LCD_COLOR_WHITE); + + // Get original color + uint32_t GetColor() { return COLOR_; } + + private: + LCD_DISCO_F746NG &lcd_; + TS_DISCO_F746NG &ts_; + const uint16_t X_, Y_, W_, H_; + const uint32_t COLOR_; // original color + const uint32_t BACK_COLOR_; // back color of screen + const string STR_; + sFONT *const FONTS_; + + // disallow copy constructor and assignment operator + Button(const Button&); + Button& operator=(const Button&); + }; +} +#endif // F746_BUTTON_HPP
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/button_group.cpp Sun Nov 22 10:07:55 2015 +0000 @@ -0,0 +1,69 @@ +//----------------------------------------------------------- +// Button group class -- Header +// +// 2015/11/22, Copyright (c) 2015 MIKAMI, Naoki +//----------------------------------------------------------- + +#include "button_group.hpp" + +namespace Mikami +{ + // Constructor + ButtonGroup::ButtonGroup(LCD_DISCO_F746NG &lcd, TS_DISCO_F746NG &ts, + uint16_t x0, uint16_t y0, + uint16_t width, uint16_t height, + uint32_t color, uint32_t backColor, + uint16_t number, string str[], + uint16_t spaceX, uint16_t spaceY, + uint16_t column, + sFONT &fonts, uint32_t textColor) + : numberOfButtons_(number) + { + buttons_ = new Button *[number]; + for (int n=0; n<number; n++) + { + div_t u1 = div(n, column); + uint16_t x = x0 + u1.rem*(width + spaceX); + uint16_t y = y0 + u1.quot*(height + spaceY); + buttons_[n] = new Button(lcd, ts, x, y, width, height, + color, backColor, + str[n], fonts, textColor); + } + } + + // Destructor + ButtonGroup::~ButtonGroup() + { + for (int n=0; n<numberOfButtons_; n++) delete buttons_[n]; + delete[] *buttons_; + } + + // Get touched number + bool ButtonGroup::GetTouchedNumber(int &num) + { + for (int n=0; n<numberOfButtons_; n++) + if (buttons_[n]->Touched()) + { + num = n; + return true; + } + return false; + } + + // Get touched number and redraw button if touched + bool ButtonGroup::GetTouchedNumber(int &num, uint32_t color, + uint32_t textColor) + { + if (GetTouchedNumber(num)) + { + for (int n=0; n<numberOfButtons_; n++) + if (n == num) + buttons_[n]->Draw(color, textColor); + else + buttons_[n]->Draw(buttons_[n]->GetColor(), textColor); + return true; + } + else + return false; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/button_group.hpp Sun Nov 22 10:07:55 2015 +0000 @@ -0,0 +1,67 @@ +//----------------------------------------------------------- +// Button group class -- Header +// +// 2015/11/22, Copyright (c) 2015 MIKAMI, Naoki +//----------------------------------------------------------- + +#ifndef F746_BUTTON_GROUP_HPP +#define F746_BUTTON_GROUP_HPP + +#include "button.hpp" +#include <string> + +namespace Mikami +{ + class ButtonGroup + { + public: + // Constructor + ButtonGroup(LCD_DISCO_F746NG &lcd, TS_DISCO_F746NG &ts, + uint16_t x0, uint16_t y0, + uint16_t width, uint16_t height, + uint32_t color, uint32_t backColor, + uint16_t number, string str[], + uint16_t spaceX = 0, uint16_t spaceY = 0, + uint16_t column = 1, + sFONT &fonts = Font12, + uint32_t textColor = LCD_COLOR_WHITE); + + // Destructor + ~ButtonGroup(); + + // Draw button + void Draw(int num, uint32_t color, uint32_t textColor = LCD_COLOR_WHITE) + { buttons_[num]->Draw(color, textColor); } + + // Redraw button with original color + void Redraw(int num, uint32_t textColor = LCD_COLOR_WHITE) + { buttons_[num]->Draw(buttons_[num]->GetColor(), textColor); } + + // Erase button with selected color + void Erase(int num, uint32_t color) + { buttons_[num]->Draw(color, color); } + + // Check touch detected for specified button + bool Touched(int num) + { return buttons_[num]->Touched(); } + + // Check touch detected for specified button and redraw button + bool Touched(int num, uint32_t color, uint32_t textColor = LCD_COLOR_WHITE) + { return buttons_[num]->Touched(color, textColor); } + + // Get touched number + bool GetTouchedNumber(int &num); + + // Get touched number and redraw button if touched + bool GetTouchedNumber(int &num, uint32_t color, uint32_t textColor = LCD_COLOR_WHITE); + + private: + Button **buttons_; + int numberOfButtons_; + + // disallow copy constructor and assignment operator + ButtonGroup(const ButtonGroup&); + ButtonGroup& operator=(const ButtonGroup&); + }; +} +#endif // F746_BUTTON_GROUP_HPP