Page 1 of 1

Teensy Help

Posted: Fri Jun 03, 2016 4:10 pm
by DE4THRE4P3R
Ok, so i uploaded the code to the teensy and it just keeps spamming "A" like as an input. Any suggestions? My soldering may not be the problem since its reading the input.

Re: Teensy Help

Posted: Fri Jun 03, 2016 6:57 pm
by DirtyBullets
Try this code

Code: Select all

//-----------------------------------------------------
//Script made by Adfruit.com Modded by Doom56.com
//-----------------------------------------------------

#define REPEATRATE 100  // milliseconds

const int pinBtnUp = 1;
const int pinBtnRight = 3;
const int pinBtnDown = 2;
const int pinBtnLeft = 0;

const int pinBtnSelect = 4;
const int pinBtnStart = 5;

const int pinBtnB = 8;
const int pinBtnA = 9;
const int pinBtnY = 7;
const int pinBtnX = 6;

const int pinBtnTrigLeft = 10;
const int pinBtnTrigRight = 11;

const int pinLEDOutput = 12;

//Variables for the states of the SNES buttons
byte buttons[] = {  pinBtnUp, pinBtnRight, pinBtnDown, pinBtnLeft, pinBtnSelect, pinBtnStart,
                    pinBtnB, pinBtnA, pinBtnY, pinBtnX, pinBtnTrigLeft, pinBtnTrigRight
                 }; 
short keys[] = {KEY_UP, KEY_RIGHT, KEY_DOWN, KEY_LEFT, KEY_TAB, KEY_ENTER, KEY_B, KEY_A, KEY_Y, KEY_X, KEY_P, KEY_Q};

#define NUMBUTTONS sizeof(buttons)

typedef void KeyFunction_t(uint8_t c);


// prototypes
void myset_key1(uint8_t c);
void myset_key2(uint8_t c);
void myset_key3(uint8_t c);
void myset_key4(uint8_t c);
void myset_key5(uint8_t c);
void myset_key6(uint8_t c);

KeyFunction_t* buttonActive[NUMBUTTONS];
KeyFunction_t* keyList[] = {myset_key6, myset_key5, myset_key4, myset_key3, myset_key2, myset_key1};
int            keySlot = sizeof(keyList) / sizeof(KeyFunction_t*);

void setup()
{
  //Setup the pin modes.
  pinMode( pinLEDOutput, OUTPUT );

  //Special for the Teensy is the INPUT_PULLUP
  //It enables a pullup resitor on the pin.
  for (byte i=0; i< NUMBUTTONS; i++) {
    pinMode(buttons[i], INPUT_PULLUP);
  }
  
  //Uncomment this line to debug the acceleromter values:
//  Serial.begin();

  for (int i=0; i < NUMBUTTONS; i++) {
    buttonActive[i] = 0;
  }
  
}

void loop()
{
//  //debugging the start button...
  digitalWrite ( pinLEDOutput, digitalRead(pinBtnStart));

  //Progess the SNES controller buttons to send keystrokes.
  fcnProcessButtons();
  
}

//Function to process the buttons from the SNES controller
void fcnProcessButtons()
{ 
  bool keysPressed = false; 
  bool keysReleased = false;
  
  // run through all the buttons
  for (byte i = 0; i < NUMBUTTONS; i++) {
    
    // are any of them pressed?
    if (! digitalRead(buttons[i])) 
    {                              //this button is pressed
      keysPressed = true;
      if (!buttonActive[i])        //was it pressed before?
        activateButton(i);            //no - activate the keypress
    }
    else
    {                              //this button is not pressed
      if (buttonActive[i]) {        //was it pressed before?
        releaseButton(i);            //yes - release the keypress
        keysReleased = true;
      }
    }
  }
  
  if (keysPressed || keysReleased)
    Keyboard.send_now();            //update all the keypresses

}

void activateButton(byte index)
{
  if (keySlot)      //any key slots left?
  {
    keySlot--;                                //Push the keySlot stack
    buttonActive[index] = keyList[keySlot];   //Associate the keySlot function pointer with the button
    (*keyList[keySlot])(keys[index]);         //Call the key slot function to set the key value 
  }
}

void releaseButton(byte index)
{
  keyList[keySlot] = buttonActive[index];    //retrieve the keySlot function pointer
  buttonActive[index] = 0;                   //mark the button as no longer pressed
  (*keyList[keySlot])(0);                    //release the key slot
  keySlot++;                                 //pop the keySlot stack
}

void myset_key1(uint8_t c)
{
  Keyboard.set_key1(c);
}

void myset_key2(uint8_t c)
{
  Keyboard.set_key2(c);
}

void myset_key3(uint8_t c)
{
  Keyboard.set_key3(c);
}

void myset_key4(uint8_t c)
{
  Keyboard.set_key4(c);
}

void myset_key5(uint8_t c)
{
  Keyboard.set_key5(c);
}

void myset_key6(uint8_t c)
{
  Keyboard.set_key6(c);
}
EDIT: forgot to say this script also helps so you can use more buttons at the same time so like you can hold A.B.X.Y and any D direction at the same time,

Re: Teensy Help

Posted: Mon Jun 06, 2016 3:57 am
by Aiolia33Fr
Hi D56,

I need your help please.
Could you write a teensy LC's code for more triggers.
I would like a gameboy zero with L1 R1 and L2 R2.

thanks a lot.

Re: Teensy Help

Posted: Mon Jun 06, 2016 4:53 am
by Fleder
Aiolia33Fr wrote:Hi D56,

