Updated fileheaders and styled files using astyle.

This commit is contained in:
t-moe
2015-06-08 11:00:52 +02:00
parent 7e61a129a3
commit fceb2d15b2
63 changed files with 6045 additions and 4691 deletions

View File

@@ -1,154 +1,188 @@
#include "tft.h"
#include "touch.h"
#include "button.h"
#include <string.h>
/* The Idea is as follows:
* When the user add's a button we create a touch area for that region and wait for PEN_DOWN events.
* Once the user puts the pen down in this area we'll redraw the button with different shadows (feedback)
* and we'll now wait on PEN_UP or PEN_LEAVE events.
* If the user takes the pen away while in the area (PEN_UP), we call the provided user callback
* Otherwise (PEN_LEAVE) we only restore the initial shadows
*/
/* Possible improvements:
* Move the button by 1 pixel while he is pressed, to create a "full 3d" experience
* Add events for the case when the button is pressed for a long time, without release
*/
//Method to calculate the shadow colors used to create the "3d" effect
void calculate_shadows(uint16_t bgcolor, uint16_t* light_shadow, uint16_t* dark_shadow) {
#define BRIGHTNESS_VAL 3 //How much the Brightness is in/decreased for button shadows (3 -> Add/Subtract 1/3 off Full Value)
uint16_t c_light,c_dark; //c_light and c_dark will be filled with a lighter and a darker color as the background color (for the shadows)
uint8_t r,g,b;
//separate the channels of the 16-bit rgb565 color
r=(bgcolor&0xF800)>>11;
g=(bgcolor&0x07E0)>>5;
b=(bgcolor&0x001F)>>0;
//For the light shadow color:
if((r + 0x1F/BRIGHTNESS_VAL) > 0x1F) //Adding one third would exceed the maximum of the red channel
c_light=0xF800; //Use full red
else //adding one third to the red channel is fine
c_light=(r+0x1F/BRIGHTNESS_VAL)<<11; //Use same red as in the background, but add one third
if((g + 0x3F/BRIGHTNESS_VAL) > 0x3F) //same for the green channel
c_light|=0x07E0;
else
c_light|=(g+0x3F/BRIGHTNESS_VAL)<<5;
if((b + 0x1F/BRIGHTNESS_VAL) > 0x1F) //and the blue channel
c_light|=0x0018;
else
c_light|=(b+0x1F/BRIGHTNESS_VAL)<<0;
//For the dark shadow color
if(r > (0x1F/BRIGHTNESS_VAL)) //Subtracting one third would NOT exceed the minimum of the red channel
c_dark=(r-0x1F/BRIGHTNESS_VAL)<<11; //Use same red as in the background, but subtract one third
else //Subtracting one third would give us a number below zero
c_dark=0x0000; //use no red channel
if(g > (0x3F/BRIGHTNESS_VAL)) //Same for the green channel
c_dark|=(g-0x3F/BRIGHTNESS_VAL)<<5;
if(b > (0x1F/BRIGHTNESS_VAL)) //and the blue channel
c_dark|=(b-0x1F/BRIGHTNESS_VAL)<<0;
//Assign the calculated shadows to out parameters
if(light_shadow!=NULL) *light_shadow = c_light;
if(dark_shadow!=NULL) *dark_shadow = c_dark;
}
//Callback which is called when the user touches the touch-area we created for the button
void buttons_cb(void* touchArea, TOUCH_ACTION triggeredAction)
{
TOUCH_AREA_STRUCT * area = (TOUCH_AREA_STRUCT*)touchArea;
BUTTON_STRUCT* button = (BUTTON_STRUCT*)touchArea;
uint16_t c_light,c_dark; //c_light and c_dark will be filled with a lighter and a darker color as the background color (for the shadows)
calculate_shadows(button->bgcolor,&c_light,&c_dark);
switch(triggeredAction)
{
case PEN_DOWN: //If the user touches the area for the "first time"
area->hookedActions=PEN_UP|PEN_LEAVE; //for the future we only want PEN_UP and PEN_LEAVE events
//Draw shadows
tft_draw_line(button->base.x1+1,button->base.y1,button->base.x2-1,button->base.y1,c_dark); //North
tft_draw_line(button->base.x1,button->base.y1+1,button->base.x1,button->base.y2-1,c_dark);//West
tft_draw_line(button->base.x1+1,button->base.y2,button->base.x2-1,button->base.y2,c_light); //South
tft_draw_line(button->base.x2,button->base.y1+1,button->base.x2,button->base.y2-1,c_light); //East
break;
case PEN_UP: //If the user took the pen away, while in the area (=button pressed!)
case PEN_LEAVE: //or the user "slided out" of the area
area->hookedActions=PEN_DOWN; //for the future we only want PEN_DOWN events
//Draw inverse shadows
tft_draw_line(button->base.x1+1,button->base.y1,button->base.x2-1,button->base.y1,c_light); //North
tft_draw_line(button->base.x1,button->base.y1+1,button->base.x1,button->base.y2-1,c_light);//West
tft_draw_line(button->base.x1+1,button->base.y2,button->base.x2-1,button->base.y2,c_dark); //South
tft_draw_line(button->base.x2,button->base.y1+1,button->base.x2,button->base.y2-1,c_dark); //East
if(triggeredAction==PEN_UP && button->callback!=NULL) //If the button got "pressed" instead of left, and the user provided a callback
button->callback(button); //execute the user callback
break;
default:break;
}
}
bool gui_button_add(BUTTON_STRUCT* button)
{
if(touch_have_empty(1)) //Check if the touch module can handle one additional area
{
//Calculate width and height of the button text
unsigned int strwidth=tft_font_width(button->font)*strlen(button->text);
unsigned char strheight=tft_font_height(button->font);
button->base.hookedActions=PEN_DOWN; //At first we are interested in PEN_DOWN events
button->base.callback = buttons_cb; //Use our own callback for the touch area events
if(button->base.x2==AUTO) { //The user wants us to calculate the button width automatically
//Use string width + half of a character width as button width
button->base.x2= button->base.x1 -1 + strwidth+(tft_font_width(button->font)/2);
} else if((button->base.x2-button->base.x1+1)<(strwidth+2)) { //the provided width is too small to fit the entire text
return false; //report error
}
if(button->base.y2==AUTO) { //The user wants us to calculate the button height automatically
//Use one and a half character heights as button height
button->base.y2=button->base.y1 -1 +strheight+(strheight/2);
} else if((button->base.y2-button->base.y1+1)<(strheight+2)) { //the provided height is too small to fit the text
return false;
}
gui_button_redraw(button); //call the redraw method, which will take care of drawing the entire button
return touch_register_area(&button->base); //Register the touch area and receive events for this button, from now on
}
return false; //no more touch areas left
}
void gui_button_redraw(BUTTON_STRUCT* button)
{
//Calculate text dimensions and shadow colors
unsigned int strwidth=tft_font_width(button->font)*strlen(button->text);
unsigned char strheight=tft_font_height(button->font);
uint16_t c_light,c_dark;
calculate_shadows(button->bgcolor,&c_light,&c_dark);
//Draw the background and the 4 lines (shadow colors)
tft_fill_rectangle(button->base.x1+1,button->base.y1+1,button->base.x2-1,button->base.y2-1,button->bgcolor);
tft_draw_line(button->base.x1+1,button->base.y1,button->base.x2-1,button->base.y1,c_light); //North
tft_draw_line(button->base.x1,button->base.y1+1,button->base.x1,button->base.y2-1,c_light);//West
tft_draw_line(button->base.x1+1,button->base.y2,button->base.x2-1,button->base.y2,c_dark); //South
tft_draw_line(button->base.x2,button->base.y1+1,button->base.x2,button->base.y2-1,c_dark); //East
//Draw the text
tft_print_line(button->base.x1+(button->base.x2-button->base.x1+1-strwidth)/2,button->base.y1+(button->base.y2-button->base.y1+1-strheight)/2,button->txtcolor,button->bgcolor,button->font,button->text);
}
void gui_button_remove(BUTTON_STRUCT* button)
{
//We only need to unregister the touch area, as we have not allocated anything else
touch_unregister_area((TOUCH_AREA_STRUCT*)button);
}
/**************************************************************************************************************************************
* Project: discoverpixy
* Website: https://github.com/t-moe/discoverpixy
* Authors: Aaron Schmocker, Timo Lang
* Institution: BFH Bern University of Applied Sciences
* File: common/gui/button.c
*
* Version History:
* Date Autor Email SHA Changes
* 2015-04-27 timolang@gmail.com 7c9eabc Added button support.
* 2015-05-17 timolang@gmail.com 2d46336 Improved comments in implementation of button, checkbox, numupdown, tft, touch and screen modules/submodules.
*
**************************************************************************************************************************************/
#include "tft.h"
#include "touch.h"
#include "button.h"
#include <string.h>
/* The Idea is as follows:
* When the user add's a button we create a touch area for that region and wait for PEN_DOWN events.
* Once the user puts the pen down in this area we'll redraw the button with different shadows (feedback)
* and we'll now wait on PEN_UP or PEN_LEAVE events.
* If the user takes the pen away while in the area (PEN_UP), we call the provided user callback
* Otherwise (PEN_LEAVE) we only restore the initial shadows
*/
/* Possible improvements:
* Move the button by 1 pixel while he is pressed, to create a "full 3d" experience
* Add events for the case when the button is pressed for a long time, without release
*/
//Method to calculate the shadow colors used to create the "3d" effect
void calculate_shadows(uint16_t bgcolor, uint16_t* light_shadow, uint16_t* dark_shadow)
{
#define BRIGHTNESS_VAL 3 //How much the Brightness is in/decreased for button shadows (3 -> Add/Subtract 1/3 off Full Value)
uint16_t c_light, c_dark; //c_light and c_dark will be filled with a lighter and a darker color as the background color (for the shadows)
uint8_t r, g, b;
//separate the channels of the 16-bit rgb565 color
r = (bgcolor & 0xF800) >> 11;
g = (bgcolor & 0x07E0) >> 5;
b = (bgcolor & 0x001F) >> 0;
//For the light shadow color:
if ((r + 0x1F / BRIGHTNESS_VAL) > 0x1F) { //Adding one third would exceed the maximum of the red channel
c_light = 0xF800; //Use full red
} else { //adding one third to the red channel is fine
c_light = (r + 0x1F / BRIGHTNESS_VAL) << 11; //Use same red as in the background, but add one third
}
if ((g + 0x3F / BRIGHTNESS_VAL) > 0x3F) { //same for the green channel
c_light |= 0x07E0;
} else {
c_light |= (g + 0x3F / BRIGHTNESS_VAL) << 5;
}
if ((b + 0x1F / BRIGHTNESS_VAL) > 0x1F) { //and the blue channel
c_light |= 0x0018;
} else {
c_light |= (b + 0x1F / BRIGHTNESS_VAL) << 0;
}
//For the dark shadow color
if (r > (0x1F / BRIGHTNESS_VAL)) { //Subtracting one third would NOT exceed the minimum of the red channel
c_dark = (r - 0x1F / BRIGHTNESS_VAL) << 11; //Use same red as in the background, but subtract one third
} else { //Subtracting one third would give us a number below zero
c_dark = 0x0000; //use no red channel
}
if (g > (0x3F / BRIGHTNESS_VAL)) { //Same for the green channel
c_dark |= (g - 0x3F / BRIGHTNESS_VAL) << 5;
}
if (b > (0x1F / BRIGHTNESS_VAL)) { //and the blue channel
c_dark |= (b - 0x1F / BRIGHTNESS_VAL) << 0;
}
//Assign the calculated shadows to out parameters
if (light_shadow != NULL) {
*light_shadow = c_light;
}
if (dark_shadow != NULL) {
*dark_shadow = c_dark;
}
}
//Callback which is called when the user touches the touch-area we created for the button
void buttons_cb(void* touchArea, TOUCH_ACTION triggeredAction)
{
TOUCH_AREA_STRUCT* area = (TOUCH_AREA_STRUCT*)touchArea;
BUTTON_STRUCT* button = (BUTTON_STRUCT*)touchArea;
uint16_t c_light, c_dark; //c_light and c_dark will be filled with a lighter and a darker color as the background color (for the shadows)
calculate_shadows(button->bgcolor, &c_light, &c_dark);
switch (triggeredAction) {
case PEN_DOWN: //If the user touches the area for the "first time"
area->hookedActions = PEN_UP | PEN_LEAVE; //for the future we only want PEN_UP and PEN_LEAVE events
//Draw shadows
tft_draw_line(button->base.x1 + 1, button->base.y1, button->base.x2 - 1, button->base.y1, c_dark); //North
tft_draw_line(button->base.x1, button->base.y1 + 1, button->base.x1, button->base.y2 - 1, c_dark); //West
tft_draw_line(button->base.x1 + 1, button->base.y2, button->base.x2 - 1, button->base.y2, c_light); //South
tft_draw_line(button->base.x2, button->base.y1 + 1, button->base.x2, button->base.y2 - 1, c_light); //East
break;
case PEN_UP: //If the user took the pen away, while in the area (=button pressed!)
case PEN_LEAVE: //or the user "slided out" of the area
area->hookedActions = PEN_DOWN; //for the future we only want PEN_DOWN events
//Draw inverse shadows
tft_draw_line(button->base.x1 + 1, button->base.y1, button->base.x2 - 1, button->base.y1, c_light); //North
tft_draw_line(button->base.x1, button->base.y1 + 1, button->base.x1, button->base.y2 - 1, c_light); //West
tft_draw_line(button->base.x1 + 1, button->base.y2, button->base.x2 - 1, button->base.y2, c_dark); //South
tft_draw_line(button->base.x2, button->base.y1 + 1, button->base.x2, button->base.y2 - 1, c_dark); //East
if (triggeredAction == PEN_UP && button->callback != NULL) { //If the button got "pressed" instead of left, and the user provided a callback
button->callback(button); //execute the user callback
}
break;
default:
break;
}
}
bool gui_button_add(BUTTON_STRUCT* button)
{
if (touch_have_empty(1)) { //Check if the touch module can handle one additional area
//Calculate width and height of the button text
unsigned int strwidth = tft_font_width(button->font) * strlen(button->text);
unsigned char strheight = tft_font_height(button->font);
button->base.hookedActions = PEN_DOWN; //At first we are interested in PEN_DOWN events
button->base.callback = buttons_cb; //Use our own callback for the touch area events
if (button->base.x2 == AUTO) { //The user wants us to calculate the button width automatically
//Use string width + half of a character width as button width
button->base.x2 = button->base.x1 - 1 + strwidth + (tft_font_width(button->font) / 2);
} else if ((button->base.x2 - button->base.x1 + 1) < (strwidth + 2)) { //the provided width is too small to fit the entire text
return false; //report error
}
if (button->base.y2 == AUTO) { //The user wants us to calculate the button height automatically
//Use one and a half character heights as button height
button->base.y2 = button->base.y1 - 1 + strheight + (strheight / 2);
} else if ((button->base.y2 - button->base.y1 + 1) < (strheight + 2)) { //the provided height is too small to fit the text
return false;
}
gui_button_redraw(button); //call the redraw method, which will take care of drawing the entire button
return touch_register_area(&button->base); //Register the touch area and receive events for this button, from now on
}
return false; //no more touch areas left
}
void gui_button_redraw(BUTTON_STRUCT* button)
{
//Calculate text dimensions and shadow colors
unsigned int strwidth = tft_font_width(button->font) * strlen(button->text);
unsigned char strheight = tft_font_height(button->font);
uint16_t c_light, c_dark;
calculate_shadows(button->bgcolor, &c_light, &c_dark);
//Draw the background and the 4 lines (shadow colors)
tft_fill_rectangle(button->base.x1 + 1, button->base.y1 + 1, button->base.x2 - 1, button->base.y2 - 1, button->bgcolor);
tft_draw_line(button->base.x1 + 1, button->base.y1, button->base.x2 - 1, button->base.y1, c_light); //North
tft_draw_line(button->base.x1, button->base.y1 + 1, button->base.x1, button->base.y2 - 1, c_light); //West
tft_draw_line(button->base.x1 + 1, button->base.y2, button->base.x2 - 1, button->base.y2, c_dark); //South
tft_draw_line(button->base.x2, button->base.y1 + 1, button->base.x2, button->base.y2 - 1, c_dark); //East
//Draw the text
tft_print_line(button->base.x1 + (button->base.x2 - button->base.x1 + 1 - strwidth) / 2, button->base.y1 + (button->base.y2 - button->base.y1 + 1 - strheight) / 2, button->txtcolor, button->bgcolor, button->font, button->text);
}
void gui_button_remove(BUTTON_STRUCT* button)
{
//We only need to unregister the touch area, as we have not allocated anything else
touch_unregister_area((TOUCH_AREA_STRUCT*)button);
}

