Use tabs for all examples.

This commit is contained in:
yellows8 2018-02-22 14:50:16 -05:00
parent 97aeaa0033
commit fff86cfc8d
3 changed files with 284 additions and 284 deletions

View File

@ -10,184 +10,184 @@
#define BYTESPERSAMPLE 4 #define BYTESPERSAMPLE 4
void fill_audio_buffer(void* audio_buffer, size_t offset, size_t size, int frequency) { void fill_audio_buffer(void* audio_buffer, size_t offset, size_t size, int frequency) {
if (audio_buffer == NULL) return; if (audio_buffer == NULL) return;
u32* dest = (u32*) audio_buffer; u32* dest = (u32*) audio_buffer;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
// This is a simple sine wave, with a frequency of `frequency` Hz, and an amplitude 30% of maximum. // This is a simple sine wave, with a frequency of `frequency` Hz, and an amplitude 30% of maximum.
s16 sample = 0.3 * 0x7FFF * sin(frequency * (2 * M_PI) * (offset + i) / SAMPLERATE); s16 sample = 0.3 * 0x7FFF * sin(frequency * (2 * M_PI) * (offset + i) / SAMPLERATE);
// Stereo samples are interleaved: left and right channels. // Stereo samples are interleaved: left and right channels.
dest[i] = (sample << 16) | (sample & 0xffff); dest[i] = (sample << 16) | (sample & 0xffff);
} }
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
Result rc = 0; Result rc = 0;
AudioOutBuffer source_buffer; AudioOutBuffer source_buffer;
AudioOutBuffer released_buffer; AudioOutBuffer released_buffer;
int notefreq[] = { int notefreq[] = {
220, 220,
440, 880, 1760, 3520, 7040, 440, 880, 1760, 3520, 7040,
14080, 14080,
7040, 3520, 1760, 880, 440 7040, 3520, 1760, 880, 440
}; };
gfxInitDefault(); gfxInitDefault();
// Initialize console. Using NULL as the second argument tells the console library to use the internal console structure as current one. // Initialize console. Using NULL as the second argument tells the console library to use the internal console structure as current one.
consoleInit(NULL); consoleInit(NULL);
// Make sure the sample buffer is aligned to 0x1000 bytes // Make sure the sample buffer is aligned to 0x1000 bytes
u32 raw_data_size = (SAMPLESPERBUF * BYTESPERSAMPLE * 2); u32 raw_data_size = (SAMPLESPERBUF * BYTESPERSAMPLE * 2);
u32 raw_data_size_aligned = (raw_data_size + 0xfff) & ~0xfff; u32 raw_data_size_aligned = (raw_data_size + 0xfff) & ~0xfff;
u8* raw_data = memalign(0x1000, raw_data_size_aligned); u8* raw_data = memalign(0x1000, raw_data_size_aligned);
// Ensure buffer was properly allocated // Ensure buffer was properly allocated
if (raw_data == NULL) if (raw_data == NULL)
{ {
rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
printf("Failed to allocate sample data buffer\n"); printf("Failed to allocate sample data buffer\n");
} }
if (R_SUCCEEDED(rc)) if (R_SUCCEEDED(rc))
memset(raw_data, 0, raw_data_size_aligned); memset(raw_data, 0, raw_data_size_aligned);
if (R_SUCCEEDED(rc)) if (R_SUCCEEDED(rc))
{ {
// Initialize the default audio output device // Initialize the default audio output device
rc = audoutInitialize(); rc = audoutInitialize();
printf("audoutInitialize() returned 0x%x\n", rc); printf("audoutInitialize() returned 0x%x\n", rc);
} }
if (R_SUCCEEDED(rc)) if (R_SUCCEEDED(rc))
{ {
printf("Sample rate: 0x%x\n", audoutGetSampleRate()); printf("Sample rate: 0x%x\n", audoutGetSampleRate());
printf("Channel count: 0x%x\n", audoutGetChannelCount()); printf("Channel count: 0x%x\n", audoutGetChannelCount());
printf("PCM format: 0x%x\n", audoutGetPcmFormat()); printf("PCM format: 0x%x\n", audoutGetPcmFormat());
printf("Device state: 0x%x\n", audoutGetDeviceState()); printf("Device state: 0x%x\n", audoutGetDeviceState());
// Start audio playback. // Start audio playback.
rc = audoutStartAudioOut(); rc = audoutStartAudioOut();
printf("audoutStartAudioOut() returned 0x%x\n", rc); printf("audoutStartAudioOut() returned 0x%x\n", rc);
} }
bool play_tone = false; bool play_tone = false;
printf("Press A, B, Y, X, Left, Up, Right, Down, L, R, ZL or ZR to play a different tone.\n"); printf("Press A, B, Y, X, Left, Up, Right, Down, L, R, ZL or ZR to play a different tone.\n");
while (appletMainLoop()) while (appletMainLoop())
{ {
//Scan all the inputs. This should be done once for each frame //Scan all the inputs. This should be done once for each frame
hidScanInput(); hidScanInput();
//hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame) //hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
u32 kDown = hidKeysDown(CONTROLLER_P1_AUTO); u32 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
if (kDown & KEY_A) if (kDown & KEY_A)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[0]); fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[0]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_B) if (kDown & KEY_B)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[1]); fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[1]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_Y) if (kDown & KEY_Y)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[2]); fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[2]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_X) if (kDown & KEY_X)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[3]); fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[3]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_DLEFT) if (kDown & KEY_DLEFT)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[4]); fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[4]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_DUP) if (kDown & KEY_DUP)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[5]); fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[5]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_DRIGHT) if (kDown & KEY_DRIGHT)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[6]); fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[6]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_DDOWN) if (kDown & KEY_DDOWN)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[7]); fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[7]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_L) if (kDown & KEY_L)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[8]); fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[8]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_R) if (kDown & KEY_R)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[9]); fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[9]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_ZL) if (kDown & KEY_ZL)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[10]); fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[10]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_ZR) if (kDown & KEY_ZR)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[11]); fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[11]);
play_tone = true; play_tone = true;
} }
if (R_SUCCEEDED(rc) && play_tone) if (R_SUCCEEDED(rc) && play_tone)
{ {
// Prepare the audio data source buffer. // Prepare the audio data source buffer.
source_buffer.next = 0; source_buffer.next = 0;
source_buffer.buffer = raw_data; source_buffer.buffer = raw_data;
source_buffer.buffer_size = raw_data_size; source_buffer.buffer_size = raw_data_size;
source_buffer.data_size = SAMPLESPERBUF * 2; source_buffer.data_size = SAMPLESPERBUF * 2;
source_buffer.data_offset = 0; source_buffer.data_offset = 0;
// Play this buffer once. // Play this buffer once.
rc = audoutPlayBuffer(&source_buffer, &released_buffer); rc = audoutPlayBuffer(&source_buffer, &released_buffer);
play_tone = false; play_tone = false;
if (R_FAILED(rc)) if (R_FAILED(rc))
printf("audoutPlayBuffer() returned 0x%x\n", rc); printf("audoutPlayBuffer() returned 0x%x\n", rc);
} }
gfxFlushBuffers(); gfxFlushBuffers();
gfxSwapBuffers(); gfxSwapBuffers();
gfxWaitForVsync(); gfxWaitForVsync();
} }
// Stop audio playback. // Stop audio playback.
rc = audoutStopAudioOut(); rc = audoutStopAudioOut();
printf("audoutStopAudioOut() returned 0x%x\n", rc); printf("audoutStopAudioOut() returned 0x%x\n", rc);
// Terminate the default audio output device. // Terminate the default audio output device.
audoutExit(); audoutExit();
gfxExit(); gfxExit();
return 0; return 0;
} }

