Implemented basic sampling and averaging of touch coordinates using timer7
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
#include "tft.h"
|
#include "tft.h"
|
||||||
#include "system.h"
|
#include "system.h"
|
||||||
#include "touch.h"
|
#include "touch.h"
|
||||||
#include "screen_guitest.h"
|
#include "screen_main.h"
|
||||||
#include "filesystem.h"
|
#include "filesystem.h"
|
||||||
|
|
||||||
void app_init() {
|
void app_init() {
|
||||||
@@ -11,15 +11,14 @@ void app_init() {
|
|||||||
touch_init();
|
touch_init();
|
||||||
filesystem_init();
|
filesystem_init();
|
||||||
|
|
||||||
gui_screen_navigate(get_screen_guitest());
|
gui_screen_navigate(get_screen_main());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//app event loop
|
//app event loop
|
||||||
void app_process() {
|
void app_process() {
|
||||||
|
|
||||||
system_process(); //Let the system handle it's pending events
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ static void enter(void* screen) {
|
|||||||
n_updown.max=11;
|
n_updown.max=11;
|
||||||
n_updown.min =-5;
|
n_updown.min =-5;
|
||||||
n_updown.callback=n_updown_cb;
|
n_updown.callback=n_updown_cb;
|
||||||
gui_numupdown_add(&n_updown);
|
//gui_numupdown_add(&n_updown);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
4
discovery/.gitignore
vendored
4
discovery/.gitignore
vendored
@@ -4,3 +4,7 @@ obj/
|
|||||||
libs/*/obj
|
libs/*/obj
|
||||||
libs/*/*.a
|
libs/*/*.a
|
||||||
libs/*/*.o
|
libs/*/*.o
|
||||||
|
|
||||||
|
*~
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
|
|||||||
@@ -14,19 +14,45 @@
|
|||||||
#define REQ_X_COORD 0x90 // Request x coordinate
|
#define REQ_X_COORD 0x90 // Request x coordinate
|
||||||
#define REQ_Y_COORD 0xD0 // Request y coordinate
|
#define REQ_Y_COORD 0xD0 // Request y coordinate
|
||||||
#define REQ_1_DATAB 0x00 // Request one databyte
|
#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 ----------------------------------------------------------- */
|
/* 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 -------------------------------------------------------- */
|
/* Prototypes -------------------------------------------------------- */
|
||||||
bool ll_touch_init();
|
bool ll_touch_init();
|
||||||
static void init_exti();
|
static void init_exti();
|
||||||
|
static void init_timer();
|
||||||
static uint8_t touch_send(uint8_t dat);
|
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();
|
||||||
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 --------------------------------------------------------- */
|
/* 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()
|
static uint16_t touch_get_x_coord()
|
||||||
{
|
{
|
||||||
uint16_t buf_x = 0;
|
uint16_t buf_x = 0;
|
||||||
@@ -64,16 +90,12 @@ static uint8_t touch_send(uint8_t dat)
|
|||||||
return SPI_I2S_ReceiveData(SPI2);
|
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 xs[10];
|
||||||
char ys[10];
|
char ys[10];
|
||||||
|
|
||||||
tft_clear(BLACK);
|
tft_clear(BLACK);
|
||||||
|
|
||||||
x = touch_get_x_coord();
|
|
||||||
y = touch_get_y_coord();
|
|
||||||
|
|
||||||
itoa(x, xs, 10);
|
itoa(x, xs, 10);
|
||||||
itoa(y, ys, 10);
|
itoa(y, ys, 10);
|
||||||
@@ -135,9 +157,11 @@ bool ll_touch_init()
|
|||||||
SPI_Cmd(SPI2, ENABLE); // enable spi
|
SPI_Cmd(SPI2, ENABLE); // enable spi
|
||||||
|
|
||||||
init_exti(); // init external interrupt for penirq
|
init_exti(); // init external interrupt for penirq
|
||||||
|
init_timer(); // init the timer 6 for sampling x and y coordinates
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void init_exti()
|
static void init_exti()
|
||||||
{
|
{
|
||||||
/* init structures */
|
/* init structures */
|
||||||
@@ -174,26 +198,54 @@ static void init_exti()
|
|||||||
NVIC_Init(&nvic); // Config NVIC
|
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 ------------------------------------------ */
|
/* Interrupt service routines ------------------------------------------ */
|
||||||
void EXTI0_IRQHandler()
|
void EXTI0_IRQHandler()
|
||||||
{
|
{
|
||||||
if (EXTI_GetITStatus(EXTI_Line0) != RESET) { // Make sure the interrupt flag is set
|
if (EXTI_GetITStatus(EXTI_Line0) == SET) { // Make sure the interrupt flag is set
|
||||||
touch_test();
|
|
||||||
|
|
||||||
//uint16_t x = touch_get_x_coord();
|
if(!pen_state){ // Check if PENDOWN or PENUP
|
||||||
//uint16_t y = touch_get_y_coord();
|
TIM_Cmd(TIM7, ENABLE); // Start the timer
|
||||||
|
while(!tim_flag); // Wait for the sampling to finish
|
||||||
uint16_t x = 0;
|
touch_add_raw_event(avg_vals(x_samples, NSAMPLE), avg_vals(y_samples, NSAMPLE), TOUCH_DOWN);
|
||||||
uint16_t y = 0;
|
|
||||||
|
|
||||||
if(pen_state){
|
|
||||||
touch_add_raw_event(x, y, TOUCH_DOWN);
|
|
||||||
}else{
|
}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
|
pen_state = !pen_state; // Toggle penstate
|
||||||
|
tim_flag = false; // Clear timer flag
|
||||||
EXTI_ClearITPendingBit(EXTI_Line0); // Clear interrupt 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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user