View File

@@ -1,71 +1,89 @@
#ifndef BUTTON_H
#define BUTTON_H
#include "touch.h"
/**
* @defgroup gui Gui
* The Gui Module
*/
/*@{*/
/**
* @defgroup button Button
* The Button Gui-Element is a clickable, rectangular box with a label inside.
* When it is pressed and released you will be notified via the provided callback.
*/
/*@}*/
/**
* @addtogroup button
*/
/*@{*/
/**
* Prototype for Event Listeners (called when the button is pressed)
* \note You should NOT execute long running things in this callback nor should you update the gui. But you can call gui_screen_navigate() for instance.
* @param button The pointer to the BUTTON_STRUCT where to corresponding Button was pressed
*/
typedef void (*BUTTON_CALLBACK)(void *button);
/**
* Structure to configure the Button
*/
typedef struct {
TOUCH_AREA_STRUCT base; //!< Basic geometry of the button. You only need to set the x1, y1, x2, y2 members of this struct.
uint16_t bgcolor; //!< The 16-bit background color of the button
BUTTON_CALLBACK callback; //!< Callback which is executed when the button is pressed
uint16_t txtcolor; //!< The 16-bit text color
uint8_t font; //!< The number of the font to use
const char *text; //!< The label of the button
} BUTTON_STRUCT;
#define AUTO 0 //!< Use this value instead of x2, y2 in the BUTTON_STRUCT to autocalculate the button width/height
/**
* Adds a button. Your Callback will be called from now on, if the button was pressed
* @param button A Pointer to the preinitialized BUTTON_STRUCT
* @return true on success
*/
bool gui_button_add(BUTTON_STRUCT* button);
/**
* Removes the button. You will no longer receive events for this button. This function will not overdraw the region where the button was located.
* @param button A Pointer to the BUTTON_STRUCT
*/
void gui_button_remove(BUTTON_STRUCT* button);
/**
* Redraws the button. Call this method if you have to redraw the entire screen or if you want to draw a button on top of an image.
* @param button A Pointer to the BUTTON_STRUCT
*/
void gui_button_redraw(BUTTON_STRUCT* button);
/*@}*/
#endif /* BUTTON_H */
/**************************************************************************************************************************************
* Project: discoverpixy
* Website: https://github.com/t-moe/discoverpixy
* Authors: Aaron Schmocker, Timo Lang
* Institution: BFH Bern University of Applied Sciences
* File: common/gui/button.h
*
* Version History:
* Date Autor Email SHA Changes
* 2015-04-27 timolang@gmail.com 7c9eabc Added button support.
* 2015-04-27 timolang@gmail.com cf72baa Introduced a Screen (sub) module and divided app into multiple screens.
* 2015-05-11 timolang@gmail.com 08d9fe0 More work on doxygen module structure
* 2015-05-12 timolang@gmail.com 1402598 Added doxygen stuff for button module and some minor changes to touch, screen_main and tft module.
* 2015-05-15 timolang@gmail.com 9a16865 Added doxgen comments to filesyste, checkbox, numupdown and screen module. And some minor changes to the other modules.
* 2015-05-17 timolang@gmail.com 2d46336 Improved comments in implementation of button, checkbox, numupdown, tft, touch and screen modules/submodules.
*
**************************************************************************************************************************************/
#ifndef BUTTON_H
#define BUTTON_H
#include "touch.h"
/**
* @defgroup gui Gui
* The Gui Module
*/
/*@{*/
/**
* @defgroup button Button
* The Button Gui-Element is a clickable, rectangular box with a label inside.
* When it is pressed and released you will be notified via the provided callback.
*/
/*@}*/
/**
* @addtogroup button
*/
/*@{*/
/**
* Prototype for Event Listeners (called when the button is pressed)
* \note You should NOT execute long running things in this callback nor should you update the gui. But you can call gui_screen_navigate() for instance.
* @param button The pointer to the BUTTON_STRUCT where to corresponding Button was pressed
*/
typedef void (*BUTTON_CALLBACK)(void* button);
/**
* Structure to configure the Button
*/
typedef struct {
TOUCH_AREA_STRUCT base; //!< Basic geometry of the button. You only need to set the x1, y1, x2, y2 members of this struct.
uint16_t bgcolor; //!< The 16-bit background color of the button
BUTTON_CALLBACK callback; //!< Callback which is executed when the button is pressed
uint16_t txtcolor; //!< The 16-bit text color
uint8_t font; //!< The number of the font to use
const char* text; //!< The label of the button
} BUTTON_STRUCT;
#define AUTO 0 //!< Use this value instead of x2, y2 in the BUTTON_STRUCT to autocalculate the button width/height
/**
* Adds a button. Your Callback will be called from now on, if the button was pressed
* @param button A Pointer to the preinitialized BUTTON_STRUCT
* @return true on success
*/
bool gui_button_add(BUTTON_STRUCT* button);
/**
* Removes the button. You will no longer receive events for this button. This function will not overdraw the region where the button was located.
* @param button A Pointer to the BUTTON_STRUCT
*/
void gui_button_remove(BUTTON_STRUCT* button);
/**
* Redraws the button. Call this method if you have to redraw the entire screen or if you want to draw a button on top of an image.
* @param button A Pointer to the BUTTON_STRUCT
*/
void gui_button_redraw(BUTTON_STRUCT* button);
/*@}*/
#endif /* BUTTON_H */

