/********************************************************************* * 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); }
void usb_tasks() { // User Application USB tasks if (USBDeviceState < CONFIGURED_STATE || USBSuspendControl == 1) return; // Check if we have received an OUT data packet from the host, and nothing is left in buffer if (!HIDRxHandleBusy(USBOutHandleCfg) && !HIDTxHandleBusy(USBInHandleCfg)) { // We just received a packet of data from the USB host. // Check the first byte of the packet to see what command the host // application software wants us to fulfill. uint8_t cmd = usbOutBuffer[0]; if (cmd == CFG_CMD_READ) { // format response cfg_read_report_t* report = (cfg_read_report_t*)usbInBuffer; report->command_id = CFG_CMD_READ; report->fw_major = FW_MAJOR; report->fw_minor = FW_MINOR; report->hw_revision = HW_REVISION; report->device_type = MUNIA_DEVICETYPE; uint8_t* r = &report->devid1; TBLPTR = 0x3FFFFF; asm("tblrd*-"); *r++ = TABLAT; asm("tblrd*"); *r = TABLAT; TBLPTRU = 0; memcpy(&report->config, &config, sizeof(config_t)); memset(usbInBuffer + sizeof(cfg_read_report_t), 0, sizeof(usbInBuffer) - sizeof(cfg_read_report_t)); USBInHandleCfg = HIDTxPacket(HID_EP_CFG, usbInBuffer, CFG_CMD_REPORT_SIZE); } else if (cmd == CFG_CMD_WRITE) { // extract config cfg_write_report_t* report = (cfg_write_report_t*)usbOutBuffer; memcpy(&config, &report->config, sizeof(config_t)); save_config(); apply_config(); // response usbInBuffer[0] = CFG_CMD_WRITE; usbInBuffer[1] = 1; // signifies ok memset(usbInBuffer + 2, 0, sizeof(usbInBuffer) - 2); USBInHandleCfg = HIDTxPacket(HID_EP_CFG, usbInBuffer, CFG_CMD_REPORT_SIZE); } else if (cmd == CFG_CMD_ENTER_BL) { // dbgs("jumping to bootloader\n"); asm("goto 0x001C"); } // re-arm USBOutHandleCfg = HIDRxPacket(HID_EP_CFG, usbOutBuffer, sizeof (usbOutBuffer)); } }
/********************************************************************* * 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. if (ReceivedDataBuffer[0] == COMMAND_TOGGLE_LED) //Look at the data the host sent, to see what kind of application specific command it sent. { //Toggle LEDs command LED_Toggle(LED_GREEN); } else { if (!check_command()) { // strcpypgm2ram((char *) ToSendDataBuffer, (farchar *) "null"); *ToSendDataBuffer = 0xFF; } if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(CUSTOM_DEVICE_HID_EP, (uint8_t*) & ToSendDataBuffer[0], 64); } } //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); } }
void Keyboard(void) { static char* c = g_tx_queue; if (SwitchIsPressed()) { g_tx_queue[0] = 0; if (g_config_mode) { ++g_lgtm_strings_idx; if (g_lgtm_strings_idx >= NUM_LGTM_STRINGS) g_lgtm_strings_idx = 0; EEADR = 0; EEDATA = g_lgtm_strings_idx; EECON1bits.EEPGD = 0; EECON1bits.CFGS = 0; EECON1bits.WREN = 1; EECON2 = 0x55; EECON2 = 0xAA; EECON1bits.WR = 1; strcatpgm2ram(g_tx_queue, CFG_MODE_STR); } strcatpgm2ram(g_tx_queue, LGTM_STRINGS[g_lgtm_strings_idx]); c = g_tx_queue; } // Check if the IN endpoint is not busy, and if it isn't check if we want to // send keystroke data to the host. if (!HIDTxHandleBusy(g_usb_handle_in)) { memset(hid_report_in, 0, sizeof(hid_report_in)); if (*c) { HIDKey key; key = AsciiToHid(*c); ++c; hid_report_in[0] = key.modifier; hid_report_in[2] = key.code; } // Send the 8 byte packet over USB to the host. g_usb_handle_in = HIDTxPacket(HID_EP, (BYTE*)hid_report_in, 0x08); } // Check if any data was sent from the PC to the keyboard device. Report // descriptor allows // host to send 1 byte of data. Bits 0-4 are LED states, bits 5-7 are unused // pad bits. // The host can potentially send this OUT report data through the HID OUT // endpoint (EP1 OUT), // or, alternatively, the host may try to send LED state information by // sending a // SET_REPORT control transfer on EP0. See the USBHIDCBSetReportHandler() // function. if (!HIDRxHandleBusy(g_usb_handle_out)) { // Do something useful with the data now. Data is in the OutBuffer[0]. // Num Lock LED state is in Bit0. // if (hid_report_out[0] & 0x01) // Make LED1 and LED2 match Num Lock // state. g_usb_handle_out = HIDRxPacket(HID_EP, (BYTE*)&hid_report_out, 1); } return; }
void ngc_tasks() { if (pollNeeded && (in_menu || (config.input_ngc && config.output_mode != output_ngc))) { USBDeviceTasks(); di(); ngc_poll(); // waste some more instructions before sampling _delay(40); asm("lfsr 0, _sample_buff+25"); // setup FSR0 ngc_sample(); asm("movff FSR0L, _sample_w+0"); // update sample_w } if (packets.ngc_test) { if (!in_menu && config.output_mode == output_ngc && !config.input_ngc) { ngc_fakeout_test(); WRITETIMER3(65000); // schedule next fake poll soon } else ngc_handle_packet(); packets.ngc_test = false; } INTCONbits.IOCIF = 0; // don't bother with stuff that happened in the meantime ei(); if (packets.ngc_avail) { // see if this packet is equal to the last transmitted one, and if so, discard it // also when in menu, menu_tasks will clear bit if (in_menu) return; else if (memcmp(&joydata_ngc_raw, &joydata_ngc_last_raw, sizeof(ngc_packet_t))) { // dbgs("new packets.ngc_avail\n"); // new, changed packet available; unpack if faking and send over usb if (config.input_sources & input_ngc && config.output_mode == output_n64) { // dbgs("ngc_create_n64_fake()\n"); ngc_to_n64(); fake_unpack((uint8_t*)&joydata_n64_raw, sizeof(n64_packet_t)); } else if (config.input_sources & input_ngc && config.output_mode == output_snes) { // dbgs("ngc_create_snes_fake()\n"); ngc_to_snes(); fake_unpack((uint8_t*)&joydata_snes_raw, sizeof(snes_packet_t)); } if (USB_READY && !HIDTxHandleBusy(USBInHandleNGC)) { // dbgs("ngc_joydata_createhid()\n"); ngc_joydata_createhid(); USBInHandleNGC = HIDTxPacket(HID_EP_NGC, (uint8_t*)&joydata_ngc_usb, sizeof(ngc_packet_t)); } // save last packet memcpy(&joydata_ngc_last_raw, &joydata_ngc_raw, sizeof(ngc_packet_t)); } packets.ngc_avail = false; // now consumed } }
void Keyboard(void) { static unsigned char state = 0; //declare a state variable for the keyboard state machine static unsigned long delaycounter = 0; static unsigned char HIDoutput; //will store an altered version of the ADRESH result //State machine switch (state) { //delay to allow the hold capacitor on the ADC to charge case 0: if(++delaycounter>9000) { delaycounter = 0; state = 1; } break; case 1: ADCON0bits.GO_DONE = 1; //Start an ADC conversion state = 2; break; case 2: if(ADCON0bits.GO_DONE == 0) //Check if conversion is //completed { state = 3; } break; case 3: HIDoutput = ADRESH>>3;//shift the result in ADRESH //left by three if(HIDoutput<=4) HIDoutput = 4; if(HIDoutput>=29) HIDoutput = 29; //Can the SIE transmit? if((HIDTxHandleBusy(lastTransmission) == 0)) { //Load the HID buffer hid_report_in[0] = 0; hid_report_in[1] = 0; hid_report_in[2] = HIDoutput; hid_report_in[3] = 0; hid_report_in[4] = 0; hid_report_in[5] = 0; hid_report_in[6] = 0; hid_report_in[7] = 0; //Send the 8 byte packet over USB to the host. lastTransmission = HIDTxPacket(HID_EP, (BYTE*)hid_report_in, 0x08); state = 0; } break; default: state = 0; break; } }//end keyboard()
void Keyboard(void) { unsigned char i = 0; if(!HIDTxHandleBusy(lastINTransmission)) { hid_report_in[0] = 0; hid_report_in[1] = 0; hid_report_in[2] = 0; hid_report_in[3] = 0; hid_report_in[4] = 0; hid_report_in[5] = 0; hid_report_in[6] = 0; hid_report_in[7] = 0; if(dynamicFlags > 0) { hid_report_in[0] = dynamicFlags; } else { hid_report_in[0] = 0; } for(i = 0; i < reportIndex; i++) { hid_report_in[i + 2] = report[i]; } /* if(!PORTAbits.RA0) hid_report_in[2] = 0x28; //[ENTER] else hid_report_in[2] = 0; */ //Send the 8 byte packet over USB to the host. lastINTransmission = HIDTxPacket(HID_EP, (BYTE*)hid_report_in, 0x08); } if(!HIDRxHandleBusy(lastOUTTransmission)) { /* if(hid_report_out[0] & 0x02) //[CAPS]lock時に点灯 LATBbits.LATB4 = 1; else LATBbits.LATB4 = 0; if(hid_report_out[0] & 0x01) //[NUM]lock時に点灯 LATBbits.LATB5 = 1; else LATBbits.LATB5 = 0; */ lastOUTTransmission = HIDRxPacket(HID_EP,(BYTE*)&hid_report_out,1); } return; }//end keyboard()
/* * This routine will poll for a received Input report, process it * and send an Output report to the host. * Both directions use interrupt transfers. * The ownership of the USB buffers will change according * to the required operation. */ void send_receive (void) { static int usb_state = 'r'; static USB_HANDLE last_transmit = 0; static USB_HANDLE last_receive = 0; switch (usb_state) { case 'r': if (! HIDRxHandleBusy (last_receive)) { // The CPU owns the endpoint. Start receiving data. last_receive = HIDRxPacket (HID_EP, (unsigned char*) &hid_report_out, HID_INT_OUT_EP_SIZE); usb_state = 'p'; } break; case 'p': if (! HIDRxHandleBusy (last_receive)) { // The CPU owns the endpoint. if (last_receive->CNT > 0) { // Data was received. Copy it to the output buffer for sending. process_data(); // Ready to transmit the received data back to the host. usb_state = 't'; } else { // No data was received. Return to checking for new received data. usb_state = 'r'; } } break; case 't': if (! HIDTxHandleBusy (last_transmit)) { // The CPU owns the endpoint. Start sending data. last_transmit = HIDTxPacket (HID_EP, (unsigned char*) &hid_report_in, HID_INPUT_REPORT_BYTES); // Return to checking for new received data. usb_state = 'r'; } break; default: // Cannot happen. break; } }
/******************************************************************** * 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) { // User Application USB tasks if((USBDeviceState < CONFIGURED_STATE) || (USBSuspendControl == 1)) { return; } // Indicate USB is connected. xSystemState.bUsbConnected = 1; // If data was received from the host... if(!HIDRxHandleBusy(USBOutHandle)) { // Make sure the transmitter is not busy. if (!HIDTxHandleBusy(USBInHandle)) { // Process the data. unsigned char ucTransmitDataLength = ucProcessCommandPacket(ReceivedDataBuffer, ToSendDataBuffer); // Send the data if there is any. if (ucTransmitDataLength > 0) { // Fill the remaining bytes with 0xff if it's not full. unsigned char i; for (i = ucTransmitDataLength; i < 64; i++) { ToSendDataBuffer[i] = 0xff; } // Transmit the data. USBInHandle = HIDTxPacket(HID_EP, ToSendDataBuffer, 64); } // Re-arm the OUT endpoint to receive next packet if there is no data to send. else { USBOutHandle = HIDRxPacket(HID_EP, ReceivedDataBuffer, 64); } } } MSDTasks(); }
/****************************************************************************** * Function: void Emulate_Mouse(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: The ownership of the USB buffers will change according * to the required operation * * Overview: This routine will emulate the function of the mouse. It * does this by sending IN packets of data to the host. * * The generic function HIDTxPacket() is used to send HID * IN packets over USB to the host. * * Note: * *****************************************************************************/ void Emulate_Mouse(void) { if(emulate_mode == TRUE) { //go 14 times in the same direction before changing direction if(movement_length > 14) { buffer[0] = 0; buffer[1] = dir_table[vector & 0x07]; // X-Vector buffer[2] = dir_table[(vector+2) & 0x07]; // Y-Vector //go to the next direction in the table vector++; //reset the counter for when to change again movement_length = 0; }//end if(movement_length > 14) } else { //don't move the mouse buffer[0] = buffer[1] = buffer[2] = 0; } if(HIDTxHandleBusy(lastTransmission) == 0) { //copy over the data to the HID buffer hid_report_in[0] = buffer[0]; hid_report_in[1] = buffer[1]; hid_report_in[2] = buffer[2]; //Send the 3 byte packet over USB to the host. lastTransmission = HIDTxPacket(HID_EP, (BYTE*)hid_report_in, 0x03); //increment the counter of when to change the data sent movement_length++; } }//end Emulate_Mouse
void Usb_TxBufferedPacket( void ) { usb_inHandle = HIDTxPacket( HID_EP, (BYTE *)&usb_txBuffer[0], USB_TXBUFFER_SIZE ); }
// Process USB commands void processUsbCommands(void) { // Check if we are in the configured state; otherwise just return if((USBDeviceState < CONFIGURED_STATE) || (USBSuspendControl == 1)) { // We are not configured return; } // Check if data was received from the host. if (!HIDRxHandleBusy(USBOutHandle)) { // Command mode switch (ReceivedDataBuffer[0]) { case 0x01: // System Commands switch (ReceivedDataBuffer[1]) { case 0x01: // System Commands // Copy any waiting debug text to the send data buffer ToSendDataBuffer[0] = 0xFF; // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; default: // Unknown command received break; } break; case 0x02: // Feeder Commands switch (ReceivedDataBuffer[1]) { case 0x02: // Feeder Status if (atmegaFeederRunning){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; case 0x03: // Go to feeder StartI2C(); WriteI2C(0x28); // sends address to the device IdleI2C(); WriteI2C(ReceivedDataBuffer[2]); // sends a control byte to the device IdleI2C(); StopI2C(); break; case 0x04: // Reset Feeder Z StartI2C(); WriteI2C(0x28); // sends address to the device IdleI2C(); WriteI2C(80); // sends a control byte to the device IdleI2C(); StopI2C(); break; case 0x05: // Full feeder reset StartI2C(); WriteI2C(0x28); // sends address to the device IdleI2C(); WriteI2C(70); // sends a control byte to the device IdleI2C(); StopI2C(); break; case 0x06: // Reset ATMEGA IC atmegaResetPin = 0; Delay1KTCYx(10); atmegaResetPin = 1; break; default: // Unknown command received break; } break; case 0x03: // Vacuum and Vibration Commands switch (ReceivedDataBuffer[1]) { case 0x01: // Vacuum 1 set if (ReceivedDataBuffer[2] == 0x01){ setVac1on; } else{ setVac1off; } break; case 0x02: // Vacuum 2 set if (ReceivedDataBuffer[2] == 0x01){ setVac2on; } else{ setVac2off; } break; case 0x03: // Vibration Motor set if (ReceivedDataBuffer[2] == 0x01){ setVibrationon; StartI2C(); WriteI2C(0x16); // sends address to the device IdleI2C(); WriteI2C(0x01); // sends a control byte to the device IdleI2C(); StopI2C(); } else{ setVibrationoff; StartI2C(); WriteI2C(0x16); // sends address to the device IdleI2C(); WriteI2C(0x02); // sends a control byte to the device IdleI2C(); StopI2C(); } break; case 0x04: // Vacuum 1 status if (vac1running == 1){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; case 0x05: // Vacuum 2 status if (vac2running == 1){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; case 0x06: // Vibration Motor status if (vibrationrunning == 1){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; case 0x07: // Vibration Motor status vibrationmotor_duty_cycle = ReceivedDataBuffer[2]; StartI2C(); WriteI2C(0x16); // sends address to the device IdleI2C(); WriteI2C(0x03); // sends a control byte to the device IdleI2C(); WriteI2C(vibrationmotor_duty_cycle); // sends a control byte to the device IdleI2C(); StopI2C(); break; default: // Unknown command received break; } break; case 0x04: // LED Commands switch (ReceivedDataBuffer[1]) { case 0x01: // LED Base Camera on/off if (ReceivedDataBuffer[2] == 0x01){ OpenPWM1(0xFF); led1_duty_cycle = led1_duty_cycle * 4; SetDCPWM1(led1_duty_cycle); // outBaseLED = 1; led1running = 1; }else{ ClosePWM1(); // outBaseLED = 0; led1running = 0; } break; case 0x02: // LED Base Camera PWM set led1_duty_cycle = ReceivedDataBuffer[2]; Write_b_eep(baseLED_EEPROM_address, led1_duty_cycle); led1_duty_cycle = led1_duty_cycle * 4; SetDCPWM1(led1_duty_cycle); break; case 0x03: // LED Head Camera on/off if (ReceivedDataBuffer[2] == 0x01){ OpenPWM2(0xFF); led2_duty_cycle = led2_duty_cycle * 4; SetDCPWM2(led2_duty_cycle); // outBaseLED = 1; led2running = 1; }else{ ClosePWM2(); // outBaseLED = 0; led2running = 0; } break; case 0x04: // LED Head Camera PWM set led2_duty_cycle = ReceivedDataBuffer[2]; Write_b_eep(headLED_EEPROM_address, led2_duty_cycle); led2_duty_cycle = led2_duty_cycle * 4; SetDCPWM2(led2_duty_cycle); break; case 0x05: // LED Base Status if (led1running == 1){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; case 0x06: // LED Head Status if (led2running == 1){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; default: // Unknown command received break; } break; default: // Unknown command received break; } // Re-arm the OUT endpoint for the next packet USBOutHandle = HIDRxPacket(HID_EP, (BYTE*) & ReceivedDataBuffer, 64); } }
// Process USB commands void processUsbCommands(void) { // Check if we are in the configured state; otherwise just return if((USBDeviceState < CONFIGURED_STATE) || (USBSuspendControl == 1)) { // We are not configured return; } // If the last transmision is complete send the joystick status if(!HIDTxHandleBusy(lastTransmission_j1)) { char XAxis, YAxis; int XNew, YNew; float cX, cY; unsigned char hat; //PollController(); //if(controller_data_error[0]) // return; // Set everything to the defult joystick_input_j1.members.buttons.B1 = 0; joystick_input_j1.members.buttons.B2 = 0; joystick_input_j1.members.buttons.B3 = 0; joystick_input_j1.members.buttons.B4 = 0; joystick_input_j1.members.buttons.B5 = 0; joystick_input_j1.members.buttons.B6 = 0; joystick_input_j1.members.buttons.B7 = 0; joystick_input_j1.members.buttons.B8 = 0; joystick_input_j1.members.buttons.B9 = 0; joystick_input_j1.members.buttons.B10 = 0; joystick_input_j1.members.buttons.B11 = 0; joystick_input_j1.members.buttons.B12 = 0; joystick_input_j1.members.buttons.B13 = 0; joystick_input_j1.members.hat_switch.hat_switch = HAT_SWITCH_NULL; // 128 is 'middle' joystick_input_j1.members.analog_stick.X = 128; joystick_input_j1.members.analog_stick.Y = 128; joystick_input_j1.members.analog_stick.Z = 128; joystick_input_j1.members.analog_stick.Rz = 128; // Now adjust according to the joystick switch states if (controller_data[0]) joystick_input_j1.members.buttons.B1 = 1; if (controller_data[1]) joystick_input_j1.members.buttons.B2 = 1; if (controller_data[2]) joystick_input_j1.members.buttons.B3 = 1; if (controller_data[3]) joystick_input_j1.members.buttons.B4 = 1; if (controller_data[4]) joystick_input_j1.members.buttons.B5 = 1; if (controller_data[5]) joystick_input_j1.members.buttons.B6 = 1; if (controller_data[6]) joystick_input_j1.members.buttons.B7 = 1; if (controller_data[7]) joystick_input_j1.members.buttons.B8 = 1; if (controller_data[10]) joystick_input_j1.members.buttons.B9 = 1; if (controller_data[11]) joystick_input_j1.members.buttons.B10 = 1; cY = cX = 0; if(controller_data[12]) cY -= 2; if(controller_data[13]) cY += 2; if(controller_data[14]) cX -= 2; if(controller_data[15]) cX += 2; if(cY != 0 || cX != 0) { if(cY < 0) hat = round(((2*PI)-acos(cX/sqrt(cX*cX+cY*cY)))*(4/PI))+2; else hat = round(acos(cX/sqrt(cX*cX+cY*cY))*(4/PI))+2; joystick_input_j1.members.hat_switch.hat_switch = hat%8; } XAxis = controller_data[16] & 0x80 | controller_data[17] & 0x40 | controller_data[18] & 0x20 | controller_data[19] & 0x10 | controller_data[20] & 0x08 | controller_data[21] & 0x04 | controller_data[22] & 0x02 | controller_data[23] & 0x01; YAxis = controller_data[24] & 0x80 | controller_data[25] & 0x40 | controller_data[26] & 0x20 | controller_data[27] & 0x10 | controller_data[28] & 0x08 | controller_data[29] & 0x04 | controller_data[30] & 0x02 | controller_data[31] & 0x01; XNew = (int)127+(float)XAxis*1.0; YNew = (int)127-(float)YAxis*1.0; if(XNew > 255) XNew = 255; else if(XNew < 0) XNew = 0; if(YNew > 255) YNew = 255; else if(YNew < 0) YNew = 0; joystick_input_j1.members.analog_stick.X = (unsigned char)XNew; joystick_input_j1.members.analog_stick.Y = (unsigned char)YNew; //Send the packet over USB to the host. lastTransmission_j1 = HIDTxPacket(HID_EP, (BYTE*)&joystick_input_j1, sizeof(joystick_input_j1)); } // Check if data was received from the host. /* if(!HIDRxHandleBusy(USBOutHandle)) { // Command mode switch(ReceivedDataBuffer[0]) { case 0x10: // Debug information request from host // Copy any waiting debug text to the send data buffer copyDebugToSendBuffer((BYTE*)&ToSendDataBuffer[0]); // Transmit the response to the host if(!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64); } break; // Place application specific commands here: case 0x80: // Toggle the LED { int i = 0; //sprintf(debugString, "Received command 0x80 from host - Toggle LED"); PollController(); LATBbits.LATB3 = 0; for(i = 0; i < 32; i++) { if(controller_data[i] > 0) { LATBbits.LATB3 = 1; sprintf(debugString, "%i %x",i,controller_data[i]); debugOut(debugString); } } sprintf(debugString, "--------------",controller_data[i],i); debugOut(debugString); // Toggle the LED0 mStatusLED0_Toggle(); break; } case 0x81: // Read the push switch status ToSendDataBuffer[0] = sw0; // Transmit the response to the host if(!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64); } break; case 0x82: // Read the LED status // Get the LED state and put it in the send buffer ToSendDataBuffer[0] = mStatusLED0_Get(); // Transmit the response to the host if(!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64); } break; case 0x83: // Read the N64 status { int i; PollController(); ToSendDataBuffer[0] = 0; ToSendDataBuffer[1] = 0; ToSendDataBuffer[2] = 0; ToSendDataBuffer[3] = 0; for(i = 0; i < 32; i++) { if(controller_data[i]) ToSendDataBuffer[i/8] |= 0x01 << (i%8); } // Transmit the response to the host if(!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64); } break; } default: // Unknown command received break; } // Re-arm the OUT endpoint for the next packet USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&ReceivedDataBuffer,64); }*/ }
/******************************************************************** * 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) { if(DemoIntroState == 0xFF) { BL_CheckLoaderEnabled(); } // User Application USB tasks if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return; // Soft Start the APP_VDD if(AppPowerReady() == FALSE) return; DemoIntroduction(); if(!HIDRxHandleBusy(USBOutHandle)) //Check if data was received from the host. { switch(ReceivedDataBuffer[0]) //Look at the data the host sent, to see what kind of application specific command it sent. { case 0x80: //mTouch callibration command mTouchCalibrate(); break; case 0x20: { WORD potVoltage; if(!HIDTxHandleBusy(USBInHandle)) { /* Select ADC channel */ ADCON0bits.CHS = 4; /* Make sure A/D interrupt is not set */ PIR1bits.ADIF = 0; /* Begin A/D conversion */ ADCON0bits.GO=1; //Wait for A/D convert complete while(!PIR1bits.ADIF); /* Get the value from the A/D */ potVoltage = ADRES; ToSendDataBuffer[0] = 0x20; ToSendDataBuffer[1] = (BYTE)(potVoltage); ToSendDataBuffer[2] = (BYTE)(potVoltage>>8); USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64); } } break; case 0x30: { WORD w; if(!HIDTxHandleBusy(USBInHandle)) { w = mTouchReadButton(0); ToSendDataBuffer[0] = 0x30; ToSendDataBuffer[1] = (BYTE)w; ToSendDataBuffer[2] = (BYTE)(w>>8); USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64); } } break; case 0x31: { WORD w; if(!HIDTxHandleBusy(USBInHandle)) { w = mTouchReadButton(1); ToSendDataBuffer[0] = 0x31; ToSendDataBuffer[1] = (BYTE)w; ToSendDataBuffer[2] = (BYTE)(w>>8); USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64); } }
void USBWrite() { USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64); }
void USBUpdate() { int a,b; // User Application USB tasks if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return; if(!HIDRxHandleBusy(USBOutHandle)) //Check if data was received from the host. { switch(ReceivedDataBuffer[0]) //Look at the data the host sent, to see what kind of application specific command it sent. { case 0x01: a=EPPoll(*(int16*)&ReceivedDataBuffer[1]); if(!USBBusy()) { ToSendDataBuffer[0]=0x03; if(a!=0) ToSendDataBuffer[1]=0; else ToSendDataBuffer[1]=255; USBWrite(); } break; case 0x02: EPBufferSize=ReceivedDataBuffer[3]; for(a=0;a<EPBufferSize;a++)Set8(a,ReceivedDataBuffer[a+4]); EPSend(*(int16*)&ReceivedDataBuffer[1]); if(!USBBusy()) { ToSendDataBuffer[0]=0x02; USBWrite(); } break; case 0x06: ToSendDataBuffer[1]=KismetExecuteEvent(((int16*)&ReceivedDataBuffer[1])[0],ReceivedDataBuffer[3]); if(!USBBusy()) { ToSendDataBuffer[0]=0x06; USBWrite(); } break; case 0x07: OperationEnabled=0; if(!USBBusy()) { ToSendDataBuffer[0]=0x07; USBWrite(); } break; case 0x08: OperationEnabled=0xff; if(!USBBusy()) { ToSendDataBuffer[0]=0x08; USBWrite(); } break; case 0x03://EEPROM Write MemoryBeginWrite(((int16*)&ReceivedDataBuffer[1])[0]); MemoryWrite(ReceivedDataBuffer[3]); MemoryEndWrite(); if(!USBBusy()) { ToSendDataBuffer[0]=0x03; USBWrite(); } break; case 0x04://EEPROM WRITE PAGE MemoryBeginWrite(((int16*)&ReceivedDataBuffer[1])[0]); for(a=0;a<32;a++) MemoryWrite(ReceivedDataBuffer[3+a]); MemoryEndWrite(); if(!USBBusy()) { ToSendDataBuffer[0]=0x04; USBWrite(); } break; case 0x05://EEPROM READ if(!USBBusy()) { MemoryBeginRead(((int16*)&ReceivedDataBuffer[1])[0]); ToSendDataBuffer[0]=0x05; ToSendDataBuffer[1]=MemoryReadInt8(); MemoryEndRead(); USBWrite(); } break; case 0x40://READ TIME if(!USBBusy()) { ToSendDataBuffer[0]=0x40; ToSendDataBuffer[1]=RTCSecond; ToSendDataBuffer[2]=RTCMinute; ToSendDataBuffer[3]=RTCHour; INT16(ToSendDataBuffer[4])=RTCDay; USBWrite(); } break; case 0x41://WRITE TIME RTCSecond =ReceivedDataBuffer[1]; RTCMinute =ReceivedDataBuffer[2]; RTCHour =ReceivedDataBuffer[3]; RTCDay =INT16(ReceivedDataBuffer[4]); if(!USBBusy()) { ToSendDataBuffer[0]=0x41; USBWrite(); } break; case 0x50://SET TIMER SetTimer(ReceivedDataBuffer[1],ReceivedDataBuffer[2],INT16(ReceivedDataBuffer[3])); if(!USBBusy()) { ToSendDataBuffer[0]=0x50; USBWrite(); } break; } if(!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64); } ReceivedDataBuffer[0]=0; //Re-arm the OUT endpoint for the next packet USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&ReceivedDataBuffer,64); } }
/** * ProcessIO: **/ static void ProcessIO(void) { uint16_t address; uint16_t erase_length; uint8_t length; uint8_t checksum; uint8_t cmd; uint8_t rc = CH_ERROR_NONE; /* reset the LED state */ led_counter--; if (led_counter == 0) { CHugSetLEDsInternal(led_color); led_color *= 2; if (led_color > CH_STATUS_LED_BLUE) led_color = CH_STATUS_LED_GREEN; led_counter = BOOTLOADER_FLASH_INTERVAL; } /* User Application USB tasks */ if ((USBDeviceState < CONFIGURED_STATE) || (USBSuspendControl == 1)) return; /* no data was received */ if(HIDRxHandleBusy(USBOutHandle)) { if (idle_counter++ == 0xff && idle_command != 0x00) CHugDeviceIdle(); return; } /* got data, reset idle counter */ idle_counter = 0; /* clear for debugging */ memset (TxBuffer, 0xff, sizeof (TxBuffer)); cmd = RxBuffer[CH_BUFFER_INPUT_CMD]; switch(cmd) { case CH_CMD_GET_HARDWARE_VERSION: TxBuffer[CH_BUFFER_OUTPUT_DATA] = PORTB & 0x0f; break; case CH_CMD_RESET: /* only reset when USB stack is not busy */ idle_command = CH_CMD_RESET; break; case CH_CMD_GET_FIRMWARE_VERSION: memcpy (&TxBuffer[CH_BUFFER_OUTPUT_DATA], &FirmwareVersion, 2 * 3); break; case CH_CMD_ERASE_FLASH: /* are we lost or stolen */ if (flash_success == 0xff) { rc = CH_ERROR_DEVICE_DEACTIVATED; break; } memcpy (&address, (const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+0], 2); /* allow to erase any address but not the bootloader */ if (address < CH_EEPROM_ADDR_RUNCODE) { rc = CH_ERROR_INVALID_ADDRESS; break; } memcpy (&erase_length, (const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+2], 2); EraseFlash(address, address + erase_length); break; case CH_CMD_READ_FLASH: /* are we lost or stolen */ if (flash_success == 0xff) { rc = CH_ERROR_DEVICE_DEACTIVATED; break; } /* allow to read any address */ memcpy (&address, (const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+0], 2); length = RxBuffer[CH_BUFFER_INPUT_DATA+2]; if (length > 60) { rc = CH_ERROR_INVALID_LENGTH; break; } ReadFlash(address, length, (unsigned char *) &TxBuffer[CH_BUFFER_OUTPUT_DATA+1]); checksum = CHugCalculateChecksum (&TxBuffer[CH_BUFFER_OUTPUT_DATA+1], length); TxBuffer[CH_BUFFER_OUTPUT_DATA+0] = checksum; break; case CH_CMD_WRITE_FLASH: /* are we lost or stolen */ if (flash_success == 0xff) { rc = CH_ERROR_DEVICE_DEACTIVATED; break; } /* write to flash that's not the bootloader */ memcpy (&address, (const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+0], 2); if (address < CH_EEPROM_ADDR_RUNCODE) { rc = CH_ERROR_INVALID_ADDRESS; break; } length = RxBuffer[CH_BUFFER_INPUT_DATA+2]; if (length > CH_FLASH_TRANSFER_BLOCK_SIZE) { rc = CH_ERROR_INVALID_LENGTH; break; } checksum = CHugCalculateChecksum(&RxBuffer[CH_BUFFER_INPUT_DATA+4], length); if (checksum != RxBuffer[CH_BUFFER_INPUT_DATA+3]) { rc = CH_ERROR_INVALID_CHECKSUM; break; } /* copy low 32 bytes into flash buffer, and only write * in 64 byte chunks as this is a limitation of the * hardware */ if ((address & CH_FLASH_TRANSFER_BLOCK_SIZE) == 0) { memset (FlashBuffer, 0xff, CH_FLASH_WRITE_BLOCK_SIZE); memcpy (FlashBuffer, (const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+4], length); } else { memcpy (FlashBuffer + CH_FLASH_TRANSFER_BLOCK_SIZE, (const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+4], length); WriteBytesFlash(address - CH_FLASH_TRANSFER_BLOCK_SIZE, CH_FLASH_WRITE_BLOCK_SIZE, (unsigned char *) FlashBuffer); } break; case CH_CMD_BOOT_FLASH: /* are we lost or stolen */ if (flash_success == 0xff) { rc = CH_ERROR_DEVICE_DEACTIVATED; break; } /* only boot when USB stack is not busy */ idle_command = CH_CMD_BOOT_FLASH; break; case CH_CMD_SET_FLASH_SUCCESS: if (RxBuffer[CH_BUFFER_INPUT_DATA] != 0x00) { rc = CH_ERROR_INVALID_VALUE; break; } flash_success = RxBuffer[CH_BUFFER_INPUT_DATA]; EraseFlash(CH_EEPROM_ADDR_FLASH_SUCCESS, CH_EEPROM_ADDR_FLASH_SUCCESS + 1); WriteBytesFlash(CH_EEPROM_ADDR_FLASH_SUCCESS, 1, (unsigned char *) &RxBuffer[CH_BUFFER_INPUT_DATA]); break; case CH_CMD_SELF_TEST: rc = CHugSelfTest(); break; default: rc = CH_ERROR_UNKNOWN_CMD_FOR_BOOTLOADER; break; } /* always send return code */ if(!HIDTxHandleBusy(USBInHandle)) { TxBuffer[CH_BUFFER_OUTPUT_RETVAL] = rc; TxBuffer[CH_BUFFER_OUTPUT_CMD] = cmd; USBInHandle = HIDTxPacket(HID_EP, (BYTE*)&TxBuffer[0], CH_USB_HID_EP_SIZE); } /* re-arm the OUT endpoint for the next packet */ USBOutHandle = HIDRxPacket(HID_EP, (BYTE*)&RxBuffer, CH_USB_HID_EP_SIZE); }
// Process input and output void ProcessIO(void) { //LED0 = ON; // If we are not in the configured state just return if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) { // We are not configured, set LED1 to off to show status //LED1 = OFF; return; } // We are configured, show state in LED1 //LED1 = ON; // Check to see if the success indicator is on and update the delay counter if (successIndicatorFlag == TRUE) { //LED2 = ON; successIndicatorCounter++; if (successIndicatorCounter == 20000) { //LED2 = OFF; successIndicatorCounter = 0; successIndicatorFlag = FALSE; } } // Check to see if the failure indicator is on and update the delay counter if (failureIndicatorFlag == TRUE) { //LED3 = ON; failureIndicatorCounter++; if (failureIndicatorCounter == 80000) { //LED3 = OFF; failureIndicatorCounter = 0; failureIndicatorFlag = FALSE; } } // Note: For all tests we expect to receive a 64 byte packet containing // the command in byte[0] and then the numbers 0-62 in bytes 1-63. unsigned char bufferPointer; unsigned char expectedData; unsigned char dataReceivedOk; unsigned char dataSentOk; // Check if data was received from the host. if(!HIDRxHandleBusy(USBOutHandle)) { // Test to see if we are in bulk send/receieve mode, or if we are waiting for a command if (bulkSendFlag == TRUE || bulkReceiveFlag == TRUE) { // We are either bulk sending or receieving // If we are bulk sending, check that we are not busy and send the next packet if (bulkSendFlag == TRUE && !HIDTxHandleBusy(USBInHandle)) { // Send the next packet expectedData = bulkSendPacketCounter; for (bufferPointer = 0; bufferPointer < 64; bufferPointer++) { ToSendDataBuffer[bufferPointer] = expectedData; } // Transmit the response to the host USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64); // Add this following delay in if you want to simulate a slow transfer // from a busy PIC when testing bulk sends: // for (long int counter = 0; counter < 1000; counter++) __delay_us(100); bulkSendPacketCounter++; // Next packet // Are we done yet? if (bulkSendPacketCounter == bulkSendExpectedPackets) { // All done, indicate success and go back to command mode bulkSendFlag = FALSE; successIndicatorFlag = TRUE; } } // If we are bulk receiving get the next packet if (bulkReceiveFlag == TRUE) { // The received data buffer is already filled by the USB stack // we just have to confirm the data integrity expectedData = bulkReceivePacketCounter; for (bufferPointer = 0; bufferPointer < 64; bufferPointer++) { // If the data isn't what we expected, turn on the failure light if (ReceivedDataBuffer[bufferPointer] != expectedData) failureIndicatorFlag = TRUE; } bulkReceivePacketCounter++; // Are we done yet? if (bulkReceivePacketCounter == bulkReceiveExpectedPackets) { // All done, indicate success and go back to command mode bulkReceiveFlag = FALSE; successIndicatorFlag = TRUE; } } } else { // Command mode switch(ReceivedDataBuffer[0]) { case 'M': dataReceivedOk = TRUE; ToSendDataBuffer[0] = highByte(frequency); ToSendDataBuffer[1] = lowByte(frequency); if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*)&ToSendDataBuffer[0], 64); } break; case 0x80: // Test 1 - Single packet write to device // Test the receieved data expectedData = 0; dataReceivedOk = TRUE; for (bufferPointer = 1; bufferPointer < 64; bufferPointer++) { if (ReceivedDataBuffer[bufferPointer] != expectedData) dataReceivedOk = FALSE; expectedData++; } // Display the test result if (dataReceivedOk == TRUE) successIndicatorFlag = TRUE; else failureIndicatorFlag = TRUE; break; case 0x81: // Test 2 - Single packet read/write // Test the receieved data expectedData = 0; dataReceivedOk = TRUE; for (bufferPointer = 1; bufferPointer < 64; bufferPointer++) { if (ReceivedDataBuffer[bufferPointer] != expectedData) dataReceivedOk = FALSE; expectedData++; } // If we got the data correctly, send the response packet if (dataReceivedOk == TRUE) { expectedData = 0; dataSentOk = TRUE; for (bufferPointer = 0; bufferPointer < 64; bufferPointer++) { ToSendDataBuffer[bufferPointer] = expectedData; expectedData++; } // Transmit the response to the host if(!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64); } // Show our success successIndicatorFlag = TRUE; } else failureIndicatorFlag = TRUE; break; case 0x82: // Test 3 - Single packet write, 128 packets read // Test the receieved data expectedData = 0; dataReceivedOk = TRUE; for (bufferPointer = 1; bufferPointer < 64; bufferPointer++) { if (ReceivedDataBuffer[bufferPointer] != expectedData) dataReceivedOk = FALSE; expectedData++; } // If the receive was ok, go into bulk sending mode if (dataReceivedOk == TRUE) { // Go into bulk sending mode... bulkSendExpectedPackets = 128; bulkSendFlag = TRUE; bulkSendPacketCounter = 0; } else failureIndicatorFlag = TRUE; break; case 0x83: // Test 4 - 128 packets write, single packet read // Test the receieved data expectedData = 0; dataReceivedOk = TRUE; for (bufferPointer = 1; bufferPointer < 64; bufferPointer++) { if (ReceivedDataBuffer[bufferPointer] != expectedData) dataReceivedOk = FALSE; expectedData++; } // If the receive was ok, go into bulk receiving mode if (dataReceivedOk == TRUE) { // Go into bulk sending mode... bulkReceiveExpectedPackets = 127; bulkReceiveFlag = TRUE; bulkReceivePacketCounter = 0; } else failureIndicatorFlag = TRUE; break; case 0x84: // Test 5 - Single Packet write, timeout on read // Here we receive a command which expects a reply, but we // deliberately don't send one to test the timeout mechanisms // in the communication class // Test the receieved data expectedData = 0; dataReceivedOk = TRUE; for (bufferPointer = 1; bufferPointer < 64; bufferPointer++) { if (ReceivedDataBuffer[bufferPointer] != expectedData) dataReceivedOk = FALSE; expectedData++; } // Display the test result if (dataReceivedOk == TRUE) successIndicatorFlag = TRUE; else failureIndicatorFlag = TRUE; // Now we quit without replying break; default: // Unknown command received break; } } // Only rearm the OUT endpoint if we are not bulk sending if (bulkSendFlag == FALSE) { // Re-arm the OUT endpoint for the next packet USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&ReceivedDataBuffer,64); } } }
/******************************************************************** * 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) { // User Application USB tasks if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return; //Check if we have received an OUT data packet from the host if(!HIDRxHandleBusy(USBOutHandle)) { //We just received a packet of data from the USB host. //Check the first byte of the packet to see what command the host //application software wants us to fulfill. switch (ReceivedDataBuffer[0]) { case 0x01: // System Commands switch (ReceivedDataBuffer[1]) { case 0x01: // System Commands // Copy any waiting debug text to the send data buffer ToSendDataBuffer[0] = 0xFF; // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; default: // Unknown command received break; } break; case 0x02: // Feeder Commands switch (ReceivedDataBuffer[1]) { case 0x02: // Feeder Status if (FeederStatus() == 1){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; case 0x03: // Go to feeder if ((ReceivedDataBuffer[2] >= 0) && (ReceivedDataBuffer[2] <= 16)){ moveToPosition(ReceivedDataBuffer[2]); } break; case 0x04: // Picker Up PickerUp(); break; case 0x05: // Zero Feeder ZeroFeeder(); break; case 0x06: // Picker Down PickerDown(); break; case 0x07: // Set Picker Port Output pickerBusval = ((ReceivedDataBuffer[3] << 8) | ReceivedDataBuffer[2]); pickerBus = pickerBusval; break; case 0x08: // Go to feeder if ((ReceivedDataBuffer[2] > 0) && (ReceivedDataBuffer[2] <= 16)){ moveToPositionWithoutPick(ReceivedDataBuffer[2]); } break; default: // Unknown command received break; } break; case 0x03: // Vacuum and Vibration Commands switch (ReceivedDataBuffer[1]) { case 0x01: // Vacuum 1 set if (ReceivedDataBuffer[2] == 0x01){ setVac1on; } else{ setVac1off; } break; case 0x02: // Vacuum 2 set if (ReceivedDataBuffer[2] == 0x01){ setVac2on; } else{ setVac2off; } break; case 0x03: // Vibration Motor set if (ReceivedDataBuffer[2] == 0x01){ SetDCOC1PWM(vibrationmotor_duty_cycle); vibrationrunning = 1; } else{ SetDCOC1PWM(0); vibrationrunning = 0; } break; case 0x04: // Vacuum 1 status if (vac1running == 1){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; case 0x05: // Vacuum 2 status if (vac2running == 1){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; case 0x06: // Vibration Motor status if (vibrationrunning == 1){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; case 0x07: // Vibration Motor status vibrationmotor_duty_cycle = ReceivedDataBuffer[2] * 4; if (vibrationrunning == 1){ SetDCOC1PWM(vibrationmotor_duty_cycle); } break; default: // Unknown command received break; } break; case 0x04: // LED Commands switch (ReceivedDataBuffer[1]) { case 0x01: // LED Base Camera on/off if (ReceivedDataBuffer[2] == 0x01){ SetDCOC2PWM(led1_duty_cycle); led1running = 1; }else{ SetDCOC2PWM(0); led1running = 0; } break; case 0x02: // LED Base Camera PWM set led1_duty_cycle = ReceivedDataBuffer[2] * 4; if (led1running == 1){ SetDCOC2PWM(led1_duty_cycle); } break; case 0x03: // LED Head Camera on/off if (ReceivedDataBuffer[2] == 0x01){ SetDCOC3PWM(led2_duty_cycle); led2running = 1; }else{ SetDCOC3PWM(0); led2running = 0; } break; case 0x04: // LED Head Camera PWM set led2_duty_cycle = ReceivedDataBuffer[2] * 4; if (led2running == 1){ SetDCOC3PWM(led2_duty_cycle); } break; case 0x05: // LED Base Status if (led1running == 1){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; case 0x06: // LED Head Status if (led2running == 1){ ToSendDataBuffer[0] = 0x01; } else{ ToSendDataBuffer[0] = 0x00; } // Transmit the response to the host if (!HIDTxHandleBusy(USBInHandle)) { USBInHandle = HIDTxPacket(HID_EP, (BYTE*) & ToSendDataBuffer[0], 64); } break; default: // Unknown command received break; } break; default: // Unknown command received 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(HID_EP, (BYTE*)&ReceivedDataBuffer, 64); } }//end ProcessIO