void APP2_Tasks ( void )
{
/* Update the application state machine based
     * on the current state */

    switch(app2Data.state)
    {
        case APP2_STATE_INIT:

            /* Open the device layer */
            app2Data.deviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE );

            if(app2Data.deviceHandle != USB_DEVICE_HANDLE_INVALID)
            {
                /* Register a callback with device layer to get event notification (for end point 0) */
                USB_DEVICE_EventHandlerSet(app2Data.deviceHandle, APP_USBDeviceEventHandler, 0);

                app2Data.state = APP2_STATE_WAIT_FOR_CONFIGURATION;
            }
            else
            {
                /* The Device Layer is not ready to be opened. We should try
                 * again later. */
            }

            break;

        case APP2_STATE_WAIT_FOR_CONFIGURATION:

            /* Check if the device was configured */
            if(app2Data.isConfigured)
            {
                app2Data.state = APP2_STATE_SCHEDULE_READ;
            }
            break;
            
        case APP2_STATE_SCHEDULE_READ:
            if(APP_StateReset())
            {
                break;
            }
            
            /* If a read is complete, then schedule a read
            * else wait for the current read to complete */
            
            app2Data.state = APP2_STATE_WAIT_FOR_READ_COMPLETE;
            if(app2Data.isReadComplete == true)
            {
                app2Data.isReadComplete = false;
                app2Data.readTransferHandle =  USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID;
                
                USB_DEVICE_CDC_Read (USB_DEVICE_CDC_INDEX_0,
                        &app2Data.readTransferHandle, app2Data.readBuffer,
                        APP_READ_BUFFER_SIZE);
                
                if(app2Data.readTransferHandle == USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID)
                {
                    app2Data.state = APP2_STATE_ERROR;
                    break;
                }
            }
            break;

        case APP2_STATE_WAIT_FOR_READ_COMPLETE:
            
            if(APP_StateReset())
            {
                break;
            }
            
            if(app2Data.isReadComplete == true)
            {            
                // Check for delimiter
                int res = APP_CheckForMessage(app2Data.readBuffer);
                if(res == MESSAGE_BAD_POINTER)
                {
                    // Do something
                    break;
                }
                else if(res == MESSAGE_NO_DELIMITER)
                {
                    strcat(messageBuffer, app2Data.readBuffer);
                    memset(app2Data.readBuffer, '\0', APP_READ_BUFFER_SIZE);
                    app2Data.state = APP2_STATE_SCHEDULE_READ;
                    break;
                }
 
                strcat(messageBuffer, app2Data.readBuffer);
                // Parse for the command being sent so we can handle it
                int command = APP_ParseCommand(messageBuffer);

                // Lets build our response message
                // First initialize the JSON value and object from parson library
                JSON_Value *rootValue = json_value_init_object();
                JSON_Object *rootObject = json_value_get_object(rootValue);
                char *serializedString = NULL;  
                    
                // Build response and handle the command
                switch(command)
                {
                    // If hello command, respond with discovery packet 
                    case COMMAND_HELLO:
                            json_object_dotset_string(rootObject, "message.command", "discovery");
                            json_object_dotset_string(rootObject, "message.discovery_object.title", APP_TITLE);
                            json_object_dotset_string(rootObject, "message.discovery_object.part_number", APP_PART_NUMBER);
                            json_object_dotset_string(rootObject, "message.discovery_object.mac_address", appData.macAddress);
                            json_object_dotset_string(rootObject, "message.discovery_object.firmware_version", APP_FIRMWARE_VERSION);
                            json_object_dotset_string(rootObject, "message.discovery_object.harmony_version", SYS_VERSION_STR);
                            json_object_dotset_boolean(rootObject, "message.discovery_object.is_commissioned", appData.isCommissioned);
                        break;
                        
                    // If configuration command, respond with ACK/NACK 
                    case COMMAND_CONFIGURE:
                    {
                        if(appData.isCommissioned == false)
                        {
                            JSON_Value *messageRoot = json_parse_string(messageBuffer);
                            if(json_value_get_type(messageRoot) != JSONObject)
                            {
                                BuildAckNackMessage(rootObject, "nack", "Invalid JSON Object");
                                break;
                            }
                            else
                            {
                                JSON_Object *messageObject = json_value_get_object(messageRoot);
                                if( json_object_dotget_string(messageObject, "message.configuration_object.aws_iot_endpoint_address") != NULL &&
                                    json_object_dotget_string(messageObject, "message.configuration_object.aws_certificate") != NULL &&
                                    json_object_dotget_string(messageObject, "message.configuration_object.aws_certificate_private_key") != NULL )
                                {
                                    json_object_dotset_string(rootObject, "message.command", "ack");
                                    json_object_dotset_string(rootObject, "message.ack_nack_message", "Writing commission parameters to flash");
                                    sprintf((char *)appData.host, json_object_dotget_string(messageObject, "message.configuration_object.aws_iot_endpoint_address"));
                                    sprintf((char *)appData.clientCert, json_object_dotget_string(messageObject, "message.configuration_object.aws_certificate"));
                                    sprintf((char *)appData.clientKey, json_object_dotget_string(messageObject, "message.configuration_object.aws_certificate_private_key"));
                                    appData.isCommissioned = true;
                                    appData.writeToNVM = true;
                                }
                                else
                                {
                                    BuildAckNackMessage(rootObject, "nack", "Invalid commission parameters");
                                    break;
                                }
                            } 
                        }
                        else
                        {
                            BuildAckNackMessage(rootObject, "nack", "Already commissioned");
                        }                    
                        break;
                    }
                    
                    case COMMAND_DEBUG_SET:
                    {
                        JSON_Value *messageRoot = json_parse_string(messageBuffer);
                        if(json_value_get_type(messageRoot) != JSONObject)
                        {
                            BuildAckNackMessage(rootObject, "nack", "Invalid JSON Object");
                            break;
                        }
                        JSON_Object *messageObject = json_value_get_object(messageRoot);
                        if(messageObject == NULL)
                        {
                            BuildAckNackMessage(rootObject, "nack", "NULL Pointer");
                            break;
                        }
                        BuildAckNackMessage(rootObject, "ack", "Received debug set");
                        int DebugMessage = DEBUG_UPDATE;
                        xQueueSendToFront(app2Data.debugQueue, &DebugMessage, 1);
                        if(json_object_dotget_boolean(messageObject, "message.debug_set_object.set"))
                            appData.debugSet = true;
                        else
                            appData.debugSet = false;
                        break;
                    }
                    
                    case COMMAND_BAD_JSON:
                            BuildAckNackMessage(rootObject, "nack", "Bad JSON");
                        break;
                    
                    case COMMAND_INVALID:
                            BuildAckNackMessage(rootObject, "nack", "Command Invalid");
                        break;
                        
                    default:
                            BuildAckNackMessage(rootObject, "nack", "Something went wrong");
                        break;
                }
                memset(app2Data.writeBuffer, '\0', APP_WRITE_BUFFER_SIZE);
                // With our response built, serialize the response into a string
                serializedString = json_serialize_to_string(rootValue);
                strcpy(app2Data.writeBuffer, serializedString);
                // Find length of string and add delimiter to end
                app2Data.writeBuffer[strlen(app2Data.writeBuffer)] = '\r';
                
                json_free_serialized_string(serializedString);  
                // Reset string buffers
                memset(app2Data.readBuffer, '\0', APP_READ_BUFFER_SIZE);
                memset(messageBuffer, '\0', APP_MESSAGE_BUFFER_SIZE);
                app2Data.state = APP2_STATE_SCHEDULE_WRITE;
            }
            else
            {
                app2Data.state = APP2_STATE_WAIT_FOR_READ_COMPLETE;
                if( uxQueueMessagesWaiting( app2Data.debugQueue ) > 0 )
                {
                    int debugMessageNumber;
                    xQueueReceive( app2Data.debugQueue, &debugMessageNumber, 1 );
                    sprintf(app2Data.writeBuffer,                       
                    "{"                                                  
                         "\"message\":{"                                 
                            "\"command\":\"debug\","
                                "\"debug_object\":{"
                                    "\"board_ip_address\":\"%d.%d.%d.%d\","
                                    "\"aws_iot_endpoint\":\"%s\","
                                    "\"mac_address\":\"%s\","
                                    "\"socket_connected\":%s,"
                                    "\"mqtt_connected\":%s,"
                                    "\"raw_message\":\"%s\""
                    "}}}\r",
                    appData.board_ipAddr.v4Add.v[0],
                    appData.board_ipAddr.v4Add.v[1],
                    appData.board_ipAddr.v4Add.v[2],
                    appData.board_ipAddr.v4Add.v[3],
                    appData.host,
                    appData.macAddress,
                    (appData.socket_connected ? "true" : "false"),
                    (appData.mqtt_connected ? "true" : "false"),
                    APP_ReturnDebugCodeToString(debugMessageNumber));
                    app2Data.state = APP2_STATE_SCHEDULE_DEBUG_WRITE;
                }
            }
            break;
            
                        
        case APP2_STATE_SCHEDULE_WRITE:

            if(APP_StateReset())
            {
                break;
            }

            app2Data.writeTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID;
            app2Data.isWriteComplete = false;
            app2Data.state = APP2_STATE_WAIT_FOR_WRITE_COMPLETE;

            USB_DEVICE_CDC_Write(USB_DEVICE_CDC_INDEX_0,
                &app2Data.writeTransferHandle, app2Data.writeBuffer, strlen(app2Data.writeBuffer),
                USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE);

            break;
            
        case APP2_STATE_WAIT_FOR_WRITE_COMPLETE:

            if(APP_StateReset())
            {
                break;
            }

            /* Check if message sent.  The isWriteComplete
             * flag gets updated in the CDC event handler */

            if(app2Data.isWriteComplete == true)
            {
                app2Data.state = APP2_STATE_SCHEDULE_READ;
            }
            break;
            
        case APP2_STATE_SCHEDULE_DEBUG_WRITE:
            if(APP_StateReset())
            {
                break;
            }

            app2Data.writeTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID;
            app2Data.isWriteComplete = false;
            app2Data.state = APP2_STATE_WAIT_FOR_DEBUG_WRITE_COMPLETE;

            USB_DEVICE_CDC_Write(USB_DEVICE_CDC_INDEX_0,
                &app2Data.writeTransferHandle, app2Data.writeBuffer, strlen(app2Data.writeBuffer),
                USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE);

            break;
            
        case APP2_STATE_WAIT_FOR_DEBUG_WRITE_COMPLETE:

            if(APP_StateReset())
            {
                break;
            }

            /* Check if message sent.  The isWriteComplete
             * flag gets updated in the CDC event handler */

            if(app2Data.isWriteComplete == true)
            {
                app2Data.state = APP2_STATE_WAIT_FOR_READ_COMPLETE;
            }
            break;

        case APP2_STATE_ERROR:
            break;
        default:
            break;
    }
}
示例#2
0
文件: app.c 项目: ctapang/v0_70_01b
/**********************************************************
 * Application tasks routine. This function implements the
 * application state machine.
 ***********************************************************/
