cleaned up modules, dependencies, includes and added font type
This commit is contained in:
parent
e0d02d3529
commit
9705273d18
9
Makefile
9
Makefile
@ -1,10 +1,13 @@
|
||||
CFLAGS=-g -Wall -lm
|
||||
|
||||
fbdash: fbdash.c fblib
|
||||
gcc fblib.o -o $@ $(CFLAGS) $<
|
||||
fbdash: fbdash.c fblib.o fbfont.o
|
||||
gcc fblib.o fbfont.o -o $@ $(CFLAGS) $<
|
||||
|
||||
fblib: fblib.o
|
||||
gcc -c fblib.c
|
||||
|
||||
fbfont: fbfont.o
|
||||
gcc -c fbfont.c
|
||||
|
||||
clean:
|
||||
rm fbdash fblib.o
|
||||
rm -f fbdash fblib.o fbfont.o
|
||||
|
||||
106
fbdash.c
106
fbdash.c
@ -21,87 +21,18 @@
|
||||
*/
|
||||
|
||||
|
||||
#include "fblib.h"
|
||||
#include "fbfonts.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "fblib.h"
|
||||
|
||||
#define WIDTH 240
|
||||
#define HEIGHT 320
|
||||
|
||||
#define FONTH 8 // 37
|
||||
#define FONTW 8 // 22
|
||||
|
||||
|
||||
// 'global' variables to store screen info
|
||||
char *fbp = 0;
|
||||
|
||||
/*
|
||||
struct fb_var_screeninfo vinfo;
|
||||
struct fb_fix_screeninfo finfo;
|
||||
*/
|
||||
|
||||
|
||||
|
||||
void render_string(char *fbp, char* font, char *text, int x, int y, int r, int g, int b)
|
||||
{
|
||||
int text_length = strlen(text);
|
||||
printf("text_length = %d\n", text_length);
|
||||
|
||||
int bytes_per_line = ceil(FONTW / 8.0);
|
||||
printf("bytes_per_line = %d\n", bytes_per_line);
|
||||
|
||||
int char_offset = bytes_per_line * FONTH;
|
||||
|
||||
// each character
|
||||
for (int char_pos = 0; char_pos < text_length; char_pos++) {
|
||||
//int char_code = 0;
|
||||
int char_code = *(text + char_pos);
|
||||
int x_offset = char_pos * FONTW + x;
|
||||
|
||||
// each line of pixels
|
||||
for (int line = 0; line < FONTH; line++) {
|
||||
int y_pos = line + y;
|
||||
|
||||
// each byte in a line of pixels
|
||||
for (int byte_num = 0; byte_num < bytes_per_line; byte_num++) {
|
||||
int byte_offset = line * bytes_per_line + byte_num;
|
||||
char current_byte = *((font + char_code * char_offset) + byte_offset);
|
||||
|
||||
// each bit/pixel in a byte
|
||||
for (int bit_number = 0; bit_number < 8; bit_number++) {
|
||||
char mask = 1 << bit_number;
|
||||
if(current_byte & mask) {
|
||||
// plot the pixel
|
||||
printf("x");
|
||||
|
||||
int x_pos = x_offset + byte_num * 8 + bit_number;
|
||||
put_pixel(fbp, x_pos, y_pos, 255, 255, 255);
|
||||
}
|
||||
else {
|
||||
// leave empty (or maybe plot 'text backgr color')
|
||||
printf(" ");
|
||||
}
|
||||
} // end "for bit_number"
|
||||
|
||||
} // end "for byte_num"
|
||||
|
||||
printf("\n");
|
||||
|
||||
} // end "for line"
|
||||
|
||||
} // end "for char_pos"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// helper function for drawing - no more need to go mess with
|
||||
// the main function when just want to change what to draw...
|
||||
void draw() {
|
||||
|
||||
clear_screen(fbp);
|
||||
|
||||
// horizontal spacersint x_space = 10;
|
||||
@ -123,17 +54,15 @@ void draw() {
|
||||
|
||||
|
||||
|
||||
char *text = "Hello world!";
|
||||
//render_string(fbp, (char*)arial_16x16, text, 0, 0, 255, 255, 255);
|
||||
render_string(fbp, (char*)font8x8_basic, text, 0, 0, 255, 255, 255);
|
||||
char *text = "Hello!";
|
||||
//render_string(fbp, arial_32, text, 0, 0, 255, 255, 255);
|
||||
render_string(fbp, basic_8, text, 0, 0, 255, 255, 255);
|
||||
}
|
||||
|
||||
// application entry point
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
|
||||
int fbfd = 0;
|
||||
//struct fb_var_screeninfo orig_vinfo;
|
||||
long int screensize = 0;
|
||||
|
||||
// Open the framebuffer file for reading and writing
|
||||
@ -144,25 +73,7 @@ int main(int argc, char* argv[])
|
||||
}
|
||||
printf("The framebuffer device was opened successfully.\n");
|
||||
|
||||
|
||||
/*
|
||||
// Get variable screen information
|
||||
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
|
||||
printf("Error reading variable information.\n");
|
||||
}
|
||||
printf("Variable info: %dx%d, %dbpp\n",
|
||||
vinfo.xres, vinfo.yres,
|
||||
vinfo.bits_per_pixel );
|
||||
|
||||
// Get fixed screen information
|
||||
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
|
||||
printf("Error reading fixed information.\n");
|
||||
}
|
||||
printf("Fixed info: smem_len %d, line_length %d\n", finfo.smem_len, finfo.line_length);
|
||||
*/
|
||||
|
||||
// map fb to user mem
|
||||
//screensize = finfo.smem_len;
|
||||
screensize = WIDTH*HEIGHT*2;
|
||||
fbp = (char*)mmap(0,
|
||||
screensize,
|
||||
@ -181,11 +92,8 @@ int main(int argc, char* argv[])
|
||||
|
||||
// cleanup
|
||||
//clear_screen(fbp);
|
||||
// unmap fb file from memory
|
||||
munmap(fbp, screensize);
|
||||
// close fb file
|
||||
close(fbfd);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
10
fbfont.c
Normal file
10
fbfont.c
Normal file
@ -0,0 +1,10 @@
|
||||
|
||||
#include "fbfont.h"
|
||||
|
||||
#include "fonts/dhepper-font8x8/font8x8_basic.h"
|
||||
#include "fonts/Arial-32px.h"
|
||||
|
||||
|
||||
fbfont arial_32 = { 22, 37, (char*)xbm_arial_32 };
|
||||
fbfont basic_8 = { 8, 8, (char*)font8x8_basic };
|
||||
|
||||
16
fbfont.h
Normal file
16
fbfont.h
Normal file
@ -0,0 +1,16 @@
|
||||
|
||||
#ifndef FBFONTS_H
|
||||
#define FBFONTS_H
|
||||
|
||||
struct s_fbfont {
|
||||
unsigned char width;
|
||||
unsigned char height;
|
||||
char *xbm;
|
||||
};
|
||||
|
||||
typedef struct s_fbfont fbfont;
|
||||
|
||||
extern fbfont arial_32;
|
||||
extern fbfont basic_8;
|
||||
|
||||
#endif
|
||||
32
fbfonts.h
32
fbfonts.h
@ -1,32 +0,0 @@
|
||||
/**
|
||||
* fbdash.c
|
||||
*
|
||||
* Adapted by Florian Klemenz for use in the fb_dash project
|
||||
*
|
||||
* Source and all credit goes to
|
||||
* https://github.com/dhepper/font8x8
|
||||
* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
* 8x8 monochrome bitmap fonts for rendering
|
||||
* Author: Daniel Hepper <daniel@hepper.net>
|
||||
*
|
||||
* License: Public Domain
|
||||
*
|
||||
* Based on:
|
||||
* // Summary: font8x8.h
|
||||
* // 8x8 monochrome bitmap fonts for rendering
|
||||
* //
|
||||
* // Author:
|
||||
* // Marcel Sondaar
|
||||
* // International Business Machines (public domain VGA fonts)
|
||||
* //
|
||||
* // License:
|
||||
* // Public Domain
|
||||
*
|
||||
* Fetched from: http://dimensionalrift.homelinux.net/combuster/mos3/?p=viewsource&file=/modules/gfx/font8_8.asm
|
||||
**/
|
||||
|
||||
|
||||
#include "fonts/dhepper-font8x8/font8x8_basic.h"
|
||||
//#include "fonts/vento-xbm-fonts/ascii_16x26.h"
|
||||
#include "fonts/arial-16x16.h"
|
||||
51
fblib.c
51
fblib.c
@ -19,7 +19,6 @@
|
||||
|
||||
#include "fblib.h"
|
||||
|
||||
|
||||
// helper function to 'plot' a pixel in given color
|
||||
void put_pixel(char *fbp, int x, int y, int r, int g, int b)
|
||||
{
|
||||
@ -166,3 +165,53 @@ void draw_line(char *fbp, int x0, int y0, int x1, int y1, int r, int g, int b) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void render_string(char *fbp, fbfont font, char *text, int x, int y, int r, int g, int b)
|
||||
{
|
||||
int text_length = strlen(text);
|
||||
printf("text_length = %d\n", text_length);
|
||||
|
||||
int bytes_per_line = ceil(font.width / 8.0);
|
||||
printf("bytes_per_line = %d\n", bytes_per_line);
|
||||
|
||||
int char_offset = bytes_per_line * font.height;
|
||||
|
||||
// each character
|
||||
for (int char_pos = 0; char_pos < text_length; char_pos++) {
|
||||
//int char_code = 0;
|
||||
int char_code = *(text + char_pos);
|
||||
int x_offset = char_pos * font.width + x;
|
||||
|
||||
// each line of pixels
|
||||
for (int line = 0; line < font.height; line++) {
|
||||
int y_pos = line + y;
|
||||
|
||||
// each byte in a line of pixels
|
||||
for (int byte_num = 0; byte_num < bytes_per_line; byte_num++) {
|
||||
int byte_offset = line * bytes_per_line + byte_num;
|
||||
char current_byte = *((font.xbm + char_code * char_offset) + byte_offset);
|
||||
|
||||
// each bit/pixel in a byte
|
||||
for (int bit_number = 0; bit_number < 8; bit_number++) {
|
||||
char mask = 1 << bit_number;
|
||||
if(current_byte & mask) {
|
||||
// plot the pixel
|
||||
printf("x");
|
||||
|
||||
int x_pos = x_offset + byte_num * 8 + bit_number;
|
||||
put_pixel(fbp, x_pos, y_pos, 255, 255, 255);
|
||||
}
|
||||
else {
|
||||
// leave empty (or maybe plot 'text backgr color')
|
||||
printf(" ");
|
||||
}
|
||||
} // end "for bit_number"
|
||||
|
||||
} // end "for byte_num"
|
||||
|
||||
printf("\n");
|
||||
|
||||
} // end "for line"
|
||||
|
||||
} // end "for char_pos"
|
||||
}
|
||||
|
||||
10
fblib.h
10
fblib.h
@ -23,17 +23,15 @@
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/fb.h>
|
||||
#include <sys/mman.h>
|
||||
#include <linux/kd.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "fbfont.h"
|
||||
|
||||
#define WIDTH 240
|
||||
#define HEIGHT 320
|
||||
|
||||
|
||||
void put_pixel(char *fbp, int x, int y, int r, int g, int b);
|
||||
void clear_screen(char *fbp);
|
||||
|
||||
@ -45,4 +43,6 @@ void fill_rect(char *fbp, int x, int y, int w, int h, int r, int g, int b);
|
||||
void draw_circle(char *fbp, int x0, int y0, int radius, int r, int g, int b);
|
||||
void fill_circle(char *fbp, int x0, int y0, int radius, int r, int g, int b);
|
||||
|
||||
void render_string(char *fbp, fbfont font, char *text, int x, int y, int r, int g, int b);
|
||||
|
||||
#endif
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#define A_width 22
|
||||
#define A_height 37
|
||||
|
||||
static char arial_32[1][111] = {
|
||||
static char xbm_arial_32[1][111] = {
|
||||
{
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1C, 0x00, 0x00, 0x3E, 0x00,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user