📄 irrecord.c
字号:
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++; } }}#ifdef DEBUGvoid remove_pre_data(struct ir_remote *remotes){ struct ir_ncode *codes; struct ir_remote *remote; remote=remotes; while(remote!=NULL) { if(remote->pre_data_bits==0) { remote=remote->next; continue; } codes=remote->codes; while(codes->name!=NULL) { codes->code|=remote->pre_data<<remote->bits; codes++; } remote->bits+=remote->pre_data_bits; remote->pre_data=0; remote->pre_data_bits=0; remote=remote->next; }}void remove_post_data(struct ir_remote *remotes){ struct ir_ncode *codes; struct ir_remote *remote; remote=remotes; while(remote!=NULL) { if(remote->post_data_bits==0) { remote=remote->next; continue; } codes=remote->codes; while(codes->name!=NULL) { codes->code<<=remote->post_data_bits; codes->code|=remote->post_data; codes++; } remote->bits+=remote->post_data_bits; remote->post_data=0; remote->post_data_bits=0; remote=remote->next; }}#endif/* analyse stuff */struct lengths{ unsigned int count; lirc_t sum,upper_bound,lower_bound,min,max; struct lengths *next;};enum analyse_mode {MODE_GAP,MODE_HAVE_GAP};struct lengths *first_space=NULL,*first_pulse=NULL;struct lengths *first_sum=NULL,*first_gap=NULL,*first_repeat_gap=NULL;struct lengths *first_signal_length=NULL;struct lengths *first_headerp=NULL,*first_headers=NULL;struct lengths *first_1lead=NULL,*first_3lead=NULL,*first_trail=NULL;struct lengths *first_repeatp=NULL,*first_repeats=NULL;unsigned long lengths[MAX_SIGNALS];unsigned long first_length,first_lengths,second_lengths;unsigned int count,count_spaces,count_3repeats,count_5repeats,count_signals;inline lirc_t calc_signal(struct lengths *len){ return((lirc_t) (len->sum/len->count));}int get_lengths(struct ir_remote *remote,int force){ int retval; lirc_t data,average,sum,remaining_gap,header; enum analyse_mode mode=MODE_GAP; int first_signal; printf("Now start pressing buttons on your remote control.\n\n"); printf("It is very important that you press many different buttons and hold them\n""down for approximately one second. Each button should generate at least one\n""dot but in no case more than ten dots of output.\n""Don't stop pressing buttons until two lines of dots (2x80) have been\n""generated.\n\n"); printf("Press RETURN now to start recording.");fflush(stdout); getchar(); flushhw(); retval=1; average=0;sum=0;count=0;count_spaces=0; count_3repeats=0;count_5repeats=0;count_signals=0; first_signal=-1;header=0; first_length=0; while(1) { data=hw.readdata(10000000); if(!data) { fprintf(stderr,"%s: no data for 10 secs, aborting\n", progname); retval=0; break; } count++; if(mode==MODE_GAP) { sum+=data&PULSE_MASK; if(average==0 && is_space(data)) { if(data>100000) { sum=0; continue; } average=data; } else if(is_space(data)) { if(data>MIN_GAP || data>100*average || /* this MUST be a gap */ (count_spaces>10 && data>5*average)) /* this should be a gap */ { struct lengths *scan; int maxcount; static int lastmaxcount=0; int i; add_length(&first_sum,sum); merge_lengths(first_sum); add_length(&first_gap,data); merge_lengths(first_gap); sum=0;count_spaces=0;average=0; maxcount=0; scan=first_sum; while(scan) { maxcount=max(maxcount, scan->count); if(scan->count>SAMPLES) { remote->gap=calc_signal(scan); remote->flags|=CONST_LENGTH; printf("\nFound const length: %lu\n",(unsigned long) remote->gap); break; } scan=scan->next; } if(scan==NULL) { scan=first_gap; while(scan) { maxcount=max(maxcount, scan->count); if(scan->count>SAMPLES) { remote->gap=calc_signal(scan); printf("\nFound gap: %lu\n",(unsigned long) remote->gap); break; } scan=scan->next; } } if(scan!=NULL) { printf("Please keep on pressing buttons like described above.\n"); mode=MODE_HAVE_GAP; sum=0; count=0; remaining_gap= is_const(remote) ? (remote->gap>data ? remote->gap-data:0): (has_repeat_gap(remote) ? remote->repeat_gap:remote->gap); if(force) { retval=0; break; } continue; } for(i=maxcount-lastmaxcount;i>0;i--) { printf("."); fflush(stdout); } lastmaxcount=maxcount; continue; } average=(average*count_spaces+data) /(count_spaces+1); count_spaces++; } if(count>SAMPLES*MAX_SIGNALS) { fprintf(stderr,"\n%s: could not find gap.\n", progname); retval=0; break; } } else if(mode==MODE_HAVE_GAP) { if(count<=MAX_SIGNALS) { signals[count-1]=data&PULSE_MASK; } else { fprintf(stderr,"%s: signal too long\n", progname); retval=0; break; } if(is_const(remote)) { remaining_gap=remote->gap>sum ? remote->gap-sum:0; } else { remaining_gap=remote->gap; } sum+=data&PULSE_MASK; if((data&PULSE_MASK)>=remaining_gap*(100-EPS)/100 || (data&PULSE_MASK)>=remaining_gap-AEPS) { if(is_space(data)) { /* signal complete */ if(count==4) { count_3repeats++; add_length(&first_repeatp,signals[0]); merge_lengths(first_repeatp); add_length(&first_repeats,signals[1]); merge_lengths(first_repeats); add_length(&first_trail,signals[2]); merge_lengths(first_trail); add_length(&first_repeat_gap,signals[3]); merge_lengths(first_repeat_gap); } else if(count==6) { count_5repeats++; add_length(&first_headerp,signals[0]); merge_lengths(first_headerp); add_length(&first_headers,signals[1]); merge_lengths(first_headers); add_length(&first_repeatp,signals[2]); merge_lengths(first_repeatp); add_length(&first_repeats,signals[3]); merge_lengths(first_repeats); add_length(&first_trail,signals[4]); merge_lengths(first_trail); add_length(&first_repeat_gap,signals[5]); merge_lengths(first_repeat_gap); } else if(count>6) { int i; printf(".");fflush(stdout); count_signals++; add_length(&first_1lead,signals[0]); merge_lengths(first_1lead); for(i=2;i<count-2;i++) { if(i%2) { add_length(&first_space,signals[i]); merge_lengths(first_space); } else { add_length(&first_pulse,signals[i]); merge_lengths(first_pulse); } } add_length(&first_trail,signals[count-2]); merge_lengths(first_trail); lengths[count-2]++; add_length(&first_signal_length,sum-data); merge_lengths(first_signal_length); if(first_signal==1 || (first_length>2 && first_length-2!=count-2)) { add_length(&first_3lead,signals[2]); merge_lengths(first_3lead); add_length(&first_headerp,signals[0]); merge_lengths(first_headerp); add_length(&first_headers,signals[1]); merge_lengths(first_headers); } if(first_signal==1) { first_lengths++; first_length=count-2; header=signals[0]+signals[1]; } else if(first_signal==0 && first_length-2==count-2) { lengths[count-2]--; lengths[count-2+2]++; second_lengths++; } } count=0; sum=0; }#if 0 /* such long pulses may appear with crappy hardware (receiver? / remote?) */ else { fprintf(stderr,"%s: wrong gap\n",progname); remote->gap=0; retval=0; break; }#endif if(count_signals>=SAMPLES) { printf("\n"); get_scheme(remote); if(!get_header_length(remote) || !get_trail_length(remote) || !get_lead_length(remote) || !get_repeat_length(remote) || !get_data_length(remote)) { retval=0; } break; } if((data&PULSE_MASK)<=(remaining_gap+header)*(100+EPS)/100 || (data&PULSE_MASK)<=(remaining_gap+header)+AEPS) { first_signal=0; header=0; } else first_signal=1; } } } free_lengths(first_space); free_lengths(first_pulse); free_lengths(first_sum); free_lengths(first_gap); free_lengths(first_repeat_gap); free_lengths(first_signal_length); free_lengths(first_headerp); free_lengths(first_headers); free_lengths(first_1lead); free_lengths(first_3lead); free_lengths(first_trail); free_lengths(first_repeatp); free_lengths(first_repeats); return(retval);}/* handle lengths */struct lengths *new_length(lirc_t length){ struct lengths *l; l=malloc(sizeof(struct lengths)); if(l==NULL) return(NULL); l->count=1; l->sum=length; l->lower_bound=length/100*100; l->upper_bound=length/100*100+99; l->min=l->max=length; l->next=NULL; return(l);}int add_length(struct lengths **first,lirc_t length){ struct lengths *l,*last; if(*first==NULL) { *first=new_length(length); if(*first==NULL) return(0); return(1); } l=*first; while(l!=NULL) { if(l->lower_bound<=length && length<=l->upper_bound) { l->count++; l->sum+=length; l->min=min(l->min,length); l->max=max(l->max,length); return(1); } last=l; l=l->next; } last->next=new_length(length); if(last->next==NULL) return(0); return(1);}void free_lengths(struct lengths *first){ struct lengths *next; if(first==NULL) return; while(first!=NULL) { next=first->next; free(first); first=next; }}void 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; }# ifdef DEBUG l=first; while(l!=NULL) { printf("%d x %lu [%lu,%lu]\n",l->count, (unsigned long) calc_signal(l), (unsigned long) l->min, (unsigned long) l->max); l=l->next; }# endif}void get_scheme(struct ir_remote *remote){ unsigned int i,length=0,sum=0; for(i=1;i<MAX_SIGNALS;i++) { if(lengths[i]>lengths[length]) { length=i; } sum+=lengths[i];# ifdef DEBUG if(lengths[i]>0) printf("%u: %lu\n",i,lengths[i]);# endif }# ifdef DEBUG printf("get_scheme(): sum: %u length: %u signals: %lu\n" "first_lengths: %lu second_lengths: %lu\n", sum,length+1,lengths[length],first_lengths,second_lengths);# endif if(lengths[length]>=TH_SPACE_ENC*sum/100) { length++; printf("Space/pulse encoded remote control found.\n"); printf("Signal length is %u.\n",length); /* this is not yet the number of bits */ remote->bits=length;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -