diff --git a/README.md b/README.md index 0d39bc8..867dcea 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,8 @@ Name | Return type | Description `acceptConnections()` | **bool** | Accepts new clients and updates the player count. `connect(serverId)` | **bool** | Starts a connection with `serverId` and changes the state to `CONNECTING`. `keepConnecting()` | **bool** | When connecting, needs to be called until the state is `CONNECTED`. It assigns a player id. -`getServers(servers)` | **bool** | Fills the `servers` vector with all the currently broadcasting servers. +`getServers(servers)` | **bool** | Fills the `servers` vector with all the currently broadcasting servers. This action takes 1 second to complete. +`getServers(servers, onWait)` | **bool** | Like `getServers(servers)`, but accepts an `onWait` function. The library will continuously invoke it each time VBlank starts, to let the user do something while waiting (like updating the screen). `send(data)` | **bool** | Enqueues `data` to be sent to other nodes. Note that this data will be sent in the next `receive(...)` call. `receive(messages)` | **bool** | Sends the pending data and fills the `messages` vector with incoming messages, checking for timeouts and forwarding if needed. This call doesn't block the hardware waiting for messages, it returns if there are no incoming messages. `receive(messages, times)` | **bool** | Performs multiple `receive(...)` calls until successfully exchanging data a number of `times`. This can only be called if `retransmission` is on. diff --git a/examples/LinkWireless_demo/src/main.cpp b/examples/LinkWireless_demo/src/main.cpp index 2c33c71..f8b3717 100644 --- a/examples/LinkWireless_demo/src/main.cpp +++ b/examples/LinkWireless_demo/src/main.cpp @@ -1,4 +1,5 @@ #include +#include #include // (0) Include the header @@ -129,7 +130,7 @@ void serve() { do { u16 keys = ~REG_KEYS & KEY_ANY; if (keys & KEY_SELECT) { - log("Canceled"); + log("Canceled!"); linkWireless->disconnect(); hang(); return; @@ -145,11 +146,20 @@ void serve() { } void connect() { - log("Searching..."); + u32 dotsCount, timer = 0; + std::function animate = [&dotsCount, &timer]() { + if (timer++ % 10 == 0) + dotsCount = 1 + (dotsCount) % 3; + + std::string dots = ""; + for (u32 i = 0; i < dotsCount; i++) + dots += "."; + log("Searching" + dots); + }; // (4) Connect to a server std::vector servers; - linkWireless->getServers(servers); + linkWireless->getServers(servers, animate); CHECK_ERRORS("Search failed :(") if (servers.size() == 0) { @@ -181,7 +191,7 @@ void connect() { while (linkWireless->getState() == LinkWireless::State::CONNECTING) { u16 keys = ~REG_KEYS & KEY_ANY; if (keys & KEY_SELECT) { - log("Canceled"); + log("Canceled!"); linkWireless->disconnect(); hang(); return; diff --git a/lib/LinkWireless.h b/lib/LinkWireless.h index 77c1a4d..3bc77ba 100644 --- a/lib/LinkWireless.h +++ b/lib/LinkWireless.h @@ -238,6 +238,11 @@ class LinkWireless { } bool getServers(std::vector& servers) { + return getServers(servers, []() {}); + } + + template + bool getServers(std::vector& servers, F onWait) { LINK_WIRELESS_RESET_IF_NEEDED if (state != AUTHENTICATED) { lastError = WRONG_STATE; @@ -253,7 +258,7 @@ class LinkWireless { return false; } - wait(LINK_WIRELESS_BROADCAST_SEARCH_WAIT); + wait(LINK_WIRELESS_BROADCAST_SEARCH_WAIT, onWait); auto result = sendCommand(LINK_WIRELESS_COMMAND_BROADCAST_READ_POLL); bool success2 = @@ -968,13 +973,21 @@ class LinkWireless { } void wait(u32 verticalLines) { + wait(verticalLines, []() {}); + } + + template + void wait(u32 verticalLines, F onVBlank) { u32 lines = 0; u32 vCount = REG_VCOUNT; while (lines < verticalLines) { if (REG_VCOUNT != vCount) { - lines++; + lines += std::max((s32)REG_VCOUNT - (s32)vCount, 0); vCount = REG_VCOUNT; + + if (REG_VCOUNT == 160) + onVBlank(); } }; }