#ifndef KEYBOARD_C #define KEYBOARD_C /** INCLUDES *******************************************************/ #include #include "./USB/usb.h" #include "HardwareProfile.h" #include "./USB/usb_function_hid.h" /** CONFIGURATION **************************************************/ // PIC24FJ64GB002 _CONFIG1(WINDIS_OFF & FWDTEN_OFF & ICS_PGx1 & GCP_OFF & JTAGEN_OFF) _CONFIG2(IESO_ON & PLLDIV_DIV2 & PLL96MHZ_ON & FNOSC_FRCPLL & FCKSM_CSDCMD & OSCIOFNC_ON & IOL1WAY_OFF & I2C1SEL_PRI & POSCMOD_NONE) _CONFIG3(WPFP_WPFP0 & SOSCSEL_IO & WUTSEL_LEG & WPDIS_WPDIS & WPCFG_WPCFGDIS & WPEND_WPENDMEM) _CONFIG4(DSWDTPS_DSWDTPS3 & DSWDTOSC_LPRC & RTCOSC_SOSC & DSBOREN_OFF & DSWDTEN_OFF) /** VARIABLES ******************************************************/ #pragma udata BYTE old_sw1,old_sw2; char buffer[8]; unsigned char OutBuffer[8]; #pragma udata USB_HANDLE lastINTransmission; USB_HANDLE lastOUTTransmission; /** PRIVATE PROTOTYPES *********************************************/ BOOL Switch1IsPressed(void); BOOL Switch2IsPressed(void); static void InitializeSystem(void); void ProcessIO(void); void UserInit(void); void USBCBSendResume(void); void Keyboard(void); void USBHIDCBSetReportComplete(void); /** VECTOR REMAPPING ***********************************************/ /** DECLARATIONS ***************************************************/ #pragma code /******************************************************************** * Function: void main(void) *******************************************************************/ void main(void) { InitializeSystem(); #if defined(USB_INTERRUPT) USBDeviceAttach(); #endif while(1) { #if defined(USB_POLLING) // Check bus status and service USB interrupts. USBDeviceTasks(); #endif // Application-specific tasks. // Application related code may be added here, or in the ProcessIO() function. ProcessIO(); } } /******************************************************************** * Function: static void InitializeSystem(void) *******************************************************************/ static void InitializeSystem(void) { UserInit(); #if defined(USE_USB_BUS_SENSE_IO) tris_usb_bus_sense = INPUT_PIN; // See HardwareProfile.h #endif #if defined(USE_SELF_POWER_SENSE_IO) tris_self_power = INPUT_PIN; // See HardwareProfile.h #endif USBDeviceInit(); //usb_device.c. Initializes USB module SFRs and firmware variables to known states. } /****************************************************************************** * Function: void UserInit(void) *****************************************************************************/ void UserInit(void) { unsigned int pll_startup_counter = 600; CLKDIV = 0x0000; // CPU:32MHz CLKDIVbits.PLLEN = 1; // 96MHz PLL On, while(pll_startup_counter--); RCONbits.SWDTEN = 0; //ウオッチドック・ソフトウエア OFF AD1PCFG = 0xFFFF; //AD OFF /* 入出力ポート設定 */ TRISA = 0x0000; //ポートA、全て出力 TRISB = 0x0003; //ポートB、Bit0-1入力、Bit2-7出力 //Initialize all of the PUSH buttons & LED mInitAllSwitches(); old_sw1 = sw1; old_sw2 = sw2; led01 = 0; //LED ON led02 = 0; //initialize the variable holding the handle for the last // transmission lastINTransmission = 0; lastOUTTransmission = 0; } /******************************************************************** * Function: void ProcessIO(void) *******************************************************************/ void ProcessIO(void) { // User Application USB tasks if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return; //Call the function that behaves like a keyboard Keyboard(); } void Keyboard(void) { static unsigned char key = 4; //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(lastINTransmission)) { if(Switch2IsPressed()) //SW2 ON? { //Load the HID buffer hid_report_in[0] = 0; //R-G,A,S,C, L-G,A,S,C if ( sw1 == 0 ) { //SW1 ON? hid_report_in[0] = 0x02; //R-G,A,S,C, L-G,A,S,C } hid_report_in[1] = 0; //0x00 hid_report_in[2] = key++; //key 1 hid_report_in[3] = 0; //key 2 hid_report_in[4] = 0; //key 3 hid_report_in[5] = 0; //key 4 hid_report_in[6] = 0; //key 5 hid_report_in[7] = 0; //key 6 //Send the 8 byte packet over USB to the host. lastINTransmission = HIDTxPacket(HID_EP, (BYTE*)hid_report_in, 0x08); if(key == 40) { key = 4; } } else { //Load the HID buffer hid_report_in[0] = 0; hid_report_in[1] = 0; hid_report_in[2] = 0; //Indicate no character pressed 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. lastINTransmission = 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(lastOUTTransmission)) { lastOUTTransmission = HIDRxPacket(HID_EP,(BYTE*)&hid_report_out,1); //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. { led02 = 0; //LED2 On; } else { led02 = 1; //LED2 Off } } return; } /****************************************************************************** * Function: BOOL Switch1IsPressed(void) *****************************************************************************/ BOOL Switch1IsPressed(void) { if(sw1 != old_sw1) { old_sw1 = sw1; // Save new value if(sw1 == 0) // If pressed return TRUE; // Was pressed } return FALSE; // Was not pressed } /****************************************************************************** * Function: BOOL Switch2IsPressed(void) *****************************************************************************/ BOOL Switch2IsPressed(void) { if(sw2 != old_sw2) { old_sw2 = sw2; // Save new value if(sw2 == 0) // If pressed return TRUE; // Was pressed } return FALSE; // Was not pressed } // ****************************************************************************************************** // ************** USB Callback Functions **************************************************************** // ******************************************************************************************************