Skip to content

Commit

Permalink
Added wireless version of Enabled Controller and added mouse mode
Browse files Browse the repository at this point in the history
  • Loading branch information
milador committed May 22, 2021
1 parent 17c6de7 commit c257ae0
Show file tree
Hide file tree
Showing 7 changed files with 1,216 additions and 40 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

/** ************************************************************************
* File Name: Enabled_Controller_USB_Software.ino
* Title: Enabled Controller Mini USB Software
* Title: Enabled Controller USB Software
* Developed by: Milad Hajihassan
* Version Number: 1.1 (24/1/2021)
* Version Number: 1.2 (21/5/2021)
* Github Link: https://github.com/milador/Enabled_Controller
***************************************************************************/

Expand All @@ -18,12 +18,15 @@


//Can be changed based on the needs of the users
#define OS_PROFILE 1 //Windows & Android = 1 , macOS = 2
#define JOYSTICK_ENABLED false //Enable Joystick
#define JOYSTICK_DEADZONE 20 //Joystick deadzone
#define JOYSTICK_NUMBER 2 //A1 = 1 , A2 = 2
#define MORSE_TIMEOUT 1000 //Maximum timeout (1000ms =1s)
#define MORSE_REACTION_TIME 10 //Minimum time for a dot or dash switch action ( level 10 : (1.5^1)x10 =15ms , level 1 : (1.5^10)x10=570ms )
#define MOUSE_MOVE_MULTI 2 //Morse mouse move multiplier
#define SWITCH_REACTION_TIME 50 //Minimum time for each switch action ( level 10 : 1x50 =50ms , level 1 : 10x50=500ms )
#define SWITCH_MODE_CHANGE_TIME 2000 //How long to hold switch 4 to change mode
#define SWITCH_MAC_PROFILE false //Windows,Android,iOS=false MacOS=true

#define LED_BRIGHTNESS 150 //The mode led color brightness which is always on ( Use a low value to decrease power usage )
#define LED_ACTION_BRIGHTNESS 150 //The action led color brightness which can be a higher value than LED_BRIGHTNESS
Expand All @@ -44,6 +47,12 @@
#define SWITCH_DOWN_PIN A1
#define SWITCH_LEFT_PIN A0

//Define Joystick pins
#define JOYSTICK_X1_PIN A4
#define JOYSTICK_Y1_PIN A5
#define JOYSTICK_X2_PIN A2
#define JOYSTICK_Y2_PIN A3

EasyMorse morse;

// Variable Declaration
Expand All @@ -53,12 +62,17 @@ int switchAState;
int switchBState;
int switchCState;
int switchDState;

int switchUpState;
int switchRightState;
int switchDownState;
int switchLeftState;


//Declare joystick variables
int joystickX;
int joystickY;
int joystickCenterX;
int joystickCenterY;

//Stopwatches array used to time switch presses
StopWatch timeWatcher[3];
Expand Down Expand Up @@ -111,6 +125,8 @@ typedef struct {
uint8_t switchNumber;
String switchName;
uint8_t switchChar;
uint8_t switchMacChar;
uint8_t switchMouse;
uint8_t switchColorNumber;
uint8_t switchMorseTimeMulti;
} switchStruct;
Expand All @@ -136,29 +152,18 @@ const colorStruct colorProperty[] {
};