View File

@@ -1,121 +1,144 @@
#include "tft.h"
#include "touch.h"
#include "checkbox.h"
#include <stdio.h>
/* The idea is as follows:
* When the user creates a checkbox we create a touch area for that region and wait for PEN_DOWN events.
* Once the user puts the pen down in this area we'll redraw the checkbox with different shadows (feedback)
* and we'll now wait on PEN_UP or PEN_LEAVE events.
* If the user takes the pen away while in the area (PEN_UP), we toggle the checkbox and we call the provided user callback
* Otherwise (PEN_LEAVE) we only restore the initial shadows
*/
#define ACTIVE_COLOR RGB(251,208,123) //shadow color (inside of border)
#define BORDER_COLOR RGB(29,82,129) //1px border color
#define BACKGROUND_COLOR WHITE //Background color
//Callback which is called when the user touches the touch-area we created for the checkbox
void checkboxes_cb(void* touchArea, TOUCH_ACTION triggeredAction)
{
TOUCH_AREA_STRUCT * area = (TOUCH_AREA_STRUCT*)touchArea;
CHECKBOX_STRUCT* checkbox = (CHECKBOX_STRUCT*)touchArea;
switch(triggeredAction)
{
case PEN_DOWN: //If the user touches the area for the "first time"
area->hookedActions=PEN_UP|PEN_LEAVE; //for the future we only want PEN_UP and PEN_LEAVE events
//Draw active shadows
tft_draw_rectangle(checkbox->base.x1+1,checkbox->base.y1+1,checkbox->base.x2-1,checkbox->base.y2-1,ACTIVE_COLOR);
tft_draw_rectangle(checkbox->base.x1+2,checkbox->base.y1+2,checkbox->base.x2-2,checkbox->base.y2-2,ACTIVE_COLOR);
break;
case PEN_UP: //If the user took the pen away, while in the area (=toggle checkbox!)
checkbox->checked=!checkbox->checked; //Toggle checkbox state
gui_checkbox_update(checkbox); //redraw/overdraw tickmark
if(checkbox->callback!=NULL) { //The user provided a callback
checkbox->callback(checkbox,checkbox->checked); //Call the provided callback with the new checked state
}
// no break statement here!
case PEN_LEAVE: //if the user "slided out" of the area
area->hookedActions=PEN_DOWN; //for the future we only want PEN_DOWN events
//Draw inactive shadows
tft_draw_rectangle(checkbox->base.x1+1,checkbox->base.y1+1,checkbox->base.x2-1,checkbox->base.y2-1,BACKGROUND_COLOR);
tft_draw_rectangle(checkbox->base.x1+2,checkbox->base.y1+2,checkbox->base.x2-2,checkbox->base.y2-2,BACKGROUND_COLOR);
break;
default:break;
}
}
bool gui_checkbox_add(CHECKBOX_STRUCT* checkbox)
{
if(touch_have_empty(1)) //Check if the touch module can handle one additional area
{
unsigned char size=0;
checkbox->base.hookedActions=PEN_DOWN; //At first we are interested in PEN_DOWN events
checkbox->base.callback = checkboxes_cb; //Use our own callback for the touch area events
//Check the size of the checkbox
if(checkbox->base.x2>checkbox->base.x1)
size = checkbox->base.x2 - checkbox->base.x1; //use width a as size
if(checkbox->base.y2>checkbox->base.y1)
{
if((checkbox->base.y2 - checkbox->base.y1)>size) //height is larger than size
size = checkbox->base.y2 - checkbox->base.y1; //use height as size
}
if(size==0) { //no size found (maybe swap x2 and x1 or y2 and y1 ?)
return false; //signal error
}
if((size&0x01)) //the size is an odd number
size++; //make size an even number
//Correct x2,y2 so that the checkbox is quadratic
checkbox->base.x2 = checkbox->base.x1 + size;
checkbox->base.y2 = checkbox->base.y1 + size;
gui_checkbox_redraw(checkbox);//Call redraw method, which will take care of the drawing of the entire checkbox
return touch_register_area(&checkbox->base); //Register the touch area and receive events for this checkbox, from now on
}
return false; //no more touch areas left
}
void gui_checkbox_redraw(CHECKBOX_STRUCT* checkbox)
{
//Draw background and border
tft_fill_rectangle(checkbox->base.x1+1,checkbox->base.y1+1,checkbox->base.x2-1,checkbox->base.y2-1,BACKGROUND_COLOR);
tft_draw_rectangle(checkbox->base.x1,checkbox->base.y1,checkbox->base.x2,checkbox->base.y2,BORDER_COLOR);
if(checkbox->checked) { //checkbox is currently checked
gui_checkbox_update(checkbox); //Call update method which will draw the tickmark
}
}
void gui_checkbox_remove(CHECKBOX_STRUCT* checkbox)
{
//We only need to unregister the touch area, as we have not allocated anything else
touch_unregister_area((TOUCH_AREA_STRUCT*)checkbox);
}
void gui_checkbox_update(CHECKBOX_STRUCT* checkbox)
{
unsigned int c = (checkbox->checked)? checkbox->fgcolor:BACKGROUND_COLOR; //color to use for the tickmark
//helper points inside the checkbox
unsigned int xcent = checkbox->base.x1+(checkbox->base.x2-checkbox->base.x1)*6/14;
unsigned int yleft = checkbox->base.y2 - (xcent- checkbox->base.x1) - 1 ;
unsigned int yright = checkbox->base.y2 - (checkbox->base.x2 - xcent) - 1 ;
unsigned int ybot = checkbox->base.y2 - 4;
//Draw tickmark as a 3pixel wide line
tft_draw_line(checkbox->base.x1+3,yleft-1,xcent,ybot -1,c);
tft_draw_line(checkbox->base.x1+3,yleft,xcent,ybot ,c);
tft_draw_line(checkbox->base.x1+3,yleft+1,xcent,ybot + 1,c);
xcent++;
ybot--;
tft_draw_line(xcent,ybot-1,checkbox->base.x2-3,yright-1,c);
tft_draw_line(xcent,ybot,checkbox->base.x2-3,yright+0,c);
tft_draw_line(xcent,ybot+1,checkbox->base.x2-3,yright+1,c);
}
/**************************************************************************************************************************************
* Project: discoverpixy
* Website: https://github.com/t-moe/discoverpixy
* Authors: Aaron Schmocker, Timo Lang
* Institution: BFH Bern University of Applied Sciences
* File: common/gui/checkbox.c
*
* Version History:
* Date Autor Email SHA Changes
* 2015-04-27 timolang@gmail.com b300ac5 Added Checkbox support
* 2015-05-17 timolang@gmail.com 2d46336 Improved comments in implementation of button, checkbox, numupdown, tft, touch and screen modules/submodules.
*
**************************************************************************************************************************************/
#include "tft.h"
#include "touch.h"
#include "checkbox.h"
#include <stdio.h>
/* The idea is as follows:
* When the user creates a checkbox we create a touch area for that region and wait for PEN_DOWN events.
* Once the user puts the pen down in this area we'll redraw the checkbox with different shadows (feedback)
* and we'll now wait on PEN_UP or PEN_LEAVE events.
* If the user takes the pen away while in the area (PEN_UP), we toggle the checkbox and we call the provided user callback
* Otherwise (PEN_LEAVE) we only restore the initial shadows
*/
#define ACTIVE_COLOR RGB(251,208,123) //shadow color (inside of border)
#define BORDER_COLOR RGB(29,82,129) //1px border color
#define BACKGROUND_COLOR WHITE //Background color
//Callback which is called when the user touches the touch-area we created for the checkbox
void checkboxes_cb(void* touchArea, TOUCH_ACTION triggeredAction)
{
TOUCH_AREA_STRUCT* area = (TOUCH_AREA_STRUCT*)touchArea;
CHECKBOX_STRUCT* checkbox = (CHECKBOX_STRUCT*)touchArea;
switch (triggeredAction) {
case PEN_DOWN: //If the user touches the area for the "first time"
area->hookedActions = PEN_UP | PEN_LEAVE; //for the future we only want PEN_UP and PEN_LEAVE events
//Draw active shadows
tft_draw_rectangle(checkbox->base.x1 + 1, checkbox->base.y1 + 1, checkbox->base.x2 - 1, checkbox->base.y2 - 1, ACTIVE_COLOR);
tft_draw_rectangle(checkbox->base.x1 + 2, checkbox->base.y1 + 2, checkbox->base.x2 - 2, checkbox->base.y2 - 2, ACTIVE_COLOR);
break;
case PEN_UP: //If the user took the pen away, while in the area (=toggle checkbox!)
checkbox->checked = !checkbox->checked; //Toggle checkbox state
gui_checkbox_update(checkbox); //redraw/overdraw tickmark
if (checkbox->callback != NULL) { //The user provided a callback
checkbox->callback(checkbox, checkbox->checked); //Call the provided callback with the new checked state
}
// no break statement here!
case PEN_LEAVE: //if the user "slided out" of the area
area->hookedActions = PEN_DOWN; //for the future we only want PEN_DOWN events
//Draw inactive shadows
tft_draw_rectangle(checkbox->base.x1 + 1, checkbox->base.y1 + 1, checkbox->base.x2 - 1, checkbox->base.y2 - 1, BACKGROUND_COLOR);
tft_draw_rectangle(checkbox->base.x1 + 2, checkbox->base.y1 + 2, checkbox->base.x2 - 2, checkbox->base.y2 - 2, BACKGROUND_COLOR);
break;
default:
break;
}
}
bool gui_checkbox_add(CHECKBOX_STRUCT* checkbox)
{
if (touch_have_empty(1)) { //Check if the touch module can handle one additional area
unsigned char size = 0;
checkbox->base.hookedActions = PEN_DOWN; //At first we are interested in PEN_DOWN events
checkbox->base.callback = checkboxes_cb; //Use our own callback for the touch area events
//Check the size of the checkbox
if (checkbox->base.x2 > checkbox->base.x1) {
size = checkbox->base.x2 - checkbox->base.x1; //use width a as size
}
if (checkbox->base.y2 > checkbox->base.y1) {
if ((checkbox->base.y2 - checkbox->base.y1) > size) { //height is larger than size
size = checkbox->base.y2 - checkbox->base.y1; //use height as size
}
}
if (size == 0) { //no size found (maybe swap x2 and x1 or y2 and y1 ?)
return false; //signal error
}
if ((size & 0x01)) { //the size is an odd number
size++; //make size an even number
}
//Correct x2,y2 so that the checkbox is quadratic
checkbox->base.x2 = checkbox->base.x1 + size;
checkbox->base.y2 = checkbox->base.y1 + size;
gui_checkbox_redraw(checkbox);//Call redraw method, which will take care of the drawing of the entire checkbox
return touch_register_area(&checkbox->base); //Register the touch area and receive events for this checkbox, from now on
}
return false; //no more touch areas left
}
void gui_checkbox_redraw(CHECKBOX_STRUCT* checkbox)
{
//Draw background and border
tft_fill_rectangle(checkbox->base.x1 + 1, checkbox->base.y1 + 1, checkbox->base.x2 - 1, checkbox->base.y2 - 1, BACKGROUND_COLOR);
tft_draw_rectangle(checkbox->base.x1, checkbox->base.y1, checkbox->base.x2, checkbox->base.y2, BORDER_COLOR);
if (checkbox->checked) { //checkbox is currently checked
gui_checkbox_update(checkbox); //Call update method which will draw the tickmark
}
}
void gui_checkbox_remove(CHECKBOX_STRUCT* checkbox)
{
//We only need to unregister the touch area, as we have not allocated anything else
touch_unregister_area((TOUCH_AREA_STRUCT*)checkbox);
}
void gui_checkbox_update(CHECKBOX_STRUCT* checkbox)
{
unsigned int c = (checkbox->checked) ? checkbox->fgcolor : BACKGROUND_COLOR; //color to use for the tickmark
//helper points inside the checkbox
unsigned int xcent = checkbox->base.x1 + (checkbox->base.x2 - checkbox->base.x1) * 6 / 14;
unsigned int yleft = checkbox->base.y2 - (xcent - checkbox->base.x1) - 1 ;
unsigned int yright = checkbox->base.y2 - (checkbox->base.x2 - xcent) - 1 ;
unsigned int ybot = checkbox->base.y2 - 4;
//Draw tickmark as a 3pixel wide line
tft_draw_line(checkbox->base.x1 + 3, yleft - 1, xcent, ybot - 1, c);
tft_draw_line(checkbox->base.x1 + 3, yleft, xcent, ybot , c);
tft_draw_line(checkbox->base.x1 + 3, yleft + 1, xcent, ybot + 1, c);
xcent++;
ybot--;
tft_draw_line(xcent, ybot - 1, checkbox->base.x2 - 3, yright - 1, c);
tft_draw_line(xcent, ybot, checkbox->base.x2 - 3, yright + 0, c);
tft_draw_line(xcent, ybot + 1, checkbox->base.x2 - 3, yright + 1, c);
}

