Announcements

Help us improve the Power & Sensing Selection Guide. Share feedback

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
shilpasonone04
Level 1
Level 1
25 sign-ins 10 sign-ins 5 questions asked

Hello Sir,

I am working on project touchpad  uart based. The cursor moves smoothly but use left and right mouse button configuration, during rotating cursor continuously left and right mouse buttons window opens with it.

I am sending my code. Please check and suggest some changes.

/*------------------------------------------------------------------------------------


Version : V0
Description : This file is used to Process Trackpad data and transfers over UART to USB controller

-------------------------------------------------------------------------------------*/

//---------Inclusion of Header Files-------//
#include "project.h"
/* Include boolean function definition */
#include <stdbool.h>
#include <cytypes.h>

//#define SQUARMOUSETEST
//#define PRINTtoTERMINAL

/* Trackpad X and Y coordinate related macros */
#define X_POSITION (0u)
#define Y_POSITION (1u)
#define X_POSITION_NEGATIVE (2u)
#define Y_POSITION_NEGATIVE (4u)

#define BufferSize (5u)

#define DATA_BYTE1_INDEX (0u)
#define DATA_BYTE2_INDEX (1u)
#define DATA_BYTE3_INDEX (2u)
#define DATA_BYTE4_INDEX (3u)
#define DATA_BYTE5_INDEX (4u)

/* Mouse click function macros */
#define MOUSE_FN_NO_CLICK (0u)
#define MOUSE_FN_LEFT_CLICK (1u)
#define MOUSE_FN_RIGHT_CLICK (2u)

/* Threshold for the delta position after which
* position is scaled by a factor of 2.
*/
#define DELTA_POS_MULT_THRESHOLD (5u)

#define FALSE (false)
#define TRUE (true)

/**********square Mouse code for testing*****************/
//static uint8 Mouse_Data[BufferSize] = {0}; /* [0] = Buttons, [1] = X-Axis, [2] = Y-Axis */
//static uint16 Delay = 0;
/*******************************************/

volatile int8 Mouse_Data[BufferSize] = {0}; /* [0] = Buttons, [1] = X-Axis, [2] = Y-Axis */

uint8 flag0 = 1;
uint8 Rflag = 0;
uint8 Lflag = 0;
uint8 sing_click = 0;
uint8 dou_click = 0;


/* Variable indicates if trackpad is touched or not */
uint16 fingerStatus;

/* Contains information of whether DeltaX and DeltaY is negative */
uint8 xyDirectionData;

/* Contains mouse left and right click data */
uint8 mouseClickData;

/* Variable to store X, Y touch coordinates */
uint16 touchPosition[2];

/* Contains previous active X, Y coordinates of trackpad */
uint16 prevTouchPosition[2];

/* Contains DeltaX and DeltaY trackpad coordinates, used for HID (mouse) */
uint16 diffPosition[2];

/* Function prototype for Square mouse data */
void squaremouse(void);

/* Function prototype for Capsense Scaning & processing data */
void capsensedata(void);

/* Function prototype for CapSense parameter initialization */
void capSenseInit(void);

/* Function prototype to process trackpad data and update UART buffer */
void TouchpadDataProcess(void);

/* Function prototype to update UART buffer with trackpad and button sensor status*/
void updateUARTBuffer(void);

