[Linux]数码相框&电子书(一)

[Linux]数码相框&电子书(一)

个人博客https://aeneag.xyz/

艾恩凝

2021/7/14

Introduction

项目第一天,主要研究了LCD显示文字,基础不1313了,搞了整整一天,知识点到也就这样了,灵活运用就可以了,几年前搞stm32的时候,现在的屏幕显示字和32的差不多,轻车熟路。其他的没什么好说

Achievement

IMG20210714190919.jpg

上面是明白基础知识以后,打上的字

Code

int fd_fb;
struct fb_var_screeninfo var;	/* Current var */
struct fb_fix_screeninfo fix;	/* Current fix */
int screen_size;
unsigned char *fbmem;
unsigned int line_width;
unsigned int pixel_width;

int fd_hzk16;
struct stat hzk_stat;
unsigned char *hzkmem;

FT_Library	  library;
FT_Face 	  face;
FT_Error error;
FT_Vector	  pen;
FT_GlyphSlot  slot;
FT_Matrix	  matrix;				  /* transformation matrix */

double		  aen_angle;

/* color : 0x00RRGGBB */
void lcd_put_pixel(int x, int y, unsigned int color)
{
	unsigned char *pen_8 = fbmem+y*line_width+x*pixel_width;
	unsigned short *pen_16;
	unsigned int *pen_32;

	unsigned int red, green, blue;

	pen_16 = (unsigned short *)pen_8;
	pen_32 = (unsigned int *)pen_8;

	switch (var.bits_per_pixel)
	{
		case 8:
		{
			*pen_8 = color;
			break;
		}
		case 16:
		{
			/* 565 */
			red   = (color >> 16) & 0xff;
			green = (color >> 8) & 0xff;
			blue  = (color >> 0) & 0xff;
			color = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);
			*pen_16 = color;
			break;
		}
		case 32:
		{
			*pen_32 = color;
			break;
		}
		default:
		{
			printf("can't surport %dbpp\n", var.bits_per_pixel);
			break;
		}
	}
}

void lcd_put_ascii(int x, int y, unsigned char c)
{
	unsigned char *dots = (unsigned char *)&fontdata_8x16[c*16];
	int i, b;
	unsigned char byte;

	for (i = 0; i < 16; i++)
	{
		byte = dots[i];
		for (b = 7; b >= 0; b--)
		{
			if (byte & (1<<b))
			{
				/* show */
				lcd_put_pixel(x+7-b, y+i, 0xffffff); /* 白 */
			}
			else
			{
				/* hide */
				lcd_put_pixel(x+7-b, y+i, 0); /* 黑 */
			}
		}
	}
}

void lcd_put_chinese(int x, int y, unsigned char *str)
{
	unsigned int area  = str[0] - 0xA1;
	unsigned int where = str[1] - 0xA1;
	unsigned char *dots = hzkmem + (area * 94 + where)*32;
	unsigned char byte;

	int i, j, b;
	for (i = 0; i < 16; i++)
		for (j = 0; j < 2; j++)
		{
			byte = dots[i*2 + j];
			for (b = 7; b >=0; b--)
			{
				if (byte & (1<<b))
				{
					/* show */
					lcd_put_pixel(x+j*8+7-b, y+i, 0xffffff); /* 白 */
				}
				else
				{
					/* hide */
					lcd_put_pixel(x+j*8+7-b, y+i, 0); /* 黑 */
				}
	
			}
		}

}



/* Replace this function with something useful. */

void
draw_bitmap( FT_Bitmap*  bitmap,
             FT_Int      x,
             FT_Int      y)
{
  FT_Int  i, j, p, q;
  FT_Int  x_max = x + bitmap->width;
  FT_Int  y_max = y + bitmap->rows;

  for ( i = x, p = 0; i < x_max; i++, p++ )
  {
    for ( j = y, q = 0; j < y_max; j++, q++ )
    {
      if ( i < 0      || j < 0       ||
           i >= var.xres || j >= var.yres )
        continue;

      //image[j][i] |= bitmap->buffer[q * bitmap->width + p];
      lcd_put_pixel(i, j, bitmap->buffer[q * bitmap->width + p]);
    }
  }
}

