-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d4be49
commit 5fddb37
Showing
12 changed files
with
461 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.env.h | ||
index.html | ||
index-html.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# M5Atom WiFi Control Car Example | ||
|
||
M5Atom によるマイコンカーでの Virtual Gamepad 用サンプルプログラムです。 | ||
|
||
## How to RUN | ||
|
||
まずこのディレクトリ (フォルダー) をダウンロードしてください。 | ||
|
||
これをコンパイルするためには、`./index-html.h` というファイルを用意してその中で `index_html` 定数に <https://twosquirrels.github.io/virtual-gamepad/> の HTML を `const char[]` 型で設定する必要があります。 | ||
この作業は Windows の場合は `./gen-index-html.bat` を、Mac 等 bash が動く環境の場合は `./gen-index-html.sh` を実行することで、自動で HTML ファイルをダウンロードしヘッダファイルを生成できるため、それを使うことを推奨します。 | ||
|
||
また WiFi の ssid と password は `./m5atom-car.ino` に直接書き込んでも良いですが、`./.env.h` を作りこのヘッダファイル内で以下のように定数を初期化することもできます。 | ||
|
||
```c | ||
const char ssid[] = "YOUR_SSID"; | ||
const char password[] = "YOUR_PASSWORD"; | ||
``` | ||
|
||
シリアルモニタを開いた状態でスケッチを書き込むと、書き込みが完了しプログラムが実行されたタイミングで M5Atom のローカル IP アドレスが表示されるため、それをスマホ等のブラウザで開いてください。ゲームパッドの画面が開けたら成功です。 | ||
|
||
## Structure | ||
|
||
`./SimpleHTTPServer.h` は ChatGPT で作った Arduino 用の簡易的な HTTP サーバーライブラリです。 | ||
`./m5atom-car.ino` ではジョイスティックのクエリからパラメータをパースし、`updateMoers` 関数でその値から左右のモーターの出力を設定しています。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// SimpleHTTPServer written by ChatGPT | ||
|
||
#include <WiFi.h> | ||
|
||
#define MAX_HANDLERS (16) | ||
|
||
class SimpleHTTPServer { | ||
private: | ||
struct Handler { | ||
const char* path; | ||
void (*handler)(WiFiClient&, const String&); | ||
}; | ||
|
||
const char* ssid; | ||
const char* password; | ||
WiFiServer server; | ||
Handler getHandlers[MAX_HANDLERS]; | ||
Handler postHandlers[MAX_HANDLERS]; | ||
int getHandlerCount; | ||
int postHandlerCount; | ||
|
||
public: | ||
SimpleHTTPServer(const char* ssid, const char* password, int port = 80) | ||
: ssid(ssid), password(password), server(port), getHandlerCount(0), postHandlerCount(0) {} | ||
|
||
void begin() { | ||
Serial.begin(115200); | ||
connectToWiFi(); | ||
server.begin(); | ||
Serial.println("Server started"); | ||
Serial.print("IP Address: "); | ||
Serial.println(WiFi.localIP()); | ||
} | ||
|
||
void handleClient() { | ||
WiFiClient client = server.available(); | ||
if (client) { | ||
Serial.println("New Client."); | ||
String currentLine = ""; | ||
String requestType = ""; | ||
String path = ""; | ||
String queryString = ""; | ||
|
||
while (client.connected()) { | ||
if (client.available()) { | ||
char c = client.read(); | ||
Serial.write(c); | ||
if (c == '\n') { | ||
if (currentLine.length() == 0) { | ||
if (requestType == "GET") { | ||
handleRequest(path, queryString, client, getHandlers, getHandlerCount); | ||
} else if (requestType == "POST") { | ||
handleRequest(path, queryString, client, postHandlers, postHandlerCount); | ||
} | ||
break; | ||
} else { | ||
currentLine = ""; | ||
} | ||
} else if (c != '\r') { | ||
currentLine += c; | ||
if (currentLine.startsWith("GET ")) { | ||
requestType = "GET"; | ||
path = extractPathAndQuery(currentLine, queryString); | ||
} else if (currentLine.startsWith("POST ")) { | ||
requestType = "POST"; | ||
path = extractPathAndQuery(currentLine, queryString); | ||
} | ||
} | ||
} | ||
} | ||
client.stop(); | ||
Serial.println("Client Disconnected."); | ||
} | ||
} | ||
|
||
void get(const char* path, void (*handler)(WiFiClient&, const String&)) { | ||
if (getHandlerCount < MAX_HANDLERS) { | ||
getHandlers[getHandlerCount++] = { path, handler }; | ||
} | ||
} | ||
|
||
void post(const char* path, void (*handler)(WiFiClient&, const String&)) { | ||
if (postHandlerCount < MAX_HANDLERS) { | ||
postHandlers[postHandlerCount++] = { path, handler }; | ||
} | ||
} | ||
|
||
private: | ||
void connectToWiFi() { | ||
Serial.print("Connecting to "); | ||
Serial.println(ssid); | ||
WiFi.begin(ssid, password); | ||
while (WiFi.status() != WL_CONNECTED) { | ||
delay(1000); | ||
Serial.print("."); | ||
} | ||
Serial.println(""); | ||
Serial.println("WiFi connected."); | ||
} | ||
|
||
String extractPathAndQuery(const String& requestLine, String& queryString) { | ||
int firstSpace = requestLine.indexOf(' '); | ||
int secondSpace = requestLine.indexOf(' ', firstSpace + 1); | ||
String fullPath = requestLine.substring(firstSpace + 1, secondSpace); | ||
|
||
int queryIndex = fullPath.indexOf('?'); | ||
if (queryIndex != -1) { | ||
queryString = fullPath.substring(queryIndex + 1); | ||
return fullPath.substring(0, queryIndex); | ||
} else { | ||
queryString = ""; | ||
return fullPath; | ||
} | ||
} | ||
|
||
void handleRequest(const String& path, const String& queryString, WiFiClient& client, Handler* handlers, int handlerCount) { | ||
for (int i = 0; i < handlerCount; i++) { | ||
if (path == handlers[i].path) { | ||
handlers[i].handler(client, queryString); | ||
return; | ||
} | ||
} | ||
defaultResponse(client); | ||
} | ||
|
||
void defaultResponse(WiFiClient& client) { | ||
client.println("HTTP/1.1 404 Not Found"); | ||
client.println("Content-type:text/html"); | ||
client.println(); | ||
client.print("<html><body><h1>404 Not Found</h1></body></html>"); | ||
client.println(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@echo off | ||
setlocal EnableDelayedExpansion | ||
|
||
powershell -Command "(New-Object Net.WebClient).DownloadFile('https://twosquirrels.github.io/virtual-gamepad/', 'index.html')" | ||
|
||
( | ||
echo const char index_html[] = R"***( | ||
type index.html | ||
echo )***"; | ||
) > index-html.h | ||
|
||
echo index-html.h has been created successfully. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
|
||
curl -o index.html https://twosquirrels.github.io/virtual-gamepad/ | ||
|
||
{ | ||
echo 'const char index_html[] = R"***(' | ||
cat index.html | ||
echo ')***";' | ||
} > index-html.h | ||
|
||
echo "index-html.h has been created successfully." |
Oops, something went wrong.