Files
player/Project/Src/MyApp/tools.c

353 lines
5.7 KiB
C
Raw Normal View History

2025-06-27 00:32:57 +08:00
#include "tools.h"
#include "nrf.h"
#include "mymem.h"
#include "system_file.h"
#include "system_updata.h"
#include "random.h"
#include "libc.h"
#ifndef BOOTLOADER
#include "rthw.h"
#define IRQ_DISABLE() rt_enter_critical()
#define IRQ_ENABLE() rt_exit_critical()
#else
#define IRQ_DISABLE() { }
#define IRQ_ENABLE() { }
#endif
/*------------------------文件传输相关函数-----------------------------*/
// 1,使用nrf否则使用串口
#define USE_NRF 0
static int tools_updata_thread_on(void);
static int tools_updata_thread_off(void);
int tools_updata_on(void)
{
tools_updata_thread_on();
return 0;
}
int tools_updata_off(void)
{
tools_updata_thread_off();
return 0;
}
static int g_updata_work;
static int g_updata_down;
static int tools_updata_all_frame(uint8_t *data,int *len,uint8_t dat);
static void cd_updata_thread(void *t)
{
g_updata_down=0;
uint8_t *data=mymalloc(256);
int len=0;
#if USE_NRF==1
if(nrf_init()!=NRF_OK)
{
g_updata_down=1;
myfree(data);
return ;
}
#else
FILE *f=fopen("usart","r");
if(f==NULL) return;
#endif
while(1)
{
rt_thread_delay(20);
if(g_updata_work)
{
uint8_t dat=0;
#if USE_NRF==1
while(nrf_read_byte(&dat)==NRF_OK)
#else
int get_c;
while(get_c=getc(f),dat=get_c,get_c!=EOF)
#endif
{
if(tools_updata_all_frame(data,&len,dat))
{
SysFile_UpdataByIrq(data,len);
#if USE_NRF==1
nrf_send((uint8_t []){0},1,0);
#else
putc(0,f);
#endif
len=0;
mymemset(data,0,4);
}
}
}
else
break;
}
#if USE_NRF==1
nrf_deinit();
#else
fclose(f);
#endif
myfree(data);
g_updata_down=1;
}
static int tools_updata_thread_on(void)
{
if(g_updata_work==0)
{
g_updata_work=1;
rt_thread_t t=rt_thread_create("updata",cd_updata_thread,0,512,5,20);
rt_thread_startup(t);
return 0;
}
return -1;
}
static int tools_updata_thread_off(void)
{
if(g_updata_work)
{
g_updata_work=0;
while(g_updata_down==0)
{
// 等待直到GPS服务线程退出
rt_thread_delay(5);
}
}
return 0;
}
// 判断是否是一个完整帧是返回1
static int tools_updata_all_frame(uint8_t *data,int *len,uint8_t dat)
{
if(*len<0) return 0;
if(*len<2) {
if(dat==0xff)
{data[*len]=dat;(*len)++;}
}
else
{
data[*len]=dat;(*len)++;
if(*len>=4)
{
if(((data[2]<<8)|data[3])==*len-2)
return 1;
}
}
return 0;
}
/*------------------------文件传输相关函数End--------------------------*/
// 获取随机数函数实现
int rand(void)
{
RANDOM_Init();
return RANDOM_Get();
}
// 重定向rt-thread 的打印输出函数
2025-06-29 11:20:46 +08:00
// void rt_hw_console_output(const char *str)
// {
// while(*str)
// {
// fputc(*str, stdout);
// str++;
// }
// }
2025-06-27 00:32:57 +08:00
#define chars_wid (480/8)
#define chars_hid (272/16)
typedef struct{
int pos_x;
int pos_y;
int wchar;//双字节字符
char chars[chars_hid][chars_wid];
}stdout_file;
static stdout_file *g_stdout_file;
2025-06-28 22:15:49 +08:00
static int topen(void)
2025-06-27 00:32:57 +08:00
{
stdout_file *file=g_stdout_file;
if(file==NULL)
{
file=mymalloc(sizeof(stdout_file));
mymemset(file,0,sizeof(stdout_file));
g_stdout_file=file;
}
return 0;
}
2025-06-28 22:15:49 +08:00
static int tclose(void)
2025-06-27 00:32:57 +08:00
{
stdout_file **file=&g_stdout_file;
if(*file)
{
myfree(*file);
*file=0;
}
return 0;
}
static void puts_inc_y(stdout_file *p);
// x方向自增
static void puts_inc_x(stdout_file *p)
{
p->pos_x++;
if(p->pos_x>=chars_wid)
{
p->pos_x=0;
puts_inc_y(p);
}
}
// y方向自增
static void puts_inc_y(stdout_file *p)
{
p->pos_y++;
if(p->pos_y>=chars_hid)
{
memcpy(p->chars[0],p->chars[1],(chars_hid-1)*chars_wid);
memset(p->chars[chars_hid-1],0,chars_wid);
p->pos_y--;
}
}
// 存储一个字符
static void puts_char(stdout_file *p,int ch)
{
p->chars[p->pos_y][p->pos_x]=ch;
}
static int put(int ch)
{
stdout_file *file=g_stdout_file;
if(file)
{
if(ch>0x80)
{
if(file->pos_x>=chars_wid-2)
{
puts_inc_y(file);
file->pos_x=0;
}
file->wchar=1;
puts_char(file,ch);
puts_inc_x(file);
}
else if(file->wchar)
{
puts_char(file,ch);
puts_inc_x(file);
file->wchar=0;
}
else if(ch>=' '&&ch<='~')
{
puts_char(file,ch);
puts_inc_x(file);
}
else if(ch=='\r')
{
file->pos_x=0;
}
else if(ch=='\n') {
puts_inc_y(file);
file->pos_x=0;
}
else if(ch=='\t'){
}
return ch;
}
return EOF;
}
static int puts_(const void *ptr,int size)
{
for(int i=0;i<size;i++)
{
put(((uint8_t *)ptr)[i]);
}
return size;
}
2025-06-28 22:15:49 +08:00
extern_device2(lcd,topen,tclose,put,NULL,puts_,NULL);
2025-06-27 00:32:57 +08:00
// 绘制调试信息到屏幕
void tools_dbg_print(int (*draw_txt)(int c,int x,int y))
{
stdout_file *p=g_stdout_file;
if(draw_txt==NULL) return;
if(p==NULL) return;
uint8_t *str;
for(int y=0;y<chars_hid;y++)
{
for(int x=0;x<chars_wid;)
{
str=(uint8_t *)&p->chars[y][x];
if(str[0]>0x80)
{
draw_txt((str[0]<<8)|str[1],x*8,y*16);
x+=2;
}
else
{
draw_txt(str[0],x*8,y*16);
x+=1;
}
}
}
}