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

353 lines
5.6 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
2025-07-05 19:47:28 +08:00
/*------------------------<2D>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>غ<EFBFBD><D8BA><EFBFBD>-----------------------------*/
2025-06-27 00:32:57 +08:00
2025-07-05 19:47:28 +08:00
// 1,ʹ<><CAB9>nrf<72><66><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ô<EFBFBD><C3B4><EFBFBD>
2025-06-27 00:32:57 +08:00
#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)
{
2025-07-05 19:47:28 +08:00
// <20>ȴ<EFBFBD>ֱ<EFBFBD><D6B1>GPS<50><53><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3>˳<EFBFBD>
2025-06-27 00:32:57 +08:00
rt_thread_delay(5);
}
}
return 0;
}
2025-07-05 19:47:28 +08:00
// <20>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡<EFBFBD><D6A1><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>1
2025-06-27 00:32:57 +08:00
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;
}
2025-07-05 19:47:28 +08:00
/*------------------------<2D>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>غ<EFBFBD><D8BA><EFBFBD>End--------------------------*/
2025-06-27 00:32:57 +08:00
2025-07-05 19:47:28 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>
2025-06-27 00:32:57 +08:00
int rand(void)
{
RANDOM_Init();
return RANDOM_Get();
}
2025-07-05 19:47:28 +08:00
// <20>ض<EFBFBD><D8B6><EFBFBD>rt-thread <20>Ĵ<EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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;
2025-07-05 19:47:28 +08:00
int wchar;//˫<>ֽ<EFBFBD><D6BD>ַ<EFBFBD>
2025-06-27 00:32:57 +08:00
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);
2025-07-05 19:47:28 +08:00
// x<><78><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
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);
}
}
2025-07-05 19:47:28 +08:00
// y<><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
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--;
}
}
2025-07-05 19:47:28 +08:00
// <20>һ<E6B4A2><D2BB><EFBFBD>ַ<EFBFBD>
2025-06-27 00:32:57 +08:00
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
2025-07-05 19:47:28 +08:00
// <20><><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>Ļ
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;
}
}
}
}