Hackster is hosting Hackster Holidays, Ep. 4: Livestream & Giveaway Drawing. Start streaming on Wednesday!Stream Hackster Holidays, Ep. 4 on Wednesday!
Elijah Maluleke
Published © GPL3+

A Smart Home Door Locker Sensor

This project ensures that a smart door locker is only locked and unlocked by a person standing very close to the door.

IntermediateFull instructions providedOver 6 days288
A Smart Home Door Locker Sensor

Things used in this project

Hardware components

Infineon CY8CKIT-041-41XX PSoC™ 4100S CapSense Pioneer Kit
×1
MINI RELAY SPDT 5 PINS 12VDC 10A 120V CONTACT
TaydaElectronics MINI RELAY SPDT 5 PINS 12VDC 10A 120V CONTACT
×1
DC POWER JACK 2.1MM BARREL-TYPE PCB MOUNT
TaydaElectronics DC POWER JACK 2.1MM BARREL-TYPE PCB MOUNT
×1
Buzzer
Buzzer
×1
Bipolar (BJT) Single Transistor, Low Noise
Bipolar (BJT) Single Transistor, Low Noise
×2
5 mm LED: Red
5 mm LED: Red
×2
5 mm LED: Yellow
5 mm LED: Yellow
×1
5 mm LED: Green
5 mm LED: Green
×1
LED, Blue
LED, Blue
×1
Resistor 1k ohm
Resistor 1k ohm
×3
Through Hole Resistor, 4.7 kohm
Through Hole Resistor, 4.7 kohm
×1
Through Hole Resistor, 680 ohm
Through Hole Resistor, 680 ohm
×3
1N4007 – High Voltage, High Current Rated Diode
1N4007 – High Voltage, High Current Rated Diode
×5
1N4148 – General Purpose Fast Switching
1N4148 – General Purpose Fast Switching
×1
SparkFun USB Type A Female Breakout
SparkFun USB Type A Female Breakout
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1
STMicroelectronics 5V Fixed Voltage Regulator
×1
5mm Screw Clamp Terminal Block • 2 way • 17A - 250V • Straight Pins • Blue
×1
CMU SOLENOID LOCK 12VDC 0,6A
×1
Power Supply 12V 2A - Compatible with Arduino ® UNO
×1
Heatsink 17 K/W for TO-220
×1

Software apps and online services

Infineon PSoC Creator
Fritzing

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
Plier, Cutting
Plier, Cutting
Soldering Iron Tip, Knife
Soldering Iron Tip, Knife
Experimental Prototying Board
BMT 31PC PRECISION SCREW DRIVERS - Screwdrivers, Nut Drivers & Socket Drivers BMT 31PC PRECISION SCREW DRIVERS - Screwdrivers, Nut Drivers & Socket Drivers BMT 31PC PRECISION SCREW DRIVERS - Screwdrivers, Nut Drivers & Socket Drivers BMT 31PC PRECISION SC
Ergo Cushion 75x5mm Phillips Screwdriver with Chrome Vanadium Steel Blade and Black Tip Finish
Ergo Cushion 75x5mm Flat Screwdriver with Chrome Vanadium Steel Blade and Black Tip Finish
150mm Lightweight Retractable Knife
Hex Thread M3 Spacer • Female to Female • 10mm
Cheese Head Machine Screws Stainless/Steel

Story

Read more

Custom parts and enclosures

A Smart Home Door Locker Sensor Fritzing files

Schematics

A Smart Home Door Locker Sensor

A Smart Home Door Locker Sensor Schmatic

Code

A Smart Home Door Locker Sensor

C/C++
/******************************************************************************
* File Name: main.c
*
* Version: 1.00
*
* Description:  This code example demonstrates how to use CapSense trackpad 
*               to input RGB color code for color mixing with PSoC 4 S-Series device.
*
* Related Document: CE214025 Trackpad with Color Gamut.pdf
*
* Hardware Dependency: See code example document CE214025 Trackpad with Color Gamut.pdf
*
******************************************************************************
* Copyright (2016), Cypress Semiconductor Corporation.
******************************************************************************
* This software, including source code, documentation and related materials
* ("Software") is owned by Cypress Semiconductor Corporation (Cypress) and is
* protected by and subject to worldwide patent protection (United States and 
* foreign), United States copyright laws and international treaty provisions. 
* Cypress hereby grants to licensee a personal, non-exclusive, non-transferable
* license to copy, use, modify, create derivative works of, and compile the 
* Cypress source code and derivative works for the sole purpose of creating 
* custom software in support of licensee product, such licensee product to be
* used only in conjunction with Cypress's integrated circuit as specified in the
* applicable agreement. Any reproduction, modification, translation, compilation,
* or representation of this Software except as specified above is prohibited 
* without the express written permission of Cypress.
* 
* Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND, 
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED 
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* Cypress reserves the right to make changes to the Software without notice. 
* Cypress does not assume any liability arising out of the application or use
* of Software or any product or circuit described in the Software. Cypress does
* not authorize its products for use as critical components in any products 
* where a malfunction or failure may reasonably be expected to result in 
* significant injury or death ("ACTIVE Risk Product"). By including Cypress's 
* product in a ACTIVE Risk Product, the manufacturer of such system or application
* assumes all risk of such use and in doing so indemnifies Cypress against all
* liability. Use of this Software may be limited by and subject to the applicable
* Cypress software license agreement.
*****************************************************************************/
/*******************************************************************************
*   Included Headers
*******************************************************************************/
#include <project.h>