int show_word(__u32 x, __u32 y,char *angle,wchar_t *str){
	/* 确定座标:
	 * lcd_x = var.xres/2 + 8 + 16
	 * lcd_y = var.yres/2 + 16
	 * 笛卡尔座标系:
	 * x = lcd_x = var.xres/2 + 8 + 16
	 * y = var.yres - lcd_y = var.yres/2 - 16
	 */
	pen.x = x * 64;
	pen.y = y * 64;


	aen_angle= ( 1.0 * strtoul(angle,NULL,0)/ 360 ) * 3.14159 * 2;	  /* use 25 degrees 	*/
	/* set up matrix */
	matrix.xx = (FT_Fixed)( cos( aen_angle ) * 0x10000L );
	matrix.xy = (FT_Fixed)(-sin( aen_angle ) * 0x10000L );
	matrix.yx = (FT_Fixed)( sin( aen_angle ) * 0x10000L );
	matrix.yy = (FT_Fixed)( cos( aen_angle ) * 0x10000L );

    /* set transformation */
    FT_Set_Transform( face,  &matrix, &pen);

    /* load glyph image into the slot (erase previous one) */
    error = FT_Load_Char( face, (FT_ULong)str, FT_LOAD_RENDER );
		if (error)
	{
		printf("FT_Load_Char error\n");
		return -1;
	}

    draw_bitmap( &slot->bitmap,
                 slot->bitmap_left,
                 var.yres - slot->bitmap_top);
	return 0;

}
void show_sentence(__u32 x, __u32 y,int width,int size,char *angle,wchar_t *str){
	__u32 xx=x;
	int i = 0;
	for(i = 0;i < size; i++){
		show_word(xx, y,angle,(wchar_t *)str[i]);
		xx = xx + width;
	}
	 
}
int main(int argc, char **argv)
{

   	wchar_t *my_blog = L"欢迎访问我的博客:\0";
	wchar_t *my_address = L"https://aeneag.xyz/";  



	if (argc != 3)
	{
		printf("Usage : %s <font_file> <angle>\n", argv[0]);
		return -1;
	}


	fd_fb = open("/dev/fb0", O_RDWR);
	if (fd_fb < 0)
	{
		printf("can't open /dev/fb0\n");
		return -1;
	}

	if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var))
	{
		printf("can't get var\n");
		return -1;
	}

	if (ioctl(fd_fb, FBIOGET_FSCREENINFO, &fix))
	{
		printf("can't get fix\n");
		return -1;
	}

	line_width  = var.xres * var.bits_per_pixel / 8;
	pixel_width = var.bits_per_pixel / 8;
	screen_size = var.xres * var.yres * var.bits_per_pixel / 8;
	fbmem = (unsigned char *)mmap(NULL , screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);
	if (fbmem == (unsigned char *)-1)
	{
		printf("can't mmap\n");
		return -1;
	}

	fd_hzk16 = open("HZK16", O_RDONLY);
	if (fd_hzk16 < 0)
	{
		printf("can't open HZK16\n");
		return -1;
	}
	if(fstat(fd_hzk16, &hzk_stat))
	{
		printf("can't get fstat\n");
		return -1;
	}
	hzkmem = (unsigned char *)mmap(NULL , hzk_stat.st_size, PROT_READ, MAP_SHARED, fd_hzk16, 0);
	if (hzkmem == (unsigned char *)-1)
	{
		printf("can't mmap for hzk16\n");
		return -1;
	}

	/* clear screen ,black */
	memset(fbmem, 0, screen_size);

	/* show word */
	error = FT_Init_FreeType( &library );			   /* initialize library */
	/* error handling omitted */

	error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */
	/* error handling omitted */
	slot = face->glyph;

	FT_Set_Pixel_Sizes(face, 24, 0);

	show_sentence(var.xres/2 - 96,var.yres/2 + 24,24,9,argv[2],my_blog);
	show_sentence(var.xres/2 - 108,var.yres/2 ,12,19,argv[2],my_address);
