void ManageDemoState ( void ) { int j; DWORD byteCount; BYTE errorCode; BYTE RetVal; //接続されていなかったら初期化 if (USBHostGenericDeviceDetached(deviceAddress) && deviceAddress != 0) { long i; for(i = 0;i < sizeof(jpeg);i++){ jpeg[i] = 0; } UART2PrintString( "Generic demo device detached - polled\r\n" ); DemoState = DEMO_INITIALIZE; deviceAddress = 0; } switch (DemoState) { case DEMO_INITIALIZE: DemoState = DEMO_STATE_IDLE; break; case DEMO_STATE_IDLE: //接続されたら、読み出し処理に移行 if (CheckForNewAttach()) { DemoState = DEMO_STATE_GET_INFO; UART2PrintString( "USB_Ver=" ); UART2PutDec(((USB_DEVICE_DESCRIPTOR *)pDeviceDescriptor)->bcdUSB >> 8); UART2PrintString( "-" ); UART2PutDec(((USB_DEVICE_DESCRIPTOR *)pDeviceDescriptor)->bcdUSB); UART2PrintString( "\r\n" ); } break; case DEMO_STATE_GET_INFO: if(control(GET_INFO, VS_PROBE_CONTROL, 1, param, 1) == USB_SUCCESS){ UART2PrintString( "GET_INFO, VS_PROBE_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_GET_INFO; } break; case DEMO_STATE_WAIT_GET_INFO: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=GET_INFO, VS_PROBE_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(param,byteCount); UART2PrintString( "\r\n" ); DemoState = DEMO_STATE_GET_DEF; } break; case DEMO_STATE_GET_DEF: if(control(GET_DEF, VS_PROBE_CONTROL, 1, temp, param_len) == USB_SUCCESS){ UART2PrintString( "GET_DEF, VS_PROBE_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_GET_DEF; } break; case DEMO_STATE_WAIT_GET_DEF: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=GET_DEF, VS_PROBE_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(temp,byteCount); UART2PrintString( "\r\n" ); DemoState = DEMO_STATE_GET_MIN; } break; case DEMO_STATE_GET_MIN: if(control(GET_MIN, VS_PROBE_CONTROL, 1, temp, param_len) == USB_SUCCESS){ UART2PrintString( "GET_MIN, VS_PROBE_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_GET_MIN; } break; case DEMO_STATE_WAIT_GET_MIN: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=GET_MIN, VS_PROBE_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(temp,byteCount); UART2PrintString( "\r\n" ); DemoState = DEMO_STATE_GET_MAX; } break; case DEMO_STATE_GET_MAX: if(control(GET_MAX, VS_PROBE_CONTROL, 1, temp, param_len) == USB_SUCCESS){ UART2PrintString( "GET_MAX, VS_PROBE_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_GET_MAX; } break; case DEMO_STATE_WAIT_GET_MAX: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=GET_MAX, VS_PROBE_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(temp,byteCount); UART2PrintString( "\r\n" ); DemoState = DEMO_STATE_GET_CUR; } break; case DEMO_STATE_GET_CUR: if(control(GET_CUR, VS_PROBE_CONTROL, 1, temp, param_len) == USB_SUCCESS){ UART2PrintString( "GET_CUR, VS_PROBE_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_GET_CUR; } break; case DEMO_STATE_WAIT_GET_CUR: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=GET_CUR, VS_PROBE_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(temp,byteCount); UART2PrintString( "\r\n" ); DemoState =DEMO_STATE_SET_CUR ; } break; case DEMO_STATE_SET_CUR: for(j = 0;j < param_len;j++){ temp[j] = 0; } temp[2] = 2;//フォーマットインデックス temp[3] = 1;//フレームインデックス /*temp[4] = 0x80; temp[5] = 0x84; temp[6] = 0x1E; temp[7] = 0x00;*/ temp[4] = 0x15;//30Hz temp[5] = 0x16; temp[6] = 0x05; temp[7] = 0x00; if(control(SET_CUR, VS_PROBE_CONTROL, 1, temp, param_len) == USB_SUCCESS){ UART2PrintString( "SET_CUR, VS_PROBE_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_SET_CUR; } break; case DEMO_STATE_WAIT_SET_CUR: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=SET_CUR, VS_PROBE_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(temp,byteCount); UART2PrintString( "\r\n" ); DemoState =DEMO_STATE_GET_CUR2 ; } break; case DEMO_STATE_GET_CUR2: if(control(GET_CUR, VS_PROBE_CONTROL, 1, temp, param_len) == USB_SUCCESS){ UART2PrintString( "GET_CUR2, VS_PROBE_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_GET_CUR2; } break; case DEMO_STATE_WAIT_GET_CUR2: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=GET_CUR2, VS_PROBE_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(temp,byteCount); UART2PrintString( "\r\n" ); DemoState =DEMO_STATE_GET_INFO2 ; } break; case DEMO_STATE_GET_INFO2: if(control(GET_INFO, VS_COMMIT_CONTROL, 1, param, 1) == USB_SUCCESS){ UART2PrintString( "GET_INFO, VS_COMMIT_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_GET_INFO2; } break; case DEMO_STATE_WAIT_GET_INFO2: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=GET_INFO, VS_COMMIT_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(param,byteCount); UART2PrintString( "\r\n" ); DemoState = DEMO_STATE_GET_CUR3; } break; case DEMO_STATE_GET_CUR3: if(control(GET_CUR, VS_COMMIT_CONTROL, 1, temp, param_len) == USB_SUCCESS){ UART2PrintString( "GET_CUR2, VS_COMMIT_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_GET_CUR3; } break; case DEMO_STATE_WAIT_GET_CUR3: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=GET_CUR2, VS_COMMIT_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(temp,byteCount); UART2PrintString( "\r\n" ); DemoState =DEMO_STATE_SET_CUR2 ; } break; case DEMO_STATE_SET_CUR2: for(j = 0;j < param_len;j++){ temp[j] = 0; } temp[2] = 2; temp[3] = 0x01;//640x480 30Hz //temp[3] = 0x02;//160x120 30Hz //temp[3] = 0x0C;//800x600 temp[4] = 0x80;//5Hz temp[5] = 0x84; temp[6] = 0x1E; temp[7] = 0x00; /*temp[4] = 0x15;//30Hz temp[5] = 0x16; temp[6] = 0x05; temp[7] = 0x00;*/ if(control(SET_CUR, VS_COMMIT_CONTROL, 1, temp, param_len) == USB_SUCCESS){ UART2PrintString( "SET_CUR, VS_COMMIT_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_SET_CUR2; } break; case DEMO_STATE_WAIT_SET_CUR2: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=SET_CUR, VS_COMMIT_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(temp,byteCount); UART2PrintString( "\r\n" ); DemoState =DEMO_STATE_SET_ISOCHRONOUS ; } break; case DEMO_STATE_SET_ISOCHRONOUS: if(USBHostIssueDeviceRequest( deviceAddress, 0x01, USB_REQUEST_SET_INTERFACE, 0x06/*Alternate Setting:0x1*/, 0x01/*interface:0x03*/, 0, NULL, USB_DEVICE_REQUEST_SET, 0x00 )== USB_SUCCESS){ UART2PrintString( "USB_REQUEST_SET_INTERFACE=OK!\r\n" ); DemoState =DEMO_STATE_WAIT_SET_ISOCHRONOUS ; } break; case DEMO_STATE_WAIT_SET_ISOCHRONOUS: if(USBHostReadIsochronous(deviceAddress,0x81,&isocData) == USB_SUCCESS){ //UART2PrintString( "USBHostReadIsochronous=OK!\r\n" ); DemoState = DEMO_STATE_ERROR; } break; case DEMO_STATE_ERROR: break; default: DemoState = DEMO_INITIALIZE; break; } //DelayMs(1); // 1ms delay } // ManageDemoState
/************************************************************************* * Function: ManageDemoState * * Preconditions: The DemoState global variable must be initialized to * DEMO_STATE_IDLE (0). (This occurs on reset.) * * Input: DemoState (global) * Actions selected based value of DemoState on function * entry. * * deviceAddress (global) * May use device address to access device, depending on * state. * * DataPacket (global) * May read data from packet buffer, depending on state. * * Output: DemoState (global) * Updates demo state as appropriate. * * DataPacket (global) * May cause data in the packet buffer to be updated, * depending on state. * * Returns: None * * Side Effects: Depend on state transition * * Overview: This routine maintains the state of the application, * updateing global data and taking actions as necessary * to maintain the custom demo operations. *************************************************************************/ void ManageDemoState ( void ) { BYTE RetVal; BlinkStatus(); // Watch for device timeouts // if (MSTimerGetTime() > DEMO_TIMEOUT_LIMIT) // { // if (DemoState == DEMO_STATE_IDLE) // { // LCDWriteLine( 2, "Awaiting Device" ); // } // else // { // UART2PrintString( "Device Time-out Error!\r\n" ); // LCDWriteLine( 2, "Dev Time-out Err" ); // DemoState = DEMO_STATE_ERROR; // } // } // Watch for device detaching if (USBHostGenericDeviceDetached(deviceAddress) && deviceAddress != 0) { UART2PrintString( "Generic demo device detached - polled\r\n" ); DemoState = DEMO_INITIALIZE; deviceAddress = 0; } switch (DemoState) { case DEMO_INITIALIZE: InitLcdMessage(); DemoState = DEMO_STATE_IDLE; break; /** Idle State: Loops here until attach **/ case DEMO_STATE_IDLE: if (CheckForNewAttach()) { DemoState = DEMO_STATE_GET_DEV_VERSION; } break; /** Sequence: Read Dev FW Version **/ case DEMO_STATE_GET_DEV_VERSION: // Send the Read Version command DataPacket.CMD = READ_VERSION; DataPacket.len = 2; if (!USBHostGenericTxIsBusy(deviceAddress)) { if ( (RetVal=USBHostGenericWrite(deviceAddress, &DataPacket, 2)) == USB_SUCCESS ) { DemoState = DEMO_STATE_WAITING_VER_REQ; } else { UART2PrintString( "1 Device Write Error 0x" ); UART2PutHex(RetVal); UART2PrintString( "\r\n" ); LCDWriteLine( 2, "Dev Write Error " ); } } break; case DEMO_STATE_WAITING_VER_REQ: if (!USBHostGenericTxIsBusy(deviceAddress) ) DemoState = DEMO_STATE_READ_DEV_VERSION; break; case DEMO_STATE_READ_DEV_VERSION: if (!USBHostGenericRxIsBusy(deviceAddress)) { if ( (RetVal=USBHostGenericRead(deviceAddress, &DataPacket, 4)) == USB_SUCCESS ) { DemoState = DEMO_STATE_WAITING_READ_VER; } else { UART2PrintString( "1 Device Read Error 0x" ); UART2PutHex(RetVal); UART2PrintString( "\r\n" ); LCDWriteLine( 2, "Dev Read Error " ); } } break; case DEMO_STATE_WAITING_READ_VER: if (!USBHostGenericRxIsBusy(deviceAddress)) DemoState = DEMO_STATE_VERIFY_DEV_FW_VER; break; case DEMO_STATE_VERIFY_DEV_FW_VER: if (ValidateAndDisplayDeviceFwVersion()) DemoState = DEMO_STATE_GET_TEMPERATURE; else DemoState = DEMO_STATE_ERROR; break; /** Sequence: Read Temperature Sensor Data **/ case DEMO_STATE_GET_TEMPERATURE: // Send the Read Temperature command DataPacket.CMD = RD_TEMP; DataPacket.len = 2; if (!USBHostGenericTxIsBusy(deviceAddress)) { if ( (RetVal=USBHostGenericWrite(deviceAddress, &DataPacket, 2)) == USB_SUCCESS) { DemoState = DEMO_STATE_WAITING_GET_TEMP; } else { UART2PrintString( "2 Device Write Error 0x" ); UART2PutHex(RetVal); UART2PrintString( "\r\n" ); LCDWriteLine( 2, "Dev Write Error " ); } } break; case DEMO_STATE_WAITING_GET_TEMP: if (!USBHostGenericTxIsBusy(deviceAddress) ) { DemoState = DEMO_STATE_READ_TEMPERATURE; } break; case DEMO_STATE_READ_TEMPERATURE: if (!USBHostGenericRxIsBusy(deviceAddress)) { if ( (RetVal=USBHostGenericRead(deviceAddress, &DataPacket, 3)) == USB_SUCCESS) { DemoState = DEMO_STATE_WAITING_READ_TEMP; } else { UART2PrintString( "2 Device Read Error 0x" ); UART2PutHex(RetVal); UART2PrintString( "\r\n" ); LCDWriteLine( 2, "Dev Read Error " ); } } break; case DEMO_STATE_WAITING_READ_TEMP: if (!USBHostGenericRxIsBusy(deviceAddress)) { DemoState = DEMO_STATE_DISPLAY_TEMPERATURE; } break; case DEMO_STATE_DISPLAY_TEMPERATURE: DisplayTemperature(); DemoState = DEMO_STATE_GET_POT; break; /** Sequence: Read POT Sensor Data **/ case DEMO_STATE_GET_POT: // Send the Read POT command DataPacket.CMD = RD_POT; DataPacket.len = 2; if (!USBHostGenericTxIsBusy(deviceAddress)) { if ( (RetVal=USBHostGenericWrite(deviceAddress, &DataPacket, 2)) == USB_SUCCESS) { DemoState = DEMO_STATE_WAITING_GET_POT; } else { UART2PrintString( "3 Device Write Error 0x" ); UART2PutHex(RetVal); UART2PrintString( "\r\n" ); LCDWriteLine( 2, "Dev Write Error " ); } } break; case DEMO_STATE_WAITING_GET_POT: if (!USBHostGenericTxIsBusy(deviceAddress) ) DemoState = DEMO_STATE_READ_POT; break; case DEMO_STATE_READ_POT: if (!USBHostGenericRxIsBusy(deviceAddress)) { if ( (RetVal=USBHostGenericRead(deviceAddress, &DataPacket, 3)) == USB_SUCCESS) { DemoState = DEMO_STATE_WAITING_READ_POT; } else { UART2PrintString( "3 Device Read Error 0x" ); UART2PutHex(RetVal); UART2PrintString( "\r\n" ); LCDWriteLine( 2, "Dev Read Error " ); } } break; case DEMO_STATE_WAITING_READ_POT: if (!USBHostGenericRxIsBusy(deviceAddress)) DemoState = DEMO_STATE_DISPLAY_POT; break; case DEMO_STATE_DISPLAY_POT: DisplayPot(); DemoState = DEMO_STATE_SEND_SET_LED; break; /** Sequence: Update LEDs **/ case DEMO_STATE_SEND_SET_LED: // Send the set-LED command DataPacket.CMD = UPDATE_LED; DataPacket.len = 3; if (Switch3WasPressed()) { DataPacket.led_num = 3; // LED 3 on original PIC18 FS USB board DataPacket.led_status = LEDState.bits.b3 ^ 1; } else if (Switch6WasPressed()) { DataPacket.led_num = 4; // LED 4 on original PIC18 FS USB board DataPacket.led_status = LEDState.bits.b4 ^ 1; } else { DemoState = DEMO_STATE_GET_TEMPERATURE; break; } if (!USBHostGenericTxIsBusy(deviceAddress)) { if ( (RetVal=USBHostGenericWrite(deviceAddress, &DataPacket, 3)) == USB_SUCCESS) { DemoState = DEMO_STATE_WAITING_SET_LED; } else { UART2PrintString( "4 Device Write Error 0x" ); UART2PutHex(RetVal); UART2PrintString( "\r\n" ); LCDWriteLine( 2, "Dev Write Error " ); } } break; case DEMO_STATE_WAITING_SET_LED: if (!USBHostGenericTxIsBusy(deviceAddress) ) DemoState = DEMO_STATE_READ_SET_LED_RESP; break; case DEMO_STATE_READ_SET_LED_RESP: if (!USBHostGenericRxIsBusy(deviceAddress)) { DataPacket.CMD = CMD_INVALID; if ( (RetVal=USBHostGenericRead(deviceAddress, &DataPacket, 1)) == USB_SUCCESS) { DemoState = DEMO_STATE_WAITING_LED_RESP; } else { UART2PrintString( "4 Device Read Error 0x" ); UART2PutHex(RetVal); UART2PrintString( "\r\n" ); LCDWriteLine( 2, "Dev Read Error " ); } } break; case DEMO_STATE_WAITING_LED_RESP: if (!USBHostGenericRxIsBusy(deviceAddress)) DemoState = DEMO_STATE_UPDATE_LED_STATE; break; case DEMO_STATE_UPDATE_LED_STATE: if (DataPacket.CMD == UPDATE_LED) // CMD updated by read from device { if (DataPacket.led_num == 3) // led_num left-over from set-LED command send { LEDState.bits.b3 ^= 1; // mLED_10_Toggle(); } else if (DataPacket.led_num == 4) { LEDState.bits.b4 ^= 1; // mLED_9_Toggle(); } } DemoState = DEMO_STATE_GET_TEMPERATURE; break; /** Error state: Hold here until detached **/ case DEMO_STATE_ERROR: // To Do: Flash LEDs break; default: DemoState = DEMO_INITIALIZE; break; } DelayMs(1); // 1ms delay } // ManageDemoState