camera/video/source/main.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <setjmp.h>
#include <3ds.h>
#include <sys/dirent.h>
#include <sys/errno.h>
#include <sys/unistd.h>
#include <stdbool.h>
#define CONFIG_3D_SLIDERSTATE (*(volatile float*)0x1FF81080)
#define WAIT_TIMEOUT 1000000000ULL
#define WIDTH 400
#define HEIGHT 240
#define SCREEN_SIZE WIDTH * HEIGHT * 2
#define BUF_SIZE SCREEN_SIZE * 2
static jmp_buf exitJmp;
inline void clearScreen(void) {
u8 *frame = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
memset(frame, 0, 320 * 240 * 3);
}
void hang(char *message) {
clearScreen();
printf("%s", message);
printf("Press start to exit");
while (aptMainLoop()) {
hidScanInput();
u32 kHeld = hidKeysHeld();
if (kHeld & KEY_START) longjmp(exitJmp, 1);
}
}
void cleanup() {
camExit();
gfxExit();
acExit();
}
void writePictureToFramebufferRGB565(void *fb, void *img, u16 x, u16 y, u16 width, u16 height) {
u8 *fb_8 = (u8*) fb;
u16 *img_16 = (u16*) img;
int i, j, draw_x, draw_y;
for(j = 0; j < height; j++) {
for(i = 0; i < width; i++) {
draw_y = y + height - j;
draw_x = x + i;
u32 v = (draw_y + draw_x * height) * 3;
u16 data = img_16[j * width + i];
uint8_t b = ((data >> 11) & 0x1F) << 3;
uint8_t g = ((data >> 5) & 0x3F) << 2;
uint8_t r = (data & 0x1F) << 3;
fb_8[v] = r;
fb_8[v+1] = g;
fb_8[v+2] = b;
}
}
}
// TODO: Figure out how to use CAMU_GetStereoCameraCalibrationData
void takePicture3D(u8 *buf) {
u32 bufSize;
printf("CAMU_GetMaxBytes: 0x%08X\n", (unsigned int) CAMU_GetMaxBytes(&bufSize, WIDTH, HEIGHT));
printf("CAMU_SetTransferBytes: 0x%08X\n", (unsigned int) CAMU_SetTransferBytes(PORT_BOTH, bufSize, WIDTH, HEIGHT));
printf("CAMU_Activate: 0x%08X\n", (unsigned int) CAMU_Activate(SELECT_OUT1_OUT2));
Handle camReceiveEvent = 0;
Handle camReceiveEvent2 = 0;
printf("CAMU_ClearBuffer: 0x%08X\n", (unsigned int) CAMU_ClearBuffer(PORT_BOTH));
printf("CAMU_SynchronizeVsyncTiming: 0x%08X\n", (unsigned int) CAMU_SynchronizeVsyncTiming(SELECT_OUT1, SELECT_OUT2));
printf("CAMU_StartCapture: 0x%08X\n", (unsigned int) CAMU_StartCapture(PORT_BOTH));
printf("CAMU_SetReceiving: 0x%08X\n", (unsigned int) CAMU_SetReceiving(&camReceiveEvent, buf, PORT_CAM1, SCREEN_SIZE, (s16) bufSize));
printf("CAMU_SetReceiving: 0x%08X\n", (unsigned int) CAMU_SetReceiving(&camReceiveEvent2, buf + SCREEN_SIZE, PORT_CAM2, SCREEN_SIZE, (s16) bufSize));
printf("svcWaitSynchronization: 0x%08X\n", (unsigned int) svcWaitSynchronization(camReceiveEvent, WAIT_TIMEOUT));
printf("svcWaitSynchronization: 0x%08X\n", (unsigned int) svcWaitSynchronization(camReceiveEvent2, WAIT_TIMEOUT));
printf("CAMU_PlayShutterSound: 0x%08X\n", (unsigned int) CAMU_PlayShutterSound(SHUTTER_SOUND_TYPE_NORMAL));
printf("CAMU_StopCapture: 0x%08X\n", (unsigned int) CAMU_StopCapture(PORT_BOTH));
svcCloseHandle(camReceiveEvent);
svcCloseHandle(camReceiveEvent2);
printf("CAMU_Activate: 0x%08X\n", (unsigned int) CAMU_Activate(SELECT_NONE));
}
int main() {
// Initializations
acInit();
gfxInitDefault();
consoleInit(GFX_BOTTOM, NULL);
// Enable double buffering to remove screen tearing
gfxSetDoubleBuffering(GFX_TOP, true);
gfxSetDoubleBuffering(GFX_BOTTOM, false);
// Save current stack frame for easy exit
if(setjmp(exitJmp)) {
cleanup();
return 0;
}
u32 kDown;
printf("Initializing camera\n");
printf("camInit: 0x%08X\n", (unsigned int) camInit());
printf("CAMU_SetSize: 0x%08X\n", (unsigned int) CAMU_SetSize(SELECT_OUT1_OUT2, SIZE_CTR_TOP_LCD, CONTEXT_A));
printf("CAMU_SetOutputFormat: 0x%08X\n", (unsigned int) CAMU_SetOutputFormat(SELECT_OUT1_OUT2, OUTPUT_RGB_565, CONTEXT_A));
printf("CAMU_SetFrameRate: 0x%08X\n", (unsigned int) CAMU_SetFrameRate(SELECT_OUT1_OUT2, FRAME_RATE_30));
printf("CAMU_SetNoiseFilter: 0x%08X\n", (unsigned int) CAMU_SetNoiseFilter(SELECT_OUT1_OUT2, true));
printf("CAMU_SetAutoExposure: 0x%08X\n", (unsigned int) CAMU_SetAutoExposure(SELECT_OUT1_OUT2, true));
printf("CAMU_SetAutoWhiteBalance: 0x%08X\n", (unsigned int) CAMU_SetAutoWhiteBalance(SELECT_OUT1_OUT2, true));
// TODO: Figure out how to use the effects properly.
//printf("CAMU_SetEffect: 0x%08X\n", (unsigned int) CAMU_SetEffect(SELECT_OUT1_OUT2, EFFECT_SEPIA, CONTEXT_A));
printf("CAMU_SetTrimming: 0x%08X\n", (unsigned int) CAMU_SetTrimming(PORT_CAM1, false));
printf("CAMU_SetTrimming: 0x%08X\n", (unsigned int) CAMU_SetTrimming(PORT_CAM2, false));
//printf("CAMU_SetTrimmingParamsCenter: 0x%08X\n", (unsigned int) CAMU_SetTrimmingParamsCenter(PORT_CAM1, 512, 240, 512, 384));
u8 *buf = malloc(BUF_SIZE);
if(!buf) {
hang("Failed to allocate memory!");
}
u32 bufSize;
printf("CAMU_GetMaxBytes: 0x%08X\n", (unsigned int) CAMU_GetMaxBytes(&bufSize, WIDTH, HEIGHT));
printf("CAMU_SetTransferBytes: 0x%08X\n", (unsigned int) CAMU_SetTransferBytes(PORT_BOTH, bufSize, WIDTH, HEIGHT));
printf("CAMU_Activate: 0x%08X\n", (unsigned int) CAMU_Activate(SELECT_OUT1_OUT2));
Handle camReceiveEvent[4] = {0};
bool captureInterrupted = false;
s32 index = 0;
// events 0 and 1 for interruption
printf("CAMU_GetBufferErrorInterruptEvent: 0x%08X\n", (unsigned int) CAMU_GetBufferErrorInterruptEvent(&camReceiveEvent[0], PORT_CAM1));
printf("CAMU_GetBufferErrorInterruptEvent: 0x%08X\n", (unsigned int) CAMU_GetBufferErrorInterruptEvent(&camReceiveEvent[1], PORT_CAM2));
printf("CAMU_ClearBuffer: 0x%08X\n", (unsigned int) CAMU_ClearBuffer(PORT_BOTH));
printf("CAMU_SynchronizeVsyncTiming: 0x%08X\n", (unsigned int) CAMU_SynchronizeVsyncTiming(SELECT_OUT1, SELECT_OUT2));
printf("CAMU_StartCapture: 0x%08X\n", (unsigned int) CAMU_StartCapture(PORT_BOTH));
printf("CAMU_PlayShutterSound: 0x%08X\n", (unsigned int) CAMU_PlayShutterSound(SHUTTER_SOUND_TYPE_MOVIE));
gfxFlushBuffers();
gspWaitForVBlank();
gfxSwapBuffers();
printf("\nUse slider to enable/disable 3D\n");
printf("Press Start to exit to Homebrew Launcher\n");
// Main loop
while (aptMainLoop()) {
if (!captureInterrupted) {
// Read which buttons are currently pressed or not
hidScanInput();
kDown = hidKeysDown();
// If START button is pressed, break loop and quit
if (kDown & KEY_START) {
break;
}
}
// events 2 and 3 for capture
if (camReceiveEvent[2] == 0) {
printf("CAMU_SetReceiving: 0x%08X\n", (unsigned int) CAMU_SetReceiving(&camReceiveEvent[2], buf, PORT_CAM1, SCREEN_SIZE, (s16)bufSize));
}
if (camReceiveEvent[3] == 0) {
CAMU_SetReceiving(&camReceiveEvent[3], buf + SCREEN_SIZE, PORT_CAM2, SCREEN_SIZE, (s16)bufSize);
}
if (captureInterrupted) {
printf("CAMU_StartCapture: 0x%08X\n", (unsigned int) CAMU_StartCapture(PORT_BOTH));
captureInterrupted = false;
}
printf("svcWaitSynchronizationN: 0x%08X\n", (unsigned int) svcWaitSynchronizationN(&index, camReceiveEvent, 4, false, WAIT_TIMEOUT));
switch (index) {
case 0:
printf("svcCloseHandle: 0x%08X\n", (unsigned int) svcCloseHandle(camReceiveEvent[2]));
camReceiveEvent[2] = 0;
captureInterrupted = true;
continue; //skip screen update
break;
case 1:
svcCloseHandle(camReceiveEvent[3]);
camReceiveEvent[3] = 0;
captureInterrupted = true;
continue; //skip screen update
break;
case 2:
printf("svcCloseHandle: 0x%08X\n", (unsigned int) svcCloseHandle(camReceiveEvent[2]));
camReceiveEvent[2] = 0;
break;
case 3:
svcCloseHandle(camReceiveEvent[3]);
camReceiveEvent[3] = 0;
break;
default:
break;
}
if(CONFIG_3D_SLIDERSTATE > 0.0f) {
gfxSet3D(true);
writePictureToFramebufferRGB565(gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL), buf, 0, 0, WIDTH, HEIGHT);
writePictureToFramebufferRGB565(gfxGetFramebuffer(GFX_TOP, GFX_RIGHT, NULL, NULL), buf + SCREEN_SIZE, 0, 0, WIDTH, HEIGHT);
} else {
gfxSet3D(false);
writePictureToFramebufferRGB565(gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL), buf, 0, 0, WIDTH, HEIGHT);
}
// Flush and swap framebuffers
gfxFlushBuffers();
gspWaitForVBlank();
gfxSwapBuffers();
}
printf("CAMU_StopCapture: 0x%08X\n", (unsigned int) CAMU_StopCapture(PORT_BOTH));
// Close camera event handles
for (int i = 0; i < 4; i++) {
if (camReceiveEvent[i] != 0) {
svcCloseHandle(camReceiveEvent[i]);
}
}
printf("CAMU_Activate: 0x%08X\n", (unsigned int) CAMU_Activate(SELECT_NONE));
// Exit
free(buf);
cleanup();
// Return to hbmenu
return 0;
}
3ds.h
Central 3DS header.
acExit
void acExit(void)
Exits AC.
acInit
Result acInit(void)
Initializes AC.
aptMainLoop
bool aptMainLoop(void)
Main function which handles sleep mode and HOME/power buttons - call this at the beginning of every f...
CAMU_SynchronizeVsyncTiming
Result CAMU_SynchronizeVsyncTiming(u32 select1, u32 select2)
Synchronizes the specified cameras' vsync timing.
CAMU_SetReceiving
Result CAMU_SetReceiving(Handle *event, void *dst, u32 port, u32 imageSize, s16 transferUnit)
Initiates the process of receiving a camera frame.
camInit
Result camInit(void)
Initializes the cam service.
CAMU_SetNoiseFilter
Result CAMU_SetNoiseFilter(u32 select, bool noiseFilter)
Sets whether the specified camera's noise filter is enabled.
CAMU_SetTransferBytes
Result CAMU_SetTransferBytes(u32 port, u32 bytes, s16 width, s16 height)
Sets the number of bytes to transfer into an image buffer.
SIZE_CTR_TOP_LCD
@ SIZE_CTR_TOP_LCD
CTR Top LCD size. (400x240)
Definition: cam.h:61
CAMU_ClearBuffer
Result CAMU_ClearBuffer(u32 port)
Clears the buffer and error flags of the specified camera port.
CAMU_SetAutoExposure
Result CAMU_SetAutoExposure(u32 select, bool autoExposure)
Sets whether auto exposure is enabled on the specified camera.
CAMU_GetBufferErrorInterruptEvent
Result CAMU_GetBufferErrorInterruptEvent(Handle *event, u32 port)
Gets a handle to the event signaled on camera buffer errors.
CAMU_StartCapture
Result CAMU_StartCapture(u32 port)
Begins capture on the specified camera port.
OUTPUT_RGB_565
@ OUTPUT_RGB_565
RGB565.
Definition: cam.h:157
FRAME_RATE_30
@ FRAME_RATE_30
30 FPS.
Definition: cam.h:77
SHUTTER_SOUND_TYPE_MOVIE
@ SHUTTER_SOUND_TYPE_MOVIE
Shutter sound to begin a movie.
Definition: cam.h:163
SHUTTER_SOUND_TYPE_NORMAL
@ SHUTTER_SOUND_TYPE_NORMAL
Normal shutter sound.
Definition: cam.h:162
CAMU_SetFrameRate
Result CAMU_SetFrameRate(u32 select, CAMU_FrameRate frameRate)
Sets the frame rate of the given camera.
CONTEXT_A
@ CONTEXT_A
Context A.
Definition: cam.h:37
camExit
void camExit(void)
Closes the cam service.
CAMU_StopCapture
Result CAMU_StopCapture(u32 port)
Terminates capture on the specified camera port.
CAMU_SetSize
Result CAMU_SetSize(u32 select, CAMU_Size size, CAMU_Context context)
Sets the image resolution of the given camera in the given context.
CAMU_SetTrimming
Result CAMU_SetTrimming(u32 port, bool trimming)
Sets whether image trimming is enabled.
CAMU_Activate
Result CAMU_Activate(u32 select)
Activates the specified camera.
CAMU_PlayShutterSound
Result CAMU_PlayShutterSound(CAMU_ShutterSoundType sound)
Plays the specified shutter sound.
CAMU_SetOutputFormat
Result CAMU_SetOutputFormat(u32 select, CAMU_OutputFormat format, CAMU_Context context)
Sets the output format of the given camera in the given context.
CAMU_SetAutoWhiteBalance
Result CAMU_SetAutoWhiteBalance(u32 select, bool autoWhiteBalance)
Sets whether auto white balance is enabled on the specified camera.
CAMU_GetMaxBytes
Result CAMU_GetMaxBytes(u32 *maxBytes, s16 width, s16 height)
Gets the maximum number of bytes that can be saved to an image buffer.
PORT_CAM2
@ PORT_CAM2
CAM2 port.
Definition: cam.h:14
PORT_BOTH
@ PORT_BOTH
Both ports.
Definition: cam.h:17
PORT_CAM1
@ PORT_CAM1
CAM1 port.
Definition: cam.h:13
SELECT_OUT1
@ SELECT_OUT1
Outer camera 1.
Definition: cam.h:23
SELECT_OUT2
@ SELECT_OUT2
Outer camera 2.
Definition: cam.h:25
SELECT_NONE
@ SELECT_NONE
No camera.
Definition: cam.h:22
SELECT_OUT1_OUT2
@ SELECT_OUT1_OUT2
Both outer cameras.
Definition: cam.h:29
consoleInit
PrintConsole * consoleInit(gfxScreen_t screen, PrintConsole *console)
Initialise the console.
gfxSwapBuffers
void gfxSwapBuffers(void)
Updates the configuration of both screens.
gfxInitDefault
void gfxInitDefault(void)
Initializes the LCD framebuffers with default parameters This is equivalent to calling:
gfxGetFramebuffer
u8 * gfxGetFramebuffer(gfxScreen_t screen, gfx3dSide_t side, u16 *width, u16 *height)
Retrieves the framebuffer of the specified screen to which graphics should be rendered.
GFX_BOTTOM
@ GFX_BOTTOM
Bottom screen.
Definition: gfx.h:27
GFX_TOP
@ GFX_TOP
Top screen.
Definition: gfx.h:26
gfxSet3D
void gfxSet3D(bool enable)
Enables or disables the 3D stereoscopic effect on the top screen.
gfxSetDoubleBuffering
void gfxSetDoubleBuffering(gfxScreen_t screen, bool enable)
Enables or disables double buffering on a screen.
gfxExit
void gfxExit(void)
Deinitializes and frees the LCD framebuffers.
GFX_LEFT
@ GFX_LEFT
Left eye framebuffer.
Definition: gfx.h:37
GFX_RIGHT
@ GFX_RIGHT
Right eye framebuffer.
Definition: gfx.h:38
gfxFlushBuffers
void gfxFlushBuffers(void)
Flushes the data cache for the current framebuffers.
gspWaitForVBlank
#define gspWaitForVBlank()
Waits for VBlank.
Definition: gspgpu.h:151
KEY_START
@ KEY_START
Start.
Definition: hid.h:15
hidKeysHeld
u32 hidKeysHeld(void)
Returns a bitmask of held buttons.
hidKeysDown
u32 hidKeysDown(void)
Returns a bitmask of newly pressed buttons, this frame.
hidScanInput
void hidScanInput(void)
Scans HID for input data.
svcCloseHandle
Result svcCloseHandle(Handle handle)
Closes a handle.
svcWaitSynchronizationN
Result svcWaitSynchronizationN(s32 *out, const Handle *handles, s32 handles_num, bool wait_all, s64 nanoseconds)
Waits for synchronization on multiple handles.
svcWaitSynchronization
Result svcWaitSynchronization(Handle handle, s64 nanoseconds)
Waits for synchronization on a handle.
u8
uint8_t u8
would be nice if newlib had this already
Definition: types.h:21
s16
int16_t s16
16-bit signed integer
Definition: types.h:27
Handle
u32 Handle
Resource handle.
Definition: types.h:41
u16
uint16_t u16
16-bit unsigned integer
Definition: types.h:22
s32
int32_t s32
32-bit signed integer
Definition: types.h:28
u32
uint32_t u32
32-bit unsigned integer
Definition: types.h:23