#if 0
    show_word(var.xres/2 - 96,  var.yres/2 + 24,argv[2],(wchar_t *)my_blog[0]);
	show_word(var.xres/2 - 72,  var.yres/2 + 24,argv[2],(wchar_t *)my_blog[1]);
	show_word(var.xres/2 - 48,  var.yres/2 + 24,argv[2],(wchar_t *)my_blog[2]);
	show_word(var.xres/2 - 24,  var.yres/2 + 24,argv[2],(wchar_t *)my_blog[3]);
	show_word(var.xres/2 ,      var.yres/2 + 24,argv[2],(wchar_t *)my_blog[4]);
	show_word(var.xres/2 + 24,  var.yres/2 + 24,argv[2],(wchar_t *)my_blog[5]);
	show_word(var.xres/2 + 48,  var.yres/2 + 24,argv[2],(wchar_t *)my_blog[6]);
	show_word(var.xres/2 + 72,  var.yres/2 + 24,argv[2],(wchar_t *)my_blog[7]);
	show_word(var.xres/2 + 96,  var.yres/2 + 24,argv[2],(wchar_t *)my_blog[8]);

	show_word(var.xres/2 - 108,	var.yres/2 ,argv[2],(wchar_t *)my_address[0]);
	show_word(var.xres/2 - 96,	var.yres/2 ,argv[2],(wchar_t *)my_address[1]);
	show_word(var.xres/2 - 84,	var.yres/2 ,argv[2],(wchar_t *)my_address[2]);
	show_word(var.xres/2 - 72,	var.yres/2 ,argv[2],(wchar_t *)my_address[3]);
	show_word(var.xres/2 - 60,	var.yres/2 ,argv[2],(wchar_t *)my_address[4]);
	show_word(var.xres/2 - 48,  var.yres/2 ,argv[2],(wchar_t *)my_address[5]);
	show_word(var.xres/2 - 36,  var.yres/2 ,argv[2],(wchar_t *)my_address[6]);
	show_word(var.xres/2 - 24,  var.yres/2 ,argv[2],(wchar_t *)my_address[7]);
	show_word(var.xres/2 - 12,  var.yres/2 ,argv[2],(wchar_t *)my_address[8]);
	show_word(var.xres/2 ,      var.yres/2 ,argv[2],(wchar_t *)my_address[9]);
	show_word(var.xres/2 + 12,  var.yres/2 ,argv[2],(wchar_t *)my_address[10]);
	show_word(var.xres/2 + 24,  var.yres/2 ,argv[2],(wchar_t *)my_address[11]);
	show_word(var.xres/2 + 36,  var.yres/2 ,argv[2],(wchar_t *)my_address[12]);
	show_word(var.xres/2 + 48,  var.yres/2 ,argv[2],(wchar_t *)my_address[13]);
	show_word(var.xres/2 + 60,  var.yres/2 ,argv[2],(wchar_t *)my_address[14]);
	show_word(var.xres/2 + 72,  var.yres/2 ,argv[2],(wchar_t *)my_address[15]);
	show_word(var.xres/2 + 84,  var.yres/2 ,argv[2],(wchar_t *)my_address[16]);
	show_word(var.xres/2 + 96,  var.yres/2 ,argv[2],(wchar_t *)my_address[17]);
	show_word(var.xres/2 + 108, var.yres/2 ,argv[2],(wchar_t *)my_address[18]);
#endif



	return 0;
}

代码是这些,有一个警告信息,稍后解决。

警告已解决,优化了代码,代码更新

end

回头看,为什么花了一天时间才搞了这么点东西,自己好菜😭 菜到连个对象都没有。又是流水账的一篇,hhhh

艾恩凝

写于大连

2021/7/14

补充

2021/7/15

显示多行文字,昨天是自己搞的,今天使用了官方例程

code

#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <wchar.h>

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H

typedef struct TGlyph_ { 
	FT_UInt index; /* glyph index */ 
	FT_Vector pos; /* glyph origin on the baseline */ 
	FT_Glyph image; /* glyph image */ 
} TGlyph, *PGlyph; 


#define MAX_GLYPHS  100

