discoverpixy
Macros | Functions
button.c File Reference
#include "tft.h"
#include "touch.h"
#include "button.h"
#include <string.h>
Include dependency graph for button.c:

Go to the source code of this file.

Macros

#define BRIGHTNESS_VAL   3
 

Functions

void calculate_shadows (uint16_t bgcolor, uint16_t *light_shadow, uint16_t *dark_shadow)
 
void buttons_cb (void *touchArea, TOUCH_ACTION triggeredAction)
 
bool gui_button_add (BUTTON_STRUCT *button)
 
void gui_button_redraw (BUTTON_STRUCT *button)
 
void gui_button_remove (BUTTON_STRUCT *button)
 

Macro Definition Documentation

#define BRIGHTNESS_VAL   3

Function Documentation

void buttons_cb ( void *  touchArea,
TOUCH_ACTION  triggeredAction 
)

Definition at line 92 of file button.c.

93 {
94  TOUCH_AREA_STRUCT* area = (TOUCH_AREA_STRUCT*)touchArea;
95  BUTTON_STRUCT* button = (BUTTON_STRUCT*)touchArea;
96 
97  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)
98  calculate_shadows(button->bgcolor, &c_light, &c_dark);
99 
100  switch (triggeredAction) {
101  case PEN_DOWN: //If the user touches the area for the "first time"
102  area->hookedActions = PEN_UP | PEN_LEAVE; //for the future we only want PEN_UP and PEN_LEAVE events
103 
104  //Draw shadows
105  tft_draw_line(button->base.x1 + 1, button->base.y1, button->base.x2 - 1, button->base.y1, c_dark); //North
106  tft_draw_line(button->base.x1, button->base.y1 + 1, button->base.x1, button->base.y2 - 1, c_dark); //West
107  tft_draw_line(button->base.x1 + 1, button->base.y2, button->base.x2 - 1, button->base.y2, c_light); //South
108  tft_draw_line(button->base.x2, button->base.y1 + 1, button->base.x2, button->base.y2 - 1, c_light); //East
109  break;
110 
111  case PEN_UP: //If the user took the pen away, while in the area (=button pressed!)
112  case PEN_LEAVE: //or the user "slided out" of the area
113  area->hookedActions = PEN_DOWN; //for the future we only want PEN_DOWN events
114 
115  //Draw inverse shadows
116  tft_draw_line(button->base.x1 + 1, button->base.y1, button->base.x2 - 1, button->base.y1, c_light); //North
117  tft_draw_line(button->base.x1, button->base.y1 + 1, button->base.x1, button->base.y2 - 1, c_light); //West
118  tft_draw_line(button->base.x1 + 1, button->base.y2, button->base.x2 - 1, button->base.y2, c_dark); //South
119  tft_draw_line(button->base.x2, button->base.y1 + 1, button->base.x2, button->base.y2 - 1, c_dark); //East
120 
121  if (triggeredAction == PEN_UP && button->callback != NULL) { //If the button got "pressed" instead of left, and the user provided a callback
122  button->callback(button); //execute the user callback
123  }
124 
125  break;
126 
127  default:
128  break;
129  }
130 }
Receive an event when the pen goes down inside the region.
Definition: touch.h:54
Receive an event when the pen goes up inside the region.
Definition: touch.h:55
void tft_draw_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
Definition: tft.c:50
uint16_t y1
Top Left Y-Coordinate of Area.
Definition: touch.h:75
uint16_t x1
Top Left X-Coordinate of Area.
Definition: touch.h:74
uint16_t bgcolor
The 16-bit background color of the button.
Definition: button.h:57
TOUCH_AREA_STRUCT base
Basic geometry of the button. You only need to set the x1, y1, x2, y2 members of this struct...
Definition: button.h:56
uint16_t y2
Bottom Right Y-Coordinate of Area.
Definition: touch.h:77
uint16_t x2
Bottom Right X-Coordinate of Area.
Definition: touch.h:76
void calculate_shadows(uint16_t bgcolor, uint16_t *light_shadow, uint16_t *dark_shadow)
Definition: button.c:34
TOUCH_ACTION hookedActions
Actions to listen to.
Definition: touch.h:73
BUTTON_CALLBACK callback
Callback which is executed when the button is pressed.
Definition: button.h:58
Receive an event when the pen leaves the region (pen was inside region before)
Definition: touch.h:57

Here is the call graph for this function:

Here is the caller graph for this function:

void calculate_shadows ( uint16_t  bgcolor,
uint16_t *  light_shadow,
uint16_t *  dark_shadow 
)

Definition at line 34 of file button.c.

35 {
36 #define BRIGHTNESS_VAL 3 //How much the Brightness is in/decreased for button shadows (3 -> Add/Subtract 1/3 off Full Value)
37 
38  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)
39  uint8_t r, g, b;
40 
41  //separate the channels of the 16-bit rgb565 color
42  r = (bgcolor & 0xF800) >> 11;
43  g = (bgcolor & 0x07E0) >> 5;
44  b = (bgcolor & 0x001F) >> 0;
45 
46  //For the light shadow color:
47  if ((r + 0x1F / BRIGHTNESS_VAL) > 0x1F) { //Adding one third would exceed the maximum of the red channel
48  c_light = 0xF800; //Use full red
49  } else { //adding one third to the red channel is fine
50  c_light = (r + 0x1F / BRIGHTNESS_VAL) << 11; //Use same red as in the background, but add one third
51  }
52 
53  if ((g + 0x3F / BRIGHTNESS_VAL) > 0x3F) { //same for the green channel
54  c_light |= 0x07E0;
55  } else {
56  c_light |= (g + 0x3F / BRIGHTNESS_VAL) << 5;
57  }
58 
59  if ((b + 0x1F / BRIGHTNESS_VAL) > 0x1F) { //and the blue channel
60  c_light |= 0x0018;
61  } else {
62  c_light |= (b + 0x1F / BRIGHTNESS_VAL) << 0;
63  }
64 
65  //For the dark shadow color
66  if (r > (0x1F / BRIGHTNESS_VAL)) { //Subtracting one third would NOT exceed the minimum of the red channel
67  c_dark = (r - 0x1F / BRIGHTNESS_VAL) << 11; //Use same red as in the background, but subtract one third
68  } else { //Subtracting one third would give us a number below zero
69  c_dark = 0x0000; //use no red channel
70  }
71 
72  if (g > (0x3F / BRIGHTNESS_VAL)) { //Same for the green channel
73  c_dark |= (g - 0x3F / BRIGHTNESS_VAL) << 5;
74  }
75 
76  if (b > (0x1F / BRIGHTNESS_VAL)) { //and the blue channel
77  c_dark |= (b - 0x1F / BRIGHTNESS_VAL) << 0;
78  }
79 
80  //Assign the calculated shadows to out parameters
81  if (light_shadow != NULL) {
82  *light_shadow = c_light;
83  }
84 
85  if (dark_shadow != NULL) {
86  *dark_shadow = c_dark;
87  }
88 
89 }
#define BRIGHTNESS_VAL

Here is the caller graph for this function: