gec6818开发板显示时间(显示字体)【gec6818系列】

目录

1.准备

效果如下

2.显示字体头文件

3.显示时间代码

4.编译命令

5.资料


1.准备

1.显示字体头文件.h

2.静态库.a

3.显示时间.c

(源码获取请看5.资料)

效果如下

注意:获取时间是当前系统的时间,并不是通过网络获取时间,所以需要自己进行微调

2.显示字体头文件

#ifndef __font_h__
#define __font_h__

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
#define color u32
#define getColor(a, b, c, d) (a|b<<8|c<<16|d<<24)

typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;

typedef char s8;
typedef short s16;
typedef int s32;
typedef long long s64;

typedef struct stbtt_fontinfo
{
   void           * userdata;
   unsigned char  * data;              // pointer to .ttf file
   int              fontstart;         // offset of start of font

   int numGlyphs;                     // number of glyphs, needed for range checking

   int loca,head,glyf,hhea,hmtx,kern; // table locations as offset from start of .ttf
   int index_map;                     // a cmap mapping for our chosen character encoding
   int indexToLocFormat;              // format needed to map from glyph index to glyph
} stbtt_fontinfo;


typedef struct{
	u32 height;
	u32 width;
	u32 byteperpixel;
	u8 *map;
}bitmap;

typedef struct{
	stbtt_fontinfo *info;
	u8 *buffer;
	float scale;
}font;


//lcd设备结构体
struct LcdDevice
{
	int fd;
	unsigned int *mp; //保存映射首地址

};


//1.初始化字库 
font *fontLoad(char *fontPath);

//2.设置字体的大小 
void fontSetSize(font *f, s32 pixels);

//3.设置字体输出框的大小
bitmap *createBitmap(u32 width, u32 height, u32 byteperpixel);

//可以指定输出框的颜色
bitmap *createBitmapWithInit(u32 width, u32 height, u32 byteperpixel, color c);


//4.把字体输出到输出框中
void fontPrint(font *f, bitmap *screen, s32 x, s32 y, char *text, color c, s32 maxWidth);

//5.把输出框的所有信息显示到LCD屏幕中 
void show_font_to_lcd(unsigned int *p,int px,int py,bitmap *bm);

// 关闭字体库
void fontUnload(font *f);

// 关闭bitmap
void destroyBitmap(bitmap *bm);

#endif

3.显示时间代码

#include "../lib/font.h"
#include <unistd.h>
#include <stdlib.h>
#include <linux/input.h>
#include <time.h>
#include <pthread.h>
#include <string.h>

#define LCD_DEV_PATH "/dev/fb0"
#define FONT_PATH "/usr/share/fonts/DroidSansFallback.ttf"
#define LCD_DEV_TOUCH_PATH "/dev/input/event0"
#define LCD_W 800
#define LCD_H 480

void show_font(struct LcdDevice *lcd, char *buf_time)
{
    // 打开字体
    font *f = fontLoad(FONT_PATH);

    // 字体大小的设置,30表示字体大小,数字越大字体越大
    fontSetSize(f, 30);

    // 创建一个画板(点阵图)宽是200个像素,高是50个像素,getColor后面三个十进制数表示颜色BGR
    bitmap *bm_time = createBitmapWithInit(220, 50, 4, getColor(0, 0, 0, 255)); 

    // 将字体写到点阵图上 0,0表示汉字在画板的左上角显示
    fontPrint(f, bm_time, 0, 0, buf_time, getColor(0, 255, 255, 255), 0);

    // 把字体框输出到LCD屏幕上  参数0,0表示画板显示的坐标位置
    show_font_to_lcd(lcd->mp, 0, 0, bm_time);

    // 关闭字体,关闭画板
    fontUnload(f);
    destroyBitmap(bm_time);
}

// 初始化Lcd
struct LcdDevice *init_lcd(const char *device)
{
    // 申请空间
    struct LcdDevice *lcd = malloc(sizeof(struct LcdDevice));
    if (lcd == NULL)
    {
        return NULL;
    }

