Simple version for outreach using https://os.mbed.com/users/wsteenberg/code/

Dependencies:   mbed LCD_DISCO_F469NI TS_DISCO_F469NI BSP_DISCO_F469NI

Committer:
wsteenberg
Date:
Tue Sep 25 12:38:48 2018 +0000
Revision:
0:ebf3f36f3a64
DISCO-F469NI_Button_and_Slider_Library v1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wsteenberg 0:ebf3f36f3a64 1 /*-----------------------------------------------------------
wsteenberg 0:ebf3f36f3a64 2 * F469_BUTTON Library v1.0
wsteenberg 0:ebf3f36f3a64 3 * Copyright (c) 2018 Wynand Steenberg
wsteenberg 0:ebf3f36f3a64 4 * s216875730@mandela.ac.za
wsteenberg 0:ebf3f36f3a64 5 *
wsteenberg 0:ebf3f36f3a64 6 *
wsteenberg 0:ebf3f36f3a64 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
wsteenberg 0:ebf3f36f3a64 8 * of this software and associated documentation files (the "Software"), to deal
wsteenberg 0:ebf3f36f3a64 9 * in the Software without restriction, including without limitation the rights
wsteenberg 0:ebf3f36f3a64 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wsteenberg 0:ebf3f36f3a64 11 * copies of the Software, and to permit persons to whom the Software is
wsteenberg 0:ebf3f36f3a64 12 * furnished to do so, subject to the following conditions:
wsteenberg 0:ebf3f36f3a64 13 *
wsteenberg 0:ebf3f36f3a64 14 * The above copyright notice and this permission notice shall be included in
wsteenberg 0:ebf3f36f3a64 15 * all copies or substantial portions of the Software.
wsteenberg 0:ebf3f36f3a64 16 *
wsteenberg 0:ebf3f36f3a64 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wsteenberg 0:ebf3f36f3a64 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wsteenberg 0:ebf3f36f3a64 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wsteenberg 0:ebf3f36f3a64 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wsteenberg 0:ebf3f36f3a64 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wsteenberg 0:ebf3f36f3a64 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wsteenberg 0:ebf3f36f3a64 23 * THE SOFTWARE.
wsteenberg 0:ebf3f36f3a64 24 *-----------------------------------------------------------
wsteenberg 0:ebf3f36f3a64 25 */
wsteenberg 0:ebf3f36f3a64 26
wsteenberg 0:ebf3f36f3a64 27 #ifndef F469_BUTTON_HPP
wsteenberg 0:ebf3f36f3a64 28 #define F469_BUTTON_HPP
wsteenberg 0:ebf3f36f3a64 29
wsteenberg 0:ebf3f36f3a64 30 #include "mbed.h"
wsteenberg 0:ebf3f36f3a64 31 #include <string>
wsteenberg 0:ebf3f36f3a64 32 #include "TS_DISCO_F469NI.h"
wsteenberg 0:ebf3f36f3a64 33 #include "LCD_DISCO_F469NI.h"
wsteenberg 0:ebf3f36f3a64 34 #include "F469GUI.hpp"
wsteenberg 0:ebf3f36f3a64 35
wsteenberg 0:ebf3f36f3a64 36 /** A Class library for using Buttons on the DISCO-F469NI Development board. The class
wsteenberg 0:ebf3f36f3a64 37 * uses the existing BSP class created by Team ST.
wsteenberg 0:ebf3f36f3a64 38 *
wsteenberg 0:ebf3f36f3a64 39 * Example:
wsteenberg 0:ebf3f36f3a64 40 * @code
wsteenberg 0:ebf3f36f3a64 41 * #include "mbed.h"
wsteenberg 0:ebf3f36f3a64 42 * #include "F469_BUTTON.hpp"
wsteenberg 0:ebf3f36f3a64 43 *
wsteenberg 0:ebf3f36f3a64 44 * DigitalOut led_green(LED1);
wsteenberg 0:ebf3f36f3a64 45 * DigitalOut led_orange(LED2);
wsteenberg 0:ebf3f36f3a64 46 * DigitalOut led_red(LED3);
wsteenberg 0:ebf3f36f3a64 47 * DigitalOut led_blue(LED4);
wsteenberg 0:ebf3f36f3a64 48 *
wsteenberg 0:ebf3f36f3a64 49 * TS_DISCO_F469NI ts_;
wsteenberg 0:ebf3f36f3a64 50 * LCD_DISCO_F469NI lcd_;
wsteenberg 0:ebf3f36f3a64 51 *
wsteenberg 0:ebf3f36f3a64 52 * int main()
wsteenberg 0:ebf3f36f3a64 53 * {
wsteenberg 0:ebf3f36f3a64 54 * led_green = 1; // Switch off all LEDs
wsteenberg 0:ebf3f36f3a64 55 * led_orange = 1;
wsteenberg 0:ebf3f36f3a64 56 * led_red = 1;
wsteenberg 0:ebf3f36f3a64 57 * led_blue = 1;
wsteenberg 0:ebf3f36f3a64 58 *
wsteenberg 0:ebf3f36f3a64 59 * lcd_.Clear(LCD_COLOR_WHITE); // Set LCD Background colour
wsteenberg 0:ebf3f36f3a64 60 *
wsteenberg 0:ebf3f36f3a64 61 * Button btn1(lcd_, ts_, 20, 100, 120, 60,
wsteenberg 0:ebf3f36f3a64 62 * LCD_COLOR_BLUE, LCD_COLOR_YELLOW, 1, "BUTTON1", Font20); // Define btn1 button
wsteenberg 0:ebf3f36f3a64 63 * btn1.Render(); // Draw btn1 button
wsteenberg 0:ebf3f36f3a64 64 *
wsteenberg 0:ebf3f36f3a64 65 * Button test(lcd_, ts_, 160, 100, 120, 60,
wsteenberg 0:ebf3f36f3a64 66 * LCD_COLOR_DARKBLUE, LCD_COLOR_GREEN, 1, "TEST", Font20); // Define test button
wsteenberg 0:ebf3f36f3a64 67 * test.Render(); // Draw test button
wsteenberg 0:ebf3f36f3a64 68 *
wsteenberg 0:ebf3f36f3a64 69 * Button show(lcd_, ts_, 300, 100, 120, 60,
wsteenberg 0:ebf3f36f3a64 70 * LCD_COLOR_BROWN, LCD_COLOR_GRAY, 1, "SHOW", Font20); // Define hide button
wsteenberg 0:ebf3f36f3a64 71 * show.Hide();
wsteenberg 0:ebf3f36f3a64 72 *
wsteenberg 0:ebf3f36f3a64 73 * Button hide(lcd_, ts_, 300, 100, 120, 60,
wsteenberg 0:ebf3f36f3a64 74 * LCD_COLOR_BROWN, LCD_COLOR_GRAY, 1, "HIDE", Font20); // Define hide button
wsteenberg 0:ebf3f36f3a64 75 * hide.Render(); // Draw hide button
wsteenberg 0:ebf3f36f3a64 76 *
wsteenberg 0:ebf3f36f3a64 77 * Button button3D(lcd_, ts_, 580, 100, 160, 60,
wsteenberg 0:ebf3f36f3a64 78 * LCD_COLOR_BLUE, LCD_COLOR_YELLOW, 2, "3D BUTTON", Font20); // Define button3D button
wsteenberg 0:ebf3f36f3a64 79 * button3D.Render(); // Draw 3Dbutton button
wsteenberg 0:ebf3f36f3a64 80 *
wsteenberg 0:ebf3f36f3a64 81 *
wsteenberg 0:ebf3f36f3a64 82 *
wsteenberg 0:ebf3f36f3a64 83 * lcd_.SetTextColor(LCD_COLOR_BLACK); // Set Text colour to Black
wsteenberg 0:ebf3f36f3a64 84 * lcd_.SetFont(&Font20); // Font size 20
wsteenberg 0:ebf3f36f3a64 85 * lcd_.DisplayStringAt(5, 5, (uint8_t *)"Button / Slider driver for DISCO_F469", LEFT_MODE); // Display main header text
wsteenberg 0:ebf3f36f3a64 86 *
wsteenberg 0:ebf3f36f3a64 87 * lcd_.SetFont(&Font16); // Font size 16
wsteenberg 0:ebf3f36f3a64 88 * lcd_.DisplayStringAt(5, 45, (uint8_t *)"Button example", LEFT_MODE); // Display secondary header text
wsteenberg 0:ebf3f36f3a64 89 *
wsteenberg 0:ebf3f36f3a64 90 *
wsteenberg 0:ebf3f36f3a64 91 * while (true) // Main program loop
wsteenberg 0:ebf3f36f3a64 92 * {
wsteenberg 0:ebf3f36f3a64 93 * led_green = 1; // Switch off all LEDs
wsteenberg 0:ebf3f36f3a64 94 * led_orange = 1;
wsteenberg 0:ebf3f36f3a64 95 * led_red = 1;
wsteenberg 0:ebf3f36f3a64 96 * led_blue = 1;
wsteenberg 0:ebf3f36f3a64 97 *
wsteenberg 0:ebf3f36f3a64 98 *
wsteenberg 0:ebf3f36f3a64 99 * if (btn1.Press()) // Check if btn1 button was touched and run instructions if true
wsteenberg 0:ebf3f36f3a64 100 * {
wsteenberg 0:ebf3f36f3a64 101 * lcd_.SetFont(&Font16);
wsteenberg 0:ebf3f36f3a64 102 * lcd_.SetTextColor(LCD_COLOR_BLACK);
wsteenberg 0:ebf3f36f3a64 103 * lcd_.DisplayStringAt(5, 200, (uint8_t *)"Button1 pressed - switch on orange LED", LEFT_MODE);
wsteenberg 0:ebf3f36f3a64 104 * led_orange = 0;
wsteenberg 0:ebf3f36f3a64 105 * wait(0.5);
wsteenberg 0:ebf3f36f3a64 106 * lcd_.DisplayStringAt(5, 200, (uint8_t *)" ", LEFT_MODE);
wsteenberg 0:ebf3f36f3a64 107 * } // End btn1 button instructions
wsteenberg 0:ebf3f36f3a64 108 *
wsteenberg 0:ebf3f36f3a64 109 * if (test.Press()) // Check if test button was touched and run instructions if true
wsteenberg 0:ebf3f36f3a64 110 * {
wsteenberg 0:ebf3f36f3a64 111 * lcd_.SetFont(&Font16);
wsteenberg 0:ebf3f36f3a64 112 * lcd_.SetTextColor(LCD_COLOR_BLACK);
wsteenberg 0:ebf3f36f3a64 113 * lcd_.DisplayStringAt(5, 200, (uint8_t *)"TEST pressed - switch on red LED", LEFT_MODE);
wsteenberg 0:ebf3f36f3a64 114 * led_red = 0;
wsteenberg 0:ebf3f36f3a64 115 * wait(0.5);
wsteenberg 0:ebf3f36f3a64 116 * lcd_.DisplayStringAt(5, 200, (uint8_t *)" ", LEFT_MODE);
wsteenberg 0:ebf3f36f3a64 117 *
wsteenberg 0:ebf3f36f3a64 118 * } // End test button instructions
wsteenberg 0:ebf3f36f3a64 119 *
wsteenberg 0:ebf3f36f3a64 120 * if (hide.Press()) // Check if hide button was touched and run instructions if true
wsteenberg 0:ebf3f36f3a64 121 * {
wsteenberg 0:ebf3f36f3a64 122 * lcd_.SetFont(&Font16);
wsteenberg 0:ebf3f36f3a64 123 * lcd_.SetTextColor(LCD_COLOR_BLACK);
wsteenberg 0:ebf3f36f3a64 124 * lcd_.DisplayStringAt(5, 200, (uint8_t *)"Hide pressed - Hide other buttons", LEFT_MODE);
wsteenberg 0:ebf3f36f3a64 125 * led_green = 0;
wsteenberg 0:ebf3f36f3a64 126 * btn1.Hide(); // Hide btn1 button
wsteenberg 0:ebf3f36f3a64 127 * test.Hide(); // Hide test button
wsteenberg 0:ebf3f36f3a64 128 * hide.Hide();
wsteenberg 0:ebf3f36f3a64 129 * show.Render();
wsteenberg 0:ebf3f36f3a64 130 * wait(0.5);
wsteenberg 0:ebf3f36f3a64 131 * lcd_.DisplayStringAt(5, 200, (uint8_t *)" ", LEFT_MODE);
wsteenberg 0:ebf3f36f3a64 132 * } // End hide button instructions
wsteenberg 0:ebf3f36f3a64 133 *
wsteenberg 0:ebf3f36f3a64 134 * if (show.Press()) // Check if hide button was touched and run instructions if true
wsteenberg 0:ebf3f36f3a64 135 * {
wsteenberg 0:ebf3f36f3a64 136 * lcd_.SetFont(&Font16);
wsteenberg 0:ebf3f36f3a64 137 * lcd_.SetTextColor(LCD_COLOR_BLACK);
wsteenberg 0:ebf3f36f3a64 138 * lcd_.DisplayStringAt(5, 200, (uint8_t *)"UNHIDE pressed - Restore other buttons", LEFT_MODE);
wsteenberg 0:ebf3f36f3a64 139 * led_blue = 0;
wsteenberg 0:ebf3f36f3a64 140 * wait(0.5);
wsteenberg 0:ebf3f36f3a64 141 * lcd_.DisplayStringAt(5, 200, (uint8_t *)" ", LEFT_MODE);
wsteenberg 0:ebf3f36f3a64 142 * show.Hide(); // Hide show button before drawing new button in same place
wsteenberg 0:ebf3f36f3a64 143 * hide.Render(); // Draw hide button after hiding button in same position
wsteenberg 0:ebf3f36f3a64 144 * btn1.Render(); // Draw btn1 button
wsteenberg 0:ebf3f36f3a64 145 * test.Render(); // Draw test button
wsteenberg 0:ebf3f36f3a64 146 *
wsteenberg 0:ebf3f36f3a64 147 * }
wsteenberg 0:ebf3f36f3a64 148 *
wsteenberg 0:ebf3f36f3a64 149 * if (button3D.Press()) // Check if 3Dbutton button was touched and run instructions if true
wsteenberg 0:ebf3f36f3a64 150 * {
wsteenberg 0:ebf3f36f3a64 151 * lcd_.SetFont(&Font16);
wsteenberg 0:ebf3f36f3a64 152 * lcd_.SetTextColor(LCD_COLOR_BLACK);
wsteenberg 0:ebf3f36f3a64 153 * lcd_.DisplayStringAt(5, 200, (uint8_t *)"3D Button pressed - switch on blue LED", LEFT_MODE);
wsteenberg 0:ebf3f36f3a64 154 * led_blue = 0;
wsteenberg 0:ebf3f36f3a64 155 * wait(0.5);
wsteenberg 0:ebf3f36f3a64 156 * lcd_.DisplayStringAt(5, 200, (uint8_t *)" ", LEFT_MODE);
wsteenberg 0:ebf3f36f3a64 157 * } // End 3D button instructions
wsteenberg 0:ebf3f36f3a64 158 *
wsteenberg 0:ebf3f36f3a64 159 * wait(0.02f);
wsteenberg 0:ebf3f36f3a64 160 * } // End Main program loop
wsteenberg 0:ebf3f36f3a64 161 * } // End Main program
wsteenberg 0:ebf3f36f3a64 162 * @endcode
wsteenberg 0:ebf3f36f3a64 163 */
wsteenberg 0:ebf3f36f3a64 164
wsteenberg 0:ebf3f36f3a64 165 class Button
wsteenberg 0:ebf3f36f3a64 166 {
wsteenberg 0:ebf3f36f3a64 167 public:
wsteenberg 0:ebf3f36f3a64 168
wsteenberg 0:ebf3f36f3a64 169 //! Constructor
wsteenberg 0:ebf3f36f3a64 170 Button(LCD_DISCO_F469NI &lcd, TS_DISCO_F469NI &ts,
wsteenberg 0:ebf3f36f3a64 171 uint16_t x, uint16_t y, uint16_t width, uint16_t height,
wsteenberg 0:ebf3f36f3a64 172 uint32_t buttonColour, uint32_t changeColour,
wsteenberg 0:ebf3f36f3a64 173 int style, const string label = "", sFONT &fonts = Font12,
wsteenberg 0:ebf3f36f3a64 174 uint32_t textColour = LCD_COLOR_WHITE)
wsteenberg 0:ebf3f36f3a64 175 : lcd_(lcd), ts_(ts), ButtonX_(x), ButtonY_(y), ButtonW_(width), ButtonH_(height),
wsteenberg 0:ebf3f36f3a64 176 BUTTON_COLOUR_(buttonColour), CHANGE_COLOUR_(changeColour),
wsteenberg 0:ebf3f36f3a64 177 STYLE_(style), LABEL_(label), FONTS_(&fonts), FONT_WIDTH_(fonts.Width),
wsteenberg 0:ebf3f36f3a64 178 FONT_HEIGHT_(fonts.Height), active_(true)
wsteenberg 0:ebf3f36f3a64 179 { Render(); }
wsteenberg 0:ebf3f36f3a64 180
wsteenberg 0:ebf3f36f3a64 181 /**
wsteenberg 0:ebf3f36f3a64 182 * @brief Draw Button.
wsteenberg 0:ebf3f36f3a64 183 *
wsteenberg 0:ebf3f36f3a64 184 */
wsteenberg 0:ebf3f36f3a64 185 void Render();
wsteenberg 0:ebf3f36f3a64 186
wsteenberg 0:ebf3f36f3a64 187 /**
wsteenberg 0:ebf3f36f3a64 188 * @brief Draw 3D Button.
wsteenberg 0:ebf3f36f3a64 189 *
wsteenberg 0:ebf3f36f3a64 190 */
wsteenberg 0:ebf3f36f3a64 191 void Render3D();
wsteenberg 0:ebf3f36f3a64 192
wsteenberg 0:ebf3f36f3a64 193 /**
wsteenberg 0:ebf3f36f3a64 194 * @brief Change colour of Button.
wsteenberg 0:ebf3f36f3a64 195 *
wsteenberg 0:ebf3f36f3a64 196 */
wsteenberg 0:ebf3f36f3a64 197 void Change();
wsteenberg 0:ebf3f36f3a64 198
wsteenberg 0:ebf3f36f3a64 199 /**
wsteenberg 0:ebf3f36f3a64 200 * @brief Change colour of 3D Button.
wsteenberg 0:ebf3f36f3a64 201 *
wsteenberg 0:ebf3f36f3a64 202 */
wsteenberg 0:ebf3f36f3a64 203 void Change3D();
wsteenberg 0:ebf3f36f3a64 204
wsteenberg 0:ebf3f36f3a64 205 /**
wsteenberg 0:ebf3f36f3a64 206 * @brief Hide Button.
wsteenberg 0:ebf3f36f3a64 207 *
wsteenberg 0:ebf3f36f3a64 208 */
wsteenberg 0:ebf3f36f3a64 209 void Hide();
wsteenberg 0:ebf3f36f3a64 210
wsteenberg 0:ebf3f36f3a64 211 /**
wsteenberg 0:ebf3f36f3a64 212 * @brief Check touch detected on Button.
wsteenberg 0:ebf3f36f3a64 213 *
wsteenberg 0:ebf3f36f3a64 214 */
wsteenberg 0:ebf3f36f3a64 215 bool Press();
wsteenberg 0:ebf3f36f3a64 216
wsteenberg 0:ebf3f36f3a64 217 /**
wsteenberg 0:ebf3f36f3a64 218 * @brief Check if touch is on Button.
wsteenberg 0:ebf3f36f3a64 219 *
wsteenberg 0:ebf3f36f3a64 220 */
wsteenberg 0:ebf3f36f3a64 221 bool ButtonBoundaryCheck();
wsteenberg 0:ebf3f36f3a64 222
wsteenberg 0:ebf3f36f3a64 223 /**
wsteenberg 0:ebf3f36f3a64 224 * @brief Check previous state of Button.
wsteenberg 0:ebf3f36f3a64 225 *
wsteenberg 0:ebf3f36f3a64 226 */
wsteenberg 0:ebf3f36f3a64 227 static TS_StateTypeDef GottenState()
wsteenberg 0:ebf3f36f3a64 228 { return state_; }
wsteenberg 0:ebf3f36f3a64 229
wsteenberg 0:ebf3f36f3a64 230 /**
wsteenberg 0:ebf3f36f3a64 231 * @brief Set or reset multi-touch.
wsteenberg 0:ebf3f36f3a64 232 *
wsteenberg 0:ebf3f36f3a64 233 */
wsteenberg 0:ebf3f36f3a64 234 static void SetMultiTouch(bool tf) { multiTouch = tf; }
wsteenberg 0:ebf3f36f3a64 235
wsteenberg 0:ebf3f36f3a64 236
wsteenberg 0:ebf3f36f3a64 237 private:
wsteenberg 0:ebf3f36f3a64 238
wsteenberg 0:ebf3f36f3a64 239 LCD_DISCO_F469NI &lcd_; // LCD
wsteenberg 0:ebf3f36f3a64 240 TS_DISCO_F469NI &ts_; // Touch
wsteenberg 0:ebf3f36f3a64 241
wsteenberg 0:ebf3f36f3a64 242 const uint16_t ButtonX_, ButtonY_, ButtonW_, ButtonH_;
wsteenberg 0:ebf3f36f3a64 243 const uint32_t BUTTON_COLOUR_; // Original colour
wsteenberg 0:ebf3f36f3a64 244 const uint32_t CHANGE_COLOUR_; // Colour to change to color
wsteenberg 0:ebf3f36f3a64 245 const int STYLE_; // Button style
wsteenberg 0:ebf3f36f3a64 246 const string LABEL_; // Button label
wsteenberg 0:ebf3f36f3a64 247 sFONT *const FONTS_; // Button Style
wsteenberg 0:ebf3f36f3a64 248 const uint16_t FONT_WIDTH_;
wsteenberg 0:ebf3f36f3a64 249 const uint16_t FONT_HEIGHT_;
wsteenberg 0:ebf3f36f3a64 250 bool active_; // Button active boolean
wsteenberg 0:ebf3f36f3a64 251
wsteenberg 0:ebf3f36f3a64 252 static TS_StateTypeDef state_;
wsteenberg 0:ebf3f36f3a64 253 static bool multiTouch;
wsteenberg 0:ebf3f36f3a64 254
wsteenberg 0:ebf3f36f3a64 255 /**
wsteenberg 0:ebf3f36f3a64 256 * @brief Disallow copy constructor and assignment operator.
wsteenberg 0:ebf3f36f3a64 257 *
wsteenberg 0:ebf3f36f3a64 258 */
wsteenberg 0:ebf3f36f3a64 259 Button(const Button&);
wsteenberg 0:ebf3f36f3a64 260 Button& operator=(const Button&);
wsteenberg 0:ebf3f36f3a64 261
wsteenberg 0:ebf3f36f3a64 262
wsteenberg 0:ebf3f36f3a64 263 };
wsteenberg 0:ebf3f36f3a64 264 #endif // F469_BUTTON_HPP