Added button support.

This commit is contained in:
t-moe
2015-04-27 20:45:07 +02:00
parent e249fb2aa2
commit 7c9eabc6a3
3 changed files with 252 additions and 5 deletions

View File

@@ -2,14 +2,15 @@
#include "tft.h"
#include "system.h"
#include "touch.h"
#include "button.h"
#include "pixy.h"
#include <stdio.h>
#include <stdlib.h>
bool pixy_connected = false;
TOUCH_AREA_STRUCT a1;
/*TOUCH_AREA_STRUCT a1;
void touchCB(void* touchArea, TOUCH_ACTION triggeredAction) {
@@ -34,8 +35,17 @@ void touchCB(void* touchArea, TOUCH_ACTION triggeredAction) {
printf("action %s\n",triggeredAction);
break;
}
}*/
BUTTON_STRUCT b1;
void buttonCB(void* button) {
printf("button pressed\n");
}
void app_init() {
system_init();
tft_init();
@@ -52,7 +62,7 @@ void app_init() {
tft_draw_line(10,215,310,225,RGB(0xFF,0,0xFF));
tft_draw_circle(10,10,100, RED);
a1.hookedActions = PEN_DOWN | PEN_UP | PEN_MOVE | PEN_ENTER | PEN_LEAVE;
/*a1.hookedActions = PEN_DOWN | PEN_UP | PEN_MOVE | PEN_ENTER | PEN_LEAVE;
a1.x1 = 30;
a1.y1 = 30;
a1.x2 = 100;
@@ -61,7 +71,20 @@ void app_init() {
touch_register_area(&a1);
tft_draw_rectangle(30,30,100,60,BLUE);
tft_print_line(30, 30, RED, BLUE, 0, "Hallo");
tft_print_line(30, 30, RED, BLUE, 0, "Hallo");*/
//b_alarm_ok
b1.base.x1=25; //Start X of Button
b1.base.y1=45; //Start Y of Button
b1.base.x2=AUTO; //b1.base.x1+160; //Auto Calculate X2 with String Width
b1.base.y2=AUTO; //Auto Calculate Y2 with String Height
b1.txtcolor=WHITE; //Set foreground color
b1.bgcolor=HEX(0xDE1010); //Set background color (Don't take 255 or 0 on at least one channel, to make shadows possible)
b1.font=0; //Select Font
b1.text="Hallo"; //Set Text (For formatted strings take sprintf)
b1.callback=buttonCB; //Call b1_cb as Callback
gui_button_add(&b1); //Register Button (and run the callback from now on)
}
@@ -74,7 +97,7 @@ int pixy_frame_test();
void app_process() {
system_process(); //Let the system handle it's pending events
gui_button_redraw(&b1);
//Note: The only way to detect that pixy has been disconnected is if a command fails. There's no pixy_is_connected method yet :'(

189
common/gui/button.c Normal file
View File

@@ -0,0 +1,189 @@
#include "tft.h"
#include "touch.h"
#include "button.h"
#include <string.h>
#define BRIGHTNESS_VAL 3 //How much the Brightness is in/decreased for button shadows (3 -> Add 1/3 off Full Value)
void buttons_cb(void* touchArea, TOUCH_ACTION triggeredAction)
//Method shared between normal Buttons and Bitmap Buttons-> Look at comment in headerfile for explanation.
{
TOUCH_AREA_STRUCT * area = (TOUCH_AREA_STRUCT*)touchArea;
BUTTON_STRUCT* button = (BUTTON_STRUCT*)touchArea;
unsigned int c1,c2;
unsigned char r,g,b;
r=(button->bgcolor&0xF800)>>11;
g=(button->bgcolor&0x07E0)>>5;
b=(button->bgcolor&0x001F)>>0;
if((r + 0x1F/BRIGHTNESS_VAL) >0x1F)
c1=0xF800;
else
c1=(r+0x1F/BRIGHTNESS_VAL)<<11;
if((g + 0x3F/BRIGHTNESS_VAL) >0x3F)
c1|=0x07E0;
else
c1|=(g+0x3F/BRIGHTNESS_VAL)<<5;
if((b + 0x1F/BRIGHTNESS_VAL) >0x1F)
c1|=0x0018;
else
c1|=(b+0x1F/BRIGHTNESS_VAL)<<0;
if(r > (0x1F/BRIGHTNESS_VAL))
c2=(r-0x1F/BRIGHTNESS_VAL)<<11;
else
c2=0x0000;
if(g > (0x3F/BRIGHTNESS_VAL))
c2|=(g-0x3F/BRIGHTNESS_VAL)<<5;
if(b > (0x1F/BRIGHTNESS_VAL))
c2|=(b-0x1F/BRIGHTNESS_VAL)<<0;
switch(triggeredAction)
{
case PEN_DOWN:
area->hookedActions=PEN_UP|PEN_LEAVE;
tft_draw_line(button->base.x1+1,button->base.y1,button->base.x2-1,button->base.y1,c2); //Nord
tft_draw_line(button->base.x1,button->base.y1+1,button->base.x1,button->base.y2-1,c2);//West
tft_draw_line(button->base.x1+1,button->base.y2,button->base.x2-1,button->base.y2,c1); //S<>d
tft_draw_line(button->base.x2,button->base.y1+1,button->base.x2,button->base.y2-1,c1); //Ost
break;
case PEN_UP:
case PEN_LEAVE:
area->hookedActions=PEN_DOWN;
tft_draw_line(button->base.x1+1,button->base.y1,button->base.x2-1,button->base.y1,c1); //Nord
tft_draw_line(button->base.x1,button->base.y1+1,button->base.x1,button->base.y2-1,c1);//West
tft_draw_line(button->base.x1+1,button->base.y2,button->base.x2-1,button->base.y2,c2); //S<>d
tft_draw_line(button->base.x2,button->base.y1+1,button->base.x2,button->base.y2-1,c2); //Ost
if(triggeredAction==PEN_UP && button->callback!=NULL)
button->callback(button);
break;
default:break;
}
}
bool gui_button_add(BUTTON_STRUCT* button)//Registers a button (fill Struct first). Return false if no more Space in the Pointertable (-->Change NUM_AREAS).
{
if(touch_have_empty(1))
{
unsigned int strwidth=tft_font_width(button->font)*strlen(button->text);
unsigned char strheight=tft_font_height(button->font);
button->base.hookedActions=PEN_DOWN;
button->base.callback = buttons_cb;
if(button->base.x2==AUTO)
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))
return false;
if(button->base.y2==AUTO)
button->base.y2=button->base.y1 -1 +strheight+(strheight/2);
else if((button->base.y2-button->base.y1+1)<(strheight+2))
return false;
gui_button_redraw(button);
return touch_register_area(&button->base);
}
return false;
}
void gui_button_redraw(BUTTON_STRUCT* button)
{
unsigned int strwidth=tft_font_width(button->font)*strlen(button->text);
unsigned char strheight=tft_font_height(button->font);
unsigned char r,g,b;
unsigned int c;
r=(button->bgcolor&0xF800)>>11;
g=(button->bgcolor&0x07E0)>>5;
b=(button->bgcolor&0x001F)>>0;
tft_fill_rectangle(button->base.x1+1,button->base.y1+1,button->base.x2-1,button->base.y2-1,button->bgcolor);
if((r + 0x1F/BRIGHTNESS_VAL) >0x1F)
c=0xF800;
else
c=(r+0x1F/BRIGHTNESS_VAL)<<11;
if((g + 0x3F/BRIGHTNESS_VAL) >0x3F)
c|=0x07E0;
else
c|=(g+0x3F/BRIGHTNESS_VAL)<<5;
if((b + 0x1F/BRIGHTNESS_VAL) >0x1F)
c|=0x0018;
else
c|=(b+0x1F/BRIGHTNESS_VAL)<<0;
tft_draw_line(button->base.x1+1,button->base.y1,button->base.x2-1,button->base.y1,c); //Nord
tft_draw_line(button->base.x1,button->base.y1+1,button->base.x1,button->base.y2-1,c);//West
if(r > (0x1F/BRIGHTNESS_VAL))
c=(r-0x1F/BRIGHTNESS_VAL)<<11;
else
c=0x0000;
if(g > (0x3F/BRIGHTNESS_VAL))
c|=(g-0x3F/BRIGHTNESS_VAL)<<5;
if(b > (0x1F/BRIGHTNESS_VAL))
c|=(b-0x1F/BRIGHTNESS_VAL)<<0;
tft_draw_line(button->base.x1+1,button->base.y2,button->base.x2-1,button->base.y2,c); //S<>d
tft_draw_line(button->base.x2,button->base.y1+1,button->base.x2,button->base.y2-1,c); //Ost
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)
{
touch_unregister_area((TOUCH_AREA_STRUCT*)button);
}
/*
bool guiAddBitmapButton (BITMAPBUTTON_STRUCT* button)
{
if(touchHaveEmpty(1))
{
button->base.hookedActions=PEN_DOWN;
button->base.callback = buttons_cb;
if(button->base.x2==AUTO)
button->base.x2= button->base.x1 -1 + button->imgwidth + button->imgwidth/4;
else if((button->base.x2-button->base.x1+1)<(button->imgwidth+2))
return false;
if(button->base.y2==AUTO)
button->base.y2=button->base.y1 -1 +button->imgheight + button->imgheight/4;
else if((button->base.y2-button->base.y1+1)<(button->imgheight+2))
return false;
guiRedrawBitmapButton(button);
return touchRegisterArea(&button->base);
}
return false;
}
void guiRedrawBitmapButton(BITMAPBUTTON_STRUCT* button)
{
unsigned char r,g,b;
unsigned int c;
r=(button->bgcolor&0xF800)>>11;
g=(button->bgcolor&0x07E0)>>5;
b=(button->bgcolor&0x001F)>>0;
tftFillRectangle(button->base.x1+1,button->base.y1+1,button->base.x2-1,button->base.y2-1,button->bgcolor);
if((r + 0x1F/BRIGHTNESS_VAL) >0x1F)
c=0xF800;
else
c=(r+0x1F/BRIGHTNESS_VAL)<<11;
if((g + 0x3F/BRIGHTNESS_VAL) >0x3F)
c|=0x07E0;
else
c|=(g+0x3F/BRIGHTNESS_VAL)<<5;
if((b + 0x1F/BRIGHTNESS_VAL) >0x1F)
c|=0x0018;
else
c|=(b+0x1F/BRIGHTNESS_VAL)<<0;
tft_draw_line(button->base.x1+1,button->base.y1,button->base.x2-1,button->base.y1,c); //Nord
tft_draw_line(button->base.x1,button->base.y1+1,button->base.x1,button->base.y2-1,c);//West
if(r > (0x1F/BRIGHTNESS_VAL))
c=(r-0x1F/BRIGHTNESS_VAL)<<11;
else
c=0x0000;
if(g > (0x3F/BRIGHTNESS_VAL))
c|=(g-0x3F/BRIGHTNESS_VAL)<<5;
if(b > (0x1F/BRIGHTNESS_VAL))
c|=(b-0x1F/BRIGHTNESS_VAL)<<0;
tft_draw_line(button->base.x1+1,button->base.y2,button->base.x2-1,button->base.y2,c); //S<>d
tft_draw_line(button->base.x2,button->base.y1+1,button->base.x2,button->base.y2-1,c); //Ost
tftDrawBitmapUnscaledStreamedRaw(button->base.x1+(button->base.x2-button->base.x1+1-button->imgwidth)/2,button->base.y1+(button->base.y2-button->base.y1+1-button->imgheight)/2,button->imgwidth,button->imgheight,button->filename);
}
void guiRemoveBitmapButton(BITMAPBUTTON_STRUCT* button)
{
touchUnregisterArea((TOUCH_AREA_STRUCT*)button);
}
*/

