a
Dependencies: TS_F746 LCD_746 LbBSP746NG GUI_746
F746GUI/F746_SLIDER.hpp@0:151a96a0fc95, 2018-09-25 (annotated)
- Committer:
- wsteenberg
- Date:
- Tue Sep 25 12:41:25 2018 +0000
- Revision:
- 0:151a96a0fc95
DISCO_F746NG_Button_and_Slider_Library v1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wsteenberg | 0:151a96a0fc95 | 1 | /*----------------------------------------------------------- |
wsteenberg | 0:151a96a0fc95 | 2 | * F746_SLIDER Library v1.0 |
wsteenberg | 0:151a96a0fc95 | 3 | * Copyright (c) 2018 Wynand Steenberg |
wsteenberg | 0:151a96a0fc95 | 4 | * s216875730@mandela.ac.za |
wsteenberg | 0:151a96a0fc95 | 5 | * |
wsteenberg | 0:151a96a0fc95 | 6 | * |
wsteenberg | 0:151a96a0fc95 | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
wsteenberg | 0:151a96a0fc95 | 8 | * of this software and associated documentation files (the "Software"), to deal |
wsteenberg | 0:151a96a0fc95 | 9 | * in the Software without restriction, including without limitation the rights |
wsteenberg | 0:151a96a0fc95 | 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
wsteenberg | 0:151a96a0fc95 | 11 | * copies of the Software, and to permit persons to whom the Software is |
wsteenberg | 0:151a96a0fc95 | 12 | * furnished to do so, subject to the following conditions: |
wsteenberg | 0:151a96a0fc95 | 13 | * |
wsteenberg | 0:151a96a0fc95 | 14 | * The above copyright notice and this permission notice shall be included in |
wsteenberg | 0:151a96a0fc95 | 15 | * all copies or substantial portions of the Software. |
wsteenberg | 0:151a96a0fc95 | 16 | * |
wsteenberg | 0:151a96a0fc95 | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
wsteenberg | 0:151a96a0fc95 | 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
wsteenberg | 0:151a96a0fc95 | 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
wsteenberg | 0:151a96a0fc95 | 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
wsteenberg | 0:151a96a0fc95 | 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
wsteenberg | 0:151a96a0fc95 | 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
wsteenberg | 0:151a96a0fc95 | 23 | * THE SOFTWARE. |
wsteenberg | 0:151a96a0fc95 | 24 | *----------------------------------------------------------- |
wsteenberg | 0:151a96a0fc95 | 25 | */ |
wsteenberg | 0:151a96a0fc95 | 26 | |
wsteenberg | 0:151a96a0fc95 | 27 | #ifndef F746_SLIDER_HPP |
wsteenberg | 0:151a96a0fc95 | 28 | #define F746_SLIDER_HPP |
wsteenberg | 0:151a96a0fc95 | 29 | |
wsteenberg | 0:151a96a0fc95 | 30 | #include "mbed.h" |
wsteenberg | 0:151a96a0fc95 | 31 | #include <string> |
wsteenberg | 0:151a96a0fc95 | 32 | #include "TS_DISCO_F746NG.h" |
wsteenberg | 0:151a96a0fc95 | 33 | #include "LCD_DISCO_F746NG.h" |
wsteenberg | 0:151a96a0fc95 | 34 | #include "F746GUI.hpp" |
wsteenberg | 0:151a96a0fc95 | 35 | |
wsteenberg | 0:151a96a0fc95 | 36 | /** A Class library for using Sliders on the DISCO-F746NG Development board. The class |
wsteenberg | 0:151a96a0fc95 | 37 | * uses the existing BSP class created by Team ST. |
wsteenberg | 0:151a96a0fc95 | 38 | * |
wsteenberg | 0:151a96a0fc95 | 39 | * Example: |
wsteenberg | 0:151a96a0fc95 | 40 | * @code |
wsteenberg | 0:151a96a0fc95 | 41 | * #include "mbed.h" |
wsteenberg | 0:151a96a0fc95 | 42 | * #include "F746_SLIDER.hpp" |
wsteenberg | 0:151a96a0fc95 | 43 | * |
wsteenberg | 0:151a96a0fc95 | 44 | * TS_DISCO_F746NG ts_; |
wsteenberg | 0:151a96a0fc95 | 45 | * LCD_DISCO_F746NG lcd_; |
wsteenberg | 0:151a96a0fc95 | 46 | * |
wsteenberg | 0:151a96a0fc95 | 47 | * |
wsteenberg | 0:151a96a0fc95 | 48 | * char char_SliderDisplayValue[4]; // String Value to display for the Slider Control value |
wsteenberg | 0:151a96a0fc95 | 49 | * uint16_t SliderDisplayValue_; // Variable used to access Slider Control Value in F746SLIDER.cpp |
wsteenberg | 0:151a96a0fc95 | 50 | * |
wsteenberg | 0:151a96a0fc95 | 51 | * int main() |
wsteenberg | 0:151a96a0fc95 | 52 | * { |
wsteenberg | 0:151a96a0fc95 | 53 | * lcd_.Clear(LCD_COLOR_WHITE); // Set LCD Background colour |
wsteenberg | 0:151a96a0fc95 | 54 | * |
wsteenberg | 0:151a96a0fc95 | 55 | * Slider sld1(lcd_, ts_, 20, 205, 150, 20, 5, 1, 20, |
wsteenberg | 0:151a96a0fc95 | 56 | * LCD_COLOR_BLUE, LCD_COLOR_YELLOW, 1, "Slider1", Font12); // Define sld1 slider |
wsteenberg | 0:151a96a0fc95 | 57 | * sld1.Render(); // Draw sld1 Slider |
wsteenberg | 0:151a96a0fc95 | 58 | * |
wsteenberg | 0:151a96a0fc95 | 59 | * Slider Slider2(lcd_, ts_, 240, 205, 220, 20, 50, 1, 100, |
wsteenberg | 0:151a96a0fc95 | 60 | * LCD_COLOR_BLUE, LCD_COLOR_YELLOW, 2, "Slider2", Font12); // Define Slider2 slider |
wsteenberg | 0:151a96a0fc95 | 61 | * Slider2.Render(); // Draw Slider2 Slider |
wsteenberg | 0:151a96a0fc95 | 62 | * |
wsteenberg | 0:151a96a0fc95 | 63 | * lcd_.SetTextColor(LCD_COLOR_BLACK); // Set Text colour to Black |
wsteenberg | 0:151a96a0fc95 | 64 | * lcd_.SetFont(&Font12); // Font size 12 |
wsteenberg | 0:151a96a0fc95 | 65 | * lcd_.DisplayStringAt(5, 5, (uint8_t *)"Slider driver for DISCO_F746", LEFT_MODE); // Display main header text |
wsteenberg | 0:151a96a0fc95 | 66 | * lcd_.DisplayStringAt(5, 135, (uint8_t *)"Slider example", LEFT_MODE); // Display secondary header text |
wsteenberg | 0:151a96a0fc95 | 67 | * |
wsteenberg | 0:151a96a0fc95 | 68 | * while (true) // Main program loop |
wsteenberg | 0:151a96a0fc95 | 69 | * { |
wsteenberg | 0:151a96a0fc95 | 70 | * if (sld1.Moved()) // Check if sld1 Slider was touched and run instructions if true |
wsteenberg | 0:151a96a0fc95 | 71 | * { |
wsteenberg | 0:151a96a0fc95 | 72 | * lcd_.SetTextColor(LCD_COLOR_BLACK); |
wsteenberg | 0:151a96a0fc95 | 73 | * lcd_.FillRect(130, 140, 80, 40); // Draw border to display Slider Control Value |
wsteenberg | 0:151a96a0fc95 | 74 | * lcd_.SetTextColor(LCD_COLOR_WHITE); |
wsteenberg | 0:151a96a0fc95 | 75 | * lcd_.FillRect(140, 145, 60, 30); // Draw border to display Slider Control Value |
wsteenberg | 0:151a96a0fc95 | 76 | * |
wsteenberg | 0:151a96a0fc95 | 77 | * sprintf(char_SliderDisplayValue, "%3d", (int) SliderDisplayValue_); // Convert integer to text |
wsteenberg | 0:151a96a0fc95 | 78 | * lcd_.SetFont(&Font12); // Set Font size |
wsteenberg | 0:151a96a0fc95 | 79 | * lcd_.SetTextColor(LCD_COLOR_BLACK); // Set Font colour |
wsteenberg | 0:151a96a0fc95 | 80 | * lcd_.DisplayStringAt(150, 155, (uint8_t *)char_SliderDisplayValue, LEFT_MODE); // Write Slider Control Value to the LCD |
wsteenberg | 0:151a96a0fc95 | 81 | * |
wsteenberg | 0:151a96a0fc95 | 82 | * if (SliderDisplayValue_ < 10) // Slider Control Value decision |
wsteenberg | 0:151a96a0fc95 | 83 | * { |
wsteenberg | 0:151a96a0fc95 | 84 | * lcd_.DisplayStringAt(60, 190, (uint8_t *)" ", LEFT_MODE); // Overwrite previous text |
wsteenberg | 0:151a96a0fc95 | 85 | * lcd_.DisplayStringAt(60, 190, (uint8_t *)"SLIDER1 < 50%", LEFT_MODE); // Write text to LCD |
wsteenberg | 0:151a96a0fc95 | 86 | * } |
wsteenberg | 0:151a96a0fc95 | 87 | * else if (SliderDisplayValue_ == 10) // Slider Control Value decision |
wsteenberg | 0:151a96a0fc95 | 88 | * { |
wsteenberg | 0:151a96a0fc95 | 89 | * lcd_.DisplayStringAt(60, 190, (uint8_t *)" ", LEFT_MODE); // Overwrite previous text |
wsteenberg | 0:151a96a0fc95 | 90 | * lcd_.DisplayStringAt(60, 190, (uint8_t *)"SLIDER1 = 50%", LEFT_MODE); // Write text to LCD |
wsteenberg | 0:151a96a0fc95 | 91 | * } |
wsteenberg | 0:151a96a0fc95 | 92 | * else // Slider Control Value decision |
wsteenberg | 0:151a96a0fc95 | 93 | * { |
wsteenberg | 0:151a96a0fc95 | 94 | * lcd_.DisplayStringAt(60, 190, (uint8_t *)" ", LEFT_MODE); // Overwrite previous text |
wsteenberg | 0:151a96a0fc95 | 95 | * lcd_.DisplayStringAt(60, 190, (uint8_t *)"SLIDER1 > 50%", LEFT_MODE); // Write text to LCD |
wsteenberg | 0:151a96a0fc95 | 96 | * } |
wsteenberg | 0:151a96a0fc95 | 97 | * } // End sld1 instructions |
wsteenberg | 0:151a96a0fc95 | 98 | * |
wsteenberg | 0:151a96a0fc95 | 99 | * if (Slider2.Moved()) // Check if Slider2 Slider was touched and run instructions if true |
wsteenberg | 0:151a96a0fc95 | 100 | * { |
wsteenberg | 0:151a96a0fc95 | 101 | * lcd_.SetTextColor(LCD_COLOR_BLACK); |
wsteenberg | 0:151a96a0fc95 | 102 | * lcd_.FillRect(390, 140, 80, 40); // Draw border to display Slider Control Value |
wsteenberg | 0:151a96a0fc95 | 103 | * lcd_.SetTextColor(LCD_COLOR_WHITE); |
wsteenberg | 0:151a96a0fc95 | 104 | * lcd_.FillRect(400, 145, 60, 30); // Draw border to display Slider Control Value |
wsteenberg | 0:151a96a0fc95 | 105 | * |
wsteenberg | 0:151a96a0fc95 | 106 | * sprintf(char_SliderDisplayValue, "%3d", (int) SliderDisplayValue_); // Convert integer to text |
wsteenberg | 0:151a96a0fc95 | 107 | * lcd_.SetFont(&Font12); // Set Font size |
wsteenberg | 0:151a96a0fc95 | 108 | * lcd_.SetTextColor(LCD_COLOR_BLACK); // Set Font colour |
wsteenberg | 0:151a96a0fc95 | 109 | * lcd_.DisplayStringAt(415, 155, (uint8_t *)char_SliderDisplayValue, LEFT_MODE); // Write Slider Control Value to the LCD |
wsteenberg | 0:151a96a0fc95 | 110 | * } |
wsteenberg | 0:151a96a0fc95 | 111 | * |
wsteenberg | 0:151a96a0fc95 | 112 | * wait(0.02f); |
wsteenberg | 0:151a96a0fc95 | 113 | * } // End Main program loop |
wsteenberg | 0:151a96a0fc95 | 114 | * } // End Main program |
wsteenberg | 0:151a96a0fc95 | 115 | * @endcode |
wsteenberg | 0:151a96a0fc95 | 116 | */ |
wsteenberg | 0:151a96a0fc95 | 117 | |
wsteenberg | 0:151a96a0fc95 | 118 | class Slider |
wsteenberg | 0:151a96a0fc95 | 119 | { |
wsteenberg | 0:151a96a0fc95 | 120 | public: |
wsteenberg | 0:151a96a0fc95 | 121 | //! Constructor |
wsteenberg | 0:151a96a0fc95 | 122 | Slider(LCD_DISCO_F746NG &lcd, TS_DISCO_F746NG &ts, |
wsteenberg | 0:151a96a0fc95 | 123 | uint16_t x, uint16_t y, uint16_t width, uint16_t height, |
wsteenberg | 0:151a96a0fc95 | 124 | uint16_t value_C, uint16_t min, uint16_t max, |
wsteenberg | 0:151a96a0fc95 | 125 | uint32_t sliderColour, uint32_t changeColour, |
wsteenberg | 0:151a96a0fc95 | 126 | int style, const string label = "", sFONT &fonts = Font12, |
wsteenberg | 0:151a96a0fc95 | 127 | uint32_t textColour = LCD_COLOR_WHITE) |
wsteenberg | 0:151a96a0fc95 | 128 | : lcd_(lcd), ts_(ts), SliderX_(x), SliderY_(y), SliderW_(width), SliderH_(height), |
wsteenberg | 0:151a96a0fc95 | 129 | valueControl_(value_C), Min_ (min), Max_ (max), |
wsteenberg | 0:151a96a0fc95 | 130 | SLIDER_COLOUR_(sliderColour), CHANGE_COLOUR_(changeColour), |
wsteenberg | 0:151a96a0fc95 | 131 | STYLE_(style), LABEL_(label), FONTS_(&fonts), FONT_WIDTH_(fonts.Width), |
wsteenberg | 0:151a96a0fc95 | 132 | FONT_HEIGHT_(fonts.Height), active_(true) |
wsteenberg | 0:151a96a0fc95 | 133 | { Render(); } |
wsteenberg | 0:151a96a0fc95 | 134 | |
wsteenberg | 0:151a96a0fc95 | 135 | /** |
wsteenberg | 0:151a96a0fc95 | 136 | * @brief Draw Slider. |
wsteenberg | 0:151a96a0fc95 | 137 | * |
wsteenberg | 0:151a96a0fc95 | 138 | */ |
wsteenberg | 0:151a96a0fc95 | 139 | void Render(); |
wsteenberg | 0:151a96a0fc95 | 140 | |
wsteenberg | 0:151a96a0fc95 | 141 | /** |
wsteenberg | 0:151a96a0fc95 | 142 | * @brief Service Slider. |
wsteenberg | 0:151a96a0fc95 | 143 | * |
wsteenberg | 0:151a96a0fc95 | 144 | */ |
wsteenberg | 0:151a96a0fc95 | 145 | void Service(); |
wsteenberg | 0:151a96a0fc95 | 146 | |
wsteenberg | 0:151a96a0fc95 | 147 | /** |
wsteenberg | 0:151a96a0fc95 | 148 | * @brief Check touch detected on Slider. |
wsteenberg | 0:151a96a0fc95 | 149 | * |
wsteenberg | 0:151a96a0fc95 | 150 | */ |
wsteenberg | 0:151a96a0fc95 | 151 | bool Moved(); |
wsteenberg | 0:151a96a0fc95 | 152 | |
wsteenberg | 0:151a96a0fc95 | 153 | /** |
wsteenberg | 0:151a96a0fc95 | 154 | * @brief Check if touch is on Slider. |
wsteenberg | 0:151a96a0fc95 | 155 | * |
wsteenberg | 0:151a96a0fc95 | 156 | */ |
wsteenberg | 0:151a96a0fc95 | 157 | bool SliderBoundaryCheck(); |
wsteenberg | 0:151a96a0fc95 | 158 | |
wsteenberg | 0:151a96a0fc95 | 159 | /** |
wsteenberg | 0:151a96a0fc95 | 160 | * @brief Check previous state of Slider. |
wsteenberg | 0:151a96a0fc95 | 161 | * |
wsteenberg | 0:151a96a0fc95 | 162 | */ |
wsteenberg | 0:151a96a0fc95 | 163 | static TS_StateTypeDef GottenState() |
wsteenberg | 0:151a96a0fc95 | 164 | { return state_; } |
wsteenberg | 0:151a96a0fc95 | 165 | |
wsteenberg | 0:151a96a0fc95 | 166 | /** |
wsteenberg | 0:151a96a0fc95 | 167 | * @brief Set or reset multi-touch. |
wsteenberg | 0:151a96a0fc95 | 168 | * |
wsteenberg | 0:151a96a0fc95 | 169 | */ |
wsteenberg | 0:151a96a0fc95 | 170 | static void SetMultiTouch(bool tf) { multiTouch = tf; } |
wsteenberg | 0:151a96a0fc95 | 171 | |
wsteenberg | 0:151a96a0fc95 | 172 | |
wsteenberg | 0:151a96a0fc95 | 173 | private: |
wsteenberg | 0:151a96a0fc95 | 174 | LCD_DISCO_F746NG &lcd_; |
wsteenberg | 0:151a96a0fc95 | 175 | TS_DISCO_F746NG &ts_; |
wsteenberg | 0:151a96a0fc95 | 176 | |
wsteenberg | 0:151a96a0fc95 | 177 | const uint16_t SliderX_, SliderY_, SliderW_, SliderH_, valueControl_, Min_, Max_; |
wsteenberg | 0:151a96a0fc95 | 178 | const uint32_t SLIDER_COLOUR_; // original color |
wsteenberg | 0:151a96a0fc95 | 179 | const uint32_t CHANGE_COLOUR_; // colour to change to color |
wsteenberg | 0:151a96a0fc95 | 180 | const int STYLE_; // Slider style |
wsteenberg | 0:151a96a0fc95 | 181 | const string LABEL_; // Slider label |
wsteenberg | 0:151a96a0fc95 | 182 | sFONT *const FONTS_; |
wsteenberg | 0:151a96a0fc95 | 183 | const uint16_t FONT_WIDTH_; |
wsteenberg | 0:151a96a0fc95 | 184 | const uint16_t FONT_HEIGHT_; |
wsteenberg | 0:151a96a0fc95 | 185 | bool active_; // Slider active boolean |
wsteenberg | 0:151a96a0fc95 | 186 | |
wsteenberg | 0:151a96a0fc95 | 187 | static TS_StateTypeDef state_; |
wsteenberg | 0:151a96a0fc95 | 188 | static bool multiTouch; |
wsteenberg | 0:151a96a0fc95 | 189 | |
wsteenberg | 0:151a96a0fc95 | 190 | /** |
wsteenberg | 0:151a96a0fc95 | 191 | * @brief Disallow copy constructor and assignment operator. |
wsteenberg | 0:151a96a0fc95 | 192 | * |
wsteenberg | 0:151a96a0fc95 | 193 | */ |
wsteenberg | 0:151a96a0fc95 | 194 | Slider(const Slider&); |
wsteenberg | 0:151a96a0fc95 | 195 | Slider& operator=(const Slider&); |
wsteenberg | 0:151a96a0fc95 | 196 | }; |
wsteenberg | 0:151a96a0fc95 | 197 | #endif // F746_SLIDER_HPP |