OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [mw/] [src/] [demos/] [nanox/] [nxclock.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 673 markom
/*
2
 * nxclock - Nano-X clock program
3
 *
4
 * Copyright (C) 2000 by Greg Haerr <greg@censoft.com>
5
 * Copyright (C) 1999 Alistair Riddoch <ajr@ecs.soton.ac.uk>
6
 */
7
 
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <unistd.h>
11
#include <time.h>
12
#include <sys/time.h>
13
#define MWINCLUDECOLORS
14
#include "nano-X.h"
15
 
16
/* If you need a clock bigger than 200x200 you will need to re-write the trig *
17
 * to use longs. (Only applies under ELKS I think. */
18
#define CWIDTH          100             /* Max 200 */
19
#define CHEIGHT         100             /* Max 200 */
20
 
21
/*
22
 * Definitions to make it easy to define cursors
23
 */
24
#define _       ((unsigned) 0)          /* off bits */
25
#define X       ((unsigned) 1)          /* on bits */
26
#define MASK(a,b,c,d,e,f,g) \
27
        (((((((((((((a * 2) + b) * 2) + c) * 2) + d) * 2) \
28
        + e) * 2) + f) * 2) + g) << 9)
29
 
30
static  GR_WINDOW_ID    w1;             /* id for window */
31
static  GR_GC_ID        gc1;            /* graphics context for text */
32
static  GR_GC_ID        gc2;            /* graphics context for rectangle */
33
static int lh = -1, lm = -1, ls = -1;
34
static time_t then;
35
 
36
static unsigned char trig[91] =
37
        { 0, 4, 8, 13, 17, 22, 26, 31, 35, 40, 44, 48, 53, 57, 61, 66,
38
        70, 74, 79, 83, 87, 91, 95, 100, 104, 108, 112, 116, 120, 124, 128,
39
        131, 135, 139, 143, 146, 150, 154, 157, 161, 164, 167, 171, 174, 177,
40
        181, 184, 187, 190, 193, 196, 198, 201, 204, 207, 209, 212, 214, 217,
41
        219, 221, 223, 226, 228, 230, 232, 233, 235, 237, 238, 240, 242, 243,
42
        244, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 254, 255, 255,
43
        255, 255, 255, 255};
44
 
45
void do_exposure();
46
void do_clock();
47
void do_idle();
48
void errorcatcher();                    /* routine to handle errors */
49
 
50
int
51
main(int ac, char **av)
52
{
53
        GR_EVENT        event;          /* current event */
54
        GR_BITMAP       bitmap1fg[7];   /* bitmaps for first cursor */
55
        GR_BITMAP       bitmap1bg[7];
56
 
57
        if (GrOpen() < 0) {
58
                fprintf(stderr, "cannot open graphics\n");
59
                exit(1);
60
        }
61
 
62
        /* create window*/
63
        w1 = GrNewWindowEx(
64
                GR_WM_PROPS_NOAUTOMOVE|GR_WM_PROPS_BORDER|GR_WM_PROPS_CAPTION|
65
                GR_WM_PROPS_CLOSEBOX, "nxclock", GR_ROOT_WINDOW_ID,
66
                10, 10, CWIDTH, CHEIGHT, GrGetSysColor(GR_COLOR_WINDOW));
67
 
68
        GrSelectEvents(w1, GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_CLOSE_REQ);
69
 
70
        gc1 = GrNewGC();
71
        gc2 = GrNewGC();
72
 
73
        GrSetGCForeground(gc1, GrGetSysColor(GR_COLOR_WINDOW));
74
        GrSetGCBackground(gc1, GrGetSysColor(GR_COLOR_WINDOWTEXT));
75
        GrSetGCForeground(gc2, GrGetSysColor(GR_COLOR_WINDOWTEXT));
76
        GrSetGCBackground(gc2, GrGetSysColor(GR_COLOR_WINDOW));
77
 
78
        bitmap1bg[0] = MASK(_,_,X,X,X,_,_);
79
        bitmap1bg[1] = MASK(_,X,X,X,X,X,_);
80
        bitmap1bg[2] = MASK(X,X,X,X,X,X,X);
81
        bitmap1bg[3] = MASK(X,X,X,X,X,X,X);
82
        bitmap1bg[4] = MASK(X,X,X,X,X,X,X);
83
        bitmap1bg[5] = MASK(_,X,X,X,X,X,_);
84
        bitmap1bg[6] = MASK(_,_,X,X,X,_,_);
85
 
86
        bitmap1fg[0] = MASK(_,_,_,X,_,_,_);
87
        bitmap1fg[1] = MASK(_,X,_,X,_,X,_);
88
        bitmap1fg[2] = MASK(_,_,_,X,_,_,_);
89
        bitmap1fg[3] = MASK(X,_,_,X,X,_,X);
90
        bitmap1fg[4] = MASK(_,_,_,_,_,_,_);
91
        bitmap1fg[5] = MASK(_,X,_,_,_,X,_);
92
        bitmap1fg[6] = MASK(_,_,_,X,_,_,_);
93
 
94
        GrSetCursor(w1, 7, 7, 3, 3, WHITE, BLACK, bitmap1fg, bitmap1bg);
95
        GrMapWindow(w1);
96
 
97
        while (1) {
98
                GrGetNextEventTimeout(&event, 500L);
99
 
100
                switch (event.type) {
101
                        case GR_EVENT_TYPE_EXPOSURE:
102
                                do_exposure(&event.exposure);
103
                                break;
104
 
105
                        case GR_EVENT_TYPE_CLOSE_REQ:
106
                                GrClose();
107
                                exit(0);
108
 
109
                        case GR_EVENT_TYPE_TIMEOUT:
110
                                do_clock();
111
                                break;
112
                }
113
        }
114
}
115
 
116
int bsin(int angle)
117
{
118
        if(angle < 91) {
119
                return trig[angle];
120
        } else if (angle < 181) {
121
                return trig[180 - angle];
122
        } else if (angle < 271) {
123
                return -trig[angle - 180];
124
        } else if (angle < 361) {
125
                return -trig[360 - angle];
126
        } else {
127
                return 0;
128
        }
129
}
130
 
131
int bcos(int angle)
132
{
133
        if(angle < 91) {
134
                return trig[90 - angle];
135
        } else if (angle < 181) {
136
                return -trig[angle - 90];
137
        } else if (angle < 271) {
138
                return -trig[270 - angle];
139
        } else if (angle < 361) {
140
                return trig[angle - 270];
141
        } else {
142
                return 0;
143
        }
144
}
145
 
146
/*
147
 * Here when an exposure event occurs.
148
 */
149
void
150
do_exposure(ep)
151
        GR_EVENT_EXPOSURE       *ep;
152
{
153
        GR_COORD        midx = CWIDTH / 2;
154
        GR_COORD        midy = CHEIGHT / 2;
155
        int i, l;
156
 
157
/*      GrFillRect(w1, gc2, 0, 0, CWIDTH, CHEIGHT); */
158
/*      GrFillEllipse(w1, gc1, midx, midy, midx, midy); */
159
        GrEllipse(w1, gc2, midx, midy, midx - 1, midy - 1);
160
        for(i = 0; i < 60; i++) {
161
                if (i%5 == 0) {
162
                        l = 5;
163
                } else {
164
                        l = 0;
165
                }
166
                GrLine(w1, gc2,
167
                        midx + (((midx - 3) * bsin(i * 6)) >> 8),
168
                        midy + (((midy - 3) * bcos(i * 6)) >> 8),
169
                        midx + (((midx - 3 - l) * bsin(i * 6)) >> 8),
170
                        midy + (((midy - 3 - l) * bcos(i * 6)) >> 8));
171
        }
172
 
173
        lh = -1; lm = -1; ls = -1;
174
        then = 0;
175
        do_clock();
176
}
177
 
178
void draw_time(int hour, int minutes, int sec, GR_GC_ID gc )
179
{
180
        GR_COORD        midx = CWIDTH / 2;
181
        GR_COORD        midy = CHEIGHT / 2;
182
 
183
        GrLine(w1, gc1,
184
                midx + (((midx - 8 - midx / 10) * bsin(ls)) >> 8),
185
                midy - (((midy - 8 - midy / 10) * bcos(ls)) >> 8),
186
                midx + (((midx - 8 - midx / 4) * bsin(ls)) >> 8),
187
                midy - (((midy - 8 - midx / 4) * bcos(ls)) >> 8));
188
        GrLine(w1, gc2,
189
                midx + (((midx - 8 - midx / 10) * bsin(sec)) >> 8),
190
                midy - (((midy - 8 - midy / 10) * bcos(sec)) >> 8),
191
                midx + (((midx - 8 - midx / 4) * bsin(sec)) >> 8),
192
                midy - (((midy - 8 - midx / 4) * bcos(sec)) >> 8));
193
        if ((lm != minutes) || (ls == minutes)) {
194
                GrLine(w1, gc1,
195
                        midx + (((midx - 8 - midx / 10) * bsin(lm)) >> 8),
196
                        midy - (((midy - 8 - midy / 10) * bcos(lm)) >> 8),
197
                        midx + (((midx / 5) * bsin(lm)) >> 8),
198
                        midy - (((midy / 5) * bcos(lm)) >> 8));
199
                GrLine(w1, gc2,
200
                        midx + (((midx - 8 - midx / 10) * bsin(minutes)) >> 8),
201
                        midy - (((midy - 8 - midy / 10) * bcos(minutes)) >> 8),
202
                        midx + (((midx / 5) * bsin(minutes)) >> 8),
203
                        midy - (((midy / 5) * bcos(minutes)) >> 8));
204
                GrLine(w1, gc1,
205
                        midx + (((midx - 8 - midx / 2) * bsin(lh)) >> 8),
206
                        midy - (((midy - 8 - midy / 2) * bcos(lh)) >> 8),
207
                        midx + (((midx / 5) * bsin(lh)) >> 8),
208
                        midy - (((midy / 5) * bcos(lh)) >> 8));
209
                GrLine(w1, gc2,
210
                        midx + (((midx - 8 - midx / 2) * bsin(hour)) >> 8),
211
                        midy - (((midy - 8 - midy / 2) * bcos(hour)) >> 8),
212
                        midx + (((midx / 5) * bsin(hour)) >> 8),
213
                        midy - (((midy / 5) * bcos(hour)) >> 8));
214
        }
215
        lh = hour;
216
        lm = minutes;
217
        ls = sec;
218
}
219
 
220
/*
221
 * Update the clock if the seconds have changed.
222
 */
223
void
224
do_clock()
225
{
226
        struct timeval tv;
227
        struct timezone tz;
228
        struct tm * tp;
229
        time_t now;
230
        int minutes, hour, sec;
231
 
232
        gettimeofday(&tv, &tz);
233
        now = tv.tv_sec - (60 * tz.tz_minuteswest);
234
        if (now == then) {
235
                return;
236
        }
237
        then = now;
238
        tp = gmtime(&now);
239
        minutes = tp->tm_min * 6;
240
        sec = tp->tm_sec * 6;
241
        hour = tp->tm_hour;
242
        if (hour > 12) {
243
                hour -= 12;
244
        }
245
        hour = hour*30 + minutes/12;
246
        draw_time(hour, minutes, sec, gc2);
247
}
248
 
249
#if 0
250
/*
251
 * Sleep a while to avoid using too much CPU time.
252
 */
253
void do_idle()
254
{
255
        struct timespec idletime;
256
        idletime.tv_sec = 0;
257
        idletime.tv_nsec = 100000;
258
        nanosleep(&idletime, NULL);
259
}
260
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.