void APP_Tasks ( APP_DATA * appData )
{

    /* Update the application state machine based
     * on the current state */

    switch(appData->state)
    {
        case APP_STATE_INIT:

            /* Open the device layer */
            appData->deviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0,
                    DRV_IO_INTENT_READWRITE );

            if(appData->deviceHandle == USB_DEVICE_HANDLE_INVALID)
            {
                SYS_ASSERT(false, "Could not open device layer");
                appData->state = APP_STATE_ERROR;
                break;
            }

            /* Register a callback with device layer to get event notification (for end point 0) */
            USB_DEVICE_EventCallBackSet(appData->deviceHandle, APP_USBDeviceEventHandler);

            /* Attach the device */
            USB_DEVICE_Attach (appData->deviceHandle);

            appData->state = APP_STATE_WAIT_FOR_CONFIGURATION;

            break;

        case APP_STATE_WAIT_FOR_CONFIGURATION:

            /* Check if the device was configured */
            if(appData->isConfigured)
            {
                /* Schedule the first read from CDC function driver */
                
                appData->state = APP_STATE_CHECK_CDC_READ;
                appData->isReadComplete = false;
                USB_DEVICE_CDC_Read (appData->cdcInstance, &appData->readTransferHandle,
                        appData->readBuffer, 64);
            }
            break;
            
        case APP_STATE_CHECK_CDC_READ:

            if(APP_StateReset())
            {
                break;
            }

            /* If a read is complete, then schedule a read
             * else check if UART has received data */
           
            if(appData->isReadComplete == true)
            {
                appData->isReadComplete = false;
                APP_UART2Write(appData->readBuffer, appData->readLength);
                appData->state = APP_STATE_CHECK_UART_TRANSMIT_COMPLETE;
            }
            else
            {
                appData->state = APP_STATE_CHECK_UART_RECEIVE;
            }
           
            break;

        case APP_STATE_CHECK_UART_TRANSMIT_COMPLETE:

            /* Check if the UART transmission is complete */
            if(!APP_UART2TransmitIsBusy())
            {
                /* This means UART transmit is done. Schedule the next
                 * read  and go to the next state */

                appData->isReadComplete = false;
                USB_DEVICE_CDC_Read (appData->cdcInstance, &appData->readTransferHandle,
                        appData->readBuffer, 64);
                appData->state = APP_STATE_CHECK_UART_RECEIVE;
            }
   
        case APP_STATE_CHECK_UART_RECEIVE:
               
            if(APP_StateReset())
            {
                break;
            }

            /* Check if a character was received on the UART */

            if(APP_UART2ReceiveReady())
            {
                /* Send the character to the CDC function driver */

                USB_DEVICE_CDC_Write(0, &appData->writeTransferHandle,
                        &appData->uartReceivedData, 1,
                        USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE);

            }

            appData->state = APP_STATE_CHECK_CDC_READ;
            break;
      
        case APP_STATE_ERROR:
            break;
        default:
            break;
    }
} 
示例#3
0
文件: app.c 项目: ctapang/v0_70_01b
void APP_Tasks (void )
{
    switch(appData.state)
    {
        case APP_STATE_INIT:

            /* Open an instance of the device layer */
            appData.deviceHandle = USB_DEVICE_Open( 0, DRV_IO_INTENT_READWRITE );

            /* Register a callback with device layer to get
             * event notification (for end point 0) */
            USB_DEVICE_EventCallBackSet(appData.deviceHandle,
                    APP_USBDeviceEventHandler);

            /* Attach the device */

            USB_DEVICE_Attach(appData.deviceHandle);

            appData.state = APP_STATE_WAIT_FOR_CONFIGURATION;

            break;
        
        case APP_STATE_WAIT_FOR_CONFIGURATION:

            /* Check if the device is configured. The
             * isConfigured flag is updated in the
             * Device Event Handler */

            if(appData.isConfigured)
            {
                /* Initialize the flag and place a request for a
                 * output report */

                appData.isReportReceived = false;

                USB_DEVICE_HID_ReportReceive(appData.hidInstance,
                        &appData.receiveTransferHandle,
                        (uint8_t *)&appData.keyboardOutputReport,1);

                appData.state = APP_STATE_CHECK_IF_CONFIGURED;
            }

            break;

        case APP_STATE_CHECK_IF_CONFIGURED:

            /* This state is needed because the device can get
             * unconfigured asynchronously. Any application state
             * machine reset should happen within the state machine
             * context only. */

            if(appData.isConfigured)
            {
                appData.state = APP_STATE_SWITCH_PROCESS;
            }
            else
            {
                /* This means the device got de-configured.
                 * We reset the state and the wait for configuration */

                APP_StateReset();
                appData.state = APP_STATE_WAIT_FOR_CONFIGURATION;
            }
            break;

        case APP_STATE_SWITCH_PROCESS:

            /* Process the switch state and go to the
             * next state. */
            
            APP_ProcessSwitchPress(&appData);
            appData.state = APP_STATE_CHECK_FOR_OUTPUT_REPORT;
            break;

        case APP_STATE_CHECK_FOR_OUTPUT_REPORT:

            if(appData.isReportReceived == true)
            {
                /* Update the LED and schedule and
                 * request */

                APP_KeyboardLEDStatus();

                appData.isReportReceived = false;
                USB_DEVICE_HID_ReportReceive(appData.hidInstance,
                        &appData.receiveTransferHandle,
                        (uint8_t *)&appData.keyboardOutputReport,1);
            }

            appData.state = APP_STATE_EMULATE_KEYBOARD;
            break;

        case APP_STATE_EMULATE_KEYBOARD:

            if(appData.isReportSentComplete)
            {
                /* This means report can be sent*/
                
                APP_EmulateKeyboard();
                
                appData.isReportSentComplete = false;
                USB_DEVICE_HID_ReportSend(appData.hidInstance,
                    &appData.sendTransferHandle,
                    (uint8_t *)&appData.keyboardInputReport,
                    sizeof(KEYBOARD_INPUT_REPORT));
             }

            appData.state = APP_STATE_CHECK_IF_CONFIGURED;
            break;

        case APP_STATE_ERROR:
            break;

        default:
            break;
    }
  
}
示例#4
0
文件: app.c 项目: therrma2/me433
void APP_Tasks ( void )
{

    /* Update the application state machine based
     * on the current state */


    switch(appData.state)
    {
        case APP_STATE_INIT:

            /* Open the device layer */
            appData.deviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0,    DRV_IO_INTENT_READWRITE );

            if(appData.deviceHandle != USB_DEVICE_HANDLE_INVALID)
            {
                appData.usartHandle = DRV_USART_Open(DRV_USART_INDEX_0,
                    (DRV_IO_INTENT_EXCLUSIVE|DRV_IO_INTENT_READWRITE|DRV_IO_INTENT_NONBLOCKING));

                /* Register a callback with device layer to get event notification (for end point 0) */
                USB_DEVICE_EventHandlerSet(appData.deviceHandle, APP_USBDeviceEventHandler, 0);

                

                appData.state = APP_STATE_WAIT_FOR_CONFIGURATION;
            }
            else
            {
                /* The Device Layer is not ready to be opened. We should try
                 * again later. */
            }

            break;

        case APP_STATE_WAIT_FOR_CONFIGURATION:

            /* Check if the device was configured */
            if(appData.isConfigured)
            {
                /* Schedule the first read from CDC function driver */

                appData.state = APP_STATE_CHECK_CDC_READ;
                appData.isReadComplete = false;
                USB_DEVICE_CDC_Read (appData.cdcInstance, &(appData.readTransferHandle),
                        appData.readBuffer, APP_READ_BUFFER_SIZE);
            }
            break;

        case APP_STATE_CHECK_CDC_READ:

            if(APP_StateReset())
            {
                break;
            }

            /* If a read is complete, then schedule a read
             * else check if UART has received data */

            if(appData.isReadComplete == true)
            {
                appData.isReadComplete = false;
                DRV_USART_Write(appData.usartHandle, appData.readBuffer, appData.readLength);
                USB_DEVICE_CDC_Read (appData.cdcInstance, &appData.readTransferHandle,
                        appData.readBuffer, APP_READ_BUFFER_SIZE);
                
                for(ii=0; ii<appData.readLength; ii++) {
                    if (appData.readBuffer[ii] == '\n' || appData.readBuffer[ii] == '\r'){
                        rx[pos] = 0;
                        txFlag = 1;
                        pos = 0;
                        sscanf(rx,"%d",&qq);
                        LATAbits.LATA4 = 1;
                        if (qq>0){
                            if(qq>500000){
                            OC2RS = qq-600000;
                            }   
                            if(qq<500000){
                                OC1RS = qq-400000;
                            }
                        }
                    }
                    else {
                        rx[pos] = appData.readBuffer[ii];
                        pos++;
                    }
                }

                
            }

            appData.state = APP_STATE_CHECK_UART_RECEIVE;
            break;

        case APP_STATE_CHECK_UART_RECEIVE:

            if(APP_StateReset())
            {
                break;
            }

            /* Check if a character was received on the UART */
            if (txFlag == 1) {
            qq = 7;
                char len = sprintf(tx,"qq = %d\r\n",qq);
                for (ii = 0;ii<len;ii++) {
                    
                    appData.uartReceivedData[ii]=tx[ii];
                }
                USB_DEVICE_CDC_Write(0, &appData.writeTransferHandle,
                        appData.uartReceivedData, len,
                        USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE);
                
                txFlag = 0;
            }

            appData.state = APP_STATE_CHECK_CDC_READ;
            break;

        case APP_STATE_ERROR:
            break;
        default:
            break;
    }
}
示例#5
0
文件: app.c 项目: kangmengl/ME433
void APP_Tasks ( void )
{
    USB_DEVICE_CDC_RESULT writeRequestResult;

    //set pwm cycles
    //OC1RS=num1*100;
    //OC2RS=20000;

    OC1RS=30000;
    OC2RS=num1*100;
   
    /* Update the application state machine based
     * on the current state */
    //int writeRequestResult = USB_DEVICE_CDC_Write(0, &appData.writeTransferHandle, message, len, USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE);
    
    switch(appData.state)
    {
        case APP_STATE_INIT:

            /* Open the device layer */
            appData.deviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0,    DRV_IO_INTENT_READWRITE );

            if(appData.deviceHandle != USB_DEVICE_HANDLE_INVALID)
            {
                appData.usartHandle = DRV_USART_Open(DRV_USART_INDEX_0,
                    (DRV_IO_INTENT_EXCLUSIVE|DRV_IO_INTENT_READWRITE|DRV_IO_INTENT_NONBLOCKING));

                /* Register a callback with device layer to get event notification (for end point 0) */
                USB_DEVICE_EventHandlerSet(appData.deviceHandle, APP_USBDeviceEventHandler, 0);

                

                appData.state = APP_STATE_WAIT_FOR_CONFIGURATION;
            }
            else
            {
                /* The Device Layer is not ready to be opened. We should try
                 * again later. */
            }

            break;

        case APP_STATE_WAIT_FOR_CONFIGURATION:

            /* Check if the device was configured */
            if(appData.isConfigured)
            {
                /* Schedule the first read from CDC function driver */

                appData.state = APP_STATE_CHECK_CDC_READ;
                appData.isReadComplete = false;
                USB_DEVICE_CDC_Read (appData.cdcInstance, &(appData.readTransferHandle),
                        appData.readBuffer, 64);
            }
            break;

        case APP_STATE_CHECK_CDC_READ:

            if(APP_StateReset())
            {
                break;
            }

            /* If a read is complete, then schedule a read
             * else check if UART has received data */

            if(appData.isReadComplete == true)
            {
                appData.isReadComplete = false;
                USB_DEVICE_CDC_Read (appData.cdcInstance, &appData.readTransferHandle,
                        appData.readBuffer, 64);

                buffer[0]=appData.readBuffer[0];
                if (buffer[1] == 'a') {
                    if (LATBbits.LATB15 == 0) {          //toggle b5
                        LATBbits.LATB15 =1;
                    }
                    else {
                         LATBbits.LATB15 =0;
                    }
                }
                //DRV_USART_Write(appData.usartHandle, appData.readBuffer, appData.readLength);
            }

            /*
            // messages come in one char at a time
            // if the char is a newline, then the message is complete

            if(appData.readBuffer[0]=='\n'||appData.readBuffer[0]=='\r'){
            sscanf(rxbuffer,"%d %d",&num1,&num2);
            rxbuffer_index = 0;
            // now use num1 and num2 for whatever
            }
            // otherwise put the char in a buffer and increment the index
            else {
            rxbuffer[rxbuffer_index] = appData.readBuffer[0];
            rxbuffer_index++;
            // watch out for running over the buffer
            if(rxbuffer_index == 100){
            rxbuffer_index = 0;
            }
            }
            */


            
            //phone CDC
            int i = 0;
            //char rxbuffer[100];
            //int num1; int num2;
            for(i=0;i<64;i++) {
            rxbuffer[i] = appData.readBuffer[i];
            }
             sscanf(rxbuffer,"%d %d",&num1,&num2);  //this is where the buffer is read in, num1 stores the COM
             if (LATBbits.LATB15 == 0) {          //toggle b15
                        LATBbits.LATB5 = 1;
            }
                    if (LATBbits.LATB5 == 1) {          //toggle b15
                        LATBbits.LATB5 = 0;
            }

            appData.state = APP_STATE_CHECK_UART_RECEIVE;
            break;

        case APP_STATE_CHECK_UART_RECEIVE:


            /*if (LATBbits.LATB15 == 0) {          //toggle b15
                        LATBbits.LATB15 = 1;
            }
                    else {
                         LATBbits.LATB15 = 0;
                    }
             */
            if(APP_StateReset())
            {
                break;
            }

            /* Check if a character was received on the UART */

            /*if(DRV_USART_Read(appData.usartHandle, &appData.uartReceivedData, 1) > 0)
            {
                //* We have received data on the UART

                USB_DEVICE_CDC_Write(0, &appData.writeTransferHandle,
                        &appData.uartReceivedData, 1,
                        USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE);

            }*/
            int len = 0;  
          while(message[len]>0){  
            len++;  
          }
            writeRequestResult = USB_DEVICE_CDC_Write(0, &appData.writeTransferHandle, message, len, USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE);
            if(USB_DEVICE_CDC_RESULT_OK != writeRequestResult) {
            // turn on another led if there is an error
                //LATBbits.LATB15 = 1;
            }
             else {
            // turn off the led if there is not an error
                //LATBbits.LATB15 = 0;
             }
           

            appData.state = APP_STATE_CHECK_CDC_READ;
            break;

        case APP_STATE_ERROR:
            break;
        default:
            break;
    }
}
示例#6
0
void APP_Tasks ( void )
{
     USB_DEVICE_CDC_RESULT writeRequestResult;
    /* Update the application state machine based
     * on the current state */
     
    switch(appData.state)
    {
        case APP_STATE_INIT:

            /* Open the device layer */
            appData.deviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0,    DRV_IO_INTENT_READWRITE );

            if(appData.deviceHandle != USB_DEVICE_HANDLE_INVALID)
            {
                appData.usartHandle = DRV_USART_Open(DRV_USART_INDEX_0,
                    (DRV_IO_INTENT_EXCLUSIVE|DRV_IO_INTENT_READWRITE|DRV_IO_INTENT_NONBLOCKING));

                /* Register a callback with device layer to get event notification (for end point 0) */
                USB_DEVICE_EventHandlerSet(appData.deviceHandle, APP_USBDeviceEventHandler, 0);

                

                appData.state = APP_STATE_WAIT_FOR_CONFIGURATION;
            }
            else
            {
                /* The Device Layer is not ready to be opened. We should try
                 * again later. */
            }

            break;

        case APP_STATE_WAIT_FOR_CONFIGURATION:

            /* Check if the device was configured */
            if(appData.isConfigured)
            {
                /* Schedule the first read from CDC function driver */

                appData.state = APP_STATE_CHECK_CDC_READ;
                appData.isReadComplete = false;
                USB_DEVICE_CDC_Read (appData.cdcInstance, &(appData.readTransferHandle),
                        appData.readBuffer, 64);
            }
            break;

        case APP_STATE_CHECK_CDC_READ:

            if(APP_StateReset())
            {
                break;
            }

            /* If a read is complete, then schedule a read
             * else check if UART has received data */

            if(appData.isReadComplete == true)
            {
                appData.isReadComplete = false;
                USB_DEVICE_CDC_Read (appData.cdcInstance, &appData.readTransferHandle,
                        appData.readBuffer, 64);
                int i = 0;
                char rxbuffer[100];
                int angle; int com1;
                for(i=0;i<64;i++) {
                rxbuffer[i] = appData.readBuffer[i];
                }
            sscanf(rxbuffer,"%d %d",&angle,&com1);
            float vel=0;
            vel=10000-(angle-0)*20;
            if (vel<5000)
            {
                vel=5000;
            }
            float vel1 = 0;
            float vel2 = 0;
            float P=0.0;
            vel1=vel;
            vel2=vel;
            float error=320-com1;
            if (error>=0)
            {
                vel1=vel;            
                vel2=vel-error*300000*10000/(vel1*vel1);
                if (vel2<3000)
                {
                    vel2=3000;
                }
                
            }
            else 
            {
                vel2=vel;          
                vel1=vel+error*300000*10000/(vel2*vel2);
                if (vel1<3000)
                {
                    vel1=3000;
                }
                
            }
            OC1RS= (int) vel1;
            OC2RS= (int) vel2;
            }

            appData.state = APP_STATE_CHECK_UART_RECEIVE;
            break;
    
        case APP_STATE_CHECK_UART_RECEIVE:

            if(APP_StateReset())
            {
                break;
            }

            /* Check if a character was received on the UART */

//            if(DRV_USART_Read(appData.usartHandle, &appData.uartReceivedData, 1) > 0)
//            {
//                /* We have received data on the UART */
//
//                USB_DEVICE_CDC_Write(0, &appData.writeTransferHandle,
//                        &appData.uartReceivedData, 1,
//                        USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE);
//
//            }
//          sprintf(message,"ritwik \n");  
//          int len = 0;  
//          while(message[len]>0){  
//            len++;  
//          }    
//          writeRequestResult = USB_DEVICE_CDC_Write(0, &appData.writeTransferHandle, message, len, USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE);

          if(USB_DEVICE_CDC_RESULT_OK != writeRequestResult) {  
            // turn on another led if there is an error  
          }  
          else {  
            // turn off the led if there is not an error  
          }    
            
            

            appData.state = APP_STATE_CHECK_CDC_READ;
            break;

        case APP_STATE_ERROR:
            break;
        default:
            break;
    }
}
示例#7
0
void APP_Tasks ( void )
{

    /* Update the application state machine based
     * on the current state */

    switch(appData.state)
    {
        case APP_STATE_INIT:

            /* Open the device layer */
            appData.deviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0,    DRV_IO_INTENT_READWRITE );

            if(appData.deviceHandle != USB_DEVICE_HANDLE_INVALID)
            {
                appData.usartHandle = DRV_USART_Open(DRV_USART_INDEX_0,
                    (DRV_IO_INTENT_EXCLUSIVE|DRV_IO_INTENT_READWRITE|DRV_IO_INTENT_NONBLOCKING));

                /* Register a callback with device layer to get event notification (for end point 0) */
                USB_DEVICE_EventHandlerSet(appData.deviceHandle, APP_USBDeviceEventHandler, 0);

                

                appData.state = APP_STATE_WAIT_FOR_CONFIGURATION;
            }
            else
            {
                /* The Device Layer is not ready to be opened. We should try
                 * again later. */
            }

            break;

        case APP_STATE_WAIT_FOR_CONFIGURATION:

            /* Check if the device was configured */
            if(appData.isConfigured)
            {
                /* Schedule the first read from CDC function driver */

                appData.state = APP_STATE_CHECK_CDC_READ;
                appData.isReadComplete = false;
                USB_DEVICE_CDC_Read (appData.cdcInstance, &(appData.readTransferHandle),
                        appData.readBuffer, APP_READ_BUFFER_SIZE);
            }
            break;

        case APP_STATE_CHECK_CDC_READ:

            if(APP_StateReset())
            {
                break;
            }

            /* If a read is complete, then schedule a read
             * else check if UART has received data */

            if(appData.isReadComplete == true)
            {
                appData.isReadComplete = false;
                DRV_USART_Write(appData.usartHandle, appData.readBuffer, appData.readLength);
                USB_DEVICE_CDC_Read (appData.cdcInstance, &appData.readTransferHandle,
                        appData.readBuffer, APP_READ_BUFFER_SIZE);
                
            }

            appData.state = APP_STATE_CHECK_UART_RECEIVE;
            break;

        case APP_STATE_CHECK_UART_RECEIVE:

            if(APP_StateReset())
            {
                break;
            }

            /* Check if a character was received on the UART */

            if(DRV_USART_Read(appData.usartHandle, appData.uartReceivedData, 1) > 0)
            {
                /* We have received data on the UART */

                USB_DEVICE_CDC_Write(0, &appData.writeTransferHandle,
                        appData.uartReceivedData, 1,
                        USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE);

            }

            appData.state = APP_STATE_CHECK_CDC_READ;
            break;

        case APP_STATE_ERROR:
            break;
        default:
            break;
    }
}
示例#8
0
文件: app.c 项目: ctapang/v0_70b
/**********************************************************
 * Application tasks routine. This function implements the
 * application state machine.
 ***********************************************************/
