Compare commits

...
Sign in to create a new pull request.

3 commits

Author SHA1 Message Date
e0a1e8d153 removed duplicate code 2025-09-28 15:39:11 +02:00
5715d60883 Added makerom to flake 2025-09-27 23:26:03 +02:00
c4b8ef1b92 bugfixes 2025-09-27 17:23:49 +02:00
3 changed files with 282 additions and 259 deletions

17
flake.lock generated
View file

@ -55,6 +55,22 @@
"type": "github" "type": "github"
} }
}, },
"makerom": {
"flake": false,
"locked": {
"lastModified": 1756011173,
"narHash": "sha256-nPRUwJwHA6cwGb0nocdCde7vfgxoRJF9T90mgEOMYu4=",
"owner": "3DSGuy",
"repo": "Project_CTR",
"rev": "f55e0fbc00f12ffb77f7af869f93472bed320ac6",
"type": "github"
},
"original": {
"owner": "3DSGuy",
"repo": "Project_CTR",
"type": "github"
}
},
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1751949589, "lastModified": 1751949589,
@ -91,6 +107,7 @@
"inputs": { "inputs": {
"devkitNix": "devkitNix", "devkitNix": "devkitNix",
"flake-utils": "flake-utils_2", "flake-utils": "flake-utils_2",
"makerom": "makerom",
"nixpkgs": "nixpkgs_2" "nixpkgs": "nixpkgs_2"
} }
}, },

View file

@ -3,6 +3,10 @@
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
devkitNix.url = "github:bandithedoge/devkitNix"; devkitNix.url = "github:bandithedoge/devkitNix";
makerom = {
flake = false;
url = "github:/3DSGuy/Project_CTR";
};
}; };
outputs = { outputs = {
@ -10,6 +14,7 @@
nixpkgs, nixpkgs,
flake-utils, flake-utils,
devkitNix, devkitNix,
makerom,
... ...
}: }:
flake-utils.lib.eachDefaultSystem ( flake-utils.lib.eachDefaultSystem (
@ -23,18 +28,47 @@
pkgs.mkShell.override { pkgs.mkShell.override {
stdenv = pkgs.devkitNix.stdenvARM; stdenv = pkgs.devkitNix.stdenvARM;
} { } {
packages = with pkgs; [imagemagick]; packages = [self.packages.${pkgs.system}.makerom];
}; };
packages.default = pkgs.devkitNix.stdenvARM.mkDerivation { packages = {
name = "somding"; default = pkgs.devkitNix.stdenvARM.mkDerivation {
src = ./.; name = "print3d";
buildInputs = [pkgs.imagemagick pkgs.which]; src = ./.;
buildInputs = [pkgs.imagemagick pkgs.which];
# makeFlags = ["TARGET=example"]; # makeFlags = ["TARGET=example"];
installPhase = '' installPhase = ''
mkdir $out mkdir $out
cp 3ds.3dsx $out cp 3ds.3dsx $out
''; '';
};
makerom =
pkgs.stdenv.mkDerivation
{
name = "makerom";
src = "${makerom}/makerom";
buildInputs = let
pkgDep = name:
pkgs.stdenv.mkDerivation {
inherit name;
src = "${makerom}/makerom/deps/${name}";
installPhase = ''
mkdir -p $out/lib
cp bin/${name}.a $out/lib
'';
};
in [
(pkgDep "libblz")
(pkgDep "libmbedtls")
(pkgDep "libyaml")
];
installPhase = ''
mkdir -p $out/bin
cp bin/makerom $out/bin
'';
};
}; };
} }
); );

View file