View File

@@ -1,70 +1,86 @@
#ifndef CHECKBOX_H
#define CHECKBOX_H
#include "touch.h"
/**
* @addtogroup gui
*/
/*@{*/
/**
* @defgroup checkbox Checkbox
* The Checkbox Gui-Element is a clickable, rectangular box with an optional tickmark inside of it.
* When the checkbox is pressed and released it's tick state changes and you will be notified via the provided callback.
*/
/*@}*/
/**
* @addtogroup checkbox
*/
/*@{*/
/**
* Prototype for Event Listeners (called when the checkbox state has changed)
* \note You should NOT execute long running things in this callback nor should you update the gui. But you can call gui_screen_navigate() for instance.
* @param checkbox The pointer to the CHECKBOX_STRUCT where to corresponding Checkbox has changed the state
* @param checked A boolean which indicates whether the checkbox is now checked or not.
*/
typedef void (*CHECKBOX_CALLBACK)(void *checkbox, bool checked);
/**
* Structure to configure the Checkbox
*/
typedef struct {
TOUCH_AREA_STRUCT base; //!< Basic geometry of the Checkbox. You only need to set the x1, y1, x2, y2 members of this struct.
uint16_t fgcolor; //!< The 16-bit color of the tickmark
bool checked; //!< A boolean which indicates whether or not the checkbox is currently checked.
CHECKBOX_CALLBACK callback; //!< Callback which is executed when the checkbox changes state
} CHECKBOX_STRUCT;
/**
* Adds a checkbox. Your Callback will be called from now on, if the checkbox changes state
* @param checkbox A Pointer to the preinitialized CHECKBOX_STRUCT
* @return true on success
*/
bool gui_checkbox_add(CHECKBOX_STRUCT* checkbox);
/**
* Removes the checkbox. You will no longer receive events for this checkbox. This function will not overdraw the region where the checkbox was located.
* @param checkbox A Pointer to the CHECKBOX_STRUCT
*/
void gui_checkbox_remove(CHECKBOX_STRUCT* checkbox);
/**
* Updates the checkbox. Call this function when you change the state of the checkbox through code.
* @param checkbox A Pointer to the CHECKBOX_STRUCT
*/
void gui_checkbox_update(CHECKBOX_STRUCT* checkbox);
/**
* Redraws the checkbox. Call this method if you have to redraw the entire screen or if you want to draw a checkbox on top of an image.
* @param checkbox A Pointer to the CHECKBOX_STRUCT
*/
void gui_checkbox_redraw(CHECKBOX_STRUCT* checkbox);
#define CHECKBOX_WIN_FG_COLOR RGB(32,161,34)
/*@}*/
#endif /* CHECKBOX_H */
/**************************************************************************************************************************************
* Project: discoverpixy
* Website: https://github.com/t-moe/discoverpixy
* Authors: Aaron Schmocker, Timo Lang
* Institution: BFH Bern University of Applied Sciences
* File: common/gui/checkbox.h
*
* Version History:
* Date Autor Email SHA Changes
* 2015-04-27 timolang@gmail.com b300ac5 Added Checkbox support
* 2015-05-11 timolang@gmail.com 08d9fe0 More work on doxygen module structure
* 2015-05-15 timolang@gmail.com 9a16865 Added doxgen comments to filesyste, checkbox, numupdown and screen module. And some minor changes to the other modules.
* 2015-05-17 timolang@gmail.com 2d46336 Improved comments in implementation of button, checkbox, numupdown, tft, touch and screen modules/submodules.
*
**************************************************************************************************************************************/
#ifndef CHECKBOX_H
#define CHECKBOX_H
#include "touch.h"
/**
* @addtogroup gui
*/
/*@{*/
/**
* @defgroup checkbox Checkbox
* The Checkbox Gui-Element is a clickable, rectangular box with an optional tickmark inside of it.
* When the checkbox is pressed and released it's tick state changes and you will be notified via the provided callback.
*/
/*@}*/
/**
* @addtogroup checkbox
*/
/*@{*/
/**
* Prototype for Event Listeners (called when the checkbox state has changed)
* \note You should NOT execute long running things in this callback nor should you update the gui. But you can call gui_screen_navigate() for instance.
* @param checkbox The pointer to the CHECKBOX_STRUCT where to corresponding Checkbox has changed the state
* @param checked A boolean which indicates whether the checkbox is now checked or not.
*/
typedef void (*CHECKBOX_CALLBACK)(void* checkbox, bool checked);
/**
* Structure to configure the Checkbox
*/
typedef struct {
TOUCH_AREA_STRUCT base; //!< Basic geometry of the Checkbox. You only need to set the x1, y1, x2, y2 members of this struct.
uint16_t fgcolor; //!< The 16-bit color of the tickmark
bool checked; //!< A boolean which indicates whether or not the checkbox is currently checked.
CHECKBOX_CALLBACK callback; //!< Callback which is executed when the checkbox changes state
} CHECKBOX_STRUCT;
/**
* Adds a checkbox. Your Callback will be called from now on, if the checkbox changes state
* @param checkbox A Pointer to the preinitialized CHECKBOX_STRUCT
* @return true on success
*/
bool gui_checkbox_add(CHECKBOX_STRUCT* checkbox);
/**
* Removes the checkbox. You will no longer receive events for this checkbox. This function will not overdraw the region where the checkbox was located.
* @param checkbox A Pointer to the CHECKBOX_STRUCT
*/
void gui_checkbox_remove(CHECKBOX_STRUCT* checkbox);
/**
* Updates the checkbox. Call this function when you change the state of the checkbox through code.
* @param checkbox A Pointer to the CHECKBOX_STRUCT
*/
void gui_checkbox_update(CHECKBOX_STRUCT* checkbox);
/**
* Redraws the checkbox. Call this method if you have to redraw the entire screen or if you want to draw a checkbox on top of an image.
* @param checkbox A Pointer to the CHECKBOX_STRUCT
*/
void gui_checkbox_redraw(CHECKBOX_STRUCT* checkbox);
#define CHECKBOX_WIN_FG_COLOR RGB(32,161,34)
/*@}*/
#endif /* CHECKBOX_H */

