Advertisement
Guest User

ZeroBoy Teensy Code Julio V1.0

a guest
Nov 3rd, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.41 KB | None | 0 0
  1. /* Teensy mit zwei Modi: Gamepad mit Analogstick und Tastatur mit Maus (PC Modus)
  2.  *  
  3.  *  Standardmäßig ist der Gamepad-Modus aktiviert.
  4.  *  Durch die Tastenkombination: Mode-Button+Select lässt sich zwischen den Modi wechseln
  5.  *  Im PC-Modus wird der Analogstick als Maus erkannt und die normalen Knöpfe werden als Buchstaben ausgegeben. R1 und R2 fungieren als Maustasten.
  6.  *  Weitere Kombos:
  7.  *  Mode+DPAD_UP = Pfeiltaste Hoch
  8.  *  Mode+DPAD_DOWN = Pfeiltaste Runter
  9.  *  Mode+DPAD_LEFT = Pfeiltaste Links
  10.  *  Mode+DPAD_RIGHT = Pfeiltaste Rechts
  11.  *  Mode+B = Enter
  12.  *  Mode+START = Escape
  13.  *  Mode+A/X/Y = Farbwert erhöhen (maximal auf 10, dann springt er wieder auf 0)
  14. */
  15. #include <Bounce.h>
  16. #include <EEPROM.h>
  17. #define NUM_KEYS 15
  18. #define COMMON_ANODE
  19.  
  20. struct Key {
  21.     char keycode;
  22.     Bounce * bounce;
  23. };
  24.  
  25. Key keys[NUM_KEYS];
  26. char volumeLVLs[21];
  27. Key key(char keycode, int pin) {
  28.     Key * ret = new Key;
  29.     ret->keycode = keycode;
  30.     ret->bounce = new Bounce(pin, 10);
  31.     pinMode(pin, INPUT_PULLUP);
  32.     return *ret;
  33. }
  34.  
  35. //Keyboard-Tastenbelegung
  36. void setupKeys() {
  37.     keys[0] = key('a', 0);
  38.     keys[1] = key('w', 1);
  39.     keys[2] = key('s', 2);
  40.     keys[3] = key('d', 3);
  41.     keys[4] = key('m', 4);
  42.     keys[5] = key('n', 5);
  43.     keys[6] = key('b', 6);
  44.     keys[7] = key('v', 7);
  45.     keys[8] = key('y', 8);
  46.     keys[9] = key('x', 9);
  47.     keys[10] = key('q', 10);
  48.     keys[11] = key('e', 11);
  49.     keys[12] = key('u', 12);
  50.     keys[13] = key('l', 13);
  51.     keys[14] = key('k', 14);
  52. }
  53. // Pseudo-Tasten zur Lautstärkeregelung über triggerhappy. Jedem Potentiometer Wert wird ein Button zugeordnet (temporäre Lösung)
  54. void setupVolumeKeys() {
  55.     volumeLVLs[20] = KEY_MINUS;         // 0%
  56.     volumeLVLs[19] = KEY_EQUAL;         // 5%
  57.     volumeLVLs[18] = KEY_LEFT_BRACE;    // 10%
  58.     volumeLVLs[17] = KEY_RIGHT_BRACE;   // 15%
  59.     volumeLVLs[16] = KEY_BACKSLASH;     // 20%
  60.     volumeLVLs[15] = KEY_SEMICOLON;     // 25%
  61.     volumeLVLs[14] = KEY_QUOTE;         // 30%
  62.     volumeLVLs[13] = KEY_TILDE;         // 35%
  63.     volumeLVLs[12] = KEY_COMMA;         // 40%
  64.     volumeLVLs[11] = KEY_PERIOD;        // 45%
  65.     volumeLVLs[10] = KEY_SLASH;         // 50%
  66.     volumeLVLs[9] = KEY_CAPS_LOCK;      // 55%
  67.     volumeLVLs[8] = KEY_END;            // 60%
  68.     volumeLVLs[7] = KEY_INSERT;         // 65%
  69.     volumeLVLs[6] = KEY_V;              // 70%
  70.     volumeLVLs[5] = KEY_T;              // 75%
  71.     volumeLVLs[4] = KEY_P;              // 80%
  72.     volumeLVLs[3] = KEY_SCROLL_LOCK;    // 85%
  73.     volumeLVLs[2] = KEY_PRINTSCREEN;    // 90%
  74.     volumeLVLs[1] = KEY_PAUSE;          // 95%
  75.     volumeLVLs[0] = KEY_I;              // 100%
  76. }
  77.  
  78. //RGB LED-Pins
  79. int redPin = 16;
  80. int greenPin = 17;
  81. int bluePin = 20;
  82.  
  83. //Change LED Color
  84. void setColor(int red, int green, int blue) {
  85.     #ifdef COMMON_ANODE
  86.     red = 255 - red;
  87.     green = 255 - green;
  88.     blue = 255 - blue;
  89.     #endif
  90.     analogWrite(redPin, red);
  91.     analogWrite(greenPin, green);
  92.     analogWrite(bluePin, blue);
  93. }
  94.  
  95. //Mode-Button Kombo-Buttons
  96. Bounce modeButton = Bounce(15, 10);
  97. Bounce upButton = Bounce(1, 10);
  98. Bounce downButton = Bounce(2, 10);
  99. Bounce leftButton = Bounce(0, 10);
  100. Bounce rightButton = Bounce(3, 10);
  101. Bounce selectButton = Bounce(4, 10);
  102. Bounce startButton = Bounce(5, 10);
  103. Bounce xButton = Bounce(9, 10);
  104. Bounce yButton = Bounce(8, 10);
  105. Bounce aButton = Bounce(7, 10);
  106. Bounce bButton = Bounce(6, 10);
  107.  
  108. boolean pcMode = false;
  109. boolean modeButtonPressed = false;
  110. boolean colorChanged = false; // Only write to EPROM if color has changed
  111. const int numButtons = 16; // 16 for Teensy, 32 for Teensy++
  112.  
  113. void setup() {
  114.  
  115.     //Debug-Nachrichten über Serial.println("Nachricht") verschicken
  116.     Serial.begin(9600);
  117.  
  118.     //Setup the LED-Pins
  119.     pinMode(redPin, OUTPUT);
  120.     pinMode(greenPin, OUTPUT);
  121.     pinMode(bluePin, OUTPUT);
  122.  
  123.     setupKeys();
  124.     setupVolumeKeys();
  125.     pinMode(15, INPUT_PULLUP);
  126.     pinMode(16, INPUT_PULLUP);
  127.     Joystick.useManualSend(true);
  128.  
  129.     for (int i = 0; i < numButtons; i++) {
  130.         pinMode(i, INPUT_PULLUP);
  131.     }
  132. }
  133.  
  134. byte allButtons[numButtons];
  135. byte prevButtons[numButtons];
  136. int mapX;
  137. int mapY;
  138. int deadzone = 60;
  139. int volumeLVL;
  140. int deltaVolume = 0;
  141. int volumeKey = 0;
  142. float volumeRel = 0;
  143. int red = EEPROM.read(0);
  144. int green = EEPROM.read(1);
  145. int blue = EEPROM.read(2);
  146.  
  147. void loop() {
  148.  
  149.     //Read potentiometer for volumecontrol
  150.     volumeLVL = analogRead(21);
  151.  
  152.     if (volumeLVL < 4) volumeLVL = 0;
  153.     if (volumeLVL > 1019) volumeLVL = 1023;
  154.  
  155.     //Convert Potentiometer value to array index
  156.     volumeRel = 0.0195503421309873 * volumeLVL;
  157.     deltaVolume = volumeKey - (int) volumeRel;
  158.     volumeKey = (int) volumeRel;
  159.  
  160.     //Check if potentiometer value has changed
  161.     if (deltaVolume != 0) {
  162.         Serial.print("Volume = ");
  163.         Serial.print(volumeKey * 5);
  164.         Serial.println(" %");
  165.         Keyboard.set_key1(volumeLVLs[volumeKey]);
  166.         Keyboard.send_now(); // send the button press
  167.         Keyboard.set_key1(0);
  168.         Keyboard.send_now(); // send the button release
  169.     }
  170.  
  171.     //Update button status
  172.     modeButton.update();
  173.     upButton.update();
  174.     downButton.update();
  175.     leftButton.update();
  176.     rightButton.update();
  177.     selectButton.update();
  178.     startButton.update();
  179.     aButton.update();
  180.     bButton.update();
  181.     xButton.update();
  182.     yButton.update();
  183.  
  184.     //Check if mode-button is pressed
  185.     if (modeButton.fallingEdge()) modeButtonPressed = true;
  186.     if (modeButton.risingEdge()) modeButtonPressed = false;
  187.  
  188.     if (modeButtonPressed && selectButton.fallingEdge()) { //Switch Mode
  189.         Serial.println("Mode changed");
  190.         pcMode = !pcMode;
  191.     }
  192.     if (modeButtonPressed && startButton.fallingEdge()) { //Escape Key
  193.         Serial.println("Escape");
  194.         Keyboard.set_key1(KEY_ESC);
  195.         Keyboard.send_now(); // send the button press
  196.         Keyboard.set_key1(0);
  197.         Keyboard.send_now(); // send the button release
  198.     }
  199.     if (modeButtonPressed && bButton.fallingEdge()) { //Enter
  200.         Serial.println("Enter");
  201.         Keyboard.set_key1(KEY_ENTER);
  202.         Keyboard.send_now(); // send the button press
  203.         Keyboard.set_key1(0);
  204.         Keyboard.send_now(); // send the button release
  205.     }
  206.     if (modeButtonPressed && upButton.fallingEdge()) { //Up
  207.         Serial.println("up");
  208.         Keyboard.set_key1(KEY_UP);
  209.         Keyboard.send_now(); // send the button press
  210.         Keyboard.set_key1(0);
  211.         Keyboard.send_now(); // send the button release
  212.     }
  213.     if (modeButtonPressed && downButton.fallingEdge()) { //Down
  214.         Serial.println("down");
  215.         Keyboard.set_key1(KEY_DOWN);
  216.         Keyboard.send_now(); // send the button press
  217.         Keyboard.set_key1(0);
  218.         Keyboard.send_now(); // send the button release
  219.     }
  220.     if (modeButtonPressed && leftButton.fallingEdge()) { //Left
  221.         Serial.println("left");
  222.         Keyboard.set_key1(KEY_LEFT);
  223.         Keyboard.send_now(); // send the button press
  224.         Keyboard.set_key1(0);
  225.         Keyboard.send_now(); // send the button release
  226.     }
  227.     if (modeButtonPressed && leftButton.fallingEdge()) { //Right
  228.         Serial.println("right");
  229.         Keyboard.set_key1(KEY_RIGHT);
  230.         Keyboard.send_now(); // send the button press
  231.         Keyboard.set_key1(0);
  232.         Keyboard.send_now(); // send the button release
  233.     }
  234.     if (modeButtonPressed && xButton.fallingEdge()) { //Red
  235.         Serial.println("Red changed");
  236.         red += 25;
  237.         if (red > 255) red = 0;
  238.         colorChanged = true;
  239.     }
  240.     if (modeButtonPressed && yButton.fallingEdge()) { //Green
  241.         Serial.println("Green changed");
  242.         green += 25;
  243.         if (green > 255) green = 0;
  244.         colorChanged = true;
  245.     }
  246.     if (modeButtonPressed && aButton.fallingEdge()) { //Blue
  247.         Serial.println("Blue chganged");
  248.         blue += 25;
  249.         if (blue > 255) blue = 0;
  250.         colorChanged = true;
  251.     }
  252.  
  253.     //write RGB-values to EPROM
  254.     if (colorChanged) {
  255.         colorChanged = false;
  256.         EEPROM.write(0, red);
  257.         EEPROM.write(1, green);
  258.         EEPROM.write(2, blue);
  259.     }
  260.  
  261.     setColor(red, green, blue);
  262.  
  263.  
  264.     // Analog Stick Kalibrierung
  265.     // Zu faul meine Kommentare wieder einzudeutschen... :)
  266.  
  267.     //Remap the data range
  268.     mapX = map(analogRead(23), 185, 839, 0, 1023);
  269.     mapY = map(analogRead(22), 125, 830, 0, 1023);
  270.  
  271.     //Deadzones
  272.     if (mapX < 512 + deadzone && mapX > 512 - deadzone) mapX = 512;
  273.     if (mapY < 512 + deadzone && mapY > 512 - deadzone) mapY = 512;
  274.     if (mapX < 24) mapX = 0;
  275.     if (mapY < 24) mapY = 0;
  276.  
  277. /*
  278.     //Non-linear correction
  279.         mapX=(int)(0.00000302*mapX*mapX*mapX-0.004631*mapX*mapX+2.577*mapX+1.23);
  280.       if(mapX>1023) mapX=1023;
  281.       if(mapX<0) mapX=0;
  282.      
  283.       mapY=(int)(0.00000302*mapY*mapY*mapY-0.004631*mapY*mapY+2.577*mapY+1.23);
  284.       if(mapY>1023) mapY=1023;
  285.       if(mapY<0) mapY=0;
  286. */
  287.  
  288.  
  289.     //Send final values
  290.     Joystick.X(mapX);
  291.     Joystick.Y(mapY);
  292.  
  293.     // PC mode
  294.     //Analogstick as mouse
  295.     if (pcMode == true) {
  296.         float X = (float) mapX;
  297.         float Y = (float) mapY;
  298.         float dX = 6 * ((X - 512) / 512); //scale the mousespeed by 6.
  299.         float dY = 6 * ((Y - 512) / 512);
  300.         Mouse.move(dX, dY, 0);
  301.  
  302.         for (int i = 0; i < NUM_KEYS; i++) {
  303.             keys[i].bounce->update();
  304.  
  305.             if (keys[i].bounce->fallingEdge()) {
  306.                 if (i == 13) Mouse.press(MOUSE_RIGHT);
  307.                 else if (i == 14) Mouse.press(MOUSE_LEFT);
  308.                 else Keyboard.press(keys[i].keycode);
  309.             } else if (keys[i].bounce-> risingEdge()) {
  310.                 if (i == 13) Mouse.release(MOUSE_RIGHT);
  311.                 else if (i == 14) Mouse.release(MOUSE_LEFT);
  312.                 else Keyboard.release(keys[i].keycode);
  313.             }
  314.         }
  315.     }
  316.  
  317.     // Gamepad mode
  318.     else {
  319.         // read digital pins and use them for the buttons
  320.         for (int i = 0; i < numButtons; i++) {
  321.             if (digitalRead(i)) {
  322.                 // when a pin reads high, the button is not pressed
  323.                 // the pullup resistor creates the "on" signal
  324.                 allButtons[i] = 0;
  325.             } else {
  326.                 // when a pin reads low, the button is connecting to ground.
  327.                 allButtons[i] = 1;
  328.             }
  329.             Joystick.button(i + 1, allButtons[i]);
  330.         }
  331.         Joystick.send_now();
  332.     }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement