1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- #include "UsbJoystick.h"
- #include "PPM.h"
-
- #define CHANNELS 8
- // D3 / PD3 / INT1 for PPM input
- // D2 / PD2 / INT0 used for D+ of USB
- // D4 / PD4 used for D- of USB
-
- unsigned short a2dValue;
- unsigned char high;
- unsigned char low;
- unsigned char temp;
- unsigned char report[8];
-
- void setup(void) {
- //Serial.begin(115200);
- //Serial.println("Init");
-
- ppm.begin(CHANNELS);
- usbDeviceConnect();
- }
-
- void loop(void) {
- UsbJoystick.refresh(); // Let the AVRUSB driver do some houskeeping
- calculateReport(); // Jump to our port read routine that orders the values
- UsbJoystick.sendJoystick(report[0],report[1],report[2],report[3],report[4],report[5],report[6],report[7]); // send the values
- }
-
- void calculateReport() { //The values read from the analog ports have to be ordered in a way the HID protocol wants it; a bit confusing.
- if(!ppm.available()) {
- //Serial.println("abort");
- return;
- }
-
- /*
- Serial.print(ppm.get(0) - MIN_PULSE);
- Serial.print(' ');
- Serial.print(ppm.get(1) - MIN_PULSE);
- Serial.print(' ');
- Serial.print(ppm.get(2) - MIN_PULSE);
- Serial.println();
- */
-
- a2dValue = ppm.get(1) - MIN_PULSE;
- high = a2dValue >> 8;
- low = a2dValue & 255;
- report[0] = low;
- temp = high;
-
- a2dValue = ppm.get(0) - MIN_PULSE;
- high = a2dValue >> 6;
- low = a2dValue & 63;
- report[1] = (low << 2) + temp;
- temp = high;
-
- a2dValue = ppm.get(2) - MIN_PULSE;
- high = a2dValue >> 4;
- low = a2dValue & 15;
- report[2] = (low << 4) + temp;
- temp = high;
-
- a2dValue = ppm.get(3) - MIN_PULSE;
- high = a2dValue >> 2;
- low = a2dValue & 3;
- report[3] = (low << 6) + temp;
- temp = high;
-
- high = 0;
- low = 0;
- report[4] = temp;
- temp = high;
-
- a2dValue = ppm.get(4) - MIN_PULSE;
- high = a2dValue >> 8;
- low = a2dValue & 255;
- report[5] = low + temp;
- temp = high;
-
- a2dValue = ppm.get(5) - MIN_PULSE;
- high = a2dValue >> 6;
- low = a2dValue & 63;
- report[6] = (low << 2) + temp;
- temp = high;
-
- // 4 buttons , tossed around
-
- report[7] = (temp & 15);
-
- }
|