int main(void)
{
CyGlobalIntEnable; /* Enable global interrupts. */

/* Variable to store the XY touch coordinates */
uint32 tempCoordinates,gesture;

uint8 index;

BLE_UART_Start();

capSenseInit();
CapSense_InitializeGestures();

/* Capsense Start And initialization and Updation */
// CapSense_Start();
CapSense_InitializeAllBaselines();
CapSense_ScanAllWidgets();

// BLE_UART_UartPutString("Received data\n");

for(;;)
{
// CyDelay(5);

// BLE_UART_SpiUartPutArray((uint8 *)Mouse_Data,BufferSize);
/* Reset all the data */

/* Checked the Capsense is Busy or Not*/
if(CapSense_NOT_BUSY == CapSense_IsBusy())
{
// Capsense Starting and Processes
CapSense_UpdateAllBaselines();
CapSense_ScanAllWidgets();
CapSense_ProcessAllWidgets();
gesture = CapSense_DecodeWidgetGestures(CapSense_TRACKPAD_WDGT_ID);
switch(gesture)
{
case CapSense_NO_GESTURE:
break;
case CapSense_ONE_FINGER_SINGLE_CLICK:
sing_click =1;
break;
case CapSense_ONE_FINGER_DOUBLE_CLICK:
dou_click = 1;
break;
case CapSense_ONE_FINGER_ROTATE_CW:
break;
case CapSense_ONE_FINGER_SCROLL_UP:
break;
case CapSense_ONE_FINGER_SCROLL_DOWN:
break;
case CapSense_ONE_FINGER_SCROLL_RIGHT:
break;
case CapSense_ONE_FINGER_SCROLL_LEFT:
break;



}
fingerStatus = CapSense_IsWidgetActive(CapSense_TRACKPAD_WDGT_ID);
/* If trackpad is active, obtain XY coordinates */
if(fingerStatus == TRUE)
{
#ifndef SQUAREMOUSETEST
tempCoordinates = CapSense_GetXYCoordinates(CapSense_TRACKPAD_WDGT_ID);

/* Extract XY positions separately */
touchPosition[X_POSITION] = LO16(tempCoordinates);
touchPosition[Y_POSITION] = HI16 (tempCoordinates);
#endif
#ifdef SQUAREMOUSETEST
squaremouse();
#endif
}
/* If trackpad is inactive, set touch position to 0. This will reset all the
* previous history
*/
else
{
touchPosition[X_POSITION] = 0u;
touchPosition[Y_POSITION] = 0u;
}

TouchpadDataProcess();
updateUARTBuffer();

}

}
}


/******************************************************************************
* Function Name: capSenseInit
*******************************************************************************
*
* Summary: This API initializes CapSense block

*
* Parameters:
* None.
*
* Return:
* None.
*
* Theory:
* capSenseInit() performs following functions:
* 1: Starts the CapSense block
* 2: Scan the trackpad widget and initialize the previous touch coordinate values
*
* Side Effects: None
*
* Note: None
*
*******************************************************************************/
void capSenseInit(void)
{
/* Temporary variable to store trackpad coordinates */
uint32 tempCoordinates;

/* Starts the CapSense block and performs baseline initialization */
CapSense_Start();

/* Set the default finger presence status and touch coordinates */
fingerStatus = 0x0u;
tempCoordinates = 0xFFFFFFFFu;

/* Get the touch position */
touchPosition[X_POSITION] = LO16(tempCoordinates);
touchPosition[Y_POSITION] = HI16 (tempCoordinates);

/* Initialize the previous touch position */
prevTouchPosition[X_POSITION] = touchPosition[X_POSITION];
prevTouchPosition[Y_POSITION] = touchPosition[Y_POSITION];
}

