Skip to content

Commit

Permalink
Adding onWait callback to getServers
Browse files Browse the repository at this point in the history
  • Loading branch information
afska committed Feb 5, 2023
1 parent aa46523 commit 03fb983
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 14 additions & 4 deletions examples/LinkWireless_demo/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <tonc.h>
#include <functional>
#include <string>

// (0) Include the header
Expand Down Expand Up @@ -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;
Expand All @@ -145,11 +146,20 @@ void serve() {
}

void connect() {
log("Searching...");
u32 dotsCount, timer = 0;
std::function<void()> 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<LinkWireless::Server> servers;
linkWireless->getServers(servers);
linkWireless->getServers(servers, animate);
CHECK_ERRORS("Search failed :(")

if (servers.size() == 0) {
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 15 additions & 2 deletions lib/LinkWireless.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ class LinkWireless {
}

bool getServers(std::vector<Server>& servers) {
return getServers(servers, []() {});
}

template <typename F>
bool getServers(std::vector<Server>& servers, F onWait) {
LINK_WIRELESS_RESET_IF_NEEDED
if (state != AUTHENTICATED) {
lastError = WRONG_STATE;
Expand All @@ -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 =
Expand Down Expand Up @@ -968,13 +973,21 @@ class LinkWireless {
}

void wait(u32 verticalLines) {
wait(verticalLines, []() {});
}

template <typename F>
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();
}
};
}
Expand Down

0 comments on commit 03fb983

Please sign in to comment.