Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. //Master
  2. //Throttle Base Pro Micro ATmega32U4
  3. //I2C 12 buttons 4 axis
  4. //1 axis direct
  5.  
  6. #include "Joystick.h"
  7.  
  8. Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
  9.                    JOYSTICK_TYPE_MULTI_AXIS, 12, 0,
  10.                    true, true, true, false, false, false,
  11.                    false, true, false, false, false);
  12.  
  13. const bool testAutoSendMode = true;
  14.  
  15. int throttleInput = 0;
  16. int reverseValue = 0;
  17. int throttleValue = 0;
  18.  
  19. #include <Wire.h>
  20. #include <EasyTransferI2C.h>
  21.  
  22. //create object
  23. EasyTransferI2C ET;
  24.  
  25. struct RECEIVE_DATA_STRUCTURE {
  26.   int frontButton1;
  27.   int frontButton2;
  28.   int frontButton3;
  29.   int thumbButton1;
  30.   int thumbButton2;
  31.   int thumbButton3;
  32.   int encoderButton;
  33.   int aStickButton;
  34.   int dPadN;
  35.   int dPadE;
  36.   int dPadS;
  37.   int dPadW;
  38.   int aStickHorizontal;
  39.   int aStickVertical;
  40.   int aHatHorizontal;
  41.   int aHatVertical;
  42. };
  43.  
  44. //give a name to the group of data
  45. RECEIVE_DATA_STRUCTURE mydata;
  46.  
  47. //define slave i2c address
  48. #define I2C_SLAVE_ADDRESS 9
  49.  
  50. const int ledPin =  17;
  51. int ledState = LOW;
  52.  
  53. void setup() {
  54.   Joystick.setXAxisRange(0, 1023);
  55.   Joystick.setYAxisRange(0, 1023);
  56.   Joystick.setThrottleRange(0, 1023);
  57.   Joystick.setZAxisRange(0, 1023);
  58.   Joystick.begin();
  59.   Wire.begin(I2C_SLAVE_ADDRESS);
  60.   //start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
  61.   ET.begin(details(mydata), &Wire);
  62.   //define handler function on receiving data
  63.   Wire.onReceive(receive);
  64.  
  65.   pinMode(ledPin, OUTPUT);
  66.   Serial.begin(9600);
  67. }
  68.  
  69. void loop() {
  70.   //check and see if a data packet has come in.
  71.   if (ET.receiveData()) {
  72.  
  73.     digitalWrite(ledPin, LOW);
  74.   } else {
  75.     // turn LED off:
  76.  
  77.     Serial.println("No data");
  78.     if (ledState == LOW) {
  79.       ledState = HIGH;
  80.     } else {
  81.       ledState = LOW;
  82.     }
  83.     digitalWrite(ledPin, ledState);
  84.     delay(100);
  85.  
  86.   }
  87.  
  88.   readAnalog();
  89.  
  90.   Joystick.setXAxis(mydata.aStickHorizontal);
  91.   Joystick.setYAxis(mydata.aStickVertical);
  92.   Joystick.setThrottle(throttleValue);
  93.   Joystick.setZAxis(reverseValue);
  94.   Joystick.setButton(0, !mydata.frontButton1);
  95.   Joystick.setButton(1, !mydata.frontButton2);
  96.   Joystick.setButton(2, !mydata.frontButton3);
  97.   Joystick.setButton(3, !mydata.thumbButton1);
  98.   Joystick.setButton(4, !mydata.thumbButton2);
  99.   Joystick.setButton(5, !mydata.thumbButton3);
  100.   Joystick.setButton(6, !mydata.encoderButton);
  101.   Joystick.setButton(7, !mydata.aStickButton);
  102.   Joystick.setButton(8, !mydata.dPadN);
  103.   Joystick.setButton(9, !mydata.dPadE);
  104.   Joystick.setButton(10, !mydata.dPadS);
  105.   Joystick.setButton(11, !mydata.dPadW);
  106. }
  107.  
  108. void readAnalog()
  109. {
  110.   throttleInput = 1023 - analogRead(A7);
  111.  
  112.   if (throttleInput >= 950)
  113.   {
  114.     throttleValue = 1023;
  115.     reverseValue = 512;
  116.   }
  117.   else if (throttleInput >= 532)
  118.   {
  119.     throttleValue = (throttleInput - 512) * 2;
  120.     reverseValue = 512;
  121.   }
  122.   else if (throttleInput <= 50)
  123.   {
  124.     throttleValue = 0;
  125.     reverseValue = 0;
  126.   }
  127.   else if (throttleInput <= 492)
  128.   {
  129.     throttleValue = 0;
  130.     reverseValue = throttleInput;
  131.   }
  132.   else
  133.   {
  134.     throttleValue = 0;
  135.     reverseValue = 512;
  136.   }
  137. }
  138.  
  139.  
  140. void receive(int numBytes) {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement