Fix console updates

This commit is contained in:
averne 2019-02-06 17:55:57 +01:00
parent 443e8201b8
commit f97dcd6e22

View File

@ -9,11 +9,6 @@
// See also libnx nfc.h. // See also libnx nfc.h.
#define PRINT_UPD(fmt, ...) ({ \
printf(fmt, ## __VA_ARGS__); \
consoleUpdate(NULL); \
})
// Indefinitely wait for an event to be signaled // Indefinitely wait for an event to be signaled
// Break when + is pressed, or if the application should quit (in this case, return value will be non-zero) // Break when + is pressed, or if the application should quit (in this case, return value will be non-zero)
Result eventWaitLoop(Event *event) { Result eventWaitLoop(Event *event) {
@ -27,6 +22,7 @@ Result eventWaitLoop(Event *event) {
return rc; return rc;
} }
// Print raw data as hexadecimal numbers.
void print_hex(void *buf, size_t size) { void print_hex(void *buf, size_t size) {
u8 *data = (u8 *)buf; u8 *data = (u8 *)buf;
for (size_t i=0; i<size; i++) for (size_t i=0; i<size; i++)
@ -47,10 +43,10 @@ int main(int argc, char* argv[])
// take a look at the graphics/opengl set of examples, which uses EGL instead. // take a look at the graphics/opengl set of examples, which uses EGL instead.
consoleInit(NULL); consoleInit(NULL);
PRINT_UPD("NFC example program.\n"); printf("NFC example program.\n");
printf("Scan an amiibo tag to display its character.\n");
PRINT_UPD("Scan an amiibo tag to display its character.\n"); printf("Press + to exit.\n\n");
PRINT_UPD("Press + to exit.\n\n"); consoleUpdate(NULL);
// Initialize the nfp:user and nfc:user services. // Initialize the nfp:user and nfc:user services.
rc = nfpuInitialize(); rc = nfpuInitialize();
@ -67,7 +63,8 @@ int main(int argc, char* argv[])
// Wait for a change in availability. // Wait for a change in availability.
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
PRINT_UPD("NFC is disabled. Please turn off plane mode via the quick settings to continue.\n"); printf("NFC is disabled. Please turn off plane mode via the quick settings to continue.\n");
consoleUpdate(NULL);
rc = eventWaitLoop(&availability_change_event); rc = eventWaitLoop(&availability_change_event);
} }
@ -97,14 +94,18 @@ int main(int argc, char* argv[])
// Start the detection of tags. // Start the detection of tags.
rc = nfpuStartDetection(controller); rc = nfpuStartDetection(controller);
if (R_SUCCEEDED(rc)) if (R_SUCCEEDED(rc)) {
PRINT_UPD("Scanning for a tag...\n"); printf("Scanning for a tag...\n");
consoleUpdate(NULL);
}
// Wait until a tag is detected. // Wait until a tag is detected.
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
rc = eventWaitLoop(&activate_event); rc = eventWaitLoop(&activate_event);
if (R_SUCCEEDED(rc)) if (R_SUCCEEDED(rc)) {
PRINT_UPD("A tag was detected, please do not remove it from the NFC spot.\n"); printf("A tag was detected, please do not remove it from the NFC spot.\n");
consoleUpdate(NULL);
}
} }
// If a tag was successfully detected, load it into memory. // If a tag was successfully detected, load it into memory.
@ -116,19 +117,22 @@ int main(int argc, char* argv[])
NfpuModelInfo model_info = {0}; NfpuModelInfo model_info = {0};
rc = nfpuGetModelInfo(controller, &model_info); rc = nfpuGetModelInfo(controller, &model_info);
if (R_SUCCEEDED(rc)) if (R_SUCCEEDED(rc)) {
PRINT_UPD("Amiibo ID: "); printf("Amiibo ID: ");
print_hex(model_info.amiibo_id, 8); print_hex(model_info.amiibo_id, 8);
consoleUpdate(NULL);
}
} }
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
PRINT_UPD("You can now remove the tag.\n"); printf("You can now remove the tag.\n");
consoleUpdate(NULL);
eventWaitLoop(&deactivate_event); eventWaitLoop(&deactivate_event);
} }
// If an error happened during detection/reading, print it. // If an error happened during detection/reading, print it.
if (R_FAILED(rc)) if (R_FAILED(rc))
PRINT_UPD("Error: 0x%x.\n", rc); printf("Error: 0x%x.\n", rc);
// Unmount the tag. // Unmount the tag.
nfpuUnmount(controller); nfpuUnmount(controller);
@ -137,11 +141,14 @@ int main(int argc, char* argv[])
nfpuStopDetection(controller); nfpuStopDetection(controller);
// Wait for the user to explicitely exit. // Wait for the user to explicitely exit.
PRINT_UPD("Press + to exit.\n"); printf("Press + to exit.\n");
while (appletMainLoop()) { while (appletMainLoop()) {
hidScanInput(); hidScanInput();
if (hidKeysDown(CONTROLLER_P1_AUTO) & KEY_PLUS) if (hidKeysDown(CONTROLLER_P1_AUTO) & KEY_PLUS)
break; break;
// Update the console, sending a new frame to the display
consoleUpdate(NULL);
} }
// Cleanup. // Cleanup.