void APP_Tasks ( APP_DATA * appData )
{

    /* Update the application state machine based
     * on the current state */

    USB_DEVICE_CDC_RESULT result;

    APP_ProcessSwitchPress(appData);

    switch(appData->state)
    {
        case APP_STATE_INIT:

            /* Open the device layer */
            appData->deviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0,
                    DRV_IO_INTENT_READWRITE );

            if(appData->deviceHandle == USB_DEVICE_HANDLE_INVALID)
            {
                SYS_ASSERT(false, "Could not open device layer");
                appData->state = APP_STATE_ERROR;
                break;
            }

            /* Register a callback with device layer to get event notification (for end point 0) */
            USB_DEVICE_EventCallBackSet(appData->deviceHandle, APP_USBDeviceEventHandler);

            /* Attach the device */
            USB_DEVICE_Attach (appData->deviceHandle);

            appData->state = APP_STATE_WAIT_FOR_CONFIGURATION;

            break;

        case APP_STATE_WAIT_FOR_CONFIGURATION:

            /* Check if the device was configured */
            if(appData->isConfigured)
            {
                /* If the device is configured then lets start reading */
                appData->state = APP_STATE_SCHEDULE_READ;
            }
            break;
            
        case APP_STATE_SCHEDULE_READ:

            if(APP_StateReset())
            {
                break;
            }

            /* If a read is complete, then schedule a read
             * else wait for the current read to complete */

            appData->state = APP_STATE_WAIT_FOR_READ_COMPLETE;
            if(appData->isReadComplete == true)
            {
                appData->isReadComplete = false;
                appData->readTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID;
                USB_DEVICE_CDC_Read (appData->cdcInstance, &appData->readTransferHandle,
                        appData->readBuffer, 64);
                if(appData->readTransferHandle == USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID)
                {
                    appData->state = APP_STATE_ERROR;
                    break;
                }
            }
           
            break;

        case APP_STATE_WAIT_FOR_READ_COMPLETE:
        case APP_STATE_CHECK_SWITCH_PRESSED:
        
            if(APP_StateReset())
            {
                break;
            }

            /* Check if a character was received or a switch was pressed.
             * The isReadComplete flag gets updated in the CDC event handler. */

            if(appData->isReadComplete || appData->isSwitchPressed)
            {
                 appData->state = APP_STATE_SCHEDULE_WRITE;
            }

            break;
          
            
        case APP_STATE_SCHEDULE_WRITE:

            if(APP_StateReset())
            {
                break;
            }

            /* Setup the write */

            appData->writeTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID;
            appData->isWriteComplete = false;
            appData->state = APP_STATE_WAIT_FOR_WRITE_COMPLETE;

            if(appData->isSwitchPressed)
            {
                /* If the switch was pressed, then send the switch prompt*/
                appData->isSwitchPressed = false;
                result = USB_DEVICE_CDC_Write(appData->cdcInstance, &appData->writeTransferHandle,
                     switchPrompt, 23, USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE);
            }
            else
            {
                /* Else echo the received character + 1*/
                appData->readBuffer[0] = appData->readBuffer[0] + 1;
                USB_DEVICE_CDC_Write(appData->cdcInstance, &appData->writeTransferHandle,
                    appData->readBuffer, 1, USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE);
            }
                   
            break;

        case APP_STATE_WAIT_FOR_WRITE_COMPLETE:

            if(APP_StateReset())
            {
                break;
            }

            /* Check if a character was sent. The isWriteComplete
             * flag gets updated in the CDC event handler */

            if(appData->isWriteComplete == true)
            {
                appData->state = APP_STATE_SCHEDULE_READ;

            }

            break;

        case APP_STATE_ERROR:
            break;
        default:
            break;
    }
}