ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/truck/DashInterface/DashInterface.cpp
(Generate patch)

Comparing DashInterface/DashInterface.cpp (file contents):
Revision 25 by douglas, 2008-02-09T01:39:22-08:00 vs.
Revision 36 by douglas, 2008-03-04T22:24:35-08:00

# Line 4 | Line 4
4   //
5   // $Id$
6  
7 + #include <iomanip>
8   #include <iostream>
9 + #include <sstream>
10  
11   #include <foreach.hpp>
12   #include <hash.hpp>
13 + #include <regex.hpp>
14  
15   #include <Audacious.hpp>
16   #include <GPS.hpp>
17  
18 + #include <sys/types.h>
19 + #include <sys/sysctl.h>
20 + #include <sys/utsname.h>
21 +
22   #include <sqlite3.h>
23  
24 + #include "Display.hpp"
25 +
26 + bool debug(false);
27 +
28 + void Uname(Display &display, time_t now = 0, bool change = false)
29 + {
30 +        if (!now || change)
31 +        {
32 +                utsname os;
33 +
34 +                ::uname(&os);
35 +
36 +                static Pcre::RegEx version(_B("^(\\d+\\.\\d+)-([A-Z])[A-Z]*(-p\\d+)?.*$"));
37 +                Pcre::RegEx::Match match(version(os.release));
38 +
39 +                display.Set(0, 1, os.sysname + _B(" ") + match[1] + match[2] + match[3]);
40 +        }
41 +
42 +        int mib[] = { CTL_KERN, KERN_BOOTTIME };
43 +        timeval boottime;
44 +        size_t size(sizeof (boottime));
45 +
46 +        if (now && ::sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0)
47 +        {
48 +                time_t uptime(now - boottime.tv_sec);
49 +
50 +                if (uptime > 60)
51 +                        uptime += 30;
52 +
53 +                char *when;
54 +                int days(uptime / 86400);
55 +
56 +                uptime %= 86400;
57 +
58 +                int hours(uptime / 3600);
59 +
60 +                uptime %= 3600;
61 +
62 +                int minutes(uptime / 60), seconds(uptime % 60);
63 +
64 +                ::asprintf(&when, "%i+%02i:%02i:%02i", days, hours, minutes, seconds);
65 +
66 +                std::string then(when);
67 +
68 +                then.append(17 - std::strlen(when), ' ');
69 +
70 +                std::free(when);
71 +
72 +                if (change)
73 +                        display.Set(0, 2, _B("up ") + then);
74 +                else
75 +                        display.Set(3, 2, then);
76 +        }
77 +        else
78 +                display.Set(0, 2, _B("up -+--:--:--       "));
79 +
80 +        double averages[3];
81 +
82 +        if (::getloadavg(averages, 3) == -1 || !now)
83 +                display.Set(0, 3, _B("-.--, -.--, -.--    "));
84 +        else
85 +        {
86 +                char *load;
87 +
88 +                ::asprintf(&load, "%.2f, %.2f, %.2f", averages[0], averages[1] , averages[2]);
89 +
90 +                display.Set(0, 3, load + std::string(20 - std::strlen(load), ' '));
91 +
92 +                std::free(load);
93 +        }
94 + }
95 +
96   int main(int argc, char *argv[])
97   {
98 +        std::string device;
99 +
100 +        {
101 +                Pcre::RegEx devicePath(_B("^-device=(/.+)$"));
102 +                Pcre::RegEx deviceNumber(_B("^-device=([0-9]+)$"));
103 +                Pcre::RegEx deviceName(_B("^-device=(.+)$"));
104 +
105 +                for (char **arg = argv + 1; *arg; ++arg)
106 +                {
107 +                        Pcre::RegEx::Match match;
108 +
109 +                        if (*arg == _B("-debug"))
110 +                                debug = true;
111 +                        else if (match = devicePath(*arg))
112 +                                device = match[1];
113 +                        else if (match = deviceNumber(*arg))
114 +                                device = _B("/dev/cuaU") + match[1];
115 +                        else if (match = deviceName(*arg))
116 +                                device = _B("/dev/") + match[1];
117 +                        else
118 +                                goto usage;
119 +                }
120 +        }
121 +
122 +        if (device.empty())
123 +        {
124 + usage:  std::cout << _B("Usage: ") << argv[0] << _B(" [-debug] [-device=device]") << std::endl;
125 +
126 +                return 2;
127 +        }
128 +
129          try
130          {
131 <                enum { Uname, Music, English, Metric, Nautical, Last } mode;
132 <                Audacious audacious;
133 <                GPS::GPS gps;
131 >                Display display(device);
132 >
133 >                display.Clear();
134 >                display.SetBacklight(100);
135 >                display.Set(2, 0, _B("--:--:-- -- ---"));
136 >
137 >                enum { Running };
138 >
139 >                display.Set(Running, Display::Green, 0);
140 >                display.Set(Running, Display::Red, 100);
141 >
142 >                {
143 >                        uint8_t mask(Display::Up | Display::Enter | Display::Cancel | Display::Left | Display::Right | Display::Down);
144 >
145 >                        display.KeyReporting(mask, mask);
146 >                }
147 >
148 >                ::Uname(display);
149 >
150 >                display.Store();
151 >                display.Set(Running, Display::Green, 100);
152 >                display.Set(Running, Display::Red, 0);
153 >
154 >                enum Mode { Uname, Music, English, Metric, Nautical, Last } mode(Uname);
155 >                //Audacious::Audacious audacious;
156 >                //GPS::GPS gps;
157  
158 +                _forever
159                  {
160 <                        gps.Query(_B("o"));
160 >                        time_t now(std::time(NULL));
161 >
162 >                        {
163 >                                char when[16];
164 >
165 >                                std::strftime(when, sizeof (when), "%l:%M:%S %p %Z", std::localtime(&now));
166 >
167 >                                display.Set(2, 0, when);
168 >                        }
169 >
170 >                        switch (mode)
171 >                        {
172 >                        default:
173 >                                ::Uname(display, now);
174 >
175 >                                break;
176 >                        case Music:
177 >                        case English:
178 >                        case Metric:
179 >                        case Nautical:
180 >                                break;
181 >                        }
182  
183 <                        std::cout << gps.GetLatitude() << std::endl;
183 >                        ::sleep(1);
184                  }
185          }
186          catch (const std::exception &exception)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines