Skip to content

Commit 5a7bd9d

Browse files
committed
first commit playstation esp32 controller code
1 parent f86824c commit 5a7bd9d

File tree

2 files changed

+276
-0
lines changed

2 files changed

+276
-0
lines changed

PlayStation_BLE/.itsybitsy_esp32.test.only

Whitespace-only changes.
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
/*
2+
* Feather ESP32 Bluetooth LE gamepad https://github.com/lemmingDev/ESP32-BLE-Gamepad
3+
* John Park for Adafruit Industries, April 2024
4+
* Deep sleep with wake on START button press
5+
* https://randomnerdtutorials.com/esp32-deep-sleep-arduino-ide-wake-up-sources/
6+
7+
* OTA WiFi uploads
8+
* https://docs.espressif.com/projects/arduino-esp32/en/latest/ota_web_update.html
9+
* Sketch > Compile binary, then http://esp32.local/?userid=admin&pwd=admin
10+
* pick compiled .bin, upload.
11+
*/
12+
13+
#include <Arduino.h>
14+
#include <BleGamepad.h>
15+
#include <Adafruit_NeoPixel.h>
16+
17+
#include <WiFi.h>
18+
#include <WiFiClient.h>
19+
#include <WebServer.h>
20+
#include <ESPmDNS.h>
21+
#include <Update.h>
22+
23+
const char* host = "esp32";
24+
const char* ssid = "xxxxxx"; // your WiFi SSID here
25+
const char* password = "xxxxxx"; // your WiFi password here
26+
WebServer server(80);
27+
28+
/*
29+
* Login page
30+
*/
31+
32+
const char* loginIndex =
33+
"<form name='loginForm'>"
34+
"<table width='20%' bgcolor='A09F9F' align='center'>"
35+
"<tr>"
36+
"<td colspan=2>"
37+
"<center><font size=4><b>ESP32 Login Page</b></font></center>"
38+
"<br>"
39+
"</td>"
40+
"<br>"
41+
"<br>"
42+
"</tr>"
43+
"<tr>"
44+
"<td>Username:</td>"
45+
"<td><input type='text' size=25 name='userid'><br></td>"
46+
"</tr>"
47+
"<br>"
48+
"<br>"
49+
"<tr>"
50+
"<td>Password:</td>"
51+
"<td><input type='Password' size=25 name='pwd'><br></td>"
52+
"<br>"
53+
"<br>"
54+
"</tr>"
55+
"<tr>"
56+
"<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
57+
"</tr>"
58+
"</table>"
59+
"</form>"
60+
"<script>"
61+
"function check(form)"
62+
"{"
63+
"if(form.userid.value=='admin' && form.pwd.value=='admin')"
64+
"{"
65+
"window.open('/serverIndex')"
66+
"}"
67+
"else"
68+
"{"
69+
" alert('Error Password or Username')/*displays error message*/"
70+
"}"
71+
"}"
72+
"</script>";
73+
74+
/*
75+
* Server Index Page
76+
*/
77+
78+
const char* serverIndex =
79+
"<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
80+
"<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
81+
"<input type='file' name='update'>"
82+
"<input type='submit' value='Update'>"
83+
"</form>"
84+
"<div id='prg'>progress: 0%</div>"
85+
"<script>"
86+
"$('form').submit(function(e){"
87+
"e.preventDefault();"
88+
"var form = $('#upload_form')[0];"
89+
"var data = new FormData(form);"
90+
" $.ajax({"
91+
"url: '/update',"
92+
"type: 'POST',"
93+
"data: data,"
94+
"contentType: false,"
95+
"processData:false,"
96+
"xhr: function() {"
97+
"var xhr = new window.XMLHttpRequest();"
98+
"xhr.upload.addEventListener('progress', function(evt) {"
99+
"if (evt.lengthComputable) {"
100+
"var per = evt.loaded / evt.total;"
101+
"$('#prg').html('progress: ' + Math.round(per*100) + '%');"
102+
"}"
103+
"}, false);"
104+
"return xhr;"
105+
"},"
106+
"success:function(d, s) {"
107+
"console.log('success!')"
108+
"},"
109+
"error: function (a, b, c) {"
110+
"}"
111+
"});"
112+
"});"
113+
"</script>";
114+
115+
////////////////////////////////////// GAMEPAD
116+
#define numOfButtons 12
117+
// sleep wake button definition (also update line in setup(): 'esp_sleep_enable_ext0_wakeup(GPIO_NUM_4,0);')
118+
#define BUTTON_PIN_BITMASK 0x10 // start button on RTC GPIO pin 4 which is 0x10 (2^4 in hex)
119+
RTC_DATA_ATTR int bootCount = 0;
120+
121+
BleGamepad bleGamepad("ItsyController", "Adafruit", 100); // name, manufacturer, batt level to start
122+
byte previousButtonStates[numOfButtons];
123+
byte currentButtonStates[numOfButtons];
124+
125+
// ItsyBitsy EPS32: 13, 12, 14, 33, 32, 7, 5, 27, 15, 20, 8, 22, 21, 19, 36, 37, 38, 4, 26, 25
126+
// RTC IO: 13, 12, 14, 33, 32, 27, 15, 36, 37, 38, 4, 26, 25
127+
// pins that act funny: 5, 37, 22
128+
byte buttonPins[numOfButtons] = { 13, 12, 14, 33, 32, 7, 27, 15, 21, 19, 4, 26 }; // ItsyBitsy
129+
byte physicalButtons[numOfButtons] = { 1, 2, 4, 5, 7, 8, 15, 16, 13, 14, 12, 11 }; // controller assignments
130+
// b0, b1, b3, b4, b6, b7, b14, b15, b12, b13, b10, b11
131+
// gampad: O/b0, X/b1, ^/b3, []]/b4, l_trig/b6, r_trig/b7, up/b14 , down/b15 , left/b12 , right/b13, select/b11, start/b10
132+
133+
int last_button_press = millis();
134+
int sleepTime = 30000; // how long is it inactive before going to sleep
135+
136+
// void print_wakeup_reason(){
137+
// esp_sleep_wakeup_cause_t wakeup_reason;
138+
// wakeup_reason = esp_sleep_get_wakeup_cause();
139+
// switch(wakeup_reason)
140+
// {
141+
// case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
142+
// case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
143+
// case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
144+
// case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
145+
// case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
146+
// default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
147+
// }
148+
// }
149+
150+
Adafruit_NeoPixel pixel(1, 0, NEO_GRB + NEO_KHZ800); // Itsy on-board NeoPixel
151+
152+
void setup()
153+
{
154+
Serial.begin(115200);
155+
delay(500);
156+
157+
//Print the wakeup reason for ESP32
158+
// print_wakeup_reason();
159+
esp_sleep_enable_ext0_wakeup(GPIO_NUM_4,0); //1 = High, 0 = Low
160+
161+
for (byte currentPinIndex = 0; currentPinIndex < numOfButtons; currentPinIndex++)
162+
{
163+
pinMode(buttonPins[currentPinIndex], INPUT_PULLUP);
164+
previousButtonStates[currentPinIndex] = HIGH;
165+
currentButtonStates[currentPinIndex] = HIGH;
166+
}
167+
168+
bleGamepad.begin();
169+
delay(100);
170+
pixel.begin();
171+
pixel.clear();
172+
173+
// Connect to WiFi network
174+
WiFi.begin(ssid, password);
175+
Serial.println("");
176+
177+
// Wait for connection
178+
while (WiFi.status() != WL_CONNECTED) {
179+
delay(500);
180+
Serial.print(".");
181+
}
182+
Serial.println("");
183+
Serial.print("Connected to ");
184+
Serial.println(ssid);
185+
Serial.print("IP address: ");
186+
Serial.println(WiFi.localIP());
187+
188+
/*use mdns for host name resolution*/
189+
if (!MDNS.begin(host)) { //http://esp32.local
190+
Serial.println("Error setting up MDNS responder!");
191+
while (1) {
192+
delay(1000);
193+
}
194+
}
195+
Serial.println("mDNS responder started");
196+
/*return index page which is stored in serverIndex */
197+
server.on("/", HTTP_GET, []() {
198+
server.sendHeader("Connection", "close");
199+
server.send(200, "text/html", loginIndex);
200+
});
201+
server.on("/serverIndex", HTTP_GET, []() {
202+
server.sendHeader("Connection", "close");
203+
server.send(200, "text/html", serverIndex);
204+
});
205+
/*handling uploading firmware file */
206+
server.on("/update", HTTP_POST, []() {
207+
server.sendHeader("Connection", "close");
208+
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
209+
ESP.restart();
210+
}, []() {
211+
HTTPUpload& upload = server.upload();
212+
if (upload.status == UPLOAD_FILE_START) {
213+
Serial.printf("Update: %s\n", upload.filename.c_str());
214+
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
215+
Update.printError(Serial);
216+
}
217+
} else if (upload.status == UPLOAD_FILE_WRITE) {
218+
/* flashing firmware to ESP*/
219+
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
220+
Update.printError(Serial);
221+
}
222+
} else if (upload.status == UPLOAD_FILE_END) {
223+
if (Update.end(true)) { //true to set the size to the current progress
224+
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
225+
} else {
226+
Update.printError(Serial);
227+
}
228+
}
229+
});
230+
server.begin();
231+
232+
}
233+
234+
void loop()
235+
{
236+
server.handleClient();
237+
delay(1);
238+
239+
if (bleGamepad.isConnected())
240+
{
241+
pixel.setPixelColor(0, 0x000033);
242+
pixel.show();
243+
244+
for (byte currentIndex = 0; currentIndex < numOfButtons; currentIndex++)
245+
{
246+
currentButtonStates[currentIndex] = digitalRead(buttonPins[currentIndex]);
247+
248+
if (currentButtonStates[currentIndex] != previousButtonStates[currentIndex])
249+
{
250+
last_button_press = millis(); // update last_button_press for sleep timing
251+
252+
if (currentButtonStates[currentIndex] == LOW)
253+
{
254+
bleGamepad.press(physicalButtons[currentIndex]);
255+
}
256+
else
257+
{
258+
bleGamepad.release(physicalButtons[currentIndex]);
259+
}
260+
}
261+
}
262+
263+
if (currentButtonStates != previousButtonStates)
264+
{
265+
for (byte currentIndex = 0; currentIndex < numOfButtons; currentIndex++)
266+
{
267+
previousButtonStates[currentIndex] = currentButtonStates[currentIndex];
268+
}
269+
270+
bleGamepad.sendReport();
271+
}
272+
if (millis() - last_button_press > sleepTime) {
273+
esp_deep_sleep_start();
274+
}
275+
}
276+
}

0 commit comments

Comments
 (0)