凌云实验室推出的ARM Linux物联网网关开发板IGKBoard(IoT Gateway Kit Board)项目源码
guowenxue
2021-12-26 9512f12282ff6bab745711a1e3eec35a9735e6dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
 
/*********************************************************************************
 *      Copyright:  (C) 2021 Guo Wenxue<Email:guowenxue@gmail.com QQ:281143292>
 *                  All rights reserved.
 *
 *       Filename:  test_key.c
 *    Description:  This file used to test GPIO button driver builtin Linux kernel on ARM board
 *                 
 *        Version:  1.0.0(11/17/2021~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "11/17/2021 02:46:18 PM"
 *                 
 *     Linux Clear LCD screen command : 
 *
 *                  dd if=/dev/zero of=/dev/fb0 bs=320 count=720  [ 240*(24bpp/8)=720 ] 
 *
 ********************************************************************************/
 
#include <unistd.h>  
#include <stdio.h>  
#include <fcntl.h>  
#include <linux/fb.h>  
#include <sys/mman.h>  
#include <stdlib.h>  
#include <string.h>
#include <sys/ioctl.h>
 
 
#define CONFIG_RGB565
 
#ifdef  CONFIG_RGB565  /* RRRR RGGG GGGB BBBB  */
#define RED       0xF800
#define GREEN     0x07E0
#define BLUE      0x001F
#define YELLOW    0xFFE0
#define WHITE     0xFFFF 
#endif
 
 
void fill_color16(short *fb_addr, short bit_map, int psize)
{
    int i;
    for(i=0; i<psize; i++) {
        *fb_addr = bit_map;
        fb_addr++;
    }
}
 
int main ()   
{  
    int fp=0;  
    struct fb_var_screeninfo vinfo;  
    struct fb_fix_screeninfo finfo;  
    long screensize=0;  
    char *fbp = NULL, *test_fbp=NULL;    
    int x = 0, y = 0;  
    long location = 0;
    int i;
    int num = 5;
    int pix_size=0;
 
    fp = open("/dev/fb0", O_RDWR);  
 
    if(fp < 0) {  
        printf("Error : Can not open framebuffer device/n");  
        exit(1);  
    }  
 
    if(ioctl(fp, FBIOGET_FSCREENINFO, &finfo)){  
        printf("Error reading fixed information/n");  
        exit(2);  
    }  
 
    if(ioctl(fp, FBIOGET_VSCREENINFO, &vinfo)){  
        printf("Error reading variable information/n");  
        exit(3);  
    }  
 
    screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;  
 
    printf("The phy mem = 0x%x, total size = %d(byte)\n", finfo.smem_start, finfo.smem_len);  
    printf("xres =  %d, yres =  %d, bits_per_pixel = %d\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);  
    printf("So the screensize = %d(byte), using %d frame\n", screensize, finfo.smem_len/screensize);
    printf("vinfo.xoffset = %d, vinfo.yoffset = %d\n", vinfo.xoffset, vinfo.yoffset);  
    printf("vinfo.vmode is :%d\n", vinfo.vmode);  
    printf("finfo.ypanstep is :%d\n", finfo.ypanstep);  
    printf("vinfo.red.offset=0x%x\n", vinfo.red.offset);
    printf("vinfo.red.length=0x%x\n", vinfo.red.length);
    printf("vinfo.green.offset=0x%x\n", vinfo.green.offset);
    printf("vinfo.green.length=0x%x\n", vinfo.green.length);
    printf("vinfo.blue.offset=0x%x\n", vinfo.blue.offset);
    printf("vinfo.blue.length=0x%x\n", vinfo.blue.length);
    printf("vinfo.transp.offset=0x%x\n", vinfo.transp.offset);
    printf("vinfo.transp.length=0x%x\n", vinfo.transp.length);
    
 
    fbp =(char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fp,0);  
    if ((int)fbp == -1)  
    {    
        printf ("Error: failed to map framebuffer device to memory./n");  
        exit (4);  
    }
    printf("Get virt mem = %p\n", fbp);  
 
 
    pix_size = vinfo.xres * vinfo.yres;
    /* using first frame, for FBIOPAN_DISPLAY
     * 当刷新需要调用FBIOPAN_DISPLAY, 要告知驱动刷哪块帧, 用到下面两个参数
     * 如果使用第二帧buffer -> vinfo.xoffset = 0; vinfo.yoffset = vinfo.yres;
     */
    vinfo.xoffset = 0;
    vinfo.yoffset = 0;
 
    /* show color loop */
    while(num--) {
        printf("\ndrawing red ...\n");
        fill_color16((short *)fbp, RED, pix_size);
        //ioctl(fp, FBIOPAN_DISPLAY, &vinfo);
        sleep(3);
 
        printf("\ndrawing green ...\n");
        fill_color16((short *)fbp, GREEN, pix_size);
        //ioctl(fp, FBIOPAN_DISPLAY, &vinfo);
        sleep(3);
 
        printf("\ndrawing blue ...\n");
        fill_color16((short *)fbp, BLUE, pix_size);
        //ioctl(fp, FBIOPAN_DISPLAY, &vinfo);
        sleep(3);
        
        printf("\ndrawing yellow ...\n");
        fill_color16((short *)fbp, YELLOW, pix_size);
        //ioctl(fp, FBIOPAN_DISPLAY, &vinfo);
        sleep(3);
 
    }
#if 1
    /*这是你想画的点的位置坐标,(0,0)点在屏幕左上角*/    
    x = 10;  
    y = 10;  
    location = x * (vinfo.bits_per_pixel / 8) + y  *  finfo.line_length;  
    test_fbp = fbp + location;
    printf("draw line.......\n");
    for(i = 0; i < (vinfo.xres - x); i++)
        *test_fbp++ = i+30;
 
    //ioctl(fp, FBIOPAN_DISPLAY, &vinfo);
#endif
 
    munmap(fbp, screensize); /*解除映射*/  
 
    close (fp);
    return 0;
}