Slider class on LCD / Touch screen
Revision 0:89d154d0806c, committed 2017-04-07
- Comitter:
- jlpadiolleau
- Date:
- Fri Apr 07 11:40:27 2017 +0000
- Commit message:
- Hi
Changed in this revision
Slider.cpp | Show annotated file Show diff for this revision Revisions of this file |
Slider.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 89d154d0806c Slider.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Slider.cpp Fri Apr 07 11:40:27 2017 +0000 @@ -0,0 +1,78 @@ +#include "Slider.h" +#include <stdio.h> + +/* Constructor: define dimensions, positions and colors */ +Slider::Slider(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t c1, uint32_t c2) +{ + if(x<(maxWidth-w)) xpos=x; + else xpos=maxWidth-w; + if(y<(maxHeight-h)) ypos=y; + else ypos=maxHeight-h; + if(w<(maxWidth)) width=w; + else width=maxWidth; + if(h<(maxHeight)) height=h; + else height=maxHeight; + color1=c1; + color2=c2; + value=128; +} + +/* Draw the slider */ +void Slider::Show() +{ + char text[10]; + uint32_t OldTextColor; + uint32_t OldBackColor; + sFONT *font; + + // Save previous colors and font + OldTextColor=BSP_LCD_GetTextColor(); + OldBackColor=BSP_LCD_GetBackColor(); + font = BSP_LCD_GetFont(); + + // Draw Slider + BSP_LCD_SetTextColor(color2); + BSP_LCD_FillRect(xpos,ypos,width,(height*(255-value))/255); + BSP_LCD_SetTextColor(color1); + BSP_LCD_DrawRect(xpos,ypos,width,(height*(255-value))/255); + BSP_LCD_FillRect(xpos,ypos+(height*(255-value))/255,width,(height*value)/255); + + //Draw text value + BSP_LCD_SetFont(&Font16); + BSP_LCD_SetBackColor(color2); + sprintf(text,"%3d",value); + BSP_LCD_DisplayStringAt(xpos,ypos+height,(uint8_t*)text,LEFT_MODE); + + //Restore colors and fonts + BSP_LCD_SetTextColor(OldTextColor); + BSP_LCD_SetBackColor(OldBackColor); + BSP_LCD_SetFont(font); +} + +/* Move the slider if TouchScreen is in area*/ +void Slider::Move(uint16_t x, uint16_t y) +{ + if ((x>=xpos) && (x<=xpos+width) && (y>=ypos) && (y<=ypos+height)) { //(y>=ypos+value*height/100-10) && (y<=ypos+value*height/100+10)) + value=255-(y-ypos)*255/height; + Show(); + } +} + +/* read the slider value */ +uint16_t Slider::GetValue() +{ + return value; +} + +/* write the slider value */ +void Slider::SetValue(uint16_t v) +{ + if (v<=255) value=v; + else value=255; + Show(); +} + +/* Destructor */ +Slider::~Slider() +{ +}
diff -r 000000000000 -r 89d154d0806c Slider.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Slider.h Fri Apr 07 11:40:27 2017 +0000 @@ -0,0 +1,26 @@ +#include "mbed.h" +#include "stm32746g_discovery_lcd.h" + +#define maxWidth 480 +#define maxHeight 272 + +class Slider +{ +public: + Slider(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t c1, uint32_t c2); + void Move(uint16_t x, uint16_t y); + uint16_t GetValue(void); + void SetValue(uint16_t v); + void Show(void); + ~Slider(void); + +private: + uint16_t xpos; + uint16_t ypos; + uint16_t width; + uint16_t height; + uint16_t value; + uint32_t color1; + uint32_t color2; +}; + \ No newline at end of file