Subversion Repositories chibiosIgnition

Rev

Rev 3 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Font.c
  3.  
  4.  *
  5.  *  Created on: 20 Dec 2015
  6.  *      Author: Mike
  7.  */
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include "Font.h"
  11. #include "ap_math.h"
  12. #include "SSD1306.h"
  13.  
  14.  
  15. // the Lucida font is on a x10 y18 grid
  16. #define FONTX 10
  17. #define FONTY 18
  18.  
  19. #include "ascii-lucida.h"
  20.  
  21. // this gets the pixel for position x,y in character c
  22. uint8_t get_font_pixel(uint16_t x, uint16_t y, uint8_t c)
  23. {
  24.         if(c<32)
  25.            c= 32;
  26.  
  27.         x = x+FONTX*(c-32);
  28.         return ((ascii_lucida_bits[(y*ascii_lucida_width)/8+(x/8)])>>(x&7)) & 1 ;
  29. }
  30.  
  31.  
  32.  
  33. uint8_t cursor_x;
  34. uint8_t cursor_y;
  35.  
  36.  
  37.  
  38.  
  39. void font_gotoxy(uint8_t x,uint8_t y)
  40. {
  41.         cursor_x=x;
  42.         cursor_y=y;
  43. }
  44.  
  45.  
  46.  
  47.  
  48. void font_puts(uint8_t * s)
  49. {
  50.     print_scaled_string(s,cursor_x,cursor_y,strlen(s),384);
  51.  
  52.  
  53.  
  54. }
  55.  
  56.  
  57.  
  58.  
  59. static uint8_t format_num(char * buff, uint8_t digits,uint8_t dp_pos,int val)
  60. {
  61.   digits++;
  62.   uint8_t pos = digits;
  63.   uint8_t dp_loc = pos - dp_pos;
  64.   uint8_t sign = 0;
  65.   if(val<0)
  66.   {
  67.         sign = 1;
  68.         val = -val;
  69.   }
  70.  
  71.   buff[pos]=0;
  72.   while (pos)
  73.   {
  74.         if(pos == dp_loc )
  75.         {
  76.                 buff[--pos]='.';
  77.         }
  78.         else
  79.         {
  80.            buff[--pos] = val%10 + '0';
  81.            val/=10;
  82.            if(val==0 && pos< dp_loc)
  83.                  break;
  84.         }
  85.   }
  86.   if(sign)
  87.  {
  88.         buff[--pos] =  '-';
  89.  }
  90.  return digits;
  91. }
  92.  
  93.  
  94. void font_digits( uint8_t digits,uint8_t dp_pos,  int val)
  95. {
  96.         char buff[10] = "        ";
  97.         uint8_t len = format_num(buff, digits,dp_pos,val);
  98.         font_puts(buff);
  99. }
  100.  
  101.  
  102. void font_sig_digits(uint8_t x, uint8_t y,uint8_t right_justify,uint8_t dp_pos, int val)
  103. {
  104.   char digits;
  105.   char sign = 0;
  106.   int uval;
  107.   if (val < 0)
  108.   {
  109.           uval = -val;
  110.           sign = 1; // mark as negative
  111.   }
  112.   else
  113.   {
  114.           uval = val;
  115.   }
  116.   if(uval<10) // always one digit for a sign or space, one for a digit
  117.   {
  118.           digits = 1;
  119.   }
  120.   if(uval>=10 && uval <100)
  121.   {
  122.           digits = 2;
  123.   }
  124.   if(uval>=100 && uval < 1000)
  125.   {
  126.          digits =3;
  127.   }
  128.   if(uval>=1000)
  129.   {
  130.          digits=4;
  131.   }
  132.   // backup for the - sign if right justified
  133.   if(right_justify)
  134.   {
  135.          if(dp_pos<10)
  136.      {
  137.            digits+=2;
  138.      }
  139.          else
  140.          {
  141.                  digits+=1;
  142.          }
  143.      x-=(digits);
  144.   }
  145.  
  146.  
  147.   font_gotoxy(x,y);
  148.   font_digits(digits,dp_pos,val);
  149. }
  150.  
  151.  
  152.  
  153. void scan_xbm(void)
  154. {
  155.  
  156.         int i,j;
  157.  
  158.         for(i=0; i< FONTY;i++)
  159.         {
  160.                 for(j=0;j<FONTX*6;j++)
  161.                 {
  162.                          drawPixel(j, i, get_font_pixel(j,i,'A')) ;
  163.                 }
  164.         }
  165. }
  166.  
  167.  
  168. // scale is multiplied by 256
  169. void print_scaled_string(char * string, int x, int y, int digits,int scale )
  170. {
  171.  int xt,yt,jt;
  172.  cursor_y = y;
  173.  for(yt=0;yt<FONTY*256;yt+=scale) // iterate down scan lines
  174.     {
  175.             // iterate along the string
  176.                 // local x plotting coordinate
  177.             cursor_x = x;
  178.             for(xt=0;xt<digits;xt++)
  179.         {
  180.                 for(jt=0;jt<FONTX*256;jt+=scale)
  181.                 {
  182.                         unsigned char c = (string[xt] & 0x7F);
  183.                         drawPixel(cursor_x, cursor_y, get_font_pixel(jt/256,yt/256,c)) ;
  184.                     cursor_x++;
  185.                 }
  186.         }
  187.         cursor_y++;
  188.         }
  189.     if(cursor_x > width())
  190.     {
  191.         cursor_x = 0;
  192.     }
  193. }
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200. void print_digits(uint8_t x, uint8_t y, uint8_t digits,uint8_t dp_pos,  int val)
  201. {
  202.  
  203.  
  204.         char buff[10] = "        ";
  205.         uint8_t len = format_num(buff, digits,dp_pos,val);
  206.     print_scaled_string(buff,x,y,len,256);
  207. }
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214. /* print a digit value 0 to 9 (not ASCII)  rotated by ang */
  215. void print_rotated(uint8_t x, uint8_t y, int ang, uint8_t val)
  216. {
  217.         int co=ap_cos(ang);
  218.         int si=ap_sin(ang);
  219.  
  220.         int xt,yt;
  221.  
  222.     for(yt=0;yt<FONTY;yt++) // iterate down scan lines
  223.     {
  224.                 for(xt=0;xt<FONTX;xt++)
  225.                 {
  226.                         if(get_font_pixel(xt,yt,val+'0'))
  227.                         {
  228.                            int cx = xt-FONTX/2;
  229.                            int cy = yt-FONTY/2;
  230.  
  231.                            int px = AP_SCALE(cx*co - cy*si);
  232.                            int py = AP_SCALE(cx*si + cy*co);
  233.  
  234.                            drawPixel(x+px, y+py,WHITE) ;
  235.                         }
  236.                 }
  237.    }
  238.  
  239.  
  240.  
  241.  
  242.  
  243. }
  244.  
  245.  
  246.  
  247.  
  248.