int fd_fb;
struct fb_var_screeninfo var;	/* Current var */
struct fb_fix_screeninfo fix;	/* Current fix */
int screen_size;
unsigned char *fbmem;
unsigned int line_width;
unsigned int pixel_width;


/* color : 0x00RRGGBB */
void lcd_put_pixel(int x, int y, unsigned int color)
{
	unsigned char *pen_8 = fbmem+y*line_width+x*pixel_width;
	unsigned short *pen_16;
	unsigned int *pen_32;

	unsigned int red, green, blue;

	pen_16 = (unsigned short *)pen_8;
	pen_32 = (unsigned int *)pen_8;

	switch (var.bits_per_pixel)
	{
		case 8:
		{
			*pen_8 = color;
			break;
		}
		case 16:
		{
			/* 565 */
			red   = (color >> 16) & 0xff;
			green = (color >> 8) & 0xff;
			blue  = (color >> 0) & 0xff;
			color = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);
			*pen_16 = color;
			break;
		}
		case 32:
		{
			*pen_32 = color;
			break;
		}
		default:
		{
			printf("can't surport %dbpp\n", var.bits_per_pixel);
			break;
		}
	}
}



/* Replace this function with something useful. */

void
draw_bitmap( FT_Bitmap*  bitmap,
             FT_Int      x,
             FT_Int      y)
{
  FT_Int  i, j, p, q;
  FT_Int  x_max = x + bitmap->width;
  FT_Int  y_max = y + bitmap->rows;

	//printf("x = %d, y = %d\n", x, y);

  for ( i = x, p = 0; i < x_max; i++, p++ )
  {
    for ( j = y, q = 0; j < y_max; j++, q++ )
    {
      if ( i < 0      || j < 0       ||
           i >= var.xres || j >= var.yres )
        continue;

      //image[j][i] |= bitmap->buffer[q * bitmap->width + p];
      lcd_put_pixel(i, j, bitmap->buffer[q * bitmap->width + p]);
    }
  }
}


int Get_Glyphs_Frm_Wstr(FT_Face face, wchar_t * wstr, TGlyph glyphs[])
{
	int n;
	PGlyph glyph = glyphs;
	int pen_x = 0;
	int pen_y = 0;
	int error;
	FT_GlyphSlot  slot = face->glyph;;


	for (n = 0; n < wcslen(wstr); n++)
	{
		glyph->index = FT_Get_Char_Index( face, wstr[n]); 
		/* store current pen position */ 
		glyph->pos.x = pen_x; 
		glyph->pos.y = pen_y;

		/* load时是把glyph放入插槽face->glyph */
		error = FT_Load_Glyph(face, glyph->index, FT_LOAD_DEFAULT);
		if ( error ) 
			continue;

		error = FT_Get_Glyph(face->glyph, &glyph->image ); 
		if ( error ) 
			continue;

		/* translate the glyph image now */ 
		/* 这使得glyph->image里含有位置信息 */
		FT_Glyph_Transform(glyph->image, 0, &glyph->pos );

		pen_x += slot->advance.x;  /* 1/64 point */

		/* increment number of glyphs */ 
		glyph++;
	}

	/* count number of glyphs loaded */ 
	return (glyph - glyphs);
}

void compute_string_bbox(TGlyph glyphs[], FT_UInt num_glyphs, FT_BBox *abbox )
{
	FT_BBox bbox; 
	int n;

	bbox.xMin = bbox.yMin = 32000; 
	bbox.xMax = bbox.yMax = -32000;

	for ( n = 0; n < num_glyphs; n++ )
	{
		FT_BBox glyph_bbox;

		FT_Glyph_Get_CBox(glyphs[n].image, FT_GLYPH_BBOX_TRUNCATE, &glyph_bbox );

		if (glyph_bbox.xMin < bbox.xMin)
			bbox.xMin = glyph_bbox.xMin;

		if (glyph_bbox.yMin < bbox.yMin)
			bbox.yMin = glyph_bbox.yMin;

		if (glyph_bbox.xMax > bbox.xMax)
			bbox.xMax = glyph_bbox.xMax;

		if (glyph_bbox.yMax > bbox.yMax)
			bbox.yMax = glyph_bbox.yMax;
	}

	*abbox = bbox;
}


