int main(void) { uchar i = 1; uchar hidCurrentMode = 255; char remainingData=0; uchar offset=0; HardwareInit(); usbInit(); // Set up descriptor hidMode = HIDM_1P; ReadController(1); SetHIDMode(); for(;;){ /* main event loop */ usbPoll(); if(usbInterruptIsReady()){ /* called after every poll of the interrupt endpoint */ ReadController(i); RemapButtons(&(reportBuffer.b1), &(reportBuffer.b2)); RemapButtons(&(reportBufferWheel.b1), &(reportBufferWheel.b2)); remainingData=reportBufferLength; offset=0; // handle report with more than 8 byte length (for NegCon and future expansion) do { if (remainingData<=8) { usbSetInterrupt(reportBufferAddress+offset, remainingData); remainingData=0; } else { usbSetInterrupt(reportBufferAddress+offset, 8); offset+=8; remainingData-=8; do { usbPoll(); } while (!usbInterruptIsReady()); } } while (remainingData>0); i++; if (i > hidNumReports) i = 1; if (hidCurrentMode != hidMode) { SetHIDMode(); hidCurrentMode = hidMode; } } } return 0; }
int main( void ) { usbInit(); sei(); init_joy(); for ( ;; ) { usbPoll(); // Don't bother reading joy if previous changes haven't gone out yet. // Forces delay after changes which serves to debounce controller as well. if ( usbInterruptIsReady() ) { read_joy(); // Don't send update unless joystick changed if ( memcmp( report_out, report, sizeof report ) ) { memcpy( report_out, report, sizeof report ); usbSetInterrupt( report_out, sizeof report_out ); toggle_led(); } } } return 0; }
void usb_keyboard_periodic(void) { if(keyDidChange && usbInterruptIsReady()){ USBKEYBOARDDEBUG("usb set interrupt lastkey %c, key: %i\n",lastKey, key); buildReport(lastKey); usbSetInterrupt(reportBuffer, sizeof(reportBuffer)); } // send end of key while (keyDidChange && !usbInterruptIsReady()){ _delay_ms(10); // as slow speed device we have to wait until next call usbPoll(); buildReport(0); usbSetInterrupt(reportBuffer, sizeof(reportBuffer)); keyDidChange = 0; } }
int main(void) { wdt_enable(WDTO_1S); initPodControls(); /* Even if you don't use the watchdog, turn it off here. On newer devices, * the status of the watchdog (on/off, period) is PRESERVED OVER RESET! */ /* RESET status: all port bits are inputs without pull-up. * That's the way we need D+ and D-. Therefore we don't need any * additional hardware initialization. */ usbInit(); usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */ uchar i = 0; while(--i){ /* fake USB disconnect for > 250 ms */ wdt_reset(); _delay_ms(1); } usbDeviceConnect(); sei(); for(;;){ /* main event loop */ wdt_reset(); usbPoll(); if(usbInterruptIsReady()){ /* called after every poll of the interrupt endpoint */ usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer)); } else { updateSensorData(); updateLEDState(); } } }
void AVRISP_poll(void) { extern uchar usbNewDeviceAddr; usbPoll(); if ((STK500_rxLen)) { STK500_processmessage(); STK500_rxLen = 0; } if(usbNewDeviceAddr) leds[LED_BLUE].frequency = LED_ON; if ((STK500_txLen) && (usbInterruptIsReady())) { RSP_Pkg_Len = STK500_txLen - STK500_txLen_tmp; if(RSP_Pkg_Len > USB_EP_SIZE) RSP_Pkg_Len = USB_EP_SIZE; usbSetInterrupt((uchar*)STK500_Buffer + STK500_txLen_tmp,RSP_Pkg_Len); STK500_txLen_tmp += RSP_Pkg_Len; if(RSP_Pkg_Len < USB_EP_SIZE) { STK500_txLen = 0; STK500_txLen_tmp = 0; } } }
void sendPS3Data(dataForController data) { /* The data being transferred is in the following format: byte0 - 8 buttons ( bit0...bit7 = [blue][green][red][yellow][orange][star power][is higher frets][unused] byte1 - 5 buttons, 3bits padding bit0...bit7 = [select][start][??][??],[ps3 button],[pad][pad][pad] byte2 - hat switch 0000 N, 0001 N/E, 0010 E, 0011 SE, 0100 S, 0101 SW, 0110 W, 0111, NW, 1000, nothing pressed byte3 - x axis (unused) byte4 - y axis (unused) byte5 - z axis (Whammy Bar) byte6 - rzaxis (Choose solo style) */ //set the buttons to all at default positions reportBuffer[0] = 0b00000000; reportBuffer[1] = 0b00000000; reportBuffer[2] = 0b00001000; reportBuffer[3] = 0b10000000; reportBuffer[4] = 0b10000000; reportBuffer[5] = 0b10000000; reportBuffer[6] = 0b00000000; // Since 'colorOn' is 1 when a fret is pressed, we shift it to the proper place and | it to set the // corresponding button bit to one, which corresponds to a button press. reportBuffer[0] |= (data.orangeOn << ORANGE_BIT); reportBuffer[0] |= (data.blueOn << BLUE_BIT); reportBuffer[0] |= (data.yellowOn << YELLOW_BIT); reportBuffer[0] |= (data.redOn << RED_BIT); reportBuffer[0] |= (data.greenOn << GREEN_BIT); // On the PS3, the strumming shows up as a hat switch, so we have to use conditionals for this.. if (data.upOn) reportBuffer[2] = 0b00000000; if (data.downOn) reportBuffer[2] = 0b00000100; // Now set the whammy bar data reportBuffer[5] = data.numberOfStringsPressed; //Finally, Start, Star Power, and the Home button (not working yet) reportBuffer[1] |= (data.plusOn << PLUS_BIT); reportBuffer[0] |= (data.minusOn << MINUS_BIT); reportBuffer[1] |= (data.homeOn << HOME_BIT); //Then we finish off some USB stuff. wdt_reset(); //Reset the watchdog timer usbPoll(); //USB poll - must be called at least once per 10ms // Now, regardless of mode, we have to send the data to the // USB controlling library from the buffer we've been building if(usbInterruptIsReady()) { /* called after every poll of the interrupt endpoint */ usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer)); } }
void updateUSB(void) { wdt_reset(); usbPoll(); if (isDataChanged() && usbInterruptIsReady()) { reportBuffer.number = getData(); usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer)); } }
void sendHidReport() { wdt_reset(); usbPoll(); if(usbInterruptIsReady()) { usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer)); } }
/**************************************************************************** * main() * * * * * ***************************************************************************/ int main(void) { uchar i; uchar calibrationValue; calibrationValue = eeprom_read_byte(0); /* calibration value from last time */ if(calibrationValue != 0xff){ OSCCAL = calibrationValue; } i2c_init(); usbInit(); usbDeviceDisconnect(); i = 0; while(--i) { _delay_ms(1); } usbDeviceConnect(); DDRB &= ~_BV(DDB1); // input from slave to indicate a msg is waiting ///////// // DDRB = 0b0010; // set pb1 as output ///////// sei(); for(;;) { usbPoll(); if (PINB & _BV(PB1)) { while (PINB & _BV(PB1)) { // wait until off } i2c_start((DEVICE_ID << 1) + I2C_READ); action = i2c_read(0); i2c_stop(); if (action <= sizeof(keys)/sizeof(uchar)) { reportCount = 0; } } if(usbInterruptIsReady() && (reportCount < 2)){ /* we can send another key */ buildReport(); usbSetInterrupt(reportBuffer, sizeof(reportBuffer)); } } return 0; }
/** * This function is used to guarantee that the data is sent to the computer once. * Sends the usb report through the serial interface. * * @param data - USB Report type * @param len - sizeof(USB Report) */ void usbSendHidReport(byte* data, uchar len){ while(1){ usbPoll(); if (usbInterruptIsReady()){ usbSetInterrupt(data, len); break; } } }
void usb_mouse_periodic(void) { if (flags & FLG_CMD_RECEIVED) { if (usbInterruptIsReady()){ usbSetInterrupt(reportBuffer, sizeof(reportBuffer)); } flags &= ~FLG_CMD_RECEIVED; } }
void usbReportSend() { // perform usb background tasks until the report can be sent, then send it while (1) { usbPoll(); // this needs to be called at least once every 10 ms if (usbInterruptIsReady()) { usbSetInterrupt((uint8_t*)report_buffer, 8); // send break; // see http://vusb.wikidot.com/driver-api } } }
void xxxProcess() { wdt_reset(); usbPoll(); if (changed) { if(usbInterruptIsReady()){ changed = false; // called after every poll of the interrupt endpoint usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer)); } } }
int main(){ reportBuffer[0] = 0x00;//(1<<1); wdt_enable(WDTO_1S); usbInit(); usbDeviceDisconnect(); register uint8_t i=0; while(--i){ wdt_reset(); _delay_ms(1); } usbDeviceConnect(); sei(); CLR(DDRB, PB0); SET(DDRD, PD7); TCCR0B = 5; //prescale TC0 1024, 20 MHz -> 50 ns * 1024 * 255 = 13.056 ms while(1){ usbPoll(); wdt_reset(); if(CHK(TIFR0, TOV0)){ TIFR0 = 1<<TOV0; TCNT0 = 0; if(CHK(PINB, PB0) && changed == 0){ keydown = 1; if (prev == keydown) changed = 0; else changed = 1; prev = 1; }else{ keydown = 0; if (prev == keydown) changed =0; else changed = 1; prev = 0; } } if(changed){ changed = 0; TIFR0 = 1<<TOV0; TCNT0 = 0; if(keydown){ reportBuffer[0] = 0x02; }else{ reportBuffer[0] = 0x00; } TOG(PORTD, PD7); wdt_reset(); usbSetInterrupt(reportBuffer, sizeof(reportBuffer)); } } }
int main(void) { wdt_enable(WDTO_1S); odDebugInit(); DBG1(0x00, 0, 0); /* debug output: main starts */ ppmInit(); usbInit(); usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */ uchar i = 0; while(--i){ /* fake USB disconnect for > 250 ms */ wdt_reset(); _delay_ms(1); } usbDeviceConnect(); DDRC|=3; // LEDs: Output sei(); DBG1(0x01, 0, 0); /* debug output: main loop starts */ uchar changed=0; ppmNewData=1; for(;;){ /* main event loop */ DBG1(0x02, 0, 0); /* debug output: main loop iterates */ wdt_reset(); usbPoll(); if (ppmNewData) { ppmNewData=0; for (i=0;i<sizeof(reportBuffer);i++) { unsigned char val=ppmGet(i); if (reportBuffer[i]!=val) { reportBuffer[i]=val; changed=1; } } if (changed) { if(usbInterruptIsReady()){ changed=0; // called after every poll of the interrupt endpoint DBG1(0x03, 0, 0); // debug output: interrupt report prepared usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer)); } } } } }
/* transfer keyboard report from buffer */ void vusb_transfer_keyboard(void) { if (usbInterruptIsReady()) { if (kbuf_head != kbuf_tail) { usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t)); kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE; if (debug_keyboard) { print("V-USB: kbuf["); pdec(kbuf_tail); print("->"); pdec(kbuf_head); print("]("); phex((kbuf_head < kbuf_tail) ? (KBUF_SIZE - kbuf_tail + kbuf_head) : (kbuf_head - kbuf_tail)); print(")\n"); } } } }
void AVRISP_init(void) { STK500_init(); uint8_t i; //Reconnect USB usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */ i = 0; while(--i) _delay_ms(2); usbDeviceConnect(); usbSetInterrupt(0, 0); /* the host eats the first packet -- don't know why... */ usbInit(); sei(); }
void send_report_once() { // perform usb background tasks until the report can be sent, then send it while (1) { usbPoll(); // this needs to be called at least once every 10 ms if (usbInterruptIsReady()) { usbSetInterrupt(&keyboard_report, sizeof(keyboard_report)); // send break; // see http://vusb.wikidot.com/driver-api } } }
int main(void) { inInitialize(); outInitialize(); usbInitialize(); for(;;){ wdt_reset(); usbPoll(); getInput(); if(usbInterruptIsReady()){ /* called after every poll of the interrupt endpoint */ /* This is a push to the host */ usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer)); } } return 0; }
int main(void) { uchar key, lastKey = 0, keyDidChange = 0; uchar idleCounter = 0; hardwareInit(); sei(); for (;;) /* main event loop */ { wdt_reset(); usbPoll(); key = keyPressed(); if (lastKey != key) { lastKey = key; keyDidChange = 1; } if (TIFR & (1<<TOV0)) /* ~63 ms timer */ { TIFR = 1<<TOV0; /* clear overflow */ if (idleRate != 0) { if (idleCounter >= OVERFLOWS_4MS) { idleCounter -= OVERFLOWS_4MS; } else { /* USB HID poll timer reached * send current state regardless of real key change */ idleCounter = idleRate; keyDidChange = 1; } } } if (keyDidChange && usbInterruptIsReady()) { keyDidChange = 0; /* use last key and not current key status in order to avoid lost changes in key status. */ buildReport(lastKey); usbSetInterrupt(reportBuffer, sizeof(reportBuffer)); } } return 0; }
int main(void) { wdt_enable(WDTO_2S); /* Enable watchdog timer 2s */ hardwareInit(); /* Initialize hardware (I/O) */ usbInit(); /* Initialize USB stack processing */ Setup_init(); speed_init(); PWM_init(); KeyScan_init(); switch (Setup_key12LED){ case Setup_key12LED_Always: case Setup_key12LED_OftenOn: PWM_setOutputLevel(0,PWM_TotalLevel); PWM_setOutputLevel(1,PWM_TotalLevel); break; case Setup_key12LED_Never: case Setup_key12LED_OftenOff: PWM_setOutputLevel(0,0); PWM_setOutputLevel(1,0); } sei(); /* Enable global interrupts */ WorkMode_set(WorkMode_Unused); for(;;){ /* Main loop */ wdt_reset(); /* Reset the watchdog */ usbPoll(); if(KeyScan_keyChanged && usbInterruptIsReady()){ KeyScan_keyChanged = 0; buildReport(); usbSetInterrupt(reportBuffer, sizeof(reportBuffer)); }else{ if(TIFR0&(1<<TOV0)){ TIFR0 |= 1<<TOV0; PWM_Generator(); } } } return 0; }
int main(void) { uchar key, lastKey = 0, keyDidChange = 0; uchar idleCounter = 0; wdt_enable(WDTO_2S); hardwareInit(); usbInit(); sei(); for(;;) { wdt_reset(); usbPoll(); key = keyPressed(); if(lastKey != key) { lastKey = key; keyDidChange = 1; } // 22 ms timer if(TIFR & (1<<TOV0)) { TIFR = 1<<TOV0; if(idleRate != 0) { if(idleCounter > 4) { // 22 ms in units of 4 ms idleCounter -= 5; } else { idleCounter = idleRate; keyDidChange = 1; } } } if(keyDidChange && usbInterruptIsReady()) { keyDidChange = 0; // use last key and not current key status in order to avoid lost // changes in key status. buildReport(lastKey); usbSetInterrupt(reportBuffer, sizeof(reportBuffer)); } } return 0; }
int main(void) { uint8_t i; bool timeout = false; RED_LED_OUT(); GREEN_LED_OUT(); RED_LED_OFF(); GREEN_LED_OFF(); usbInit(); usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */ i = 0; while(--i){ /* fake USB disconnect for > 250 ms */ wdt_reset(); _delay_ms(1); } usbDeviceConnect(); sei(); for(;;){ /* main event loop */ wdt_reset(); usbPoll(); if((get_input_requested == true) || ((timeout == true) && (feature_report_buf.ucReportingState != HID_USAGE_SENSOR_PROPERTY_REPORTING_STATE_NO_EVENTS_SEL_ENUM)) ) { if(get_input_requested == true) { get_input_requested = false; input_report_buf.ucEventType = HID_USAGE_SENSOR_EVENT_POLL_RESPONSE_SEL_ENUM; } else if(timeout == true) { timeout = false; input_report_buf.ucEventType = HID_USAGE_SENSOR_EVENT_DATA_UPDATED_SEL_ENUM; input_report_buf.sGyroXValue++; } RED_LED_TOGGLE(); /* called after every poll of the interrupt endpoint */ usbSetInterrupt((void *)&input_report_buf, sizeof(input_report_buf)); } _delay_ms(2); if(i++ == 0) { timeout = true; } } }
void sendDataUSB(uchar* data, unsigned int byteCount) { int currentByte; int currentCount; currentByte = 0; while(currentByte < byteCount) { currentCount = byteCount - currentByte; if(currentCount > 8) currentCount = 8; while(!usbInterruptIsReady()) usbPoll(); usbSetInterrupt(data + currentByte, currentCount*sizeof(uchar)); currentByte += currentCount; } }
void main(void) { usbInit(); usbDeviceDisconnect(); _delay_ms(100); usbDeviceConnect(); sei(); for(;;) { usbPoll(); if(usbInterruptIsReady()) { DDRB ^= 1; hid_report.button ^= 1; usbSetInterrupt((void*)&hid_report, sizeof(hid_report)); } _delay_ms(1); } }
int main() { //--setup changed= 0; PORTB= 0b11111111; //internal pull-ups PORTD= 0b01110011; //internal pull-ups lastPINB= 0; //used to detect changes lastPIND= 0; //used to detect changes wdt_enable(WDTO_1S); //enable 1s watchdog timer usbInit(); usbDeviceDisconnect(); //enforce re-enumeration, do this while interrupts are disabled! uchar i= 0; while(--i) { //fake USB disconnect for > 250 ms wdt_reset(); _delay_ms(1); } usbDeviceConnect(); sei(); //Enable interrupts after re-enumeration //--loop while(1) { wdt_reset(); // keep the watchdog happy usbPoll(); if(PINB!=lastPINB) { changed= 1; lastPINB= PINB; reportBuffer[0]= lastPINB; } if((PIND&0b01110011)!=lastPIND) { changed= 1; lastPIND= PIND&0b01110011; reportBuffer[1]= lastPIND; } if(usbInterruptIsReady()&&changed) { usbSetInterrupt(reportBuffer, sizeof(reportBuffer)); changed= 0; } } return 0; }
int __attribute__((noreturn)) main(void) { uchar i; wdt_enable(WDTO_1S); /* Even if you don't use the watchdog, turn it off here. On newer devices, * the status of the watchdog (on/off, period) is PRESERVED OVER RESET! */ /* RESET status: all port bits are inputs without pull-up. * That's the way we need D+ and D-. Therefore we don't need any * additional hardware initialization. */ usbInit(); usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */ i = 0; while(--i){ /* fake USB disconnect for > 250 ms */ wdt_reset(); _delay_ms(1); } usbDeviceConnect(); RED_LED_OUT(); sei(); for(;;){ /* main event loop */ wdt_reset(); usbPoll(); if(usbInterruptIsReady()){ if(state == SEND) { reportBuffer.keys[0] = 0x52; /* 'UP' */ //reportBuffer.modifier |= (1 << 2); /* SHIFT */ state = NO_KEYS; counter = 0; } usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer)); reportBuffer.keys[0] = 0; /* Empty the report (no keys pressed) */ reportBuffer.modifier = 0; } counter++; if(counter == 30000) { state = SEND; } } }
int main() { uchar buffer,i,lastkey = 0; // Fetch password from EEPROM and send it eeprom_read_block(messageBuffer, stored_password, sizeof(messageBuffer)); messagePtr = 0; messageState = STATE_SEND; for(i=0; i<sizeof(keyboard_report); i++) // clear report initially ((uchar *)&keyboard_report)[i] = 0; wdt_enable(WDTO_1S); // enable 1s watchdog timer init_uart0(); usbInit(); usbDeviceDisconnect(); // enforce re-enumeration for(i = 0; i<250; i++) { // wait 500 ms wdt_reset(); // keep the watchdog happy _delay_ms(2); } usbDeviceConnect(); // TCCR0B |= (1 << CS01); // timer 0 at clk/8 will generate randomness sei(); // Enable interrupts after re-enumeration while(1) { wdt_reset(); // keep the watchdog happy usbPoll(); buffer = UDR0; lastkey = buffer; // characters are sent when messageState == STATE_SEND and after receiving // the initial LED state from PC (good way to wait until device is recognized) if(usbInterruptIsReady() && messageState == STATE_SEND) { messageState = buildReport(lastkey); usbSetInterrupt((void *)&keyboard_report, sizeof(keyboard_report)); } } return 0; }
int main(void){ //cli(); /* Ensure usb interrupts enabled by bootloader alter disconnect of usb */ wdt_enable(WDTO_1S); SCH_Init(); ADM_Init(); UIB_Init(); UIF_Init(); LED_Init(); CRD_Init(); UCP_Init(); OSCCAL_Init(); /* USB Init */ usbInit(); usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */ _delay_ms(500); usbDeviceConnect(); sei(); /* 1 - Keyboard report id 2 - HID feature report id reportBuffer is only used to send keyboard data so, initialize to 1 */ reportBuffer.reportid = 1; for(;; ) { /* main event loop */ wdt_reset(); usbPoll(); ADM_Task(); SCH_Task(); if(usbInterruptIsReady()) { UCP_WriteTask(); LED_Task(); printUpdate(); usbSetInterrupt((void*)&reportBuffer, sizeof(reportBuffer)); } } }
int main(void) { uchar updateNeeded = 0; uchar idleCounter = 0; wdt_enable(WDTO_2S); /* Enable watchdog timer 2s */ hardwareInit(); /* Initialize hardware (I/O) */ //odDebugInit(); usbInit(); /* Initialize USB stack processing */ sei(); /* Enable global interrupts */ for(;;){ /* Main loop */ wdt_reset(); /* Reset the watchdog */ usbPoll(); /* Poll the USB stack */ updateNeeded|=scankeys(); /* Scan the keyboard for changes */ /* Check timer if we need periodic reports */ if(TIFR0 & (1<<TOV0)){ TIFR0 = 1<<TOV0; /* Reset flag */ if(idleRate != 0){ /* Do we need periodic reports? */ if(idleCounter > 4){ /* Yes, but not yet */ idleCounter -= 5; /* 22 ms in units of 4 ms */ }else{ /* Yes, it is time now */ updateNeeded = 1; idleCounter = idleRate; } } } /* If an update is needed, send the report */ if(updateNeeded && usbInterruptIsReady()){ updateNeeded = 0; usbSetInterrupt(reportBuffer, sizeof(reportBuffer)); } } return 0; }