0% found this document useful (0 votes)
11 views3 pages

Yoke Arduino Code

The document defines structures and functions for reading button and axis values and sending reports via USB HID to emulate a joystick. It contains defines for the number of buttons and axes, structures for joystick reports, and functions for setting/clearing buttons, reading inputs, and sending reports.

Uploaded by

dinew66518
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Yoke Arduino Code

The document defines structures and functions for reading button and axis values and sending reports via USB HID to emulate a joystick. It contains defines for the number of buttons and axes, structures for joystick reports, and functions for setting/clearing buttons, reading inputs, and sending reports.

Uploaded by

dinew66518
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#undef DEBUG

#define NUM_BUTTONS 40 // you don't need to change this value


#define NUM_AXES 8 // 6 axes to UNO, and 8 to MEGA. If you are using UNO,
don't need to change this value.

typedef struct joyReport_t {


int16_t axis[NUM_AXES];
uint8_t button[(NUM_BUTTONS + 7) / 8]; // 8 buttons per byte
} joyReport_t;

joyReport_t joyReport;

uint8_t btn[12];
int fulloff = 0;
void setup(void);
void loop(void);
void setButton(joyReport_t *joy, uint8_t button);
void clearButton(joyReport_t *joy, uint8_t button);
void sendJoyReport(joyReport_t *report);

void setup()
{
//set pin to input Button
for ( int portId = 02; portId < 13; portId ++ )
{
pinMode( portId, INPUT_PULLUP);
}
Serial.begin(115200);
delay(200);

for (uint8_t ind = 0; ind < 8; ind++) {


joyReport.axis[ind] = ind * 1000;
}
for (uint8_t ind = 0; ind < sizeof(joyReport.button); ind++) {
joyReport.button[ind] = 0;
}
}

// Send an HID report to the USB interface


void sendJoyReport(struct joyReport_t *report)
{
#ifndef DEBUG
Serial.write((uint8_t *)report, sizeof(joyReport_t));
#else
// dump human readable output for debugging
for (uint8_t ind = 0; ind < NUM_AXES; ind++) {
Serial.print("axis[");
Serial.print(ind);
Serial.print("]= ");
Serial.print(report->axis[ind]);
Serial.print(" ");
}
Serial.println();
for (uint8_t ind = 0; ind < NUM_BUTTONS / 8; ind++) {
Serial.print("button[");
Serial.print(ind);
Serial.print("]= ");
Serial.print(report->button[ind], HEX);
Serial.print(" ");
}
Serial.println();
#endif
}

// turn a button on
void setButton(joyReport_t *joy, uint8_t button)
{
uint8_t index = button / 8;
uint8_t bit = button - 8 * index;

joy->button[index] |= 1 << bit;


}

// turn a button off


void clearButton(joyReport_t *joy, uint8_t button)
{
uint8_t index = button / 8;
uint8_t bit = button - 8 * index;

joy->button[index] &= ~(1 << bit);


}

/*
Read Digital port for Button
Read Analog port for axis
*/
void loop()
{

for (int bt = 1; bt < 13; bt ++)


{
// btn[bt] = digitalRead(bt + 1);
btn[bt] = LOW;
}

for (int on = 01; on < 13; on++)


{
if (btn[on] == LOW)
{
setButton(&joyReport, on + 16);

delay(1);
}
for (int on = 01; on < 13; on++)
{
if (btn[on] == HIGH)
{
clearButton(&joyReport, on + 16);
}

}
}
joyReport.axis[0] = map(analogRead(0), 0, 1023, -32768, 32767 );
joyReport.axis[1] = map(analogRead(1), 0, 1023, -32768, 32767 );
joyReport.axis[2] = 0;
joyReport.axis[3] = 0;
joyReport.axis[4] = 0;
joyReport.axis[5] = 0;
joyReport.axis[6] = 0;
joyReport.axis[7] = 0;
joyReport.axis[8] = 0;

//Send Data to HID


sendJoyReport(&joyReport);

delay(35);
fulloff = 0;
}

You might also like