void Draw_Glyphs(TGlyph glyphs[], FT_UInt num_glyphs, FT_Vector pen)
{
	int n;
	int error;

	for (n = 0; n < num_glyphs; n++)
	{
		FT_Glyph_Transform(glyphs[n].image, 0, &pen);
		/* convert glyph image to bitmap (destroy the glyph copy!) */ 
		error = FT_Glyph_To_Bitmap(&glyphs[n].image, FT_RENDER_MODE_NORMAL, 0, /* no additional translation */ 
                              		1 ); 		/* destroy copy in "image" */
		if ( !error ) 
		{ 
			FT_BitmapGlyph bit = (FT_BitmapGlyph)glyphs[n].image; 
			draw_bitmap(&bit->bitmap, bit->left, var.yres - bit->top); 
			FT_Done_Glyph(glyphs[n].image ); 
		}
	}
}

int main(int argc, char **argv)
{
	wchar_t *wstr1 = L"欢迎访问我的博客:";
	wchar_t *wstr2 = L"https://aeneag.xyz/";

	FT_Library	  library;
	FT_Face 	  face;
	int error;
    FT_Vector     pen;
	FT_GlyphSlot  slot;
	int i;
	FT_BBox bbox;

	int line_box_ymin = 10000;
	int line_box_ymax = 0;

	int line_box_width;
	int line_box_height;

	TGlyph glyphs[MAX_GLYPHS]; /* glyphs table */ 
	FT_UInt num_glyphs;


	if (argc != 2)
	{
		printf("Usage : %s <font_file>\n", argv[0]);
		return -1;
	}


	fd_fb = open("/dev/fb0", O_RDWR);
	if (fd_fb < 0)
	{
		printf("can't open /dev/fb0\n");
		return -1;
	}

	if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var))
	{
		printf("can't get var\n");
		return -1;
	}

	if (ioctl(fd_fb, FBIOGET_FSCREENINFO, &fix))
	{
		printf("can't get fix\n");
		return -1;
	}

	line_width  = var.xres * var.bits_per_pixel / 8;
	pixel_width = var.bits_per_pixel / 8;
	screen_size = var.xres * var.yres * var.bits_per_pixel / 8;
	fbmem = (unsigned char *)mmap(NULL , screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);
	if (fbmem == (unsigned char *)-1)
	{
		printf("can't mmap\n");
		return -1;
	}

	/* 清屏: 全部设为黑色 */
	memset(fbmem, 0, screen_size);

	/* 显示矢量字体 */
	error = FT_Init_FreeType( &library );			   /* initialize library */
	/* error handling omitted */

	error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */
	/* error handling omitted */
	slot = face->glyph;

	FT_Set_Pixel_Sizes(face, 24, 0);

	/* wstr1 */
	num_glyphs = Get_Glyphs_Frm_Wstr(face, wstr1, glyphs);

	compute_string_bbox(glyphs, num_glyphs, &bbox);
	line_box_width  = bbox.xMax - bbox.xMin;
	line_box_height = bbox.yMax - bbox.yMin;

	pen.x = (var.xres - line_box_width)/2 * 64;
	pen.y = (var.yres - line_box_height)/2 * 64;

	Draw_Glyphs(glyphs, num_glyphs, pen);

	/* wstr2 */
	num_glyphs = Get_Glyphs_Frm_Wstr(face, wstr2, glyphs);

	compute_string_bbox(glyphs, num_glyphs, &bbox);
	line_box_width  = bbox.xMax - bbox.xMin;
	line_box_height = bbox.yMax - bbox.yMin;

	pen.x = (var.xres - line_box_width)/2 * 64;
	pen.y = pen.y - 24 * 64;
	Draw_Glyphs(glyphs, num_glyphs, pen);


	return 0;
}

end

艾恩凝

更新写法

艾恩凝

写于大连

2021/7/15

catalogue

[Linux]数码相框&电子书系列


    


公众号'艾恩凝'
个人公众号
个人微信
个人微信
    吾心信其可行,
          则移山填海之难,
                  终有成功之日!
                                  ——孙文
    评论
    0 评论
avatar

取消