/***************************************************** This program was produced by the CodeWizardAVR V1.25.1 Standard Automatic Program Generator © Copyright 1998-2006 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.com Project : Version : Date : 1/28/2007 Author : Theodore Johnson Company : 225 e. 36th st 17B NY Comments: Chip type : ATmega644 Program type : Application Clock frequency : 8.000000 MHz Memory model : Small External SRAM size : 0 Data Stack size : 1024 *****************************************************/ #include #include #include #include #include #include"font.h" #define LCD_DATA PORTB #define LCD_CS1 PORTD.0 #define LCD_E PORTD.1 #define LCD_RW PORTD.2 #define LCD_DI PORTD.3 #define LCD_RST PORTD.4 #define LCD_CS2 PORTD.5 #define BTN1 PINA.0 #define BTN2 PINA.1 #define LED0 PORTD.7 // #define LED1 PORTC.0 #define CAM_VOUT PINA.7 #define CAM_READ PINA.6 #define CAM_XCK PORTD.6 #define CAM_RESET PORTC.1 #define CAM_LOAD PORTC.2 #define CAM_SIN PORTC.3 #define CAM_START PORTC.4 #define PTR_SC PINA.5 #define PTR_NL PINA.4 #define PTR_MOTOR PORTC.0 #define PTR_HMR1 PORTC.5 #define PTR_HMR2 PORTC.6 #define PTR_HMR3 PORTC.7 // Pin change 0-7 interrupt service routine interrupt [PCINT0] void pin_change_isr0(void) { // Place your code here } ////////////////////////////////////////////////// // Functions for driving the LCD void init_display(){ LCD_E = 0; LCD_RST = 0; delay_us(5); LCD_RST = 1; LCD_DI = 0; LCD_RW = 0; delay_us(2); LCD_CS1 = 1; LCD_CS2 = 0; LCD_DATA = 0x3f; // display on LCD_E = 1; delay_us(1); LCD_E = 0; delay_us(7); LCD_DATA = 0xc0; // set display start line to zero. LCD_E = 1; delay_us(1); LCD_E = 0; delay_us(7); LCD_CS1 = 0; LCD_CS2 = 1; LCD_DATA = 0x3f; // display on LCD_E = 1; delay_us(1); LCD_E = 0; delay_us(7); LCD_DATA = 0xc0; // set display start line to zero. LCD_E = 1; delay_us(1); LCD_E = 0; } // The KS0108B seems to need 7us processing time after // receiving an instruction / data element. void display_buf(unsigned char x, unsigned char y, unsigned char *buf, unsigned char len){ unsigned char ly,l_len,i; if(y<64){ LCD_CS1=1; ly=y; l_len = (y+len <= 64) ? len : 64-y; }else{ LCD_CS2=1; ly = y-64; l_len = (y+len <= 128) ? len : 128-y; } LCD_DI = 0; LCD_RW = 0; LCD_DATA = 0x40 | ly; LCD_E = 1; delay_us(1); LCD_E = 0; delay_us(7); LCD_DATA = 0xB8 | x; LCD_E = 1; delay_us(1); LCD_E = 0; delay_us(7); LCD_DI = 1; for(i=0;i0;b = b >> 1){ if(r&b) CAM_SIN = 1; else CAM_SIN = 0; delay_us(1); CAM_XCK = 1; delay_us(1); CAM_XCK = 0; } for(b=128;b>0;b = b >> 1){ if(v&b) CAM_SIN = 1; else CAM_SIN = 0; delay_us(1); CAM_XCK = 1; if(b == 1) CAM_LOAD = 1; delay_us(1); CAM_XCK = 0; } delay_us(1); CAM_LOAD = 0; } void load_cam_registers(){ unsigned char i; for(i=0;i<8;i++){ load_cam_reg(i,cam_regs[i]); } } void set_N(unsigned char c){ if(c) cam_regs[1] |= 0x80; else cam_regs[1] &= 0x7f; } void set_VH(unsigned char c){ cam_regs[1] = (cam_regs[1] & 0x9f) | ((c&3) << 5); } void set_E(unsigned char c){ cam_regs[7] = (cam_regs[7] & 0x8f) | ((c & 0x07) << 4); } void set_E3(unsigned char c){ cam_regs[7] = (cam_regs[7] & 0x7f) | ((c & 0x01) << 7); } void set_Z(unsigned char c){ cam_regs[0] = (cam_regs[0] & 0x3f) | ((c&3) << 6); } void set_I(unsigned char c){ cam_regs[7] = (cam_regs[7] & 0xf7) | ((c & 1) << 3); } void set_C(unsigned int i){ cam_regs[3] = (unsigned char)(i & 0x00ff); cam_regs[2] = (unsigned char)((i & 0xff00) >> 8); } void set_O(unsigned char pn, unsigned char c){ c = c | ((pn & 1) << 5); cam_regs[0] = (cam_regs[0] & 0xc0) | (c & 0x3f); } void set_V(unsigned char c){ cam_regs[7] = (cam_regs[7] & 0xf8) | (c & 7); } void set_G(unsigned char c){ cam_regs[1] = (cam_regs[1] & 0xe0) | (c & 0x1f); } void set_P(unsigned char c){ cam_regs[4] = c; } void set_M(unsigned char c){ cam_regs[5] = c; } void set_X(unsigned char c){ cam_regs[6] = c; } void init_cam_regs(){ set_N(1); set_VH(3); set_E3(0); set_E(0); set_Z(2); set_I(0); set_C(625); set_O(0,0); set_V(0); set_G(4); set_P(1); set_M(0); set_X(1); } void set_mode(unsigned char m){ switch(m){ case 0: // as given by the article. set_N(1); set_VH(0); set_E3(0); set_E(2); set_Z(0); set_I(0); set_C(625); set_O(0,0); set_V(0); set_G(4); set_P(1); set_M(0); set_X(1); break; case 1: // positive set_N(0); set_VH(0); set_E3(0); set_E(2); set_Z(2); set_I(0); set_C(625); set_O(0,0); set_V(0); set_G(4); set_P(1); set_M(0); set_X(1); break; case 2: // 2d enh A set_N(1); set_VH(3); set_E3(0); set_E(0); set_Z(2); set_I(0); set_C(625); set_O(0,0); set_V(0); set_G(4); set_P(1); set_M(0); set_X(1); break; case 3: // 2d enh B set_N(1); set_VH(3); set_E3(0); set_E(1); set_Z(2); set_I(0); set_C(625); set_O(0,0); set_V(0); set_G(4); set_P(1); set_M(0); set_X(1); break; case 4: // 2d ext A set_N(1); set_VH(3); set_E3(1); set_E(0); set_Z(0); set_I(0); set_C(625); set_O(0,0); set_V(0); set_G(4); set_P(1); set_M(0); set_X(1); break; case 5: // 2d ext B set_N(1); set_VH(3); set_E3(1); set_E(1); set_Z(0); set_I(0); set_C(625); set_O(0,0); set_V(0); set_G(4); set_P(1); set_M(0); set_X(1); break; } } #define MIN_EXP 2 #define MAX_EXP 16 void set_exposure(unsigned char e){ unsigned int c; c=(625*(unsigned int)(e))/((unsigned int)10); set_C(c); } unsigned char image[2048]; unsigned char max_v, min_v; unsigned int avg_v; unsigned int thr_v; unsigned int hi_cnt; unsigned int histo[16]; unsigned int histof[256]; unsigned int pline[12]; unsigned int pcell[144]; void read_image(){ unsigned int row_avg = 0,bytepos; unsigned char v, bitpos,i,j, v2; unsigned char k, pln, ppos, pc_pos, pc; for(bytepos=0;bytepos<2048;bytepos++) image[bytepos] = 0; for(i=0;i<16;i++) histo[i] = 0; for(bytepos=0;bytepos<256;bytepos++) histof[bytepos] = 0; max_v = 0; min_v = 255; avg_v = 0; hi_cnt = 0; reset_cam(); load_cam_registers(); CAM_XCK = 0; CAM_START = 1; delay_us(1); CAM_XCK = 1; delay_us(1); CAM_START = 0; CAM_XCK = 0; delay_us(1); while(! CAM_READ){ CAM_XCK = 1; delay_us(1); CAM_XCK = 0; delay_us(1); } pln = 0; // 0 .. 6, counts rows added to pcell ppos = 0; // 0 .. 12, index to tmp pcell pc_pos = 0; // index into pcell pc = 0; // 0..6, counts cols added to a pcell bytepos = 0; bitpos = 1; for(k=0;k<12;k++) pline[k] = 0; for(i=0;i<128;i++){ row_avg = 0; for(j=0;j<128;j++){ v=read_adc(7); CAM_XCK = 0; if(i>=32 && i<96 && j>=32 && j<96){ row_avg += v; if(vmax_v) max_v = v; if(v<=thr_v) hi_cnt ++; histof[v]++; } v2 = (v & 0xf0) >> 4; histo[v2]++; if(v<=thr_v){ image[bytepos] |= bitpos; } bitpos = bitpos << 1; if(!bitpos){ bitpos=1; bytepos++; } if(i>=34 && i<94 && j>=34 && j < 94){ pline[ppos] += v; pc++; if(pc >= 5){ pc = 0; ppos++; if(ppos >= 12){ ppos = 0; pln++; if(pln >= 5){ pln = 0; for(k=0;k<12;k++){ pcell[pc_pos] = pline[k]; pline[k] = 0; pc_pos++; } } } } } delay_us(1); CAM_XCK = 1; } if(i>=32 && i<96 ){ row_avg = row_avg >> 6; avg_v += row_avg; } } avg_v = avg_v >> 6; } //////////////////////////////////////////////////////////////////////////// // Utility unsigned char button_pressed(unsigned char b, unsigned char *s){ unsigned char retval; if(b == 1 && *s == 0) retval = 1; else retval = 0; *s = ((*s) << 1) + b; return(retval); } //////////////////////////////////////////////////////////////// // Printer Management unsigned char h1[160], h2[160], h3[160]; unsigned char p1,p2,p3, nlp, pcell_pos; unsigned int vdark, vmed, vlmed, vlight, vfaint; void init_printjob(){ p1=0; p2=0; p3=0; nlp = 0; pcell_pos = 0; } void add_blank_line(){ h1[p1] = 0; p1++; h2[p2] = 0; p2++; h3[p3] = 0; p3++; nlp++; } #define VDARK 10 #define VMED 13 #define VLMED 13 #define VLIGHT 13 #define VFAINT 15 flash unsigned char medchar1[7] = {3,4,5,6,7,9,10}; flash unsigned char medchar2[7] = {1,2,4,7,10,11,12}; unsigned char sample_med(unsigned char col, unsigned char prev){ unsigned char ret, smpl, i; for(i=0;i<10;i++){ smpl = (rand()>>4)% 7; if(col < 11) ret = medchar1[smpl]; else ret = medchar2[smpl]; if(ret != prev) return ret; } return 4; } unsigned char sample_lmed(unsigned char col){ unsigned char smpl; smpl = (rand()>>4) & 1; if(col < 11){ if(smpl) return 1; else return 8; } if(smpl) return 0; else return 8; } unsigned char sample_light(unsigned char col){ unsigned char smpl; smpl = (rand()>>4) & 1; if(col < 9){ if(smpl) return 2; else return 12; }else{ if(col < 11) return 2; } if(smpl) return 5; else return 6; } unsigned char sample_faint(unsigned char col){ unsigned char smpl; smpl = (rand()>>1) % 1; if(col < 8){ if(smpl) return 0; else return 11; }else{ if(col < 11) return 0; } if(smpl) return 5; else return 6; } unsigned char process_vid(unsigned int *vid, unsigned char *buf, unsigned char col){ unsigned int i, j, tmp,bufpos = 0; for(i=0;i<4;i++){ if(vid[i] < vdark){ buf[bufpos] = sample_med(i+col,99); bufpos++; buf[bufpos] = sample_med(i+col,buf[bufpos-1]); buf[bufpos-1] = 4+4*buf[bufpos-1]+3-i; buf[bufpos] = 4+4*buf[bufpos]+3-i; bufpos++; continue; } if(vid[i] <= vmed){ buf[bufpos] = sample_med(i+col,99); buf[bufpos] = 4+4*buf[bufpos]+3-i; bufpos++; continue; } if(vid[i] <= vlmed){ buf[bufpos] = sample_lmed(i+col); buf[bufpos] = 4+4*buf[bufpos]+3-i; bufpos++; continue; } if(vid[i] <= vlight){ buf[bufpos] = sample_light(i+col); buf[bufpos] = 4+4*buf[bufpos]+3-i; bufpos++; continue; } if(vid[i] <= vfaint){ buf[bufpos] = sample_faint(i+col); buf[bufpos] = 4+4*buf[bufpos]+3-i; bufpos++; continue; } } // Insertion sort on the result for(i=0;i4){ // must be a significant LF. done = 1; } } } } cnt = 0; done = 0; btn_sc = 0; while(! done){ if(button_pressed(PTR_SC,&btn_sc)){ PTR_HMR1 = 0; PTR_HMR2 = 0; PTR_HMR3 = 0; cnt++; delay_us(100); if(PTR_NL) done = 1; if(cnt == h1[h1pos]){ h1pos++; PTR_HMR1 = 1; } if(cnt == h2[h2pos]){ h2pos++; PTR_HMR2 = 1; } if(cnt == h3[h3pos]){ h3pos++; PTR_HMR3 = 1; } delay_ms(1); PTR_HMR1 = 0; PTR_HMR2 = 0; PTR_HMR3 = 0; } } h1pos++; h2pos++; h3pos++; } PTR_MOTOR = 0; } //////////////////////////////////////////////////////////////////////////// void main(void){ unsigned char xpos, ypos, val, i,j,k,c,exp; unsigned char level, inv, mode=0,r1,r2, mid,diff; unsigned char dbitpos, imask, ishift, ibit; unsigned char btn1=0, btn2=0, btn_sc = 0; unsigned int ipos, img_bits, sum=0, lv, pfloor; unsigned int cnt, char_cnt, line_cnt; unsigned char clicks, h1pos,h2pos,h3pos,done; // Input/Output Ports initialization // Port A initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTA=0x00; DDRA=0x00; // Port B initialization // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 PORTB=0x00; DDRB=0xFF; // Port C initialization // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 PORTC=0x00; DDRC=0xFF; // Port D initialization // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 PORTD=0x00; DDRD=0xFF; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: Timer 0 Stopped // Mode: Normal top=FFh // OC0A output: Disconnected // OC0B output: Disconnected TCCR0A=0x00; TCCR0B=0x00; TCNT0=0x00; OCR0A=0x00; OCR0B=0x00; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: Timer 1 Stopped // Mode: Normal top=FFFFh // OC1A output: Discon. // OC1B output: Discon. // Noise Canceler: Off // Input Capture on Falling Edge // Timer 1 Overflow Interrupt: Off // Input Capture Interrupt: Off // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off TCCR1A=0x00; TCCR1B=0x00; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer 2 Stopped // Mode: Normal top=FFh // OC2A output: Disconnected // OC2B output: Disconnected ASSR=0x00; TCCR2A=0x00; TCCR2B=0x00; TCNT2=0x00; OCR2A=0x00; OCR2B=0x00; // External Interrupt(s) initialization // INT0: Off // INT1: Off // INT2: Off // Interrupt on any change on pins PCINT0-7: On // Interrupt on any change on pins PCINT8-15: Off // Interrupt on any change on pins PCINT16-23: Off // Interrupt on any change on pins PCINT24-31: Off EICRA=0x00; EIMSK=0x00; PCMSK0=0x20; PCICR=0x01; PCIFR=0x01; // Timer/Counter 0 Interrupt(s) initialization TIMSK0=0x00; // Timer/Counter 1 Interrupt(s) initialization TIMSK1=0x00; // Timer/Counter 2 Interrupt(s) initialization TIMSK2=0x00; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; ADCSRB=0x00; // ADC initialization // ADC Clock frequency: 1000.000 kHz // ADC Voltage Reference: AVCC pin // ADC Auto Trigger Source: None // Only the 8 most significant bits of // the AD conversion result are used // Digital input buffers on ADC0: On, ADC1: On, ADC2: On, ADC3: On // ADC4: On, ADC5: On, ADC6: On, ADC7: Off DIDR0=0x80; ADMUX=ADC_VREF_TYPE; //ADCSRA=0x83; ADCSRA=0x84; // set to 500k freq. // Global enable interrupts #asm("sei") init_display(); init_cam_regs(); exp=10; set_mode(2); set_exposure(exp); init_cam(); level = 2; inv = 0; mode=0; //set_mode(mode); for(i=0;i<=10;i++) cbuf[i] = ' '; display_text(cbuf,10,0,11); display_text(cbuf,10,1,11); btn1 = 0; btn2 = 0; ///////////////////////////////////////////////////// while(1){ read_image(); // Clear left half of the screen for(i=0;i<=64;i++) dbuf[i] = 0; for(i=0;i<8;i++) display_buf(i,64,dbuf,64); // Show some parameters /* sprintf(cbuf,"%u %u",(unsigned int)min_v,(unsigned int)max_v); display_text(cbuf,strlen(cbuf),0,11); sprintf(cbuf,"avg_v %u",(unsigned int)avg_v); display_text(cbuf,strlen(cbuf),1,11); sprintf(cbuf,"thr_v %u",(unsigned int)thr_v); display_text(cbuf,strlen(cbuf),2,11); sprintf(cbuf,"l=%u e=%u",(unsigned int)level,(unsigned int)exp); display_text(cbuf,strlen(cbuf),3,11); sprintf(cbuf,"mode %u",mode); display_text(cbuf,strlen(cbuf),4,11); */ sprintf(cbuf,"level=%u",(unsigned int)level); display_text(cbuf,strlen(cbuf),0,11); sprintf(cbuf,"exp=%u",(unsigned int)exp); display_text(cbuf,strlen(cbuf),1,11); // Show the exposure histogram for(j=0;j<64;j++) dbuf[j] = 0; j=0; dbitpos = 1; for(i=0;i<16;i++){ c=histo[i] >> 8; for(k=0;k 140 && (exp > MIN_EXP)) exp=exp-1; if(max_v < 140 && (exp < MAX_EXP)) exp=exp+1; }else{ if((avg_v > (mid+diff)) && (exp > MIN_EXP)) exp=exp-1; if((avg_v < (mid-diff)) && (exp < MAX_EXP)) exp=exp+1; } set_exposure(exp); // sprintf(cbuf,"%u %u",mid-diff,mid+diff); // display_text(cbuf,strlen(cbuf),5,11); // Display the captured image ipos = 32*16+4; ibit = 1; img_bits = 0; for(r1=0;r1<8;r1++){ for(i=0;i<64;i++) dbuf[i] = 0; dbitpos = 1; for(r2=0;r2<8;r2++){ for(c=0;c<64;c++){ if(image[ipos] & ibit){ dbuf[c] |= dbitpos; img_bits++; } ibit = ibit << 1; if(! ibit){ ipos++; ibit = 1; } } dbitpos = dbitpos << 1; ibit = 1; ipos += 8; } display_buf(r1,0,dbuf,64); } // sprintf(cbuf,"imc %u",img_bits); // display_text(cbuf,strlen(cbuf),5,11); for(i=0;i<250;i++){ if(button_pressed(BTN1,&btn1)){ level++; if(level >= 4) level = 1; break; } if(button_pressed(BTN2,&btn2)){ done = 0; LED0 = 1; /* diff = (max_v-min_v); pfloor = 25 * (unsigned int)(min_v); vdark = pfloor + VDARK*(unsigned int)(diff); vmed = pfloor + VMED*(unsigned int)(diff); vlmed = pfloor + VLMED*(unsigned int)(diff); vlight = pfloor + VLIGHT*(unsigned int)(diff); vfaint = pfloor + VFAINT*(unsigned int)(diff); */ diff = max_v - thr_v; vlight = 25*thr_v; vfaint = vlight - (25*diff) / 10; vmed = ((25*3)*diff)/10 + vlight; vdark = ((25*8)*diff)/10 + vlight; vlmed = vmed; for(k=0;k<=64;k++) dbuf[k] = 0; for(k=3;k<4;k++) display_buf(k,64,dbuf,64); sprintf(cbuf,"Print or "); display_text(cbuf,strlen(cbuf),3,11); sprintf(cbuf,"Cancel "); display_text(cbuf,strlen(cbuf),4,11); while(! done){ if(button_pressed(BTN1,&btn1)) done = 1; if(button_pressed(BTN2,&btn2)){ done = 1; init_printjob(); for(k=0;k<12;k++) add_to_printjob(); add_blank_line(); add_blank_line(); do_printjob(15); for(k=0;k<=64;k++) dbuf[k] = 0; for(k=3;k<4;k++) display_buf(k,64,dbuf,64); sprintf(cbuf,"Print to "); display_text(cbuf,strlen(cbuf),3,11); sprintf(cbuf,"continue "); display_text(cbuf,strlen(cbuf),4,11); while(! button_pressed(BTN2,&btn2)); } delay_ms(1); } LED0 = 0; break; } delay_ms(4); } switch(level){ case 1: thr_v = (19*avg_v)/20; break; case 2: thr_v = avg_v; break; case 3: thr_v = (21*avg_v)/20; } } ////////////////////////////////////////////////////////////// sprintf(cbuf,"c=%u",mode); display_text(cbuf,strlen(cbuf),0,11); srand(1); vfaint = 1; vlight = 2; vlmed = 3; vmed = 4; vdark = 4; while(1){ if(button_pressed(BTN2,&btn2)){ init_printjob(); k=0; for(i=0;i<6;i++){ for(j=0;j<12;j++){ pcell[k] = 5-i; k++; } } LED0 = 1; for(i=0;i<12;i++){ pcell[k] = i % 6; k++; } for(i=0;i<12;i++){ pcell[k] = 5-(i % 6); k++; } for(i=0;i<6;i++){ pcell[k] = 1; pcell[k+6] = 4; k++; } k+=6; for(i=0;i<6;i++){ pcell[k] = 4; pcell[k+6] = 1; k++; } for(i=0;i<10;i++) add_to_printjob(); do_printjob(10); } LED0 = 0; delay_ms(5); } while(0){ if(button_pressed(BTN1,&btn1)){ mode++; if(mode > 5) mode = 0; for(i=0;i<=10;i++) cbuf[i] = ' '; sprintf(cbuf,"c=%u",mode); display_text(cbuf,strlen(cbuf),0,11); } if(button_pressed(BTN2,&btn2)){ PTR_HMR1 = 1; PTR_HMR2 = 1; PTR_HMR3 = 1; LED0 = 1; switch(mode){ case 0: delay_ms(10); break; case 1: delay_ms(25); break; case 2: delay_ms(50); break; case 3: delay_ms(75); break; case 4: delay_ms(100); break; case 5: delay_ms(200); break; } PTR_HMR1 = 0; PTR_HMR2 = 0; PTR_HMR3 = 0; LED0 = 0; } } btn2 = 0; while(1){ if(button_pressed(BTN2,&btn2)){ LED0 = 1; h1pos = 0; h2pos = 0; h3pos = 0; for(mode=0;mode<13;mode++){ for(i=0;i<4;i++){ h1[h1pos] = 4+4*mode + i; h2[h1pos] = 4+4*mode + i; h3[h1pos] = 4+4*mode + i; h1pos ++; } h1[h1pos] = 0; h2[h1pos] = 0; h3[h1pos] = 0; h1pos ++; } do_printjob(13); LED0 = 0; } } mode = 1; while(1){ if(button_pressed(BTN2,&btn2)){ mode++; if(mode>12) mode = 0; for(i=0;i<=10;i++) cbuf[i] = ' '; display_text(cbuf,10,0,11); display_text(cbuf,10,3,11); display_text(cbuf,10,4,11); display_text(cbuf,10,5,11); display_text(cbuf,10,6,11); sprintf(cbuf,"c=%u",mode); display_text(cbuf,strlen(cbuf),0,11); for(i=0;i<4;i++){ h1[i] = 4+4*mode + i; h2[i] = 4+4*mode + i; h3[i] = 4+4*mode + i; } sprintf(cbuf,"h0=%u",h2[0]); display_text(cbuf,strlen(cbuf),1,11); sprintf(cbuf,"h0=%u",h2[1]); display_text(cbuf,strlen(cbuf),2,11); sprintf(cbuf,"h0=%u",h2[2]); display_text(cbuf,strlen(cbuf),3,11); sprintf(cbuf,"h0=%u",h2[3]); display_text(cbuf,strlen(cbuf),4,11); h1[4] = 0; h2[4] = 0; h3[4] = 0; } if(button_pressed(BTN1,&btn1)){ PTR_MOTOR = 1; // Wait for a first LF cnt = 0; btn_sc = 0; done = 0; while(!done){ if(button_pressed(PTR_SC,&btn_sc)){ delay_us(100); if(PTR_NL){ PTR_HMR1 = 1; PTR_HMR2 = 1; PTR_HMR3 = 1; cnt++; }else{ if(cnt>0){ done = 1; } } } } line_cnt = cnt; cnt = 0; h1pos = 0; h2pos = 0; h3pos = 0; done = 0; btn_sc = 0; while(! done){ if(button_pressed(PTR_SC,&btn_sc)){ PTR_HMR1 = 0; PTR_HMR2 = 0; PTR_HMR3 = 0; cnt++; delay_us(100); if(PTR_NL) done = 1; if(cnt == h1[h1pos]){ h1pos++; PTR_HMR1 = 1; } if(cnt == h2[h2pos]){ h2pos++; PTR_HMR2 = 1; } if(cnt == h3[h3pos]){ h3pos++; PTR_HMR3 = 1; } delay_ms(1); PTR_HMR1 = 0; PTR_HMR2 = 0; PTR_HMR3 = 0; } } for(i=0;i<=10;i++) cbuf[i] = ' '; display_text(cbuf,10,1,11); display_text(cbuf,10,2,11); sprintf(cbuf,"n=%u",cnt); display_text(cbuf,strlen(cbuf),1,11); sprintf(cbuf,"l=%u",line_cnt); display_text(cbuf,strlen(cbuf),2,11); PTR_MOTOR = 0; } LED0 = PTR_NL & PTR_SC; // delay_ms(1); } while(1){ PTR_MOTOR = BTN1; PTR_HMR1 = BTN2; LED0 = PTR_SC & PTR_NL; if(button_pressed(PTR_SC,&btn1)){ delay_us(1); if(mode==0){ if(PTR_NL){ char_cnt = cnt; cnt = 0; mode = 1; } cnt++; }else{ if(!PTR_NL){ line_cnt = cnt; cnt = 0; mode=0; clicks++; if(clicks >= 5){ for(i=0;i<=10;i++) cbuf[i] = ' '; display_text(cbuf,10,0,11); display_text(cbuf,10,1,11); sprintf(cbuf,"c=%u",char_cnt); display_text(cbuf,strlen(cbuf),0,11); sprintf(cbuf,"l=%u",line_cnt); display_text(cbuf,strlen(cbuf),1,11); clicks = 0; } } cnt++; } } } while(1){ PTR_MOTOR = BTN1; PTR_HMR1 = BTN2; LED0 = PTR_NL; } while(1){ read_image(); for(i=0;i<=10;i++) cbuf[i] = ' '; display_text(cbuf,10,0,11); display_text(cbuf,10,1,11); display_text(cbuf,10,2,11); display_text(cbuf,10,3,11); display_text(cbuf,10,4,11); display_text(cbuf,10,5,11); display_text(cbuf,10,6,11); display_text(cbuf,10,7,11); sprintf(cbuf,"%u %u",(unsigned int)min_v,(unsigned int)max_v); display_text(cbuf,strlen(cbuf),0,11); sprintf(cbuf,"avg_v %u",(unsigned int)avg_v); display_text(cbuf,strlen(cbuf),1,11); sprintf(cbuf,"thr_v %u",(unsigned int)thr_v); display_text(cbuf,strlen(cbuf),2,11); sprintf(cbuf,"l=%u e=%u",(unsigned int)level,(unsigned int)exp); display_text(cbuf,strlen(cbuf),3,11); sprintf(cbuf,"mode %u",mode); display_text(cbuf,strlen(cbuf),4,11); // sprintf(cbuf,"hic %u",hi_cnt); // display_text(cbuf,strlen(cbuf),5,11); for(j=0;j<64;j++) dbuf[j] = 0; j=0; dbitpos = 1; for(i=0;i<16;i++){ c=histo[i] >> 8; for(k=0;k 140 && (exp > MIN_EXP)) exp=exp-2; if(max_v < 140 && (exp < MAX_EXP)) exp=exp+2; }else{ if((avg_v > (mid+diff)) && (exp > MIN_EXP)) exp=exp-2; if((avg_v < (mid-diff)) && (exp < MAX_EXP)) exp=exp+2; } set_exposure(exp); sprintf(cbuf,"%u %u",mid-diff,mid+diff); display_text(cbuf,strlen(cbuf),5,11); ipos = 32*16+4; ibit = 1; img_bits = 0; for(r1=0;r1<8;r1++){ for(i=0;i<64;i++) dbuf[i] = 0; dbitpos = 1; for(r2=0;r2<8;r2++){ for(c=0;c<64;c++){ if(image[ipos] & ibit){ dbuf[c] |= dbitpos; img_bits++; } ibit = ibit << 1; if(! ibit){ ipos++; ibit = 1; } } dbitpos = dbitpos << 1; ibit = 1; ipos += 8; } display_buf(r1,0,dbuf,64); } // sprintf(cbuf,"imc %u",img_bits); // display_text(cbuf,strlen(cbuf),5,11); for(i=0;i<250;i++){ if(button_pressed(BTN1,&btn1)){ level++; if(level >= 4) level = 1; break; } // if(button_pressed(BTN2,&btn2)){ // mode++; // if(mode>5) // mode=0; // set_mode(mode); // break; // } delay_ms(4); } switch(level){ case 1: thr_v = (9*avg_v)/10; break; case 2: thr_v = avg_v; break; case 3: thr_v = (11*avg_v)/10; } switch(level){ case 1: lv = 1638; break; case 2: lv = 2048; break; case 3: lv = 2457; break; } sum = 0; for(thr_v=0;sum= 120){ xpos ++; ypos = 0; } if(xpos >= 8) xpos = 0; cbuf[0] = c++; cbuf[1] = c++; display_text(cbuf,2,xpos,ypos/6); ypos+= 12; if(ypos >= 120){ xpos ++; ypos = 0; } if(xpos >= 8) xpos = 0; delay_ms(300); } }