int main(void) { InitializeSystem(); initADCDMA(); #if defined(USB_INTERRUPT) if(USB_BUS_SENSE && (USBGetDeviceState() == DETACHED_STATE)) { USBDeviceAttach(); } #endif #if defined(USB_POLLING) // Check bus status and service USB interrupts. USBDeviceTasks(); #endif while(1) { if (!ADC_DATA_READY) continue; ADC_DATA_READY = 0; RunFFT(); putrsUSBUSART((char*)&fftOut[0].real); ProcessIO(); } return (EXIT_SUCCESS); }
void ProcessIO(void) { BYTE numBytesRead; //Blink the LEDs according to the USB device status BlinkUSBStatus(); // User Application USB tasks if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return; if(buttonPressed) { if(stringPrinted == FALSE) { if(mUSBUSARTIsTxTrfReady()) { putrsUSBUSART("Button Pressed data-- \r\n"); stringPrinted = TRUE; } } } else { stringPrinted = FALSE; } if(USBUSARTIsTxTrfReady()) { numBytesRead = getsUSBUSART(USB_Out_Buffer,64); if(numBytesRead != 0) { BYTE i; for(i=0;i<numBytesRead;i++) { switch(USB_Out_Buffer[i]) { case 0x0A: case 0x0D: putUSBUSART(&USB_Out_Buffer[i],numBytesRead); break; case 0x53://letter S to start sampling ReadADC(); putUSBUSART(ADC_sample,SAMPLE_SIZE); break; case 0x51: //letter Q to stop break; default: putUSBUSART(&USB_Out_Buffer[i],numBytesRead); break; } } //putUSBUSART(USB_In_Buffer,numBytesRead); } } CDCTxService(); } //end ProcessIO
/******************************************************************** * Function: void ProcessMenu(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: This function parses the input string given * by the user. * * Note: None *******************************************************************/ void ProcessMenu(void) { int i; // Debug // If the line is empty return. if(RS232cp == 0) return; // Strip the CR/NL character. RS232_Out_Data[RS232cp] = '\0'; // Test different menu options if(strcmpram2pgm("help", RS232_Out_Data) == 0) putrsUSBUSART("\r\nAvailable commands:\r\n\r\n* help - Displays this message, recursive style!\r\n* setMessage <text> - Defines a new message to be displayed on the POV.\r\n* showMessage - Shows the current message\r\n* version - Displays firmware version."); else if(strncmpram2pgm("setMessage ", RS232_Out_Data, 10) == 0) { // Copy message strncpy(message, &RS232_Out_Data[11], MESSAGE_LENGTH); message[MESSAGE_LENGTH] = '\0'; writeEEPROM(0x00, crc(0x00, (char) strlen(message))); writeEEPROM(0x01, (char) strlen(message)); writeStrEEPROM(0x03, strlen(message), message); writeEEPROM(0x02, strcrc(message)); // Display confirmation strcpypgm2ram(RS232_Out_Data, "\r\nYour message is now defined as:\r\n\t"); strcat(RS232_Out_Data, message); putsUSBUSART(RS232_Out_Data); } else if(strcmpram2pgm("showMessage", RS232_Out_Data) == 0) { sprintf(RS232_Out_Data, "\r\nYour message is defined as:\r\n\t%s", message); putsUSBUSART(RS232_Out_Data); } else if(strcmpram2pgm("version", RS232_Out_Data) == 0) putrsUSBUSART("\r\nPOV serial configuration. Version 0.9"); else putrsUSBUSART("\r\nUnknown command. Type 'help' for the list of available commands."); clearRS232Buffer = 1; }
/******************************************************************** * Function: void ProcessIO(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: This function is a place holder for other user * routines. It is a mixture of both USB and * non-USB tasks. * * Note: None *******************************************************************/ void ProcessIO(void) { //Blink the LEDs according to the USB device status BlinkUSBStatus(); // User Application USB tasks if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return; if(mUSBUSARTIsTxTrfReady()) { putrsUSBUSART("test123"); } CDCTxService(); } //end ProcessIO
// *--------------------------------------------------------------------------------* void ProcessIO(void){ BYTE numBytesRead; //Blink the LEDs according to the USB device status BlinkUSBStatus(); // User Application USB tasks if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return; if(buttonPressed){ if(stringPrinted == FALSE){ if(mUSBUSARTIsTxTrfReady()){ putrsUSBUSART("Button Pressed -- \r\n"); stringPrinted = TRUE; } } }else{ stringPrinted = FALSE; } if(USBUSARTIsTxTrfReady()){ numBytesRead = getsUSBUSART(USB_Out_Buffer,64); if(numBytesRead != 0){ BYTE i; for(i=0;i<numBytesRead;i++){ switch(USB_Out_Buffer[i]){ case 0x0A: case 0x0D: USB_In_Buffer[i] = USB_Out_Buffer[i]; break; default: USB_In_Buffer[i] = USB_Out_Buffer[i] + 1; break; } } putUSBUSART(USB_In_Buffer,numBytesRead); } } CDCTxService(); }
/******************************************************************** * Function: void ProcessIO(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: This function is a place holder for other user * routines. It is a mixture of both USB and * non-USB tasks. * * Note: None *******************************************************************/ void ProcessIO(void) { char backSpace[3] = {0x8,' ',0x8}; // User Application USB tasks if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return; // If DTR is low no client is connected, back to default. if (UART_DTR == 0) { nextProcessState = P_VERSION; RS232cp = 0; } // Print welcome message if(USBUSARTIsTxTrfReady() && (UART_DTR == 1)) { setLEDs(0); // Print Version if(nextProcessState == P_VERSION) { putrsUSBUSART("\r\n POV serial configuration. Version 0.9\r\n\r\n\ttype 'help' for the list of available commands.\r\n"); nextProcessState = P_PROMPT; } // Print Prompt else if(nextProcessState == P_PROMPT) { putrsUSBUSART("\r\n>> "); nextProcessState = P_INPUT; } else if (nextProcessState == P_INPUT) { // In case the buffer is full, reset the input buffer and display error message. if (RS232cp == CDC_DATA_IN_EP_SIZE) { RS232cp = 0; nextProcessState = P_PROMPT; } // Process IO else { LastRS232Out = getsUSBUSART(&(RS232_Out_Data[RS232cp]), 1); if(LastRS232Out > 0) switch(RS232_Out_Data[RS232cp]) { // Enter/Return, process message case 0x0A: case 0x0D: ProcessMenu(); RS232cp = 0; nextProcessState = P_PROMPT; break; // Backspace case 0x7F: case 0x08: if(RS232cp > 0) {putUSBUSART(backSpace,3); RS232cp--;} break; // Replay character to user default: putUSBUSART(&(RS232_Out_Data[RS232cp]), 1); RS232cp++; } } } } CDCTxService(); // Limpa o buffer caso necess‡rio :) if(clearRS232Buffer == 1) { memset(RS232_Out_Data, '\0', sizeof(RS232_Out_Data)); clearRS232Buffer = 0; } } //end ProcessIO
/********************************************************************* * Function: void APP_DeviceCDCBasicDemoTasks(void); * * Overview: Keeps the demo running. * * PreCondition: The demo should have been initialized and started via * the APP_DeviceCDCBasicDemoInitialize() and APP_DeviceCDCBasicDemoStart() demos * respectively. * * Input: None * * Output: None * ********************************************************************/ void APP_DeviceCDCBasicDemoTasks() { static uint8_t msg_buf_idx = 0; static bool msg_done = false; static char msg_type; /* Check to see if there is a transmission in progress, if there isn't, then * we can see about performing an echo response to data received. */ if( USBUSARTIsTxTrfReady() == true) { uint8_t i; uint8_t numBytesRead; numBytesRead = getsUSBUSART(readBuffer, sizeof(readBuffer)); /* For every byte that was read... */ for(i=0; i<numBytesRead; i++) { if(msg_buf_idx == MSG_BUF_SIZE) { putrsUSBUSART(buttonMessage); msg_buf_idx = 0; msg_buffer[0] = 0; break; } else { if(readBuffer[i] == '\r' || readBuffer[i] == '\n') { msg_buffer[msg_buf_idx] = 0; msg_done = true; break; } if(readBuffer[i] >= '0' && readBuffer[i] <= '9') { msg_buffer[msg_buf_idx] = readBuffer[i]; msg_buf_idx++; } else { msg_type = readBuffer[i]; msg_buf_idx = 0; msg_buffer[msg_buf_idx] = 0; } msg_done = false; } } if(numBytesRead > 0 && msg_done == true && msg_buf_idx > 0) { /* After processing all of the received data, we need to send out * the "echo" data now. */ int number; number = atoi(msg_buffer); //sprintf(writeBuffer, "Type: %c, number: %d\r\n", msg_type, number); if(msg_type == 'c' && number > 0 && number < 128) { PWM1DCH = (uint8_t)number; } if(msg_type == 'm' && number > 0 && number < 128) { PWM2DCH = (uint8_t)number; } //putrsUSBUSART(writeBuffer); msg_buf_idx = 0; } } CDCTxService(); }
/********************************************************************* * Function: void APP_DeviceCDCBasicDemoTasks(void); * * Overview: Keeps the demo running. * * PreCondition: The demo should have been initialized and started via * the APP_DeviceCDCBasicDemoInitialize() and APP_DeviceCDCBasicDemoStart() demos * respectively. * * Input: None * * Output: None * ********************************************************************/ void APP_DeviceCDCBasicDemoTasks() { /* If the user has pressed the button associated with this demo, then we * are going to send a "Button Pressed" message to the terminal. */ if(BUTTON_IsPressed(BUTTON_DEVICE_CDC_BASIC_DEMO) == true) { /* Make sure that we only send the message once per button press and * not continuously as the button is held. */ if(buttonPressed == false) { /* Make sure that the CDC driver is ready for a transmission. */ if(mUSBUSARTIsTxTrfReady() == true) { putrsUSBUSART(buttonMessage); buttonPressed = true; } } } else { /* If the button is released, we can then allow a new message to be * sent the next time the button is pressed. */ buttonPressed = false; } /* Check to see if there is a transmission in progress, if there isn't, then * we can see about performing an echo response to data received. */ if( USBUSARTIsTxTrfReady() == true) { uint8_t numBytesRead; numBytesRead = getsUSBUSART(readBuffer, sizeof(readBuffer)); if (numBytesRead > 0) { switch(readBuffer[0]) { case 0x10: { unsigned char size = readBuffer[1]; debug_flag2 = !debug_flag2; PORTCbits.RC1 = debug_flag2; writeBuffer[0] = 0x90; writeBuffer[1] = 4; writeBuffer[2] = numBytesRead; writeBuffer[3] = readBuffer[1]; writeBuffer[4] = readBuffer[2]; writeBuffer[5] = readBuffer[3]; writeBuffer[1] = 4+size; for (unsigned char i = 0; i<size; i++) { writeBuffer[i+6] = readBuffer[4 + i]; } if (WaitToReadySerial()) putUSBUSART(writeBuffer, writeBuffer[1]+2); WaitToReadySerial(); } { unsigned char size = readBuffer[1]; i2c_start(0x50, 0); // address in big-endian format i2c_send(readBuffer[3]); // address MSB i2c_send(readBuffer[2]); // address LSB for (unsigned char i = 0; i<size; i++) { i2c_send(readBuffer[4 + i]); } i2c_stop(); __delay_ms(10); } break; case 0x11: { unsigned char size = readBuffer[1]; unsigned char data; unsigned char i; i2c_start(0x50, 0); // address in big-endian format i2c_send(readBuffer[3]); // address MSB i2c_send(readBuffer[2]); // address LSB i2c_start(0x50, 1); for (i=0; i<size-1; i++) { writeBuffer[i+2] = i2c_receive(ACK); } writeBuffer[i+2] = i2c_receive(NOACK); i2c_stop(); __delay_ms(10); writeBuffer[0] = 0x12; writeBuffer[1] = size; putUSBUSART(writeBuffer, writeBuffer[1]+2); } break; } } //if (debug_flag) { // debug_flag = 0; // writeBuffer[0] = 9; // writeBuffer[1] = 1; // writeBuffer[2] = debug_data; // putUSBUSART(writeBuffer, writeBuffer[1]+2); //} } CDCTxService(); }
void cdc_write_ok(void) { putrsUSBUSART(msg_ok); }
void cdc_print_help(void) { putrsUSBUSART( msg_help ); }
void cdc_write_log(void) { putrsUSBUSART(msg_log); }
void cdc_write_refat(void) { putrsUSBUSART(msg_refat); }
void cdc_eol(void) { putrsUSBUSART(msg_eol); }