View File

@ -10,63 +10,63 @@
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
u32* framebuf; u32* framebuf;
u32 cnt=0; u32 cnt=0;
#ifdef DISPLAY_IMAGE #ifdef DISPLAY_IMAGE
u8* imageptr = (u8*)image_bin; u8* imageptr = (u8*)image_bin;
#endif #endif
//Enable max-1080p support. Remove for 720p-only resolution. //Enable max-1080p support. Remove for 720p-only resolution.
//gfxInitResolutionDefault(); //gfxInitResolutionDefault();
gfxInitDefault(); gfxInitDefault();
//Set current resolution automatically depending on current/changed OperationMode. Only use this when using gfxInitResolution*(). //Set current resolution automatically depending on current/changed OperationMode. Only use this when using gfxInitResolution*().
//gfxConfigureAutoResolutionDefault(true); //gfxConfigureAutoResolutionDefault(true);
while(appletMainLoop()) while(appletMainLoop())
{ {
//Scan all the inputs. This should be done once for each frame //Scan all the inputs. This should be done once for each frame
hidScanInput(); hidScanInput();
//hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame) //hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
u32 kDown = hidKeysDown(CONTROLLER_P1_AUTO); u32 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
u32 width, height; u32 width, height;
u32 pos; u32 pos;
framebuf = (u32*) gfxGetFramebuffer((u32*)&width, (u32*)&height); framebuf = (u32*) gfxGetFramebuffer((u32*)&width, (u32*)&height);
if(cnt==60) if(cnt==60)
{ {
cnt=0; cnt=0;
} }
else else
{ {
cnt++; cnt++;
} }
//Each pixel is 4-bytes due to RGBA8888. //Each pixel is 4-bytes due to RGBA8888.
u32 x, y; u32 x, y;
for (y=0; y<height; y++)//Access the buffer linearly. for (y=0; y<height; y++)//Access the buffer linearly.
{ {
for (x=0; x<width; x++) for (x=0; x<width; x++)
{ {
pos = y * width + x; pos = y * width + x;
#ifdef DISPLAY_IMAGE #ifdef DISPLAY_IMAGE
framebuf[pos] = RGBA8_MAXALPHA(imageptr[pos*3+0]+(cnt*4), imageptr[pos*3+1], imageptr[pos*3+2]); framebuf[pos] = RGBA8_MAXALPHA(imageptr[pos*3+0]+(cnt*4), imageptr[pos*3+1], imageptr[pos*3+2]);
#else #else
framebuf[pos] = 0x01010101 * cnt * 4;//Set framebuf to different shades of grey. framebuf[pos] = 0x01010101 * cnt * 4;//Set framebuf to different shades of grey.
#endif #endif
} }
} }
gfxFlushBuffers(); gfxFlushBuffers();
gfxSwapBuffers(); gfxSwapBuffers();
gfxWaitForVsync(); gfxWaitForVsync();
} }
gfxExit(); gfxExit();
return 0; return 0;
} }