//Switch properties
#if (OS_PROFILE == 1)
const switchStruct switchProperty[] {
{1,"DOT",'a',5,1}, //{1=dot,"DOT",'a',5=blue,1=1xMORSE_REACTION}
{2,"DASH",'b',6,3}, //{2=dash,"DASH",'b',6=red,3=3xMORSE_REACTION}
{3,"C",'c',1,1}, //{3,"C",'c',1=green,1=1xMORSE_REACTION}
{4,"D",'d',3,1}, //{4,"D",'d',3=yellow,1=1xMORSE_REACTION}
{5,"UP",KEY_UP_ARROW,3,1}, //{5,"UP",KEY_UP_ARROW,3=yellow,1=1xMORSE_REACTION}
{6,"RIGHT",KEY_RIGHT_ARROW,3,1}, //{6,"RIGHT",KEY_RIGHT_ARROW,3=yellow,1=1xMORSE_REACTION}
{7,"DOWN",KEY_DOWN_ARROW,3,1}, //{7,"DOWN",KEY_DOWN_ARROW,3=yellow,1=1xMORSE_REACTION}
{8,"LEFT",KEY_LEFT_ARROW,3,1} //{8,"LEFT",KEY_LEFT_ARROW,3=yellow,1=1xMORSE_REACTION}
{1,"DOT",'a',KEY_F1,MOUSE_LEFT,5,1}, //{1=dot,"DOT",'a','F1',Left Click,5=blue,1=1xMORSE_REACTION}
{2,"DASH",'b',KEY_F2,MOUSE_RIGHT,6,3}, //{2=dash,"DASH",'b','F2',Right Click,6=red,3=3xMORSE_REACTION}
{3,"C",'c',KEY_F3,0,1,1}, //{3,"C",'c','F3',No Click,1=green,1=1xMORSE_REACTION}
{4,"D",'d',KEY_F4,0,3,1}, //{4,"D",'d','F4',No Click,3=yellow,1=1xMORSE_REACTION}
{5,"UP",KEY_UP_ARROW,KEY_F5,0,4,1}, //{5,"UP",'UP','F5',No Click,4=orange,1=1xMORSE_REACTION}
{6,"RIGHT",KEY_RIGHT_ARROW,KEY_F6,0,4,1}, //{6,"RIGHT",'RIGHT','F6',No Click,4=orange,1=1xMORSE_REACTION}
{7,"DOWN",KEY_DOWN_ARROW,KEY_F7,0,4,1}, //{7,"DOWN",'DOWN','F7',No Click,4=orange,1=1xMORSE_REACTION}
{8,"LEFT",KEY_LEFT_ARROW,KEY_F8,0,4,1}, //{8,"LEFT",'LEFT','F8',No Click,4=orange,1=1xMORSE_REACTION}
{9,"ANALOG",' ',' ',0,4,1} //{9,"ANALOG",'NONE','F9',No Click,4=orange,1=1xMORSE_REACTION}
};
#else if (OS_PROFILE == 2)
const switchStruct switchProperty[] {
{1,"DOT",KEY_F1,5,1}, //{1=dot,"DOT",KEY_F1,5=blue,1=1xMORSE_REACTION}
{2,"DASH",KEY_F2,6,3}, //{2=dash,"DASH",KEY_F2,6=red,3=3xMORSE_REACTION}
{3,"C",KEY_F3,1,1}, //{3,"C",KEY_F3,1=green,1=1xMORSE_REACTION}
{4,"D",KEY_F4,3,1}, //{4,"D",KEY_F4,3=yellow,1=1xMORSE_REACTION}
{5,"UP",KEY_F5,3,1}, //{5,"UP",KEY_F5,3=yellow,1=1xMORSE_REACTION}
{6,"RIGHT",KEY_F6,3,1}, //{6,"RIGHT",KEY_F6,3=yellow,1=1xMORSE_REACTION}
{7,"DOWN",KEY_F7,3,1}, //{7,"DOWN",KEY_F7,3=yellow,1=1xMORSE_REACTION}
{8,"LEFT",KEY_F8,3,1} //{8,"LEFT",KEY_F8,3=yellow,1=1xMORSE_REACTION}
};
#endif



//Settings Action properties
Expand All @@ -174,7 +179,8 @@ const modeStruct modeProperty[] {
{1,"Keyboard Switch",8},
{2,"Morse Keyboard",7},
{3,"Morse Mouse",2},
{4,"Settings",4}
{4,"Mouse",1},
{5,"Settings",4}
};


Expand All @@ -189,6 +195,7 @@ void setup() {
Serial.begin(115200); //Start Serial
Keyboard.begin(); //Starts keyboard emulation
Mouse.begin(); //Starts mouse emulation
initJoystick();
delay(1000);
switchSetup(); //Setup switch
delay(5);
Expand All @@ -202,7 +209,8 @@ void setup() {
pinMode(SWITCH_A_PIN, INPUT_PULLUP);
pinMode(SWITCH_B_PIN, INPUT_PULLUP);
pinMode(SWITCH_C_PIN, INPUT_PULLUP);
pinMode(SWITCH_D_PIN, INPUT_PULLUP);
pinMode(SWITCH_D_PIN, INPUT_PULLUP);

pinMode(SWITCH_UP_PIN, INPUT_PULLUP);
pinMode(SWITCH_RIGHT_PIN, INPUT_PULLUP);
pinMode(SWITCH_DOWN_PIN, INPUT_PULLUP);
Expand Down Expand Up @@ -230,7 +238,15 @@ void loop() {
switchDownState = digitalRead(SWITCH_DOWN_PIN);
switchLeftState = digitalRead(SWITCH_LEFT_PIN);


//Update joystick values based on available joystick number
if(JOYSTICK_ENABLED && JOYSTICK_NUMBER == 1) {
joystickX = analogRead(JOYSTICK_X1_PIN);
joystickY = analogRead(JOYSTICK_Y1_PIN);
} else if (JOYSTICK_ENABLED && JOYSTICK_NUMBER == 2) {
joystickX = analogRead(JOYSTICK_X2_PIN);
joystickY = analogRead(JOYSTICK_Y2_PIN);
}

timePressed = timeNotPressed = 0; //reset time counters
if (!ctr) { //Set previous status of switch four
previousSwitchDState = HIGH;
Expand All @@ -256,6 +272,8 @@ void loop() {
changeSwitchMode();
} else if(switchMode==1) {
keyboardAction(switchAState,switchBState,switchCState,LOW,switchUpState,switchRightState,switchDownState,switchLeftState);
} else if(switchMode==4) {
mouseAction(switchAState,switchBState,switchCState,LOW,switchUpState,switchRightState,switchDownState,switchLeftState,joystickX,joystickY); //Mouse mode
}
}
//Serial.println(switchAState);
Expand All @@ -271,6 +289,9 @@ void loop() {
morseAction(2,switchAState,switchBState); //Mouse Morse mode
break;
case 4:
mouseAction(switchAState,switchBState,switchCState,HIGH,switchUpState,switchRightState,switchDownState,switchLeftState,joystickX,joystickY); //Switch mode
break;
case 5:
settingsAction(switchAState,switchBState); //Settings mode
break;
};
Expand All @@ -286,10 +307,10 @@ void displayFeatureList(void) {
Serial.println(" --- ");
Serial.println("Enabled Controller USB firmware");
Serial.println(" ");
Serial.println("VERSION: 1.1 (24 January 2021)");
Serial.println("VERSION: 1.2 (21 May 2021)");
Serial.println(" ");
Serial.println(" --- ");
Serial.println("Features: Adaptive switch, Morse Keyboard, Morse Mouse");
Serial.println("Features: Adaptive switch, Morse Keyboard, Morse Mouse, and Mouse");
Serial.println(" --- ");
Serial.println(" ");

Expand Down Expand Up @@ -478,41 +499,55 @@ void initLedFeedback(){
delay(5);
}

//***INITIALIZE JOYSTICK INPUTS FUNCTION***//

void initJoystick() {
//Update joystick values based on available joystick number
if(JOYSTICK_ENABLED && JOYSTICK_NUMBER == 1) {
joystickCenterX = analogRead(JOYSTICK_X1_PIN);
joystickCenterY = analogRead(JOYSTICK_Y1_PIN);
} else if (JOYSTICK_ENABLED && JOYSTICK_NUMBER == 2) {
joystickCenterX = analogRead(JOYSTICK_X2_PIN);
joystickCenterY = analogRead(JOYSTICK_Y2_PIN);
}
}


//***ADAPTIVE SWITCH KEYBOARD FUNCTION***//

void keyboardAction(int switch1,int switch2,int switch3,int switch4,int switch5,int switch6,int switch7,int switch8) {
if(!switch1) {
switchFeedback(1,switchMode,switchReactionTime,1);
//Serial.println("Switch A");
Keyboard.press(switchProperty[0].switchChar);
(SWITCH_MAC_PROFILE) ? Keyboard.press(switchProperty[0].switchMacChar) : Keyboard.press(switchProperty[0].switchChar);
} else if(!switch2) {
switchFeedback(2,switchMode,switchReactionTime,1);
//Serial.println("Switch B");
Keyboard.press(switchProperty[1].switchChar);
(SWITCH_MAC_PROFILE) ? Keyboard.press(switchProperty[1].switchMacChar) : Keyboard.press(switchProperty[1].switchChar);
} else if(!switch3) {
switchFeedback(3,switchMode,switchReactionTime,1);
//Serial.println("Switch C");
Keyboard.press(switchProperty[2].switchChar);
(SWITCH_MAC_PROFILE) ? Keyboard.press(switchProperty[2].switchMacChar) : Keyboard.press(switchProperty[2].switchChar);
} else if(!switch4) {
switchFeedback(4,switchMode,switchReactionTime,1);
//Serial.println("Switch D");
Keyboard.press(switchProperty[3].switchChar);
(SWITCH_MAC_PROFILE) ? Keyboard.press(switchProperty[3].switchMacChar) : Keyboard.press(switchProperty[3].switchChar);
} else if(!switch5) {
switchFeedback(5,switchMode,switchReactionTime,1);
//Serial.println("Switch UP");
Keyboard.press(switchProperty[4].switchChar);
(SWITCH_MAC_PROFILE) ? Keyboard.press(switchProperty[4].switchMacChar) : Keyboard.press(switchProperty[4].switchChar);
} else if(!switch6) {
switchFeedback(6,switchMode,switchReactionTime,1);
//Serial.println("Switch RIGHT");
Keyboard.press(switchProperty[5].switchChar);
(SWITCH_MAC_PROFILE) ? Keyboard.press(switchProperty[5].switchMacChar) : Keyboard.press(switchProperty[5].switchChar);
} else if(!switch7) {
switchFeedback(7,switchMode,switchReactionTime,1);
//Serial.println("Switch DOWN");
Keyboard.press(switchProperty[6].switchChar);
(SWITCH_MAC_PROFILE) ? Keyboard.press(switchProperty[6].switchMacChar) : Keyboard.press(switchProperty[6].switchChar);
} else if(!switch8) {
switchFeedback(8,switchMode,switchReactionTime,1);
//Serial.println("Switch LEFT");
Keyboard.press(switchProperty[7].switchChar);
(SWITCH_MAC_PROFILE) ? Keyboard.press(switchProperty[7].switchMacChar) : Keyboard.press(switchProperty[7].switchChar);
}
else
{
Expand All @@ -522,6 +557,58 @@ void keyboardAction(int switch1,int switch2,int switch3,int switch4,int switch5,

}

//***ADAPTIVE SWITCH MOUSE FUNCTION***//

void mouseAction(int switch1,int switch2,int switch3,int switch4,int switch5,int switch6,int switch7,int switch8,int analogX, int analogY) {

int xx = map(analogX, 0, 1023, -127, 127);
int yy = map(analogY, 0, 1023, -127, 127);

if(!switch1) {
switchFeedback(1,switchMode,switchReactionTime,1);
//Serial.println("a");
enterMouse(switchProperty[0].switchMouse,xx,yy);
} else if(!switch2) {
switchFeedback(2,switchMode,switchReactionTime,1);
//Serial.println("b");
enterMouse(switchProperty[1].switchMouse,xx,yy);
} else if(!switch3) {
switchFeedback(3,switchMode,switchReactionTime,1);
//Serial.println("c");
enterMouse(switchProperty[2].switchMouse,xx,yy);
} else if(!switch4) {
switchFeedback(4,switchMode,switchReactionTime,1);
//Serial.println("d");
enterMouse(switchProperty[3].switchMouse,xx,yy);
} else if(!switch5) {
switchFeedback(5,switchMode,switchReactionTime,1);
//Serial.println("Up");
enterMouse(switchProperty[4].switchMouse,xx,yy);
} else if(!switch6) {
switchFeedback(6,switchMode,switchReactionTime,1);
//Serial.println("Right");
enterMouse(switchProperty[5].switchMouse,xx,yy);
} else if(!switch7) {
switchFeedback(7,switchMode,switchReactionTime,1);
//Serial.println("Down");
enterMouse(switchProperty[6].switchMouse,xx,yy);
} else if(!switch8) {
switchFeedback(8,switchMode,switchReactionTime,1);
//Serial.println("Left");
enterMouse(switchProperty[7].switchMouse,xx,yy);
} else if (abs(analogX-joystickCenterX)>=JOYSTICK_DEADZONE || abs(analogY-joystickCenterY)>=JOYSTICK_DEADZONE) {
if (abs(analogX-joystickCenterX)>=JOYSTICK_DEADZONE && abs(analogY-joystickCenterY)>=JOYSTICK_DEADZONE) { enterMouse(0,xx,yy); }
else if (abs(analogX-joystickCenterX)>=JOYSTICK_DEADZONE) { enterMouse(0,xx,0); }
else if (abs(analogY-joystickCenterY)>=JOYSTICK_DEADZONE) { enterMouse(0,0,yy); }
}
else
{
clearMouse();
}
delay(SWITCH_REACTION_TIME);

}

//***MORSE CODE TO MOUSE CONVERT FUNCTION***//

void morseAction(int mode,int switch1,int switch2) {
Expand Down Expand Up @@ -614,7 +701,7 @@ void morseAction(int mode,int switch1,int switch2) {
} else if (mode==2) {
int* mouseAct;
mouseAct=morse.getMouse();
mouseAction((int)mouseAct[0],(int)mouseAct[1],(int)mouseAct[2]); //Perform mouse action if it's in morse mouse mode
enterMouse((int)mouseAct[0],(int)mouseAct[1],(int)mouseAct[2]); //Perform mouse action if it's in morse mouse mode
}

//Clean up morse code and get ready for next character
Expand All @@ -629,7 +716,7 @@ void morseAction(int mode,int switch1,int switch2) {

//***PERFORM MOUSE ACTIONS FUNCTION***//

void mouseAction(int button,int xValue,int yValue) {
void enterMouse(int button,int xValue,int yValue) {
switch (button) {
case 0:
break;
Expand Down Expand Up @@ -703,6 +790,12 @@ void mouseAction(int button,int xValue,int yValue) {

}

//***PERFORM MOUSE ACTIONS FUNCTION***//

void clearMouse() {
Mouse.move(0, 0, 0);
Mouse.release();
}

//***CHANGE SWITCH MODE FUNCTION***//

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// COMMON SETTINGS
// ----------------------------------------------------------------------------------------------
// These settings are used in both SW UART, HW UART and SPI mode
// ----------------------------------------------------------------------------------------------

#define BLUEFRUIT_DEVICE_NAME "Enabled-Controller"
#define BLUEFRUIT_DEVICE_MANUFACTURER "Milador"
#define BLUEFRUIT_DEVICE_MODEL "Switch-Interface"
#define BLUEFRUIT_DEVICE_HID BLE_APPEARANCE_HID_KEYBOARD && BLE_APPEARANCE_HID_MOUSE
Loading

0 comments on commit c257ae0

Please sign in to comment.