Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ A homebrew camera app for the Nintendo DSi. Fork of [Pk11's dsi-camera](https://
| SELECT | Album |
| START | Exit |

The touch screen supports capture, the photo/video labels, album, controls,
and camera switch. In Controls, tap an on/off value to toggle it, or tap left
or right of a numeric value to adjust. Delete confirmations support touch too.

## Building

Requires [devkitPro](https://devkitpro.org) with devkitARM, libnds, and calico.
Expand All @@ -48,6 +52,33 @@ survive export to the DSi album and conversion on a PC. It's applied at capture
time only; existing photos are never modified, and the live viewfinder stays
clean.

## UI preview

The interface uses a black background and small, pixel-aligned IBM Plex Mono
bitmap text without antialiasing. Controls open while photo saves finish in the
background; unchanged exposure settings keep the preview stream running.
The font is licensed under the SIL Open Font License; see `tools/ui/OFL-IBMPlexMono.txt`.
The bitmap subset is compiled into the app and needs no font files on the SD card.

To render the actual C drawing code on a host (requires clang, Python, and Pillow):

```sh
python3 tools/ui/render.py
```

Captures are written to `/tmp/dsi-camera-ui-dark/`, including `overview.png`.
The renderer checks UI states, every delete/cancel touch coordinate through the
real confirmation loop, Night Burst cancellation input, complete video diagnostics,
and scroll/clipping bounds with AddressSanitizer and UndefinedBehaviorSanitizer.
Long status messages use two lines; ordinary status messages stay on one.
It uses fixture settings; camera hardware and touch behavior still need a
physical DSi.
To regenerate the font subset from the IBM Plex Mono Regular TTF:

```sh
python3 tools/ui/font.py /path/to/IBMPlexMono-Regular.ttf
```

## Credits

- [Pk11](https://github.com/Epicpkmn11) for the original app
Expand All @@ -58,7 +89,7 @@ clean.
## Advanced controls

Press **X**. Use up/down to select a row and left/right to adjust,
or tap the arrows beside a value. Press **A** on Shutter or White Balance to return to Auto,
or tap left/right beside a value. Press **A** on Shutter or White Balance to return to Auto,
on Gain, Night Burst, Rear Light, or Timestamp to cycle/toggle, or on Reset to restore all
settings. Press **B** or **X** to return to the viewfinder. The top screen stays
live while adjusting. Gain is gray and cannot be selected until Shutter is manual.
Expand All @@ -85,7 +116,7 @@ Rear Light is available whenever the outer camera is selected.
not a calibrated color temperature or a sensor setting. The live preview is
an approximation of the saved result.
- **Night Burst:** one shutter press captures four frames and saves one merged
photo. Hold still; press B to cancel. Alignment compensates for small camera
photo. Hold still; press B or tap the screen to cancel between frames. Alignment compensates for small camera
shifts, and changed pixels are rejected to limit ghosting. If alignment fails,
the first frame supplies the affected detail. It reduces noise without raising
brightness or recovering clipped highlights. Use a longer manual integration
Expand Down
17 changes: 16 additions & 1 deletion arm7/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "i2c_handler.h"

// #include <calico.h>
#include <calico/nds/arm7/i2c.h>
#include <calico/nds/arm7/mcu.h>
#include <nds.h>
#include <string.h>
// #include <maxmod7.h>
Expand All @@ -46,6 +48,19 @@
u32 g_cameraKey[4];
#define CAMERA_KEY_ADDR 0x03FFC5B0

static void startupPowerInit(void) {
if(systemIsTwlMode()) {
mcuInit();
i2cLock();
// Reading acknowledges power-button events left over from the launcher.
unsigned pending = i2cReadRegister8(I2cDev_MCU, McuReg_IrqFlags);
i2cUnlock();
if(pending & (MCU_IRQ_PWRBTN_SHUTDOWN | MCU_IRQ_BATTERY_EMPTY))
mcuIssueShutdown();
}
pmInit();
}

//---------------------------------------------------------------------------------
int main() {
//---------------------------------------------------------------------------------
Expand All @@ -68,7 +83,7 @@ int main() {
rtcSyncTime();

// Initialize power management
pmInit();
startupPowerInit();

// Set up block device peripherals
blkInit();
Expand Down
324 changes: 324 additions & 0 deletions arm9/include/ui_font.h

Large diffs are not rendered by default.

44 changes: 26 additions & 18 deletions arm9/source/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ CaptureMode activeMode = CAPTURE_MODE_PREVIEW;
#define CAM_FRAMES 3
alignas(32) static u16 s_frames[CAM_FRAMES][256 * 192];

static bool s_streaming;
static volatile bool s_streaming;
static bool s_streamRequested;
static bool s_rearLight = true;
static unsigned s_exposureLines;
Expand All @@ -43,29 +43,28 @@ static bool s_modeValid; // sensor is known to be in activeMode
static volatile u32 s_frameCount; // frames completed since stream start
static volatile u8 s_fill; // buffer the camera is writing right now
static volatile u32 s_overruns; // CAM_CNT.4 episodes seen in the ISR
static bool s_overrunSeen; // CAM_CNT.4 stays latched until a flush
static volatile bool s_resync; // discard a misaligned frame and restart outside the ISR
static u32 s_seen; // frames handed to the caller so far
static u32 s_callsWithoutFrame; // stall watchdog for cameraNextFrame

// Re-arm straight from the completion IRQ so the gap between frames is a few
// microseconds, not the time it takes the main loop to notice and copy.
static void camDmaIsr(void) {
if(REG_CAM_CNT & BIT(4)) {
s_overruns++;
REG_CAM_CNT &= ~BIT(15);
REG_NDMA1CNT = 0;
s_streaming = false;
s_resync = true;
return;
}

int next = s_fill + 1;
if(next == CAM_FRAMES)
next = 0;
s_fill = (u8)next;
s_frameCount++;

// Bit 4 latches until it is cleared with bit 5, so count episodes, not
// every frame that happens to still see it.
if(REG_CAM_CNT & BIT(4)) {
if(!s_overrunSeen)
s_overruns++;
s_overrunSeen = true;
} else {
s_overrunSeen = false;
}

REG_NDMA1DAD = (u32)s_frames[next];
REG_NDMA1TCNT = CAM_PREVIEW_WORDS;
REG_NDMA1WCNT = 512; // 4 scanlines per block
Expand Down Expand Up @@ -180,8 +179,6 @@ static bool camConfigure(CaptureMode mode) {
REG_CAM_CNT |= 0x2003;
else // raw YUV and "scanline count - 1" = 0
REG_CAM_CNT &= ~0x2003;
REG_CAM_CNT |= BIT(5); // clear overrun flag, flush till next camera vblank
REG_CAM_CNT |= BIT(15);
return true;
}

Expand All @@ -208,6 +205,8 @@ bool cameraSetExposure(unsigned lines, unsigned gain) {
s_exposureError[i] = 0;
if(lines > CAM_EXPOSURE_MAX_LINES || gain > 3 || activeCamera == CAM_NONE)
return false;
if(!s_exposureDirty && lines == s_exposureLines && gain == s_exposureGain && (!s_streamRequested || s_streaming))
return true;
bool resume = s_streamRequested;
unsigned oldLines = s_exposureLines, oldGain = s_exposureGain;
cameraStreamStop();
Expand Down Expand Up @@ -245,7 +244,9 @@ bool cameraCapture(u16 *dst, CaptureMode mode) {
REG_NDMA1TCNT = (mode == CAPTURE_MODE_PREVIEW ? CAM_PREVIEW_WORDS : (640 * 480) / 2);
REG_NDMA1WCNT = (mode == CAPTURE_MODE_PREVIEW ? 512 : 320);
REG_NDMA1BCNT = 2;
REG_NDMA1CNT = CAM_NDMA_ONE;
REG_CAM_CNT |= BIT(5); // flush stale data before arming DMA and enabling capture
REG_NDMA1CNT = CAM_NDMA_ONE;
REG_CAM_CNT |= BIT(15);

bool ok = camWaitDone(120); // ~2s at 60Hz
if(ok) // the DMA wrote around the cache, drop whatever the CPU still has
Expand All @@ -272,7 +273,6 @@ bool cameraStreamStart(void) {
s_fill = 0;
s_frameCount = 0;
s_seen = 0;
s_overrunSeen = false;
s_callsWithoutFrame = 0;
s_streaming = true;

Expand All @@ -285,7 +285,9 @@ bool cameraStreamStart(void) {
REG_NDMA1TCNT = CAM_PREVIEW_WORDS;
REG_NDMA1WCNT = 512;
REG_NDMA1BCNT = 2;
REG_NDMA1CNT = CAM_NDMA_CFG;
REG_CAM_CNT |= BIT(5); // flush stale data before arming DMA and enabling capture
REG_NDMA1CNT = CAM_NDMA_CFG;
REG_CAM_CNT |= BIT(15);
return true;
}

Expand All @@ -297,11 +299,17 @@ void cameraStreamStop(void) {
REG_CAM_CNT &= ~BIT(15);
REG_IF = IRQ_NDMA1;
irqClear(IRQ_NDMA1);
s_streaming = false;
s_streaming = false;
s_streamRequested = false;
s_resync = false;
}

u16 *cameraNextFrame(void) {
if(s_resync) {
cameraStreamStart();
return NULL;
}

u32 count = s_frameCount;
if(count == s_seen || count == 0) {
// Nothing for a long time means the camera stream died (or the sensor
Expand Down
Loading
Loading