/* Include boolean function definition */
#include <stdbool.h>

/* Include color mixing API declarations */
#include "colormixing.h"

/* Include sprintf API definitions */
#include <stdio.h>

/*******************************************************************************
*   Macros and #define Constants
*******************************************************************************/
    
/**/
#define BUZZER_ON   (0x01u)
#define BUZZER_OFF   (0x00u)

#define LED_ON   (0x01u)
#define LED_OFF   (0x00u)

#define LOCKER_UNLOCKED      (0x01u)
#define LOCKER_LOCKED   (0x00u)

#define CUSTOM_RED_LED     (0x00u)
#define CUSTOM_YELLOW_LED   (0x01u)
#define CUSTOM_GREEN_LED    (0x02u) 

#define STARTED     (0x01u)
#define STOPED      (0x00u)

/* Intensity control button status */    
#define BUTTON_ON                   (0x01u)
#define BUTTON_OFF                  (0x00u)

/* Multiplier value for color mixing to avoid floating point math */    
#define POS_MULT_100                (100u)    

/* Number of active sensors when palm is placed on trackpad */
#define ACTIVE_SENSORS_PALM          (4u)

/* Minimum and maximum intensity values. This value is divided by 10 and is multiplied
*  by MAX_COMPAREVALUE     
*/    
#define MAX_INTENSITY_LEVEL         (16u)

/* Default intensity value */    
#define DEFAULT_LED_INTENSITY       (MAX_COMPAREVALUE/2)

/*LED wil be driven for the specified number of loops after no touch*/
#define MAX_LOOPS_TO_DRIVE_LED      (75u)

/*Index for brightness control array*/
#define MAX_INTENSITY_INDEX         (3u)
#define MIN_INTENSITY_INDEX         (0u)

/*LED brightness macros*/
#define LED_BRIGHTNESS_12_5         (2u)
#define LED_BRIGHTNESS_25_0         (4u)
#define LED_BRIGHTNESS_37_5         (6u)
#define LED_BRIGHTNESS_100_0        (16u)

/*Offset correction macros*/
#define MIN_X_COORDINATE            (15u)
#define MAX_X_COORDINATE            (75u)
#define MIN_Y_COORDINATE            (8u)
#define MAX_Y_COORDINATE            (77u)
#define Y_COORDINATE_OFFSET         (7u)
#define Y_COORDINATE_NEED_OFFSET    (29u)
#define MAX_X_COORDINATE_FOR_COLOR_MIXING   (64u)

/*******************************************************************************
*   Module Variable and Constant Declarations with Applicable Initializations
*******************************************************************************/
    
/* Finite state machine states for device operation */
typedef enum
{
    SENSOR_SCAN = 0x01u,    /* Sensor is scanned in this state */
    WAIT_FOR_SCAN_COMPLETE = 0x02u, /* CPU is put to sleep in this state */
    PROCESS_DATA = 0x03u,   /* Sensor data is processed */
} DEVICE_STATE;

const uint8 brightnessLevels[] = {LED_BRIGHTNESS_12_5,LED_BRIGHTNESS_25_0
                                    ,LED_BRIGHTNESS_37_5 ,LED_BRIGHTNESS_100_0};

uint8 brightnessIndex = MAX_INTENSITY_INDEX;

/**/
typedef enum
{
    INIT_STATE = 0x00u,
    UNLOCKED_STATE = 0x01u,
    LOCKED_STATE = 0x02u,
    UNLOCKING_STATE = 0x03u,
    LOCKING_STATE = 0x04u
} LOCK_STATE;

LOCK_STATE currentLockState = INIT_STATE;

/*******************************************************************************
*  Function Declarations
*******************************************************************************/

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

/* Function prototype to scan CapSense sensors */
void capSenseProcess(void); 

/* Function prototype to initialize TCPWM components */
void prismInit(void);

