Fast 7-segment number display for TFT

I realized some may be using other orientations of the display and need numbers shown accordingly.

Here are versions for all four directions just in case... :slight_smile:

/* Routine to Draw Large 7-Segment formated number with Arduino TFT Library
   Contributed by William Zaggle  (Uses TFT Library DrawLine and Fill Rectangle functions).

   int n - The number to be displayed
   int xLoc = The x location of the upper left corner of the number
   int yLoc = The y location of the upper left corner of the number
   int cS = The size of the number. 
   fC is the foreground color of the number
   bC is the background color of the number (prevents having to clear previous space)
   nD is the number of digit spaces to occupy (must include space for minus sign for numbers < 0).


    // Example to draw the number 37 in four directions in four corners of the display
    draw7Number(37,10,10,4,WHITE,BLACK,2);          //LEFT2RIGHT
    draw7Number90(37,10,310,4,WHITE,BLACK,2);     //DOWN2UP
    draw7Number180(37,230,310,4,WHITE,BLACK,2);  //RIGHT2LEFT
    draw7Number270(37,230,10,4,WHITE,BLACK,2);    //UP2DOWN
*/
void draw7Number(int n, unsigned int xLoc, unsigned int yLoc, char cS, unsigned int fC, unsigned int bC, char nD) {
  unsigned int num=abs(n),i,s,t,w,col,h,a,b,si=0,j=1,d=0,S2=5*cS,S3=2*cS,S4=7*cS,x1=cS+1,x2=S3+S2+1,y1=yLoc+x1,y3=yLoc+S3+S4+1;
  unsigned int seg[7][3]={{x1,yLoc,1},{x2,y1,0},{x2,y3+x1,0},{x1,(2*y3)-yLoc,1},{0,y3+x1,0},{0,y1,0},{x1,y3,1}};
  unsigned char nums[12]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x67,0x00,0x40},c=(c=abs(cS))>10?10:(c<1)?1:c,cnt=(cnt=abs(nD))>10?10:(cnt<1)?1:cnt;
  for (xLoc+=cnt*(d=S2+(3*S3)+2);cnt>0;cnt--){
    for (i=(num>9)?num%10:((!cnt)&&(n<0))?11:((nD<0)&&(!num))?10:num,xLoc-=d,num/=10,j=0;j<7;++j){
      col=(nums[i]&(1<<j))?fC:bC;
      if (seg[j][2])for(w=S2,t=seg[j][1]+S3,h=seg[j][1]+cS,a=xLoc+seg[j][0]+cS,b=seg[j][1];b<h;b++,a--,w+=2)Tft.drawHorizontalLine(a,b,w,col);
      else for(w=S4,t=xLoc+seg[j][0]+S3,h=xLoc+seg[j][0]+cS,b=xLoc+seg[j][0],a=seg[j][1]+cS;b<h;b++,a--,w+=2)Tft.drawVerticalLine(b,a,w,col);
      for (;b<t;b++,a++,w-=2)seg[j][2]?Tft.drawHorizontalLine(a,b,w,col):Tft.drawVerticalLine(b,a,w,col);
    }
  }
}

void draw7Number90(int n, unsigned int xLoc, unsigned int yLoc, char cS, unsigned int fC, unsigned int bC, char nD) {
  unsigned int num=abs(n),i,s,t,w,col,h,a,b,si=0,j=1,d=0,S2=5*cS,S3=2*cS,S4=7*cS;
  unsigned int x1=cS+1,x2=S3+S2+1,y1=xLoc+x1,y3=xLoc+S3+S4+1;
  unsigned int seg[7][3]={{x1,xLoc,1},{x2,y1,0},{x2,y3+x1,0},{x1,(2*y3)-xLoc,1},{0,y3+x1,0},{0,y1,0},{x1,y3,1}};
  unsigned char nums[12]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x67,0x00,0x40},c=(c=abs(cS))>10?10:(c<1)?1:c,cnt=(cnt=abs(nD))>10?10:(cnt<1)?1:cnt;
  for (yLoc-=cnt*(d=S2+(3*S3)+2);cnt>0;cnt--){
    for (  i=(num>9)?num%10:((!cnt)&&(n<0))?11:((nD<0)&&(!num))?10:num,yLoc+=d,num/=10,j=0;j<7;++j){
      col=(nums[i]&(1<<j))?fC:bC;
      if (seg[j][2])for(w=S2,t=seg[j][1]+S3,h=seg[j][1]+cS,a=yLoc-(seg[j][0]+cS+S2-1),b=seg[j][1];b<h;b++,a--,w+=2)Tft.drawVerticalLine(b,a,w,col);
      else for(w=S4,t=yLoc-seg[j][0]-S3,h=yLoc-seg[j][0]-cS,b=yLoc-seg[j][0],a=seg[j][1]+cS;b>h;b--,a--,w+=2)Tft.drawHorizontalLine(a,b,w,col);
      for (;seg[j][2]?b<t:b>t;seg[j][2]?b++:b--,a++,w-=2)seg[j][2]?Tft.drawVerticalLine(b,a,w,col):Tft.drawHorizontalLine(a,b,w,col);
    }
  }
}


