int main(void) { // Make sure our watchdog timer is disabled! wdt_reset(); MCUSR &= ~(1 << WDRF); wdt_disable(); // Start up the USART for serial communications // 25 corresponds to 38400 baud - see datasheet for more values USART_Init(25);// 103 corresponds to 9600, 8 corresponds to 115200 baud, 3 for 250000 // set the prescale for the USB for our 16 MHz clock CPU_PRESCALE(0); // Initialize our USB connection usb_init(); while (!usb_configured()){ LEDon(TXLED); _delay_ms(50); LEDoff(TXLED); _delay_ms(50); } // wait // Wait an extra second for the PC's operating system to load drivers // and do whatever it does to actually be ready for input // This wait also gives the Arduino bootloader time to timeout, // so the serial data you'll be properly aligned. _delay_ms(500); dataForController_t dataToSend; char buttonData1; char buttonData2; char buttonData3; while (1) { // Delay so we're not going too fast _delay_ms(10); // We get our data from the ATmega328p by writing which byte we // want from the dataForController_t, and then wait for the // ATmega328p to send that back to us. // The serialRead(number) function reads the serial port, and the // number is a timeout (in ms) so if there's a transmission error, // we don't stall forever. LEDon(TXLED); flushSerialRead(); serialWrite(0); buttonData1 = serialRead(25); serialWrite(1); buttonData2 = serialRead(25); serialWrite(2); buttonData3 = serialRead(25); serialWrite(3); dataToSend.leftStickX = serialRead(25); serialWrite(4); dataToSend.leftStickY = serialRead(25); serialWrite(5); dataToSend.rightStickX = serialRead(25); serialWrite(6); dataToSend.rightStickY= serialRead(25); serialWrite(7); dataToSend.centerStickX = serialRead(25); serialWrite(8); dataToSend.centerStickY= serialRead(25); LEDoff(TXLED); // Now, we take the button data we got in and input that information // into our controller data we want to send dataToSend.triangleOn = 1 & (buttonData1 >> 0); dataToSend.circleOn = 1 & (buttonData1 >> 1); dataToSend.squareOn = 1 & (buttonData1 >> 2); dataToSend.crossOn = 1 & (buttonData1 >> 3); dataToSend.l1On = 1 & (buttonData1 >> 4); dataToSend.l2On = 1 & (buttonData1 >> 5); dataToSend.l3On = 1 & (buttonData1 >> 6); dataToSend.r1On = 1 & (buttonData1 >> 7); dataToSend.r2On = 1 & (buttonData2 >> 0); dataToSend.r3On = 1 & (buttonData2 >> 1); dataToSend.selectOn = 1 & (buttonData2 >> 2); dataToSend.startOn = 1 & (buttonData2 >> 3); dataToSend.homeOn = 1 & (buttonData2 >> 4); dataToSend.dpadLeftOn = 1 & (buttonData2 >> 5); dataToSend.dpadUpOn = 1 & (buttonData2 >> 6); dataToSend.dpadRightOn = 1 & (buttonData2 >> 7); dataToSend.dpadDownOn = 1 & (buttonData3 >> 0); // Finally, we send the data out via the USB port sendPS3Data(dataToSend); } }
int main(void) { // Make sure our watchdog timer is disabled! wdt_reset(); MCUSR &= ~(1 << WDRF); wdt_disable(); // Start up the USART for serial communications // 25 corresponds to 38400 baud - see datasheet for more values USART_Init(25);// 103 corresponds to 9600, 8 corresponds to 115200 baud, 3 for 250000 // set the prescale for the USB for our 16 MHz clock CPU_PRESCALE(0); // Initialize our USB connection usb_init(); while (!usb_configured()){ LEDon(TXLED); _delay_ms(50); LEDoff(TXLED); _delay_ms(50); } // wait // Wait an extra second for the PC's operating system to load drivers // and do whatever it does to actually be ready for input // This wait also gives the Arduino bootloader time to timeout, // so the serial data you'll be properly aligned. _delay_ms(500); dataForMegaController_t controllerData1; dataForMegaController_t controllerData2; while (1) { // Delay so we're not going too fast _delay_ms(10); // We get our data from the ATmega328p by writing which byte we // want from the dataForController_t, and then wait for the // ATmega328p to send that back to us. // The serialRead(number) function reads the serial port, and the // number is a timeout (in ms) so if there's a transmission error, // we don't stall forever. LEDon(TXLED); flushSerialRead(); int serialIndex = 0; // The buttons are held in an array, so we need to break it between the two controllers for (int i = 0; i < BUTTON_ARRAY_LENGTH; i++){ serialWrite(serialIndex); serialIndex++; controllerData1.buttonArray[i] = serialRead(25); } for (int i = 0; i < BUTTON_ARRAY_LENGTH; i++){ serialWrite(serialIndex); serialIndex++; controllerData2.buttonArray[i] = serialRead(25); } serialWrite(serialIndex); serialIndex++; uint8_t directionButtons = serialRead(25); controllerData1.dpadLeftOn = 1 & (directionButtons >> 0); controllerData1.dpadUpOn = 1 & (directionButtons >> 1); controllerData1.dpadRightOn = 1 & (directionButtons >> 2); controllerData1.dpadDownOn = 1 & (directionButtons >> 3); controllerData2.dpadLeftOn = 1 & (directionButtons >> 4); controllerData2.dpadUpOn = 1 & (directionButtons >> 5); controllerData2.dpadRightOn = 1 & (directionButtons >> 6); controllerData2.dpadDownOn = 1 & (directionButtons >> 7); // Assuming that 16 bit data gets sent high byte first controllerData1.leftStickX = get16bitValue(serialIndex); serialIndex += 2; controllerData1.leftStickY = get16bitValue(serialIndex); serialIndex += 2; controllerData1.rightStickX = get16bitValue(serialIndex); serialIndex += 2; controllerData1.rightStickY = get16bitValue(serialIndex); serialIndex += 2; controllerData1.stick3X = get16bitValue(serialIndex); serialIndex += 2; controllerData1.stick3Y = get16bitValue(serialIndex); serialIndex += 2; controllerData2.leftStickX = get16bitValue(serialIndex); serialIndex += 2; controllerData2.leftStickY = get16bitValue(serialIndex); serialIndex += 2; controllerData2.rightStickX = get16bitValue(serialIndex); serialIndex += 2; controllerData2.rightStickY = get16bitValue(serialIndex); serialIndex += 2; controllerData2.stick3X = get16bitValue(serialIndex); serialIndex += 2; controllerData2.stick3Y = get16bitValue(serialIndex); // Communication with the Arduino chip is over here LEDoff(TXLED); // Finally, we send the data out via the USB port sendControllerDataViaUSB(controllerData1, 1); _delay_ms(10); sendControllerDataViaUSB(controllerData2, 2); } }