当前位置:   article > 正文

gec6818开发板显示时间(显示字体)【gec6818系列】_gec6818怎么获取实时时间

gec6818怎么获取实时时间

目录

1.准备

效果如下

2.显示字体头文件

3.显示时间代码

4.编译命令

5.资料


1.准备

1.显示字体头文件.h

2.静态库.a

3.显示时间.c

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

效果如下

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

2.显示字体头文件

  1. #ifndef __font_h__
  2. #define __font_h__
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <stdlib.h>
  8. #include <sys/mman.h>
  9. #define color u32
  10. #define getColor(a, b, c, d) (a|b<<8|c<<16|d<<24)
  11. typedef unsigned char u8;
  12. typedef unsigned short u16;
  13. typedef unsigned int u32;
  14. typedef unsigned long long u64;
  15. typedef char s8;
  16. typedef short s16;
  17. typedef int s32;
  18. typedef long long s64;
  19. typedef struct stbtt_fontinfo
  20. {
  21. void * userdata;
  22. unsigned char * data; // pointer to .ttf file
  23. int fontstart; // offset of start of font
  24. int numGlyphs; // number of glyphs, needed for range checking
  25. int loca,head,glyf,hhea,hmtx,kern; // table locations as offset from start of .ttf
  26. int index_map; // a cmap mapping for our chosen character encoding
  27. int indexToLocFormat; // format needed to map from glyph index to glyph
  28. } stbtt_fontinfo;
  29. typedef struct{
  30. u32 height;
  31. u32 width;
  32. u32 byteperpixel;
  33. u8 *map;
  34. }bitmap;
  35. typedef struct{
  36. stbtt_fontinfo *info;
  37. u8 *buffer;
  38. float scale;
  39. }font;
  40. //lcd设备结构体
  41. struct LcdDevice
  42. {
  43. int fd;
  44. unsigned int *mp; //保存映射首地址
  45. };
  46. //1.初始化字库
  47. font *fontLoad(char *fontPath);
  48. //2.设置字体的大小
  49. void fontSetSize(font *f, s32 pixels);
  50. //3.设置字体输出框的大小
  51. bitmap *createBitmap(u32 width, u32 height, u32 byteperpixel);
  52. //可以指定输出框的颜色
  53. bitmap *createBitmapWithInit(u32 width, u32 height, u32 byteperpixel, color c);
  54. //4.把字体输出到输出框中
  55. void fontPrint(font *f, bitmap *screen, s32 x, s32 y, char *text, color c, s32 maxWidth);
  56. //5.把输出框的所有信息显示到LCD屏幕中
  57. void show_font_to_lcd(unsigned int *p,int px,int py,bitmap *bm);
  58. // 关闭字体库
  59. void fontUnload(font *f);
  60. // 关闭bitmap
  61. void destroyBitmap(bitmap *bm);
  62. #endif

3.显示时间代码

  1. #include "../lib/font.h"
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <linux/input.h>
  5. #include <time.h>
  6. #include <pthread.h>
  7. #include <string.h>
  8. #define LCD_DEV_PATH "/dev/fb0"
  9. #define FONT_PATH "/usr/share/fonts/DroidSansFallback.ttf"
  10. #define LCD_DEV_TOUCH_PATH "/dev/input/event0"
  11. #define LCD_W 800
  12. #define LCD_H 480
  13. void show_font(struct LcdDevice *lcd, char *buf_time)
  14. {
  15. // 打开字体
  16. font *f = fontLoad(FONT_PATH);
  17. // 字体大小的设置,30表示字体大小,数字越大字体越大
  18. fontSetSize(f, 30);
  19. // 创建一个画板(点阵图)宽是200个像素,高是50个像素,getColor后面三个十进制数表示颜色BGR
  20. bitmap *bm_time = createBitmapWithInit(220, 50, 4, getColor(0, 0, 0, 255));
  21. // 将字体写到点阵图上 0,0表示汉字在画板的左上角显示
  22. fontPrint(f, bm_time, 0, 0, buf_time, getColor(0, 255, 255, 255), 0);
  23. // 把字体框输出到LCD屏幕上 参数0,0表示画板显示的坐标位置
  24. show_font_to_lcd(lcd->mp, 0, 0, bm_time);
  25. // 关闭字体,关闭画板
  26. fontUnload(f);
  27. destroyBitmap(bm_time);
  28. }
  29. // 初始化Lcd
  30. struct LcdDevice *init_lcd(const char *device)
  31. {
  32. // 申请空间
  33. struct LcdDevice *lcd = malloc(sizeof(struct LcdDevice));
  34. if (lcd == NULL)
  35. {
  36. return NULL;
  37. }
  38. // 1打开设备
  39. lcd->fd = open(device, O_RDWR);
  40. if (lcd->fd < 0)
  41. {
  42. perror("open lcd fail");
  43. free(lcd);
  44. return NULL;
  45. }
  46. // 映射
  47. lcd->mp = mmap(NULL, LCD_H * LCD_W * 4, PROT_READ | PROT_WRITE, MAP_SHARED, lcd->fd, 0);
  48. return lcd;
  49. }
  50. // 获取时间
  51. char *get_time()
  52. {
  53. time_t rawtime;
  54. struct tm *timeinfo;
  55. char *buffer = (char *)malloc(sizeof(char) * 80);
  56. time(&rawtime);
  57. timeinfo = localtime(&rawtime);
  58. // 格式化时间到 buffer 字符串
  59. snprintf(buffer, 80, "%04d-%02d-%02d %02d:%02d:%02d",
  60. timeinfo->tm_year + 1900, // 年份是从1900年开始计算的,所以要加1900
  61. timeinfo->tm_mon + 1, // 月份是从0开始的,所以要加1
  62. timeinfo->tm_mday, // 日期
  63. timeinfo->tm_hour, // 小时
  64. timeinfo->tm_min, // 分钟
  65. timeinfo->tm_sec); // 秒
  66. return buffer;
  67. }
  68. int main()
  69. {
  70. // 初始化Lcd
  71. struct LcdDevice *lcd = init_lcd(LCD_DEV_PATH);
  72. if (lcd == (struct LcdDevice *)NULL)
  73. {
  74. printf("初始化LCD失败!\n");
  75. return -1;
  76. }
  77. while (1)
  78. {
  79. char *buf_time = get_time();
  80. show_font(lcd, buf_time);
  81. }
  82. return 0;
  83. }

4.编译命令

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

5.资料

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/749998
推荐阅读
相关标签
  

闽ICP备14008679号