void TouchpadDataProcess(void)
{
/* Variable to detect raising edge of left button status */
static uint8 prevLeftClick = 0u;
uint8 currentLeftClick = 0u;

/* Variable to detect raising edge of right button status */
static uint8 prevRightClick = 0u;
uint8 currentRightClick = 0u;

/* Variable to detect rising edge/falling edge of trackpad status */
static uint8 prevfingerStatus = 0u;
uint8 currentfingerStatus = 0u;

/* Initialize the sign indication variable to '0' */
xyDirectionData = 0u;

/* If trackpad is active, perform delta position computation */
currentfingerStatus = CapSense_IsWidgetActive(CapSense_TRACKPAD_WDGT_ID);

/* Compute delta positions only when the finger is placed on the trackpad
* Delta position is not computed when there is a touchdown or liftoff condition is detected
*/
if((currentfingerStatus && prevfingerStatus))
{
/* Get the DeltaX and DeltaY coordinates and sign information */
if(prevTouchPosition[X_POSITION] > touchPosition[X_POSITION])
{
diffPosition[X_POSITION] = prevTouchPosition[X_POSITION] - touchPosition[X_POSITION];
// xyDirectionData = xyDirectionData | X_POSITION_NEGATIVE;

// if(xyDirectionData & X_POSITION_NEGATIVE)
// {
diffPosition[X_POSITION] = -(diffPosition[X_POSITION]);
// xyDirectionData = 0;
// }

}
/* If previous X position is less than current X position */
else
{
diffPosition[X_POSITION] = touchPosition[X_POSITION] - prevTouchPosition[X_POSITION];
//diffPosition[X_POSITION] = diffPosition[X_POSITION] << 1u; //added
}

if(prevTouchPosition[Y_POSITION] > touchPosition[Y_POSITION])
{
diffPosition[Y_POSITION] = prevTouchPosition[Y_POSITION] - touchPosition[Y_POSITION];
// xyDirectionData = xyDirectionData | Y_POSITION_NEGATIVE;

// if(xyDirectionData & Y_POSITION_NEGATIVE)
// {
diffPosition[Y_POSITION] = -(diffPosition[Y_POSITION]);
// xyDirectionData = 0;
// }

}
/* If previous Y position is less than current Y position */
else
{
diffPosition[Y_POSITION] = touchPosition[Y_POSITION] - prevTouchPosition[Y_POSITION];
//diffPosition[Y_POSITION] = diffPosition[Y_POSITION] << 1u; //added
}
}
/* If either trackpad is not active, or a touchdown or liftoff condition is detected
* delta position is initialized to 0.
*/
else
{
diffPosition[X_POSITION] = 0u;
diffPosition[Y_POSITION] = 0u;
}

/* If delta position is greater than the threshold limit, scale the
* delta position by a factor of 2.
*/
if(diffPosition[X_POSITION] > DELTA_POS_MULT_THRESHOLD)
diffPosition[X_POSITION] = diffPosition[X_POSITION] << 1u;

if(diffPosition[Y_POSITION] > DELTA_POS_MULT_THRESHOLD)
diffPosition[Y_POSITION] = diffPosition[Y_POSITION] << 1u;

/* Update previous touch position variables once delta position is computed */
prevTouchPosition[X_POSITION] = touchPosition[X_POSITION];
prevTouchPosition[Y_POSITION] = touchPosition[Y_POSITION];

/* Get the current button status */
currentLeftClick = CapSense_IsWidgetActive(CapSense_LEFTCLICK_WDGT_ID);
currentRightClick = CapSense_IsWidgetActive(CapSense_RIGHTCLICK_WDGT_ID);


/* Get the mouse left click and right click data */
mouseClickData = 0;

/* Send left click only on the raising edge of left-click button status */

if((prevLeftClick == 0) && (currentLeftClick != 0))
{
mouseClickData |= MOUSE_FN_LEFT_CLICK;
}
else
{
mouseClickData &= ~(MOUSE_FN_LEFT_CLICK);
}

/* Send right click only on the raising edge of right-click button status */

if((prevRightClick == 0) && (currentRightClick != 0))
{
mouseClickData |= MOUSE_FN_RIGHT_CLICK;
}
else
{
mouseClickData &= ~(MOUSE_FN_RIGHT_CLICK);
}

/* Update previous trackpad and button status */
prevfingerStatus = currentfingerStatus;
prevLeftClick = currentLeftClick;
prevRightClick = currentRightClick;
}