/* Function that checks if trackpad or button sensors are ON */
bool anyWidgetActive(void);

/* Function that performs color mixing */
void colorMixingProcess(void);

/**/
void Test_UART();

/**/
void Check_TouchedPad(void);

/**/
uint8 xyCordinates(uint16 xToDigit, uint16 yToDigit);

/**/
void buzzer(uint8 numberOfBeeps, uint32 beepDelay);

/**/
void BoardStartUp(void);

/**/
void SmartLocker(uint8 LockState);

/**/
void LedController(uint8 LedState, uint8 LedRGB);

/**/
void ProcessSmartLockData(uint8 xCorValue, uint8 yCorValue);

/*******************************************************************************
*   Module Variable and Constant Declarations with Applicable Initializations
*******************************************************************************/

/* Contains current x, y coordinates of trackpad */
uint16 trackpadXPos = CapSense_SLIDER_POS_NONE;
uint16 trackpadYPos = CapSense_SLIDER_POS_NONE;    

/* Variable to store color mixing error status */
uint8 colorMixingError;

/* Variable to store interrupt state */
uint32 interruptState = 0u;

/* Variable to store the current trackpad XY coordinates. 
*  The XY coordinate is multiplied with 100 to avoid floating point math
*/
XY_COORDINATE currentXYCoordinate;

/* Variable to store last valid touch coordinates */
XY_COORDINATE lastValidXYCoordinate;

/* Variable to store RGB led dimming values */
uint16 rgbLEDDimmingValue[NUM_LEDS];

uint16 timeoutCounter;

/* Variable to store RGB LED coordinates on the color gamut 
*  The coordinates are multiplied by 10000 to avoid floating point math
*/
LED_COORDINATE rgbLEDXYPosition[NUM_LEDS];

/* Variable to store On/Off condition of trackpad */
uint8 trackpadStatus = 0;

/* Variable to control the maximum intensity of RGB LED 
*  Variable takes the following values 2, 4, 8 and 16
*  The resulting PWM compare value is multiplied with this value
*  and normalized to achieve duty cycle of 100%, 50%, 25% and 12.5%
*/
uint8 intensityCtrl = MAX_INTENSITY_LEVEL;

/* Variable to store the status of intensity control buttons */  
uint8 button0CurrState = BUTTON_OFF;
uint8 button0PrevState = BUTTON_OFF;
uint8 button1CurrState = BUTTON_OFF;
uint8 button1PrevState = BUTTON_OFF;

/**/
uint16 remainderVar = 0;
uint16 tempVar1 = 0;
uint16 tempVar2 = 0;
uint8 pinCounter = 1;
uint8 LockingUnlockingSignal = INIT_STATE;

/**/
uint16 xValue = 0;
uint16 yValue = 0;
uint8 PIN[5] =  {0x36, 0x32, 0x30, 0x38, 0x31};
uint8 keypad[10] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39};

/******************************************************************************
* Function Name: main
*******************************************************************************
*
* Summary: This function implements the state machine for device operation.
*
* Parameters:
*  None.
*
* Return:
*  None.
*
* Theory: 
*   main() performs following functions:
*  1: Initialize the CapSense, EZI2C and PWM Components
*  2: Scans trackpad, buttons and performs color mixing and brightness control using
*     RGB LED
* 
* Side Effects: None
*
* Note:None
*
*******************************************************************************/