View File

@@ -1,143 +1,169 @@
#include "tft.h"
#include "touch.h"
#include "button.h"
#include "numupdown.h"
#include <stdio.h> //for sprintf
#include <stddef.h> //for offsetof macro
#include <stdlib.h> //for abs
/* The idea is as follows:
* When the user add's a numupdown we create two buttons, one with a plus and one with a minus sign in it
* When the user presses one of the buttons we check and increase the value and execute the provided user callback
*/
#define BASE_COLOR RGB(90,90,90) //Background color for the whole element
//Callback which is called when the user presses the "plus" button
void button_up_cb(void* button)
{
//Get the pointer to the numupdown: subtract the offset of the buttonUp member in the struct from the button pointer
NUMUPDOWN_STRUCT* element = button-offsetof(NUMUPDOWN_STRUCT,buttonUp);
if(element->value<element->max) { //old value lies below the maximum
element->value++; //let's increase the value
gui_numupdown_update(element); //and redraw everything
if(element->callback!=NULL) { //the user provided a callback
element->callback(element,element->value); //Call the user callback with the new value
}
}
}
//Callback which is called when the user presses the "minus" button
void button_down_cb(void* button)
{
//Get the pointer to the numupdown: subtract the offset of the buttonDown member in the struct from the button pointer
NUMUPDOWN_STRUCT* element = button-offsetof(NUMUPDOWN_STRUCT,buttonDown);
if(element->value>element->min) { //old value lies above the minimum
element->value--; //let's decrease the value
gui_numupdown_update(element); //and redraw everything
if(element->callback!=NULL) { //the user provided a callback
element->callback(element,element->value); //Call the user callback with the new value
}
}
}
//Method to calculate the number of characters needed to print the provided number in decimal notation (with optional sign)
static uint8_t calc_text_width(int16_t val) {
uint8_t width = 1 + (val<0); //1 if positive, 2 if negative (to let space for sign)
val=abs(val); //Make the number positive
while(val>=10) { //while we have two or more digits
val/=10; //remove one digit
width++; //add one character
}
return width;
}
bool gui_numupdown_add(NUMUPDOWN_STRUCT* numupdown)
{
if(touch_have_empty(2)) //Check if the touch module can handle two additional areas
{
if(numupdown->min > numupdown->max) { //min is bigger than max?
return false; //invalid parameter
}
if(numupdown->value < numupdown->min) { //value is smaller than min?
numupdown->value = numupdown->min; //normalize value
} else if(numupdown->value > numupdown->max) { //value is bigger than max?
numupdown->value = numupdown->max; //normalize value
}
uint8_t tw1 = calc_text_width(numupdown->max); //Calculate character width to render maximum value
uint8_t tw2 = calc_text_width(numupdown->min); //Calculate character width to render minimum value
if(tw2 > tw1) tw1 = tw2; //ensure tw1 contains the larger number of the two
uint8_t width= tft_font_width(0)*(tw1+1); //Calculate width of the number area
//Add "minus" button to the left side of the number area
numupdown->buttonDown.base.x1=numupdown->x;
numupdown->buttonDown.base.y1=numupdown->y;
numupdown->buttonDown.base.x2=AUTO;
numupdown->buttonDown.base.y2=numupdown->y+tft_font_height(0)*2;
numupdown->buttonDown.text="-";
numupdown->buttonDown.font=0;
numupdown->buttonDown.bgcolor=BASE_COLOR;
numupdown->buttonDown.txtcolor=WHITE;
numupdown->buttonDown.callback = button_down_cb;
gui_button_add(&numupdown->buttonDown);
//Add "plus" button to the right side of the number area
numupdown->buttonUp.base.x1=numupdown->buttonDown.base.x2+width+2;
numupdown->buttonUp.base.y1=numupdown->y;
numupdown->buttonUp.base.x2=AUTO;
numupdown->buttonUp.base.y2=numupdown->y +tft_font_height(0)*2;
numupdown->buttonUp.text="+";
numupdown->buttonUp.font=0;
numupdown->buttonUp.bgcolor=BASE_COLOR;
numupdown->buttonUp.txtcolor=WHITE;
numupdown->buttonUp.callback = button_up_cb;
gui_button_add(&numupdown->buttonUp);
//Draw background and label of the number area
tft_fill_rectangle(numupdown->buttonDown.base.x2+2,numupdown->y,numupdown->buttonDown.base.x2+width,numupdown->buttonUp.base.y2,BASE_COLOR);
tft_print_formatted(numupdown->buttonDown.base.x2+2+tft_font_width(0)/2,numupdown->y+tft_font_height(0)/2,numupdown->fgcolor,BASE_COLOR,0,"%*d",tw1,numupdown->value);
return true;
}
return false; //not enough touch areas left
}
void gui_numupdown_remove(NUMUPDOWN_STRUCT* numupdown)
{
//remove the two buttons, we have no other allocated resources
gui_button_remove(&numupdown->buttonUp);
gui_button_remove(&numupdown->buttonDown);
}
void gui_numupdown_redraw(NUMUPDOWN_STRUCT* numupdown)
{
//redraw the two buttons
gui_button_redraw(&numupdown->buttonUp);
gui_button_redraw(&numupdown->buttonDown);
//call update method which will take care of the number-area rendering
gui_numupdown_update(numupdown);
}
void gui_numupdown_update(NUMUPDOWN_STRUCT* numupdown)
{
//Calculate the number area width again (see above)
uint8_t tw1 = calc_text_width(numupdown->max);
uint8_t tw2 = calc_text_width(numupdown->min);
if(tw2 > tw1) tw1 = tw2;
uint8_t width= tft_font_width(0)*(tw1+1);
//Draw background and label of the number area
tft_fill_rectangle(numupdown->buttonDown.base.x2+2,numupdown->y,numupdown->buttonDown.base.x2+width,numupdown->buttonUp.base.y2,BASE_COLOR);
tft_print_formatted(numupdown->buttonDown.base.x2+2+tft_font_width(0)/2,numupdown->y+tft_font_height(0)/2,numupdown->fgcolor,BASE_COLOR,0,"%*d",tw1,numupdown->value);
}
/**************************************************************************************************************************************
* Project: discoverpixy
* Website: https://github.com/t-moe/discoverpixy
* Authors: Aaron Schmocker, Timo Lang
* Institution: BFH Bern University of Applied Sciences
* File: common/gui/numupdown.c
*
* Version History:
* Date Autor Email SHA Changes
* 2015-04-30 timolang@gmail.com 76ea9d8 Added num up down support.
* 2015-04-30 timolang@gmail.com b491b78 Made numupdown horizontal
* 2015-05-15 timolang@gmail.com 9a16865 Added doxgen comments to filesyste, checkbox, numupdown and screen module. And some minor changes to the other modules.
* 2015-05-17 timolang@gmail.com 2d46336 Improved comments in implementation of button, checkbox, numupdown, tft, touch and screen modules/submodules.
*
**************************************************************************************************************************************/
#include "tft.h"
#include "touch.h"
#include "button.h"
#include "numupdown.h"
#include <stdio.h> //for sprintf
#include <stddef.h> //for offsetof macro
#include <stdlib.h> //for abs
/* The idea is as follows:
* When the user add's a numupdown we create two buttons, one with a plus and one with a minus sign in it
* When the user presses one of the buttons we check and increase the value and execute the provided user callback
*/
#define BASE_COLOR RGB(90,90,90) //Background color for the whole element
//Callback which is called when the user presses the "plus" button
void button_up_cb(void* button)
{
//Get the pointer to the numupdown: subtract the offset of the buttonUp member in the struct from the button pointer
NUMUPDOWN_STRUCT* element = button - offsetof(NUMUPDOWN_STRUCT, buttonUp);
if (element->value < element->max) { //old value lies below the maximum
element->value++; //let's increase the value
gui_numupdown_update(element); //and redraw everything
if (element->callback != NULL) { //the user provided a callback
element->callback(element, element->value); //Call the user callback with the new value
}
}
}
//Callback which is called when the user presses the "minus" button
void button_down_cb(void* button)
{
//Get the pointer to the numupdown: subtract the offset of the buttonDown member in the struct from the button pointer
NUMUPDOWN_STRUCT* element = button - offsetof(NUMUPDOWN_STRUCT, buttonDown);
if (element->value > element->min) { //old value lies above the minimum
element->value--; //let's decrease the value
gui_numupdown_update(element); //and redraw everything
if (element->callback != NULL) { //the user provided a callback
element->callback(element, element->value); //Call the user callback with the new value
}
}
}
//Method to calculate the number of characters needed to print the provided number in decimal notation (with optional sign)
static uint8_t calc_text_width(int16_t val)
{
uint8_t width = 1 + (val < 0); //1 if positive, 2 if negative (to let space for sign)
val = abs(val); //Make the number positive
while (val >= 10) { //while we have two or more digits
val /= 10; //remove one digit
width++; //add one character
}
return width;
}
bool gui_numupdown_add(NUMUPDOWN_STRUCT* numupdown)
{
if (touch_have_empty(2)) { //Check if the touch module can handle two additional areas
if (numupdown->min > numupdown->max) { //min is bigger than max?
return false; //invalid parameter
}
if (numupdown->value < numupdown->min) { //value is smaller than min?
numupdown->value = numupdown->min; //normalize value
} else if (numupdown->value > numupdown->max) { //value is bigger than max?
numupdown->value = numupdown->max; //normalize value
}
uint8_t tw1 = calc_text_width(numupdown->max); //Calculate character width to render maximum value
uint8_t tw2 = calc_text_width(numupdown->min); //Calculate character width to render minimum value
if (tw2 > tw1) {
tw1 = tw2; //ensure tw1 contains the larger number of the two
}
uint8_t width = tft_font_width(0) * (tw1 + 1); //Calculate width of the number area
//Add "minus" button to the left side of the number area
numupdown->buttonDown.base.x1 = numupdown->x;
numupdown->buttonDown.base.y1 = numupdown->y;
numupdown->buttonDown.base.x2 = AUTO;
numupdown->buttonDown.base.y2 = numupdown->y + tft_font_height(0) * 2;
numupdown->buttonDown.text = "-";
numupdown->buttonDown.font = 0;
numupdown->buttonDown.bgcolor = BASE_COLOR;
numupdown->buttonDown.txtcolor = WHITE;
numupdown->buttonDown.callback = button_down_cb;
gui_button_add(&numupdown->buttonDown);
//Add "plus" button to the right side of the number area
numupdown->buttonUp.base.x1 = numupdown->buttonDown.base.x2 + width + 2;
numupdown->buttonUp.base.y1 = numupdown->y;
numupdown->buttonUp.base.x2 = AUTO;
numupdown->buttonUp.base.y2 = numupdown->y + tft_font_height(0) * 2;
numupdown->buttonUp.text = "+";
numupdown->buttonUp.font = 0;
numupdown->buttonUp.bgcolor = BASE_COLOR;
numupdown->buttonUp.txtcolor = WHITE;
numupdown->buttonUp.callback = button_up_cb;
gui_button_add(&numupdown->buttonUp);
//Draw background and label of the number area
tft_fill_rectangle(numupdown->buttonDown.base.x2 + 2, numupdown->y, numupdown->buttonDown.base.x2 + width, numupdown->buttonUp.base.y2, BASE_COLOR);
tft_print_formatted(numupdown->buttonDown.base.x2 + 2 + tft_font_width(0) / 2, numupdown->y + tft_font_height(0) / 2, numupdown->fgcolor, BASE_COLOR, 0, "%*d", tw1, numupdown->value);
return true;
}
return false; //not enough touch areas left
}
void gui_numupdown_remove(NUMUPDOWN_STRUCT* numupdown)
{
//remove the two buttons, we have no other allocated resources
gui_button_remove(&numupdown->buttonUp);
gui_button_remove(&numupdown->buttonDown);
}
void gui_numupdown_redraw(NUMUPDOWN_STRUCT* numupdown)
{
//redraw the two buttons
gui_button_redraw(&numupdown->buttonUp);
gui_button_redraw(&numupdown->buttonDown);
//call update method which will take care of the number-area rendering
gui_numupdown_update(numupdown);
}
void gui_numupdown_update(NUMUPDOWN_STRUCT* numupdown)
{
//Calculate the number area width again (see above)
uint8_t tw1 = calc_text_width(numupdown->max);
uint8_t tw2 = calc_text_width(numupdown->min);
if (tw2 > tw1) {
tw1 = tw2;
}
uint8_t width = tft_font_width(0) * (tw1 + 1);
//Draw background and label of the number area
tft_fill_rectangle(numupdown->buttonDown.base.x2 + 2, numupdown->y, numupdown->buttonDown.base.x2 + width, numupdown->buttonUp.base.y2, BASE_COLOR);
tft_print_formatted(numupdown->buttonDown.base.x2 + 2 + tft_font_width(0) / 2, numupdown->y + tft_font_height(0) / 2, numupdown->fgcolor, BASE_COLOR, 0, "%*d", tw1, numupdown->value);
}

