diff --git a/common/app/app.c b/common/app/app.c index 07d08b2..cb7bcf9 100644 --- a/common/app/app.c +++ b/common/app/app.c @@ -2,253 +2,23 @@ #include "tft.h" #include "system.h" #include "touch.h" -#include "pixy.h" -#include -#include +#include "screen_main.h" -bool pixy_connected = false; - -TOUCH_AREA_STRUCT a1; - - -void touchCB(void* touchArea, TOUCH_ACTION triggeredAction) { - - switch(triggeredAction) { - case PEN_DOWN: - printf("action PEN_DOWN\n"); - break; - - case PEN_UP: - printf("action PEN_UP\n"); - break; - case PEN_MOVE: - printf("action PEN_MOVE\n"); - break; - case PEN_ENTER: - printf("action PEN_ENTER\n"); - break; - case PEN_LEAVE: - printf("action PEN_LEAVE\n"); - break; - default: - printf("action %s\n",triggeredAction); - break; - } -} void app_init() { system_init(); tft_init(); touch_init(); - pixy_connected = (pixy_init()==0); //try to connect to pixy - - //only testwise - tft_clear(WHITE); - tft_draw_pixel(10,210,BLUE); - tft_draw_pixel(12,210,BLUE); - tft_draw_rectangle(40,210,60,235,BLUE); - tft_fill_rectangle(100,215,200,225,GREEN); - 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.x1 = 30; - a1.y1 = 30; - a1.x2 = 100; - a1.y2 = 60; - a1.callback = touchCB; - touch_register_area(&a1); - - tft_draw_rectangle(30,30,100,60,BLUE); - + gui_screen_navigate(get_screen_main()); } - - -int pixy_led_test(); -int pixy_frame_test(); - //app event loop void app_process() { system_process(); //Let the system handle it's pending events - - - //Note: The only way to detect that pixy has been disconnected is if a command fails. There's no pixy_is_connected method yet :'( - - if(!pixy_connected) { //Pixy not connected - pixy_close(); //Ensure that all pixy resources are freed (failsafe) - if(pixy_init()==0) { //try to connect to pixy - pixy_connected=true; - } - } - - if(pixy_connected) { - pixy_service(); //Send/receive event data from/to pixy failed - - //Code for tests see below - if(pixy_led_test()!=0) { - pixy_connected=false; - } - - /*if(pixy_frame_test()!=0) { - pixy_connected=false; - }*/ - system_delay(500); - } + gui_screen_update(); //update the currently active screen } -//----------------------------------------------------------------------------------------------------------------- - -int colorind; -const uint32_t colors [] = {0xFF0000, 0x00FF00,0x0000FF,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF,0x000000}; -const int num_colors = sizeof(colors)/sizeof(uint32_t); - -int pixy_led_test() { - if(colorind==0) { - pixy_led_set_max_current(5); - } - - int32_t response; - int return_value; - return_value = pixy_command("led_set", INT32(colors[colorind++]), END_OUT_ARGS, &response, END_IN_ARGS); - colorind%=num_colors; - - if(return_value!=0) { - colorind=0; //reset color ind, to start at zero when plugging pixy in again - } - - return return_value; -} - -//---------------------------------------------------------------------------------------------------------------------------- - -int renderBA81(uint8_t renderFlags, uint16_t width, uint16_t height, uint32_t frameLen, uint8_t *frame); - - -int pixy_frame_test() { - - uint8_t* videodata; - int32_t response; - int32_t fourccc; - int8_t renderflags; - uint16_t xwidth; - uint16_t ywidth; - uint32_t size; - - - int return_value = pixy_command("cam_getFrame", // String id for remote procedure - INT8(0x21), // mode - INT16(0), // xoffset - INT16(0), // yoffset - INT16(320), // width - INT16(200), // height - END_OUT_ARGS, // separator - &response, // pointer to mem address for return value - &fourccc, - &renderflags, - &xwidth, - &ywidth, - &size, - &videodata, // pointer to mem address for returned frame - END_IN_ARGS); - - if(return_value==0) { - return_value = renderBA81(renderflags,xwidth,ywidth,size,videodata); - } - - return return_value; -} - - - -void interpolateBayer(uint16_t width, uint16_t x, uint16_t y, uint8_t *pixel, uint8_t* r, uint8_t* g, uint8_t* b) -{ - if (y&1) - { - if (x&1) - { - *r = *pixel; - *g = (*(pixel-1)+*(pixel+1)+*(pixel+width)+*(pixel-width))>>2; - *b = (*(pixel-width-1)+*(pixel-width+1)+*(pixel+width-1)+*(pixel+width+1))>>2; - } - else - { - *r = (*(pixel-1)+*(pixel+1))>>1; - *g = *pixel; - *b = (*(pixel-width)+*(pixel+width))>>1; - } - } - else - { - if (x&1) - { - *r = (*(pixel-width)+*(pixel+width))>>1; - *g = *pixel; - *b = (*(pixel-1)+*(pixel+1))>>1; - } - else - { - *r = (*(pixel-width-1)+*(pixel-width+1)+*(pixel+width-1)+*(pixel+width+1))>>2; - *g = (*(pixel-1)+*(pixel+1)+*(pixel+width)+*(pixel-width))>>2; - *b = *pixel; - } - } - -} - - - -int renderBA81(uint8_t renderFlags, uint16_t width, uint16_t height, uint32_t frameLen, uint8_t *frame) -{ - uint16_t x, y; - uint8_t r, g, b; - - - // skip first line - frame += width; - - // don't render top and bottom rows, and left and rightmost columns because of color - // interpolation - //uint32_t decodedimage[(width-2)*(height-2)]; - uint16_t* decodedimage = malloc(sizeof(uint16_t)*(width-2)*(height-2)); - - if(decodedimage==NULL) { //not enough free space to decode image in memory - //decode & render image pixel by pixel - uint16_t* line = decodedimage; - for (y=1; y +#include "system.h" + +static volatile bool pixy_connected = false; + + +static BUTTON_STRUCT b_back; +static TOUCH_AREA_STRUCT a_area; + +static void b_back_cb(void* button) { + gui_screen_back(); +} + +POINT_STRUCT pixy_pos; +POINT_STRUCT old_pos; +static void touchCB(void* touchArea, TOUCH_ACTION triggeredAction) { + POINT_STRUCT p = touch_get_last_point(); + switch(triggeredAction) { + case PEN_ENTER: + case PEN_DOWN: + old_pos = p; + break; + case PEN_MOVE: + { + int16_t deltaX = p.x - old_pos.x; + int16_t deltaY = p.y - old_pos.y; + old_pos=p; + printf("%d %d\n",deltaX,deltaY); + if(pixy_connected) { + int16_t new_x = pixy_pos.x+deltaX*2; + int16_t new_y = pixy_pos.y-deltaY*2; + if(new_x<0) new_x=0; + if(new_x>1000) new_x=1000; + if(new_y<0) new_y=0; + if(new_y>1000) new_y=1000; + pixy_pos.x = new_x; + pixy_pos.y= new_y; + } + } + break; + case PEN_UP: + case PEN_LEAVE: + printf("Leave/up\n"); + default: break; + } + + +} + + +static void enter(void* screen) { + tft_clear(WHITE); + + //Back button + b_back.base.x1=10; //Start X of Button + b_back.base.y1=210; //Start Y of Button + b_back.base.x2=AUTO; //b_back.base.x1+160; //Auto Calculate X2 with String Width + b_back.base.y2=AUTO; //Auto Calculate Y2 with String Height + b_back.txtcolor=WHITE; //Set foreground color + b_back.bgcolor=HEX(0xAE1010); //Set background color (Don't take 255 or 0 on at least one channel, to make shadows possible) + b_back.font=0; //Select Font + b_back.text="Back"; //Set Text (For formatted strings take sprintf) + b_back.callback=b_back_cb; //Call b_back_cb as Callback + gui_button_add(&b_back); //Register Button (and run the callback from now on) + + //Area test + a_area.hookedActions = PEN_DOWN | PEN_MOVE | PEN_ENTER | PEN_UP | PEN_LEAVE; + a_area.x1 = 0; + a_area.y1 = 0; + a_area.x2 = 317; + a_area.y2 = 197; + a_area.callback = touchCB; + touch_register_area(&a_area); + + + + //Pixy stuff + pixy_connected = (pixy_init()==0); //try to connect to pixy + if(pixy_connected) { + pixy_pos.x=pixy_pos.y=500; + } +} + +static void leave(void* screen) { + gui_button_remove(&b_back); + touch_unregister_area(&a_area); +} + +int pixy_led_test(); +int pixy_frame_test(); + +static void update(void* screen) { + + //Note: The only way to detect that pixy has been disconnected is if a command fails. There's no pixy_is_connected method yet :'( + + if(!pixy_connected) { //Pixy not connected + pixy_close(); //Ensure that all pixy resources are freed (failsafe) + if(pixy_init()==0) { //try to connect to pixy + pixy_connected=true; + pixy_pos.x=pixy_pos.y=500; + printf("pixy reinitialized\n"); + } + } + + if(pixy_connected) { + pixy_service(); //Send/receive event data from/to pixy failed + + //Code for tests see below + if(pixy_led_test()!=0) { + pixy_connected=false; + } + + if(pixy_frame_test()!=0) { + pixy_connected=false; + } + + + pixy_rcs_set_position(0,pixy_pos.x); + pixy_rcs_set_position(1,pixy_pos.y); + + //system_delay(500); + } +} + + +static SCREEN_STRUCT screen = { + enter, + leave, + update +}; + + +SCREEN_STRUCT* get_screen_pixytest() { + return &screen; +} + + + +//----------------------------------------------------------------------------------------------------------------- + +int colorind; +const uint32_t colors [] = {0xFF0000, 0x00FF00,0x0000FF,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF,0x000000}; +const int num_colors = sizeof(colors)/sizeof(uint32_t); + +int pixy_led_test() { + if(colorind==0) { + pixy_led_set_max_current(5); + } + + int32_t response; + int return_value; + return_value = pixy_command("led_set", INT32(colors[colorind++]), END_OUT_ARGS, &response, END_IN_ARGS); + colorind%=num_colors; + + if(return_value!=0) { + colorind=0; //reset color ind, to start at zero when plugging pixy in again + } + + return return_value; +} + +//---------------------------------------------------------------------------------------------------------------------------- + +int renderBA81(uint8_t renderFlags, uint16_t width, uint16_t height, uint32_t frameLen, uint8_t *frame); + + +int pixy_frame_test() { + + uint8_t* videodata; + int32_t response; + int32_t fourccc; + int8_t renderflags; + uint16_t xwidth; + uint16_t ywidth; + uint32_t size; + + + int return_value = pixy_command("cam_getFrame", // String id for remote procedure + INT8(0x21), // mode + INT16(0), // xoffset + INT16(0), // yoffset + INT16(320), // width + INT16(200), // height + END_OUT_ARGS, // separator + &response, // pointer to mem address for return value + &fourccc, + &renderflags, + &xwidth, + &ywidth, + &size, + &videodata, // pointer to mem address for returned frame + END_IN_ARGS); + + if(return_value==0) { + return_value = renderBA81(renderflags,xwidth,ywidth,size,videodata); + } + + return return_value; +} + + + + +void interpolateBayer(uint16_t width, uint16_t x, uint16_t y, uint8_t *pixel, uint8_t* r, uint8_t* g, uint8_t* b) +{ + if (y&1) + { + if (x&1) + { + *r = *pixel; + *g = (*(pixel-1)+*(pixel+1)+*(pixel+width)+*(pixel-width))>>2; + *b = (*(pixel-width-1)+*(pixel-width+1)+*(pixel+width-1)+*(pixel+width+1))>>2; + } + else + { + *r = (*(pixel-1)+*(pixel+1))>>1; + *g = *pixel; + *b = (*(pixel-width)+*(pixel+width))>>1; + } + } + else + { + if (x&1) + { + *r = (*(pixel-width)+*(pixel+width))>>1; + *g = *pixel; + *b = (*(pixel-1)+*(pixel+1))>>1; + } + else + { + *r = (*(pixel-width-1)+*(pixel-width+1)+*(pixel+width-1)+*(pixel+width+1))>>2; + *g = (*(pixel-1)+*(pixel+1)+*(pixel+width)+*(pixel-width))>>2; + *b = *pixel; + } + } + +} + + + + + + +int renderBA81(uint8_t renderFlags, uint16_t width, uint16_t height, uint32_t frameLen, uint8_t *frame) +{ + uint16_t x, y; + uint8_t r, g, b; + + + // skip first line + frame += width; + + // don't render top and bottom rows, and left and rightmost columns because of color + // interpolation + //uint32_t decodedimage[(width-2)*(height-2)]; + uint16_t* decodedimage = malloc(sizeof(uint16_t)*(width-2)*(height-2)); + + if(decodedimage==NULL) { //not enough free space to decode image in memory + //decode & render image pixel by pixel + uint16_t* line = decodedimage; + for (y=1; y + +#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); +} + +*/ diff --git a/common/gui/button.h b/common/gui/button.h new file mode 100644 index 0000000..e451943 --- /dev/null +++ b/common/gui/button.h @@ -0,0 +1,44 @@ +#ifndef BUTTON_H +#define BUTTON_H + + +#include "touch.h" + + +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); +*/ + + + +#endif /* BUTTON_H */ diff --git a/common/gui/checkbox.c b/common/gui/checkbox.c new file mode 100644 index 0000000..96325a6 --- /dev/null +++ b/common/gui/checkbox.c @@ -0,0 +1,88 @@ +#include "tft.h" +#include "touch.h" +#include "checkbox.h" +#include + +#define BRIGHTNESS_VAL 2 //How much the Brightness is in/decreased for checkbox shadows (3 -> Add 1/3 off Full Value) +#define ACTIVE_COLOR RGB(251,208,123) +#define BORDER_COLOR RGB(29,82,129) +#define BACKGROUND_COLOR WHITE + +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: + area->hookedActions=PEN_UP|PEN_LEAVE; + 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: + checkbox->checked=!checkbox->checked; + gui_checkbox_update(checkbox); + if(checkbox->callback!=NULL) + checkbox->callback(checkbox,checkbox->checked); + case PEN_LEAVE: + area->hookedActions=PEN_DOWN; + 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)) + { + unsigned char size=0; + checkbox->base.hookedActions=PEN_DOWN; + checkbox->base.callback = checkboxes_cb; + if(checkbox->base.x2>checkbox->base.x1) + size = checkbox->base.x2 - checkbox->base.x1; + if(checkbox->base.y2>checkbox->base.y1) + { + if((checkbox->base.y2 - checkbox->base.y1)>size) + size = checkbox->base.y2 - checkbox->base.y1; + } + if((size&0x01)) + size++; + checkbox->base.x2 = checkbox->base.x1 + size; + checkbox->base.y2 = checkbox->base.y1 + size; + gui_checkbox_redraw(checkbox); + return touch_register_area(&checkbox->base); + } + return false; +} + +void gui_checkbox_redraw(CHECKBOX_STRUCT* checkbox) +{ + 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) + gui_checkbox_update(checkbox); +} + +void gui_checkbox_remove(CHECKBOX_STRUCT* checkbox) +{ + touch_unregister_area((TOUCH_AREA_STRUCT*)checkbox); +} + +void gui_checkbox_update(CHECKBOX_STRUCT* checkbox) +{ + unsigned int c = (checkbox->checked)? checkbox->fgcolor:BACKGROUND_COLOR; + 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; + 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); +} diff --git a/common/gui/checkbox.h b/common/gui/checkbox.h new file mode 100644 index 0000000..1a063ab --- /dev/null +++ b/common/gui/checkbox.h @@ -0,0 +1,15 @@ + +typedef void (*CHECKBOX_CALLBACK)(void *checkbox, bool checked); //!< Function pointer used... +typedef struct { + TOUCH_AREA_STRUCT base; + uint16_t fgcolor; + bool checked; + CHECKBOX_CALLBACK callback; //Callback +} CHECKBOX_STRUCT; + + +bool gui_checkbox_add(CHECKBOX_STRUCT* checkbox); +void gui_checkbox_remove(CHECKBOX_STRUCT* checkbox); +void gui_checkbox_update(CHECKBOX_STRUCT* checkbox); +void gui_checkbox_redraw(CHECKBOX_STRUCT* checkbox); +#define CHECKBOX_WIN_FG_COLOR RGB(32,161,34) diff --git a/common/gui/numupdown.c b/common/gui/numupdown.c new file mode 100644 index 0000000..a6c8024 --- /dev/null +++ b/common/gui/numupdown.c @@ -0,0 +1,120 @@ +#include "tft.h" +#include "touch.h" +#include "button.h" +#include "numupdown.h" +#include //for sprintf +#include //for offsetof macro +#include //for abs + + +#define BASE_COLOR RGB(90,90,90) +void button_up_cb(void* button) +{ + NUMUPDOWN_STRUCT* element = button-offsetof(NUMUPDOWN_STRUCT,buttonUp); + if(element->valuemax) { + element->value++; + gui_numupdown_update(element); + if(element->callback!=NULL) { + element->callback(element,element->value); + } + } +} + +void button_down_cb(void* button) +{ + NUMUPDOWN_STRUCT* element = button-offsetof(NUMUPDOWN_STRUCT,buttonDown); + if(element->value>element->min) { + element->value--; + gui_numupdown_update(element); + if(element->callback!=NULL) { + element->callback(element,element->value); + } + } +} + + +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); + while(val>=10) { + val/=10; + width++; + } + return width; +} + + +bool gui_numupdown_add(NUMUPDOWN_STRUCT* numupdown) +{ + if(touch_have_empty(2)) //We require 2 TouchAreas (for Buttons) + { + if(numupdown->min > numupdown->max) return false; + + if(numupdown->value > numupdown->max) { + numupdown->value = numupdown->max; + } + if(numupdown->value < numupdown->min) { + numupdown->value = numupdown->min; + } else if(numupdown->value > numupdown->max) { + numupdown->value = numupdown->max; + } + + 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); + + 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); + 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); + + 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; + } + +void gui_numupdown_remove(NUMUPDOWN_STRUCT* numupdown) + { + gui_button_remove(&numupdown->buttonUp); + gui_button_remove(&numupdown->buttonDown); + } + +void gui_numupdown_update(NUMUPDOWN_STRUCT* numupdown) + { + + + 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); + + 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); + } + + void gui_numupdown_redraw(NUMUPDOWN_STRUCT* numupdown) + { + gui_button_redraw(&numupdown->buttonUp); + gui_button_redraw(&numupdown->buttonDown); + gui_numupdown_update(numupdown); + } diff --git a/common/gui/numupdown.h b/common/gui/numupdown.h new file mode 100644 index 0000000..09a4abb --- /dev/null +++ b/common/gui/numupdown.h @@ -0,0 +1,22 @@ +#include "button.h" + +typedef void (*NUMUPDOWN_CALLBACK)(void *numupdown, int16_t value); //!< Function pointer used... +typedef struct { + uint16_t x; + uint16_t y; + uint16_t fgcolor; + int16_t value; + int16_t min; + int16_t max; + NUMUPDOWN_CALLBACK callback; //Callback + + //Internally used: + BUTTON_STRUCT buttonUp; + BUTTON_STRUCT buttonDown; +} NUMUPDOWN_STRUCT; + + +bool gui_numupdown_add(NUMUPDOWN_STRUCT* numupdown); +void gui_numupdown_remove(NUMUPDOWN_STRUCT* numupdown); +void gui_numupdown_update(NUMUPDOWN_STRUCT* numupdown); +void gui_numupdown_redraw(NUMUPDOWN_STRUCT* numupdown); diff --git a/common/gui/screen.c b/common/gui/screen.c new file mode 100644 index 0000000..a8a34ea --- /dev/null +++ b/common/gui/screen.c @@ -0,0 +1,68 @@ +#include "screen.h" + + +static volatile SCREEN_STRUCT* screen_list = NULL; +static volatile SCREEN_STRUCT* screen_current = NULL; +static volatile SCREEN_STRUCT* screen_goto = NULL; + +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_goto; //Backup volatile variable + screen_goto=NULL; + if(go->next!=NULL) { //we're going back + if(go->next!=screen_current) return; //list corrupted? + screen_current->on_leave(screen_current); + go->next=NULL; + } else { //we're going forward + if(screen_current!=NULL) { //this is not the first screen + screen_current->on_leave(screen_current); + screen_current->next = go; + } else { //first screen ever seen + screen_list=go; + } + } + go->on_enter(go); + screen_current = go; + } + + 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) return false; + screen->next = NULL; + screen_goto=screen; //send message to main loop, to switch the screen + return true; +} + + + +bool gui_screen_back() { + if(screen_list==NULL) 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. + if(current!=screen_current) return false; //List corrupted? + screen_goto=last; //send message to main loop, to switch the screen + return true; +} + + + + + + + diff --git a/common/gui/screen.h b/common/gui/screen.h new file mode 100644 index 0000000..375ccbf --- /dev/null +++ b/common/gui/screen.h @@ -0,0 +1,33 @@ +#ifndef SCREEN_H +#define SCREEN_H + + +#include +#include + +typedef void (*SCREEN_CALLBACK)(void* screen); //!< Function pointer used... + +typedef struct SCREEN_S{ + SCREEN_CALLBACK on_enter; + SCREEN_CALLBACK on_leave; + SCREEN_CALLBACK on_update; + + + struct SCREEN_S* next; //Used internally. do not modify + +} SCREEN_STRUCT; + + +//Navigate to the given string as soon as the app enters the main loop again. Method can be called from an interrupt +bool gui_screen_navigate(SCREEN_STRUCT* screen); + +//Navigate one screen back as soon as the app enters the main loop again. Method can be called from an interrupt +bool gui_screen_back(); + +//Returns the current active screen +SCREEN_STRUCT* gui_screen_get_current(); + +//Updates/switches the screens. Call this from the app main loop, as fast as you can. +void gui_screen_update(); + +#endif /* SCREEN_H */ diff --git a/common/lowlevel/ll_tft.h b/common/lowlevel/ll_tft.h index 3353b83..11e3b1e 100644 --- a/common/lowlevel/ll_tft.h +++ b/common/lowlevel/ll_tft.h @@ -12,3 +12,13 @@ void ll_tft_draw_rectangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2, uint void ll_tft_fill_rectangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2, uint16_t color); void ll_tft_draw_bitmap_unscaled(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint16_t *dat); void ll_tft_draw_circle(uint16_t x, uint16_t y, uint16_t r, uint16_t color); + + +uint8_t ll_tft_num_fonts(); +uint8_t ll_tft_font_height(uint8_t fontnum); +uint8_t ll_tft_font_width(uint8_t fontnum); +void ll_tft_draw_char(uint16_t x, uint16_t y, uint16_t color, uint16_t bgcolor, uint8_t font, char c); + + + + diff --git a/common/tft/tft.c b/common/tft/tft.c index 2604be0..67fc977 100644 --- a/common/tft/tft.c +++ b/common/tft/tft.c @@ -1,7 +1,7 @@ #include "tft.h" #include "ll_tft.h" - -//it might seems pointless to forward all the functions but we might also introduce functions which have some logic here +#include +#include bool tft_init() { return ll_tft_init(); @@ -37,3 +37,32 @@ void tft_draw_bitmap_unscaled(uint16_t x, uint16_t y, uint16_t width, uint16_t h void tft_draw_circle(uint16_t x, uint16_t y, uint16_t r, uint16_t color) { ll_tft_draw_circle(x, y, r, color); } + +uint8_t tft_num_fonts() { + return ll_tft_num_fonts(); +} + +uint8_t tft_font_height(uint8_t fontnum) { + return ll_tft_font_height(fontnum); +} + +uint8_t tft_font_width(uint8_t fontnum) { + return ll_tft_font_width(fontnum); +} + +void tft_print_line(uint16_t x, uint16_t y, uint16_t color, uint16_t bgcolor, uint8_t font, const char* text) { + if(font>=ll_tft_num_fonts()) return; + for(int i=0; i>16),((h)>>8),(h))) +#define TRANSPARENT ((uint16_t)0x80C2) bool tft_init(); @@ -19,3 +20,10 @@ void tft_draw_rectangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2, uint16_ void tft_fill_rectangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2, uint16_t color); void tft_draw_bitmap_unscaled(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint16_t* dat); void tft_draw_circle(uint16_t x, uint16_t y, uint16_t r, uint16_t color); + + +uint8_t tft_num_fonts(); +uint8_t tft_font_height(uint8_t fontnum); +uint8_t tft_font_width(uint8_t fontnum); +void tft_print_line(uint16_t x, uint16_t y, uint16_t color, uint16_t bgcolor, uint8_t font, const char* text); +void tft_print_formatted(uint16_t x, uint16_t y, uint16_t color, uint16_t bgcolor, uint8_t font, const char* format, ...); diff --git a/common/touch/touch.c b/common/touch/touch.c index dbecbc6..ad8c589 100644 --- a/common/touch/touch.c +++ b/common/touch/touch.c @@ -5,8 +5,7 @@ #define NUM_AREAS 50 //Number of Structs Reserved in Memory for TouchAreas (e.g Buttons) TOUCH_AREA_STRUCT* areas[NUM_AREAS] = {NULL}; -volatile int touchY=0; //Last Y Coordinate in pixels -volatile int touchX=0; //Last X Coordinate in pixels +volatile POINT_STRUCT pos; volatile TOUCH_STATE oldState=TOUCH_UP; bool touch_init() { @@ -14,12 +13,12 @@ bool touch_init() { } -bool touch_add_raw_event(uint16_t x, uint16_t y, TOUCH_STATE state) { +bool touch_add_raw_event(uint16_t touchX, uint16_t touchY, TOUCH_STATE state) { bool penDown = (state==TOUCH_DOWN); bool oldPenDown = (oldState==TOUCH_DOWN); oldState=state; - uint16_t touchX = x; - uint16_t touchY = y; + pos.x=touchX; + pos.y=touchY; if(penDown) { // tftDrawPixel(touchX,touchY,WHITE); @@ -127,3 +126,10 @@ void touch_unregister_area(TOUCH_AREA_STRUCT* area)//Unregisters an Area } } } + + +POINT_STRUCT touch_get_last_point() { + return pos; +} + + diff --git a/common/touch/touch.h b/common/touch/touch.h index 4fd5c25..603a83a 100644 --- a/common/touch/touch.h +++ b/common/touch/touch.h @@ -1,3 +1,7 @@ +#ifndef TOUCH_H +#define TOUCH_H + + #include #include @@ -9,19 +13,27 @@ typedef void (*TOUCH_CALLBACK)(void* touchArea, TOUCH_ACTION triggeredAction); typedef struct { TOUCH_ACTION hookedActions; //Actions to listen to - unsigned int x1; //Top Left X Coordiate of Area - unsigned int y1; //Top Left Y Coordiate of Area - unsigned int x2; //Bottom Right X Coordiate of Area - unsigned int y2; //Bottom Right Y Coordiate of Area + uint16_t x1; //Top Left X Coordiate of Area + uint16_t y1; //Top Left Y Coordiate of Area + uint16_t x2; //Bottom Right X Coordiate of Area + uint16_t y2; //Bottom Right Y Coordiate of Area TOUCH_CALLBACK callback; //Callback uint8_t flags; //Internal Used, don't change } TOUCH_AREA_STRUCT; +typedef struct { + uint16_t x; + uint16_t y; +} POINT_STRUCT; + + bool touch_init(); bool touch_add_raw_event(uint16_t x, uint16_t y,TOUCH_STATE state); bool touch_have_empty(unsigned char num); bool touch_register_area(TOUCH_AREA_STRUCT* area); void touch_unregister_area(TOUCH_AREA_STRUCT* area); +POINT_STRUCT touch_get_last_point(); +#endif /* TOUCH_H */ diff --git a/discovery/src/ll_tft.c b/discovery/src/ll_tft.c index 5c05488..e0edc8d 100644 --- a/discovery/src/ll_tft.c +++ b/discovery/src/ll_tft.c @@ -490,6 +490,23 @@ void ll_tft_draw_pixel(uint16_t x,uint16_t y,uint16_t color) TFT_RAM = color; } +uint8_t ll_tft_num_fonts() { + return 1; +} + +uint8_t ll_tft_font_height(uint8_t fontnum) { + return 8; +} + +uint8_t ll_tft_font_width(uint8_t fontnum) { + return 5; +} + +void ll_tft_draw_char(uint16_t x, uint16_t y, uint16_t color, uint16_t bgcolor, uint8_t font, char c) { + + +} + void ll_tft_draw_rectangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2, uint16_t color) { // TODO diff --git a/doc/docu.odt b/doc/docu.odt index b090e3f..5915516 100644 Binary files a/doc/docu.odt and b/doc/docu.odt differ diff --git a/emulator/qt/emulatorqt.pro b/emulator/qt/emulatorqt.pro index 2059c2f..47b672c 100644 --- a/emulator/qt/emulatorqt.pro +++ b/emulator/qt/emulatorqt.pro @@ -21,7 +21,8 @@ HEADERS += \ INCLUDEPATH+= ../../common/lowlevel/ \ - ../../common/touch/ + ../../common/touch/ \ + ../../common/tft/ FORMS += \ diff --git a/emulator/qt/ll_tft.cpp b/emulator/qt/ll_tft.cpp index 8ba3ed3..6a9ad0c 100644 --- a/emulator/qt/ll_tft.cpp +++ b/emulator/qt/ll_tft.cpp @@ -43,4 +43,37 @@ void ll_tft_draw_circle(uint16_t x, uint16_t y, uint16_t r, uint16_t color) { mainwindow->draw_circle(x,y,r,color); } +uint8_t ll_tft_num_fonts() { + return 1; +} + +QFont get_font(uint8_t fontnum) { + switch(fontnum) { + case 0: + return QFont("Monospace",8); + default: + return QFont(); + } +} + + +uint8_t ll_tft_font_height(uint8_t fontnum) { + QFont f = get_font(fontnum); + if(f == QFont()) return -1; + QFontMetrics m(f); + return m.height(); +} + +uint8_t ll_tft_font_width(uint8_t fontnum) { + QFont f = get_font(fontnum); + if(f == QFont()) return -1; + QFontMetrics m(f); + return m.averageCharWidth(); +} + +void ll_tft_draw_char(uint16_t x, uint16_t y, uint16_t color, uint16_t bgcolor, uint8_t font, char c) { + QFont f = get_font(font); + if(f == QFont()) return; + mainwindow->draw_char(x,y,color,bgcolor,f,c); +} diff --git a/emulator/qt/mainwindow.cpp b/emulator/qt/mainwindow.cpp index 43493db..aa3fa5f 100644 --- a/emulator/qt/mainwindow.cpp +++ b/emulator/qt/mainwindow.cpp @@ -7,6 +7,7 @@ extern "C" { #include "touch.h" + #include "tft.h" } #define DISPLAY_WIDTH 320 @@ -110,6 +111,24 @@ void MainWindow::draw_circle(uint16_t x, uint16_t y, uint16_t r, uint16_t color) update(); } +void MainWindow::draw_char(uint16_t x, uint16_t y, uint16_t color, uint16_t bgcolor, QFont font, char c) +{ + //render_mutex.lock(); + QPainter painter(&(image)); + painter.setFont(font); + + if(bgcolor!=TRANSPARENT) { + painter.setBackgroundMode(Qt::OpaqueMode); + painter.setBackground(QColorFromRGB565(bgcolor)); + } + + painter.setPen(QColorFromRGB565(color)); + y+=QFontMetrics(font).ascent(); //use y pos as highest point of char, instead of baseline + painter.drawText(QPoint(x,y), QString(QChar(c))); + //render_mutex.unlock(); + update(); +} + void MainWindow::paintEvent(QPaintEvent *) { //render_mutex.lock(); diff --git a/emulator/qt/mainwindow.h b/emulator/qt/mainwindow.h index 397a5ad..c4c015b 100644 --- a/emulator/qt/mainwindow.h +++ b/emulator/qt/mainwindow.h @@ -22,7 +22,7 @@ public: void fill_rectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color); void draw_bitmap_unscaled(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint16_t *dat); void draw_circle(uint16_t x, uint16_t y, uint16_t r, uint16_t color); - + void draw_char(uint16_t x, uint16_t y, uint16_t color, uint16_t bgcolor, QFont font, char c); protected: void paintEvent(QPaintEvent * evt);