
Version1
Dependencies: BSP_DISCO_F746NG DHT22
button.cpp@0:d60753bdf6d7, 2020-06-22 (annotated)
- Committer:
- antoinnneee
- Date:
- Mon Jun 22 15:16:28 2020 +0000
- Revision:
- 0:d60753bdf6d7
FirstCommit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
antoinnneee | 0:d60753bdf6d7 | 1 | #include "button.h" |
antoinnneee | 0:d60753bdf6d7 | 2 | #include "mbed.h" |
antoinnneee | 0:d60753bdf6d7 | 3 | |
antoinnneee | 0:d60753bdf6d7 | 4 | #include "stm32746g_discovery_lcd.h" |
antoinnneee | 0:d60753bdf6d7 | 5 | |
antoinnneee | 0:d60753bdf6d7 | 6 | int Button::strlen(uint8_t *str) |
antoinnneee | 0:d60753bdf6d7 | 7 | { |
antoinnneee | 0:d60753bdf6d7 | 8 | int i = 0; |
antoinnneee | 0:d60753bdf6d7 | 9 | while(str[i]) |
antoinnneee | 0:d60753bdf6d7 | 10 | i++; |
antoinnneee | 0:d60753bdf6d7 | 11 | return i; |
antoinnneee | 0:d60753bdf6d7 | 12 | } |
antoinnneee | 0:d60753bdf6d7 | 13 | |
antoinnneee | 0:d60753bdf6d7 | 14 | Button::Button(int x, int y, int width, int height, uint32_t bgColor, uint32_t borderWidth) |
antoinnneee | 0:d60753bdf6d7 | 15 | : m_x(x), m_y(y), m_width(width), m_height(height), m_bgColor(bgColor), m_borderWidth(borderWidth) |
antoinnneee | 0:d60753bdf6d7 | 16 | { |
antoinnneee | 0:d60753bdf6d7 | 17 | } |
antoinnneee | 0:d60753bdf6d7 | 18 | |
antoinnneee | 0:d60753bdf6d7 | 19 | bool Button::contain(int x, int y) |
antoinnneee | 0:d60753bdf6d7 | 20 | { |
antoinnneee | 0:d60753bdf6d7 | 21 | return (x > m_x && x < m_x + m_width && y > m_y && y < m_y + m_height); |
antoinnneee | 0:d60753bdf6d7 | 22 | } |
antoinnneee | 0:d60753bdf6d7 | 23 | |
antoinnneee | 0:d60753bdf6d7 | 24 | void Button::draw() |
antoinnneee | 0:d60753bdf6d7 | 25 | { |
antoinnneee | 0:d60753bdf6d7 | 26 | int x = m_x; |
antoinnneee | 0:d60753bdf6d7 | 27 | while (x < m_x + m_width) { |
antoinnneee | 0:d60753bdf6d7 | 28 | int y = m_y; |
antoinnneee | 0:d60753bdf6d7 | 29 | while (y < m_y + m_height) { |
antoinnneee | 0:d60753bdf6d7 | 30 | if (y - m_y < m_borderWidth) |
antoinnneee | 0:d60753bdf6d7 | 31 | BSP_LCD_DrawPixel(x, y, m_borderColor); |
antoinnneee | 0:d60753bdf6d7 | 32 | else if ( (m_y + m_height) - y <= m_borderWidth) |
antoinnneee | 0:d60753bdf6d7 | 33 | BSP_LCD_DrawPixel(x, y, m_borderColor); |
antoinnneee | 0:d60753bdf6d7 | 34 | else if (x - m_x < m_borderWidth) |
antoinnneee | 0:d60753bdf6d7 | 35 | BSP_LCD_DrawPixel(x, y, m_borderColor); |
antoinnneee | 0:d60753bdf6d7 | 36 | else if ( (m_x + m_width) - x <= m_borderWidth) |
antoinnneee | 0:d60753bdf6d7 | 37 | BSP_LCD_DrawPixel(x, y, m_borderColor); |
antoinnneee | 0:d60753bdf6d7 | 38 | else |
antoinnneee | 0:d60753bdf6d7 | 39 | BSP_LCD_DrawPixel(x, y, m_bgColor); |
antoinnneee | 0:d60753bdf6d7 | 40 | y++; |
antoinnneee | 0:d60753bdf6d7 | 41 | } |
antoinnneee | 0:d60753bdf6d7 | 42 | x++; |
antoinnneee | 0:d60753bdf6d7 | 43 | } |
antoinnneee | 0:d60753bdf6d7 | 44 | BSP_LCD_SetTextColor(m_textColor); |
antoinnneee | 0:d60753bdf6d7 | 45 | BSP_LCD_SetBackColor(m_bgColor); |
antoinnneee | 0:d60753bdf6d7 | 46 | BSP_LCD_DisplayStringAt((m_x + m_width/2)- 8*(strlen(m_text)/2), (m_y + m_height/2) - 5, (uint8_t *)&m_text, LEFT_MODE); |
antoinnneee | 0:d60753bdf6d7 | 47 | } |
antoinnneee | 0:d60753bdf6d7 | 48 | |
antoinnneee | 0:d60753bdf6d7 | 49 | void Button::setText(const char *str, uint32_t textColor){ |
antoinnneee | 0:d60753bdf6d7 | 50 | m_textColor = textColor; |
antoinnneee | 0:d60753bdf6d7 | 51 | sprintf((char*)m_text, str); |
antoinnneee | 0:d60753bdf6d7 | 52 | } |