Example #1
0
/*******************************************************************************
* Function Name: HandleBleProcessing
********************************************************************************
*
* Summary:
*   Handles the BLE state machine for intiating different procedures
*   during different states of BLESS.
*
* Parameters:
*   None.
*
* Return:
*   None.
*
*******************************************************************************/
void HandleBleProcessing(void)
{    
    CYBLE_API_RESULT_T      cyble_api_result;
    
    switch (cyBle_state)
    {
        case CYBLE_STATE_SCANNING:
            if(peerDeviceFound)
            {
                CyBle_GapcStopScan();
            }
            break;
    
        case CYBLE_STATE_CONNECTED:
            
            /* if Client does not has all the information about attribute handles 
             * call procedure for getting it */
            if((INFO_EXCHANGE_COMPLETE != infoExchangeState))
            {
                attrHandleInit();
            }
            
            /* enable notifications if not enabled already */
            else if(false == notificationEnabled)
            {
                enableNotifications();
            }
            
            /* if client has all required info and stack is free, handle UART traffic */
            else if(CyBle_GattGetBusStatus() != CYBLE_STACK_STATE_BUSY)
            {
                HandleUartTxTraffic();
            }
            
            break;
                
        case CYBLE_STATE_DISCONNECTED:
        {
            if(peerDeviceFound)
            {
                cyble_api_result = CyBle_GapcConnectDevice(&peerAddr);
                
			    if(CYBLE_ERROR_OK == cyble_api_result)
			    {
				    peerDeviceFound = false;
			    }
            }
            else
            {
                CyBle_GapcStartScan(CYBLE_SCANNING_FAST);  
            }
            break;
        }
        
        default:
            break;       
    }
}
Example #2
0
File: MIDI.c Project: bhwj/BLE
/*******************************************************************************
* Function Name: Midi_TransferMessages
********************************************************************************
*
* Summary:
*  This function fetches the MIDI messages from the MIDI buffer and triggers
*  a BLE notification.
*
* Parameters:
*  void
*
* Return:
*  None
*
*******************************************************************************/
static void Midi_TransferMessages(void)
{
    uint8 index;
    
    /* Parse the buffers and extract messages */
    for(index = 0; index < MAX_NUMBER_OF_MIDI_PACKETS; index++)
    {
        /* Valid packet is waiting to be scheduled for notification */
        if(midiBuffer.midiPacket[index].packetStatus == MIDI_PACKET_VALID && midiBuffer.midiPacket[index].dataIndex > 1)
        {
        	CYBLE_GATTS_HANDLE_VALUE_NTF_T		midiNotification;	
        	
        	/* If stack is not busy, then send the notification */
        	if( CyBle_GattGetBusStatus() == CYBLE_STACK_STATE_FREE &&
                ((midiBuffer.midiPacket[index].dataIndex >= (CYBLE_GATT_MTU - MAX_MIDI_PACKET_LENGTH) ) ||
                CyBle_GetBleSsState() != CYBLE_BLESS_STATE_EVENT_CLOSE) )
        	{
                CYBLE_API_RESULT_T result;
                
        		/* Update notification handle with CapSense slider data*/
        		midiNotification.attrHandle = CYBLE_MIDI_SERVICE_MIDI_IO_CHARATERISTIC_CHAR_HANDLE;				
        		midiNotification.value.val = &(midiBuffer.midiPacket[index].midiNotificationPacket[0]);
        		midiNotification.value.len = midiBuffer.midiPacket[index].dataIndex;
        		
        		/* Send the updated handle as part of attribute for notifications */
        		result = CyBle_GattsNotification(cyBle_connHandle,&midiNotification);
                
                if(result == CYBLE_ERROR_OK)
                {
                    /* Notification is successfully queued */
                    midiBuffer.midiPacket[index].packetStatus = MIDI_PACKET_INVALID;
                    midiBuffer.midiPacket[index].dataIndex = 0;
                }
                else
                {
                    break;    
                }
        	}
            else
            {
                break;    
            }
        }
    }
}
Example #3
0
void HandleI2CTraffic(void)
{
	/* Wait for I2C master to complete a write */
	if(0u != (I2C_I2CSlaveStatus() & I2C_I2C_SSTAT_WR_CMPLT))
	{
		/* Read the number of bytes transferred */
		byteCnt = I2C_I2CSlaveGetWriteBufSize();
		
		/* Clear the write status bits*/
		I2C_I2CSlaveClearWriteStatus();

		if(CyBle_GattGetBusStatus() != CYBLE_STACK_STATE_BUSY)
		{
			HandleI2CWrite();
			
			/* Clear the write buffer pointer so that the next write operation will
			start from index 0 */
			I2C_I2CSlaveClearWriteBuf();
		}
	}
	/* If the master has read the data , reset the read buffer pointer to 0
	and clear the read status */
	if(0u != (I2C_I2CSlaveStatus() & I2C_I2C_SSTAT_RD_CMPLT))
	{
		/* Clear the read buffer pointer so that the next read operations starts
		from index 0 */
		I2C_I2CSlaveClearReadBuf();
		
#ifdef	RESET_I2C_READ_DATA
	    uint8 i;
	       
		for(i=0;i<I2C_READ_BUFFER_SIZE;i++)
			rdBuf[i] = 0;
#endif  /* RESET_I2C_READ_DATA */	
		
		/* Clear the read status bits */
		I2C_I2CSlaveClearReadStatus();
	}
}
Example #4
0
/*******************************************************************************
* Function Name: SimulateKeyboard()
********************************************************************************
*
* Summary:
*   The custom function to simulate Left and Right arrow key pressing
*
* @return
*    None
*
* @param[in] 
*    none
*
* @par Theory of Operation
*   keyboard_data[3u] contains the keyboard data. Following a valid key,
*   zero is sent to indicate key lift.
*
* @par Notes
*    None
*
**//***************************************************************************/
void SimulateKeyboard(void)
{
    static uint8 keyboard_data[KEYBOARD_DATA_SIZE]={0,0,0,0,0,0,0,0};
    CYBLE_API_RESULT_T apiResult;
   
    if(CyBle_GattGetBusStatus() == CYBLE_STACK_STATE_FREE)
    {

        
        keyboard_data[3u] = SendKeyForDetectedGesture();
            
        apiResult = CyBle_HidssGetCharacteristicValue(CYBLE_HUMAN_INTERFACE_DEVICE_SERVICE_INDEX, 
            CYBLE_HIDS_PROTOCOL_MODE, sizeof(protocol), &protocol);
        if(apiResult == CYBLE_ERROR_OK)
        {
            /* Send notification only when a valid gesture is detected */
            if(0 != keyboard_data[3u])
            {
                apiResult = CyBle_HidssSendNotification(cyBle_connHandle, CYBLE_HUMAN_INTERFACE_DEVICE_SERVICE_INDEX, 
                    CYBLE_HUMAN_INTERFACE_DEVICE_REPORT_IN, KEYBOARD_DATA_SIZE, keyboard_data);
            }
            if(apiResult == CYBLE_ERROR_OK)
            {
                keyboard_data[2u] = 0u;                       /* Set up keyboard data*/
                keyboard_data[3u] = 0u;                       /* Set up keyboard data*/

                apiResult = CyBle_HidssSendNotification(cyBle_connHandle, CYBLE_HUMAN_INTERFACE_DEVICE_SERVICE_INDEX, 
                    CYBLE_HUMAN_INTERFACE_DEVICE_REPORT_IN, KEYBOARD_DATA_SIZE, keyboard_data);

            }
            if(apiResult != CYBLE_ERROR_OK)
            {
                keyboardSimulation = DISABLED;
            }
        }
    }
}