Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for code understanding #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
269 changes: 124 additions & 145 deletions Arcade_Spinner.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,160 +19,170 @@
#include "Mouse.h"
#include <Joystick.h>


#define pinA 2 //The pins that the rotary encoder's A and B terminals are connected to.
#define pinB 3
#define maxBut 6 //The number of buttons you are using up to 10.
// The code preceded by # is evaluated only at compile time.
// so variables used in that code must be created with #define
#define numButtons 6 // The number of buttons you are using. From 0 to 10.


//Create a Joystick object.
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
maxBut, 0, // Button Count, Hat Switch Count
true, true, false, // X, Y, but no Z Axis. We need at least two axes even though they're used.
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering;


//The previous state of the AB pins
volatile int previousReading = 0;

//Keeps track of how much the encoder has been moved
volatile int rotPosition = 0;

volatile int rotMulti = 0;


//Set the initial last state of the buttons depending on the max number of buttons defined in maxBut

#if maxBut==1
int lastButtonState[maxBut] = {1};
// Set the initial last state of the buttons depending on the number of buttons defined in numButtons.
#if numButtons == 1
int lastButtonState[numButtons] = {1};
#endif

#if maxBut==2
int lastButtonState[maxBut] = {1,1};
#if numButtons == 2
int lastButtonState[numButtons] = {1,1};
#endif

#if maxBut==3
int lastButtonState[maxBut] = {1,1,1};
#if numButtons == 3
int lastButtonState[numButtons] = {1,1,1};
#endif

#if maxBut==4
int lastButtonState[maxBut] = {1,1,1,1};
#if numButtons == 4
int lastButtonState[numButtons] = {1,1,1,1};
#endif

#if maxBut==5
int lastButtonState[maxBut] = {1,1,1,1,1};
#if numButtons == 5
int lastButtonState[numButtons] = {1,1,1,1,1};
#endif

#if maxBut==6
int lastButtonState[maxBut] = {1,1,1,1,1,1};
#if numButtons == 6
int lastButtonState[numButtons] = {1,1,1,1,1,1};
#endif

#if maxBut==7
int lastButtonState[maxBut] = {1,1,1,1,1,1,1};
#if numButtons == 7
int lastButtonState[numButtons] = {1,1,1,1,1,1,1};
#endif

#if maxBut==8
int lastButtonState[maxBut] = {1,1,1,1,1,1,1,1};
#if numButtons == 8
int lastButtonState[numButtons] = {1,1,1,1,1,1,1,1};
#endif

#if maxBut==9
int lastButtonState[maxBut] = {1,1,1,1,1,1,1,1,1};
#if numButtons == 9
int lastButtonState[numButtons] = {1,1,1,1,1,1,1,1,1};
#endif

#if maxBut==10
int lastButtonState[maxBut] = {1,1,1,1,1,1,1,1,1,1};
#if numButtons == 10
int lastButtonState[numButtons] = {1,1,1,1,1,1,1,1,1,1};
#endif

