fb-dash/fbdash.c

192 lines
4.9 KiB
C

/*
* fbdash.c
*
* Adapted by Florian Klemenz for use in the fb_dash project
*
* Source and all credit goes to
* http://raspberrycompote.blogspot.com/
* --------------------------------------------------------------------------------------------
*
* http://raspberrycompote.blogspot.ie/2014/03/low-level-graphics-on-raspberry-pi-part_14.html
*
* Original work by J-P Rosti (a.k.a -rst- and 'Raspberry Compote')
*
* Licensed under the Creative Commons Attribution 3.0 Unported License
* (http://creativecommons.org/licenses/by/3.0/deed.en_US)
*
* Distributed in the hope that this will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
#include "fblib.h"
#include "fbfonts.h"
#include <stdio.h>
#include <math.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;
int y_step_width = HEIGHT / 4;
for (int i = 1; i < 4; i++) {
int x0 = 15;
int x1 = WIDTH - x0;
int y = y_step_width * i;
int r = 225;
int g = 32;
int b = 32;
draw_line(fbp, x0, y-1, x1, y-1, r, g, b);
draw_line(fbp, x0, y , x1, y , r, g, b);
draw_line(fbp, x0, y+1, x1, y+1, r, g, b);
}
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);
}
// 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
fbfd = open("/dev/fb1", O_RDWR);
if (fbfd == -1) {
printf("Error: cannot open framebuffer device.\n");
return(1);
}
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,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fbfd,
0);
if ((int)fbp == -1) {
printf("Failed to mmap.\n");
}
else {
// draw...
draw();
}
// cleanup
//clear_screen(fbp);
// unmap fb file from memory
munmap(fbp, screensize);
// close fb file
close(fbfd);
return 0;
}