From 7d2d1a13e44013669dcca912855fa8e95fd9441c Mon Sep 17 00:00:00 2001 From: id101010 Date: Fri, 29 May 2015 01:34:55 +0200 Subject: [PATCH] Implemented basic sampling and averaging of touch coordinates using timer7 --- common/app/app.c | 7 ++- common/app/screen_guitest.c | 2 +- discovery/.gitignore | 4 ++ discovery/src/ll_touch.c | 90 +++++++++++++++++++++++++++++-------- 4 files changed, 79 insertions(+), 24 deletions(-) diff --git a/common/app/app.c b/common/app/app.c index 61c1d0f..21da23b 100644 --- a/common/app/app.c +++ b/common/app/app.c @@ -2,7 +2,7 @@ #include "tft.h" #include "system.h" #include "touch.h" -#include "screen_guitest.h" +#include "screen_main.h" #include "filesystem.h" void app_init() { @@ -11,15 +11,14 @@ void app_init() { touch_init(); filesystem_init(); - gui_screen_navigate(get_screen_guitest()); + gui_screen_navigate(get_screen_main()); } - //app event loop void app_process() { system_process(); //Let the system handle it's pending events - //gui_screen_update(); //update the currently active screen + gui_screen_update(); //update the currently active screen } diff --git a/common/app/screen_guitest.c b/common/app/screen_guitest.c index 282636e..201b23e 100644 --- a/common/app/screen_guitest.c +++ b/common/app/screen_guitest.c @@ -104,7 +104,7 @@ static void enter(void* screen) { n_updown.max=11; n_updown.min =-5; n_updown.callback=n_updown_cb; - gui_numupdown_add(&n_updown); + //gui_numupdown_add(&n_updown); } diff --git a/discovery/.gitignore b/discovery/.gitignore index 0ccb2ea..1004af7 100644 --- a/discovery/.gitignore +++ b/discovery/.gitignore @@ -4,3 +4,7 @@ obj/ libs/*/obj libs/*/*.a libs/*/*.o + +*~ +*.swp +*.swo diff --git a/discovery/src/ll_touch.c b/discovery/src/ll_touch.c index 6e2e7f2..1879e00 100644 --- a/discovery/src/ll_touch.c +++ b/discovery/src/ll_touch.c @@ -14,19 +14,45 @@ #define REQ_X_COORD 0x90 // Request x coordinate #define REQ_Y_COORD 0xD0 // Request y coordinate #define REQ_1_DATAB 0x00 // Request one databyte +#define DWIDTH 320 +#define DHEIGHT 240 +#define CCENTER 20 +#define x1 0x0231 +#define dx 0x0C08 +#define y1 0x0287 +#define dy 0x0B56 +#define NSAMPLE 16 /* Globals ----------------------------------------------------------- */ -volatile bool pen_state = false; // PenDown = True; PenUp = False; +volatile bool pen_state = false; // PenDown = True; PenUp = False; +volatile bool tim_flag = false; +volatile uint16_t x_samples[NSAMPLE-1]; +volatile uint16_t y_samples[NSAMPLE-1]; +volatile int i; /* Prototypes -------------------------------------------------------- */ bool ll_touch_init(); static void init_exti(); +static void init_timer(); static uint8_t touch_send(uint8_t dat); +static uint16_t avg_vals(uint16_t samples[], uint16_t len); static uint16_t touch_get_y_coord(); static uint16_t touch_get_y_coord(); -void touch_test(); +void touch_test(uint16_t x, uint16_t y); /* Functions --------------------------------------------------------- */ +static uint16_t avg_vals(uint16_t samples[], uint16_t len) +{ + uint16_t j = 0; + uint32_t tmp = 0; + + for(j = 0; j < len; j++){ + tmp += samples[j]; + } + + return (uint16_t)(tmp/len); +} + static uint16_t touch_get_x_coord() { uint16_t buf_x = 0; @@ -64,16 +90,12 @@ static uint8_t touch_send(uint8_t dat) return SPI_I2S_ReceiveData(SPI2); } -void touch_test() +void touch_test(uint16_t x, uint16_t y) { - uint16_t x = 0, y = 0; char xs[10]; char ys[10]; tft_clear(BLACK); - - x = touch_get_x_coord(); - y = touch_get_y_coord(); itoa(x, xs, 10); itoa(y, ys, 10); @@ -135,9 +157,11 @@ bool ll_touch_init() SPI_Cmd(SPI2, ENABLE); // enable spi init_exti(); // init external interrupt for penirq + init_timer(); // init the timer 6 for sampling x and y coordinates return true; } + static void init_exti() { /* init structures */ @@ -174,26 +198,54 @@ static void init_exti() NVIC_Init(&nvic); // Config NVIC } +static void init_timer() +{ + TIM_TimeBaseInitTypeDef t; + const int APB1_CLK = 42E6; + + RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE); // Enable clock for TIM6 + + /* Timer 7 configuration */ + TIM_TimeBaseStructInit(&t); // Init TimeBaseStruct + t.TIM_Prescaler = APB1_CLK / 1000 - 1; // 0..41999 prescaler + t.TIM_Period = 20- 1; // 10ms cycle time + TIM_TimeBaseInit(TIM7, &t); // Init TIM7 + TIM_ITConfig(TIM7, TIM_IT_Update, ENABLE); // Enable update IRQ for TIM7 + NVIC_EnableIRQ(TIM7_IRQn); // Enable IRQs for TIM7 +} + /* Interrupt service routines ------------------------------------------ */ void EXTI0_IRQHandler() { - if (EXTI_GetITStatus(EXTI_Line0) != RESET) { // Make sure the interrupt flag is set - touch_test(); + if (EXTI_GetITStatus(EXTI_Line0) == SET) { // Make sure the interrupt flag is set - //uint16_t x = touch_get_x_coord(); - //uint16_t y = touch_get_y_coord(); - - uint16_t x = 0; - uint16_t y = 0; - - if(pen_state){ - touch_add_raw_event(x, y, TOUCH_DOWN); + if(!pen_state){ // Check if PENDOWN or PENUP + TIM_Cmd(TIM7, ENABLE); // Start the timer + while(!tim_flag); // Wait for the sampling to finish + touch_add_raw_event(avg_vals(x_samples, NSAMPLE), avg_vals(y_samples, NSAMPLE), TOUCH_DOWN); }else{ - touch_add_raw_event(x, y, TOUCH_UP); + TIM_Cmd(TIM7, ENABLE); // Start the timer + while(!tim_flag); // Wait for the sampling to finish + touch_add_raw_event(avg_vals(x_samples, NSAMPLE), avg_vals(y_samples, NSAMPLE), TOUCH_UP); } pen_state = !pen_state; // Toggle penstate - + tim_flag = false; // Clear timer flag EXTI_ClearITPendingBit(EXTI_Line0); // Clear interrupt flag } } + +void TIM7_IRQHandler() +{ + if(TIM_GetFlagStatus(TIM7, TIM_IT_Update) == SET){ // Make sure the interrupt flag is set + for(i = 0; i < (NSAMPLE-1); i++){ + /* get x and y coordinate and apply calibration */ + x_samples[i] = (((long)(DWIDTH-2*CCENTER)*2*(long)((long)touch_get_x_coord()-x1)/dx+1)>>1)+CCENTER; + y_samples[i] = (((long)(DHEIGHT-2*CCENTER)*2*(long)((long)touch_get_y_coord()-y1)/dy+1)>>1)+CCENTER; + } + + tim_flag = true; // Set the global timer flag + TIM_Cmd(TIM7, DISABLE); // Count only once + } + +}