2020-06-30 12:34:31 +02:00
|
|
|
/****************************************************************************
|
2021-09-16 04:03:33 +02:00
|
|
|
* apps/examples/camera/camera_fileutil.c
|
2020-06-30 12:34:31 +02:00
|
|
|
*
|
2021-09-16 04:03:33 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2020-06-30 12:34:31 +02:00
|
|
|
*
|
2021-09-16 04:03:33 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2020-06-30 12:34:31 +02:00
|
|
|
*
|
2021-09-16 04:03:33 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2020-06-30 12:34:31 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "camera_fileutil.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define IMAGE_FILENAME_LEN (32)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-12-07 03:36:41 +01:00
|
|
|
static FAR const char *g_save_dir;
|
2022-12-06 12:20:55 +01:00
|
|
|
static int g_framecount;
|
2020-06-30 12:34:31 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: futil_initialize()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Choose strage to write a file.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-12-07 03:36:41 +01:00
|
|
|
FAR const char *futil_initialize(void)
|
2020-06-30 12:34:31 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct stat stat_buf;
|
|
|
|
|
|
|
|
/* In SD card is available, use SD card.
|
|
|
|
* Otherwise, use SPI flash.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = stat("/mnt/sd0", &stat_buf);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-12-06 12:20:55 +01:00
|
|
|
g_save_dir = "/mnt/spif";
|
2020-06-30 12:34:31 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-06 12:20:55 +01:00
|
|
|
g_save_dir = "/mnt/sd0";
|
2020-06-30 12:34:31 +02:00
|
|
|
}
|
|
|
|
|
2022-12-06 12:20:55 +01:00
|
|
|
return g_save_dir;
|
2020-06-30 12:34:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: futil_writeimage()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write a image file to selected storage.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-12-07 03:36:41 +01:00
|
|
|
int futil_writeimage(FAR uint8_t *data, size_t len, FAR const char *fsuffix)
|
2020-06-30 12:34:31 +02:00
|
|
|
{
|
2022-12-06 12:20:55 +01:00
|
|
|
char fname[IMAGE_FILENAME_LEN];
|
2022-12-07 03:36:41 +01:00
|
|
|
FAR FILE *fp;
|
2020-06-30 12:34:31 +02:00
|
|
|
|
2022-12-06 12:20:55 +01:00
|
|
|
g_framecount++;
|
|
|
|
if (g_framecount >= 1000)
|
2020-06-30 12:34:31 +02:00
|
|
|
{
|
2022-12-06 12:20:55 +01:00
|
|
|
g_framecount = 1;
|
2020-06-30 12:34:31 +02:00
|
|
|
}
|
|
|
|
|
2022-12-06 12:20:55 +01:00
|
|
|
snprintf(fname,
|
2020-06-30 12:34:31 +02:00
|
|
|
IMAGE_FILENAME_LEN,
|
|
|
|
"%s/VIDEO%03d.%s",
|
2022-12-06 12:20:55 +01:00
|
|
|
g_save_dir, g_framecount, fsuffix);
|
2020-06-30 12:34:31 +02:00
|
|
|
|
2022-12-06 12:20:55 +01:00
|
|
|
printf("FILENAME:%s\n", fname);
|
2020-06-30 12:34:31 +02:00
|
|
|
|
2022-12-06 12:20:55 +01:00
|
|
|
fp = fopen(fname, "wb");
|
2020-06-30 12:34:31 +02:00
|
|
|
if (NULL == fp)
|
|
|
|
{
|
|
|
|
printf("fopen error : %d\n", errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len != fwrite(data, 1, len, fp))
|
|
|
|
{
|
|
|
|
printf("fwrite error : %d\n", errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|