/********************************************************************* * Function: ADC_ReadPercentage(ADC_CHANNEL channel); * * Overview: Reads the requested ADC channel and returns the percentage * of that conversions result (0-100%). * * PreCondition: channel is configured via the ADCConfigure() function * * Input: ADC_CHANNEL channel - enumeration of the ADC channels * available in this demo. They should be meaningful names and * not the names of the ADC pins on the device (as the demo code * may be ported to other boards). * i.e. ADC_ReadPercentage(ADC_CHANNEL_POTENTIOMETER); * * Output: uint8_t indicating the percentage of the result 0-100% or * 0xFF for an error * ********************************************************************/ uint8_t ADC_ReadPercentage ( ADC_CHANNEL channel ) { uint8_t percent ; switch (channel) { case ADC_CHANNEL_5: break ; case ADC_CHANNEL_4: break ; default: return 0xFF ; } //A very crude percentage calculation percent = ( ADC_Read10bit ( channel ) / 10 ) ; if (percent > 100) { percent = 100 ; } return percent ; }
/********************************************************************* * Function: void APP_DeviceCustomHIDTasks(void); * * Overview: Keeps the Custom HID demo running. * * PreCondition: The demo should have been initialized and started via * the APP_DeviceCustomHIDInitialize() and APP_DeviceCustomHIDStart() demos * respectively. * * Input: None * * Output: None * ********************************************************************/ void APP_DeviceCustomHIDTasks() { //Check if we have received an OUT data packet from the host if(HIDRxHandleBusy(USBOutHandle) == false) { //We just received a packet of data from the USB host. //Check the first uint8_t of the packet to see what command the host //application software wants us to fulfill. switch(ReceivedDataBuffer[0]) //Look at the data the host sent, to see what kind of application specific command it sent. { case COMMAND_TOGGLE_LED: //Toggle LEDs command LED_Toggle(LED_USB_DEVICE_HID_CUSTOM); break; case COMMAND_GET_BUTTON_STATUS: //Get push button state //Check to make sure the endpoint/buffer is free before we modify the contents if(!HIDTxHandleBusy(USBInHandle)) { ToSendDataBuffer[0] = 0x81; //Echo back to the host PC the command we are fulfilling in the first uint8_t. In this case, the Get Pushbutton State command. if(BUTTON_IsPressed(BUTTON_USB_DEVICE_HID_CUSTOM) == false) //pushbutton not pressed, pull up resistor on circuit board is pulling the PORT pin high { ToSendDataBuffer[1] = 0x01; } else //sw3 must be == 0, pushbutton is pressed and overpowering the pull up resistor { ToSendDataBuffer[1] = 0x00; } //Prepare the USB module to send the data packet to the host USBInHandle = HIDTxPacket(CUSTOM_DEVICE_HID_EP, (uint8_t*)&ToSendDataBuffer[0],64); } break; case COMMAND_READ_POTENTIOMETER: //Read POT command. Uses ADC to measure an analog voltage on one of the ANxx I/O pins, and returns the result to the host { uint16_t pot; //Check to make sure the endpoint/buffer is free before we modify the contents if(!HIDTxHandleBusy(USBInHandle)) { //Use ADC to read the I/O pin voltage. See the relevant HardwareProfile - xxxxx.h file for the I/O pin that it will measure. //Some demo boards, like the PIC18F87J50 FS USB Plug-In Module board, do not have a potentiometer (when used stand alone). //This function call will still measure the analog voltage on the I/O pin however. To make the demo more interesting, it //is suggested that an external adjustable analog voltage should be applied to this pin. pot = ADC_Read10bit(ADC_CHANNEL_POTENTIOMETER); ToSendDataBuffer[0] = 0x37; //Echo back to the host the command we are fulfilling in the first uint8_t. In this case, the Read POT (analog voltage) command. ToSendDataBuffer[1] = (uint8_t)pot; //LSB ToSendDataBuffer[2] = pot >> 8; //MSB //Prepare the USB module to send the data packet to the host USBInHandle = HIDTxPacket(CUSTOM_DEVICE_HID_EP, (uint8_t*)&ToSendDataBuffer[0],64); } } break; } //Re-arm the OUT endpoint, so we can receive the next OUT data packet //that the host may try to send us. USBOutHandle = HIDRxPacket(CUSTOM_DEVICE_HID_EP, (uint8_t*)&ReceivedDataBuffer, 64); }
char ReadADC_char(void) { char ADC_read; uint16_t ADC_read_int; ADC_read_int = ADC_Read10bit( (char) 5); ADC_read_int = (unsigned int) ((float) ADC_read_int * 0.093) + (int)32; // maps 00,0000,0000 to 010,0000 and 11,1111,1111 to 111,1111 ADC_read = (unsigned char) ADC_read_int; if (ADC_read <0x20) ADC_read = 0x20; if (ADC_read >0x7F) ADC_read = 0x7F; return ADC_read; }
/********************************************************************* * Function: void APP_HostMSDDataLoggerTasks(void); * * Overview: Keeps the demo running. * * PreCondition: The demo should have been initialized via * the APP_HostMSDDataLoggerInitialize() * * Input: None * * Output: None * ********************************************************************/ void APP_HostMSDDataLoggerTasks() { if(FILEIO_MediaDetect(&gUSBDrive, &deviceAddress) == false) { //The device has been removed. Now we should wait for a new // device to be attached. demoState = WAITING_FOR_ATTACH; } switch( demoState) { case WAITING_FOR_ATTACH: break; case MOUNTING_DRIVE: { // Attempt to mount the drive described by gUSBDrive as drive 'A' // The deviceAddress parameter describes the USB address of the device; it is initialized by the application in the // USB_ApplicationEventHandler function when a new device is detected. if( FILEIO_DriveMount ('A', &gUSBDrive, &deviceAddress) == FILEIO_ERROR_NONE) { demoState = OPENING_FILE; } break; } case OPENING_FILE: // Opening a file with the FILEIO_OPEN_WRITE option allows us to write to the file. // Opening a file with the FILEIO_OPEN_CREATE file will create the file if it does not already exist. // Opening a file with the FILEIO_OPEN_TRUNCATE file will truncate it to a 0-length file if it already exists. if(FILEIO_Open(&myFile, "LOG.CSV", FILEIO_OPEN_WRITE | FILEIO_OPEN_CREATE | FILEIO_OPEN_TRUNCATE) == FILEIO_RESULT_SUCCESS) { //Opening the file failed. Since we can't write to the // device, abort the write attempt and wait for the device // to be removed. demoState = WRITING_TO_FILE; blinkCount = 0; sampleRequested = false; TIMER_RequestTick(&APP_HostMSDDataLoggerTickHandler, 50); LED_On(LED_USB_HOST_MSD_DATA_LOGGER); ADC_ChannelEnable(ADC_USB_HOST_MSD_DATA_SOURCE); break; } break; case WRITING_TO_FILE: if(sampleRequested == true) { uint16_t adcResult; int charCount; sampleRequested = false; adcResult = ADC_Read10bit(ADC_USB_HOST_MSD_DATA_SOURCE); charCount = sprintf(printBuffer, "%d\r\n", adcResult); //Write some data to the new file. FILEIO_Write(printBuffer, 1, charCount, &myFile); } if(BUTTON_IsPressed(BUTTON_USB_HOST_MSD_DATA_LOGGER) == true) { demoState = CLOSING_FILE; } break; case CLOSING_FILE: //Always make sure to close the file so that the data gets // written to the drive. FILEIO_Close(&myFile); TIMER_CancelTick(&APP_HostMSDDataLoggerTickHandler); demoState = UNMOUNT_DRIVE; break; case UNMOUNT_DRIVE: // Unmount the drive since it is no longer in use. FILEIO_DriveUnmount ('A'); //Now that we are done writing, we can do nothing until the // drive is removed. demoState = WAITING_FOR_DETACH; break; case WAITING_FOR_DETACH: LED_Off(LED_USB_HOST_MSD_DATA_LOGGER); break; default: break; } }
/********************************************************************* * 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() { uint8_t numBytesWrite = 0; uint8_t packet[MAX_PACKET_SIZE]; uint8_t packetSize; packet_data_u data; /* Make sure that the CDC driver is ready for a transmission. */ if (mUSBUSARTIsTxTrfReady() == true) { uint16_t value; uint32_t percent; button_state_s cur_state[3]; memset(cur_state, 0x00, 3 * sizeof(button_state_s)); bool button1IsPressed = BUTTON_IsPressed(BUTTON_S1); bool button2IsPressed = BUTTON_IsPressed(BUTTON_S2); bool button3IsPressed = BUTTON_IsPressed(BUTTON_S3); value = ADC_Read10bit(ADC_CHANNEL_1); percent = ((uint32_t)100 * value) / 0x03FF; cur_state[0].pos = 0; cur_state[0].state = button1IsPressed ? BUTTON_PRESSED : BUTTON_RELEASED; cur_state[0].state |= ((percent >= 95) ? BUTTON_UNMOUNTED : BUTTON_MOUNTED); cur_state[0].uid = percent; value = ADC_Read10bit(ADC_CHANNEL_2); percent = ((uint32_t)100 * value) / 0x03FF; cur_state[1].pos = 1; cur_state[1].state = button2IsPressed ? BUTTON_PRESSED : BUTTON_RELEASED; cur_state[1].state |= ((percent >= 95) ? BUTTON_UNMOUNTED : BUTTON_MOUNTED); cur_state[1].uid = percent; value = ADC_Read10bit(ADC_CHANNEL_3); percent = ((uint32_t)100 * value) / 0x03FF; cur_state[2].pos = 2; cur_state[2].state = button3IsPressed ? BUTTON_PRESSED : BUTTON_RELEASED; cur_state[2].state |= ((percent >= 95) ? BUTTON_UNMOUNTED : BUTTON_MOUNTED); cur_state[2].uid = percent; if ((buttons_state[0].state != cur_state[0].state) || (buttons_state[1].state != cur_state[1].state) || (buttons_state[2].state != cur_state[2].state)) { memset(&data, 0x00, sizeof(packet_data_u)); memcpy(data.buttons_state, cur_state, 3 * sizeof(button_state_s)); assemblyPacket(GET_BUTTONS_STATE, &data, packet, &packetSize); memcpy(writeBuffer, packet, packetSize); numBytesWrite = packetSize; if (numBytesWrite > 0) { putUSBUSART(writeBuffer, numBytesWrite); } memcpy(buttons_state, cur_state, 3 * sizeof(button_state_s)); } } /* 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) { uint16_t i; uint16_t validDataLen; uint16_t numBytesRead; char deviceID[] = "Keys 011"; char *e; packet_type_e type; uint8_t payload[MAX_DATA_SIZE]; uint8_t payloadSize; // Collect incoming data in read buffer. Bear in mind, buffer have to had ability receive whole CDC packet. if ((readPos + CDC_DATA_OUT_EP_SIZE) >= sizeof(readBuffer)) { e = memchr(readBuffer, PREAMBLE, sizeof(readBuffer)); // Wipe out trash before valid data if (e == 0) { memset(readBuffer, 0x00, sizeof(readBuffer)); readPos = 0; } else { validDataLen = (uint16_t)(&readBuffer[sizeof(readBuffer)] - e); memmove(readBuffer, e, validDataLen); memset(&readBuffer[validDataLen], 0x00, sizeof(readBuffer) - validDataLen); readPos = 0; } } numBytesRead = getsUSBUSART(&readBuffer[readPos], CDC_DATA_OUT_EP_SIZE); if (numBytesRead > 0) { // Parse all collected incoming data do { e = memchr(readBuffer, PREAMBLE, sizeof(readBuffer)); if (e == 0) { break; } i = (uint16_t)(e - readBuffer); parsePacket(&readBuffer[i], sizeof(readBuffer) - i, &type, payload, &payloadSize); numBytesWrite = 0; if (type == GET_DEVICE_ID) { memset(&data, 0x00, sizeof(packet_data_u)); memcpy(data.device_id, deviceID, 8); assemblyPacket(GET_DEVICE_ID, &data, packet, &packetSize); memcpy(writeBuffer, packet, packetSize); numBytesWrite = packetSize; } else if (type == GET_STATUS) { memset(&data, 0x00, sizeof(packet_data_u)); data.device_status.errors = ERROR_NONE; data.device_status.rtc = 0; assemblyPacket(GET_STATUS, &data, packet, &packetSize); memcpy(writeBuffer, packet, packetSize); numBytesWrite = packetSize; } else if (type == SET_LEDS_STATE) { led_state_s *led = (led_state_s *)payload; if (led->state == LED_TURN_ON) { if (led->pos == 0) LED_On(LED_D2); if (led->pos == 1) LED_On(LED_D3); if (led->pos == 2) LED_On(LED_D4); } else { if (led->pos == 0) LED_Off(LED_D2); if (led->pos == 1) LED_Off(LED_D3); if (led->pos == 2) LED_Off(LED_D4); } } else if (type == RESET_DEVICE) { LED_Off(LED_D2); LED_Off(LED_D3); LED_Off(LED_D4); memset(readBuffer, 0x00, sizeof(readBuffer)); readPos = 0; } else { memcpy(writeBuffer, &readBuffer[i], 20); numBytesWrite = 20; } // Clear packet's start marker - PREAMBLE memset(&readBuffer[i], 0x00, 1); // Send answer if (numBytesWrite > 0) { putUSBUSART(writeBuffer, numBytesWrite); } } while (1); } } CDCTxService(); }