Use proper namespace for iostream classes
[AXE.git] / demos / metronome.cpp
1 #include <pthread.h>
2 #include <unistd.h>
3 #include "button.h"
4
5 #define CMD_QUIT    1
6 #define CMD_FASTER  2
7 #define CMD_SLOWER  3
8
9 class xapp: public xapplication
10 {
11    window *main_frame;
12    window *flash;
13
14    pthread_t  beat;
15
16    virtual void SetupResources(void);
17
18    virtual int DoCommand(int code);
19
20 public:
21
22
23 };
24
25 xapp Application;
26 unsigned       BPM  = 80;
27
28 void * heartbeat(void *v)
29 {
30    unsigned long  period;
31    unsigned long  flash_time = 200000;
32    int    count = 0;
33    color  forebeat("red");
34    color  afterbeat("blue");
35    color  idle("white");
36    window *w = (window *)v;
37
38
39    for (;;)
40    {
41       period = 1000000 * 60 / BPM;
42       if (count == 0)
43          w->Background(forebeat);
44       else
45          w->Background(afterbeat);
46
47       w->Clear();
48       XFlush(stddpy.Dpy());
49       usleep(flash_time);
50
51       w->Background(idle);
52       w->Clear();
53       XFlush(stddpy.Dpy());
54       usleep(period - flash_time);
55
56       count++;
57       if (count == 4)
58          count = 0;
59    }
60
61 }
62
63 void xapp::SetupResources()
64 {
65    command_button *btn;
66
67    main_frame = new managed_window;
68
69    main_frame->Resize(700, 500);
70    flash = new window(*main_frame, 92, 2, 606, 496, 1);
71
72    btn = new command_button(*main_frame, 2, 2, "QUIT", CMD_QUIT);
73    btn = new command_button(*main_frame, 2, 42, "++", CMD_FASTER);
74    btn = new command_button(*main_frame, 2, 82, "--", CMD_SLOWER);
75    main_frame->Realize();
76
77    pthread_create(&beat, NULL, heartbeat, flash);
78 }
79
80 int xapp::DoCommand(int code)
81 {
82    switch (code)
83    {
84    case CMD_FASTER:
85       if (BPM < 200)
86       {
87          BPM += 10;
88       }
89       std::cout << BPM << "beats per minute\n";
90       return 1;
91
92    case CMD_SLOWER:
93       if (BPM > 20)
94       {
95          BPM -= 10;
96       }
97       std::cout << BPM << "beats per minute\n";
98       return 1;
99
100    case CMD_QUIT:
101       return 0;
102
103    default:
104       return 1;
105    }
106 }