|
5 | 5 | #include <AudioFileSourceLittleFS.h>
|
6 | 6 | #include <AudioOutputI2S.h>
|
7 | 7 | #include <AudioGeneratorMP3.h>
|
8 |
| -#include <ArduinoWebsockets.h> |
9 |
| -#include <ArduinoJson.h> |
10 |
| -#include <UUID.h> |
11 |
| -#include <atomic> |
12 | 8 | #include <LittleFS.h>
|
| 9 | +#include <TapToLaunchApi.h> |
13 | 10 | #include "TapToEsp32.hpp"
|
14 | 11 |
|
15 |
| -using namespace websockets; |
16 |
| - |
17 | 12 | //Config found in ReadTag.hpp
|
18 | 13 |
|
19 | 14 | MFRC522 mfrc522(SS_PIN, RST_PIN);
|
20 | 15 | NfcAdapter nfc = NfcAdapter(&mfrc522);
|
21 |
| -boolean requestSent = false; |
| 16 | +TapToLaunchApi client; |
22 | 17 | AudioOutputI2S* out;
|
23 | 18 | boolean wifiEnabled = false;
|
24 | 19 |
|
25 | 20 | void setup() {
|
26 | 21 | Serial.begin(115200);
|
27 |
| - Serial.println("Started"); |
28 | 22 | setupPins();
|
29 | 23 | #ifndef SERIAL_ONLY
|
30 | 24 | initWiFi();
|
| 25 | + client.url(tapToUrl); |
31 | 26 | #endif
|
32 | 27 | SPI.begin(); // Init SPI bus
|
33 | 28 | mfrc522.PCD_Init(); // Init MFRC522
|
@@ -126,54 +121,13 @@ void playAudio(){
|
126 | 121 | #endif
|
127 | 122 | }
|
128 | 123 |
|
129 |
| -bool sendTapTo(String gamePath){ |
| 124 | +bool sendTapTo(String& gamePath){ |
130 | 125 | if(!wifiEnabled) return true;
|
131 |
| - WebsocketsClient client; |
132 |
| - std::atomic<bool> complete(false); |
133 |
| - std::atomic<bool> wasError(false); |
134 |
| - JsonDocument doc; |
135 |
| - UUID uuid; |
136 |
| - const char* id = uuid.toCharArray(); |
137 |
| - doc["jsonrpc"]= "2.0"; |
138 |
| - doc["method"]="launch"; |
139 |
| - doc["id"]= uuid.toCharArray(); |
140 |
| - doc["params"]["text"] = gamePath; |
141 |
| - doc.shrinkToFit(); |
142 |
| - client.onMessage([&complete, &wasError, &id](WebsocketsMessage msg){ |
143 |
| - if(complete.load()) return; |
144 |
| - JsonDocument result; |
145 |
| - DeserializationError error = deserializeJson(result, msg.data()); |
146 |
| - if (error) { |
147 |
| - Serial.print("Failed to parse json"); |
148 |
| - Serial.println(error.c_str()); |
149 |
| - expressError(4); |
150 |
| - complete.store(true); |
151 |
| - wasError.store(true); |
152 |
| - return; |
153 |
| - } |
154 |
| - const char* resultId = result["id"]; |
155 |
| - if(strcmp(id, resultId) != 0) return; |
156 |
| - complete.store(true); |
157 |
| - if(result.containsKey("result")){ |
158 |
| - Serial.print("Error with game path"); |
159 |
| - expressError(3); |
160 |
| - wasError.store(true); |
161 |
| - return; |
162 |
| - } |
163 |
| - }); |
164 |
| - if(!client.connect(tapToUrl)){ |
165 |
| - Serial.println("Unable to connect"); |
166 |
| - expressError(2); |
167 |
| - return false; |
168 |
| - } |
169 |
| - String request; |
170 |
| - serializeJson(doc, request); |
171 |
| - client.send(request); |
172 |
| - while(!complete.load()){ |
173 |
| - client.poll(); |
| 126 | + int code = client.launch(gamePath); |
| 127 | + if(code > 0){ |
| 128 | + expressError(code); |
174 | 129 | }
|
175 |
| - client.close(); |
176 |
| - return !wasError.load(); |
| 130 | + return code == 0; |
177 | 131 | }
|
178 | 132 |
|
179 | 133 | void initWiFi() {
|
@@ -208,16 +162,19 @@ void loop(void) {
|
208 | 162 | for (int i = 3; i < payloadLength; i++) {
|
209 | 163 | payloadAsString += (char)payload[i];
|
210 | 164 | }
|
211 |
| - if(sendTapTo(payloadAsString)){ |
212 |
| - Serial.print("SCAN\t" + payloadAsString + "\n"); |
213 |
| - launchLedOn(0); |
214 |
| - motorOn(0); |
215 |
| - playAudio(); |
216 |
| - motorOff(0); |
217 |
| - launchLedOff(); |
| 165 | + if(!payloadAsString.equalsIgnoreCase("")){ |
| 166 | + if(sendTapTo(payloadAsString)){ |
| 167 | + Serial.print("SCAN\t" + payloadAsString + "\n"); |
| 168 | + Serial.flush(); |
| 169 | + launchLedOn(0); |
| 170 | + motorOn(0); |
| 171 | + playAudio(); |
| 172 | + motorOff(0); |
| 173 | + launchLedOff(); |
| 174 | + } |
| 175 | + nfc.haltTag(); |
| 176 | + delay(1000); |
218 | 177 | }
|
219 |
| - nfc.haltTag(); |
220 |
| - delay(1000); |
221 | 178 | }
|
222 | 179 | }
|
223 | 180 | }
|
0 commit comments