@ -6,155 +6,152 @@
#include <inttypes.h> #include <inttypes.h>
#include <jpeglib.h> #include <jpeglib.h>
#include <malloc.h> #include <malloc.h>
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#define WIDTH 400 #define W 400
#define HEIGHT 240 #define H 240
#define FB_SIZE (WIDTH * HEIGHT * 3) #define FB_BYTES (W * H * 3)
#define STORAGE_DIR "sdmc:/printcam" #define DIR "sdmc:/printcam"
#define ENDPOINT_PATH STORAGE_DIR "/endpoint.txt" #define EP_PATH DIR "/endpoint.txt"
#define API_ENDPOINT_DEFAULT "https://example.com/upload" #define EP_DEFAULT "https://example.com/upload"
#define JPEG_QUALITY 100 #define JPG_QUALITY 100
#define CAM_TIMEOUT_NS (5ULL * 1000ULL * 1000ULL * 1000ULL)
static volatile bool exitRequested = false; static volatile bool quit = false;
static Thread camThread = NULL; static Thread camThread = NULL;
static Handle ready[2] = {0, 0}; static Handle readyEv[2] = {0, 0};
static u8 *camBuf[2] = {NULL, NULL}; static u8 *camBuf[2] = {NULL, NULL};
static u8 *rgbBuf = NULL; static u8 *dispBuf = NULL;
static volatile u8 writeIdx = 0, dispIdx = 0; static volatile u8 writeIdx = 0;
static volatile bool camera_reinit = false; static volatile u8 dispIdx = 0;
static volatile int camera_index = 0; static volatile bool reinitCam = false;
static const u32 cam_selects[2] = {SELECT_OUT1, SELECT_IN1}; static volatile int camIndex = 0;
static char api_endpoint[512] = API_ENDPOINT_DEFAULT; static char endpoint[512] = EP_DEFAULT;
static PrintConsole bottomScreen; static PrintConsole bottom;
static bool ensure_storage_dir(void) { static int init_endpoint_config(void) {
if (mkdir(STORAGE_DIR, 0777) == 0) if (mkdir(DIR, 0777) != 0 && errno != EEXIST)
return true;
return errno == EEXIST;
}
static bool ensure_default_endpoint_file(void) {
FILE *f = fopen(ENDPOINT_PATH, "r");
if (f) {
fclose(f);
return true;
}
f = fopen(ENDPOINT_PATH, "w");
if (!f)
return false;
if (fprintf(f, "%s\n", API_ENDPOINT_DEFAULT) < 0) {
fclose(f);
return false;
}
fclose(f);
return true;
}
static int load_endpoint_from_sd(void) {
FILE *f = fopen(ENDPOINT_PATH, "r");
if (!f)
return -1; return -1;
if (!fgets(api_endpoint, sizeof(api_endpoint), f)) { FILE *f = fopen(EP_PATH, "r");
if (f != NULL) {
if (fgets(endpoint, sizeof(endpoint), f) == NULL) {
fclose(f);
return -1;
}
fclose(f); fclose(f);
size_t n = strlen(endpoint);
while (n && (endpoint[n - 1] == '\n' || endpoint[n - 1] == '\r')) {
endpoint[--n] = '\0';
}
if (n == 0)
return -1;
return 1;
}
if (errno != ENOENT)
return -1;
FILE *w = fopen(EP_PATH, "w");
if (!w)
return -1;
if (fprintf(w, "%s\n", EP_DEFAULT) < 0) {
fclose(w);
unlink(EP_PATH);
return -1; return -1;
} }
size_t len = strlen(api_endpoint); fflush(w);
while (len && fsync(fileno(w));
(api_endpoint[len - 1] == '\n' || api_endpoint[len - 1] == '\r')) { fclose(w);
api_endpoint[len - 1] = '\0'; strncpy(endpoint, EP_DEFAULT, sizeof(endpoint) - 1);
--len; endpoint[sizeof(endpoint) - 1] = '\0';
}
fclose(f);
return 0; return 0;
} }
static inline void writePix(u8 *dst, int x, int y, u8 r, u8 g, u8 b) { static inline void put_pixel(u8 *d, int x, int y, u8 r, u8 g, u8 b) {
int off = (x * HEIGHT + (HEIGHT - 1 - y)) * 3; int off = (x * H + (H - 1 - y)) * 3;
dst[off] = b; d[off + 0] = b;
dst[off + 1] = g; d[off + 1] = g;
dst[off + 2] = r; d[off + 2] = r;
} }
static void blitYUYVtoRGB(const u8 *src, u8 *dst) { static void yuyv_to_display(const u8 *s, u8 *d) {
for (int y = 0; y < HEIGHT; ++y) { for (int y = 0; y < H; ++y) {
for (int x = 0; x < WIDTH; x += 2) { for (int x = 0; x < W; x += 2) {
u8 y0 = src[0], u = src[1], y1 = src[2], v = src[3]; u8 y0 = s[0];
src += 4; u8 u = s[1];
int8_t u2 = (int8_t)(u - 128), v2 = (int8_t)(v - 128); u8 y1 = s[2];
int r = (int)(1.402f * v2), g = (int)(-0.344f * u2 - 0.714f * v2), u8 v = s[3];
b = (int)(1.772f * u2); s += 4;
int R = y0 + r, G = y0 + g, B = y0 + b; int8_t u2 = (int8_t)(u - 128);
int8_t v2 = (int8_t)(v - 128);
int rcoef = (int)(1.402f * v2);
int gcoef = (int)(-0.344f * u2 - 0.714f * v2);
int bcoef = (int)(1.772f * u2);
int R = y0 + rcoef;
int G = y0 + gcoef;
int B = y0 + bcoef;
R = R < 0 ? 0 : (R > 255 ? 255 : R); R = R < 0 ? 0 : (R > 255 ? 255 : R);
G = G < 0 ? 0 : (G > 255 ? 255 : G); G = G < 0 ? 0 : (G > 255 ? 255 : G);
B = B < 0 ? 0 : (B > 255 ? 255 : B); B = B < 0 ? 0 : (B > 255 ? 255 : B);
writePix(dst, x, y, (u8)R, (u8)G, (u8)B); put_pixel(d, x, y, (u8)R, (u8)G, (u8)B);
R = y1 + r; R = y1 + rcoef;
G = y1 + g; G = y1 + gcoef;
B = y1 + b; B = y1 + bcoef;
R = R < 0 ? 0 : (R > 255 ? 255 : R); R = R < 0 ? 0 : (R > 255 ? 255 : R);
G = G < 0 ? 0 : (G > 255 ? 255 : G); G = G < 0 ? 0 : (G > 255 ? 255 : G);
B = B < 0 ? 0 : (B > 255 ? 255 : B); B = B < 0 ? 0 : (B > 255 ? 255 : B);
writePix(dst, x + 1, y, (u8)R, (u8)G, (u8)B); put_pixel(d, x + 1, y, (u8)R, (u8)G, (u8)B);
} }
} }
} }
static void convert_display_to_row_major(const u8 *disp, u8 *out) { static void display_to_row_major(const u8 *in, u8 *out) {
for (int y = 0; y < HEIGHT; ++y) { for (int y = 0; y < H; ++y) {
for (int x = 0; x < WIDTH; ++x) { for (int x = 0; x < W; ++x) {
int disp_off = (x * HEIGHT + (HEIGHT - 1 - y)) * 3; int di = (x * H + (H - 1 - y)) * 3;
int out_off = (y * WIDTH + x) * 3; int oi = (y * W + x) * 3;
out[out_off + 0] = disp[disp_off + 0]; out[oi + 0] = in[di + 0];
out[out_off + 1] = disp[disp_off + 1]; out[oi + 1] = in[di + 1];
out[out_off + 2] = disp[disp_off + 2]; out[oi + 2] = in[di + 2];
} }
} }
} }
static int encode_bgr_to_jpeg_file(const u8 *in_bgr, int width, int height, static int write_jpeg(const u8 *bgr, const char *path) {
int quality, const char *path) {
struct jpeg_compress_struct cinfo; struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr; struct jpeg_error_mgr jerr;
FILE *f = NULL; FILE *f = fopen(path, "wb");
unsigned char *row = NULL;
int row_stride = width * 3;
if (!in_bgr || !path)
return -1;
f = fopen(path, "wb");
if (!f) if (!f)
return -1; return -1;
cinfo.err = jpeg_std_error(&jerr); cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo); jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, f); jpeg_stdio_dest(&cinfo, f);
cinfo.image_width = width; cinfo.image_width = W;
cinfo.image_height = height; cinfo.image_height = H;
cinfo.input_components = 3; cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB; cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo); jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE); jpeg_set_quality(&cinfo, JPG_QUALITY, TRUE);
jpeg_start_compress(&cinfo, TRUE); jpeg_start_compress(&cinfo, TRUE);
row = malloc(row_stride); int rowstride = W * 3;
u8 *row = malloc(rowstride);
if (!row) { if (!row) {
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo); jpeg_destroy_compress(&cinfo);
fclose(f); fclose(f);
return -1; return -1;
} }
while (cinfo.next_scanline < cinfo.image_height) { while (cinfo.next_scanline < cinfo.image_height) {
const u8 *src = in_bgr + (size_t)cinfo.next_scanline * (size_t)row_stride; const u8 *src = bgr + (size_t)cinfo.next_scanline * rowstride;
for (int x = 0; x < width; ++x) { for (int x = 0; x < W; ++x) {
row[x * 3 + 0] = src[x * 3 + 2]; row[x * 3 + 0] = src[x * 3 + 2];
row[x * 3 + 1] = src[x * 3 + 1]; row[x * 3 + 1] = src[x * 3 + 1];
row[x * 3 + 2] = src[x * 3 + 0]; row[x * 3 + 2] = src[x * 3 + 0];
} }
JSAMPROW row_pointer[1]; JSAMPROW rp[1];
row_pointer[0] = row; rp[0] = row;
jpeg_write_scanlines(&cinfo, row_pointer, 1); jpeg_write_scanlines(&cinfo, rp, 1);
} }
free(row); free(row);
jpeg_finish_compress(&cinfo); jpeg_finish_compress(&cinfo);
@ -163,251 +160,226 @@ static int encode_bgr_to_jpeg_file(const u8 *in_bgr, int width, int height,
return 0; return 0;
} }
Result http_post_file(const char *url, const char *filename) { static Result post_multipart(const char *url, const char *filepath) {
Result ret = 0; FILE *f = fopen(filepath, "rb");
httpcContext context;
u32 statuscode = 0;
FILE *f = fopen(filename, "rb");
if (!f) if (!f)
return -1; return -1;
if (fseek(f, 0, SEEK_END) != 0) { if (fseek(f, 0, SEEK_END) != 0) {
fclose(f); fclose(f);
return -1; return -1;
} }
long fsize = ftell(f); long sz = ftell(f);
if (fsize < 0) { if (sz < 0) {
fclose(f); fclose(f);
return -1; return -1;
} }
rewind(f); rewind(f);
u8 *filebuf = malloc((size_t)fsize); u8 *filebuf = malloc((size_t)sz);
if (!filebuf) { if (!filebuf) {
fclose(f); fclose(f);
return -1; return -1;
} }
if (fread(filebuf, 1, (size_t)fsize, f) != (size_t)fsize) { if (fread(filebuf, 1, (size_t)sz, f) != (size_t)sz) {
free(filebuf); free(filebuf);
fclose(f); fclose(f);
return -1; return -1;
} }
fclose(f); fclose(f);
char boundary[64]; char boundary[48];
srand((unsigned)time(NULL)); snprintf(boundary, sizeof(boundary), "----printcam%08x", (unsigned)rand());
snprintf(boundary, sizeof(boundary), "----printcam%08x", rand()); const char *fn = strrchr(filepath, '/');
const char *filename_only = filename; if (fn)
const char *p = strrchr(filename, '/'); fn++;
if (p) else
filename_only = p + 1; fn = filepath;
char preamble[512]; char head[256];
int pre_len = snprintf( int headlen = snprintf(
preamble, sizeof(preamble), head, sizeof(head),
"--%s\r\n" "--%s\r\n"
"Content-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n" "Content-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n"
"Content-Type: image/jpeg\r\n\r\n", "Content-Type: image/jpeg\r\n\r\n",
boundary, filename_only); boundary, fn);
const char *epilogue_fmt = "\r\n--%s--\r\n"; char tail[64];
int epilogue_len = snprintf(NULL, 0, epilogue_fmt, boundary); int taillen = snprintf(tail, sizeof(tail), "\r\n--%s--\r\n", boundary);
char *epilogue = malloc((size_t)epilogue_len + 1); size_t total = (size_t)headlen + (size_t)sz + (size_t)taillen;
if (!epilogue) { u8 *buf = malloc(total);
if (!buf) {
free(filebuf); free(filebuf);
return -1; return -1;
} }
snprintf(epilogue, (size_t)epilogue_len + 1, epilogue_fmt, boundary); memcpy(buf, head, (size_t)headlen);
size_t multipart_size = memcpy(buf + headlen, filebuf, (size_t)sz);
(size_t)pre_len + (size_t)fsize + (size_t)epilogue_len; memcpy(buf + headlen + sz, tail, (size_t)taillen);
u8 *multipart_buf = malloc(multipart_size);
if (!multipart_buf) {
free(filebuf);
free(epilogue);
return -1;
}
memcpy(multipart_buf, preamble, (size_t)pre_len);
memcpy(multipart_buf + pre_len, filebuf, (size_t)fsize);
memcpy(multipart_buf + pre_len + fsize, epilogue, (size_t)epilogue_len);
free(filebuf); free(filebuf);
free(epilogue); httpcContext ctx;
ret = httpcOpenContext(&context, HTTPC_METHOD_POST, url, 0); Result r = httpcOpenContext(&ctx, HTTPC_METHOD_POST, url, 0);
if (ret != 0) { if (r != 0) {
free(multipart_buf); free(buf);
return ret; return r;
} }
httpcSetSSLOpt(&context, SSLCOPT_DisableVerify); httpcSetSSLOpt(&ctx, SSLCOPT_DisableVerify);
httpcSetKeepAlive(&context, HTTPC_KEEPALIVE_ENABLED); httpcSetKeepAlive(&ctx, HTTPC_KEEPALIVE_ENABLED);
char content_type_header[128]; char ct[128];
snprintf(content_type_header, sizeof(content_type_header), snprintf(ct, sizeof(ct), "multipart/form-data; boundary=%s", boundary);
"multipart/form-data; boundary=%s", boundary); httpcAddRequestHeaderField(&ctx, "User-Agent", "printcam/1.0");
httpcAddRequestHeaderField(&context, "User-Agent", "printcam/1.0"); httpcAddRequestHeaderField(&ctx, "Content-Type", ct);
httpcAddRequestHeaderField(&context, "Content-Type", content_type_header); r = httpcAddPostDataRaw(&ctx, (u32 *)buf, (u32)total);
ret = if (r != 0) {
httpcAddPostDataRaw(&context, (u32 *)multipart_buf, (u32)multipart_size); httpcCloseContext(&ctx);
if (ret != 0) { free(buf);
httpcCloseContext(&context); return r;
free(multipart_buf);
return ret;
} }
ret = httpcBeginRequest(&context); r = httpcBeginRequest(&ctx);
if (ret != 0) { if (r == 0) {
httpcCloseContext(&context); u32 sc = 0;
free(multipart_buf); httpcGetResponseStatusCode(&ctx, &sc);
return ret;
}
ret = httpcGetResponseStatusCode(&context, &statuscode);
if (ret == 0) {
u32 downloaded = 0; u32 downloaded = 0;
u32 contentsize = 0; u32 contentsize = 0;
while (true) { while (httpcGetDownloadSizeState(&ctx, &downloaded, &contentsize) == 0 &&
ret = httpcGetDownloadSizeState(&context, &downloaded, &contentsize); contentsize != 0 && downloaded < contentsize) {
if (ret != 0) u32 rem = contentsize - downloaded;
break; u32 toread = rem > 4096 ? 4096 : rem;
if (contentsize == 0)
break;
if (downloaded >= contentsize)
break;
u32 remaining = contentsize - downloaded;
u32 toread = remaining > 4096 ? 4096 : remaining;
u8 tmp[4096]; u8 tmp[4096];
ret = httpcReceiveData(&context, tmp, toread); if (httpcReceiveData(&ctx, tmp, toread) != 0)
if (ret != 0)
break; break;
} }
} }
httpcCloseContext(&context); httpcCloseContext(&ctx);
free(multipart_buf); free(buf);
return ret; return r;
} }
static void cameraThreadFunc(void *arg) { static void setup_camera(u32 max) {
acInit(); u32 sel = camIndex ? SELECT_IN1 : SELECT_OUT1;
camInit();
u32 sel = cam_selects[camera_index];
CAMU_SetSize(sel, SIZE_CTR_TOP_LCD, CONTEXT_A); CAMU_SetSize(sel, SIZE_CTR_TOP_LCD, CONTEXT_A);
CAMU_SetOutputFormat(sel, OUTPUT_YUV_422, CONTEXT_A); CAMU_SetOutputFormat(sel, OUTPUT_YUV_422, CONTEXT_A);
CAMU_SetFrameRate(sel, FRAME_RATE_30); CAMU_SetFrameRate(sel, FRAME_RATE_30);
CAMU_SetNoiseFilter(sel, true); CAMU_SetNoiseFilter(sel, true);
CAMU_SetAutoExposure(sel, true); CAMU_SetAutoExposure(sel, true);
CAMU_SetAutoWhiteBalance(sel, true); CAMU_SetAutoWhiteBalance(sel, true);
u32 maxBytes; CAMU_SetTransferBytes(PORT_BOTH, max, W, H);
CAMU_GetMaxBytes(&maxBytes, WIDTH, HEIGHT);
CAMU_SetTransferBytes(PORT_BOTH, maxBytes, WIDTH, HEIGHT);
CAMU_Activate(sel); CAMU_Activate(sel);
Handle ev = 0; }
static void cam_thread(void *arg) {
acInit();
camInit();
u32 max;
CAMU_GetMaxBytes(&max, W, H);
setup_camera(max); // Initial setup
CAMU_ClearBuffer(PORT_BOTH); CAMU_ClearBuffer(PORT_BOTH);
CAMU_SynchronizeVsyncTiming(SELECT_OUT1, SELECT_OUT2); CAMU_SynchronizeVsyncTiming(SELECT_OUT1, SELECT_OUT2);
CAMU_StartCapture(PORT_BOTH); CAMU_StartCapture(PORT_BOTH);
while (!exitRequested) {
if (camera_reinit) { while (!quit) {
sel = cam_selects[camera_index]; if (reinitCam) {
CAMU_StopCapture(PORT_BOTH); CAMU_StopCapture(PORT_BOTH);
CAMU_ClearBuffer(PORT_BOTH); CAMU_ClearBuffer(PORT_BOTH);
CAMU_SetSize(sel, SIZE_CTR_TOP_LCD, CONTEXT_A); setup_camera(max); // Reuse same setup logic
CAMU_SetOutputFormat(sel, OUTPUT_YUV_422, CONTEXT_A);
CAMU_SetFrameRate(sel, FRAME_RATE_30);
CAMU_SetNoiseFilter(sel, true);
CAMU_SetAutoExposure(sel, true);
CAMU_SetAutoWhiteBalance(sel, true);
CAMU_SetTransferBytes(PORT_BOTH, maxBytes, WIDTH, HEIGHT);
CAMU_Activate(sel);
CAMU_StartCapture(PORT_BOTH); CAMU_StartCapture(PORT_BOTH);
camera_reinit = false; reinitCam = false;
} }
CAMU_SetReceiving(&ev, camBuf[writeIdx], PORT_CAM1, WIDTH * HEIGHT * 2,
maxBytes); Handle ev = 0;
svcWaitSynchronization(ev, U64_MAX); int recv = camIndex ? PORT_CAM2 : PORT_CAM1;
CAMU_SetReceiving(&ev, camBuf[writeIdx], recv, W * H * 2, max);
svcWaitSynchronization(ev, CAM_TIMEOUT_NS);
svcCloseHandle(ev); svcCloseHandle(ev);
svcSignalEvent(ready[writeIdx]); svcSignalEvent(readyEv[writeIdx]);
writeIdx ^= 1; writeIdx ^= 1;
} }
CAMU_StopCapture(PORT_BOTH); CAMU_StopCapture(PORT_BOTH);
CAMU_Activate(SELECT_NONE); CAMU_Activate(SELECT_NONE);
camExit(); camExit();
acExit(); acExit();
} }
int main(int argc, char *argv[]) { int main(int argc, char **argv) {
gfxInitDefault(); gfxInitDefault();
httpcInit(4 * 1024 * 1024); httpcInit(4 * 1024 * 1024);
consoleInit(GFX_BOTTOM, &bottomScreen); consoleInit(GFX_BOTTOM, &bottom);
consoleSelect(&bottomScreen); consoleSelect(&bottom);
printf("printcam\nL/R=take X=switch-front/back Y=reload-url START=exit\n"); printf("printcam\nL/R=take X=switch Y=reload START=exit\n");
if (!ensure_storage_dir()) int rc = init_endpoint_config();
printf("Failed to create %s\n", STORAGE_DIR); if (rc == 1)
if (!ensure_default_endpoint_file()) printf("Endpoint loaded: %s\n", endpoint);
printf("Failed to create default endpoint file\n"); else if (rc == 0)
if (load_endpoint_from_sd() == 0) printf("Endpoint created: %s\n", endpoint);
printf("Endpoint: %s\n", api_endpoint); else
camBuf[0] = (u8 *)memalign(0x40, WIDTH * HEIGHT * 2); printf("Endpoint init failed errno=%d %s\n", errno, strerror(errno));
camBuf[1] = (u8 *)memalign(0x40, WIDTH * HEIGHT * 2); camBuf[0] = memalign(0x40, W * H * 2);
rgbBuf = (u8 *)memalign(0x40, FB_SIZE); camBuf[1] = memalign(0x40, W * H * 2);
u8 *linearBuf = malloc(FB_SIZE); dispBuf = memalign(0x40, FB_BYTES);
if (!camBuf[0] || !camBuf[1] || !rgbBuf || !linearBuf) { u8 *linear = malloc(FB_BYTES);
printf("Out of memory\n"); if (!camBuf[0] || !camBuf[1] || !dispBuf || !linear) {
goto cleanup; printf("OOM\n");
goto end;
} }
svcCreateEvent(&ready[0], RESET_ONESHOT); svcCreateEvent(&readyEv[0], RESET_ONESHOT);
svcCreateEvent(&ready[1], RESET_ONESHOT); svcCreateEvent(&readyEv[1], RESET_ONESHOT);
gfxSetDoubleBuffering(GFX_TOP, true); gfxSetDoubleBuffering(GFX_TOP, true);
camThread = threadCreate(cameraThreadFunc, NULL, 0x1000, 0x2F, -2, false); camThread = threadCreate(cam_thread, NULL, 0x1000, 0x2F, -2, false);
while (aptMainLoop()) { while (aptMainLoop()) {
hidScanInput(); hidScanInput();
u32 k = hidKeysDown(); u32 k = hidKeysDown();
if (k & KEY_START) if (k & KEY_START)
break; break;
if (k & KEY_X) { if (k & KEY_X) {
camera_index ^= 1; camIndex ^= 1;
camera_reinit = true; reinitCam = true;
printf("Switched to %s\n", printf("Switched to %s\n", camIndex ? "front" : "back");
camera_index ? "front/input1" : "back/output1");
} }
if (k & KEY_Y) { if (k & KEY_Y) {
if (load_endpoint_from_sd() == 0) int r = init_endpoint_config();
printf("Reloaded endpoint: %s\n", api_endpoint); if (r >= 0)
printf("Reload=%d endpoint=%s\n", r, endpoint);
else else
printf("Failed to reload endpoint\n"); printf("Reload failed errno=%d %s\n", errno, strerror(errno));
} }
if (k & (KEY_L | KEY_R)) { if (k & (KEY_L | KEY_R)) {
consoleSelect(&bottomScreen); consoleSelect(&bottom);
printf("Snapshot requested\n"); printf("Snapshot\n");
convert_display_to_row_major(rgbBuf, linearBuf); display_to_row_major(dispBuf, linear);
char jpgpath[128]; char path[128];
time_t t = time(NULL); time_t t = time(NULL);
strftime(jpgpath, sizeof(jpgpath), STORAGE_DIR "/%Y%m%d_%H%M%S.jpg", strftime(path, sizeof(path), DIR "/%Y%m%d_%H%M%S.jpg", localtime(&t));
localtime(&t)); if (write_jpeg(linear, path) == 0) {
if (encode_bgr_to_jpeg_file(linearBuf, WIDTH, HEIGHT, JPEG_QUALITY, printf("Wrote %s\n", path);
jpgpath) == 0) { Result r = post_multipart(endpoint, path);
printf("Wrote %s\n", jpgpath); printf("upload: 0x%" PRIx32 "\n", (u32)r);
Result res = http_post_file(api_endpoint, jpgpath);
printf("http_post_file returned: %" PRIx32 "\n", (u32)res);
} else { } else {
printf("JPEG write failed\n"); printf("JPEG failed\n");
} }
} }
gspWaitForVBlank(); gspWaitForVBlank();
if (svcWaitSynchronization(ready[dispIdx], 0) == 0) { if (svcWaitSynchronization(readyEv[dispIdx], 0) == 0) {
blitYUYVtoRGB(camBuf[dispIdx], rgbBuf); yuyv_to_display(camBuf[dispIdx], dispBuf);
dispIdx ^= 1; dispIdx ^= 1;
} }
u8 *fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL); u8 *fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
memcpy(fb, rgbBuf, FB_SIZE); memcpy(fb, dispBuf, FB_BYTES);
gfxSwapBuffersGpu(); gfxSwapBuffersGpu();
} }
cleanup: end:
exitRequested = true; quit = true;
if (camThread) if (camThread)
threadJoin(camThread, U64_MAX); threadJoin(camThread, U64_MAX);
if (camThread) if (camThread)
threadFree(camThread); threadFree(camThread);
if (ready[0]) if (readyEv[0])
svcCloseHandle(ready[0]); svcCloseHandle(readyEv[0]);
if (ready[1]) if (readyEv[1])
svcCloseHandle(ready[1]); svcCloseHandle(readyEv[1]);
if (camBuf[0]) if (camBuf[0])
free(camBuf[0]); free(camBuf[0]);
if (camBuf[1]) if (camBuf[1])
free(camBuf[1]); free(camBuf[1]);
if (rgbBuf) if (dispBuf)
free(rgbBuf); free(dispBuf);
if (linearBuf) if (linear)
free(linearBuf); free(linear);
httpcExit(); httpcExit();
gfxExit(); gfxExit();
return 0; return 0;