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

Comparing:
SteeringWheelRemote/Audacious.cpp (file contents), Revision 12 by douglas, 2007-05-13T04:38:16-07:00 vs.
DashInterface/Display.cpp (file contents), Revision 38 by douglas, 2008-03-05T13:36:38-08:00

# Line 1 | Line 1
1 < // Beep Remote
1 > // Display (Crystalfontz CFA-635 USB LCD)
2   //
3   // Douglas Thrift
4   //
5   // $Id$
6  
7 < #include <cxx/standard.hh>
7 > #include <foreach.hpp>
8 > #include <scopes.hpp>
9 > #include <timing.hpp>
10  
11 < #include <audacious/beepctrl.h>
11 > #include <iomanip>
12 > #include <iostream>
13  
14 < #include "Audacious.hpp"
14 > #include <fcntl.h>
15 > #include <termios.h>
16  
17 < void Audacious::Playlist(char **list, int size, bool enqueue)
14 < {
15 <        ::xmms_remote_playlist(session, list, size, enqueue);
16 < }
17 > #include "Display.hpp"
18  
19 < int Audacious::GetVersion() const
19 < {
20 <        return ::xmms_remote_get_version(session);
21 < }
19 > extern bool debug;
20  
21 < void Audacious::PlaylistAdd(::GList *list)
21 > static uint16_t GetCRC(uint8_t *buffer, size_t length)
22   {
23 <        ::xmms_remote_playlist_add(session, list);
24 < }
23 >        static const uint16_t table[256] = {
24 >                0x00000, 0x01189, 0x02312, 0x0329b, 0x04624, 0x057ad, 0x06536, 0x074bf,
25 >                0x08C48, 0x09DC1, 0x0AF5A, 0x0BED3, 0x0CA6C, 0x0DBE5, 0x0E97E, 0x0F8F7,
26 >                0x01081, 0x00108, 0x03393, 0x0221a, 0x056a5, 0x0472c, 0x075b7, 0x0643e,
27 >                0x09CC9, 0x08D40, 0x0BFDB, 0x0AE52, 0x0DAED, 0x0CB64, 0x0F9FF, 0x0E876,
28 >                0x02102, 0x0308b, 0x00210, 0x01399, 0x06726, 0x076af, 0x04434, 0x055bd,
29 >                0x0AD4A, 0x0BCC3, 0x08E58, 0x09FD1, 0x0EB6E, 0x0FAE7, 0x0C87C, 0x0D9F5,
30 >                0x03183, 0x0200a, 0x01291, 0x00318, 0x077a7, 0x0662e, 0x054b5, 0x0453c,
31 >                0x0BDCB, 0x0AC42, 0x09ED9, 0x08F50, 0x0FBEF, 0x0EA66, 0x0D8FD, 0x0C974,
32 >                0x04204, 0x0538d, 0x06116, 0x0709f, 0x00420, 0x015a9, 0x02732, 0x036bb,
33 >                0x0ce4c, 0x0dfc5, 0x0ed5e, 0x0fcd7, 0x08868, 0x099e1, 0x0ab7a, 0x0baf3,
34 >                0x05285, 0x0430c, 0x07197, 0x0601e, 0x014a1, 0x00528, 0x037b3, 0x0263a,
35 >                0x0decd, 0x0cf44, 0x0fddf, 0x0ec56, 0x098e9, 0x08960, 0x0bbfb, 0x0aa72,
36 >                0x06306, 0x0728f, 0x04014, 0x0519d, 0x02522, 0x034ab, 0x00630, 0x017b9,
37 >                0x0EF4E, 0x0FEC7, 0x0CC5C, 0x0DDD5, 0x0A96A, 0x0B8E3, 0x08A78, 0x09BF1,
38 >                0x07387, 0x0620e, 0x05095, 0x0411c, 0x035a3, 0x0242a, 0x016b1, 0x00738,
39 >                0x0FFCF, 0x0EE46, 0x0DCDD, 0x0CD54, 0x0B9EB, 0x0A862, 0x09AF9, 0x08B70,
40 >                0x08408, 0x09581, 0x0a71a, 0x0b693, 0x0c22c, 0x0d3a5, 0x0e13e, 0x0f0b7,
41 >                0x00840, 0x019c9, 0x02b52, 0x03adb, 0x04e64, 0x05fed, 0x06d76, 0x07cff,
42 >                0x09489, 0x08500, 0x0b79b, 0x0a612, 0x0d2ad, 0x0c324, 0x0f1bf, 0x0e036,
43 >                0x018c1, 0x00948, 0x03bd3, 0x02a5a, 0x05ee5, 0x04f6c, 0x07df7, 0x06c7e,
44 >                0x0a50a, 0x0b483, 0x08618, 0x09791, 0x0e32e, 0x0f2a7, 0x0c03c, 0x0d1b5,
45 >                0x02942, 0x038cb, 0x00a50, 0x01bd9, 0x06f66, 0x07eef, 0x04c74, 0x05dfd,
46 >                0x0b58b, 0x0a402, 0x09699, 0x08710, 0x0f3af, 0x0e226, 0x0d0bd, 0x0c134,
47 >                0x039C3, 0x0284A, 0x01AD1, 0x00B58, 0x07FE7, 0x06E6E, 0x05CF5, 0x04D7C,
48 >                0x0c60c, 0x0d785, 0x0e51e, 0x0f497, 0x08028, 0x091a1, 0x0a33a, 0x0b2b3,
49 >                0x04a44, 0x05bcd, 0x06956, 0x078df, 0x00c60, 0x01de9, 0x02f72, 0x03efb,
50 >                0x0d68d, 0x0c704, 0x0f59f, 0x0e416, 0x090a9, 0x08120, 0x0b3bb, 0x0a232,
51 >                0x05AC5, 0x04B4C, 0x079D7, 0x0685E, 0x01CE1, 0x00D68, 0x03FF3, 0x02E7A,
52 >                0x0e70e, 0x0f687, 0x0c41c, 0x0d595, 0x0a12a, 0x0b0a3, 0x08238, 0x093b1,
53 >                0x06b46, 0x07acf, 0x04854, 0x059dd, 0x02d62, 0x03ceb, 0x00e70, 0x01ff9,
54 >                0x0f78f, 0x0e606, 0x0d49d, 0x0c514, 0x0b1ab, 0x0a022, 0x092b9, 0x08330,
55 >                0x07BC7, 0x06A4E, 0x058D5, 0x0495C, 0x03DE3, 0x02C6A, 0x01EF1, 0x00F78,
56 >        };
57 >        register uint16_t crc(0xffff);
58  
59 < void Audacious::PlaylistDelete(int position)
60 < {
30 <        ::xmms_remote_playlist_delete(session, position);
31 < }
59 >        while (length--)
60 >                crc = (crc >> 8) ^ table[(crc ^ *buffer++) & 0xff];
61  
62 < void Audacious::Play()
34 < {
35 <        ::xmms_remote_play(session);
62 >        return ~crc;
63   }
64  
65 < void Audacious::Pause()
65 > Display::Packet::Packet(Display::Packet::Command command, uint8_t length, const uint8_t *data) : command(command), length(length)
66   {
67 <        ::xmms_remote_pause(session);
68 < }
67 >        if (length > sizeof (this->data))
68 >                throw;
69  
70 < void Audacious::Stop()
44 < {
45 <        ::xmms_remote_stop(session);
46 < }
70 >        ::memcpy(this->data, data, length);
71  
72 < bool Audacious::IsPlaying() const
49 < {
50 <        return ::xmms_remote_is_playing(session);
72 >        crc = GetCRC(reinterpret_cast<uint8_t *>(this), length + 2);
73   }
74  
75 < bool Audacious::IsPaused() const
75 > std::ostream &operator<<(std::ostream &output, const Display::Packet &packet)
76   {
77 <        return ::xmms_remote_is_paused(session);
77 >        return output << _B("0x") << std::hex << std::setw(2) << std::setfill('0') << unsigned(packet.command) << _B(" [") << packet.GetData() << ']';
78   }
79  
80 < int Audacious::GetPlaylistPosition() const
80 > void operator<<(int fd, const Display::Packet &packet)
81   {
82 <        return ::xmms_remote_get_playlist_pos(session);
83 < }
82 >        if (packet.length != sizeof (packet.data))
83 >                ::memcpy(const_cast<uint8_t *>(packet.data) + packet.length, &packet.crc, 2);
84  
85 < void Audacious::SetPlaylistPosition(int position)
86 < {
65 <        ::xmms_remote_set_playlist_pos(session, position);
66 < }
85 >        if (debug)
86 >                std::cerr << _B("<< ") << packet << std::endl;
87  
88 < int Audacious::GetPlaylistLength() const
69 < {
70 <        return ::xmms_remote_get_playlist_length(session);
88 >        Posix::Write(fd, &packet, packet.length + 4);
89   }
90  
91 < void Audacious::PlaylistClear()
91 > void operator>>(int fd, Display::Packet &packet)
92   {
93 <        return ::xmms_remote_playlist_clear(session);
94 < }
93 >        Posix::Read(fd, &packet, 2);
94 >        Posix::Read(fd, packet.data, packet.length);
95 >        Posix::Read(fd, &packet.crc, 2);
96  
97 < int Audacious::GetOutputTime() const
98 < {
80 <        return ::xmms_remote_get_output_time(session);
97 >        if (debug)
98 >                std::cerr << _B(">> ") << packet << std::endl;
99   }
100  
101 < void Audacious::JumpToTime(int position)
101 > void Communicate(Display *display)
102   {
103 <        ::xmms_remote_jump_to_time(session, position);
103 >        display->Communicate_();
104   }
105  
106 < void Audacious::GetVolume(int& left, int& right) const
106 > void Activity(Display *display)
107   {
108 <        ::xmms_remote_get_volume(session, &left, &right);
108 >        display->Activity();
109   }
110  
111 < int Audacious::GetMainVolume() const
111 > Display::Display(const std::string &device, Callback callback) : ucom(Posix::Open(device, O_RDWR | O_NOCTTY | O_NONBLOCK)), ucomCondition(ucomLock), running(true), responsesCondition(responsesLock), keyActivityCondition(keyActivityLock), callback(callback), communicate(reinterpret_cast<void *(*)(void *)>(::Communicate), this), activity(reinterpret_cast<void *(*)(void *)>(::Activity), this)
112   {
113 <        return ::xmms_remote_get_main_volume(session);
96 < }
113 >        ::termios state;
114  
115 < int Audacious::GetBalance() const
116 < {
117 <        return ::xmms_remote_get_balance(session);
101 < }
115 >        Posix::CheckError(::tcgetattr(ucom, &state));
116 >        Posix::CheckError(::cfsetospeed(&state, B115200));
117 >        Posix::CheckError(::cfsetispeed(&state, B115200));
118  
119 < void Audacious::SetVolume(int left, int right)
120 < {
121 <        ::xmms_remote_set_volume(session, left, right);
122 < }
119 >        state.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | INPCK | ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXOFF);
120 >        state.c_iflag |= IGNPAR;
121 >        state.c_oflag &= ~(OPOST | ONLCR | ONOCR | ONLRET);
122 >        state.c_cflag &= ~(CSIZE | PARENB | PARODD | HUPCL | CRTSCTS);
123 >        state.c_cflag |= CREAD | CS8 | CSTOPB | CLOCAL;
124 >        state.c_lflag &= ~(ISIG | ICANON | IEXTEN | ECHO);
125 >        state.c_lflag |= NOFLSH;
126  
127 < void Audacious::SetMainVolume(int volume)
109 < {
110 <        ::xmms_remote_set_main_volume(session, volume);
111 < }
127 >        Posix::CheckError(::tcsetattr(ucom, TCSANOW, &state));
128  
129 < void Audacious::SetBalance(int balance)
130 < {
115 <        ::xmms_remote_set_balance(session, balance);
129 >        _synchronized (ucomLock)
130 >                ucomCondition.Signal();
131   }
132  
133 < cse::String Audacious::GetSkin() const
133 > Display::~Display()
134   {
135 <        return ::xmms_remote_get_skin(session);
121 < }
135 >        running = false;
136  
137 < void Audacious::SetSkin(const cse::String &skin)
138 < {
125 <        ::xmms_remote_set_skin(session, const_cast<char*>(skin.NullTerminate()));
126 < }
137 >        communicate.Join();
138 >        activity.Join();
139  
140 < cse::String Audacious::GetPlaylistFile(int position) const
129 < {
130 <        return ::xmms_remote_get_playlist_file(session, position);
140 >        Posix::Close(ucom);
141   }
142  
143 < cse::String Audacious::GetPlaylistTitle(int position) const
143 > void Display::Communicate_()
144   {
145 <        return ::xmms_remote_get_playlist_title(session, position);
146 < }
145 >        _synchronized (ucomLock)
146 >                ucomCondition.Wait();
147  
148 < int Audacious::GetPlaylistTime(int position) const
149 < {
150 <        return ::xmms_remote_get_playlist_time(session, position);
141 < }
148 >        while (running)
149 >        {
150 >                Packet response;
151  
152 < void Audacious::GetInfo(int &rate, int &frequency, int &channels) const
153 < {
154 <        ::xmms_remote_get_info(session, &rate, &frequency, &channels);
155 < }
152 >                _synchronized (ucomLock)
153 >                        try
154 >                        {
155 >                                ucom >> response;
156 >                        }
157 >                        catch (const Posix::Error error)
158 >                        {
159 >                                if (error.what() != _B("Resource temporarily unavailable"))
160 >                                        throw error;
161  
162 < void Audacious::MainWindowToggle(bool show)
163 < {
150 <        ::xmms_remote_main_win_toggle(session, show);
151 < }
162 >                                goto next;
163 >                        }
164  
165 < void Audacious::PlaylistWindowToggle(bool show)
166 < {
167 <        ::xmms_remote_pl_win_toggle(session, show);
168 < }
165 >                if ((response.command & Packet::Error) == Packet::Error);
166 >                else if ((response.command & Packet::Report) == Packet::Report)
167 >                        switch (response.command)
168 >                        {
169 >                        case Packet::KeyActivity:
170 >                                _synchronized (keyActivityLock)
171 >                                {
172 >                                        keyActivity.push(response);
173 >                                        keyActivityCondition.Signal();
174 >                                }
175  
176 < void Audacious::EqualizerWindowToggle(bool show)
177 < {
178 <        ::xmms_remote_eq_win_toggle(session, show);
179 < }
176 >                                break;
177 >                        case Packet::FanSpeedReport:
178 >                        case Packet::TemperatureSensorReport:
179 >                                break;
180 >                        }
181 >                else if ((response.command & Packet::Response) == Packet::Response)
182 >                        _synchronized (responsesLock)
183 >                        {
184 >                                responses.push(response);
185 >                                responsesCondition.Signal();
186 >                        }
187  
188 < bool Audacious::IsMainWindow() const
189 < {
165 <        return ::xmms_remote_is_main_win(session);
166 < }
188 > next:   Timing::NanoSleep(Timing::Time(0, 10000000));
189 >        }
190  
191 < bool Audacious::IsPlaylistWindow() const
169 < {
170 <        return ::xmms_remote_is_pl_win(session);
191 >        keyActivityCondition.Signal();
192   }
193  
194 < bool Audacious::IsEqualizerWindow() const
194 > Display::Packet Display::Communicate_(Display::Packet::Command command, uint8_t length, const uint8_t *data)
195   {
196 <        return ::xmms_remote_is_eq_win(session);
176 < }
196 >        Packet packet(command, length, data);
197  
198 < void Audacious::ShowPreferencesBox()
199 < {
200 <        ::xmms_remote_show_prefs_box(session);
181 < }
198 > retry:
199 >        _synchronized (ucomLock)
200 >                ucom << packet;
201  
202 < void Audacious::ToggleAlwaysOnTop(bool always)
203 < {
204 <        ::xmms_remote_toggle_aot(session, always);
205 < }
202 >        _synchronized (responsesLock)
203 >        {
204 >                while (responses.empty())
205 >                        try
206 >                        {
207 >                                responsesCondition.Wait(Timing::GetTimeOfDay() += 1);
208 >                        }
209 >                        catch (const Posix::Error &error)
210 >                        {
211 >                                if (error.what() != _B("Operation timed out"))
212 >                                        throw error;
213  
214 < void Audacious::Eject()
215 < {
190 <        ::xmms_remote_eject(session);
191 < }
214 >                                goto retry;
215 >                        }
216  
217 < void Audacious::PlaylistPrevious()
194 < {
195 <        ::xmms_remote_playlist_prev(session);
196 < }
217 >                Packet response(responses.front());
218  
219 < void Audacious::PlaylistNext()
199 < {
200 <        ::xmms_remote_playlist_next(session);
201 < }
219 >                responses.pop();
220  
221 < void Audacious::PlaylistAddUrl(const cse::String &url)
204 < {
205 <        ::xmms_remote_playlist_add_url_string(session, const_cast<char*>(url.NullTerminate()));
206 < }
221 >                assert ((packet.command | Packet::Response) == response.command);
222  
223 < bool Audacious::IsRunning() const
224 < {
210 <        return ::xmms_remote_is_running(session);
211 < }
223 >                return response;
224 >        }
225  
226 < void Audacious::ToggleRepeat()
214 < {
215 <        ::xmms_remote_toggle_repeat(session);
226 >        throw;
227   }
228  
229 < void Audacious::ToggleShuffle()
229 > void Display::Activity()
230   {
231 <        ::xmms_remote_toggle_shuffle(session);
232 < }
231 >        if (callback == NULL)
232 >                return;
233  
234 < bool Audacious::IsRepeat() const
235 < {
236 <        return ::xmms_remote_is_repeat(session);
237 < }
234 >        while (running)
235 >                _synchronized (keyActivityLock)
236 >                {
237 >                        while (keyActivity.empty())
238 >                                keyActivityCondition.Wait();
239  
240 < bool Audacious::IsShuffle() const
241 < {
242 <        return ::xmms_remote_is_shuffle(session);
231 < }
240 >                        while (keyActivity.size())
241 >                        {
242 >                                KeyActivity activity(KeyActivity(*keyActivity.front().data));
243  
244 < void Audacious::GetEqualizer(float& preamp, float bands[10]) const
234 < {
235 <        float* bands_;
244 >                                keyActivity.pop();
245  
246 <        ::xmms_remote_get_eq(session, &preamp, &bands_);
247 <
248 <        _forall (uint8_t, index, 0, 10)
249 <                bands[index] = bands[index];
241 <
242 <        ::g_free(bands_);
243 < }
244 <
245 < float Audacious::GetEqualizerPreamp() const
246 < {
247 <        return ::xmms_remote_get_eq_preamp(session);
248 < }
249 <
250 < float Audacious::GetEqualizerBand(int band) const
251 < {
252 <        return ::xmms_remote_get_eq_band(session, band);
253 < }
254 <
255 < void Audacious::SetEqualizer(float preamp, float bands[10])
256 < {
257 <        ::xmms_remote_set_eq(session, preamp, bands);
258 < }
259 <
260 < void Audacious::SetEqualizerPreamp(float preamp)
261 < {
262 <        ::xmms_remote_set_eq_preamp(session, preamp);
263 < }
264 <
265 < void Audacious::SetEqualizerBand(int band, float value)
266 < {
267 <        ::xmms_remote_set_eq_band(session, band, value);
268 < }
269 <
270 < // XMMS 1.2.1
271 < void Audacious::Quit()
272 < {
273 <        ::xmms_remote_quit(session);
274 < }
275 <
276 < // XMMS 1.2.6
277 < void Audacious::PlayPause()
278 < {
279 <        ::xmms_remote_play_pause(session);
280 < }
281 <
282 < void Audacious::PlaylistInsertUrl(const cse::String &url, int position)
283 < {
284 <        ::xmms_remote_playlist_ins_url_string(session, const_cast<char*>(url.NullTerminate()), position);
285 < }
286 <
287 < // XMMS 1.2.11
288 < void Audacious::PlayqueueAdd(int position)
289 < {
290 <        ::xmms_remote_playqueue_add(session, position);
291 < }
292 <
293 < void Audacious::PlayqueueRemove(int position)
294 < {
295 <        ::xmms_remote_playqueue_remove(session, position);
296 < }
297 <
298 < int Audacious::GetPlayqueueLength() const
299 < {
300 <        return ::xmms_remote_get_playqueue_length(session);
301 < }
302 <
303 < void Audacious::ToggleAdvance()
304 < {
305 <        ::xmms_remote_toggle_advance(session);
306 < }
307 <
308 < bool Audacious::IsAdvance() const
309 < {
310 <        return ::xmms_remote_is_advance(session);
311 < }
312 <
313 < // BMP 0.9.7
314 < void Audacious::Activate()
315 < {
316 <        ::xmms_remote_activate(session);
317 < }
318 <
319 < // Audacious 1.1
320 < void Audacious::ShowJumpToFileBox()
321 < {
322 <        ::xmms_remote_show_jtf_box(session);
323 < }
324 <
325 < void Audacious::PlayqueueClear()
326 < {
327 <        ::xmms_remote_playqueue_clear(session);
328 < }
329 <
330 < bool Audacious::PlayqueueIsQueued(int position) const
331 < {
332 <        return ::xmms_remote_playqueue_is_queued(session, position);
333 < }
334 <
335 < int Audacious::GetPlayqueuePosition(int position) const
336 < {
337 <        return ::xmms_remote_get_playqueue_position(session, position);
338 < }
339 <
340 < int Audacious::GetPlayqueueQueuePosition(int position) const
341 < {
342 <        return ::xmms_remote_get_playqueue_queue_position(session, position);
343 < }
344 <
345 < // Audacious 1.2
346 < void Audacious::SetSessionUri(const cse::String &uri)
347 < {
348 <        ::audacious_set_session_uri(const_cast<char *>(uri.NullTerminate()));
349 < }
350 <
351 < cse::String Audacious::GetSessionUri() const
352 < {
353 <        return ::audacious_get_session_uri(session);
246 >                                _desynchronized (keyActivityLock)
247 >                                        callback(activity);
248 >                        }
249 >                }
250   }
251  
252 < void Audacious::SetSessionType(Type type)
252 > bool Display::Ping(const std::string &data)
253   {
254 <        ::audacious_set_session_type(type);
254 >        return data == Communicate<const std::string &>(Packet::PingCommand, data).GetData();
255   }
256  
257 < // Audacious 1.3
362 < void Audacious::PlaylistEnqueueToTemp(const cse::String &string)
257 > std::string Display::Version()
258   {
259 <        ::xmms_remote_playlist_enqueue_to_temp(session, const_cast<char *>(string.NullTerminate()));
259 >        return Communicate(Packet::GetHardwareAndFirmwareVersion).GetData();
260   }
261  
262 < cse::String Audacious::GetTupleFieldData(const cse::String &field, int position)
262 > void Display::Set(uint8_t column, uint8_t row, const std::string &data)
263   {
264 <        return ::audacious_get_tuple_field_data(session, const_cast<char *>(field.NullTerminate()), position);
264 >        Communicate(Packet::SendDataToLCD, column, row, data);
265   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines