RuiViana

TesteEncoder.ino

Sep 23rd, 2020 (edited)
1,252
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "MegaJoy.h"
  2.  
  3.  
  4. void setup() {
  5.   setupPins();
  6.   setupMegaJoy();
  7.  
  8. }
  9.  
  10. void loop() {
  11.   // Always be getting fresh data
  12.   megaJoyControllerData_t controllerData = getControllerData();
  13.   setControllerData(controllerData);
  14. }
  15.  
  16. void setupPins(void) {
  17.   // Set all the digital pins as inputs
  18.   // with the pull-up enabled, except for the
  19.   // two serial line pins
  20.   for (int i = 6; i <= 54; i++) {
  21.     pinMode(i, INPUT);
  22.     digitalWrite(i, HIGH);
  23.   }
  24.  
  25.   pinMode(2, INPUT);
  26.   pinMode(3, INPUT);
  27.   pinMode(4, INPUT);
  28.   pinMode(5, INPUT);
  29.  
  30. }
  31.  
  32. megaJoyControllerData_t getControllerData(void) {
  33.  
  34.   // Set up a place for our controller data
  35.   //  Use the getBlankDataForController() function, since
  36.   //  just declaring a fresh dataForController_t tends
  37.   //  to get you one filled with junk from other, random
  38.   //  values that were in those memory locations before
  39.   megaJoyControllerData_t controllerData = getBlankDataForMegaController();
  40.   // Since our buttons are all held high and
  41.   //  pulled low when pressed, we use the "!"
  42.   //  operator to invert the readings from the pins
  43.   for (int i = 6; i < 54; i++) {
  44.     controllerData.buttonArray[(i - 2) / 8] |= (!digitalRead(i)) << ((i - 2) % 8);
  45.   }
  46.  
  47.   digitalRead(2) ? (controllerData.analogAxisArray[0] = 1) :0 ;
  48.   digitalRead(3) ? (controllerData.analogAxisArray[1] = 1) :0 ;
  49.  
  50.   // Set the analog sticks
  51.   //  Since analogRead(pin) returns a 10 bit value,
  52.   //  we need to perform a bit shift operation to
  53.   //  lose the 2 least significant bits and get an
  54.   //  8 bit number that we can use
  55.   controllerData.analogAxisArray[2] = 0;
  56.   controllerData.analogAxisArray[3] = 0;
  57.   controllerData.analogAxisArray[4] = 0;
  58.   controllerData.analogAxisArray[5] = 0;
  59.   controllerData.analogAxisArray[6] = 0;
  60.   controllerData.analogAxisArray[7] = 0;
  61.   controllerData.analogAxisArray[8] = 0;
  62.   controllerData.analogAxisArray[9] = 0;
  63.   controllerData.analogAxisArray[10] = 0;
  64.   controllerData.analogAxisArray[11] = 0;
  65.  
  66.   // And return the data!
  67.   return controllerData;
  68. }
RAW Paste Data