mirror of
https://github.com/Astatin3/Remote-Xbox-ESP8266.git
synced 2026-06-09 00:28:03 -06:00
Make code
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
// #include ""
|
||||
|
||||
const char* ssid = "test";
|
||||
const char* password = "test";
|
||||
const int port = 12345;
|
||||
|
||||
WiFiServer server(port);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(10);
|
||||
|
||||
// Configure the ESP8266 as an access point
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAP(ssid, password);
|
||||
|
||||
// Start the server
|
||||
server.begin();
|
||||
|
||||
Serial.println("Access Point created");
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(ssid);
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.softAPIP());
|
||||
Serial.print("Listening on port: ");
|
||||
Serial.println(port);
|
||||
}
|
||||
|
||||
short bytesToShort(byte highByte, byte lowByte) {
|
||||
return (short)((highByte << 8) | lowByte);
|
||||
}
|
||||
|
||||
void bytesToBoolArray(byte byte1, byte byte2, bool boolArray[]) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
boolArray[i] = bitRead(byte1, i);
|
||||
boolArray[i + 8] = bitRead(byte2, i);
|
||||
}
|
||||
}
|
||||
|
||||
short axis_ls_x = 0;
|
||||
short axis_ls_y = 0;
|
||||
short axis_rs_x = 0;
|
||||
short axis_rs_y = 0;
|
||||
short axis_lt = 0;
|
||||
short axis_rt = 0;
|
||||
|
||||
bool btn_A = false;
|
||||
bool btn_B = false;
|
||||
bool btn_X = false;
|
||||
bool btn_Y = false;
|
||||
bool btn_LB = false;
|
||||
bool btn_RB = false;
|
||||
bool btn_LS = false;
|
||||
bool btn_RS = false;
|
||||
bool btn_xbox = false;
|
||||
bool btn_home = false;
|
||||
bool btn_menu = false;
|
||||
bool btn_dpad_up = false;
|
||||
bool btn_dpad_down = false;
|
||||
bool btn_dpad_left = false;
|
||||
bool btn_dpad_right = false;
|
||||
|
||||
void loop() {
|
||||
// Check if a client has connected
|
||||
WiFiClient client = server.available();
|
||||
if (client) {
|
||||
Serial.println("New client connected");
|
||||
|
||||
// Read data from the client
|
||||
while (client.connected()) {
|
||||
if (client.available()) {
|
||||
byte buffer[14];
|
||||
client.readBytes(buffer, 14);
|
||||
|
||||
axis_ls_x = bytesToShort(buffer[0], buffer[1]);
|
||||
axis_ls_y = bytesToShort(buffer[2], buffer[3]);
|
||||
axis_rs_x = bytesToShort(buffer[4], buffer[5]);
|
||||
axis_rs_y = bytesToShort(buffer[6], buffer[7]);
|
||||
axis_lt = bytesToShort(buffer[8], buffer[9]);
|
||||
axis_rt = bytesToShort(buffer[10], buffer[11]);
|
||||
|
||||
bool buttons[16];
|
||||
bytesToBoolArray(buffer[12], buffer[13], buttons);
|
||||
|
||||
|
||||
// if(buttons[0] != btn_A && !btn_A){
|
||||
// Serial.println("A pressed!");
|
||||
// }else if (buttons[0] != btn_A && btn_A){
|
||||
// Serial.println("A Unpressed!");
|
||||
// }
|
||||
|
||||
btn_A = buttons[0];
|
||||
btn_B = buttons[1];
|
||||
btn_X = buttons[2];
|
||||
btn_Y = buttons[3];
|
||||
btn_LB = buttons[4];
|
||||
btn_RB = buttons[5];
|
||||
btn_LS = buttons[6];
|
||||
btn_RS = buttons[7];
|
||||
btn_xbox = buttons[8];
|
||||
btn_home = buttons[9];
|
||||
btn_menu = buttons[10];
|
||||
btn_dpad_up = buttons[11];
|
||||
btn_dpad_down = buttons[12];
|
||||
btn_dpad_left = buttons[13];
|
||||
btn_dpad_right = buttons[14];
|
||||
|
||||
printController();
|
||||
}
|
||||
}
|
||||
|
||||
// Close the connection
|
||||
client.stop();
|
||||
Serial.println("Client disconnected");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
String repeatString(String str, int count) {
|
||||
String result = "";
|
||||
for (int i = 0; i < count; i++) {
|
||||
result += str;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
String leftPad(String str, int targetWidth){
|
||||
return repeatString(" ", (targetWidth - str.length())) + str;
|
||||
}
|
||||
|
||||
String printButton(String str, bool isPressed){
|
||||
if(isPressed){
|
||||
return str;
|
||||
}else{
|
||||
return repeatString("-", str.length());
|
||||
}
|
||||
}
|
||||
|
||||
void printController(){
|
||||
Serial.println("LS:[" +
|
||||
leftPad(String(axis_ls_x), 6) + ", " + leftPad(String(axis_ls_y),6) + "] RS:[" +
|
||||
leftPad(String(axis_rs_x), 6) + ", " + leftPad(String(axis_rs_y),6) + "] LT:" +
|
||||
leftPad(String(axis_lt), 4) + ", RT:" +
|
||||
leftPad(String(axis_rt), 4) + " [" +
|
||||
printButton("A", btn_A) +
|
||||
printButton("B", btn_B) +
|
||||
printButton("X", btn_X) +
|
||||
printButton("Y", btn_Y) +
|
||||
printButton("LB", btn_LB) +
|
||||
printButton("RB", btn_RB) +
|
||||
printButton("LS", btn_LS) +
|
||||
printButton("RS", btn_RS) +
|
||||
printButton("@", btn_xbox) +
|
||||
printButton("H", btn_home) +
|
||||
printButton("M", btn_menu) +
|
||||
printButton("^", btn_dpad_up) +
|
||||
printButton("v", btn_dpad_down) +
|
||||
printButton("<", btn_dpad_left) +
|
||||
printButton(">", btn_dpad_right) + "]");
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
import socket
|
||||
from evdev import list_devices, InputDevice, categorize, ecodes
|
||||
|
||||
#CENTER_TOLERANCE = 350
|
||||
|
||||
TCP_IP = '192.168.4.1'
|
||||
TCP_PORT = 12345
|
||||
|
||||
|
||||
try:
|
||||
dev = InputDevice( list_devices()[0] )
|
||||
except:
|
||||
print("Controller Issue!")
|
||||
exit()
|
||||
|
||||
axisRef = {
|
||||
0: 'ls-x',
|
||||
1: 'ls-y',
|
||||
2: 'lt',
|
||||
3: 'rs-x',
|
||||
4: 'rs-y',
|
||||
5: 'rt'
|
||||
}
|
||||
|
||||
btnRef = {
|
||||
304: 'A',
|
||||
305: 'B',
|
||||
307: 'X',
|
||||
308: 'Y',
|
||||
310: 'LB',
|
||||
311: 'RB',
|
||||
317: 'LS',
|
||||
318: 'RS',
|
||||
316: 'xbox',
|
||||
314: 'home',
|
||||
315: 'menu'
|
||||
}
|
||||
|
||||
controllerState = {
|
||||
'ls-x': 0,
|
||||
'ls-y': 0,
|
||||
'rs-x': 0,
|
||||
'rs-y': 0,
|
||||
'lt': 0,
|
||||
'rt': 0,
|
||||
'dpad-up': False,
|
||||
'dpad-down': False,
|
||||
'dpad-left': False,
|
||||
'dpad-right': False,
|
||||
'A': False,
|
||||
'B': False,
|
||||
'X': False,
|
||||
'Y': False,
|
||||
'LB': False,
|
||||
'RB': False,
|
||||
'LS': False,
|
||||
'RS': False,
|
||||
'xbox': False,
|
||||
'home': False,
|
||||
'menu': False
|
||||
}
|
||||
|
||||
def leftPad(string:str, targetNum:int):
|
||||
return (' ' * (targetNum - len(string))) + string
|
||||
|
||||
def btnText(string:str, boolean:bool):
|
||||
if boolean:
|
||||
return string
|
||||
else:
|
||||
return '-' * len(string)
|
||||
|
||||
|
||||
def printState():
|
||||
print(f"\rLS:[{leftPad(str(controllerState['ls-x']), 6)}, {leftPad(str(controllerState['ls-y']), 6)}], " + # Left stick
|
||||
f"RS:[{leftPad(str(controllerState['rs-x']), 6)}, {leftPad(str(controllerState['rs-y']), 6)}], " + # Right stick
|
||||
f"LT:{leftPad(str(controllerState['lt']), 4)}, " +
|
||||
f"RT:{leftPad(str(controllerState['rt']), 4)} [" +
|
||||
btnText("A", controllerState['A']) +
|
||||
btnText("B", controllerState['B']) +
|
||||
btnText("X", controllerState['X']) +
|
||||
btnText("Y", controllerState['Y']) +
|
||||
btnText("LB", controllerState['LB']) +
|
||||
btnText("RB", controllerState['RB']) +
|
||||
btnText("LS", controllerState['LS']) +
|
||||
btnText("RS", controllerState['RS']) +
|
||||
btnText("@", controllerState['xbox']) +
|
||||
btnText("H", controllerState['home']) +
|
||||
btnText("M", controllerState['menu']) +
|
||||
btnText("^", controllerState['dpad-up']) +
|
||||
btnText("v", controllerState['dpad-down']) +
|
||||
btnText("<", controllerState['dpad-left']) +
|
||||
btnText(">", controllerState['dpad-right']), end="] ")
|
||||
|
||||
def doDPAD(event):
|
||||
if event.code == 16:
|
||||
if event.value == -1:
|
||||
controllerState['dpad-left'] = True
|
||||
controllerState['dpad-right'] = False
|
||||
elif event.value == 0:
|
||||
controllerState['dpad-left'] = False
|
||||
controllerState['dpad-right'] = False
|
||||
elif event.value == 1:
|
||||
controllerState['dpad-left'] = False
|
||||
controllerState['dpad-right'] = True
|
||||
elif event.code == 17:
|
||||
if event.value == -1:
|
||||
controllerState['dpad-up'] = True
|
||||
controllerState['dpad-down'] = False
|
||||
elif event.value == 0:
|
||||
controllerState['dpad-up'] = False
|
||||
controllerState['dpad-down'] = False
|
||||
elif event.value == 1:
|
||||
controllerState['dpad-up'] = False
|
||||
controllerState['dpad-down'] = True
|
||||
|
||||
|
||||
def toShort(num):
|
||||
if num > 65535:
|
||||
raise OverflowError
|
||||
return num.to_bytes(2, 'big', signed=True)
|
||||
|
||||
def getButtonBytes():
|
||||
buttons = [
|
||||
controllerState['A'],
|
||||
controllerState['B'],
|
||||
controllerState['X'],
|
||||
controllerState['Y'],
|
||||
controllerState['LB'],
|
||||
controllerState['RB'],
|
||||
controllerState['LS'],
|
||||
controllerState['RS'],
|
||||
controllerState['xbox'],
|
||||
controllerState['home'],
|
||||
controllerState['menu'],
|
||||
controllerState['dpad-up'],
|
||||
controllerState['dpad-down'],
|
||||
controllerState['dpad-left'],
|
||||
controllerState['dpad-right'],
|
||||
False # Unused button
|
||||
]
|
||||
data = 0
|
||||
for i in range(16):
|
||||
data |= buttons[i] << i
|
||||
return data.to_bytes(2, "little", signed=True)
|
||||
|
||||
|
||||
def getBytes():
|
||||
return toShort(controllerState['ls-x']) + \
|
||||
toShort(controllerState['ls-y']) + \
|
||||
toShort(controllerState['rs-x']) + \
|
||||
toShort(controllerState['rs-y']) + \
|
||||
toShort(controllerState['lt']) + \
|
||||
toShort(controllerState['rt']) + \
|
||||
getButtonBytes()
|
||||
|
||||
|
||||
|
||||
try:
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.connect((TCP_IP, TCP_PORT))
|
||||
for event in dev.read_loop():
|
||||
if event.type == ecodes.EV_KEY:
|
||||
controllerState[btnRef[event.code]] = event.value
|
||||
|
||||
elif event.type == ecodes.EV_ABS:
|
||||
|
||||
if event.code in [16, 17]:
|
||||
doDPAD(event)
|
||||
else:
|
||||
controllerState[axisRef[event.code]] = event.value
|
||||
|
||||
printState()
|
||||
s.send(getBytes())
|
||||
except:
|
||||
print("\n\n\nClient not connected!")
|
||||
Reference in New Issue
Block a user