📄 learndlg.cpp
字号:
void Clearndlg::merge_lengths(struct lengths *first)
{
struct lengths *l,*inner,*last;
unsigned long new_sum;
int new_count;
l=first;
while(l!=NULL)
{
last=l;
inner=l->next;
while(inner!=NULL)
{
new_sum=l->sum+inner->sum;
new_count=l->count+inner->count;
if((l->max<=new_sum/new_count+AEPS &&
l->min>=new_sum/new_count-AEPS &&
inner->max<=new_sum/new_count+AEPS &&
inner->min>=new_sum/new_count-AEPS)
||
(l->max<=new_sum/new_count*(100+EPS) &&
l->min>=new_sum/new_count*(100-EPS) &&
inner->max<=new_sum/new_count*(100+EPS) &&
inner->min>=new_sum/new_count*(100-EPS)))
{
l->sum=new_sum;
l->count=new_count;
l->upper_bound=max(l->upper_bound,
inner->upper_bound);
l->lower_bound=min(l->lower_bound,
inner->lower_bound);
l->min=min(l->min,inner->min);
l->max=max(l->max,inner->max);
last->next=inner->next;
free(inner);
inner=last;
}
last=inner;
inner=inner->next;
}
l=l->next;
}
}
void Clearndlg::get_header_length(struct ir_remote *remote,struct lengths **first_pulse,
struct lengths **first_space)
{
unsigned int sum,match,i;
struct lengths *p,*s,*plast,*slast;
struct ir_ncode *codes;
sum=get_sum(remote);
p=*first_pulse;
plast=NULL;
while(p!=NULL)
{
if(p->count>=90*sum/100)
{
s=*first_space;
slast=NULL;
while(s!=NULL)
{
if(s->count>=90*sum/100 &&
(p->count<=sum || s->count<=sum))
{
codes=remote->codes;
match=0;
for(i=0;codes[i].name!=NULL;i++)
{
if(expect(remote,
remote->codes[i].signals[0],
(int) (p->sum/p->count))
&&
expect(remote,
remote->codes[i].signals[1],
(int) (s->sum/s->count)))
{
match++;
}
}
if(match>=sum*90/100)
{
remote->phead=p->sum/p->count;
remote->shead=s->sum/s->count;
p->sum-=sum*p->sum/p->count;
s->sum-=sum*s->sum/s->count;
p->count-=sum;
s->count-=sum;
if(p->count<=0)
{
if(plast==NULL)
{
plast=*first_pulse;
*first_pulse=plast->next;
free(plast);
}
else
{
plast->next=p->next;
free(p);
}
}
if(s->count<=0)
{
if(slast==NULL)
{
slast=*first_space;
*first_space=slast->next;
free(slast);
}
else
{
slast->next=s->next;
free(s);
}
}
return;
}
}
slast=s;
s=s->next;
}
}
plast=p;
p=p->next;
}
}
unsigned long Clearndlg::get_length(struct ir_remote *remote,struct lengths *first,
unsigned long l)
{
while(first!=NULL)
{
if(expect(remote,l,first->sum/first->count))
{
return(first->sum/first->count);
}
first=first->next;
}
return(0);
}
int Clearndlg::is_bit(struct ir_remote *remote,unsigned long pulse, unsigned long space)
{
int i,j,match,sum;
struct ir_ncode *codes;
sum=get_sum(remote);
match=0;
codes=remote->codes;
for(i=0;codes[i].name!=NULL;i++)
{
for(j=has_header(remote) ? 2:0;j+2<codes[i].length;j+=2)
{
if(expect(remote,codes[i].signals[j],pulse) &&
expect(remote,codes[i].signals[j+1],space))
{
match++;
}
}
}
sum*=(remote->bits-1-(has_header(remote) ? 2:0));
sum/=2;
if(match>=20*sum/100)
{
return(1);
}
return(0);
}
int Clearndlg::get_one_length(struct ir_remote *remote,struct lengths **first_pulse,
struct lengths **first_space)
{
int i,j;
struct ir_ncode *codes;
unsigned long pulse,space;
codes=remote->codes;
for(i=0;codes[i].name!=NULL;i++)
{
j=has_header(remote) ? 2:0;
pulse=get_length(remote,*first_pulse,codes[i].signals[j]);
space=get_length(remote,*first_space,codes[i].signals[j+1]);
if(pulse!=0 && space!=0)
{
if(is_bit(remote,pulse,space))
{
remote->pone=pulse;
remote->sone=space;
return(0);
}
}
}
return(-1);
}
int Clearndlg::get_zero_length(struct ir_remote *remote,struct lengths **first_pulse,
struct lengths **first_space)
{
int i,j;
struct ir_ncode *codes;
unsigned long pulse,space;
codes=remote->codes;
for(i=0;codes[i].name!=NULL;i++)
{
for(j=has_header(remote) ? 2:0;j+2<codes[i].length;j+=2)
{
if(expect(remote,codes[i].signals[j],
remote->pone)==0
|| expect(remote,codes[i].signals[j+1],
remote->sone)==0)
{
pulse=get_length(remote,*first_pulse,
codes[i].signals[j]);
space=get_length(remote,*first_space,
codes[i].signals[j+1]);
if(is_bit(remote,pulse,space))
{
remote->pzero=pulse;
remote->szero=space;
return(0);
}
}
}
}
return(-1);
}
int Clearndlg::get_trail_length(struct ir_remote *remote,struct lengths **first_pulse)
{
unsigned int sum,match,i;
struct lengths *p,*plast;
struct ir_ncode *codes;
sum=get_sum(remote);
p=*first_pulse;
plast=NULL;
while(p!=NULL)
{
if(p->count>=sum)
{
codes=remote->codes;
match=0;
for(i=0;codes[i].name!=NULL;i++)
{
if(expect(remote,
remote->codes[i].signals[remote->codes[i].length-1],
(int) (p->sum/p->count)))
{
match++;
}
}
if(match>=sum*90/100)
{
remote->ptrail=p->sum/p->count;
p->sum-=sum*p->sum/p->count;
p->count-=sum;
if(p->count==0)
{
if(plast==NULL)
{
plast=*first_pulse;
*first_pulse=plast->next;
free(plast);
}
else
{
plast->next=p->next;
free(p);
}
}
return(0);
}
}
plast=p;
p=p->next;
}
return(-1);
}
int Clearndlg::get_lengths(struct ir_remote *remote)
{
struct lengths *first_space=NULL,*first_pulse=NULL;
int i,j;
struct ir_ncode *codes;
/* get all spaces */
codes=remote->codes;
for(i=0;codes[i].name!=NULL;i++)
{
for(j=1;codes[i].signals[j]!=0;j+=2)
{
if(-1==add_length(&first_space,codes[i].signals[j]))
{
free_lengths(first_space);
output("add_length failed");
return(-1);
}
}
}
merge_lengths(first_space);
/* and now all pulses */
codes=remote->codes;
for(i=0;codes[i].name!=NULL;i++)
{
for(j=0;j<codes[i].length;j+=2)
{
if(-1==add_length(&first_pulse,codes[i].signals[j]))
{
free_lengths(first_space);
free_lengths(first_pulse);
output("add_length failed");
return(-1);
}
}
}
merge_lengths(first_pulse);
get_header_length(remote,&first_pulse,&first_space);
if(-1==get_trail_length(remote,&first_pulse))
{
free_lengths(first_space);
free_lengths(first_pulse);
output("get_trail_length failed");
return(-1);
}
if(-1==get_one_length(remote,&first_pulse,&first_space))
{
free_lengths(first_space);
free_lengths(first_pulse);
output("get_one_length failed");
return(-1);
}
if(-1==get_zero_length(remote,&first_pulse,&first_space))
{
free_lengths(first_space);
free_lengths(first_pulse);
output("get_zero_length failed");
return(-1);
}
remote->bits--;
if(has_header(remote)) remote->bits-=2;
remote->bits/=2;
if(remote->bits>64) /* can't handle more than 64 bits in normal mode */
{
free_lengths(first_space);
free_lengths(first_pulse);
output("bits>64");
return(-1);
}
#ifndef LONG_IR_CODE
if(remote->bits>32)
{
output("this remote control sends more than 32 bits");
output("recompile the package using LONG_IR_CODE");
return(-1);
}
#endif
free_lengths(first_space);
free_lengths(first_pulse);
return(0);
}
int Clearndlg::get_codes(struct ir_remote *remote)
{
struct ir_ncode *codes;
codes=remote->codes;
while(codes->name!=NULL)
{
rec_buffer.rptr=rec_buffer.wptr=0;
clear_rec_buffer(remote->gap);
current=codes;
use_ir_hardware=false;
if(decode(remote))
{
codes->code=remote->post_data;
remote->post_data=0;
}
else
{
return(-1);
}
codes++;
}
return(0);
}
unsigned long Clearndlg::readdata(unsigned long maxusec)
{
static int i=0;
static struct ir_ncode *codes=NULL;
unsigned long data;
if(codes!=current)
{
i=0;
codes=current;
}
if(i<current->length)
{
data=current->signals[i];
i++;
return(i%2 ? data|PULSE_BIT:data);
}
return(0);
}
void Clearndlg::get_pre_data(struct ir_remote *remote)
{
struct ir_ncode *codes;
ir_code mask,last;
int count,i;
if(remote->bits==0) return;
mask=(-1);
codes=remote->codes;
if(codes->name==NULL) return; /* at least 2 codes needed */
last=codes->code;
codes++;
if(codes->name==NULL) return; /* at least 2 codes needed */
while(codes->name!=NULL)
{
mask&=~(last^codes->code);
last=codes->code;
codes++;
}
count=0;
#ifdef LONG_IR_CODE
while(mask&0x8000000000000000)
#else
while(mask&0x80000000)
#endif
{
count++;
mask=mask<<1;
}
count-=sizeof(ir_code)*CHAR_BIT-remote->bits;
/* only "even" numbers should go to pre/post data */
if(count%8 && (remote->bits-count)%8)
{
count-=count%8;
}
if(count>0)
{
mask=0;
for(i=0;i<count;i++)
{
mask=mask<<1;
mask|=1;
}
remote->bits-=count;
mask=mask<<(remote->bits);
remote->pre_data_bits=count;
remote->pre_data=(last&mask)>>(remote->bits);
codes=remote->codes;
while(codes->name!=NULL)
{
codes->code&=~mask;
codes++;
}
}
}
void Clearndlg::get_post_data(struct ir_remote *remote)
{
struct ir_ncode *codes;
ir_code mask,last;
int count,i;
if(remote->bits==0) return;
mask=(-1);
codes=remote->codes;
if(codes->name==NULL) return; /* at least 2 codes needed */
last=codes->code;
codes++;
if(codes->name==NULL) return; /* at least 2 codes needed */
while(codes->name!=NULL)
{
mask&=~(last^codes->code);
last=codes->code;
codes++;
}
count=0;
while(mask&0x1)
{
count++;
mask=mask>>1;
}
/* only "even" numbers should go to pre/post data */
if(count%8 && (remote->bits-count)%8)
{
count-=count%8;
}
if(count>0)
{
mask=0;
for(i=0;i<count;i++)
{
mask=mask<<1;
mask|=1;
}
remote->bits-=count;
remote->post_data_bits=count;
remote->post_data=last&mask;
codes=remote->codes;
while(codes->name!=NULL)
{
codes->code=codes->code>>count;
codes++;
}
}
}
void Clearndlg::EndDialog2( int nResult )
{
if(fout!=NULL) { fclose(fout); fout=NULL; }
if (nResult==IDOK)
PostMessage(WM_COMMAND,IDOK,0);
else
PostMessage(WM_SYSCOMMAND,SC_CLOSE,0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -