// 全体の初期化 void init( void ) { // シリアル通信、カウンタ、PWM出力の初期化 sci_init( 38400 ); // コンペアマッチタイマの初期化 setIntMask( 9 ); // レベル10〜15は割り込み許可 initServo( ); noPWM_brake( ); initCounter( ); // エンコーダカウンタの初期化 initAD( ); }
void tileSetup(void){ //Initialization routines initIO(); setPort(&PORTB); sendColor(LEDCLK,LEDDAT,dark); sei(); initAD(); initTimer(); set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); //Set up timing ring buffers uint8_t i; for(i = 0; i<6; i++){ timeBuf[i]=0; } mode = running; }
int main(void) { //Initialization routines initIO(); setPort(&PORTB); sendColor(LEDCLK,LEDDAT,colors[0]); sei(); initAD(); initTimer(); set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); //Set up timing ring buffers for(uint8_t i = 0; i<6; i++){ timeBuf[i]=0; } mode = running; while(1) { if(mode==sleep){ disAD(); // DDRB &= ~IR;//Set direction in PORTB &= ~IR;//Set pin tristated sendColor(LEDCLK, LEDDAT, dark); PORTA |= POWER;//Set LED and Mic power pin high (off) wake = 0; while(!wake){ cli(); uint32_t diff = timer-powerDownTimer; sei(); if(diff>500){ sleep_cpu(); wake = 1; } } holdoff=500; PORTA &= ~POWER; uint8_t done = 0; cli(); uint32_t startTime = timer; sei(); while(!done){ cli(); uint32_t time = timer; sei(); if(time-startTime>250){ done=1; } } done = 0; cli(); startTime = timer; sei(); DDRB |= IR;//Set direction out PORTB |= IR;//Set pin on sendColor(LEDCLK, LEDDAT, transmitColor); while(!done){ cli(); uint32_t time = timer; sei(); if(time-startTime>500){ done=1; } } enAD(); powerDownTimer = timer; sleepTimer = timer; mode = running; }else if(mode==running){ if(click){ uint8_t neighborStates[6]; getStates(neighborStates); uint8_t clickColor[] = {bright(colors[state][0]),bright(colors[state][1]),bright(colors[state][2])}; sendColor(LEDCLK, LEDDAT, clickColor); uint8_t numOn = 0; sync = 3;//request sync pulse be sent at next possible opportunity (set to 4 for logistical reasons) for (uint8_t i = 0; i< 6; i++) { //at the moments specific state detection is a bit iffy, //so mapping any received state>1 to a state of 1 works for now if(neighborStates[i]>0){ numOn++; } } //Logic for what to do to the state based on number of on neighbors if(state == 0) { if(birthRules[numOn]) { state=1; } } else { if(deathRules[numOn]) { state = (state + 1) % numStates; } } //End logic //prevent another click from being detected for a bit holdoff = 100; click = 0; } //periodically update LED once every 0x3F = 64 ms (fast enough to feel responsive) cli(); if(!(timer & 0x3F)){ if(timer!=lastTime){ lastTime=timer; cycleTimer++; cycleTimer %= CYCLE_TIME; uint8_t outColor[3] = {(colors[state][0]*breatheCycle[cycleTimer])/255, (colors[state][1]*breatheCycle[cycleTimer])/255, (colors[state][2]*breatheCycle[cycleTimer])/255}; sendColor(LEDCLK, LEDDAT, outColor); } } sei(); //check if we enter sleep mode cli();//temporarilly prevent interrupts to protect timer if(timer-sleepTimer>1000*TIMEOUT){ mode = sleep; } sei(); }else if(mode==recieving){ //disable A/D disAD(); //set photo transistor interrupt to only trigger on specific direction setDir(progDir); //set recieving color sendColor(LEDCLK, LEDDAT, recieveColor); //record time entering the mode for timeout cli(); uint32_t modeStart = timer; sei(); while(mode==recieving){//stay in this mode until instructed to leave or timeout cli(); uint32_t diff = timer-modeStart; sei(); if(diff>3000){//been in mode 1 for more than 5 seconds mode = transmitting; } } }else if(mode==transmitting){ //disable Phototransistor Interrupt setDirNone(); //set LED to output DDRB |= IR;//Set direction out //send 5 pulses uint32_t startTime = timer; if(bitsRcvd>=8 && comBuf[0]!=seqNum){ for(int i=0; i<5; i++){ while(timer==startTime){ PORTB &= ~IR; } startTime = timer; while(timer==startTime){ PORTB |= IR; } startTime = timer; } parseBuffer(); }else{ bitsRcvd = 0; } startTime = timer; sendColor(LEDCLK, LEDDAT, transmitColor);//update color while waiting while(timer<startTime+20);//pause for mode change startTime = timer; uint16_t timeDiff; uint16_t bitNum; while(bitsRcvd>0){ timeDiff = (timer-startTime)/PULSE_WIDTH; bitNum = timeDiff/2; if(timeDiff%2==0){//first half if(comBuf[bitNum/8]&(1<<bitNum%8)){//bit high PORTB &= ~IR; }else{//bit low PORTB |= IR; } }else{//second half if(comBuf[bitNum/8]&(1<<bitNum%8)){//bit high PORTB |= IR; }else{//bit low PORTB &= ~IR; } } if(bitNum>=bitsRcvd){ bitsRcvd = 0; } } //while(timer<startTime+2000);//pause for effect //done transmitting //re-enable A/D enAD(); //re-enable all phototransistors setDirAll(); state = 0; mode = running; } } }