View File

@ -7,86 +7,86 @@
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
//Matrix containing the name of each key. Useful for printing when a key is pressed //Matrix containing the name of each key. Useful for printing when a key is pressed
char keysNames[32][32] = { char keysNames[32][32] = {
"KEY_A", "KEY_B", "KEY_X", "KEY_Y", "KEY_A", "KEY_B", "KEY_X", "KEY_Y",
"KEY_LSTICK", "KEY_RSTICK", "KEY_L", "KEY_R", "KEY_LSTICK", "KEY_RSTICK", "KEY_L", "KEY_R",
"KEY_ZL", "KEY_ZR", "KEY_PLUS", "KEY_MINUS", "KEY_ZL", "KEY_ZR", "KEY_PLUS", "KEY_MINUS",
"KEY_DLEFT", "KEY_DUP", "KEY_DRIGHT", "KEY_DDOWN", "KEY_DLEFT", "KEY_DUP", "KEY_DRIGHT", "KEY_DDOWN",
"KEY_LSTICK_LEFT", "KEY_LSTICK_UP", "KEY_LSTICK_RIGHT", "KEY_LSTICK_DOWN", "KEY_LSTICK_LEFT", "KEY_LSTICK_UP", "KEY_LSTICK_RIGHT", "KEY_LSTICK_DOWN",
"KEY_RSTICK_LEFT", "KEY_RSTICK_UP", "KEY_RSTICK_RIGHT", "KEY_RSTICK_DOWN", "KEY_RSTICK_LEFT", "KEY_RSTICK_UP", "KEY_RSTICK_RIGHT", "KEY_RSTICK_DOWN",
"KEY_SL", "KEY_SR", "KEY_TOUCH", "", "KEY_SL", "KEY_SR", "KEY_TOUCH", "",
"", "", "", "" "", "", "", ""
}; };
gfxInitDefault(); gfxInitDefault();
consoleInit(NULL); consoleInit(NULL);
u32 kDownOld = 0, kHeldOld = 0, kUpOld = 0; //In these variables there will be information about keys detected in the previous frame u32 kDownOld = 0, kHeldOld = 0, kUpOld = 0; //In these variables there will be information about keys detected in the previous frame
printf("\x1b[1;1HPress PLUS to exit."); printf("\x1b[1;1HPress PLUS to exit.");
printf("\x1b[2;1HLeft joystick position:"); printf("\x1b[2;1HLeft joystick position:");
printf("\x1b[4;1HRight joystick position:"); printf("\x1b[4;1HRight joystick position:");
// Main loop // Main loop
while(appletMainLoop()) while(appletMainLoop())
{ {
//Scan all the inputs. This should be done once for each frame //Scan all the inputs. This should be done once for each frame
hidScanInput(); hidScanInput();
//hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame) //hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
u32 kDown = hidKeysDown(CONTROLLER_P1_AUTO); u32 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
//hidKeysHeld returns information about which buttons have are held down in this frame //hidKeysHeld returns information about which buttons have are held down in this frame
u32 kHeld = hidKeysHeld(CONTROLLER_P1_AUTO); u32 kHeld = hidKeysHeld(CONTROLLER_P1_AUTO);
//hidKeysUp returns information about which buttons have been just released //hidKeysUp returns information about which buttons have been just released
u32 kUp = hidKeysUp(CONTROLLER_P1_AUTO); u32 kUp = hidKeysUp(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
//Do the keys printing only if keys have changed //Do the keys printing only if keys have changed
if (kDown != kDownOld || kHeld != kHeldOld || kUp != kUpOld) if (kDown != kDownOld || kHeld != kHeldOld || kUp != kUpOld)
{ {
//Clear console //Clear console
consoleClear(); consoleClear();
//These two lines must be rewritten because we cleared the whole console //These two lines must be rewritten because we cleared the whole console
printf("\x1b[1;1HPress PLUS to exit."); printf("\x1b[1;1HPress PLUS to exit.");
printf("\x1b[2;1HLeft joystick position:"); printf("\x1b[2;1HLeft joystick position:");
printf("\x1b[4;1HRight joystick position:"); printf("\x1b[4;1HRight joystick position:");
printf("\x1b[6;1H"); //Move the cursor to the sixth row because on the previous ones we'll write the joysticks' position printf("\x1b[6;1H"); //Move the cursor to the sixth row because on the previous ones we'll write the joysticks' position
//Check if some of the keys are down, held or up //Check if some of the keys are down, held or up
int i; int i;
for (i = 0; i < 32; i++) for (i = 0; i < 32; i++)
{ {
if (kDown & BIT(i)) printf("%s down\n", keysNames[i]); if (kDown & BIT(i)) printf("%s down\n", keysNames[i]);
if (kHeld & BIT(i)) printf("%s held\n", keysNames[i]); if (kHeld & BIT(i)) printf("%s held\n", keysNames[i]);
if (kUp & BIT(i)) printf("%s up\n", keysNames[i]); if (kUp & BIT(i)) printf("%s up\n", keysNames[i]);
} }
} }
//Set keys old values for the next frame //Set keys old values for the next frame
kDownOld = kDown; kDownOld = kDown;
kHeldOld = kHeld; kHeldOld = kHeld;
kUpOld = kUp; kUpOld = kUp;
JoystickPosition pos_left, pos_right; JoystickPosition pos_left, pos_right;
//Read the joysticks' position //Read the joysticks' position
hidJoystickRead(&pos_left, CONTROLLER_P1_AUTO, JOYSTICK_LEFT); hidJoystickRead(&pos_left, CONTROLLER_P1_AUTO, JOYSTICK_LEFT);
hidJoystickRead(&pos_right, CONTROLLER_P1_AUTO, JOYSTICK_RIGHT); hidJoystickRead(&pos_right, CONTROLLER_P1_AUTO, JOYSTICK_RIGHT);
//Print the joysticks' position //Print the joysticks' position
printf("\x1b[3;1H%04d; %04d", pos_left.dx, pos_left.dy); printf("\x1b[3;1H%04d; %04d", pos_left.dx, pos_left.dy);
printf("\x1b[5;1H%04d; %04d", pos_right.dx, pos_right.dy); printf("\x1b[5;1H%04d; %04d", pos_right.dx, pos_right.dy);
gfxFlushBuffers(); gfxFlushBuffers();
gfxSwapBuffers(); gfxSwapBuffers();
gfxWaitForVsync(); gfxWaitForVsync();
} }
gfxExit(); gfxExit();
return 0; return 0;
} }