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 155 156
| /** * @file graphic_test.c * @author Late Lee * @date Oct 2011 * * @brief A test of LCD, including a clock, some text * * @log * 2011-10-? begin of this project * 2011-10-14 finish the clock, add some text on the screen * 2011-10-22 将图片资源嵌入到程序文件中并显示 */
#include <unistd.h> #include <stdlib.h> #include <math.h> #include <time.h> #include <string.h>
#include "fb_utils.h" #include "fb_graphic.h" #include "jpeg.h"
/** * black: 0x000000 * white: 0xffffff * red: 0xff0000 * green: 0x00ff00 (0x008000) * blue: 0x0000ff * yellow: 0xffff00 * gold: 0xffd700 * */ /* 0.black 1.xx 2.white 3.xx 4.xx 5.xx 6.purple 7.red 8.green 9.blue 10. yellow 11. gold */ static int palette [] = { 0x000000, 0xffe080, 0xffffff, 0xe0c0a0, 0x304050, 0x80b8c0, 0x6600cc, 0xff0000, 0x008000, 0x0000ff, 0xffff00, 0xffd700, 0xcbcbcb, };
#define NR_COLORS (sizeof (palette) / sizeof (palette [0]))
#define WHITE 2 #define GOLD 11
#define PI 3.1415926536 #if 0 #define X 120 #define Y 110 #define R 100 #endif
// 分别: x/Y坐标、大圆/中圆半径,秒/分/时针长度 #define X 100 #define Y 100 #define R 75 #define MID_R 25 #define SEC_LEN (R - 20) #define MIN_LEN (SEC_LEN - 10) #define HOUR_LEN (MIN_LEN - 10)
void draw_clock(int x, int y, int r, int mid_r, int colidx) { int xc, yc; int i; circle(x, y, r, colidx); // 画大圆 circle(x, y, mid_r, colidx); // 画中圆 circle(x, y, 2, colidx); // 画小圆 for (i = 0; i < 60; i++) { xc = x + (int)((r-10) * sin(PI * 2 * i/60)); yc = y + (int)((r-10) * cos(PI * 2 * i/60)); if (i % 15 == 0) fillrect(xc-2, yc-2, xc+2, yc+2, colidx); else if (i % 5 == 0) circle(xc, yc, 2, colidx); else pixel(xc, yc, colidx); } }
void action(int x, int y, int hour, int minute, int second, int fresh) { float a_hour, a_min, a_sec; int x_hour, y_hour; int x_min, y_min; int x_sec, y_sec;
a_sec = second * 2 * PI / 60; a_min = minute * 2 * PI / 60 + a_sec / 60; a_hour = hour * 2 * PI / 12 + a_min / 12; x_sec = (int)(SEC_LEN * sin(a_sec)); y_sec = (int)(SEC_LEN * cos(a_sec)); x_min = (int)(MIN_LEN * sin(a_min)); y_min = (int)(MIN_LEN * cos(a_min)); x_hour = (int)(HOUR_LEN * sin(a_hour)); y_hour = (int)(HOUR_LEN * cos(a_hour));
if (fresh) { line(x + x_hour, y - y_hour, x - x_hour / 8, y + y_hour / 8, 9); line(x + x_min, y - y_min, x - x_min / 4, y + y_min / 4, 8); line(x + x_sec, y - y_sec, x - x_sec / 4 , y + y_sec / 4, 7); } else { line(x + x_hour, y - y_hour, x - x_hour / 8, y + y_hour / 8, 0); line(x + x_min, y - y_min, x - x_min / 4, y + y_min / 4, 0); line(x + x_sec, y - y_sec, x - x_sec / 4 , y + y_sec / 4, 0); } }
int main(void) { int i; int hour, min, sec; time_t t; struct tm *mytime; char *buf;
fb_init(); // 初始化屏幕 graphic_init(); //初始化画图界面 jpeg_init(); // 初始化jpeg color_init(palette, NR_COLORS);
#define TIME_Y 160 // y of string time draw_jpeg(); // draw the picture while (1) { draw_clock(xres/2, yres/4, R, MID_R, 12); t = time(NULL); mytime = localtime(&t); hour = mytime->tm_hour; min = mytime->tm_min; sec = mytime->tm_sec; action(xres/2, yres/4, hour, min, sec, 1);
buf = ctime(&t); buf = strchr(buf, ':') - 2; /* Fri Jun 17 11:16:21 UTC 2011 -> 11:16:21*/ buf[8] = '\0'; put_string_ascii(70, TIME_Y, buf, 6); // display time
//usleep(1000000); sleep(1); //fb_refresh(); action(xres/2, yres/4, hour, min, sec, 0);
fb_refresh_region(TIME_Y, 25); } fb_release(); return 0; }
|