void setup() {
//No need to set the pin modes with DDRx = DDRx | 0b00000000 as we're using all input and that's the initial state of the pins
//Use internal input resistors for all the pins we're using
PORTD = 0b11010011; //Digital pins D2, D3, D4, D6, and D12.
PORTB = 0b11110000; //Digital pins D8, D9, D10, D11
//PORTB = 0b01110010; //Digital pins D8, D9, D10, and D15 Pro Micro Craig B
PORTC = 0b11000000; //Digital pin D5 and D13
PORTE = 0b01000000; //Digital pin D7
//PORTF = 0b11000000; //Digital pin A0 & A1
const int rotEncPinA = 3; // Digital pin where the rotary encoder A terminal is connected.
const int rotEncPinB = 2; // Digital pin where the rotary encoder B terminal is connected.

// Volatile vars because they are modified by interruptions (not in main loop), so need to use RAM, because if they are stored in registers their value can be inaccurate.
volatile int rotEncPrevRead = 0; // The previous state of the AB pins.
volatile int rotEncMove = 0; // Keeps track of how much the encoder has been moved.
volatile int rotEncShifted = 0; // Used to shift the rotEncMove value. Explained in later code.

// Create a Joystick object. X and Y must be true to avoid problems detected on Raspberry Pi.
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
numButtons, 0, // Button Count, Hat Switch Count
true, true, false, // X, Y, but no Z Axis. We need at least two axes even though they're not used.
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering;

/**
* The setup
*/
void setup() {
// No need to set the pin modes with DDRx = DDRx | 0b00000000 or pinMode function,
// as we're using all input and that's the default initial state of the pins.

// Set initial state of digital pins using the port registers (We are setting all digital pins to HIGH)
// https://www.arduino.cc/en/Reference/PortManipulation
// Be aware of the differences with used board ATmega 32U4 AKA Arduino Leonardo
// https://www.arduino.cc/en/Hacking/PinMapping32u4

PORTB = PORTB | 0b11110000; // Digital pins D8(bit 4), D9(bit 5), D10(bit 6), D11(bit 7).
PORTD = PORTD | 0b11010011; // Digital pins D2(bit 1), D3 (bit 0), D4(bit 4), D12(bit 6) and D6(bit 7).
PORTC = PORTC | 0b11000000; // Digital pin D5(bit 6) and D13(bit 7).
PORTE = PORTE | 0b01000000; // Digital pin D7(bit 6).

//Start the joystick
// Start the joystick
Joystick.begin();

//Center the X and Y axes on the joystick
Joystick.setXAxis(511);
Joystick.setYAxis(511);

//Set up the interrupt handler for the encoder's A and B terminals on digital pins 2 and 3 respectively. Both interrupts use the same handler.
attachInterrupt(digitalPinToInterrupt(pinA), pinChange, CHANGE);
attachInterrupt(digitalPinToInterrupt(pinB), pinChange, CHANGE);

//Start the mouse
// Start the mouse
Mouse.begin();
}

//Interrupt handler
void pinChange() {

//Set the currentReading variable to the current state of encoder terminals A and B which are conveniently located in bits 0 and 1 (digital pins 2 and 3) of PIND
//This will give us a nice binary number, eg. 0b00000011, representing the current state of the two terminals.
//You could do int currentReading = (digitalRead(pinA) << 1) | digitalRead(pinB); to get the same thing, but it would be much slower.
int currentReading = PIND & 0b00000011;

// Set up the interrupt handler for the encoder's A and B terminals on digital pins 2 and 3 respectively.
// Both interrupts use the same handler.
attachInterrupt(digitalPinToInterrupt(rotEncPinA), rotEncChange, CHANGE); // Any change goes to interruption
attachInterrupt(digitalPinToInterrupt(rotEncPinB), rotEncChange, CHANGE);
}

//Take the nice binary number we got last time there was an interrupt and shift it to the left by 2 then OR it with the current reading.
//This will give us a nice binary number, eg. 0b00001100, representing the former and current state of the two encoder terminals.
/**
* Interrupt handler for rotary encoder changes
*/
void rotEncChange() {

int combinedReading = (previousReading << 2) | currentReading;
// Get the current state of encoder terminals A and B which are conveniently located in bits 0 and 1 (digital pins 2 and 3) of PIND.
// You could do it with [digitalRead] function, but it would be much slower. We use port register to avoid input lags.
int rotEncReading = PIND & 0b00000011; // Use the & operator to apply a mask for retrieve only the digital inputs related to rotary encoder A and B terminals.

//Now that we know the previous and current state of the two terminals we can determine which direction the rotary encoder is turning.
// Take the binary number stored in rotEncReading and shift it to the left by 2
// Example: the result of 0b00000011<<2 will be 0b00001100
// Then OR it with the current reading.
// Result is the previous reading is stored in bits 2 and 3 while current reading in bits 0 and 1.
int rotEncCombinedReading = (rotEncPrevRead << 2) | rotEncReading;

//Going to the right
if(combinedReading == 0b0010 ||
combinedReading == 0b1011 ||
combinedReading == 0b1101 ||
combinedReading == 0b0100) {
// Now that we know the previous state (b2,b3) and current state (b1,b0) of the rotary encoder, we can determine which direction is turning.

// Clockwise (right)
// A B
// t1 0 0
// t2 1 0
// t3 1 1
// t4 0 1

// Counter clockwise (left)
// A B
// t1 0 0
// t2 0 1
// t3 1 1
// t4 1 0

// Going to the right
if(rotEncCombinedReading == 0b0010 ||
rotEncCombinedReading == 0b1011 ||
rotEncCombinedReading == 0b1101 ||
rotEncCombinedReading == 0b0100) {

rotPosition++; //update the position of the encoder

rotEncMove++; // Update the position of the encoder
}

//Going to the left
if(combinedReading == 0b0001 ||
combinedReading == 0b0111 ||
combinedReading == 0b1110 ||
combinedReading == 0b1000) {

rotPosition--; //update the position of the encoder
// Going to the left
if(rotEncCombinedReading == 0b0001 ||
rotEncCombinedReading == 0b0111 ||
rotEncCombinedReading == 0b1110 ||
rotEncCombinedReading == 0b1000) {

rotEncMove--; // Update the position of the encoder
}


//Save the previous state of the A and B terminals for next time
previousReading = currentReading;
// Save the previous state of the A and B terminals for next time
rotEncPrevRead = rotEncReading;
}


/**
* The program loop
*/
void loop(){

int currentButtonState;


//If the encoder has moved 1 or more transitions move the mouse in the appropriate direction
//and update the rotPosition variable to reflect that we have moved the mouse. The mouse will move 1/2
//the number of pixels of the value currently in the rotPosition variable. We are using 1/2 (rotPosition>>1) because the total number
//of transitions(positions) on our encoder is 2400 which is way too high. 1200 positions is more than enough.

if(rotPosition >= 1 || rotPosition <= -1) {
rotMulti = rotPosition>> 1; //copy rotPosition/2 to a temporary variable in case there's an interrupt while we're moving the mouse
Mouse.move(rotMulti,0,0);
rotPosition -= (rotMulti<< 1); //adjust rotPosition to account for mouse movement
// If the encoder has moved 1 or more transitions move the mouse in the appropriate direction
// and update the rotEncMove variable to reflect that we have moved the mouse. The mouse will move 1/2
// the number of pixels of the value currently in the rotPosition variable. We are using 1/2 (rotPosition>>1) because the total number
// of transitions(positions) on our encoder is 2400 which is way too high. 1200 positions is more than enough.

if(rotEncMove != 0) {
rotEncShifted = rotEncMove >> 1; //copy rotPosition/2 to a temporary variable in case there's an interrupt while we're moving the mouse
Mouse.move(rotEncShifted, 0, 0);
rotEncMove -= (rotEncShifted << 1); //adjust rotPosition to account for mouse movement
}



//Iterate through the 10 buttons (0-9) assigning the current state of the pin for each button, HIGH(0b00000001) or LOW(0b00000000), to the currentState variable
// Iterate through the 10 buttons (0-9) assigning the current state of the pin for each button, HIGH(0b00000001) or LOW(0b00000000), to the currentState variable
int button = 0;
do {
switch ( button ) {
switch (button) {
case 0: //on digital pin 4, PD4 - Arcade Button 0
currentButtonState = (PIND & 0b00010000) >> 4; //logical AND the 8-bit pin reading with a mask to isolate the specific bit we're interested in and then shift it to the end of the byte
break;
Expand Down Expand Up @@ -201,51 +211,20 @@ void loop(){
currentButtonState = (PIND & 0b01000000) >> 6;
break;
case 9: //on digital pin 13, PC7 - Arcade Button 9
currentButtonState = (PINC & 0b10000000) >> 6;
currentButtonState = (PINC & 0b10000000) >> 7;
break;
default: //should never happen
currentButtonState = 0b00000000;
break;

/*Pro Micro 10 buttons (2 unused) Craig B
* switch ( button ) {
case 0: //on digital pin 4, PD4 - Arcade Button 1
currentButtonState = (PIND & 0b00010000) >> 4;
break;
case 1: //on digital pin 5, PC6 - Arcade Button 2
currentButtonState = (PINC & 0b01000000) >> 6;
break;
case 2: //on digital pin 6, PD7 - Arcade Button 3
currentButtonState = (PIND & 0b10000000) >> 7;
break;
case 3: //on digital pin 7, PE6 - Arcade Button 4
currentButtonState = (PINE & 0b01000000) >> 6;
break;
case 4: //on digital pin 8, PB4 - Arcade Button 5
currentButtonState = (PINB & 0b00010000) >> 4;
break;
case 5: //on digital pin 9, PB5 - Arcade Button 6
currentButtonState = (PINB & 0b00100000) >> 5;
break;
case 8: //on digital pin 10, PB6 - COIN/Select Button 9
currentButtonState = (PINB & 0b01000000) >> 6;
break;
case 9: //on digital pin 15, PB1 - PLAYER/Start Button 10
currentButtonState = (PINB & 0b00000010) >> 1;
break;
default: //Extra digital pins 16, PB2 and 14, PB3
currentButtonState = 0b00000000;
break;
*/
}
//If the current state of the pin for each button is different than last time, update the joystick button state
// If the current state of the pin for each button is different than last time, update the joystick button state
if(currentButtonState != lastButtonState[button])
Joystick.setButton(button, !currentButtonState);

//Save the last button state for each button for next time
// Save the last button state for each button for next time
lastButtonState[button] = currentButtonState;

++button;
} while (button < maxBut);
} while (button < numButtons);

}