#define F_CPU 12000000UL //mega128 #define samplerate 8000 #include #include #include #include #include #include #include #include #include #include "head.h" #include #define MAX_SAM 32767 #define MAX_AMP (1.0) #define BV(bit) (1<<(bit)) // Byte Value => converts bit into a byte value. One at bit location. #define cbi(reg, bit) reg &= ~(BV(bit)) // Clears the corresponding bit in register reg #define sbi(reg, bit) reg |= (BV(bit)) // Sets the corresponding bit in register reg #define HEX__(n) 0x##n##UL #define B8__(x) ((x&0x0000000FLU)?1:0) \ +((x&0x000000F0LU)?2:0) \ +((x&0x00000F00LU)?4:0) \ +((x&0x0000F000LU)?8:0) \ +((x&0x000F0000LU)?16:0) \ +((x&0x00F00000LU)?32:0) \ +((x&0x0F000000LU)?64:0) \ +((x&0xF0000000LU)?128:0) #define B8(d) ((unsigned char)B8__(HEX__(d))) #define low(port, pin) (port &= ~_BV(pin)) #define high(port, pin) (port |= _BV(pin)) #define PI 3.1415926535897932384626433832795 #define BET(A, B, C) (((A>=B)&&(A<=C))?1:0) /* a between [b,c] */ #define NSTEPS 10000 #define HOW 100 #define FRAMESIZE 512 uint32_t tick, place, newplace; volatile uint32_t maxsamp = MAX_SAM; const float oneoversr = 1./100; ifss ifs; rosstype ross; unsigned int cttt, ctttt; volatile unsigned int dtae; unsigned char datagen, reset, oldreset, effect; unsigned char *xramptr; unsigned char *xxramptr; int ADConvert(short Channel, short DivFactor); int funct(unsigned char eff, int place, int dta); SIGNAL(TIMER1_COMPA_vect) { int ADresult; ADMUX &= 0xF8; // clear existing channel selection ADMUX |= 0; sbi(ADCSRA, ADSC); /* Start ADC*/ loop_until_bit_is_set(ADCSRA, ADIF); /* Wait for ADIF, will happen soon */ ADresult = ADCL; /* Read LSB first */ ADresult |= ((int)ADCH) << 8;/* then MSB */ xramptr = (unsigned char *)(0x1100+cttt); *xramptr = (unsigned char) ADresult; // 4 bits ADMUX|=1; sbi(ADCSRA, ADSC); loop_until_bit_is_set(ADCSRA, ADIF); ADresult = ADCL; ADresult |= ((int)ADCH) << 8; effect=ADresult>>5; // 5 bits newplace=place=cttt; switch(effect){ case 1: newplace=place; break; case 2: newplace=maxsamp-place; break; case 3: newplace=(place*2)&maxsamp; break; case 4: newplace=(place*4)&maxsamp; break; case 5: newplace=place/2; break; case 6: newplace=place/4; break; case 7: // granulate??? newplace=place>>4; break; case 8: // fract newplace=(dtae+place)>>4; break; case 9: // ???? newplace=place/dtae; break; case 10: // xor //xx[place]^=xx[place]; *xramptr^=*xramptr; break; case 11: // //xx[place]&=dta; *xramptr&=dtae; break; case 12: // newplace=maxsamp-(place*dtae); break; case 13: // // xx[place]|=dta; *xramptr|=dtae; break; case 14: // //xx[place]^=dta; *xramptr^=dtae; break; case 15: xxramptr = (unsigned char *)(0x1100+(maxsamp-place)); *xramptr = *xxramptr; // xx[place]=xx[maxsamp-place]; break; case 16: xxramptr = (unsigned char *)(0x1100+((place*2)&maxsamp)); *xramptr = *xxramptr; // xx[place]=xx[(place*2)&maxsamp]; break; case 17: xxramptr = (unsigned char *)(0x1100+((place*4)&maxsamp)); *xramptr = *xxramptr; // xx[place]=xx[(place*4)&maxsamp]; break; case 18: xxramptr = (unsigned char *)(0x1100+(place/2)); *xramptr = *xxramptr; // xx[place]=xx[place/2]; break; case 19: xxramptr = (unsigned char *)(0x1100+(place/4)); *xramptr = *xxramptr; // xx[place]=xx[place/4]; break; case 20: // granulate??? xxramptr = (unsigned char *)(0x1100+(place>>4)); *xramptr = *xxramptr; // xx[place]=xx[place>>4]; break; case 21: // fract xxramptr = (unsigned char *)(0x1100+((dtae+place)>>4)); *xramptr = *xxramptr; // xx[place]=xx[(dta+place)>>4]; break; case 22: // ???? xxramptr = (unsigned char *)(0x1100+(place/dtae)); *xramptr = *xxramptr; break; case 23: *xramptr=dtae; break; case 24: *xramptr=*xramptr<=maxsamp) cttt=0; } void initross(rosstype* ross){ ross->h = 0.01; ross->a = 0.2; ross->b = 0.2; ross->c = 5.7; ross->lx0 = 0.1; ross->ly0 = 0; ross->lz0 = 0; ross->step=1; } void runross(rosstype* ross){ double lx0,ly0,lz0,lx1,ly1,lz1; double h,a,b,c; int x; for (x=0;xstep;x++){ h = ross->h; a = ross->a; b = ross->b; c = ross->c; lx0 = ross->lx0; ly0 = ross->ly0; lz0 = ross->lz0; lx1 = lx0 + h * (-ly0 - lz0); ly1 = ly0 + h * (lx0 + (a * ly0)); lz1 = lz0 + h * (b + (lx0*lz0) - (c * lz0));; ross->lx0 = lx1; ross->ly0 = ly1; ross->lz0 = lz1;} ross->intx=(signed int)((float)lx1*1024)%1024; ross->inty=(signed int)((float)ly1*1024)%1024; ross->intz=(signed int)((float)lz1*1024)%1024; } void ioinit(void) { DDRD=0xFF; // all output } void adc_init(void) { // ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Set ADC prescalar to 128 - 125KHz sample rate @ 16MHz ADCSRA |= (1 << ADPS0); // prescalar of 4 ADMUX |= (1 << REFS0); // Set ADC reference to AVCC ADCSRA |= (1 << ADEN); // Enable ADC DDRF = 0x00; // configure a2d port (PORTF) as input so we can receive analog signals PORTF = 0x80; // make sure pull-up resistors are turned off (else we’ll just read 0xCFF) } int ADConvert(short Channel, short DivFactor) { int ADresult;/* Result to be returned*/ ADMUX &= 0xF8; // clear existing channel selection ADMUX |= (Channel & 0x07); sbi(ADCSRA, ADSC); /* Start ADC*/ loop_until_bit_is_set(ADCSRA, ADIF); /* Wait for ADIF, will happen soon */ ADresult = ADCL; /* Read LSB first */ ADresult |= ((int)ADCH) << 8;/* then MSB */ return(ADresult); } void enable_external_ram (void) __attribute__ ((naked)) __attribute__ ((section (".init1"))); void enable_external_ram(void) { MCUCR |= _BV(SRE); } void initifs(ifss* ifs){ int i,iter; int column = 6, row = 4; ifs->step=1; ifs->ifscount=0; ifs->prob[0]=0.0; ifs->prob[1]=0.85; ifs->prob[2]=0.92; ifs->prob[3]=0.99; ifs->prob[4]=1.0; ifs->p1.x=0.1; ifs->p1.y=0.1; for (iter=0;itercoeff[iter][i]=((double)rand()/(double)(RAND_MAX)); if (((double)rand()/(double)(RAND_MAX))>0.5) ifs->coeff[iter][i]= ifs->coeff[iter][i]-1; ifs->prob[iter]=((double)rand()/(double)(RAND_MAX)); } } } void runifs(ifss* ifs){ double random_num; int iter,i,it; int column = 6, row = 4; for (it=0;itstep;it++){ ifs->ifscount++; random_num = (double)rand()/(double)(RAND_MAX); for(i = 0; i < row; i++){ if ( BET(random_num,ifs->prob[i],ifs->prob[i+1]) ){ ifs->p2.x = ifs->coeff[i][0]*ifs->p1.x + ifs->coeff[i][1]*ifs->p1.y + ifs->coeff[i][4]; ifs->p2.y = ifs->coeff[i][2]*ifs->p1.x + ifs->coeff[i][3]*ifs->p1.y + ifs->coeff[i][5]; break; } } ifs->p1=ifs->p2; if (ifs->p2.x>0.0) ifs->returnvalx=(int)((ifs->p2.x)*1024); if (ifs->p2.y>0.0) ifs->returnvaly=(int)((ifs->p2.y)*1024); if (ifs->ifscount>NSTEPS) { iter=rand()%row; i=rand()%column; ifs->coeff[iter][i]=((double)rand()/(double)(RAND_MAX)); if (((double)rand()/(double)(RAND_MAX))>0.5) ifs->coeff[iter][i]= ifs->coeff[iter][i]-1; ifs->prob[iter]=((double)rand()/(double)(RAND_MAX)); ifs->p1.x=0.5; ifs->p1.y=0.5; } } } int main(void) { unsigned int x=0; unsigned char channel=0; int ADresult;/* Result to be returned*/ unsigned int knob[5] = {0, 0, 0, 0}; dtae=0; uint8_t rule = 41; uint8_t l = 0; uint8_t cell = 0; uint8_t state = 0; uint8_t sizeLow = 1; uint8_t sizeHigh = 39; uint8_t image[35][22]; for (l = 0; l < 22; l++){ for (cell = 0; cell< 35; cell++){ image[cell][l] = 0; } } cttt=0; adc_init(); ioinit(); // set up interrupt timer for 128 - 22000 sample per second TIFR |= _BV(OCF1A); TCCR1A = 0; // no pwm TCCR1B = _BV(WGM12) | _BV(CS10); // no clock div, CTC mode tick = F_CPU/samplerate; // make it go off 1ce per sample, no more than 22khz OCR1A = tick; OCR1B = 1; TIMSK |= _BV(OCIE1A); // turn it on initifs(&ifs); initross(&ross); sei(); for(;;){ for (x=0;x>4; ifs.step=knob[0]>>4; maxsamp=MAX_SAM/knob[1]; sizeLow = 16 - (knob[2] >> 4); sizeHigh = 18 + (knob[2] >> 4); if (knob[3]>512){ PORTD = (PORTD & 15) + ((dtae<<4)&240); // for switchings } else PORTD = (PORTD & 15); if ((x%knob[2])==0){ switch(knob[3]>>6){ // convert to lookup for right shifted values of potis case 1: l++; // increment line l %= 22; // the board has 22 rows for (cell = 0; cell < 34; cell++){ // do CA if ((cell > sizeLow) && (cell < sizeHigh)){ state = 0; if (image[cell + 1][l]) state |= 0x4; if (image[cell][l]) state |= 0x2; if (image[cell - 1][l]) state |= 0x1; if ((rule >> state) & 1){ state = image[cell][(l) % 22]; state++; // a cell changes color depending on how long it's been alive state %= 5; state++; dtae *= state; image[cell][(l + 1) % 22] = state; } else{ image[cell][(l + 1) % 22] = 0; } } else image[cell][(l + 1) % 22] = 0; } dtae++; break; case 2: runifs(&ifs); dtae=(ifs.returnvalx+1)<<2; // and y break; case 3: runifs(&ifs); dtae=(ifs.returnvaly+1)<<2; // and y break; case 4: runross(&ross); dtae=ross.intx+1; // and y and z break; case 5: runross(&ross); dtae=ross.inty+1; // and y and z break; case 6: runross(&ross); dtae=ross.intz+1; // and y and z break; case 7: xramptr = (unsigned char *)(0x1100+x); dtae=*xramptr; break; case 8: dtae=knob[0]; break; case 9: dtae+=1; break; case 10: dtae=dtae*dtae; } sleep_mode(); // enter sleep mode to emliminate interrupt jitter } } } }