Lufa help

Help a noob, I'm just starting Arduino.

I have a Arduino Uno, and want to reprogram the usb serial converter chip with some stuff from LUFA.
Here's a link.

I want it to function as a keyboard/joystick. I am pretty sure this is possible.
I want to know if I need any additional hardware to program the chip,ETC.
And how could i restore the original program if something goes wrong?

BTW, I have a smd edition if that makes a difference,

Hi,
I've done a bit of reading on this subject because I too, want to get my arduino to make the computer think it is a joystick.
you can dig up a bit more information if you search for something like 'arduino Uno DFU mode' (if you haven't already done so). I'm sorry I couldn't be of more help... I'm a noob as well. (I've just ordered my first arduino)

I think you put it into dfu mode by connecting reset and ground headers on the iscp
Headers for the atmega (something) then use a avr compiler to download the .hex onto the chip

DId you take a look at the links from the playground? InterfacingWithHardware#USB

Yes this is the most useful page:
http://hunt.net.nz/users/darran/
as it has specific instructions. I will try that as soon as i get my joystick and keypad (from adafruit). I was wondering what program to put on the 328. Maybe using serialprint or something similar to send it to the ATMega8u2.
2-Axis Joystick : ID 245 : $9.95 : Adafruit Industries, Unique & fun DIY electronics and kits (joystick)
Membrane 3x4 Matrix Keypad + extras [3x4] : ID 419 : $3.95 : Adafruit Industries, Unique & fun DIY electronics and kits (keypad)

Ok so I was looking through this site http://hunt.net.nz/users/darran/ and found this code for a joystick:

/* Arduino USB Joystick HID demo */

/* Author: Darran Hunt
 * Released into the public domain.
 */

struct {
    int8_t x;
    int8_t y;
    uint8_t buttons;
    uint8_t rfu; 	/* reserved for future use */
} joyReport;

void setup();
void loop();

void setup() 
{
    Serial.begin(115200);
    delay(200);
}

/* Move the joystick in a clockwise square every 5 seconds,
 * and press button 1 then button 2.
 */
void loop() 
{
    int ind;
    delay(5000);

    joyReport.buttons = 0;
    joyReport.x = 0;
    joyReport.y = 0;
    joyReport.rfu = 0;

    /* Move the joystick in a clockwise direction */
    joyReport.x = 100;
    Serial.write((uint8_t *)&joyReport, 4);
    delay(1000);

    joyReport.x = 0;
    joyReport.y = 100;
    Serial.write((uint8_t *)&joyReport, 4);
    delay(1000);

    joyReport.x = -100;
    joyReport.y = 0;
    Serial.write((uint8_t *)&joyReport, 4);
    delay(1000);

    joyReport.x = 0;
    joyReport.y = -100;
    Serial.write((uint8_t *)&joyReport, 4);
    delay(1000);

    /* Send button 1 then button 2 */
    joyReport.y = 0;
    joyReport.buttons = 1;
    Serial.write((uint8_t *)&joyReport, 4);
    delay(1000);

    joyReport.buttons = 2;
    Serial.write((uint8_t *)&joyReport, 4);
    delay(1000);
    
    joyReport.buttons = 0;
    Serial.write((uint8_t *)&joyReport, 4);
}

and this code for a Keyboard:

/* Arduino USB Keyboard HID demo
*
* Sends Volume up, hello world, and Volume Down to the host PC
*
*/

uint8_t buf[8] = { 0 };	/* Keyboard report buffer */

void setup();
void loop();

#define KEY_LEFT_CTRL	0x01
#define KEY_LEFT_SHIFT	0x02
#define KEY_RIGHT_CTRL	0x10
#define KEY_RIGHT_SHIFT	0x20

void setup() 
{
    Serial.begin(9600);
    delay(200);
}

char *str = "+hello world-";

void loop() 
{
    char *chp = str;
    delay(5000);
    while (*chp) {
	    
	if ((*chp >= 'a') && (*chp <= 'z')) {
	    buf[2] = *chp - 'a' + 4;
	} else if ((*chp >= 'A') && (*chp <= 'Z')) {
	    buf[0] = KEY_LEFT_SHIFT;	/* Caps */
	    buf[2] = *chp - 'A' + 4;
	} else {
	    switch (*chp) {
	    case ' ':
	    	buf[2] = 0x2c;	// Space
		break;
	    case '+':
	       buf[2] = 128;	// Volume up
	       break;
	    case '-':
	       buf[2] = 129;	// Volume down
	       break;
	    default:
	        /* Character not handled. To do: add rest of chars from HUT1_11.pdf */
		buf[2] = 0x37;	// Period
		break;
	    }
	}

	Serial.write(buf, 8);	// Send keypress
	buf[0] = 0;
	buf[2] = 0;
	Serial.write(buf, 8);	// Release key
	chp++;
    }
}

It seems I am right, you use serial write for sending data (probably becuase those are the pins connected to the Atmega8u2)
but the code uses several functions i am not familiar with.

Which functions are you unfamiliar with? Serial.write(buf, size) sends the first "size" bytes of the byte array "buf" out the serial port. So all you need to do is set the fields in joyReport (e.g. based on the current inputs from your joystick) and then use Serial.write() to send the report to the atmega8u2 which will in turn send it as a USB HID report to the PC.

So you are taking the information from the joystick and putting that into a byte and sending that through serial?

Yes.

For example, you could have something like this to read inputs from your joystick and send them to the host PC:

struct {
    int8_t x;
    int8_t y;
    uint8_t buttons;
    uint8_t rfu; 	/* reserved for future use */
} joyReport;

/* Declare your pins here */

void setup()
{
    Serial.begin(115200);
    delay(200);

   /* setup your input pins here */
}

void loop() 
{
    delay(20);
    joyReport.x = map(analogRead(myJoystickXPin), 0, 1023, -100, 100);
    joyReport.y = map(analogRead(myJoystickYPin), 0, 1023, -100, 100);
    joyReport.buttons = digitalRead(myJoystickButton1Pin) | digitalRead(myJoystickButton2Pin) << 1;

    Serial.write((uint8_t *)&joyReport, sizeof(joyReport));

}

Ok I see it now, thanks for explaining, I will try this as soon as i get my joystick. Thank you for taking the time to make code.

Ok i uploaded the code to see what i get in the serial port and i get

is that normal?

is that normal?

Yes, if you have the serial monitor baudrate set (lower right hand corner) at a different value then the sketch Serial.begin(xxxx) value used in the sketch.

Lefty

Yes the sketch is sending raw data via the serial port that will look like garbage on the serial monitor. Once you program the atmega8u2 with the HID joystick firmware it will convert that data into HID joystick reports.

Thats what i thought because it slightly varies if i press buttons or move the joystick.