diff --git a/README.md b/README.md index 4e8fda0..ab43603 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 @@ -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. @@ -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 diff --git a/arm7/source/main.c b/arm7/source/main.c index b056975..7d57609 100644 --- a/arm7/source/main.c +++ b/arm7/source/main.c @@ -36,6 +36,8 @@ #include "i2c_handler.h" // #include +#include +#include #include #include // #include @@ -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() { //--------------------------------------------------------------------------------- @@ -68,7 +83,7 @@ int main() { rtcSyncTime(); // Initialize power management - pmInit(); + startupPowerInit(); // Set up block device peripherals blkInit(); diff --git a/arm9/include/ui_font.h b/arm9/include/ui_font.h new file mode 100644 index 0000000..3a890e0 --- /dev/null +++ b/arm9/include/ui_font.h @@ -0,0 +1,324 @@ +// IBM Plex Mono bitmap subset, SIL Open Font License; see tools/ui/OFL-IBMPlexMono.txt. +// Generated by tools/ui/font.py. +#ifndef UI_FONT_H +#define UI_FONT_H +#include +typedef struct { uint16_t offset; uint8_t width, advance; } UiGlyph; +typedef struct { const UiGlyph *glyphs; const uint8_t *pixels; uint8_t height; } UiFont; +static const UiGlyph ui_glyphs_10[] = { + {0, 6, 6}, + {14, 6, 6}, + {28, 6, 6}, + {42, 6, 6}, + {56, 6, 6}, + {70, 6, 6}, + {84, 6, 6}, + {98, 6, 6}, + {112, 6, 6}, + {126, 6, 6}, + {140, 6, 6}, + {154, 6, 6}, + {168, 6, 6}, + {182, 6, 6}, + {196, 6, 6}, + {210, 6, 6}, + {224, 6, 6}, + {238, 6, 6}, + {252, 6, 6}, + {266, 6, 6}, + {280, 6, 6}, + {294, 6, 6}, + {308, 6, 6}, + {322, 6, 6}, + {336, 6, 6}, + {350, 6, 6}, + {364, 6, 6}, + {378, 6, 6}, + {392, 6, 6}, + {406, 6, 6}, + {420, 6, 6}, + {434, 6, 6}, + {448, 6, 6}, + {462, 6, 6}, + {476, 6, 6}, + {490, 6, 6}, + {504, 6, 6}, + {518, 6, 6}, + {532, 6, 6}, + {546, 6, 6}, + {560, 6, 6}, + {574, 6, 6}, + {588, 6, 6}, + {602, 6, 6}, + {616, 6, 6}, + {630, 6, 6}, + {644, 6, 6}, + {658, 6, 6}, + {672, 6, 6}, + {686, 6, 6}, + {700, 6, 6}, + {714, 6, 6}, + {728, 6, 6}, + {742, 6, 6}, + {756, 6, 6}, + {770, 6, 6}, + {784, 6, 6}, + {798, 6, 6}, + {812, 6, 6}, + {826, 6, 6}, + {840, 6, 6}, + {854, 6, 6}, + {868, 6, 6}, + {882, 6, 6}, + {896, 6, 6}, + {910, 6, 6}, + {924, 6, 6}, + {938, 6, 6}, + {952, 6, 6}, + {966, 6, 6}, + {980, 6, 6}, + {994, 6, 6}, + {1008, 6, 6}, + {1022, 6, 6}, + {1036, 6, 6}, + {1050, 6, 6}, + {1064, 6, 6}, + {1078, 6, 6}, + {1092, 6, 6}, + {1106, 6, 6}, + {1120, 6, 6}, + {1134, 6, 6}, + {1148, 6, 6}, + {1162, 6, 6}, + {1176, 6, 6}, + {1190, 6, 6}, + {1204, 6, 6}, + {1218, 6, 6}, + {1232, 6, 6}, + {1246, 6, 6}, + {1260, 6, 6}, + {1274, 6, 6}, + {1288, 6, 6}, + {1302, 6, 6}, + {1316, 6, 6}, +}; +static const uint8_t ui_pixels_10[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x0c, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x14, 0x36, + 0x0a, 0x1b, 0x0a, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x1e, 0x16, 0x06, 0x1c, 0x14, 0x37, 0x1e, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x27, 0x15, 0x0f, 0x00, 0x3e, 0x2a, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x0a, 0x02, 0x36, 0x19, 0x11, 0x3e, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x08, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x08, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x04, 0x08, 0x08, 0x08, 0x08, 0x08, 0x04, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x04, 0x04, 0x3f, 0x0c, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x1e, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x08, 0x08, + 0x04, 0x04, 0x04, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x12, 0x12, 0x1e, 0x12, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x0c, 0x0a, 0x0a, 0x08, 0x08, 0x08, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x12, 0x10, 0x10, 0x0c, 0x02, 0x1e, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x12, 0x10, 0x0c, 0x10, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x14, 0x14, 0x12, 0x11, + 0x3f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x02, 0x1e, 0x12, 0x10, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, + 0x02, 0x1e, 0x12, 0x12, 0x12, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x12, 0x10, 0x08, 0x08, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x1e, 0x12, 0x12, 0x0c, 0x12, 0x12, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x12, 0x12, 0x12, 0x1e, 0x08, 0x04, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, + 0x00, 0x00, 0x00, 0x0c, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x08, 0x06, 0x02, 0x0c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x1e, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x04, 0x18, 0x10, 0x0c, 0x02, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x12, 0x10, 0x08, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x12, 0x1a, 0x16, 0x16, + 0x1e, 0x02, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x0c, 0x0a, 0x12, 0x1e, 0x12, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, + 0x12, 0x12, 0x0e, 0x12, 0x12, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x12, 0x02, 0x02, 0x02, 0x12, 0x1c, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x0e, 0x12, 0x12, 0x12, 0x12, 0x12, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x02, 0x02, 0x1e, 0x02, 0x02, 0x1e, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x02, 0x02, 0x1e, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x12, 0x02, + 0x1a, 0x12, 0x12, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x12, 0x12, 0x1e, 0x12, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x1e, 0x04, 0x04, 0x04, 0x04, 0x04, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x10, 0x10, 0x10, 0x10, 0x12, 0x0e, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x0a, 0x0a, 0x0e, 0x0a, 0x12, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, + 0x02, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x1a, 0x1e, 0x1e, 0x16, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, + 0x16, 0x16, 0x16, 0x1a, 0x1a, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x12, 0x12, 0x12, 0x12, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x1e, 0x12, 0x12, 0x1e, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x12, 0x12, 0x12, 0x12, 0x12, 0x1e, + 0x04, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x12, 0x12, 0x1e, 0x0a, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x12, 0x02, + 0x1e, 0x10, 0x13, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x12, 0x12, 0x12, 0x0c, 0x0c, 0x0c, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x21, 0x1d, 0x1d, 0x1d, 0x1a, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x12, 0x0c, 0x0c, 0x0c, + 0x12, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x12, 0x0c, 0x0c, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, + 0x10, 0x08, 0x04, 0x04, 0x02, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x1c, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x02, 0x02, 0x04, 0x04, 0x04, 0x08, 0x08, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, + 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x0c, 0x12, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x0e, 0x12, 0x1e, 0x12, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x1e, 0x12, 0x12, 0x12, 0x1e, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x12, 0x02, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x1e, 0x12, 0x12, + 0x12, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x12, 0x1e, 0x02, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, + 0x04, 0x1e, 0x04, 0x04, 0x04, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x1e, 0x12, 0x1e, 0x02, 0x1e, 0x22, 0x1e, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x02, 0x02, 0x1e, 0x12, 0x12, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0e, 0x08, 0x08, 0x08, 0x1e, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x0e, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x12, + 0x0a, 0x0e, 0x12, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x04, 0x04, 0x04, 0x04, 0x04, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x16, 0x1e, 0x16, 0x16, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x12, 0x12, 0x12, 0x12, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x12, 0x12, 0x12, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x12, 0x12, + 0x12, 0x1e, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x12, 0x12, 0x12, 0x1e, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x36, 0x0c, 0x04, 0x04, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x02, 0x1e, 0x10, 0x1e, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x04, 0x04, 0x1f, 0x04, 0x04, 0x04, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x12, 0x12, 0x12, 0x1e, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x12, 0x0a, 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, + 0x1d, 0x1d, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x0c, 0x0c, 0x0c, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x12, 0x12, 0x0a, 0x0c, 0x0c, 0x04, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x10, 0x0c, 0x02, 0x1e, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x1c, 0x04, 0x04, 0x08, 0x06, 0x08, 0x04, 0x04, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, + 0x08, 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x08, 0x04, 0x04, 0x18, 0x04, 0x04, 0x08, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x16, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +}; +static const UiFont ui_font_10 = {ui_glyphs_10, ui_pixels_10, 14}; +static const UiGlyph ui_glyphs_11[] = { + {0, 7, 7}, + {15, 7, 7}, + {30, 7, 7}, + {45, 7, 7}, + {60, 7, 7}, + {75, 7, 7}, + {90, 7, 7}, + {105, 7, 7}, + {120, 7, 7}, + {135, 7, 7}, + {150, 7, 7}, + {165, 7, 7}, + {180, 7, 7}, + {195, 7, 7}, + {210, 7, 7}, + {225, 7, 7}, + {240, 7, 7}, + {255, 7, 7}, + {270, 7, 7}, + {285, 7, 7}, + {300, 7, 7}, + {315, 7, 7}, + {330, 7, 7}, + {345, 7, 7}, + {360, 7, 7}, + {375, 7, 7}, + {390, 7, 7}, + {405, 7, 7}, + {420, 7, 7}, + {435, 7, 7}, + {450, 7, 7}, + {465, 7, 7}, + {480, 7, 7}, + {495, 7, 7}, + {510, 7, 7}, + {525, 7, 7}, + {540, 7, 7}, + {555, 7, 7}, + {570, 7, 7}, + {585, 7, 7}, + {600, 7, 7}, + {615, 7, 7}, + {630, 7, 7}, + {645, 7, 7}, + {660, 7, 7}, + {675, 7, 7}, + {690, 7, 7}, + {705, 7, 7}, + {720, 7, 7}, + {735, 7, 7}, + {750, 7, 7}, + {765, 7, 7}, + {780, 7, 7}, + {795, 7, 7}, + {810, 7, 7}, + {825, 7, 7}, + {840, 7, 7}, + {855, 7, 7}, + {870, 7, 7}, + {885, 7, 7}, + {900, 7, 7}, + {915, 7, 7}, + {930, 7, 7}, + {945, 7, 7}, + {960, 7, 7}, + {975, 7, 7}, + {990, 7, 7}, + {1005, 7, 7}, + {1020, 7, 7}, + {1035, 7, 7}, + {1050, 7, 7}, + {1065, 7, 7}, + {1080, 7, 7}, + {1095, 7, 7}, + {1110, 7, 7}, + {1125, 7, 7}, + {1140, 7, 7}, + {1155, 7, 7}, + {1170, 7, 7}, + {1185, 7, 7}, + {1200, 7, 7}, + {1215, 7, 7}, + {1230, 7, 7}, + {1245, 7, 7}, + {1260, 7, 7}, + {1275, 7, 7}, + {1290, 7, 7}, + {1305, 7, 7}, + {1320, 7, 7}, + {1335, 7, 7}, + {1350, 7, 7}, + {1365, 7, 7}, + {1380, 7, 7}, + {1395, 7, 7}, + {1410, 7, 7}, +}; +static const uint8_t ui_pixels_11[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, + 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x14, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x3c, 0x14, 0x76, 0x14, 0x14, 0x37, 0x12, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x1c, 0x2a, 0x0a, 0x0e, 0x38, 0x28, 0x2a, 0x1c, 0x08, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x29, 0x19, 0x06, 0x30, 0x4c, 0x4a, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x12, 0x02, + 0x24, 0x2a, 0x19, 0x12, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x30, 0x08, 0x08, 0x04, 0x04, 0x04, 0x04, 0x08, 0x08, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x08, 0x08, 0x08, 0x10, 0x10, + 0x08, 0x08, 0x08, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x3e, 0x08, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x08, 0x08, 0x3e, 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x08, + 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x10, 0x10, 0x08, 0x08, 0x08, 0x04, 0x04, 0x02, 0x02, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x1c, 0x22, 0x22, 0x22, 0x2a, 0x22, 0x22, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x0e, 0x0a, 0x08, 0x08, 0x08, + 0x08, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x22, 0x20, 0x20, 0x10, 0x08, 0x04, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x1c, 0x22, 0x20, 0x1c, 0x30, 0x20, 0x22, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x18, 0x14, 0x12, 0x12, 0x3e, 0x10, 0x10, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x02, 0x02, 0x1e, 0x22, 0x20, 0x22, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x04, 0x02, + 0x1e, 0x22, 0x22, 0x22, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x22, 0x10, 0x10, 0x10, 0x08, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x1c, 0x22, 0x22, 0x1c, 0x22, 0x22, 0x22, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x22, 0x22, 0x22, 0x3c, 0x10, + 0x10, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x00, 0x00, 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x08, 0x08, 0x00, 0x00, 0x08, 0x08, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x18, 0x06, 0x06, 0x18, 0x20, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, + 0x0c, 0x30, 0x30, 0x0c, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x22, 0x20, 0x18, 0x08, 0x00, 0x08, 0x0c, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x1c, 0x22, 0x3a, 0x2a, 0x26, 0x26, 0x3a, 0x02, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x14, 0x14, 0x14, 0x14, 0x3e, + 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x22, 0x22, 0x1e, 0x22, 0x22, 0x22, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x1c, 0x26, 0x02, 0x02, 0x02, 0x02, 0x26, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x1e, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x02, 0x02, 0x3e, 0x02, 0x02, 0x02, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x02, 0x02, + 0x3e, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x22, 0x02, 0x02, 0x3a, 0x22, 0x22, 0x3c, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x22, 0x22, 0x22, 0x3e, 0x22, 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x08, 0x08, 0x08, 0x08, 0x08, + 0x08, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x10, 0x10, 0x10, 0x10, 0x10, 0x12, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x22, 0x12, 0x0a, 0x0a, 0x0e, 0x12, 0x22, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x3e, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x36, 0x36, 0x2e, 0x2a, 0x2a, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x26, 0x26, 0x26, + 0x2a, 0x2a, 0x32, 0x32, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x1c, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x1e, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x22, 0x22, 0x22, 0x22, 0x22, + 0x22, 0x1c, 0x08, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x22, 0x22, 0x22, 0x1e, 0x12, 0x12, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x1c, 0x22, 0x02, 0x0e, 0x30, 0x20, 0x22, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x22, 0x22, + 0x14, 0x14, 0x14, 0x14, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x22, 0x2a, 0x2e, 0x36, 0x36, 0x36, 0x36, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x22, 0x24, 0x14, 0x08, 0x08, 0x14, 0x22, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x22, 0x14, 0x14, 0x08, 0x08, + 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x20, 0x10, 0x08, 0x08, 0x04, 0x02, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x38, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x04, 0x04, 0x04, 0x08, 0x08, 0x10, 0x10, 0x10, + 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x14, 0x12, + 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, + 0x00, 0x04, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x12, 0x10, 0x1e, + 0x12, 0x2e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x1a, 0x26, 0x22, 0x22, 0x22, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x1c, 0x22, 0x02, 0x02, 0x22, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x2c, 0x32, 0x22, 0x22, 0x22, 0x3c, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x22, 0x3e, 0x02, 0x22, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x08, 0x3e, + 0x08, 0x08, 0x08, 0x08, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x1c, 0x22, 0x22, 0x1c, 0x02, 0x3c, 0x22, 0x3e, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x02, 0x02, 0x1a, 0x26, 0x22, 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x0e, 0x08, 0x08, 0x08, + 0x08, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1e, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x02, 0x22, 0x12, 0x0a, 0x16, 0x12, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x3e, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1a, + 0x26, 0x22, 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x22, 0x22, 0x22, 0x22, 0x1c, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x1a, 0x26, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x32, 0x22, 0x22, + 0x22, 0x3c, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x08, 0x08, 0x08, 0x08, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x1c, 0x32, 0x0e, 0x30, 0x22, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x3e, 0x04, 0x04, 0x04, 0x04, 0x38, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x22, 0x22, 0x22, 0x22, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, + 0x22, 0x14, 0x14, 0x14, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a, 0x2e, 0x2e, 0x36, 0x36, 0x36, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x14, 0x08, 0x08, 0x14, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x22, 0x14, 0x14, + 0x0c, 0x08, 0x08, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x10, 0x18, 0x08, 0x04, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x38, 0x08, 0x08, 0x08, 0x16, 0x08, 0x18, 0x08, 0x08, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, + 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x08, 0x08, 0x04, 0x38, 0x0c, 0x04, 0x08, 0x08, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x26, 0x2a, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +}; +static const UiFont ui_font_11 = {ui_glyphs_11, ui_pixels_11, 15}; +#endif diff --git a/arm9/source/camera.c b/arm9/source/camera.c index a6ffb3e..5a79ac9 100644 --- a/arm9/source/camera.c +++ b/arm9/source/camera.c @@ -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; @@ -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 @@ -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; } @@ -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(); @@ -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 @@ -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; @@ -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; } @@ -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 diff --git a/arm9/source/main.c b/arm9/source/main.c index 7b0f888..02a929d 100644 --- a/arm9/source/main.c +++ b/arm9/source/main.c @@ -1,5 +1,6 @@ #include "camera.h" #include "night.h" +#include "ui_font.h" #include "version.h" #include "white_balance.h" @@ -16,7 +17,6 @@ #include #include #include -#include #include #define STB_IMAGE_WRITE_IMPLEMENTATION @@ -38,9 +38,9 @@ int clamp(int val, int min, int max) { return val < min ? min : (val > max ? max : val); } -static u16 *s_subGfx; // bottom-screen bitmap, drawn by the ui* helpers -static Camera s_camera; // currently active camera -static bool s_videoMode; // camera screen: photo vs video mode +static u16 *s_subGfx; // bottom-screen bitmap, drawn by the ui* helpers +static Camera s_camera; // currently active camera +static bool s_videoMode; // camera screen: photo vs video mode static const unsigned s_shutterLines[] = {0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 640}; #define SHUTTER_COUNT (sizeof(s_shutterLines) / sizeof(s_shutterLines[0])) static unsigned s_shutter; @@ -49,13 +49,46 @@ static int s_warmth; static bool s_nightBurst; static bool s_rearLight = true; static bool s_timestamp; // burn the capture time into saved pixels -static char s_savedMsg[48]; // last "saved video" summary, kept after the redraw +static char s_savedMsg[96]; // last "saved video" summary, kept after the redraw static void uiStatus(const char *s); -void uiSpace(void); +static void uiRecordingScreen(void); static void watermarkYuv(u16 *yuv, time_t t); static void watermarkFrame(u16 *gfx, time_t t); static void drainJobs(void); +static u16 uiCameraTap(int x, int y, bool fatInited) { + if(x >= 170 && y >= 8 && y <= 34) + return KEY_A; + if(!fatInited) + return 0; + if(y >= 8 && y <= 34 && x >= 12 && x <= 112) + return (x >= 61) != s_videoMode ? KEY_Y : 0; + if(x >= 64 && x <= 192 && y >= 88 && y <= 120) + return KEY_L; + if(y >= 140 && y <= 161) { + if(x >= 12 && x <= 96) + return KEY_SELECT; + if(x >= 174 && x <= 244) + return KEY_X; + } + return 0; +} + +static u16 uiConfirmTap(int x, int y) { + if(y < 108 || y > 129) + return 0; + if(x >= 25 && x < 119) + return KEY_B; + if(x >= 133 && x < 231) + return KEY_A; + return 0; +} + +static bool captureCancelled(void) { + scanKeys(); + return (keysDown() & (KEY_B | KEY_TOUCH)) || (keysHeld() & KEY_LID) || pmShouldReset(); +} + // The viewfinder is a continuous camera stream now. camera.c owns the state; // these wrappers just avoid restarting a stream that is already running. static void streamStart(void) { @@ -380,7 +413,7 @@ static int workerMain(void *arg) { if(!saved) remove(path); } - snprintf(msg, sizeof(msg), saved ? "SAVED IMG_%04d" : "SAVE FAILED: IMG_%04d", (int)job->aux); + snprintf(msg, sizeof(msg), saved ? "Saved IMG_%04d" : "Could not save IMG_%04d", (int)job->aux); uiStatus(msg); // Thumbnail cache for the album grid. @@ -420,7 +453,7 @@ static int workerMain(void *arg) { // sleeping, or opening the viewer so the photo list is complete. static void drainJobs(void) { if(JOBS_PENDING) - uiStatus("SAVING..."); + uiStatus("Saving..."); while(JOBS_PENDING) swiWaitForVBlank(); } @@ -441,7 +474,7 @@ static bool captureRaw(u16 *previewGfx) { if(!JOBS_PENDING) { free(yuv); free(job); - uiStatus("NOT ENOUGH MEMORY"); + uiStatus("Not enough memory. Try again."); return false; } swiWaitForVBlank(); @@ -452,7 +485,7 @@ static bool captureRaw(u16 *previewGfx) { u16 *next = NULL; const char *error = "CAMERA FAILED - TRY AGAIN"; if(s_nightBurst) - uiStatus("HOLD STILL 1/4 B: CANCEL"); + uiStatus("Hold still 1/4 B/tap: cancel"); if(!cameraCapture(yuv, CAPTURE_MODE_CAPTURE)) goto fail; memcpy(yuv, yuv + 640, 640 * sizeof(u16)); @@ -461,29 +494,27 @@ static bool captureRaw(u16 *previewGfx) { next = (u16 *)memalign(32, RAW_SIZE); stack = nightCreate((const u8 *)yuv); if(!next || !stack) { - error = "NOT ENOUGH MEMORY"; + error = "Not enough memory. Try again."; goto fail; } for(int i = 1; i < NIGHT_FRAMES; i++) { - scanKeys(); - if((keysDown() & KEY_B) || (keysHeld() & KEY_LID) || pmShouldReset()) { - error = "NIGHT BURST CANCELLED"; + if(captureCancelled()) { + error = "Night Burst cancelled"; goto fail; } char msg[40]; - sprintf(msg, "HOLD STILL %d/4 B: CANCEL", i + 1); + sprintf(msg, "Hold still %d/4 B/tap: cancel", i + 1); uiStatus(msg); if(!cameraCapture(next, CAPTURE_MODE_CAPTURE)) goto fail; memcpy(next, next + 640, 640 * sizeof(u16)); nightAdd(stack, (const u8 *)next); } - scanKeys(); - if((keysDown() & KEY_B) || (keysHeld() & KEY_LID) || pmShouldReset()) { - error = "NIGHT BURST CANCELLED"; + if(captureCancelled()) { + error = "Night Burst cancelled"; goto fail; } - uiStatus("PROCESSING..."); + uiStatus("Processing..."); nightFinish(stack, (u8 *)yuv); nightFree(stack); free(next); @@ -605,7 +636,7 @@ static void playVideo(u16 *gfx, int num) { } fseek(fa, sizeof(hdr), SEEK_SET); - uiStatus("LOADING..."); + uiStatus("Loading..."); // Prebuffer two ring halves of audio (zero-padded = silence). memset(s_playRing, 0, sizeof(s_playRing)); @@ -714,7 +745,7 @@ static void playVideo(u16 *gfx, int num) { lastSec = sec; char msg[40]; sprintf(msg, - "PLAYING %d:%02d / %lu:%02lu", + "Playing %d:%02d / %lu:%02lu", sec / 60, sec % 60, (unsigned long)(durSec / 60), @@ -728,7 +759,7 @@ static void playVideo(u16 *gfx, int num) { fclose(fa); fclose(fv); free(stage); - uiStatus("B: BACK"); + uiStatus("B to return"); } // Records video + mic audio to a .VID container until L/R is tapped again (or @@ -789,6 +820,7 @@ static void recordVideo(u16 *gfx, int num) { // as a freeze right at the start of the clip. u32 ovStart = cameraStreamOverruns(); streamStart(); + uiRecordingScreen(); u32 keepEvery = 1000 / VID_FPS; u32 nextKeep = 0; @@ -802,6 +834,12 @@ static void recordVideo(u16 *gfx, int num) { while(!stop) { swiWaitForVBlank(); scanKeys(); + if(keysDown() & KEY_TOUCH) { + touchPosition touch; + touchRead(&touch); + if(uiCameraTap(touch.px, touch.py, true) == KEY_L) + stop = true; + } if(keysDown() & (KEY_L | KEY_R) || (keysHeld() & KEY_LID) || pmShouldReset()) stop = true; @@ -863,7 +901,7 @@ static void recordVideo(u16 *gfx, int num) { if(sec != lastSec) { lastSec = sec; char msg[24]; - sprintf(msg, "%s REC %d:%02d", (sec & 1) ? "*" : " ", sec / 60, sec % 60); + sprintf(msg, "%d:%02d", sec / 60, sec % 60); uiStatus(msg); } } @@ -876,7 +914,7 @@ static void recordVideo(u16 *gfx, int num) { streamStop(); // no waiting on a possibly wedged DMA // Let the worker finish the remaining chunks, then patch the totals. - uiStatus("SAVING..."); + uiStatus("Saving..."); while(JOBS_PENDING) swiWaitForVBlank(); u32 durMs = (u32)((u64)recTicks * 1000 / PLAY_TIMER_HZ); @@ -912,21 +950,20 @@ static void recordVideo(u16 *gfx, int num) { u32 gapS = maxGapMs / 1000; if(gapS > 999) gapS = 999; - char msg[48]; + char msg[96]; snprintf(msg, sizeof(msg), - "VID_%04d %lu.%luFPS D:%lu A:%lu O:%lu G:%lu.%lus", + "VID_%04d %lu.%luFPS G:%lu.%lus D:%lu A:%lu O:%lu", num, (unsigned long)(fps10 / 10), (unsigned long)(fps10 % 10), + (unsigned long)gapS, + (unsigned long)((maxGapMs / 100) % 10), (unsigned long)framesDropped, (unsigned long)s_audDropped, - (unsigned long)(cameraStreamOverruns() - ovStart), - (unsigned long)gapS, - (unsigned long)((maxGapMs / 100) % 10)); + (unsigned long)(cameraStreamOverruns() - ovStart)); uiStatus(msg); snprintf(s_savedMsg, sizeof(s_savedMsg), "%s", msg); - uiSpace(); } // Fallback numbering for Album export when pit.bin is unusable. @@ -1034,6 +1071,11 @@ static bool confirmAB(void) { swiWaitForVBlank(); scanKeys(); u16 p = keysDown(); + if(p & KEY_TOUCH) { + touchPosition touch; + touchRead(&touch); + p |= uiConfirmTap(touch.px, touch.py); + } if(p & KEY_A) return true; if(p & KEY_B || pmShouldReset()) @@ -1080,9 +1122,9 @@ static void getThumb(int num, bool isVideo, u16 *out) { // --- Album gallery: one continuous scrolling layout, photos in rows of 4 // --- grouped under per-day headers, with eased scrolling. -#define HEADER_H 14 +#define HEADER_H 23 #define ROW_H (THUMB_H + 2) -#define SECTION_GAP 4 +#define SECTION_GAP 7 #define SCREEN_H 192 // 5x7 pixel font (columns, LSB = top row) for the date headers; covers @@ -1218,102 +1260,117 @@ static void watermarkFrame(u16 *gfx, time_t t) { drawText(gfx, x, y, s, BIT(15) | RGB15(31, 31, 31)); } -// --- Bottom-screen UI: minimalist monochrome panels drawn with the pixel -// --- font instead of a scrolling console. +// --- Bottom-screen UI, rendered directly into the RGB555 bitmap. -#define UI_BG (BIT(15) | RGB15(3, 3, 4)) -#define UI_FG (BIT(15) | RGB15(28, 28, 29)) -#define UI_DIM (BIT(15) | RGB15(13, 13, 15)) +#define UI_BG BIT(15) +#define UI_FG (BIT(15) | RGB15(28, 28, 28)) +#define UI_DIM (BIT(15) | RGB15(17, 17, 17)) +#define UI_DISABLED (BIT(15) | RGB15(10, 10, 10)) +#define UI_ALERT (BIT(15) | RGB15(28, 8, 8)) static void uiRect(int x, int y, int w, int h, u16 col) { - for(int yy = y; yy < y + h; yy++) - for(int xx = x; xx < x + w; xx++) + for(int yy = clamp(y, 0, SCREEN_H); yy < clamp(y + h, 0, SCREEN_H); yy++) + for(int xx = clamp(x, 0, 256); xx < clamp(x + w, 0, 256); xx++) s_subGfx[yy * 256 + xx] = col; } -static void uiText(int x, int y, const char *s, u16 col) { drawText(s_subGfx, x, y, s, col); } - -static void uiTextCenter(int y, const char *s, u16 col) { uiText((256 - (int)strlen(s) * 6) / 2, y, s, col); } +static int uiTextWidth(const char *s, const UiFont *font) { + int width = 0; + for(; *s; s++) { + unsigned c = (unsigned char)*s; + width += font->glyphs[(c >= 32 && c <= 126 ? c : '?') - 32].advance; + } + return width; +} -// Double-size text for headers. -static void uiText2(int x, int y, const char *s, u16 col) { - for(; *s; s++, x += 12) { - const u8 *g = glyph(*s); - for(int cx = 0; cx < 5; cx++) { - for(int cy = 0; cy < 7; cy++) { - if(!(g[cx] >> cy & 1)) +static void uiDrawText(u16 *gfx, int x, int y, const char *s, u16 col, const UiFont *font) { + for(; *s && x < 256; s++) { + unsigned c = (unsigned char)*s; + const UiGlyph *g = &font->glyphs[(c >= 32 && c <= 126 ? c : '?') - 32]; + int stride = (g->width + 7) / 8; + for(int cy = 0; cy < font->height; cy++) { + int yy = y + cy; + if(yy < 0 || yy >= SCREEN_H) + continue; + for(int cx = 0; cx < g->width; cx++) { + int xx = x + cx; + if(xx < 0 || xx >= 256) continue; - int xx = x + cx * 2, yy = y + cy * 2; - s_subGfx[yy * 256 + xx] = col; - s_subGfx[yy * 256 + xx + 1] = col; - s_subGfx[(yy + 1) * 256 + xx] = col; - s_subGfx[(yy + 1) * 256 + xx + 1] = col; + if(font->pixels[g->offset + cy * stride + cx / 8] & BIT(cx % 8)) + gfx[yy * 256 + xx] = col; } } + x += g->advance; } } -// Clears the screen and draws the title bar. +static void uiText(int x, int y, const char *s, u16 col) { uiDrawText(s_subGfx, x, y, s, col, &ui_font_11); } + +static void uiTextCenter(int y, const char *s, u16 col) { uiText((256 - uiTextWidth(s, &ui_font_11)) / 2, y, s, col); } + +static void uiSmall(int x, int y, const char *s, u16 col) { uiDrawText(s_subGfx, x, y, s, col, &ui_font_10); } + static void uiHeader(const char *title) { uiRect(0, 0, 256, SCREEN_H, UI_BG); - uiText2((256 - (int)strlen(title) * 12) / 2, 12, title, UI_FG); - uiRect(24, 34, 208, 1, UI_DIM); + uiText(16, 14, title, UI_FG); } -// One-line status at the bottom of the screen (safe to call from the worker -// thread; it only touches its own strip). +// The worker only draws inside these two status rows. static void uiStatus(const char *s) { - uiRect(0, 178, 256, 14, UI_BG); - uiTextCenter(182, s, UI_DIM); + uiRect(0, 164, 256, 28, UI_BG); + const UiFont *font = &ui_font_10; + char line[96]; + snprintf(line, sizeof(line), "%s", s); + size_t split = strlen(line); + while(split && uiTextWidth(line, font) > 240) + line[--split] = '\0'; + if(s[split]) { + char *space = strrchr(line, ' '); + if(space && space != line && uiTextWidth(s + (space - line) + 1, font) <= 240) { + split = space - line; + line[split] = '\0'; + } + uiSmall((256 - uiTextWidth(line, font)) / 2, 164, line, UI_DIM); + const char *rest = s + split; + if(*rest == ' ') + rest++; + snprintf(line, sizeof(line), "%s", rest); + while(strlen(line) > 3 && uiTextWidth(line, font) > 240) + line[strlen(line) - 1] = '\0'; + if(uiTextWidth(rest, font) > 240 && strlen(line) > 3) + strcpy(line + strlen(line) - 3, "..."); + } + uiSmall((256 - uiTextWidth(line, font)) / 2, 179, line, UI_DIM); } -// A right-aligned key hint column and left-aligned action, one row. static void uiKey(int y, const char *key, const char *action) { - uiText(116 - (int)strlen(key) * 6, y, key, UI_DIM); - uiText(126, y, action, UI_FG); -} - -// Free/total SD space, drawn just under the camera name. Safe to call from -// the worker thread; it only touches its own strip. -void uiSpace(void) { - struct statvfs sv; - uiRect(0, 58, 256, 10, UI_BG); - if(statvfs("/", &sv) != 0 || !sv.f_blocks) - return; - u64 freeB = (u64)sv.f_bavail * sv.f_frsize; - u64 totB = (u64)sv.f_blocks * sv.f_frsize; - char s[32]; - sprintf(s, "%.1f GB FREE OF %.1f", (double)freeB / 1e9, (double)totB / 1e9); - uiTextCenter(58, s, UI_DIM); + uiSmall(16, y, key, UI_DIM); + uiText(62, y - 1, action, UI_FG); } static void uiCameraScreen(int cam, bool fatInited) { - uiHeader("CAMERA"); - char sub[36]; - sprintf(sub, "%s CAMERA - %s", cam == CAM_INNER ? "INNER" : "OUTER", s_videoMode ? "VIDEO" : "PHOTO"); - uiTextCenter(44, sub, UI_DIM); - uiSpace(); - - int y = 78; - if(fatInited) { - uiKey(y, "L/R", s_videoMode ? "RECORD" : s_nightBurst ? "NIGHT PHOTO" : "TAKE PHOTOS"); - uiKey(y += 14, "Y", s_videoMode ? "PHOTO MODE" : "VIDEO MODE"); - uiKey(y += 14, "A", "SWAP CAMERA"); - uiKey(y += 14, "X", "ADVANCED"); - uiKey(y += 14, "SELECT", "ALBUM"); - } else { - uiTextCenter(y, "NO SD CARD - CANT SAVE", UI_FG); - uiKey(y += 14, "A", "SWAP CAMERA"); + uiHeader(""); + uiText(16, 14, "photo", s_videoMode ? UI_DIM : UI_FG); + uiText(65, 14, "video", s_videoMode ? UI_FG : UI_DIM); + uiSmall(186, 15, cam == CAM_INNER ? "inner / A" : "outer / A", UI_DIM); + if(!fatInited) { + uiTextCenter(98, "insert SD card to save", UI_DIM); + return; } - uiKey(y += 14, "START", "EXIT"); + uiTextCenter(98, s_videoMode ? "L/R record" : s_nightBurst ? "L/R night photo" : "L/R capture", UI_FG); + uiSmall(16, 146, "select album", UI_DIM); + uiSmall(180, 146, "X controls", UI_DIM); } -#define UI_ACCENT (BIT(15) | RGB15(31, 25, 5)) -#define UI_PANEL (BIT(15) | RGB15(6, 6, 7)) +static void uiRecordingScreen(void) { + uiHeader(""); + uiText(16, 14, "recording", UI_ALERT); + uiTextCenter(98, "L/R stop", UI_FG); +} enum { ADV_SHUTTER, ADV_GAIN, ADV_WHITE_BALANCE, ADV_NIGHT, ADV_REAR_LIGHT, ADV_TIMESTAMP, ADV_RESET, ADV_COUNT }; -#define ADV_ROW_Y 52 -#define ADV_ROW_H 14 +#define ADV_ROW_Y 40 +#define ADV_ROW_H 17 static bool advancedRowEnabled(int row) { if(row == ADV_SHUTTER || row == ADV_WHITE_BALANCE || row == ADV_NIGHT) @@ -1333,53 +1390,51 @@ static int advancedMoveSelection(int selected, int direction) { } static void uiAdvancedScreen(int selected) { - uiHeader("ADVANCED"); - uiTextCenter(43, s_videoMode ? "VIDEO - PHOTO CONTROLS LOCKED" : "PHOTO", UI_DIM); - const char *labels[] = {"SHUTTER", "GAIN", "WHITE BALANCE", "NIGHT BURST", "REAR LIGHT", "TIMESTAMP", "RESET"}; + uiHeader("controls"); + uiSmall(204, 15, "B back", UI_DIM); + const char *labels[] = {"shutter", "gain", "white balance", "night burst", "rear light", "timestamp", "reset"}; char shutter[16], gain[16], warmth[16]; if(!s_warmth || s_videoMode) - strcpy(warmth, "AUTO"); + strcpy(warmth, "auto"); else - sprintf(warmth, "%s %d", s_warmth > 0 ? "WARM" : "COOL", abs(s_warmth)); + sprintf(warmth, "%s %d", s_warmth > 0 ? "warm" : "cool", abs(s_warmth)); if(!s_videoMode && s_shutter) - sprintf(shutter, "%u LINE%s", s_shutterLines[s_shutter], s_shutterLines[s_shutter] == 1 ? "" : "S"); + sprintf(shutter, "%u line%s", s_shutterLines[s_shutter], s_shutterLines[s_shutter] == 1 ? "" : "s"); else - strcpy(shutter, "AUTO"); + strcpy(shutter, "auto"); if(!s_videoMode && s_shutter) - sprintf(gain, "%uX", 1u << s_gain); + sprintf(gain, "%ux", 1u << s_gain); else - strcpy(gain, "AUTO"); + strcpy(gain, "auto"); const char *values[] = {shutter, gain, warmth, - !s_videoMode && s_nightBurst ? "ON" : "OFF", - s_camera == CAM_OUTER && s_rearLight ? "ON" : "OFF", - s_timestamp ? "ON" : "OFF", - ""}; + !s_videoMode && s_nightBurst ? "on" : "off", + s_camera == CAM_OUTER && s_rearLight ? "on" : "off", + s_timestamp ? "on" : "off", + "A"}; for(int i = 0; i < ADV_COUNT; i++) { int y = ADV_ROW_Y + i * ADV_ROW_H; bool enabled = advancedRowEnabled(i); bool active = enabled && i == selected; - uiRect(12, y, 232, ADV_ROW_H, active ? UI_PANEL : UI_BG); if(active) - uiRect(12, y + 3, 2, 11, UI_ACCENT); - uiText(22, y + 5, labels[i], !enabled ? UI_DIM : active ? UI_ACCENT : UI_FG); - uiText(222 - strlen(values[i]) * 6, y + 5, values[i], enabled ? UI_FG : UI_DIM); - if(enabled && i != ADV_RESET) { - uiText(138, y + 5, "<", UI_DIM); - uiText(234, y + 5, ">", UI_DIM); - } + uiText(6, y + 2, ">", UI_FG); + uiText(18, y + 2, labels[i], enabled ? UI_FG : UI_DISABLED); + uiText(238 - uiTextWidth(values[i], &ui_font_11), + y + 2, + values[i], + !enabled ? UI_DISABLED + : active ? UI_FG + : UI_DIM); } - uiTextCenter(159, "UP/DOWN SELECT < > ADJUST", UI_DIM); - uiTextCenter(171, s_videoMode ? "A: SET B/X: DONE" : "A: SET L/R: PHOTO B/X: DONE", UI_DIM); - const char *hints[] = {"A: AUTO - MANUAL UNLOCKS GAIN", - "HIGHER GAIN ADDS NOISE", - "A: AUTO < COOL / WARM >", - "4 FRAMES - REAR LIGHT OFF", - "OFF WHILE NIGHT BURST SHOOTS", - "BURN DATE AND TIME INTO IMAGE", - "A: RESET ALL SETTINGS"}; - uiStatus(hints[selected]); + const char *hints[] = {"A for Auto. Manual unlocks gain.", + "Higher gain adds noise.", + "A for Auto. Left cools, right warms.", + "Four frames. Keep the camera still.", + "Off during Night Burst captures.", + "Add date and time to saved images.", + "A to restore default settings."}; + uiStatus(s_videoMode && !advancedRowEnabled(selected) ? "Photo controls unavailable in video." : hints[selected]); } static void uiExposureFailure(unsigned lines, unsigned gain) { @@ -1410,7 +1465,8 @@ static void uiExposureFailure(unsigned lines, unsigned gain) { static bool advancedMenu(u16 *gfx) { bool exitRequested = false; int selected = s_videoMode ? ADV_TIMESTAMP : ADV_SHUTTER; - keysSetRepeat(18, 6); + bool dirty = false; + keysSetRepeat(14, 4); uiAdvancedScreen(selected); while(!pmShouldReset()) { swiWaitForVBlank(); @@ -1419,25 +1475,27 @@ static bool advancedMenu(u16 *gfx) { lidSleep(); continue; } - u16 *frame = cameraNextFrame(); + u16 pressed = keysDownRepeat(); + u16 *frame = pressed ? NULL : cameraNextFrame(); if(frame) { if(!s_videoMode && s_warmth) whiteBalancePreview(frame, gfx, VID_W * VID_H, s_warmth); else memcpy(gfx, frame, VID_FRAME_SIZE); } - u16 pressed = keysDownRepeat(); bool tappedAction = false; if(keysDown() & KEY_TOUCH) { touchPosition touch; touchRead(&touch); + if(touch.px >= 170 && touch.py < 35) + break; if(touch.py >= ADV_ROW_Y && touch.py < ADV_ROW_Y + ADV_COUNT * ADV_ROW_H) { int row = (touch.py - ADV_ROW_Y) / ADV_ROW_H; if(!advancedRowEnabled(row)) continue; selected = row; if(touch.px >= 128) { - if(selected == ADV_RESET) + if(selected >= ADV_NIGHT) tappedAction = true; else pressed |= touch.px < 180 ? KEY_LEFT : KEY_RIGHT; @@ -1493,8 +1551,9 @@ static bool advancedMenu(u16 *gfx) { } bool changed = !s_videoMode && (oldShutter != s_shutter || oldGain != s_gain); unsigned attemptedLines = s_shutterLines[s_shutter], attemptedGain = s_gain; + uiAdvancedScreen(selected); if(changed) - uiStatus("APPLYING SENSOR SETTINGS..."); + uiStatus("Applying settings..."); bool ok = !changed || cameraSetExposure(attemptedLines, attemptedGain); if(!ok) { s_shutter = oldShutter; @@ -1509,22 +1568,27 @@ static bool advancedMenu(u16 *gfx) { s_rearLight = oldRearLight; cameraSetRearLight(s_rearLight); } - uiAdvancedScreen(selected); + dirty |= oldShutter != s_shutter || oldGain != s_gain || oldWarmth != s_warmth || oldNight != s_nightBurst || + oldTimestamp != s_timestamp || oldRearLight != s_rearLight; + if(changed || !lightOk) + uiAdvancedScreen(selected); if(!ok) uiExposureFailure(attemptedLines, attemptedGain); else if(!lightOk) uiStatus("REAR LIGHT SETTING FAILED"); } - cfgSave(); + if(dirty) + cfgSave(); return exitRequested; } // Centered modal question; waits for A (true) / B (false). static bool uiConfirm(const char *msg) { - uiRect(27, 63, 202, 58, UI_DIM); - uiRect(28, 64, 200, 56, UI_BG); - uiTextCenter(78, msg, UI_FG); - uiTextCenter(100, "A: YES B: NO", UI_DIM); + uiHeader("delete"); + uiTextCenter(69, msg, UI_FG); + uiSmall(62, 88, "moves to SD trash", UI_DIM); + uiSmall(48, 112, "B cancel", UI_DIM); + uiSmall(156, 112, "A delete", UI_ALERT); return confirmAB(); } @@ -1537,7 +1601,7 @@ typedef struct Section { // Lays out photos into rows of 4 under per-day headers. Fills px/py (virtual // position per photo) and *outSec/*outNsec; returns the total virtual height. static int buildLayout(const Photo *ph, int count, int *px, int *py, Section **outSec, int *outNsec) { - static const char *mon[12] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}; + static const char *mon[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; Section *sec = (Section *)malloc(count * sizeof(Section)); int ns = 0, y = 0, i = 0; @@ -1548,7 +1612,7 @@ static int buildLayout(const Photo *ph, int count, int *px, int *py, Section **o if(lt) sprintf(sec[ns].label, "%s %d, %d", mon[lt->tm_mon], lt->tm_mday, 1900 + lt->tm_year); else - strcpy(sec[ns].label, "UNKNOWN DATE"); + strcpy(sec[ns].label, "Unknown date"); y += HEADER_H; int dk = dayKey(ph[i].ts), col = 0; @@ -1590,14 +1654,14 @@ static void composeAlbum(u16 *gfx, int scroll, int sel, const bool *marked) { - u16 bgCol = BIT(15) | RGB15(3, 3, 4); + u16 bgCol = UI_BG; for(int i = 0; i < 256 * SCREEN_H; i++) gfx[i] = bgCol; for(int s = 0; s < nsec; s++) { int dy = sec[s].y - scroll; if(dy > -HEADER_H && dy < SCREEN_H) - drawText(gfx, 3, dy + 3, sec[s].label, BIT(15) | RGB15(24, 24, 26)); + uiDrawText(gfx, 6, dy + 3, sec[s].label, UI_FG, &ui_font_11); } for(int i = 0; i < count; i++) { @@ -1624,44 +1688,44 @@ static void composeAlbum(u16 *gfx, } } - // Marked photos get an orange border. - u16 orange = BIT(15) | RGB15(31, 20, 4); + // Marked items are bright; other focus borders are muted. + u16 markedColor = UI_FG; for(int i = 0; i < count; i++) { if(!marked || !marked[i] || i == sel) continue; int my = py[i] - scroll; if(my <= -THUMB_H || my >= SCREEN_H) continue; - hline(gfx, px[i], my, THUMB_W, orange); - hline(gfx, px[i], my + 1, THUMB_W, orange); - hline(gfx, px[i], my + THUMB_H - 2, THUMB_W, orange); - hline(gfx, px[i], my + THUMB_H - 1, THUMB_W, orange); + hline(gfx, px[i], my, THUMB_W, markedColor); + hline(gfx, px[i], my + 1, THUMB_W, markedColor); + hline(gfx, px[i], my + THUMB_H - 2, THUMB_W, markedColor); + hline(gfx, px[i], my + THUMB_H - 1, THUMB_W, markedColor); for(int y = 0; y < THUMB_H; y++) { int yy = my + y; if(yy < 0 || yy >= SCREEN_H) continue; - gfx[yy * 256 + px[i]] = orange; - gfx[yy * 256 + px[i] + 1] = orange; - gfx[yy * 256 + px[i] + THUMB_W - 2] = orange; - gfx[yy * 256 + px[i] + THUMB_W - 1] = orange; + gfx[yy * 256 + px[i]] = markedColor; + gfx[yy * 256 + px[i] + 1] = markedColor; + gfx[yy * 256 + px[i] + THUMB_W - 2] = markedColor; + gfx[yy * 256 + px[i] + THUMB_W - 1] = markedColor; } } - // Selection border (orange instead of white when the photo is marked). - u16 white = marked && marked[sel] ? orange : (BIT(15) | RGB15(31, 31, 31)); + // Keep the focused item distinct from marked items. + u16 focus = marked && marked[sel] ? markedColor : UI_DIM; int dy = py[sel] - scroll; - hline(gfx, px[sel], dy, THUMB_W, white); - hline(gfx, px[sel], dy + 1, THUMB_W, white); - hline(gfx, px[sel], dy + THUMB_H - 2, THUMB_W, white); - hline(gfx, px[sel], dy + THUMB_H - 1, THUMB_W, white); + hline(gfx, px[sel], dy, THUMB_W, focus); + hline(gfx, px[sel], dy + 1, THUMB_W, focus); + hline(gfx, px[sel], dy + THUMB_H - 2, THUMB_W, focus); + hline(gfx, px[sel], dy + THUMB_H - 1, THUMB_W, focus); for(int y = 0; y < THUMB_H; y++) { int yy = dy + y; if(yy < 0 || yy >= SCREEN_H) continue; - gfx[yy * 256 + px[sel]] = white; - gfx[yy * 256 + px[sel] + 1] = white; - gfx[yy * 256 + px[sel] + THUMB_W - 2] = white; - gfx[yy * 256 + px[sel] + THUMB_W - 1] = white; + gfx[yy * 256 + px[sel]] = focus; + gfx[yy * 256 + px[sel] + 1] = focus; + gfx[yy * 256 + px[sel] + THUMB_W - 2] = focus; + gfx[yy * 256 + px[sel] + THUMB_W - 1] = focus; } } @@ -1728,20 +1792,19 @@ static bool fullView(u16 *gfx, const Photo *ph, int count, int *idx) { if(lt) strftime(when, sizeof(when), "%b %d, %Y %H:%M", lt); else - strcpy(when, "UNKNOWN DATE"); + strcpy(when, "Unknown date"); sprintf(line, "%s_%04d", ph[*idx].isVideo ? "VID" : "IMG", ph[*idx].num); uiHeader(line); uiTextCenter(44, when, UI_DIM); sprintf(line, "%d/%d", *idx + 1, count); uiTextCenter(58, line, UI_DIM); int y = 88; - uiKey(y, "< >", "BROWSE"); if(ph[*idx].isVideo) - uiKey(y += 16, "A", "PLAY"); + uiKey(y, "A", "play video"); else - uiKey(y += 16, "A", "SEND TO DSI ALBUM"); - uiKey(y += 16, "X", "DELETE"); - uiKey(y += 16, "B", "BACK"); + uiKey(y, "A", "send to DSi Album"); + uiKey(y += 20, "X", "delete"); + uiSmall(16, 146, "left/right browse B back", UI_DIM); dirty = false; } @@ -1764,12 +1827,12 @@ static bool fullView(u16 *gfx, const Photo *ph, int count, int *idx) { playVideo(gfx, ph[*idx].num); dirty = true; // redraw the still + info afterwards } else { - uiStatus("EXPORTING..."); - uiStatus(exportToAlbum(ph[*idx].num) ? "SENT TO DSI ALBUM" : "EXPORT FAILED"); + uiStatus("Sending to DSi Album..."); + uiStatus(exportToAlbum(ph[*idx].num) ? "Sent to DSi Album" : "Could not send photo. Try again."); } } else if(pressed & KEY_X) { char msg[32]; - sprintf(msg, "DELETE %s_%04d?", ph[*idx].isVideo ? "VID" : "IMG", ph[*idx].num); + sprintf(msg, "Delete %s_%04d?", ph[*idx].isVideo ? "VID" : "IMG", ph[*idx].num); if(uiConfirm(msg)) { softDelete(ph[*idx].num, ph[*idx].isVideo); deleted = true; @@ -1816,8 +1879,8 @@ static void viewer(u16 *gfx, int cam) { // Cache every thumbnail in RAM (~6KB each) so scrolling never hits the SD. u16 *thumbs = (u16 *)malloc(count * THUMB_W * THUMB_H * sizeof(u16)); - uiHeader("ALBUM"); - uiStatus("LOADING..."); + uiHeader("album"); + uiStatus("Loading..."); for(int i = 0; i < count; i++) getThumb(ph[i].num, ph[i].isVideo, thumbs + i * THUMB_W * THUMB_H); @@ -1876,9 +1939,9 @@ static void viewer(u16 *gfx, int cam) { nMarked += marked[i]; char msg[36]; if(nMarked) - sprintf(msg, "DELETE %d SELECTED PHOTO%s?", nMarked, nMarked == 1 ? "" : "S"); + sprintf(msg, "Delete %d selected item%s?", nMarked, nMarked == 1 ? "" : "s"); else - sprintf(msg, "DELETE IMG_%04d?", ph[sel].num); + sprintf(msg, "Delete %s_%04d?", ph[sel].isVideo ? "VID" : "IMG", ph[sel].num); if(uiConfirm(msg)) { if(nMarked) { for(int i = 0; i < count; i++) @@ -1922,20 +1985,23 @@ static void viewer(u16 *gfx, int cam) { for(int i = 0; i < count; i++) nMarked += marked[i]; char line[36]; - uiHeader("ALBUM"); - sprintf(line, "IMG_%04d %d/%d%s", ph[sel].num, sel + 1, count, marked[sel] ? " *" : ""); + uiHeader("album"); + sprintf(line, + "%s_%04d %d/%d%s", + ph[sel].isVideo ? "VID" : "IMG", + ph[sel].num, + sel + 1, + count, + marked[sel] ? " *" : ""); uiTextCenter(44, line, UI_FG); if(nMarked) { - sprintf(line, "%d SELECTED", nMarked); + sprintf(line, "%d selected", nMarked); uiTextCenter(58, line, UI_DIM); } - int y = 82; - uiKey(y, "+", "NAVIGATE"); - uiKey(y += 16, "L/R", "JUMP DAY"); - uiKey(y += 16, "A", "VIEW"); - uiKey(y += 16, "Y", "SELECT"); - uiKey(y += 16, "X", "DELETE"); - uiKey(y += 16, "B", "CAMERA"); + uiKey(88, "A", "open"); + uiKey(108, "Y", "select"); + uiKey(128, "X", "delete"); + uiSmall(16, 146, "d-pad browse B camera", UI_DIM); infoDirty = false; } } @@ -1952,7 +2018,7 @@ static void viewer(u16 *gfx, int cam) { uiCameraScreen(cam, true); if(empty) - uiStatus("NO PHOTOS YET"); + uiStatus("No photos yet. Press L/R to take one."); } int main(int argc, char **argv) { @@ -1965,8 +2031,8 @@ int main(int argc, char **argv) { u16 *gfx = bgGetGfxPtr(bg3Main); s_subGfx = bgGetGfxPtr(bg3Sub); - uiHeader("CAMERA"); - uiStatus("BOOTING..."); + uiHeader("Camera"); + uiStatus("Starting camera..."); bool fatInited = fatInitDefault(); if(fatInited) { @@ -2010,10 +2076,10 @@ int main(int argc, char **argv) { closedir(vdir); } } else { - uiStatus("NO SD CARD"); + uiStatus("Insert an SD card to save"); } - uiStatus("INITIALIZING..."); + uiStatus("Connecting camera..."); pxiWaitRemote(PXI_CAMERA); // Wait for ARM7 to initialize PXI cameraInit(); cameraSetRearLight(s_rearLight); @@ -2049,6 +2115,7 @@ int main(int argc, char **argv) { if(!exposureReady) uiStatus("SAVED EXPOSURE SETTING FAILED"); streamStart(); // continuous viewfinder + scanKeys(); // buttons held over from the launcher aren't new presses while(1) { u16 pressed = 0; @@ -2068,14 +2135,19 @@ int main(int argc, char **argv) { continue; } // Blit whatever the camera has produced since the last vblank. - u16 *frame = cameraNextFrame(); + pressed = keysDown(); + u16 *frame = pressed ? NULL : cameraNextFrame(); if(frame) { if(!s_videoMode && s_warmth) whiteBalancePreview(frame, gfx, VID_W * VID_H, s_warmth); else memcpy(gfx, frame, VID_FRAME_SIZE); } - pressed = keysDown(); + if(pressed & KEY_TOUCH) { + touchPosition touch; + touchRead(&touch); + pressed |= uiCameraTap(touch.px, touch.py, fatInited); + } } while(!pressed); if(pressed & KEY_A) { @@ -2086,15 +2158,16 @@ int main(int argc, char **argv) { uiCameraScreen(camera, fatInited); streamStart(); } else if(fatInited && pressed & KEY_Y) { - bool video = !s_videoMode; + bool video = !s_videoMode; + s_videoMode = video; + uiCameraScreen(camera, fatInited); if(!cameraSetExposure(video ? 0 : s_shutterLines[s_shutter], s_gain)) { + s_videoMode = !video; + uiCameraScreen(camera, fatInited); uiExposureFailure(video ? 0 : s_shutterLines[s_shutter], s_gain); continue; } - s_videoMode = video; - uiCameraScreen(camera, fatInited); } else if(fatInited && pressed & KEY_X) { - drainJobs(); if(advancedMenu(gfx)) { cameraDeactivate(camera); drainJobs(); @@ -2114,7 +2187,6 @@ int main(int argc, char **argv) { streamStart(); // recordVideo left the camera idle } else { takePhotos(gfx); - uiSpace(); // refresh the free-space line after the burst } } else if(fatInited && pressed & KEY_SELECT) { // Stop the live preview and browse photos. diff --git a/tools/capture/camera_test.c b/tools/capture/camera_test.c index 11bf44d..7b9b68b 100644 --- a/tools/capture/camera_test.c +++ b/tools/capture/camera_test.c @@ -58,12 +58,16 @@ void irqSet(u32 mask, void (*handler)(void)) { (void)mask; (void)handler; } -void irqEnable(u32 mask) { (void)mask; } +void irqEnable(u32 mask) { + (void)mask; + assert(!(REG_CAM_CNT & BIT(15))); +} void irqDisable(u32 mask) { (void)mask; } void irqClear(u32 mask) { (void)mask; } void armDCacheFlush(const void *address, unsigned size) { (void)address; (void)size; + assert(!(REG_CAM_CNT & BIT(15))); } void armDCacheInvalidate(const void *address, unsigned size) { (void)address; @@ -79,6 +83,10 @@ int main(void) { assert(cameraActivate(CAM_OUTER)); assert(cameraSetExposure(64, 2)); assert(cameraStreamStart() && cameraStreamRunning()); + camDmaIsr(); + unsigned unchangedCalls = exposureCalls; + assert(cameraSetExposure(64, 2)); + assert(exposureCalls == unchangedCalls && s_frameCount == 1 && cameraStreamRunning()); failures = 1; assert(!cameraSetExposure(640, 3)); @@ -141,5 +149,40 @@ int main(void) { assert(cameraStreamStart()); pollPreview(); assert(!light && lightWrites == writes + 1); - puts("camera: apply/rollback faults, preview retry, mode entry, capture and intentional stop passed"); + + // A FIFO error must not publish a frame that spans two sensor frames. + u32 overruns = cameraStreamOverruns(); + REG_CAM_CNT &= ~BIT(4); + u16 *frame = s_frames[s_fill]; + for(unsigned y = 0; y < 192; y++) + for(unsigned x = 0; x < 256; x++) + frame[y * 256 + x] = y; + camDmaIsr(); + assert(cameraNextFrame() == frame); + assert(frame[0] == 0 && frame[191 * 256] == 191); + + u32 completed = s_frameCount; + REG_CAM_CNT |= BIT(4); + camDmaIsr(); + assert(s_frameCount == completed); + assert(cameraStreamOverruns() == overruns + 1); + assert(!cameraStreamRunning() && cameraStreamRequested()); + assert(!(REG_CAM_CNT & BIT(15)) && !REG_NDMA1CNT); + assert(cameraNextFrame() == NULL); + assert(cameraStreamRunning() && s_frameCount == 0); + assert((REG_CAM_CNT & (BIT(5) | BIT(15))) == (BIT(5) | BIT(15))); + assert(REG_NDMA1CNT == CAM_NDMA_CFG && REG_NDMA1TCNT == CAM_PREVIEW_WORDS); + assert(cameraNextFrame() == NULL); + + REG_CAM_CNT &= ~BIT(4); // hardware clears the error when the FIFO is flushed + frame = s_frames[s_fill]; + camDmaIsr(); + assert(cameraNextFrame() == frame); + REG_CAM_CNT |= BIT(4); + camDmaIsr(); + assert(cameraStreamOverruns() == overruns + 2); + cameraStreamStop(); + pollPreview(); + assert(!cameraStreamRunning() && !cameraStreamRequested()); + puts("camera: exposure faults, preview retry, DMA startup, FIFO resync, capture and stop passed"); } diff --git a/tools/ui/OFL-IBMPlexMono.txt b/tools/ui/OFL-IBMPlexMono.txt new file mode 100644 index 0000000..670c6c0 --- /dev/null +++ b/tools/ui/OFL-IBMPlexMono.txt @@ -0,0 +1,93 @@ +Copyright © 2017 IBM Corp. with Reserved Font Name "Plex" + +This Font Software is licensed under the SIL Open Font License, Version 1.1. + +This license is copied below, and is also available with a FAQ at: http://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/tools/ui/font.py b/tools/ui/font.py new file mode 100644 index 0000000..faf8a99 --- /dev/null +++ b/tools/ui/font.py @@ -0,0 +1,47 @@ +"""Generate the monochrome IBM Plex Mono bitmap subset (requires Pillow).""" +import argparse +from pathlib import Path +from PIL import Image, ImageDraw, ImageFont + +parser = argparse.ArgumentParser(description=__doc__) +parser.add_argument("font", type=Path, help="IBM Plex Mono Regular TTF") +args = parser.parse_args() +root = Path(__file__).resolve().parents[2] +out = [ + '// IBM Plex Mono bitmap subset, SIL Open Font License; see tools/ui/OFL-IBMPlexMono.txt.', + '// Generated by tools/ui/font.py.', + '#ifndef UI_FONT_H', + '#define UI_FONT_H', + '#include ', + 'typedef struct { uint16_t offset; uint8_t width, advance; } UiGlyph;', + 'typedef struct { const UiGlyph *glyphs; const uint8_t *pixels; uint8_t height; } UiFont;', +] +for size in (10, 11): + font = ImageFont.truetype(str(args.font), size) + advance = round(font.getlength('M')) + data, glyphs = [], [] + for code in range(32, 127): + width, height = advance, size + 4 + im = Image.new('L', (width, height)) + draw = ImageDraw.Draw(im) + draw.fontmode = '1' + draw.text((0, size), chr(code), font=font, fill=255, anchor='ls') + glyphs.append((len(data), width, advance)) + for y in range(height): + for x in range(0, width, 8): + data.append(sum( + (im.getpixel((xx, y)) != 0) << (xx - x) + for xx in range(x, min(x + 8, width)) + )) + out.append(f'static const UiGlyph ui_glyphs_{size}[] = {{') + out.extend(' {%d, %d, %d},' % g for g in glyphs) + out.append('};') + out.append(f'static const uint8_t ui_pixels_{size}[] = {{') + out.extend( + ' ' + ', '.join(f'0x{v:02x}' for v in data[i:i + 24]) + ',' + for i in range(0, len(data), 24) + ) + out.append('};') + out.append(f'static const UiFont ui_font_{size} = {{ui_glyphs_{size}, ui_pixels_{size}, {size + 4}}};') +out.extend(['#endif', '']) +(root / 'arm9/include/ui_font.h').write_text('\n'.join(out)) diff --git a/tools/ui/render.py b/tools/ui/render.py new file mode 100644 index 0000000..2b16712 --- /dev/null +++ b/tools/ui/render.py @@ -0,0 +1,337 @@ +"""Render the actual RGB555 UI on the host, with deterministic device state. + +Requires clang and Pillow. Screens and temporary build files go to /tmp. +This checks drawing/layout; camera hardware and input still require a DSi. +""" +import os +from pathlib import Path +import subprocess +import tempfile +from PIL import Image, ImageDraw + +ROOT = Path(__file__).resolve().parents[2] +OUT = Path('/tmp/dsi-camera-ui-dark') +OUT.mkdir(exist_ok=True) +source = (ROOT / 'arm9/source/main.c').read_text() + + +def section(start, end): + return source[source.index(start):source.index(end, source.index(start))] + + +preamble = r''' +#include +#include +#include +#include +#include +#include +#include +#include "ui_font.h" +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef int Camera; +#define KEY_A (1 << 0) +#define KEY_B (1 << 1) +#define KEY_START (1 << 3) +#define KEY_RIGHT (1 << 4) +#define KEY_LEFT (1 << 5) +#define KEY_UP (1 << 6) +#define KEY_DOWN (1 << 7) +#define KEY_R (1 << 8) +#define KEY_LID (1 << 12) +#define KEY_TOUCH (1 << 14) +#define KEY_SELECT (1 << 2) +#define KEY_L (1 << 9) +#define KEY_X (1 << 10) +#define KEY_Y (1 << 11) +#define CAM_INNER 0 +#define CAM_OUTER 1 +#define BIT(n) (1u << (n)) +#define RGB15(r,g,b) ((r) | ((g) << 5) | ((b) << 10)) +#define cameraStreamOverruns() UINT32_MAX +#define SCREEN_H 192 +#define THUMB_W 64 +#define THUMB_H 48 +#define GRID_COLS 4 +#define HEADER_H 23 +#define ROW_H 50 +#define SECTION_GAP 7 +static u16 canvas[256 * 192]; +int clamp(int n, int a, int b) { return n < a ? a : n > b ? b : n; } +typedef struct { int px, py; } touchPosition; +typedef struct { u16 pressed, held; int x, y; bool reset; } InputEvent; +static const InputEvent *events; +static size_t eventCount, eventIndex; +static InputEvent currentEvent; +static void input(const InputEvent *script, size_t count) { + events = script; eventCount = count; eventIndex = 0; + currentEvent = (InputEvent){0}; +} +static void swiWaitForVBlank(void) {} +static void scanKeys(void) { + assert(eventIndex < eventCount); + currentEvent = events[eventIndex++]; +} +static u16 keysDown(void) { return currentEvent.pressed; } +static u16 keysDownRepeat(void) { return currentEvent.pressed; } +static void keysSetRepeat(int delay, int repeat) { assert(delay == 14 && repeat == 4); } +static u16 keysHeld(void) { return currentEvent.held; } +static bool pmShouldReset(void) { return currentEvent.reset; } +static void touchRead(touchPosition *touch) { + touch->px = currentEvent.x; touch->py = currentEvent.y; +} +static void capture(const char *name) { + char path[256]; snprintf(path, sizeof(path), "%s/%s.ppm", getenv("DSI_UI_OUT"), name); + FILE *f = fopen(path, "wb"); if(!f) exit(1); + fprintf(f, "P6\n256 192\n255\n"); + for(int i = 0; i < 256 * 192; i++) { + unsigned char rgb[3] = {(canvas[i] & 31)*255/31, ((canvas[i] >> 5)&31)*255/31, ((canvas[i] >> 10)&31)*255/31}; + fwrite(rgb, 3, 1, f); + } + fclose(f); +} +''' +code = preamble +code += section('static u16 *s_subGfx;', 'static void uiStatus(') +code += section('static u16 uiCameraTap(', '// The viewfinder is a continuous') +code += section('typedef struct Photo {', 'static int cmpPhoto') +code += section('static int dayKey(', '// Async save pipeline') +code += section('static const u8 s_font', '// --- Timestamp watermark:') +code += section('// --- Bottom-screen UI,', 'static void uiExposureFailure') +code += r''' +#define VID_W 256 +#define VID_H 192 +#define VID_FRAME_SIZE (VID_W * VID_H * 2) +static unsigned exposureCalls, lightCalls, previewCalls, configWrites, exposureFailures; +static bool failExposure, failLight; +static u16 expectedFeedback[256 * SCREEN_H]; +static u16 *cameraNextFrame(void) { previewCalls++; return NULL; } +static void whiteBalancePreview(const u16 *src, u16 *dst, unsigned pixels, int warmth) { + (void)src; (void)dst; (void)pixels; (void)warmth; assert(false); +} +static void lidSleep(void) { assert(false); } +static void takePhotos(u16 *gfx) { (void)gfx; assert(false); } +static void cfgSave(void) { configWrites++; } +static bool cameraSetExposure(unsigned lines, unsigned gain) { + assert(lines == 1 && gain == 0); + // Feedback must already be visible when the blocking sensor call begins. + assert(memcmp(canvas, expectedFeedback, sizeof(canvas)) == 0); + exposureCalls++; + return !failExposure; +} +static bool cameraSetRearLight(bool enabled) { (void)enabled; lightCalls++; return !failLight; } +static void uiExposureFailure(unsigned lines, unsigned gain) { + (void)lines; (void)gain; exposureFailures++; +} +''' +code += section('static bool advancedMenu(', '// Centered modal question;') +code += section('// Waits for A (true)', '// Loads (or builds') +code += section('static bool uiConfirm(', '// Moves the selection') +code += r''' +static void testMenu(void) { + const InputEvent browse[] = {{.pressed=KEY_DOWN}, {.pressed=KEY_B}}; + input(browse, 2); + assert(!advancedMenu(canvas)); + assert(!configWrites && !exposureCalls && !previewCalls); + + const InputEvent timestamp[] = { + {.pressed=KEY_TOUCH, .x=220, .y=ADV_ROW_Y+ADV_TIMESTAMP*ADV_ROW_H+8}, + {.pressed=KEY_B} + }; + input(timestamp, 2); + assert(!advancedMenu(canvas)); + assert(s_timestamp && configWrites == 1 && !exposureCalls && !previewCalls); + s_timestamp = false; + configWrites = 0; + + s_shutter = 1; + uiAdvancedScreen(ADV_SHUTTER); uiStatus("Applying settings..."); + memcpy(expectedFeedback, canvas, sizeof(canvas)); + s_shutter = 0; + const InputEvent shutter[] = {{.pressed=KEY_RIGHT}, {.pressed=KEY_B}}; + input(shutter, 2); + assert(!advancedMenu(canvas)); + assert(s_shutter == 1 && exposureCalls == 1 && configWrites == 1 && !previewCalls); + + s_shutter = 0; failExposure = true; configWrites = 0; + input(shutter, 2); + assert(!advancedMenu(canvas)); + assert(s_shutter == 0 && exposureCalls == 2 && exposureFailures == 1 && !configWrites); + failExposure = false; + + failLight = true; + const InputEvent light[] = { + {.pressed=KEY_TOUCH, .x=220, .y=ADV_ROW_Y+ADV_REAR_LIGHT*ADV_ROW_H+8}, + {.pressed=KEY_B} + }; + input(light, 2); + assert(!advancedMenu(canvas)); + assert(s_rearLight && lightCalls == 2 && !configWrites); + failLight = false; + + const InputEvent idle[] = {{0}, {.pressed=KEY_B}}; + input(idle, 2); + assert(!advancedMenu(canvas)); + assert(previewCalls == 1 && !configWrites); +} +static void testFont(void) { + const UiFont *fonts[] = {&ui_font_10, &ui_font_11}; + for(unsigned f = 0; f < 2; f++) { + for(int c = 32; c <= 126; c++) { + char text[] = {(char)c, '\0'}; + uiHeader(""); + uiDrawText(canvas, 20, 20, text, UI_FG, fonts[f]); + for(unsigned i = 0; i < 256 * SCREEN_H; i++) + assert(canvas[i] == UI_BG || canvas[i] == UI_FG); + } + } +} +static void expectConfirm(const InputEvent *script, size_t count, bool result) { + input(script, count); + assert(confirmAB() == result); + assert(eventIndex == count); +} +static void testInput(void) { + const InputEvent hardwareDelete[] = {{.pressed=KEY_A}}; + const InputEvent hardwareCancel[] = {{.pressed=KEY_B}}; + const InputEvent reset[] = {{.reset=true}}; + expectConfirm(hardwareDelete, 1, true); + expectConfirm(hardwareCancel, 1, false); + expectConfirm(reset, 1, false); + // Exercise the actual dialog loop across every pixel, including edges and the dead gap. + for(int y = 0; y < SCREEN_H; y++) { + for(int x = 0; x < 256; x++) { + const InputEvent script[] = { + {.pressed=KEY_TOUCH, .held=KEY_TOUCH, .x=x, .y=y}, + {.pressed=KEY_B} + }; + bool insideRow = y >= 108 && y <= 129; + bool deletes = insideRow && x >= 133 && x <= 230; + bool cancels = insideRow && x >= 25 && x <= 118; + expectConfirm(script, deletes || cancels ? 1 : 2, deletes); + } + } + // A held initiating touch is not a second tap; a fresh tap or B cancels the burst. + const InputEvent burst[] = { + {.held=KEY_TOUCH}, {0}, {.pressed=KEY_TOUCH, .held=KEY_TOUCH}, + {.pressed=KEY_B}, {.held=KEY_LID}, {.reset=true} + }; + input(burst, sizeof(burst)/sizeof(burst[0])); + assert(!captureCancelled()); assert(!captureCancelled()); + assert(captureCancelled()); assert(captureCancelled()); + assert(captureCancelled()); assert(captureCancelled()); +} +static void expectStatus(const char *message, const char *first, const char *second) { + u16 actual[256 * SCREEN_H]; + uiHeader(""); uiStatus(message); memcpy(actual, canvas, sizeof(actual)); + uiHeader(""); + if(first[0]) uiSmall((256 - uiTextWidth(first, &ui_font_10))/2, 164, first, UI_DIM); + uiSmall((256 - uiTextWidth(second, &ui_font_10))/2, 179, second, UI_DIM); + assert(memcmp(actual, canvas, sizeof(actual)) == 0); +} +int main(void) { + s_subGfx = canvas; s_camera = CAM_OUTER; + testMenu(); + testFont(); + testInput(); + expectStatus("saved", "", "saved"); + expectStatus("VID_0001 10.0FPS D:120 A:0 O:1000 G:12.4s", + "VID_0001 10.0FPS D:120 A:0 O:1000", "G:12.4s"); + expectStatus("VID_2147483647 429496729.5FPS G:999.9s D:4294967295 A:4294967295 O:4294967295", + "VID_2147483647 429496729.5FPS G:999.9s", + "D:4294967295 A:4294967295 O:4294967295"); + { + int num = 2147483647; + u32 fps10 = UINT32_MAX, maxGapMs = 999999, ovStart = 0; + u32 framesDropped = UINT32_MAX, s_audDropped = UINT32_MAX; + /* VIDEO_SUMMARY */ + assert(strcmp(s_savedMsg, + "VID_2147483647 429496729.5FPS G:999.9s D:4294967295 A:4294967295 O:4294967295") == 0); + } + assert(advancedMoveSelection(ADV_SHUTTER, 1) == ADV_WHITE_BALANCE); + assert(uiCameraTap(128, 104, true) == KEY_L); + assert(uiCameraTap(128, 104, false) == 0); + assert(uiCameraTap(43, 156, true) == KEY_SELECT); + assert(uiCameraTap(213, 156, true) == KEY_X); + assert(uiCameraTap(195, 20, false) == KEY_A); + assert(uiCameraTap(90, 20, true) == KEY_Y); + assert(uiCameraTap(35, 20, true) == 0); + assert(uiCameraTap(0, 191, true) == 0); + uiCameraScreen(CAM_OUTER, true); capture("camera"); + s_videoMode = true; + assert(uiCameraTap(35, 20, true) == KEY_Y); + assert(uiCameraTap(90, 20, true) == 0); + uiCameraScreen(CAM_INNER, true); capture("video"); + uiRecordingScreen(); uiStatus("0:12"); capture("recording"); + uiCameraScreen(CAM_OUTER, true); + uiStatus("VID_0001 10.0FPS G:12.4s D:120 A:0 O:1000"); capture("diagnostics"); + uiAdvancedScreen(ADV_TIMESTAMP); capture("video-controls"); + s_videoMode = false; + uiCameraScreen(CAM_OUTER, false); capture("no-sd"); + uiAdvancedScreen(ADV_SHUTTER); capture("controls-auto"); + s_shutter = 11; s_gain = 3; s_warmth = -3; s_nightBurst = true; s_timestamp = true; + uiAdvancedScreen(ADV_SHUTTER); capture("controls-manual"); + for(int row = 0; row < ADV_COUNT; row++) { + uiAdvancedScreen(row); + char name[32]; snprintf(name, sizeof(name), "control-%d", row); capture(name); + } + s_camera = CAM_INNER; uiAdvancedScreen(ADV_NIGHT); capture("inner-controls"); + { + Photo ph[24] = {0}; bool marked[24] = {false}; + int sel = 23, count = 24, index = 23; int *idx = &index; + ph[23] = (Photo){.num=24, .ts=1793197800}; + { /* ALBUM_INFO */ } + capture("album"); + const InputEvent cancel[] = {{.pressed=KEY_B}}; + input(cancel, 1); + assert(!uiConfirm("Delete 24 selected items?")); capture("delete"); + { /* PHOTO_INFO */ } + capture("photo"); + } + Photo ph[12]; u16 thumbs[12 * THUMB_W * THUMB_H]; int px[12], py[12]; bool marked[12] = {true}; + for(int i = 0; i < 12; i++) { + ph[i] = (Photo){.num=i+1, .ts=1793188800 + (i/8)*86400, .isVideo=i==3, .durSec=42}; + for(int j = 0; j < THUMB_W*THUMB_H; j++) + thumbs[i*THUMB_W*THUMB_H+j] = BIT(15) | RGB15(16+i, 21+i/2, 25); + } + Section *sec; int ns; int height = buildLayout(ph, 12, px, py, &sec, &ns); + for(int scroll = 0; scroll <= height; scroll++) + composeAlbum(canvas, 12, ph, thumbs, sec, ns, px, py, scroll, 1, marked); + composeAlbum(canvas, 12, ph, thumbs, sec, ns, px, py, 0, 1, marked); capture("gallery"); + free(sec); + uiHeader("Camera"); uiTextCenter(44, "a deliberately long centered string that runs off both sides of the screen", UI_FG); + uiRect(-50, -50, 400, 400, UI_BG); + uiStatus("VID_0001 10.0FPS D:123 A:456 O:789 G:999.9s"); + return 0; +} +''' +# Use the in-app composition blocks as well as its drawing primitives. +code = code.replace('/* ALBUM_INFO */', section('if(infoDirty) {', 'infoDirty = false;').split('{', 1)[1]) +code = code.replace('/* PHOTO_INFO */', section('char when[40], line[40];', 'dirty = false;')) +code = code.replace('/* VIDEO_SUMMARY */', section('u32 gapS = maxGapMs / 1000;', '// Fallback numbering').rsplit('}', 1)[0]) +with tempfile.TemporaryDirectory(prefix='dsi-ui-', dir='/tmp') as tmp: + c = Path(tmp) / 'render.c' + c.write_text(code) + binary = Path(tmp) / 'render' + subprocess.run(['clang', '-std=c11', '-Wall', '-Wextra', '-Werror', '-Wno-deprecated-declarations', '-fsanitize=address,undefined', + '-fno-sanitize-recover=all', '-I', str(ROOT / 'arm9/include'), str(c), '-o', str(binary)], check=True) + subprocess.run([str(binary)], env={**os.environ, 'DSI_UI_OUT': str(OUT), 'TZ': 'UTC'}, check=True) +for ppm in OUT.glob('*.ppm'): + with Image.open(ppm) as im: + im.save(ppm.with_suffix('.png')) + ppm.unlink() +# Nearest-neighbor enlargement preserves the exact device pixels. +shots = [('camera', 'Camera'), ('controls-manual', 'Controls'), ('album', 'Album'), + ('recording', 'Recording'), ('video-controls', 'Video controls'), ('delete', 'Delete confirmation')] +board = Image.new('RGB', (256*3+16*4, (192+28)*2+16*3), '#101010') +draw = ImageDraw.Draw(board) +for i, (name, label) in enumerate(shots): + x = 16+(i%3)*272; y = 16+(i//3)*236 + draw.text((x, y), label, fill='#b0b0b0') + with Image.open(OUT / (name+'.png')) as im: + board.paste(im, (x,y+20)) +board.resize((board.width*2,board.height*2), Image.Resampling.NEAREST).save(OUT/'overview.png') +print(f'Rendered UI states, menu feedback/rollback, monochrome glyphs, confirmation input, burst cancellation, full diagnostics, clipping and album scroll checks passed: {OUT}')