35
common/gui/button.h Normal file
View File

@@ -0,0 +1,35 @@
typedef void (*BUTTON_CALLBACK)(void *button); //!< Function pointer used...
typedef struct {
TOUCH_AREA_STRUCT base;
uint16_t bgcolor;
BUTTON_CALLBACK callback; //Callback
uint16_t txtcolor;
uint8_t font;
const char *text;
} BUTTON_STRUCT;
/*
typedef struct {
TOUCH_AREA_STRUCT base;
unsigned int bgcolor;
BUTTON_CALLBACK callback; //Callback
unsigned char imgwidth;
unsigned char imgheight;
char* filename;
} BITMAPBUTTON_STRUCT;
*/
//Notice that the first 3 Members are Equal, so it's possible to cast it to a BUTTON_STRUCT even if it's a BITMAPBUTTON_STRUCT (when changeing only the first 3 Members).
#define AUTO 0
bool gui_button_add(BUTTON_STRUCT* button);
void gui_button_remove(BUTTON_STRUCT* button);
void gui_button_redraw(BUTTON_STRUCT* button);
/*
bool guiAddBitmapButton(BITMAPBUTTON_STRUCT* button);
void guiRemoveBitmapButton(BITMAPBUTTON_STRUCT* button);
void guiRedrawBitmapButton(BITMAPBUTTON_STRUCT* button);
*/