View File

@@ -1,73 +1,88 @@
#ifndef NUMUPDOWN_H
#define NUMUPDOWN_H
#include "button.h"
/**
* @addtogroup gui
*/
/*@{*/
/**
* @defgroup numupdown NummericUpDown
* The NummericUpDown Gui Element
*/
/*@}*/
/**
* @addtogroup numupdown
*/
/*@{*/
/**
* Prototype for Event Listeners (called when the NummericUpDown value has changed)
* \note You should NOT execute long running things in this callback nor should you update the gui. But you can call gui_screen_navigate() for instance.
* @param numupdown The pointer to the NUMUPDOWN_STRUCT where to corresponding NummericUpDown has changed it's value
* @param value The new value of the NummericUpDown
*/
typedef void (*NUMUPDOWN_CALLBACK)(void *numupdown, int16_t value);
/**
* Structure to configure the NummericUpDown
*/
typedef struct {
uint16_t x; //!< The x-Coordinate of the Top-Left Starting Point.
uint16_t y; //!< The y-Coordinate of the Top-Left Starting Point.
uint16_t fgcolor; //!< The 16-bit color of the value-text
int16_t value; //!< The current/default value
int16_t min; //!< The minimum possible value (inclusive)
int16_t max; //!< The maximum possible value (inclusive)
NUMUPDOWN_CALLBACK callback; //!< Callback which is executed when the value changes
BUTTON_STRUCT buttonUp; //!< For internal use, don't change, don't initialize
BUTTON_STRUCT buttonDown; //!< For internal use, don't change, don't initialize
} NUMUPDOWN_STRUCT;
/**
* Adds a NummericUpDown. Your Callback will be called from now on, if the numupdown's value changes
* @param numupdown A Pointer to the preinitialized NUMUPDOWN_STRUCT
* @return true on success
*/
bool gui_numupdown_add(NUMUPDOWN_STRUCT* numupdown);
/**
* Removes the NummericUpDown. You will no longer receive events for this numupdown. This function will not overdraw the region where the numupdown was located.
* @param numupdown A Pointer to the NUMUPDOWN_STRUCT
*/
void gui_numupdown_remove(NUMUPDOWN_STRUCT* numupdown);
/**
* Updates the NummericUpDown. Call this function when you change the value/min/max of the numupdown through code.
* @param numupdown A Pointer to the NUMUPDOWN_STRUCT
*/
void gui_numupdown_update(NUMUPDOWN_STRUCT* numupdown);
/**
* Redraws the NummericUpDown. Call this method if you have to redraw the entire screen or if you want to draw a numupdown on top of an image.
* @param numupdown A Pointer to the NUMUPDOWN_STRUCT
*/
void gui_numupdown_redraw(NUMUPDOWN_STRUCT* numupdown);
/*@}*/
#endif /* NUMUPDOWN_H */
/**************************************************************************************************************************************
* Project: discoverpixy
* Website: https://github.com/t-moe/discoverpixy
* Authors: Aaron Schmocker, Timo Lang
* Institution: BFH Bern University of Applied Sciences
* File: common/gui/numupdown.h
*
* Version History:
* Date Autor Email SHA Changes
* 2015-04-30 timolang@gmail.com 76ea9d8 Added num up down support.
* 2015-05-11 timolang@gmail.com 08d9fe0 More work on doxygen module structure
* 2015-05-15 timolang@gmail.com 9a16865 Added doxgen comments to filesyste, checkbox, numupdown and screen module. And some minor changes to the other modules.
*
**************************************************************************************************************************************/
#ifndef NUMUPDOWN_H
#define NUMUPDOWN_H
#include "button.h"
/**
* @addtogroup gui
*/
/*@{*/
/**
* @defgroup numupdown NummericUpDown
* The NummericUpDown Gui Element
*/
/*@}*/
/**
* @addtogroup numupdown
*/
/*@{*/
/**
* Prototype for Event Listeners (called when the NummericUpDown value has changed)
* \note You should NOT execute long running things in this callback nor should you update the gui. But you can call gui_screen_navigate() for instance.
* @param numupdown The pointer to the NUMUPDOWN_STRUCT where to corresponding NummericUpDown has changed it's value
* @param value The new value of the NummericUpDown
*/
typedef void (*NUMUPDOWN_CALLBACK)(void* numupdown, int16_t value);
/**
* Structure to configure the NummericUpDown
*/
typedef struct {
uint16_t x; //!< The x-Coordinate of the Top-Left Starting Point.
uint16_t y; //!< The y-Coordinate of the Top-Left Starting Point.
uint16_t fgcolor; //!< The 16-bit color of the value-text
int16_t value; //!< The current/default value
int16_t min; //!< The minimum possible value (inclusive)
int16_t max; //!< The maximum possible value (inclusive)
NUMUPDOWN_CALLBACK callback; //!< Callback which is executed when the value changes
BUTTON_STRUCT buttonUp; //!< For internal use, don't change, don't initialize
BUTTON_STRUCT buttonDown; //!< For internal use, don't change, don't initialize
} NUMUPDOWN_STRUCT;
/**
* Adds a NummericUpDown. Your Callback will be called from now on, if the numupdown's value changes
* @param numupdown A Pointer to the preinitialized NUMUPDOWN_STRUCT
* @return true on success
*/
bool gui_numupdown_add(NUMUPDOWN_STRUCT* numupdown);
/**
* Removes the NummericUpDown. You will no longer receive events for this numupdown. This function will not overdraw the region where the numupdown was located.
* @param numupdown A Pointer to the NUMUPDOWN_STRUCT
*/
void gui_numupdown_remove(NUMUPDOWN_STRUCT* numupdown);
/**
* Updates the NummericUpDown. Call this function when you change the value/min/max of the numupdown through code.
* @param numupdown A Pointer to the NUMUPDOWN_STRUCT
*/
void gui_numupdown_update(NUMUPDOWN_STRUCT* numupdown);
/**
* Redraws the NummericUpDown. Call this method if you have to redraw the entire screen or if you want to draw a numupdown on top of an image.
* @param numupdown A Pointer to the NUMUPDOWN_STRUCT
*/
void gui_numupdown_redraw(NUMUPDOWN_STRUCT* numupdown);
/*@}*/
#endif /* NUMUPDOWN_H */

View File

@@ -1,3 +1,21 @@
/**************************************************************************************************************************************
* Project: discoverpixy
* Website: https://github.com/t-moe/discoverpixy
* Authors: Aaron Schmocker, Timo Lang
* Institution: BFH Bern University of Applied Sciences
* File: common/gui/screen.c
*
* Version History:
* Date Autor Email SHA Changes
* 2015-04-27 timolang@gmail.com cf72baa Introduced a Screen (sub) module and divided app into multiple screens.
* 2015-04-27 timolang@gmail.com 77e6d0e Fixed screen implementation.
* 2015-05-10 timolang@gmail.com b6ab7c8 Fixed compiler warning in tft and screen module.
* 2015-05-17 timolang@gmail.com 2d46336 Improved comments in implementation of button, checkbox, numupdown, tft, touch and screen modules/submodules.
* 2015-06-01 timolang@gmail.com eb573bc Finalized calibration. Fixed a bug in screen module.
* 2015-06-06 timolang@gmail.com c06661d Fixed some outdated comments in source code. Documented Gui Module in docu.
*
**************************************************************************************************************************************/
#include "screen.h"
/* The idea is as follows:
@@ -14,62 +32,79 @@ static SCREEN_STRUCT* screen_list = NULL; //Head of the linked list which stores
static SCREEN_STRUCT* screen_current = NULL; //Pointer to the current screen (= tail of the list)
static volatile SCREEN_STRUCT* screen_goto = NULL; //Screen we should navigate to once we enter the gui_screen_update() method again
SCREEN_STRUCT* gui_screen_get_current() {
return screen_current;
SCREEN_STRUCT* gui_screen_get_current()
{
return screen_current;
}
void gui_screen_update() {
if(screen_goto!=NULL) { //we received the task to switch the screen
SCREEN_STRUCT* go = (SCREEN_STRUCT*) screen_goto; //Backup volatile variable
screen_goto=NULL; //reset the "goto instruction", since we're processing it now
if(go->next!=NULL) { //The screen is not the last in the list, so we're going back
if(go->next!=screen_current) { //this condition should always be false
return; //list corrupted?
}
screen_current->on_leave(screen_current); //let the current screen free/unregister it's resources
go->next=NULL; //remove the current screen from the list
} else { //we're going forward (to a new screen)
if(screen_current!=NULL) { //this is not the first screen
screen_current->on_leave(screen_current); //let the current screen free/unregister it's resources
screen_current->next = go; //append the new screen to the end of the list
} else { //first screen ever seen
screen_list=go; //set the new screen as list-head
}
}
go->on_enter(go); //let the new screen allocate/register it's resources
screen_current = go; //the new screen is now the current screen. Transition done
}
void gui_screen_update()
{
if (screen_goto != NULL) { //we received the task to switch the screen
SCREEN_STRUCT* go = (SCREEN_STRUCT*) screen_goto; //Backup volatile variable
screen_goto = NULL; //reset the "goto instruction", since we're processing it now
if(screen_current!=NULL) { //A screen has been set
screen_current->on_update(screen_current); //Update current screen
}
if (go->next != NULL) { //The screen is not the last in the list, so we're going back
if (go->next != screen_current) { //this condition should always be false
return; //list corrupted?
}
screen_current->on_leave(screen_current); //let the current screen free/unregister it's resources
go->next = NULL; //remove the current screen from the list
} else { //we're going forward (to a new screen)
if (screen_current != NULL) { //this is not the first screen
screen_current->on_leave(screen_current); //let the current screen free/unregister it's resources
screen_current->next = go; //append the new screen to the end of the list
} else { //first screen ever seen
screen_list = go; //set the new screen as list-head
}
}
go->on_enter(go); //let the new screen allocate/register it's resources
screen_current = go; //the new screen is now the current screen. Transition done
}
if (screen_current != NULL) { //A screen has been set
screen_current->on_update(screen_current); //Update current screen
}
}
bool gui_screen_navigate(SCREEN_STRUCT* screen) {
if(screen==NULL || screen==screen_current || screen==screen_goto) { //invalid argument passed
return false;
}
screen->next = NULL; //this will become the new tail of the list, so the next pointer must be NULL
screen_goto=screen; //"send message" to main loop, to switch the screen
return true;
bool gui_screen_navigate(SCREEN_STRUCT* screen)
{
if (screen == NULL || screen == screen_current || screen == screen_goto) { //invalid argument passed
return false;
}
screen->next = NULL; //this will become the new tail of the list, so the next pointer must be NULL
screen_goto = screen; //"send message" to main loop, to switch the screen
return true;
}
bool gui_screen_back() {
if(screen_list==NULL) { //the list head is emtpy, nothing to go back to
return false;
}
SCREEN_STRUCT* current = screen_list;
SCREEN_STRUCT* last = NULL;
//Find second last element in list
while(current->next != NULL) {
last = current;
current = current->next;
}
if(last==NULL) return false; //There's only a single screen, there's no going back here
if(current!=screen_current) return false; //The last entry in the list is not the current screen. List corrupted?
screen_goto=last; //"send message" to main loop, to switch the screen
return true;
bool gui_screen_back()
{
if (screen_list == NULL) { //the list head is emtpy, nothing to go back to
return false;
}
SCREEN_STRUCT* current = screen_list;
SCREEN_STRUCT* last = NULL;
//Find second last element in list
while (current->next != NULL) {
last = current;
current = current->next;
}
if (last == NULL) {
return false; //There's only a single screen, there's no going back here
}
if (current != screen_current) {
return false; //The last entry in the list is not the current screen. List corrupted?
}
screen_goto = last; //"send message" to main loop, to switch the screen
return true;
}

View File

@@ -1,3 +1,21 @@
/**************************************************************************************************************************************
* Project: discoverpixy
* Website: https://github.com/t-moe/discoverpixy
* Authors: Aaron Schmocker, Timo Lang
* Institution: BFH Bern University of Applied Sciences
* File: common/gui/screen.h
*
* Version History:
* Date Autor Email SHA Changes
* 2015-04-27 timolang@gmail.com cf72baa Introduced a Screen (sub) module and divided app into multiple screens.
* 2015-04-27 timolang@gmail.com 77e6d0e Fixed screen implementation.
* 2015-05-11 timolang@gmail.com 08d9fe0 More work on doxygen module structure
* 2015-05-15 timolang@gmail.com 9a16865 Added doxgen comments to filesyste, checkbox, numupdown and screen module. And some minor changes to the other modules.
* 2015-05-17 timolang@gmail.com 2d46336 Improved comments in implementation of button, checkbox, numupdown, tft, touch and screen modules/submodules.
* 2015-06-06 timolang@gmail.com c06661d Fixed some outdated comments in source code. Documented Gui Module in docu.
*
**************************************************************************************************************************************/
#ifndef SCREEN_H
#define SCREEN_H
@@ -12,7 +30,7 @@
/**
* @defgroup screen Screen
* The Screen Submodule provides an api to navigate between different "screens" on the UI.
* Each screen must provide an enter, update and a leave method; which will be called from this module at the right time.
* Each screen must provide an enter, update and a leave method; which will be called from this module at the right time.
* The implemented screens of the application are documented in the \ref screens module.
*/
/*@}*/
@@ -31,12 +49,12 @@ typedef void (*SCREEN_CALLBACK)(void* screen);
/**
* Structure to configure the Screen
*/
typedef struct SCREEN_S{
SCREEN_CALLBACK on_enter; //!< The Callback which is called when the screen is entered. Add/Register all UI-Elements here
SCREEN_CALLBACK on_leave; //!< The Callback which is called when the screen is left. Remove/Unregister all UI-Elements here
SCREEN_CALLBACK on_update; //!< The Callback which is called repeatedly when the screen should be updated. Update/Redraw all UI-Elements here
typedef struct SCREEN_S {
SCREEN_CALLBACK on_enter; //!< The Callback which is called when the screen is entered. Add/Register all UI-Elements here
SCREEN_CALLBACK on_leave; //!< The Callback which is called when the screen is left. Remove/Unregister all UI-Elements here
SCREEN_CALLBACK on_update; //!< The Callback which is called repeatedly when the screen should be updated. Update/Redraw all UI-Elements here
struct SCREEN_S* next; //!< Used internally. do not modify, do not initialize
struct SCREEN_S* next; //!< Used internally. do not modify, do not initialize
} SCREEN_STRUCT;