& r' V1 m4 b9 h: V; T* W
分享一个STM32移植FAFTS文件系统遍历创建文件夹$ k u' c' T* t' J0 E: p: _
- FRESULT folder_create(char* path)! U2 O+ F" ]& h* U4 d7 k. r
- {
3 W9 i L( ^1 F4 K; t: Q - FRESULT res;5 j3 k( A: Q5 ]5 W% o- O9 H' z
- DIR dir;# B# r: |9 ^0 r
- char *str = path;
9 D3 }1 U. x r7 v/ X8 i. h - char *pbuf;7 K) e: J& ]9 n) |$ }8 f, h8 y: O
- u16 cnt=0;! @, |' ?/ z' K; w& _
-
) T9 V( ~, L. u' U7 x - res = f_opendir(&dir, path); 6 n2 t' G& V- ~. {: P- }7 y
- if (res == FR_OK) 6 @8 L7 s7 z0 {
- {
! \) Z/ I" h7 ^1 i I7 t+ ?: s5 H1 z m - f_closedir(&dir);
" Q: t7 _$ Y( p) C P. k) X& U1 P - return FR_OK;+ O o* ]- J% r/ ?8 T
- }2 k3 R# A8 A8 y0 m4 O7 J) E
- else1 s* m8 L6 i$ `
- {) _- x! u2 J' l3 [6 P- J# ]
- pbuf=mymalloc(SRAMIN,200);
, I7 R* n) M: q! F" B! } - if(pbuf==NULL)- u- m3 a$ V) Z) r
- return FR_NOT_ENOUGH_CORE;
+ y& z- j+ U' [% B4 f - while(*str !=0)
/ }5 ?! X0 ]! V8 z - {# W2 v$ A* D+ v5 F* |
- if(*str=='/')! t. f1 w0 U6 s0 s' D# k8 G6 J5 g& n
- {
# z" u5 ? a, v# ~8 V3 @# \* ^7 k2 f - memset(pbuf,0x00,200);% I/ e/ Z0 N2 F
- memcpy(pbuf,path,cnt);
( z2 `2 b9 A7 r5 ^. c) e - printf("%s\r\n",pbuf);8 ]' F: O- o& h7 K9 o4 A- z
1 P0 M2 ]( m1 @8 S) b- if(f_opendir(&dir, pbuf)==FR_NO_PATH)
) U5 w1 A0 s2 \9 Y - f_mkdir(pbuf);9 r$ y/ t( l* h
- else
% `2 x @1 M: b - f_closedir(&dir); $ x. G, v8 \4 P( N
- }9 x" `+ ^6 B5 L
-
1 c) K4 L2 d) [# M: T - cnt++;. T& u. S- ~: r2 c
-
$ d( f" Z& k6 l - str++;
3 k- O/ i# k+ G - }( S& x1 U' j A7 C/ E# i) f+ r
- myfree(SRAMIN,pbuf);
& h h& R) x, ? \ - res = f_mkdir(path);//创建最后一级目录* I5 C! U# S) U5 f% C, |
-
. J' U L8 j2 r0 ` - }( h8 m, ^! I: L7 o$ e
- return res;
( C4 |2 M+ p6 e% S# u - }
7 B R( J/ G+ W: r
复制代码 调用方法:' G; ?( W! P& N3 u
- folder_create("0:/ccc/x1/x2/x3/x4/x5/x6/x7/x8");
5 Q$ \) [2 M5 P) J" \
复制代码 7 K9 T8 a) G5 p+ v6 y5 d4 Y7 M% h
$ a! v8 Y" c8 s3 S; H$ \% M+ z1 m7 N8 t4 c: B
" i7 d. l" T+ ^. M |
#include <stdlib.h># N7 x) a8 g) }3 m& L
#define LRC_TABLE_MAX 100 # G6 d6 f( U; E& ^% G( W
static uint16_t g_usaLrcTimeTable[LRC_TABLE_MAX]; //记录歌词时间
static char g_caLrcTextTable[LRC_TABLE_MAX][100]; //记录歌词文本
static uint8_t g_ucLrcTableIndex; //歌词"时间/文件表"的索引值& L) G( ]3 x( G% n
/*****************************************************************************\
函数功能:歌词解析
形参说明:_cpLrcPath 歌词路径5 E" d: E7 b g
返 回 值:0 - 成功 / 1 - 失败2 W0 b; E5 Z- A6 j# a. U: H( [
\*****************************************************************************/
u8 VS1053B_Parse_MusicLrc(const char *_cpLrcPath)7 `4 x+ g0 r1 w* a& D% b4 j9 S
{
//变量定义2 I% X7 J* U9 h8 P# I) ?
FIL fp; //文件指针% c$ `: X" \. R9 D
FRESULT res; //文件系统返回值
UINT br; //成功读取的字节数9 @& c2 K! D. F* P; g& K
uint16_t usTime = 0; //用于临时存放转换计算后的时间
uint32_t ulFileSize; //歌词文件总大小
uint32_t i = 0, j = 0; //临时变量
char *ptmp = NULL; //歌词内容指针
char *pbuff = NULL; //用于存放所有歌词内容2 M, r; O. G6 T8 b
* }* z! S! W2 d3 G4 p) @. L
//打开文件
res = f_open(&fp, _cpLrcPath, FA_READ);
if (res == FR_OK)
{5 E( y0 a5 ?( g" \( u; S( R' S
//计算文件大小; ~6 x9 D+ y0 M
ulFileSize = f_size(&fp);% j+ m/ m: f' j j) {% o
//申请空间% t+ [0 }/ v2 N+ |$ c
pbuff = malloc(ulFileSize);
if (pbuff != NULL)
{+ a# B( C% F$ I; ]3 {3 m1 s
//一次性将总缓冲区读满 ^$ a$ b+ }/ a6 ^2 [& Z
res = f_read(&fp, pbuff, ulFileSize, &br);. D$ b- B+ ]4 }. o" q7 N5 H
if (res == FR_OK && br == ulFileSize)
{
//找到歌词的起始位置
i = 0;
ptmp = strstr(pbuff, "["); : U9 C3 y# L+ v/ v
while (ptmp[i] != '\0') //遍历缓冲区中的信息
{% E0 x4 E+ \* p
/**********************解析时间**********************/4 j8 S+ _$ O" y1 Y3 w4 u
//[00:40.452]
//[分:秒.毫秒]
usTime = 0;4 F% S+ b+ Y: E, x: h$ a( b
//计算该行歌词播放时间(秒),毫秒部分四舍五入
usTime += (ptmp[i++] - '0') * 60 * 10; //分(十位) x2 n& G5 L- } F' G
usTime += (ptmp[i++] - '0') * 60 * 1; //分(个位)
i++; // ':') @+ y8 f: D% j- i
usTime += (ptmp[i++] - '0') * 10; //秒(十位)8 G% l6 v, K$ O+ p3 J( A. e
usTime += (ptmp[i++] - '0') * 1; //秒(个位)
i++; // '.'
if (ptmp[i++] >= 5) usTime++;
//手动调整时间误差
if (usTime > 5) usTime -= (usTime / 10 + 1);7 b$ B% @& O2 }$ W5 b7 w" l
//保存时间
g_usaLrcTimeTable[g_ucLrcTableIndex] = usTime;
printf("%02d:%02d ", usTime/60, usTime%60);8 a2 o' {5 t; t3 d; w
/**********************拷贝歌词**********************/( K3 g0 r3 y* o0 }; t# Y
j = 0;
//让指针偏移到歌词文本部分
while (ptmp[i++] != ']');
//拷贝歌词内容& G* a- N% I* J. W) ]8 {$ w5 o
while (ptmp[i] != '\0' && ptmp[i] != '[') - M1 R2 A: \3 U& Z, p; R4 a/ P# |
{
g_caLrcTextTable[g_ucLrcTableIndex][j++] = ptmp[i++]; - b+ l" o7 @' f/ k; ~0 N
}
//制作成字符串
g_caLrcTextTable[g_ucLrcTableIndex][j] = '\0';
printf("%s\r\n", g_caLrcTextTable[g_ucLrcTableIndex]);
g_ucLrcTableIndex++;
}
}
free(pbuff);
} ) P) e3 ^7 h2 k/ x2 P
f_close(&fp);
}/ y% h2 ?) z2 S9 R- J3 m* g* n }& D8 e& S
if (i != 0) return 0;
else return 1;
}