/*
* Font.c
*
* Created on: 20 Dec 2015
* Author: Mike
*/
#include <stdint.h>
#include <string.h>
#include "Font.h"
#include "ap_math.h"
#include "SSD1306.h"
// the Lucida font is on a x10 y18 grid
#define FONTX 10
#define FONTY 18
#include "ascii-lucida.h"
// this gets the pixel for position x,y in character c
uint8_t get_font_pixel(uint16_t x, uint16_t y, uint8_t c)
{
if(c<32)
c= 32;
x = x+FONTX*(c-32);
return ((ascii_lucida_bits[(y*ascii_lucida_width)/8+(x/8)])>>(x&7)) & 1 ;
}
uint8_t cursor_x;
uint8_t cursor_y;
void font_gotoxy(uint8_t x,uint8_t y)
{
cursor_x=x;
cursor_y=y;
}
void font_puts(uint8_t * s)
{
print_scaled_string
(s
,cursor_x
,cursor_y
,strlen(s
),384);
}
static uint8_t format_num(char * buff, uint8_t digits,uint8_t dp_pos,int val)
{
digits++;
uint8_t pos = digits;
uint8_t dp_loc = pos - dp_pos;
uint8_t sign = 0;
if(val<0)
{
sign = 1;
val = -val;
}
buff[pos]=0;
while (pos)
{
if(pos == dp_loc )
{
buff[--pos]='.';
}
else
{
buff[--pos] = val%10 + '0';
val/=10;
if(val==0 && pos< dp_loc)
break;
}
}
if(sign)
{
buff[--pos] = '-';
}
return digits;
}
void font_digits( uint8_t digits,uint8_t dp_pos, int val)
{
char buff[10] = " ";
uint8_t len = format_num(buff, digits,dp_pos,val);
font_puts(buff);
}
void font_sig_digits(uint8_t x, uint8_t y,uint8_t right_justify,uint8_t dp_pos, int val)
{
char digits;
char sign = 0;
int uval;
if (val < 0)
{
uval = -val;
sign = 1; // mark as negative
}
else
{
uval = val;
}
if(uval<10) // always one digit for a sign or space, one for a digit
{
digits = 1;
}
if(uval>=10 && uval <100)
{
digits = 2;
}
if(uval>=100 && uval < 1000)
{
digits =3;
}
if(uval>=1000)
{
digits=4;
}
// backup for the - sign if right justified
if(right_justify)
{
if(dp_pos<10)
{
digits+=2;
}
else
{
digits+=1;
}
x-=(digits);
}
font_gotoxy(x,y);
font_digits(digits,dp_pos,val);
}
void scan_xbm(void)
{
int i,j;
for(i=0; i< FONTY;i++)
{
for(j=0;j<FONTX*6;j++)
{
drawPixel(j, i, get_font_pixel(j,i,'A')) ;
}
}
}
// scale is multiplied by 256
void print_scaled_string(char * string, int x, int y, int digits,int scale )
{
int xt,yt,jt;
cursor_y = y;
for(yt=0;yt<FONTY*256;yt+=scale) // iterate down scan lines
{
// iterate along the string
// local x plotting coordinate
cursor_x = x;
for(xt=0;xt<digits;xt++)
{
for(jt=0;jt<FONTX*256;jt+=scale)
{
unsigned char c = (string[xt] & 0x7F);
drawPixel(cursor_x, cursor_y, get_font_pixel(jt/256,yt/256,c)) ;
cursor_x++;
}
}
cursor_y++;
}
if(cursor_x > width())
{
cursor_x = 0;
}
}
void print_digits(uint8_t x, uint8_t y, uint8_t digits,uint8_t dp_pos, int val)
{
char buff[10] = " ";
uint8_t len = format_num(buff, digits,dp_pos,val);
print_scaled_string(buff,x,y,len,256);
}
/* print a digit value 0 to 9 (not ASCII) rotated by ang */
void print_rotated(uint8_t x, uint8_t y, int ang, uint8_t val)
{
int co=ap_cos(ang);
int si=ap_sin(ang);
int xt,yt;
for(yt=0;yt<FONTY;yt++) // iterate down scan lines
{
for(xt=0;xt<FONTX;xt++)
{
if(get_font_pixel(xt,yt,val+'0'))
{
int cx = xt-FONTX/2;
int cy = yt-FONTY/2;
int px = AP_SCALE(cx*co - cy*si);
int py = AP_SCALE(cx*si + cy*co);
drawPixel(x+px, y+py,WHITE) ;
}
}
}
}