Subversion Repositories chibiosIgnition

Rev

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