显示一个电子时钟(假的)

  • 主程序(就一个c文件)
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <sys/types.h>
#include <math.h>
#include <stdlib.h>
#include</usr/include/time.h>

static void *plcd;//全局变量,用来保存映射地址的首地址
static unsigned int lcd_width=0;//保存屏幕宽度
static unsigned int lcd_high=0;//保存屏幕高度
static unsigned int bits_per_pixel=0;//保存屏幕颜色深度
static unsigned int const RED=0x00ff0000;
static unsigned int const BACKGROUNG=0xffffffff;
struct point{
    int m_x;
    int m_y;
};

void LCD_drawpoint(unsigned int x,unsigned int y,int color);
void LCD_display(unsigned int color);
int LCD_init(void);
void showClock(void);
void drow_line(struct point cen,double radius,double angle,unsigned int color);
int main(){
    LCD_init();
    //unsigned int i=0;

    printf("lcd_width:%d\n",lcd_width);
    printf("lcd_high:%d\n",lcd_high);
    printf("bits_per_pixel:%d\n",bits_per_pixel);

    showClock();

    return 0;
}
/**
 * @function:在开发板上显示一个时钟
 * 
 */
void showClock(void){
    struct point circle_centre;
    double radius=lcd_high/4;
    double angle=0;
    struct tm *t;
    time_t tt;
    int hour;
    int min;
    int sec;
    double h,m,s;
    circle_centre.m_x=lcd_width/2;
    circle_centre.m_y=lcd_high/2;

    LCD_display(BACKGROUNG);

    struct point p;
    //printf("circle_centre:x%d,y%d\n",circle_centre.m_x,circle_centre.m_y);
    for(angle=0;angle<=6.28;angle+=(6.28/60.0)){//先画一个圆
        p.m_x=circle_centre.m_x+(int)(cos(angle)*radius);
        p.m_y=circle_centre.m_y+(int)(sin(angle)*radius);
        LCD_drawpoint(p.m_x,p.m_y,RED);
    }

    time(&tt);
    t=localtime(&tt);
    hour=t->tm_hour;
    min=t->tm_min;
    sec=t->tm_sec;
 8(0)

    h=(double)hour/12.0*6.28;
    m=(double)min/60.0*6.28;
    s=(double)sec/60.0*6.28;
    while(1){
        // t=localtime(&tt);
        // hour=t->tm_hour;
        // min=t->tm_min;
        // sec=t->tm_sec;
        //printf("%d:%d:%d\n",hour,min,sec);
        LCD_display(BACKGROUNG);

        for(angle=0;angle<=6.28;angle+=(6.28/60.0)){//先画一个圆
            p.m_x=circle_centre.m_x+(int)(cos(angle)*radius);
            p.m_y=circle_centre.m_y+(int)(sin(angle)*radius);
            LCD_drawpoint(p.m_x,p.m_y,RED);
        }

        h=(double)hour/12.0*6.28;
        //drow_line(circle_centre,radius/2.0,h,BACKGROUNG);
        drow_line(circle_centre,radius/2.0,h,RED);

        //drow_line(circle_centre,radius/3.0*2.0,m,BACKGROUNG);
        m=(double)min/60.0*6.28;
        drow_line(circle_centre,radius/3.0*2.0,m,0xf0ffff00);

        //drow_line(circle_centre,radius/5.0*4.0,s,BACKGROUNG);
        s=(double)sec/60.0*6.28;
        drow_line(circle_centre,radius/5.0*4.0,s,0xf000ffff);
        sleep(1);
        if(sec<60){
            sec+=1;
        }else{
            sec=0;
            if(min<60){
                min+=1;
            }else{
                min=0;
                hour+=1;
                hour=hour%12;
            }
        }
    }
}

/**
 * @function:读取屏幕高度、宽度、每个像素点的比特位
 * @return:成功返回0
 */
int LCD_init(void){
    int fdd = open("/dev/fb0",O_RDWR);
    if(fdd == -1)
    {
        perror("open device error");
        return -1;
    }
    struct fb_var_screeninfo buf;
    ioctl(fdd,FBIOGET_VSCREENINFO,&buf);//获取设备文件信息
    lcd_width=buf.xres;
    lcd_high=buf.yres;
    bits_per_pixel=buf.bits_per_pixel;

    plcd = mmap(NULL,//映射地址,NULL表示让系统自行分配
        buf.xres*buf.yres*buf.bits_per_pixel/8,//映射空间大小
        PROT_READ|PROT_WRITE,//映射地址权限
        MAP_SHARED,//映射标识
        fdd,//要映射的那个文件描述副
        0//要映射的那个文件开始的地址
        );
    close(fdd);
    return 0;
}
/**
 * @function:画点
 * @param:无
 */
void LCD_drawpoint(unsigned int x,unsigned int y,int color)
{
    unsigned int *p = (unsigned int *)plcd;
    if(x>=0&&x<lcd_width&&y>=0&&y<lcd_high)
        *(p + x + y * lcd_width ) = color;
}

/**
 * @function:清屏
 * @param:屏幕颜色,ARGB格式
 */
void LCD_display(unsigned int color)
{
    unsigned int x,y;
    for(y=0;y<lcd_high;y++)
    {
        for(x=0;x<lcd_width;x++)
        {
            LCD_drawpoint(x,y,color);
        }
    }
}

/**
 * @function:画线
 * @param cen:中心类
 * @param radius:半径
 * @param angle:角度
 * @param color:颜色
 */
void drow_line(struct point cen,double radius,double angle,unsigned int color){
    struct point p;
    double radius2=0;
    for(radius2=0;radius2<radius;radius2+=(radius/15.0)){
        p.m_x=cen.m_x+(int)(cos(angle)*radius2);
        p.m_y=cen.m_y+(int)(sin(angle)*radius2);
        LCD_drawpoint(p.m_x,p.m_y,RED);
    }
}
  • makefile文件
PROGS = clock
CFLAGS  = -Wall -g

CC:=/usr/local/arm/arm-2009q3/bin/arm-linux-gcc
#CC = gcc
depends_c = $(wildcard  ./*.c)             #找到所有的.c文件
depends_o = $(wildcard  ./*.o)             #找到所有的.o文件
depends_h = $(wildcard  ./*.h)             #找到所有的.h文件

all: ${PROGS}									  #生成all的所有依赖文件,即生成$(PROGS)文件
                                                              #省略编译规则,让make自己推导
clock:clock.c
    ${CC} ${CFLAGS} clock.c -o clock -lm

    cp ${PROGS} ~/tftp

clean:                                                    #定义伪目标
    rm -f $(depends_o) $(PROGS)         #  make clean  清楚所有编译生成的文件

猜你喜欢

转载自blog.csdn.net/qq_36337149/article/details/81094491