ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/truck/DashInterface/DashInterface.cpp
Revision: 33
Committed: 2008-02-29T19:05:18-08:00 (17 years, 3 months ago) by douglas
File size: 3306 byte(s)
Log Message:
This is starting to work, but there seems to be some strange with mutexes every once in a while, grr!

File Contents

# Content
1 // Dash Interface
2 //
3 // Douglas Thrift
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)
29 {
30 utsname os;
31
32 ::uname(&os);
33
34 static Pcre::RegEx version(_B("^(\\d+\\.\\d+)-([A-Z])[A-Z]*(-p\\d+)?.*$"));
35 Pcre::RegEx::Match match(version(os.release));
36
37 display.Set(0, 1, os.sysname + _B(" ") + match[1] + match[2] + match[3]);
38
39 int mib[] = { CTL_KERN, KERN_BOOTTIME };
40 timeval boottime;
41 size_t size(sizeof (boottime));
42
43 if (now && ::sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0)
44 {
45 time_t uptime(now - boottime.tv_sec);
46
47 if (uptime > 60)
48 uptime += 30;
49
50 std::ostringstream output;
51
52 output << _B("up ") << (uptime / 86400) << '+';
53
54 uptime %= 86400;
55
56 output.width(2);
57 output.fill('0');
58
59 output << (uptime / 3600) << ':';
60
61 uptime %= 3600;
62
63 output << (uptime / 60) << ':' << (uptime % 60);
64 output << std::string(20 - output.str().size(), ' ');
65
66 display.Set(0, 2, output.str());
67 }
68 else
69 display.Set(0, 2, _B("up -+--:--:-- "));
70
71 double averages[3];
72
73 if (::getloadavg(averages, 3) == -1 || !now)
74 display.Set(0, 3, _B("-.--, -.--, -.-- "));
75 else
76 {
77 std::ostringstream output;
78
79 output << std::fixed << std::setprecision(2) << averages[0] << _B(", ") << averages[1] << _B(", ") << averages[2];
80 output << std::string(20 - output.str().size(), ' ');
81
82 display.Set(0, 3, output.str());
83 }
84 }
85
86 int main(int argc, char *argv[])
87 {
88 std::string device;
89
90 {
91 Pcre::RegEx devicePath(_B("^-device=(/.+)$"));
92 Pcre::RegEx deviceNumber(_B("^-device=([0-9]+)$"));
93 Pcre::RegEx deviceName(_B("^-device=(.+)$"));
94
95 for (char **arg = argv + 1; *arg; ++arg)
96 {
97 Pcre::RegEx::Match match;
98
99 if (*arg == _B("-debug"))
100 debug = true;
101 else if (match = devicePath(*arg))
102 device = match[1];
103 else if (match = deviceNumber(*arg))
104 device = _B("/dev/cuaU") + match[1];
105 else if (match = deviceName(*arg))
106 device = _B("/dev/") + match[1];
107 else
108 goto usage;
109 }
110 }
111
112 if (device.empty())
113 {
114 usage: std::cout << _B("Usage: ") << argv[0] << _B(" [-debug] [-device=device]") << std::endl;
115
116 return 2;
117 }
118
119 try
120 {
121 //enum Mode { Uname, Music, English, Metric, Nautical, Last } mode(Uname);
122 //Audacious audacious;
123 Display display(device);
124
125 display.Clear();
126 display.SetBacklight(100);
127 display.Set(2, 0, _B("--:--:-- -- ---"));
128 display.Set(0, Display::Green, 0);
129 display.Set(0, Display::Red, 100);
130
131 {
132 uint8_t mask(Display::Up | Display::Enter | Display::Cancel | Display::Left | Display::Right | Display::Down);
133
134 display.KeyReporting(mask, mask);
135 }
136
137 ::Uname(display);
138
139 display.Store();
140
141 ::Uname(display, std::time(NULL));
142
143 /*GPS::GPS gps;
144
145 gps.Query(_B("o"));
146
147 std::cout << gps.GetLatitude() << std::endl;*/
148
149 /*time_t now(std::time(NULL));
150 char when[16];
151
152 std::strftime(when, sizeof (when), "%l:%M:%S %p %Z", std::localtime(&now));*/
153 }
154 catch (const std::exception &exception)
155 {
156 std::cerr << argv[0] << _B(": ") << exception.what() << std::endl;
157
158 return 1;
159 }
160
161 return 0;
162 }

Properties

Name Value
svn:keywords Id