I need your help please.
Could you write a teensy LC's code for more triggers.
I would like a gameboy zero with L1 R1 and L2 R2.

thanks a lot.

Code: Select all

//-----------------------------------------------------
//Script made by Adfruit.com Modded by Doom56.com / Fleder
//-----------------------------------------------------

#define REPEATRATE 100  // milliseconds

const int pinBtnUp = 1;
const int pinBtnRight = 3;
const int pinBtnDown = 2;
const int pinBtnLeft = 0;

const int pinBtnSelect = 4;
const int pinBtnStart = 5;

const int pinBtnB = 8;
const int pinBtnA = 9;
const int pinBtnY = 7;
const int pinBtnX = 6;

const int pinBtnTrigLeft = 10;
const int pinBtnTrigRight = 11;

const int pinBtnTrigLeft2 = 13;
const int pinBtnTrigRight2 = 14;

const int pinLEDOutput = 12;

//Variables for the states of the SNES buttons
byte buttons[] = {  pinBtnUp, pinBtnRight, pinBtnDown, pinBtnLeft, pinBtnSelect, pinBtnStart,
                    	pinBtnB, pinBtnA, pinBtnY, pinBtnX, pinBtnTrigLeft, pinBtnTrigRight, pinBtnTrigLeft2, pinBtnTrigRight2
                 }; 
short keys[] = {KEY_UP, KEY_RIGHT, KEY_DOWN, KEY_LEFT, KEY_TAB, KEY_ENTER, KEY_B, KEY_A, KEY_Y, KEY_X, KEY_P, KEY_Q, KEY_H, KEY_J};

#define NUMBUTTONS sizeof(buttons)

typedef void KeyFunction_t(uint8_t c);


// prototypes
void myset_key1(uint8_t c);
void myset_key2(uint8_t c);
void myset_key3(uint8_t c);
void myset_key4(uint8_t c);
void myset_key5(uint8_t c);
void myset_key6(uint8_t c);

KeyFunction_t* buttonActive[NUMBUTTONS];
KeyFunction_t* keyList[] = {myset_key6, myset_key5, myset_key4, myset_key3, myset_key2, myset_key1};
int            keySlot = sizeof(keyList) / sizeof(KeyFunction_t*);

void setup()
{
  //Setup the pin modes.
  pinMode( pinLEDOutput, OUTPUT );

  //Special for the Teensy is the INPUT_PULLUP
  //It enables a pullup resitor on the pin.
  for (byte i=0; i< NUMBUTTONS; i++) {
    pinMode(buttons[i], INPUT_PULLUP);
  }
  
  //Uncomment this line to debug the acceleromter values:
//  Serial.begin();

  for (int i=0; i < NUMBUTTONS; i++) {
    buttonActive[i] = 0;
  }
  
}

void loop()
{
//  //debugging the start button...
  digitalWrite ( pinLEDOutput, digitalRead(pinBtnStart));

  //Progess the SNES controller buttons to send keystrokes.
  fcnProcessButtons();
  
}

//Function to process the buttons from the SNES controller
void fcnProcessButtons()
{ 
  bool keysPressed = false; 
  bool keysReleased = false;
  
  // run through all the buttons
  for (byte i = 0; i < NUMBUTTONS; i++) {
    
    // are any of them pressed?
    if (! digitalRead(buttons[i])) 
    {                              //this button is pressed
      keysPressed = true;
      if (!buttonActive[i])        //was it pressed before?
        activateButton(i);            //no - activate the keypress
    }
    else
    {                              //this button is not pressed
      if (buttonActive[i]) {        //was it pressed before?
        releaseButton(i);            //yes - release the keypress
        keysReleased = true;
      }
    }
  }
  
  if (keysPressed || keysReleased)
    Keyboard.send_now();            //update all the keypresses

}

void activateButton(byte index)
{
  if (keySlot)      //any key slots left?
  {
    keySlot--;                                //Push the keySlot stack
    buttonActive[index] = keyList[keySlot];   //Associate the keySlot function pointer with the button
    (*keyList[keySlot])(keys[index]);         //Call the key slot function to set the key value 
  }
}

void releaseButton(byte index)
{
  keyList[keySlot] = buttonActive[index];    //retrieve the keySlot function pointer
  buttonActive[index] = 0;                   //mark the button as no longer pressed
  (*keyList[keySlot])(0);                    //release the key slot
  keySlot++;                                 //pop the keySlot stack
}

void myset_key1(uint8_t c)
{
  Keyboard.set_key1(c);
}

void myset_key2(uint8_t c)
{
  Keyboard.set_key2(c);
}

void myset_key3(uint8_t c)
{
  Keyboard.set_key3(c);
}

void myset_key4(uint8_t c)
{
  Keyboard.set_key4(c);
}

void myset_key5(uint8_t c)
{
  Keyboard.set_key5(c);
}

void myset_key6(uint8_t c)
{
  Keyboard.set_key6(c);
}
Can't test it myself, but it should work.
Pin 13 and 14 are the new lower shoulder buttons.

Re: Teensy Help

Posted: Mon Jun 06, 2016 5:48 am
by Aiolia33Fr
Thank you Fleder

Re: Teensy Help

Posted: Mon Jun 06, 2016 1:26 pm
by DE4THRE4P3R
It wasn't the code. it was my soldiering. Some of the D pad controls were bridging the ground point so when i get a day off im gonna go back and fix it. Thanks Guys!!