int main()
{    
    /* Start the firmware state machine with sensor scan */
    DEVICE_STATE currentState = SENSOR_SCAN;
    
    /* Variable to hold active sensors count */
    uint16 sensorCount = 0u;
    
    /* Variable to check active sensor bits */
    uint16 mask = 0u;
    
    /* Temporary variable */
    uint16 tempVar = 0u;    
   
    /* Enable interrupts. This is required for CapSense and I2C operation */ 
    CyGlobalIntEnable; 
    
    /**/
    UART_Start();
    UART_UartPutString("\r\n***********************************************************************************\r\n");
    UART_UartPutString("This is SCB_UartComm datasheet example project\r\n");
    UART_UartPutString("If you are able to read this text the terminal connection is configured correctly.\r\n");
    UART_UartPutString("Start transmitting the characters to see an echo in the terminal.\r\n");
    UART_UartPutString("\r\n");
    
    /* Initialize I2C component for CapSense tuner */
    EZI2C_Start();
    
    /* Set up communication data buffer to CapSense data structure to 
    * expose to I2C master at primary slave address request
    */
    EZI2C_EzI2CSetBuffer1(sizeof(CapSense_dsRam), sizeof(CapSense_dsRam),\
                         (uint8 *)&CapSense_dsRam);
    
    /* Start the CapSense component and autocalibrate IDAC values */
    capSenseInit();  
    
    /* Initialize PWM components */
    prismInit();
    
    /* Load XY coordinates of RGB LED on color gamut into local variables */
    initializeLedCoordinates(rgbLEDXYPosition);
    
    /**/
    //printf("Loop forever\r\n");
    BoardStartUp();
    currentLockState  = LOCKED_STATE;
    LedController(LED_ON, CUSTOM_RED_LED);
    LedController(LED_OFF, CUSTOM_YELLOW_LED);
    LedController(LED_OFF, CUSTOM_GREEN_LED);
    
    for(;;)
    {        
        switch(currentState)
        {
            case SENSOR_SCAN:
			{
                /* Initiate new scan only if the CapSense hardware is idle */
                if(CapSense_NOT_BUSY == CapSense_IsBusy())
                {
                    /* Update CapSense parameters set via CapSense tuner */
                    CapSense_RunTuner();      
                    
                    /*Scan trackpad and buttons*/
                    CapSense_ScanAllWidgets();
                    
                    /* Set the state machine to wait state until the scan is complete */
                    currentState = WAIT_FOR_SCAN_COMPLETE;   
                }
            	break;
			}
            
			case WAIT_FOR_SCAN_COMPLETE:
			{
                /* Device is in CPU Sleep until CapSense scanning is complete or
                *  device is woken-up by either CapSense interrupt or I2C interrupt 
                */
                /* Disable interrupts, so that ISR is not serviced while
                *  checking for CapSense scan status. Otherwise, interrupt might
                *  get serviced after checking for IsBusy condition and device
                *  might not wakeup since CapSense interrupt is already serviced
                */
                interruptState = CyEnterCriticalSection();
                            
                /* Check if CapSense scanning is complete */
                if(CapSense_NOT_BUSY != CapSense_IsBusy())
                {
                    /* If CapSense scannning is in progress, put CPU to sleep 
                    *  Device wakesup because of CapSense scan complete interrupt
                    */
                    CySysPmSleep();
                }
                /* If CapSense scanning is complete, process the CapSense data */
                else
                {
                    /* If current widget is trackpad or mode is slow scan mode, process the sensor data */
                    currentState = PROCESS_DATA;
                }
                /* Enable interrupts for servicing ISR */
                CyExitCriticalSection(interruptState);
            	break;
            }           
            
			case PROCESS_DATA:
            {            
                /* Set next state to Sensor Scan */
                currentState = SENSOR_SCAN;
                CapSense_ProcessAllWidgets();
                /* The below code resets trackpad baseline if a palm is detected on the trackpad */
                if(CapSense_IsWidgetActive(CapSense_TRACKPAD_WDGT_ID))
                {
                    /* Initialize mask variable */
                    mask = 1u;
                    
                    /* Set active sensor count to zero */
                    sensorCount = 0u;
                    
                    /* Loop through all the row and column sensors */
                    for(tempVar = CapSense_TRACKPAD_COL0_ID; tempVar <= CapSense_TRACKPAD_ROW6_ID; tempVar++)
                    {
                        /* Check each bit for active sensor condition */
                        if(CapSense_SNS_STATUS0_VALUE & mask)
                        {
                            /* Increment sensor count for each active sensor */
                           sensorCount++;
                        }
                        /* If all the column sensors are searched and active sensorCount is not greater than threshold
                        *  reset the sensorCount variable to detect active row sensors
                        */
                        if((tempVar == CapSense_TRACKPAD_COL6_ID) && (sensorCount <= ACTIVE_SENSORS_PALM))
                        {
                            sensorCount = 0u;
                        }
                        
                        /* If active sensor count in either a row or column has exceed the threshold
                        *  reset all the trackpad sensor baselines
                        */
                        if(sensorCount > ACTIVE_SENSORS_PALM)
                        {
                            CapSense_InitializeWidgetBaseline(CapSense_TRACKPAD_WDGT_ID);
                            break;
                        }
                        /* Update the mask variable until all the bits are scanned for active status */
                        mask = mask << 1u;
                    }                           
                }
                /* Check if IntensityUp button is active */
                if(CapSense_IsWidgetActive(CapSense_INTENSITYUP_WDGT_ID))
                    button0CurrState = BUTTON_ON;
                else 
                    button0CurrState = BUTTON_OFF;

                /* Check if IntensityDown button is active */
                if(CapSense_IsWidgetActive(CapSense_INTENSITYDOWN_WDGT_ID))
                    button1CurrState = BUTTON_ON;
                else
                    button1CurrState = BUTTON_OFF;

                /* Check for rising edge of IntensityUp button status  and calculate the LED
                    brightness control array index*/
                if((button0CurrState == BUTTON_ON) && (button0PrevState == BUTTON_OFF))
                {                    
                    if(brightnessIndex >= MAX_INTENSITY_INDEX)
                    {
                        brightnessIndex = MAX_INTENSITY_INDEX; 
                    }
                    else
                    {
                        brightnessIndex++;
                    }
                }
                /* Check for rising edge of IntensityDown button status  and calculate the LED
                    brightness control array index*/
                else if((button1CurrState == BUTTON_ON) && (button1PrevState == BUTTON_OFF))
                {
                    if(brightnessIndex <= MIN_INTENSITY_INDEX)
                    {
                        brightnessIndex = MIN_INTENSITY_INDEX;
                    }
                    else
                    {
                        brightnessIndex--;
                    }                    
                }
                
                intensityCtrl = brightnessLevels[brightnessIndex];

                /* Initialize previous button state to current button state */
                button0PrevState = button0CurrState;
                button1PrevState = button1CurrState;                
               
                /* If trackpad or button sensor is not active, increment the LED timeout counter */
               if(!anyWidgetActive())
                 {
                    timeoutCounter++;

                    /* Check if sensor is inactive for a duration greater than MAX_LOOPS_TO_DRIVE_LED */
                    if(timeoutCounter >= MAX_LOOPS_TO_DRIVE_LED)
                    {                        
                        /* Set LED pin drive mode to high-z to stop driving LEDs */
                        Red_LED_SetDriveMode(Red_LED_DM_ALG_HIZ);
                        Green_LED_SetDriveMode(Green_LED_DM_ALG_HIZ);
                        Blue_LED_SetDriveMode(Blue_LED_DM_ALG_HIZ);                        
                    }
                }  
                /* If either trackpad or button sensors are active, perform color mixing */
                else
                {                    
                    /* Because sensor is active, reset the counter */
                    timeoutCounter = 0;

                    /* If widget is active, perform color mixing */
                    colorMixingProcess();
                }                               
            	break;
            }
               
            default:
			{
            /*******************************************************************
             * Unknown state. Unexpected situation.
             ******************************************************************/
            	CYASSERT(0);
            	break;
			}
        }
    }
}

/******************************************************************************
* 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)
{
    /* Variable to store the XY coordinates */
    uint32 tempCoordinates;
    
    /* Initialize CapSense block */
    CapSense_Start();
    
    CapSense_InitializeWidgetBaseline(CapSense_INTENSITYUP_WDGT_ID);
    CapSense_InitializeWidgetBaseline(CapSense_INTENSITYDOWN_WDGT_ID);
    
    /* Scan the trackpad widget and initialize the previous touch coordinate values*/
    trackpadStatus =  CapSense_IsWidgetActive(CapSense_TRACKPAD_WDGT_ID);
    
    /* Get XY Coordinates */
    tempCoordinates = CapSense_GetXYCoordinates(CapSense_TRACKPAD_WDGT_ID);
    
    /* Load XY position to variable. Note: the XY position is interchanged as the 
    *  columns and rows in the PCB layout is inverse of X, Y coordinate of color gamut 
    */
    trackpadYPos = LO16(tempCoordinates);
    trackpadXPos = HI16 (tempCoordinates);
    
    /* Initialize last valid coordinates so that RGB LED glows
    *  when button is touched before trackpad after device power-up or reset.
    */
    lastValidXYCoordinate.currentX = RED_BASE_COLOR_X;
    lastValidXYCoordinate.currentY = RED_BASE_COLOR_Y;
    
    /* Set default led intensity value */
    currentXYCoordinate.ledIntensity = DEFAULT_LED_INTENSITY;
}

/******************************************************************************
* Function Name: prismInit
*******************************************************************************
*
* Summary: This API initializes PWM components
*
* Parameters:
*  None.
*
* Return:
*  None.
*
* Theory:
*  prismInit() performs following functions:
*  1: Starts the TCPWM block
*  2: Initializes the TCPWM compare value to minimum value
* 
* Side Effects: None
*
* Note: None
*
*******************************************************************************/
void prismInit(void)
{
    /* Start TCPWM Blocks */
    PrISM_Red_Start();
    PrISM_Green_Start();
    PrISM_Blue_Start();
    
    /* Set TCPWM compare value to zero initially */
    PrISM_Red_WriteCompare(MIN_COMPAREVALUE);
    PrISM_Green_WriteCompare(MIN_COMPAREVALUE);
    PrISM_Blue_WriteCompare(MIN_COMPAREVALUE);

}
/*******************************************************************************
* Function Name: anyWidgetActive
********************************************************************************
*
* Summary:
*  This API checks if any widget is active
*
* Parameters:
*  None.
*
* Theory: This API checks if any of the IntensityUp, IntensityDown or trackpad sensors
*         are active and returns 
*
* Side Effects: None
*
* Note: None
*
*******************************************************************************/

