void APP_Tasks ( void ) { /* Check the application's current state. */ switch ( appData.state ) { /* Application's initial state. */ case APP_STATE_INIT: { appData.suspended = false; appData.usbDevHandle = -1; /* Open the device layer */ appData.usbDevHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE ); if(appData.usbDevHandle != USB_DEVICE_HANDLE_INVALID) { /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventHandlerSet(appData.usbDevHandle, APP_USBDeviceEventHandler, 0); appData.state = APP_STATE_USB_OPENED; } break; } case APP_STATE_CONFIGURED: { APP_Task_configured_state(); break; //APP_STATE_CONFIGURED } /* The default state should never be executed. */ default: { break; } } app_tuner_updown_tasks(); }
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; } }
void APP_Tasks ( void ) { //static int8_t vector = 0; static uint8_t movement_length = 0; static bool sent_dont_move = false; short accels[3]; double xvel; double yvel; //int8_t dir_table[] ={-4,-4,-4, 0, 4, 4, 4, 0}; /* Check the application's current state. */ switch ( appData.state ) { /* Application's initial 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) { /* 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 is configured. The * isConfigured flag is updated in the * Device Event Handler */ if(appData.isConfigured) { appData.state = APP_STATE_MOUSE_EMULATE; } break; case APP_STATE_MOUSE_EMULATE: APP_ProcessSwitchPress(); /* The following logic rotates the mouse icon when * a switch is pressed */ if(appData.isSwitchPressed) { /* Toggle the mouse emulation with each switch press */ appData.emulateMouse ^= 1; appData.isSwitchPressed = false; } if(appData.emulateMouse) { sent_dont_move = false; if(movement_length > 50) { acc_read_register(OUT_X_L_A, (unsigned char *) accels, 6); xvel = accels[0]/10000; yvel = accels[1]/10000; appData.xCoordinate = (double) xvel; appData.yCoordinate = (double) yvel; appData.mouseButton[0] = MOUSE_BUTTON_STATE_RELEASED; appData.mouseButton[1] = MOUSE_BUTTON_STATE_RELEASED; //appData.xCoordinate =(int8_t)dir_table[vector & 0x07] ; //appData.yCoordinate =(int8_t)dir_table[(vector+2) & 0x07]; //vector ++; movement_length = 0; } } else { appData.mouseButton[0] = MOUSE_BUTTON_STATE_RELEASED; appData.mouseButton[1] = MOUSE_BUTTON_STATE_RELEASED; appData.xCoordinate = 0; appData.yCoordinate = 0; } if(!appData.isMouseReportSendBusy) { if(((sent_dont_move == false) && (!appData.emulateMouse)) || (appData.emulateMouse)) { /* This means we can send the mouse report. The isMouseReportBusy flag is updated in the HID Event Handler. */ appData.isMouseReportSendBusy = true; /* Create the mouse report */ MOUSE_ReportCreate(appData.xCoordinate, appData.yCoordinate, appData.mouseButton, &mouseReport); if(memcmp((const void *)&mouseReportPrevious, (const void *)&mouseReport, (size_t)sizeof(mouseReport)) == 0) { /* Reports are same as previous report. However mouse reports * can be same as previous report as the co-ordinate positions are relative. * In that case it needs to be send */ if((appData.xCoordinate == 0) && (appData.yCoordinate == 0)) { /* If the coordinate positions are 0, that means there * is no relative change */ if(appData.idleRate == 0) { appData.isMouseReportSendBusy = false; } else { /* Check the idle rate here. If idle rate time elapsed * then the data will be sent. Idle rate resolution is * 4 msec as per HID specification; possible range is * between 4msec >= idlerate <= 1020 msec. */ if(appData.setIdleTimer * APP_USB_CONVERT_TO_MILLISECOND >= appData.idleRate * 4) { /* Send REPORT as idle time has elapsed */ appData.isMouseReportSendBusy = true; } else { /* Do not send REPORT as idle time has not elapsed */ appData.isMouseReportSendBusy = false; } } } } if(appData.isMouseReportSendBusy == true) { /* Copy the report sent to previous */ memcpy((void *)&mouseReportPrevious, (const void *)&mouseReport, (size_t)sizeof(mouseReport)); /* Send the mouse report. */ USB_DEVICE_HID_ReportSend(appData.hidInstance, &appData.reportTransferHandle, (uint8_t*)&mouseReport, sizeof(MOUSE_REPORT)); appData.setIdleTimer = 0; } movement_length ++; sent_dont_move = true; } } break; case APP_STATE_ERROR: break; /* The default state should never be executed. */ default: { /* TODO: Handle error in application's state machine. */ break; } } }
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; } }
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; } }
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; } }
void APP_Tasks (void ) { char message[25]; char inputfrompc[25]; int i; short accels[3]; // accelerations for the 3 axes short accelsMAF; short accelsFIR; int buf1=0; int buf2=0; int buf3=0; int buf4=0; int buf5=0; //bn*1000 for FIR float b1=.0088; float b2=.0479; float b3=.1640; float b4=.2793; float b5=.2793; float b6=.1640; float b7=.0479; float b8=.0088; int FIRbuf1=0; int FIRbuf2=0; int FIRbuf3=0; int FIRbuf4=0; int FIRbuf5=0; int FIRbuf6=0; int FIRbuf7=0; int FIRbuf8=0; //sprintf(message,"Hello!"); //use_display(20,20,message); //display_draw(); /* Check if device is configured. See if it is configured with correct * configuration value */ switch(appData.state) { case APP_STATE_INIT: /* Open the device layer */ appData.usbDevHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE ); if(appData.usbDevHandle != USB_DEVICE_HANDLE_INVALID) { /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventHandlerSet(appData.usbDevHandle, 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: if(appData.deviceConfigured == true) { /* Device is ready to run the main task */ appData.hidDataReceived = false; appData.hidDataTransmitted = true; appData.state = APP_STATE_MAIN_TASK; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64); } break; case APP_STATE_MAIN_TASK: if(!appData.deviceConfigured) { /* Device is not configured */ appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; } else if( appData.hidDataReceived ) { /* Look at the data the host sent, to see what * kind of application specific command it sent. */ switch(appData.receiveDataBuffer[0]) { case 0x80: /* Toggle on board LED1 to LED2. */ BSP_LEDToggle( APP_USB_LED_1 ); BSP_LEDToggle( APP_USB_LED_2 ); for (i=0; i<8; i++){ inputfrompc[i] = appData.receiveDataBuffer[i+1]; } use_display(20,20,inputfrompc); display_draw(); appData.hidDataReceived = false; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64 ); break; case 0x81: if(appData.hidDataTransmitted) { /* Echo back to the host PC the command we are fulfilling in * the first byte. In this case, the Get Push-button State * command. */ appData.transmitDataBuffer[0] = 0x81; acc_read_register(OUT_X_L_A, (unsigned char *) accels, 6); sprintf(message,"Z: %d",accels[0]); use_display(10,10,message); display_draw(); if(_CP0_GET_COUNT()>800000){ appData.transmitDataBuffer[1] = 1; appData.transmitDataBuffer[2] = accels[0]>>8; appData.transmitDataBuffer[3] = accels[0]&0xFF; //use MAF buffer values to calculate accelsMAF accelsMAF = ((buf1+buf2+buf3+buf4+buf5+accels[0])/6); //change MAF buffer values buf5=buf4; buf4=buf3; buf3=buf2; buf2=buf1; buf1=accels[0]; //change FIR buffer values FIRbuf8=FIRbuf7; FIRbuf7=FIRbuf6; FIRbuf6=FIRbuf5; FIRbuf5=FIRbuf4; FIRbuf4=FIRbuf3; FIRbuf3=FIRbuf2; FIRbuf2=FIRbuf1; FIRbuf1=accels[0]; //FIR Filtering calculations accelsFIR = (b1*FIRbuf1)+(b2*FIRbuf2)+(b3*FIRbuf3)+(b4*FIRbuf4)+(b5*FIRbuf5)+(b6*FIRbuf6)+(b7*FIRbuf7)+(b8*FIRbuf8); appData.transmitDataBuffer[4] = accelsMAF>>8; appData.transmitDataBuffer[5] = accelsMAF&0xFF; appData.transmitDataBuffer[6] = accelsFIR>>8; appData.transmitDataBuffer[7] = accelsFIR&0xFF; _CP0_SET_COUNT(0); } else{appData.transmitDataBuffer[1]=0;} appData.hidDataTransmitted = false; /* Prepare the USB module to send the data packet to the host */ USB_DEVICE_HID_ReportSend (USB_DEVICE_HID_INDEX_0, &appData.txTransferHandle, appData.transmitDataBuffer, 64 ); appData.hidDataReceived = false; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64 ); } break; default: appData.hidDataReceived = false; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64 ); break; }
void APP_Tasks (void ) { /* Check if device is configured. See if it is configured with correct * configuration value */ switch(appData.state) { case APP_STATE_INIT: /* Open the device layer */ appData.usbDevHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE ); if(appData.usbDevHandle != USB_DEVICE_HANDLE_INVALID) { /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventHandlerSet(appData.usbDevHandle, 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: if(appData.deviceConfigured == true) { /* Device is ready to run the main task */ appData.hidDataReceived = false; appData.hidDataTransmitted = true; appData.state = APP_STATE_MAIN_TASK; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64); } break; case APP_STATE_MAIN_TASK: if(!appData.deviceConfigured) { /* Device is not configured */ appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; } else if( appData.hidDataReceived ) { /* Look at the data the host sent, to see what * kind of application specific command it sent. */ switch(appData.receiveDataBuffer[0]) { case 0x80: /* Toggle on board LED1 to LED2. */ BSP_LEDToggle( APP_USB_LED_1 ); BSP_LEDToggle( APP_USB_LED_2 ); appData.hidDataReceived = false; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64 ); break; case 0x81: if(appData.hidDataTransmitted) { /* Echo back to the host PC the command we are fulfilling in * the first byte. In this case, the Get Push-button State * command. */ appData.transmitDataBuffer[0] = 0x81; if( BSP_SwitchStateGet(APP_USB_SWITCH_1) == BSP_SWITCH_STATE_PRESSED ) { appData.transmitDataBuffer[1] = 0x00; } else { appData.transmitDataBuffer[1] = 0x01; } appData.hidDataTransmitted = false; /* Prepare the USB module to send the data packet to the host */ USB_DEVICE_HID_ReportSend (USB_DEVICE_HID_INDEX_0, &appData.txTransferHandle, appData.transmitDataBuffer, 64 ); appData.hidDataReceived = false; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64 ); } break; default: appData.hidDataReceived = false; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64 ); break; } } case APP_STATE_ERROR: break; default: break; } }
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; } }
void APP_Tasks (void ) { unsigned char print[26]; int jj = 0,row; short accels[3]; /* Check if device is configured. See if it is configured with correct * configuration value */ switch(appData.state) { case APP_STATE_INIT: /* Open the device layer */ appData.usbDevHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE ); if(appData.usbDevHandle != USB_DEVICE_HANDLE_INVALID) { /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventHandlerSet(appData.usbDevHandle, 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: if(appData.deviceConfigured == true) { /* Device is ready to run the main task */ appData.hidDataReceived = false; appData.hidDataTransmitted = true; appData.state = APP_STATE_MAIN_TASK; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64); } break; case APP_STATE_MAIN_TASK: if(!appData.deviceConfigured) { /* Device is not configured */ appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; } else if( appData.hidDataReceived ) { /* Look at the data the host sent, to see what * kind of application specific command it sent. */ switch(appData.receiveDataBuffer[0]) { case 0x1: /* Toggle on board LED1 to LED2. */ BSP_LEDToggle( APP_USB_LED_1 ); BSP_LEDToggle( APP_USB_LED_2 ); memcpy(print,&appData.receiveDataBuffer[2],25); print[26] = '\0'; display_clear(); write_OLED_message(print,appData.receiveDataBuffer[1],0); display_draw(); appData.hidDataReceived = false; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64 ); _CP0_SET_COUNT(0); break; case 0x2: if(appData.hidDataTransmitted) { /* Echo back to the host PC the command we are fulfilling in * the first byte. In this case, the Get Push-button State * command. */ BSP_LEDToggle( APP_USB_LED_1 ); BSP_LEDToggle( APP_USB_LED_2 ); if(_CP0_GET_COUNT() > 200000){ appData.transmitDataBuffer[0] = 1; acc_read_register(OUT_X_L_A,(unsigned char *) accels, 6); appData.transmitDataBuffer[1] = accels[0] >> 8; appData.transmitDataBuffer[2] = accels[0]; appData.transmitDataBuffer[3] = accels[1] >> 8; appData.transmitDataBuffer[4] = accels[1]; appData.transmitDataBuffer[5] = accels[2] >> 8; appData.transmitDataBuffer[6] = accels[2]; _CP0_SET_COUNT(0); } else{ appData.transmitDataBuffer[0] = 0; } // appData.transmitDataBuffer[0] = 0x81; // // if( BSP_SwitchStateGet(APP_USB_SWITCH_1) == BSP_SWITCH_STATE_PRESSED ) // { // appData.transmitDataBuffer[1] = 0x00; // } // else // { // appData.transmitDataBuffer[1] = 0x01; // } appData.hidDataTransmitted = false; /* Prepare the USB module to send the data packet to the host */ USB_DEVICE_HID_ReportSend (USB_DEVICE_HID_INDEX_0, &appData.txTransferHandle, appData.transmitDataBuffer, 64 ); appData.hidDataReceived = false; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64 ); } break; default: appData.hidDataReceived = false; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64 ); break; }
void APP_Tasks(void) { /* Check if device is configured. See if it is configured with correct * configuration value */ switch (appData.state) { case APP_STATE_INIT: /* Open the device layer */ appData.usbDevHandle = USB_DEVICE_Open(USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE); if (appData.usbDevHandle != USB_DEVICE_HANDLE_INVALID) { /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventHandlerSet(appData.usbDevHandle, 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: if (appData.deviceConfigured == true) { /* Device is ready to run the main task */ appData.hidDataReceived = false; appData.hidDataTransmitted = true; appData.state = APP_STATE_MAIN_TASK; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive(USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64); } break; case APP_STATE_MAIN_TASK: if (!appData.deviceConfigured) { /* Device is not configured */ appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; } else if (appData.hidDataReceived) { /* Look at the data the host sent, to see what * kind of application specific command it sent. */ switch (appData.receiveDataBuffer[0]) { case 0x80: /* Toggle on board LED1 to LED2. */ BSP_LEDToggle(APP_USB_LED_1); BSP_LEDToggle(APP_USB_LED_2); setRTR(); break; case 0x81: if (appData.hidDataTransmitted) { /* Echo back to the host PC the command we are fulfilling in * the first byte. In this case, the Get Push-button State * command. */ appData.transmitDataBuffer[0] = 0x81; appData.transmitDataBuffer[1] = 0b1 & BSP_SwitchStateGet(APP_USB_SWITCH_1); appData.transmitDataBuffer[2] = 111; setRTS(); setRTR(); } break; case 0x82: if (!appData.numTX || _CP0_GET_COUNT() > 200000) { //prepare new data to send acc_read_register(OUT_X_L_A, (unsigned char *) appData.accels, 6); appData.transmitDataBuffer[0] = 1; //we have data to send appData.transmitDataBuffer[1] = appData.accels[0] >> 8; //x high byte appData.transmitDataBuffer[2] = appData.accels[0] & 0xFF; //x low byte appData.transmitDataBuffer[3] = appData.accels[1] >> 8; //y high byte appData.transmitDataBuffer[4] = appData.accels[1] & 0xFF; //y low byte appData.transmitDataBuffer[5] = appData.accels[2] >> 8; //z high byte appData.transmitDataBuffer[6] = appData.accels[2] & 0xFF; //z low byte // reset core timer for 100 hz _CP0_SET_COUNT(0); appData.numTX++; } else { appData.transmitDataBuffer[0] = 0; // we don't have new data } setRTS(); setRTR(); break; case 0x83: // prepare for a bout of sending accel data //parse incoming data to screen oled_clear_buffer(); int row = appData.receiveDataBuffer[1]; char * msg; msg = &appData.receiveDataBuffer[2]; oled_draw_string(0, row, msg, 1); oled_update(); // clear buffered accel data so we read new data to send acc_read_register(OUT_X_L_A, (unsigned char *) appData.accels, 6); appData.numTX = 0; //we're starting over setRTR(); break; case 0x84: // done asking for data oled_draw_string(0, 55, "Done!", 1); oled_update(); setRTR(); break; default: setRTR(); break; } }