// === Color Thread ================================================= // draw 3 color patches for R,G,B from a random number //static int color ; //static int i; static PT_THREAD (protothread_color(struct pt *pt)) { PT_BEGIN(pt); static int color ; static int i; while(1) { // yield time 1 second PT_YIELD_TIME_msec(2000) ; // choose a random color color = rand() & 0xffff ; // draw color string tft_fillRoundRect(0,50, 150, 14, 1, ILI9340_BLACK);// x,y,w,h,radius,color tft_setCursor(0, 50); tft_setTextColor(ILI9340_WHITE); tft_setTextSize(1); sprintf(buffer," %04x %04x %04x %04x", color & 0x1f, color & 0x7e0, color & 0xf800, color); tft_writeString(buffer); // draw the actual color patches tft_fillRoundRect(5,70, 30, 30, 1, color & 0x1f);// x,y,w,h,radius,blues tft_fillRoundRect(40,70, 30, 30, 1, color & 0x7e0);// x,y,w,h,radius,greens tft_fillRoundRect(75,70, 30, 30, 1, color & 0xf800);// x,y,w,h,radius,reds // now draw the RGB mixed color tft_fillRoundRect(110,70, 30, 30, 1, color);// x,y,w,h,radius,mix color // NEVER exit while } // END WHILE(1) PT_END(pt); } // color thread
static PT_THREAD (protothread_key(struct pt *pt)) { PT_BEGIN(pt); static int keypad, i, pattern; // order is 0 thru 9 then * ==10 and # ==11 // no press = -1 // table is decoded to natural digit order (except for * and #) // 0x80 for col 1 ; 0x100 for col 2 ; 0x200 for col 3 // 0x01 for row 1 ; 0x02 for row 2; etc static int keytable[12]={0x108, 0x81, 0x101, 0x201, 0x82, 0x102, 0x202, 0x84, 0x104, 0x204, 0x88, 0x208}; // init the keypad pins A0-A3 and B7-B9 // PortA ports as digital outputs mPORTASetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2 | BIT_3); //Set port as output // PortB as inputs mPORTBSetPinsDigitalIn(BIT_7 | BIT_8 | BIT_9); //Set port as input while(1) { // read each row sequentially mPORTAClearBits(BIT_0 | BIT_1 | BIT_2 | BIT_3); pattern = 1; mPORTASetBits(pattern); // yield time PT_YIELD_TIME_msec(30); //mPORTAClearBits(BIT_0 | BIT_1 | BIT_2 | BIT_3); //pattern = 1; mPORTASetBits(pattern); for (i=0; i<4; i++) { keypad = mPORTBReadBits(BIT_7 | BIT_8 | BIT_9); if(keypad!=0) {keypad |= pattern ; break;} mPORTAClearBits(pattern); pattern <<= 1; mPORTASetBits(pattern); } // search for keycode if (keypad > 0){ // then button is pushed for (i=0; i<12; i++){ if (keytable[i]==keypad) break; } } else i = -1; // no button pushed // draw key number tft_fillRoundRect(30,200, 100, 28, 1, ILI9340_BLACK);// x,y,w,h,radius,color tft_setCursor(30, 200); tft_setTextColor(ILI9340_YELLOW); tft_setTextSize(4); sprintf(buffer,"%d", i); if (i==10)sprintf(buffer,"*"); if (i==11)sprintf(buffer,"#"); tft_writeString(buffer); // NEVER exit while } // END WHILE(1) PT_END(pt); } // keypad thread
// This thead displays the value of the capcitor if it is in the 1nF-100nF range // or diplays a message if the capacitor is out of range static PT_THREAD(protothread_cap_read(struct pt *pt)) { PT_BEGIN(pt); tft_setCursor(0,0); tft_setTextColor(ILI9340_WHITE); tft_setTextSize(2); tft_writeString("Measuring capacitance:"); while(1) { tft_setCursor(0,30); tft_setTextColor(ILI9340_YELLOW); tft_setTextSize(2); if (capacitance < 1 || capacitance > 99) { tft_fillRoundRect(0,30, 320, 40, 1, ILI9340_BLACK);// x,y,w,h,radius,color tft_writeString("Capacitor out of range!"); } else { tft_fillRoundRect(0,30, 320, 40, 1, ILI9340_BLACK);// x,y,w,h,radius,color sprintf(buffer, "%4.1f nF", capacitance); tft_writeString(buffer); } PT_YIELD_TIME_msec(200); } PT_END(pt); }
// === Timer Thread ================================================= // update a 1 second tick counter static PT_THREAD (protothread_timer(struct pt *pt)) { PT_BEGIN(pt); tft_setCursor(0, 0); tft_setTextColor(ILI9340_WHITE); tft_setTextSize(1); tft_writeString("Time in seconds since boot\n"); while(1) { // yield time 1 second PT_YIELD_TIME_msec(1000) ; sys_time_seconds++ ; // draw sys_time tft_fillRoundRect(0,10, 100, 14, 1, ILI9340_BLACK);// x,y,w,h,radius,color tft_setCursor(0, 10); tft_setTextColor(ILI9340_YELLOW); tft_setTextSize(2); sprintf(buffer,"%d", sys_time_seconds); tft_writeString(buffer); // NEVER exit while } // END WHILE(1) PT_END(pt); } // timer thread
//======================= Refresh ========================= // //Does Ball calculations and Draws the necessary elements on the screen static PT_THREAD (protothread_refresh(struct pt *pt)) { PT_BEGIN(pt); PT_YIELD_TIME_msec(100); //waits for the scoreboard to be set up while(1) { while (timeElapsed <=60) { PT_YIELD_TIME_msec(10); DmaChnDisable(dmaChn); DmaChnDisable(dmaChn2); //Generates a new ball at a given interval if(ballgen >= 10) { int troll1 = -((rand()) % 2)-1; int troll2 = ((rand()) % 6) - 3; struct Ball *temp = Ball_create(320,120,troll1,troll2,(numBalls+1)*500,0,NULL); temp->b = head; head = temp; ballgen = 0; numBalls++; } else ballgen ++; //collision calculations struct Ball *ti = head; struct Ball *tj = NULL; if(ti != NULL) tj = ti->b; while(ti !=NULL){ //Calculates the collisions between every ball while(tj != NULL) { int rij_x = ti->xpos - tj->xpos; int rij_y = ti->ypos - tj->ypos; int mag_rij = pow(rij_x,2) + pow(rij_y,2); //Checks if ti and tj are not pointing to the same ball, //If they close enough for a collision and there is no collision //delay. if( ti->delay + tj->delay == 0 && mag_rij < dist) { int vij_x = ti->xvel - tj->xvel; int vij_y = ti->yvel - tj->yvel; if (mag_rij==0) { mag_rij=dist; } int deltaVi_x = (int)((-1*(rij_x) * ((((rij_x * vij_x)+ (rij_y*vij_y)) << 7)/mag_rij)) >> 7); int deltaVi_y = (int)((-1*(rij_y) * ((((rij_x * vij_x)+ (rij_y*vij_y)) << 7)/mag_rij)) >> 7); /* tft_fillRoundRect(0,30, 320, 14, 1, ILI9340_BLACK);// x,y,w,h,radius,color tft_setCursor(0, 30); tft_setTextColor(ILI9340_WHITE); tft_setTextSize(2); sprintf(buffer,"%d:%d", (-1*(rij_x)/128 * (128*((rij_x * vij_x)+ (rij_y*vij_y))/mag_rij)), mag_rij); tft_writeString(buffer); */ //Updates the velocity ti->xvel = ti->xvel + deltaVi_x; ti->yvel = ti->yvel + deltaVi_y; tj->xvel = tj->xvel - deltaVi_x; tj->yvel = tj->yvel - deltaVi_y; ti->delay = delay_master; tj->delay = delay_master; } tj = tj->b; } //checks for wall collisions if(ti->xpos >= 320*scale || ti->xpos <= 0) ti->xvel = -1*ti->xvel; if(ti->ypos >= 240*scale || ti->ypos <= 35*scale) { ti->yvel = -1*ti->yvel; if (ti->xpos > 120*scale && ti->xpos < 200*scale) { //check for catch bin ti->delay=-1; //set to -1 to indicate +1 point } } //calculates the drag if(ti->xvel > 0) ti->xvel = ti->xvel - ti->xvel/drag; else ti->xvel = ti->xvel + ti->xvel/drag; if(ti->yvel > 0) ti->yvel = ti->yvel - ti->yvel/drag; else ti->yvel = ti->yvel - ti->yvel/drag; // Check for paddle Collisions if(abs(paddle_xpos-ti->xpos/scale) <= ballradius && ti->delay == 0) if(ti->ypos/scale > paddle_ypos - half_paddle_length && ti->ypos/scale < paddle_ypos + half_paddle_length) { ti->xvel = -1*ti->xvel; ti->yvel = ti->yvel + paddle_drag*paddle_v; ti->delay=delay_master; } //Decrement the collide delay if(ti->delay > 0) ti->delay = ti->delay -1; //iterates through the next set ti = ti->b; if(ti != NULL) tj = ti->b; //removes the last element if the limit is reached if(numBalls > maxBalls && tj->b == NULL) { tft_fillCircle(tj->xpos/scale,tj->ypos/scale,ballradius,ILI9340_BLACK); //erases from the screen ti->b = NULL; numBalls--; score++; //free(tj); } } // Calculates position of the paddle and draw //TODO: Calculate paddle position tft_drawLine(paddle_xpos,paddle_ypos - half_paddle_length, paddle_xpos, paddle_ypos + half_paddle_length, ILI9340_BLACK); paddle_v=paddle_ypos; paddle_ypos=(adc_9*240)/1023; paddle_v=paddle_ypos-paddle_v; tft_drawLine(paddle_xpos,paddle_ypos - half_paddle_length, paddle_xpos, paddle_ypos + half_paddle_length, ILI9340_WHITE); // Now it calculates the new position ti = head; tj = head; while(ti != NULL){ //"Clears" the image of the last ball tft_fillCircle(ti->xpos/scale,ti->ypos/scale,ballradius,ILI9340_BLACK); //Updates the new position of the ball ti->xpos = ti->xpos + ti->xvel; ti->ypos = ti->ypos + ti->yvel; //ensures the positions are within bounds //If the pos is less than 0 then we remove it //delay must also not be -1 (ie >=0) if(ti->xpos > paddle_xpos && ti->delay != -1) { if(ti->xpos > 320*scale) ti->xpos = 320*scale; if(ti->ypos > 240*scale) ti->ypos = 240*scale; else if(ti->ypos < 35*scale) ti->ypos = 35*scale; if(ti->delay > 0) tft_fillCircle(ti->xpos/scale, ti->ypos/scale, ballradius, ILI9340_WHITE); else tft_fillCircle(ti->xpos/scale, ti->ypos/scale, ballradius, ti->color); } else { //REMOVES THE BALL IF IT CROSSES THE BOUNDARY if (ti->delay==-1) { //check if went into catch bins score++; DmaChnEnable(dmaChn2); } else { DmaChnEnable(dmaChn); score--; } if(ti == head) head = head->b; else tj->b = ti->b; numBalls--; //free(ti); } tj = ti;//what does this do? ti = ti->b; } frames ++; } tft_fillRoundRect(0,35, 320, 205, 1, ILI9340_BLACK);// x,y,w,h,radius,color DmaChnDisable(dmaChn); DmaChnDisable(dmaChn2); DmaChnEnable(dmaChn3); while (1) { tft_setCursor(20, 120); tft_setTextColor(ILI9340_WHITE); tft_setTextSize(4); sprintf(buffer,"Game Over!"); tft_writeString(buffer); } }