Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15346

Input registers programming - Sanity check

$
0
0

Hello guys,
I am trying to write some line of code for a project I am working on.
The project is a multi-HID button box based on arduino leo and input shift registers to increase the number of inputs.
When a button is pressed, I want it to emulate a joy button press or a key press depending on the value contained in the dedicated array (keybtn[]).
The pronlem is: if I assign joypad button presses, everithing is ok. If I start assigning key presses, only the one assigned to keybtn[0] works, the other keys being ignored.
Could someone take a look at my mess and give feedback?

#include <SPI.h>
#include <Joystick.h>
#include <Keyboard.h>

#define REG_SWITCH 24 //number of switches and buttons

//SHIFT REGISTERS
  const byte IN_REG = 3; //number of input shift registers

//PINOUT
  const byte inLatchPin = 9; //input registers latch pin
  //SPI MISO 14
  //SPI MOSI 16
  //SPI SCLK 15
  
//inputs variables
  byte imask;
  byte iState;
  int debounceTime = 40; //ms
  byte stateByte[IN_REG];
  byte prevStateByte[IN_REG];
  unsigned long debounce [REG_SWITCH];
  
  //Numbers up to 23 are emulated as joypad buttons, numbers >= 24 are emulated as key presses (ASCII)
  //https://www.arduino.cc/reference/en/language/functions/usb/keyboard/keyboardmodifiers/
  const byte keybtn[REG_SWITCH] = {177 /*ESC*/, 176 /*RETURN*/, 2, 3, 4, 5, 6, 7, 
                                  8, 9, 10, 11, 12, 13, 14, 15, 
                                  16, 17, 18, 19, 20, 21, 22, 23};

//JOYSTICK DEFINITION
//X and Y rotation are L and R triggers (separate axis). 
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, 
  JOYSTICK_TYPE_JOYSTICK, REG_SWITCH, 0, //joy type, button count, hatswitch count
  false, false, false, // X, Y, Z axis
  false, false, false, // X, Y, Z rotation
  false, false, //rudder, throttle
  false, false, false); //accelerator, brake, steering

void setup (){
  pinMode (inLatchPin, OUTPUT);
  digitalWrite (inLatchPin, HIGH);
  //initialize debounce
  for (int a = 0; a < REG_SWITCH; a++){
    debounce[a] = millis();
  }
  SPI.begin ();//start SPI
  Joystick.begin();//Initialize Joystick
  Keyboard.begin();//Initialize keyboard
}  // end of setup

void loop (){
  InRegPulse();
  InRegRead();
  InRegHandle();
}  // end of loop

void InRegHandle(){ //Handle registry inputs (74HC165)
  for (int a = 0; a < IN_REG; a++){
    imask = 0b00000001;
    for (int i = 0; i < 8; i++){
      iState = stateByte[a] & imask;
      if (millis()- debounce[i+(a*8)] > debounceTime && iState != (prevStateByte[a] & imask)){
        debounce[i+(a*8)] = millis();
        if(keybtn[i+(a*8)] < REG_SWITCH){//JOYPAD
          Joystick.setButton(keybtn[i+(a*8)], iState);
          delay(8);
        }
        else{//KEYBOARD
          if (iState == 1){//BUTTON PRESSED
              Keyboard.write(keybtn[i+(a*8)]);
              delay(50);
          }
        }
      }
      imask <<= 1; //move to the next bit
    }
    prevStateByte[a] = stateByte[a];
  }
}

void InRegPulse(){  // pulse the parallel load latch to send bytes serially
  digitalWrite (inLatchPin, LOW);
  //delayMicroseconds(5);  
  digitalWrite (inLatchPin, HIGH);
}

void InRegRead(){ //read inputs states from registers
  for (int a = 0; a < IN_REG; a++){
    stateByte[a] = SPI.transfer (0);//"0" is dummy: you could have any value in
  }
}

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 15346

Trending Articles