bool anyWidgetActive(void)
{
    /* Check if either trackpad or any of the two button sensors are active */
    if(CapSense_IsWidgetActive(CapSense_TRACKPAD_WDGT_ID) || \
       CapSense_IsWidgetActive(CapSense_INTENSITYUP_WDGT_ID) || \
       CapSense_IsWidgetActive(CapSense_INTENSITYDOWN_WDGT_ID))
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

/*******************************************************************************
* Function Name: colorMixingProcess
********************************************************************************
*
* Summary:
*  This API checks if any widget is active and computes the TCPWM compare value
*
* Parameters:
*  None.
*
* Theory: This API checks if any of the IntensityUp, IntensityDown or trackpad sensors
*         are active. Based on the status, the color mixing algorithm is executed to
*         compute the compare
*
* Side Effects: None
*
* Note: None
*
*******************************************************************************/
void colorMixingProcess(void)
{
    /* Temporary variable */
    uint32 tempVar;
    
    /* Variable to store XY coordinates of trackpad */
    uint32 tempCoordinates;

    /* Obtain the maximum compare value depending on the intensity level set by user */
    tempVar = (uint32) (intensityCtrl * MAX_COMPAREVALUE);
    tempVar /= MAX_INTENSITY_LEVEL;
    currentXYCoordinate.ledIntensity = (uint16)tempVar;
    lastValidXYCoordinate.ledIntensity = currentXYCoordinate.ledIntensity;    
    
    /* Check if trackpad is active */
    trackpadStatus =  CapSense_IsWidgetActive(CapSense_TRACKPAD_WDGT_ID);
    
    /* Get trackpad XY coordinates */
    tempCoordinates = CapSense_GetXYCoordinates(CapSense_TRACKPAD_WDGT_ID);
    
    /* XY position values and store in local variable 
    *  Note: The XY position is interchanged because the rows and columns are 
    *  interchanged in the kit
    */
    trackpadYPos = LO16(tempCoordinates);
    trackpadXPos = HI16(tempCoordinates);
    
    /* If trackpad is active, load the XY position for computing dimming values */
    if(trackpadStatus)
    {
        /* Normalization is done because row0 to row6 and column0 to column 6 
        *  are inverted in the layout       
        */
        trackpadXPos = CapSense_TRACKPAD_Y_RESOLUTION - trackpadXPos;
        trackpadYPos = CapSense_TRACKPAD_X_RESOLUTION - trackpadYPos;
        
        /*Offset correction to get smooth response near color gamut edges*/
        if(trackpadXPos <= MIN_X_COORDINATE)
        {
            trackpadXPos = MIN_X_COORDINATE;
        }        
        else if (trackpadXPos >= MAX_X_COORDINATE_FOR_COLOR_MIXING && trackpadXPos <= MAX_X_COORDINATE)
        {
            trackpadXPos = MAX_X_COORDINATE_FOR_COLOR_MIXING;
        }        
        
        if(trackpadYPos <= MIN_Y_COORDINATE)
        {
            trackpadYPos = MIN_Y_COORDINATE;
        }
        else if (trackpadYPos <= Y_COORDINATE_NEED_OFFSET)
        {
            trackpadYPos = trackpadYPos + Y_COORDINATE_OFFSET;
        }
        else if(trackpadYPos >= MAX_Y_COORDINATE)
        {
            trackpadYPos = MAX_Y_COORDINATE;
        }
        
    }
    else
    {
        trackpadXPos = 0u;
        trackpadYPos = 0u;
    }
    
    /* Multiply the coordinate value by 100 to avoid floating point math */
    currentXYCoordinate.currentX = (int16)(trackpadXPos * POS_MULT_100);
    currentXYCoordinate.currentY = (int16)(trackpadYPos * POS_MULT_100);
    
    /* If finger is on trackpad, use current touch coordinates to compute dimming values */
    if(trackpadStatus)
    {
        /* Compute RGB LED dimming values for a given XY coordinate */
        colorMixingError = rgbColorMix(currentXYCoordinate, rgbLEDXYPosition, rgbLEDDimmingValue);
        
        /* If color mixing was successful, save the current coordinates */
        if(colorMixingError != INVALID_COLOR)
        {
            lastValidXYCoordinate.currentX = currentXYCoordinate.currentX;
            lastValidXYCoordinate.currentY = currentXYCoordinate.currentY;
        }
    }
    /* If IntensityUp or IntensityDown button is pressed, compute dimming value using previous saved touch coordinates */
    else
    {
        colorMixingError = rgbColorMix(lastValidXYCoordinate, rgbLEDXYPosition, rgbLEDDimmingValue);
    }

    /* Update the LED dimming value only when color mixing process is successful */
    if(colorMixingError != INVALID_COLOR)
    {
        /* Set LED pin drive mode to strong to save power */
        Red_LED_SetDriveMode(Red_LED_DM_STRONG);
        Green_LED_SetDriveMode(Green_LED_DM_STRONG);
        Blue_LED_SetDriveMode(Blue_LED_DM_STRONG);
                        
        /* Load the computed dimming value to the PrISM component */
        PrISM_Red_WriteCompare(rgbLEDDimmingValue[LED_RED]);
        PrISM_Green_WriteCompare(rgbLEDDimmingValue[LED_GREEN]);
        PrISM_Blue_WriteCompare(rgbLEDDimmingValue[LED_BLUE]); 
        
        /**/
        switch(currentLockState)
        {
            case LOCKED_STATE:
            {
                if(pinCounter <= 5)
                {
                    /**/
                    UART_UartPutString("pinCounter: ");UART_UartPutChar(pinCounter + 0x30);
                    UART_UartPutString("\r\n\r\n");
                    LedController(LED_ON, CUSTOM_RED_LED);
                    LedController(LED_ON, CUSTOM_YELLOW_LED);
                    LedController(LED_OFF, CUSTOM_GREEN_LED);
                    ProcessSmartLockData(currentXYCoordinate.currentX, currentXYCoordinate.currentY);
                    buzzer(1, 50);
                    pinCounter++;
                }
                else
                {
                    /**/
                    SmartLocker(LOCKER_UNLOCKED);
                    pinCounter = 1;
                    currentLockState = UNLOCKED_STATE;
                    LedController(LED_OFF, CUSTOM_RED_LED);
                    LedController(LED_OFF, CUSTOM_YELLOW_LED);
                    LedController(LED_ON, CUSTOM_GREEN_LED);
                    ProcessSmartLockData(currentXYCoordinate.currentX, currentXYCoordinate.currentY);
                }
                break;
            }
            case UNLOCKED_STATE:
            {
                if(pinCounter <= 5)
                {
                    /**/
                    UART_UartPutString("pinCounter: ");UART_UartPutChar(pinCounter + 0x30);
                    UART_UartPutString("\r\n\r\n");
                    LedController(LED_OFF, CUSTOM_RED_LED);
                    LedController(LED_ON, CUSTOM_YELLOW_LED);
                    LedController(LED_ON, CUSTOM_GREEN_LED);
                    ProcessSmartLockData(currentXYCoordinate.currentX, currentXYCoordinate.currentY);
                    buzzer(1, 50);
                    pinCounter++;
                }
                else
                {
                    /**/
                    SmartLocker(LOCKER_LOCKED);
                    pinCounter = 1;
                    currentLockState = LOCKED_STATE;
                    LedController(LED_ON, CUSTOM_RED_LED);
                    LedController(LED_OFF, CUSTOM_YELLOW_LED);
                    LedController(LED_OFF, CUSTOM_GREEN_LED);
                    ProcessSmartLockData(currentXYCoordinate.currentX, currentXYCoordinate.currentY);
                }
                break;
            }
            default:
            {
                
                break;
            }
        }
    }
}

/*******************************************************************************
* Function Name: ProcessSmartLockData
********************************************************************************
*
* Summary:
*  This API Process Smart Lock Data
*
* Parameters:
*  None.
*
* Theory: 
*         
*        
*
* Side Effects: None
*
* Note: None
*
*******************************************************************************/
void ProcessSmartLockData(uint8 xCorValue, uint8 yCorValue)
{
    
    UART_UartPutString("\r\n\r\n");
    UART_UartPutString("currentXCoordinate: ");UART_UartPutChar(xCorValue);
    UART_UartPutString(" ");
    UART_UartPutString("currentXYCoordinate: ");UART_UartPutChar(yCorValue);
    UART_UartPutString("\r\n\r\n");
    //
    UART_UartPutString("xCorValue: ");
    tempVar1 = xCorValue / 100;
    tempVar2 = xCorValue % 100;
    UART_UartPutChar(tempVar1 + 0x30);
    tempVar1 = tempVar2 / 10;
    UART_UartPutChar(tempVar1 + 0x30);
    tempVar1 = tempVar2 % 10;
    UART_UartPutChar(tempVar1 + 0x30);
    UART_UartPutString(" ");    
    UART_UartPutString("yCorValue: ");
    tempVar1 = yCorValue / 100;
    tempVar2 = yCorValue % 100;
    UART_UartPutChar(tempVar1 + 0x30);
    tempVar1 = tempVar2 / 10;
    UART_UartPutChar(tempVar1 + 0x30);
    tempVar1 = tempVar2 % 10;
    UART_UartPutChar(tempVar1 + 0x30);
    UART_UartPutString("\r\n\r\n");
    //
    uint8 keypadValue = xyCordinates(xCorValue, yCorValue);
    if((keypadValue >= 0x30)&&(keypadValue <= 0x39))
    {
        UART_UartPutString("Keypad Digit: ");UART_UartPutChar(keypadValue);
    }
    else
    {
        UART_UartPutString("Out of Range! ");UART_UartPutChar(keypadValue);
    }
    UART_UartPutString("\r\n\r\n");
    
    UART_UartPutString("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
}

/*******************************************************************************
* Function Name: BoardStartUp
********************************************************************************
*
* Summary: This API BoardStartUp
*  
*
* Parameters:
*  None.
*
* Theory:
*        
*         
*
* Side Effects: None
*
* Note: None
*
*******************************************************************************/
void BoardStartUp(void)
{
    Pin_RedLed_Write(LED_ON);
    Pin_YellowLed_Write(LED_ON);
    Pin_GreenLed_Write(LED_ON);
    Pin_Buzzer_Write(BUZZER_ON);
    CyDelay(500);
    Pin_RedLed_Write(LED_OFF);
    Pin_YellowLed_Write(LED_OFF);
    Pin_GreenLed_Write(LED_OFF);
    Pin_Buzzer_Write(BUZZER_OFF);
    CyDelay(500);
}

/*******************************************************************************
* Function Name: xyCordinates
********************************************************************************
*
* Summary:
*  
*
* Parameters:
*  None.
*
* Theory: 
*         
*         
*
* Side Effects: None
*
* Note: None
*
*******************************************************************************/
uint8 xyCordinates(uint16 xToDigit, uint16 yToDigit)
{
    uint8 result = 0;
    
    if((xToDigit == 220)&&(yToDigit == 112))  // 0
    { result = keypad[0];}
    else if((xToDigit >= 600&&xToDigit <= 600)&&(yToDigit >= 290&&yToDigit <= 300)) // 1
    { result = keypad[1]; }
    else if((xToDigit >= 390&&xToDigit <= 390)&&(yToDigit >= 440&&yToDigit <= 450)) // 2
    { result = keypad[2]; }
    else if((xToDigit >= 320&&xToDigit <= 320)&&(yToDigit >= 590&&yToDigit <= 600)) // 3
    { result = keypad[3]; }
    else if((xToDigit >= 200&&xToDigit <= 200)&&(yToDigit >= 700&&yToDigit <= 710)) // 4
    { result = keypad[4]; }
    else if((xToDigit >= 140&&xToDigit <= 140)&&(yToDigit >= 160&&yToDigit <= 170)) // 5
    { result = keypad[5]; }
    else if((xToDigit >= 140&&xToDigit <= 140)&&(yToDigit >= 350&&yToDigit <= 360)) // 6
    { result = keypad[6]; }
    else if((xToDigit >= 140&&xToDigit <= 140)&&(yToDigit >= 540&&yToDigit <= 550)) // 7
    { result = keypad[7]; }
    else if((xToDigit >= 140&&xToDigit <= 140)&&(yToDigit >= 740&&yToDigit <= 750)) // 8
    { result = keypad[8]; }
    else if((xToDigit >= 380&&xToDigit <= 380)&&(yToDigit >= 200&&yToDigit <= 230)) // 9
    { result = keypad[9]; }
    else 
    { result = 0xFF;}
    return result;
}

/*******************************************************************************
* Function Name: buzzer
********************************************************************************
*
* Summary:
*
*
* Parameters:
*  None.
*
* Theory: 
*       
*         
*
* Side Effects: None
*
* Note: None
*
*******************************************************************************/
void buzzer(uint8 numberOfBeeps, uint32 beepDelay)
{
    uint8 i = 0;
    for(i = 0; i < numberOfBeeps; i++)
    {
        Pin_Buzzer_Write(BUZZER_ON);
        CyDelay(beepDelay);
        Pin_Buzzer_Write(BUZZER_OFF);
        CyDelay(beepDelay);
    }
}

/*******************************************************************************
* Function Name: SmartLocker
********************************************************************************
*
* Summary:
*  
*
* Parameters:
*  None.
*
* Theory:
*        
*         
...

This file has been truncated, please download it to see its full contents.

Credits

Elijah Maluleke

Elijah Maluleke

6 projects • 32 followers

Comments