void updateUARTBuffer(void)
{
uint8 index;
/* To send interger data over UART char array is declared */
char string[20];

/* If either trackpad or button sensor is touched, update the I2C buffer */
if(fingerStatus || mouseClickData)
//if(fingerStatus)
{
/* Reset all the data */
for(index = 0; index < BufferSize;index++)
{
Mouse_Data[index] = 0u;
}

/* 5 bytes of data is sent to host
BYTE1 - Mouse click data
BYTE2 - Delta x data
BYTE3 - Delta y data
BYTE4 - 0 (Scroll data, not used in this code example)
BYTE5 - delta x and delta y sign information
*/
#ifndef SQUAREMOUSETEST
Mouse_Data[DATA_BYTE1_INDEX] = mouseClickData;
Mouse_Data[DATA_BYTE2_INDEX] = (diffPosition[X_POSITION]);
Mouse_Data[DATA_BYTE3_INDEX] = (diffPosition[Y_POSITION]);
Mouse_Data[DATA_BYTE4_INDEX] = 0u;
Mouse_Data[DATA_BYTE5_INDEX] = xyDirectionData;

#endif
BLE_UART_SpiUartPutArray((uint8 *)Mouse_Data,BufferSize);

#ifdef PRINTtoTERMINAL
sprintf(string,"%d \t",Mouse_Data[DATA_BYTE1_INDEX]);
BLE_UART_UartPutString(string);
sprintf(string,"%d \t",Mouse_Data[DATA_BYTE2_INDEX]);
BLE_UART_UartPutString(string);
sprintf(string,"%d \n",Mouse_Data[DATA_BYTE3_INDEX]);
BLE_UART_UartPutString(string);
//sprintf(string, "%d \t",Mouse_Data[3]);
//BLE_UART_UartPutString(string);
//sprintf(string, "%d \n",Mouse_Data[4]);
//BLE_UART_UartPutString(string);
CyDelay(10);
#endif


// BLE_UART_SpiUartClearTxBuffer();

for(index = 0; index < BufferSize;index++)
{
Mouse_Data[index] = 0u;
}

}
}

/**********Square mouse for testing***********/
#ifdef SQUAREMOUSETEST
void squaremouse()
{
switch (Delay){ // Switch statement creates a state machine for mouse movement
case 128:
Mouse_Data[0] = 0;
Mouse_Data[1] = 5; // Moves mouse to right
Mouse_Data[2] = 0; // Keeps Y static

break;
case 256:
Mouse_Data[0] = 0;
Mouse_Data[1] = 0; // Keeps X static
Mouse_Data[2] = 5; // Moves mouse down

break;
case 384:
Mouse_Data[0] = 0;
Mouse_Data[1] = -5; // Moves mouse to left
Mouse_Data[2] = 0; // Keeps Y static

break;
case 512:
Mouse_Data[0] = 0;
Mouse_Data[1] = 0; // Keeps X static
Mouse_Data[2] = -5; // Moves mouse up
Delay = 0;
break;
default:
break;
}

Delay++;
}
#endif

 

 

0 Likes
2 Replies
Ekta_N
Moderator
Moderator
Moderator
750 replies posted First like given 250 solutions authored

Hello @shilpasonone04 

I am working on project touchpad  uart based. The cursor moves smoothly but use left and right mouse button configuration, during rotating cursor continuously left and right mouse buttons window opens with it.

According to my understanding of the issue, when you are making circle (rotating cursor continuously) on the track, the left and the right mouse buttons are getting triggered which opens a window in your laptop/Desktop.
Can you please confirm if my understanding of the issue here is correct?

Can  you also attach you your project and tell us the device or evaluation board you are using? This would halp us to recreate the issue at our end.

Best Regards
Ekta

 

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I briefly read your code.

One thing came to my mind is although you have  program structure

        if(CapSense_NOT_BUSY == CapSense_IsBusy())
        {
            // Capsense Starting and Processes
            CapSense_UpdateAllBaselines();
            CapSense_ScanAllWidgets();
            CapSense_ProcessAllWidgets();
            // processing capsense data
           ...
        }

I would suggest to 

        if(CapSense_NOT_BUSY == CapSense_IsBusy())
        {
            // Capsense Starting and Processes
            CapSense_ProcessAllWidgets();
            // process capsense data
           ...
           CapSense_UpdateAllBaselines();
           CapSense_ScanAllWidgets();
        }

 

moto

0 Likes