void draw7Number180(int n, unsigned int xLoc, unsigned int yLoc, char cS, unsigned int fC, unsigned int bC, char nD) {
  unsigned int num=abs(n),i,s,t,w,col,h,a,b,si=0,j=1,d=0,S2=5*cS,S3=2*cS,S4=7*cS;
  unsigned int x1=cS,x2=S3+S2+1,y1=yLoc-x1,y3=yLoc-S3-S4-1;
  unsigned int seg[7][3]={{x1,yLoc,1},{x2,y1,0},{x2,y3-x1,0},{x1,(2*y3)-yLoc,1},{0,y3-x1,0},{0,y1,0},{x1,y3,1}};
  unsigned char nums[12]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x67,0x00,0x40},c=(c=abs(cS))>10?10:(c<1)?1:c,cnt=(cnt=abs(nD))>10?10:(cnt<1)?1:cnt;
  for (xLoc-=cnt*(d=S2+(3*S3)+2);cnt>0;cnt--){
    for (i=(num>9)?num%10:((!cnt)&&(n<0))?11:((nD<0)&&(!num))?10:num,xLoc+=d,num/=10,j=0;j<7;++j){
      col=(nums[i]&(1<<j))?fC:bC;
      if (seg[j][2])for(w=S2,t=seg[j][1]-S3,h=seg[j][1]-cS,a=xLoc-(seg[j][0]+cS+S2),b=seg[j][1];b>h;b--,a--,w+=2)Tft.drawHorizontalLine(a,b,w,col);
      else for(w=S4,t=xLoc-seg[j][0]-S3,h=xLoc-seg[j][0]-cS,b=xLoc-seg[j][0],a=seg[j][1]-cS-S4;b>h;b--,a--,w+=2)Tft.drawVerticalLine(b,a,w,col);
      for (;b>t;b--,a++,w-=2)seg[j][2]?Tft.drawHorizontalLine(a,b,w,col):Tft.drawVerticalLine(b,a,w,col);
    }
  }
}

void draw7Number270(int n, unsigned int xLoc, unsigned int yLoc, char cS, unsigned int fC, unsigned int bC, char nD) {
  unsigned int num=abs(n),i,s,t,w,col,h,a,b,si=0,j=1,d=0,S2=5*cS,S3=2*cS,S4=7*cS;
  unsigned int x1=cS+1,x2=S3+S2+1,y1=xLoc-x1,y3=xLoc-S3-S4-1;
  unsigned int seg[7][3]={{x1,xLoc,1},{x2,y1,0},{x2,y3-x1,0},{x1,(2*y3)-xLoc,1},{0,y3-x1,0},{0,y1,0},{x1,y3,1}};
  unsigned char nums[12]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x67,0x00,0x40},c=(c=abs(cS))>10?10:(c<1)?1:c,cnt=(cnt=abs(nD))>10?10:(cnt<1)?1:cnt;
  for (yLoc+=cnt*(d=S2+(3*S3)+2);cnt>0;cnt--){
    for (i=(num>9)?num%10:((!cnt)&&(n<0))?11:((nD<0)&&(!num))?10:num,yLoc-=d,num/=10,j=0;j<7;++j){
      col=(nums[i]&(1<<j))?fC:bC;
      if (seg[j][2])for(w=S2,t=seg[j][1]-S3,h=seg[j][1]-cS,a=yLoc+(seg[j][0]+cS),b=seg[j][1];b>h;b--,a--,w+=2)Tft.drawVerticalLine(b,a,w,col);
      else for(w=S4,t=yLoc+seg[j][0]+S3,h=yLoc+seg[j][0]+cS,b=yLoc+seg[j][0],a=seg[j][1]-cS-S4+1;b<h;b++,a--,w+=2)Tft.drawHorizontalLine(a,b,w,col);
      for (;seg[j][2]?b>t:b<t;seg[j][2]?b--:b++,a++,w-=2)seg[j][2]?Tft.drawVerticalLine(b,a,w,col):Tft.drawHorizontalLine(a,b,w,col);
    }
  }
}