    // 1打开设备
    lcd->fd = open(device, O_RDWR);
    if (lcd->fd < 0)
    {
        perror("open lcd fail");
        free(lcd);
        return NULL;
    }
    // 映射
    lcd->mp = mmap(NULL, LCD_H * LCD_W * 4, PROT_READ | PROT_WRITE, MAP_SHARED, lcd->fd, 0);
    return lcd;
}

// 获取时间
char *get_time()
{
    time_t rawtime;
    struct tm *timeinfo;
    char *buffer = (char *)malloc(sizeof(char) * 80);
    time(&rawtime);
    timeinfo = localtime(&rawtime);

    // 格式化时间到 buffer 字符串
    snprintf(buffer, 80, "%04d-%02d-%02d %02d:%02d:%02d",
             timeinfo->tm_year + 1900, // 年份是从1900年开始计算的,所以要加1900
             timeinfo->tm_mon + 1,     // 月份是从0开始的,所以要加1
             timeinfo->tm_mday,        // 日期
             timeinfo->tm_hour,        // 小时
             timeinfo->tm_min,         // 分钟
             timeinfo->tm_sec);        // 秒
    
    return buffer;
}

int main()
{
    // 初始化Lcd
    struct LcdDevice *lcd = init_lcd(LCD_DEV_PATH);
    if (lcd == (struct LcdDevice *)NULL)
    {
        printf("初始化LCD失败!\n");
        return -1;
    }
    while (1)
    {
        char *buf_time = get_time();
        show_font(lcd, buf_time);
    }

    return 0;
}

4.编译命令

arm-linux-gcc  test.c  libfont.a  -o test   -lm

5.资料

链接:https://pan.baidu.com/s/1BXn7Ui1gFfGHdjrOpxiBIg 
提取码:b4fz

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
要在GEC6818开发板显示一个字符,您可以使用开发板上的显示屏来实现。以下是使用Linux Framebuffer在GEC6818开发板显示一个字符的示例代码: ```c #include <fcntl.h> #include <stdio.h> #include <sys/ioctl.h> #include <linux/fb.h> #include <unistd.h> #define FRAMEBUFFER_DEV "/dev/fb0" int main() { int fbfd = open(FRAMEBUFFER_DEV, O_RDWR); if (fbfd == -1) { printf("无法打开Framebuffer设备\n"); return 1; } struct fb_var_screeninfo vinfo; if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) { printf("无法获取屏幕信息\n"); close(fbfd); return 1; } int screen_width = vinfo.xres; int screen_height = vinfo.yres; unsigned int bytes_per_pixel = vinfo.bits_per_pixel / 8; unsigned int framebuffer_size = screen_width * screen_height * bytes_per_pixel; char character = 'A'; // 将字符替换为您想要显示的字符 char* framebuffer = (char*)mmap(0, framebuffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0); if (framebuffer == MAP_FAILED) { printf("内存映射失败\n"); close(fbfd); return 1; } // 设置字符的坐标和颜色 int x = screen_width / 2; int y = screen_height / 2; unsigned int red = 255; unsigned int green = 255; unsigned int blue = 255; // 计算字符在Framebuffer中的偏移量 unsigned int offset = (x + y * screen_width) * bytes_per_pixel; // 将字符写入Framebuffer framebuffer[offset] = blue; framebuffer[offset + 1] = green; framebuffer[offset + 2] = red; framebuffer[offset + 3] = character; munmap(framebuffer, framebuffer_size); close(fbfd); return 0; } ``` 请将代码中的字符变量替换为您想要显示的字符。此代码将字符显示在屏幕的中心位置,并使用白色作为字符的颜色。要编译和运行此代码,您需要将其保存为一个源文件(例如`display_char.c`),然后在GEC6818开发板上使用适当的编译器进行编译和链接,如: ``` $ gcc display_char.c -o display_char $ ./display_char ``` 注意:在运行此代码之前,请确保您具有足够的权限来访问Framebuffer设备(通常需要root权限)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aogu181

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值