/*------------------------------------------------ Display large digits (18 x 28 pixels) on the NXT screen by techbricks.nl two row, each row 4 digits, range: .0001 ... 9999 24 september 2009 NXC, build on NXC/NBC firmware version 1.28, Bricxcc 3.3 - build 3.3.7.20 Check http://bricxcc.sourceforge.net/nbc/ for more info abou NXC Read more about it at www.techbricks.nl/My-NXT-projects/nxttachometerspeedcomputer.html Use this demo program to display digits for your own applications.... -------------------------------------------------*/ // variable declaration int row,value,len_string,current,oldest; int decimal_point,current_position,point_position,start_position,end_position; int digit,current_digit,shift_x,shift_y; string sdiget,svalue,szeros; bool bit[10][8]; int shift_x_digit[5],shift_y_digit[5]; /*--------------------------------------------------- This function draws the digit on the NXT display x_position is the horizontal position (digit 1,2,3 or 4) y_positopn is the virtical position (row 1 or 2) ----------------------------------------------------*/ void DisplayDigit(int digit, int position_x, int position_y) { // shift digit to the disired position shift_x=shift_x_digit[position_x]; shift_y=shift_y_digit[position_y]; /*---------- Construction of the digits ----- The digits are made of 7 bits --1-- | | 2 3 |--4--| 5 6 | | --7-- --------------------*/ // display the first bit if (bit[digit][1]==true) { LineOut(1+shift_x,60-shift_y,16+shift_x,60-shift_y); LineOut(2+shift_x,59-shift_y,15+shift_x,59-shift_y); LineOut(3+shift_x,58-shift_y,14+shift_x,58-shift_y); } // display the second bit if (bit[digit][2]==true) { LineOut(0+shift_x,58-shift_y,0+shift_x,48-shift_y); LineOut(1+shift_x,57-shift_y,1+shift_x,49-shift_y); LineOut(2+shift_x,56-shift_y,2+shift_x,50-shift_y); } // display the thirt bit if (bit[digit][3]==true) { LineOut(17+shift_x,58-shift_y,17+shift_x,48-shift_y); LineOut(16+shift_x,57-shift_y,16+shift_x,49-shift_y); LineOut(15+shift_x,56-shift_y,15+shift_x,50-shift_y); } // display the fourth bit if (bit[digit][4]==true) { LineOut(2+shift_x,47-shift_y,15+shift_x,47-shift_y); LineOut(1+shift_x,46-shift_y,16+shift_x,46-shift_y); LineOut(2+shift_x,45-shift_y,15+shift_x,45-shift_y); } // display the fifth bit if (bit[digit][5]==true) { LineOut(0+shift_x,44-shift_y,0+shift_x,34-shift_y); LineOut(1+shift_x,43-shift_y,1+shift_x,35-shift_y); LineOut(2+shift_x,42-shift_y,2+shift_x,36-shift_y); } // display the sixth bit if (bit[digit][6]==true) { LineOut(17+shift_x,44-shift_y,17+shift_x,34-shift_y); LineOut(16+shift_x,43-shift_y,16+shift_x,35-shift_y); LineOut(15+shift_x,42-shift_y,15+shift_x,36-shift_y); } // display the seventh bit if (bit[digit][7]==true) { LineOut(3+shift_x,34-shift_y,14+shift_x,34-shift_y); LineOut(2+shift_x,33-shift_y,15+shift_x,33-shift_y); LineOut(1+shift_x,32-shift_y,16+shift_x,32-shift_y); } } /*-------------------------------------------------------- This function does the initializing of the figures ---------------------------------------------------------*/ void DisplayInitiation() { // digit 0 - bit 1,2,3,5,6,7 bit[0][1]=true; // _ bit[0][2]=true; // | | bit[0][3]=true; // |_| bit[0][4]=false; bit[0][5]=true; bit[0][6]=true; bit[0][7]=true; // digit 1 - bit 3,6 bit[1][1]=false; // | bit[1][2]=false; // | bit[1][3]=true; bit[1][4]=false; bit[1][5]=false; bit[1][6]=true; bit[1][7]=false; // digit 2 - bit 1,3,4,5,7 bit[2][1]=true; // _ bit[3][2]=false; // _| bit[2][3]=true; // |_ bit[2][4]=true; bit[2][5]=true; bit[2][6]=false; bit[2][7]=true; // digit 3 - bit 1,3,4,6,7 bit[3][1]=true; // _ bit[3][2]=false; // _| bit[3][3]=true; // _| bit[3][4]=true; bit[3][5]=false; bit[3][6]=true; bit[3][7]=true; // digit 4 - bit 2,3,4,6 bit[4][1]=false; // bit[4][2]=true; // |_| bit[4][3]=true; // | bit[4][4]=true; bit[4][5]=false; bit[4][6]=true; bit[4][7]=false; // digit 5 - bit 1,2,4,6,7 bit[5][1]=true; // _ bit[5][2]=true; // |_ bit[5][3]=false; // _| bit[5][4]=true; bit[5][5]=false; bit[5][6]=true; bit[5][7]=true; // digit 6 - bit 1,2,4,5,6,7 bit[6][1]=true; // _ bit[6][2]=true; // |_ bit[6][3]=false; // |_| bit[6][4]=true; bit[6][5]=true; bit[6][6]=true; bit[6][7]=true; // digit 7 - bit 1,3,6 bit[7][1]=true; // _ bit[7][2]=false; // | bit[7][3]=true; // | bit[7][4]=false; bit[7][5]=false; bit[7][6]=true; bit[7][7]=false; // digit 8 - bit 1,2,3,4,5,6,7 bit[8][1]=true; // _ bit[8][2]=true; // |_| bit[8][3]=true; // |_| bit[8][4]=true; bit[8][5]=true; bit[8][6]=true; bit[8][7]=true; // digit 9 - bit 1,2,3,4,6,7 bit[9][1]=true; // _ bit[9][2]=true; // |_| bit[9][3]=true; // _| bit[9][4]=true; bit[9][5]=false; bit[9][6]=true; bit[9][7]=true; // this specifices how many pixels to shift horizontal for the next digits shift_x_digit[1]=7; shift_x_digit[2]=32; shift_x_digit[3]=55; shift_x_digit[4]=80; // this specifices how many pixels to shift virtical for the next line shift_y_digit[1]=-1; shift_y_digit[2]=31; // clear the NXT display ClearScreen(); } /*-------------------------------------------------------- This function displayd the digits and decimal point on the correct row ---------------------------------------------------------*/ void DisplayNumber(int value,int decimal_point,int row) { if (row<1) row=1; if (row>2) row=2; if (decimal_point<0) decimal_point=0; if (decimal_point>6) decimal_point=6; // transform integer to string and determine the lenght. svalue=NumToStr(value); len_string=StrLen(svalue); if ((len_string-decimal_point)>4) { svalue="9999"; len_string=4; decimal_point=0; } else { if ((len_string-decimal_point)<-3) { svalue="0000"; len_string=4; decimal_point=0; } else { if ((len_string-decimal_point)<1) { szeros=SubStr("0000",1,(1+decimal_point-len_string)); svalue=StrCat(szeros,svalue); len_string=StrLen(svalue); decimal_point=decimal_point-len_string; if (len_string>4) { len_string=4; } decimal_point=decimal_point+len_string; } } } // Determine most right displayed digit. if (len_string>4) { decimal_point=decimal_point-(len_string-4); len_string=4; } // Determine most left digit position. start_position=5-len_string; current_position=1; while (current_position<=len_string) { // get sub string sdiget=SubStr(svalue,current_position-1,1); current_digit=StrToNum(sdiget); // display digit DisplayDigit(current_digit,(start_position+current_position-1),row); current_position=current_position+1; } point_position=4-decimal_point; if ((point_position>=0)&&(point_position<4)) { CircleOut(shift_x_digit[point_position+1]-4, 33-shift_y_digit[row], 1); CircleOut(shift_x_digit[point_position+1]-4, 33-shift_y_digit[row], 2); } } task main() { DisplayInitiation(); while (true) { ClearScreen(); // Set these values: value=Random(10000); //value to be displayed on the NXT screen, can 4 digits row=1; //row on the NXT screen, can be 1 or 2 decimal_point=Random(6); //amount of figures on the right side of the decimal point, can be between 0 and 5 // Display Number on NXT screen DisplayNumber(value,decimal_point,row); value=Random(10000); //value to be displayed on the NXT screen, can 4 digits row=2; //row on the NXT screen, can be 1 or 2 decimal_point=Random(6); //amount of figures on the right side of the decimal point, can be between 0 and 5 // Display Number on NXT screen DisplayNumber(value,decimal_point,row); Wait(1000); } }