#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();
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;
}
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;
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){
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,RED);
m=(double)min/60.0*6.28;
drow_line(circle_centre,radius/3.0*2.0,m,0xf0ffff00);
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;
}
}
}
}
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,
buf.xres*buf.yres*buf.bits_per_pixel/8,
PROT_READ|PROT_WRITE,
MAP_SHARED,
fdd,
0
);
close(fdd);
return 0;
}
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;
}
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);
}
}
}
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);
}
}
PROGS = clock
CFLAGS = -Wall -g
CC:=/usr/local/arm/arm-2009q3/bin/arm-linux-gcc
depends_c = $(wildcard ./*.c)
depends_o = $(wildcard ./*.o)
depends_h = $(wildcard ./*.h)
all: ${PROGS}
clock:clock.c
${CC} ${CFLAGS} clock.c -o clock -lm
cp ${PROGS} ~/tftp
clean:
rm -f $(depends_o) $(PROGS)