-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
185 lines (154 loc) · 3.76 KB
/
main.cpp
File metadata and controls
185 lines (154 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* ------------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <ramo@goodvikings.com> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return - Ramo
* ------------------------------------------------------------------------------
*/
#include <wiringPi.h>
#include <lcd.h>
#include <cstring>
#include <stdio.h>
#include <unistd.h>
#include <mpd/client.h>
#include <stdio.h>
#include "mpdConn.h"
#include "screenLayout.h"
#include "interrupts.h"
#define ROWS 4
#define COLS 20
#define BITS 4
#define RS 6
#define STRB 5
#define DATA0 4
#define DATA1 0
#define DATA2 2
#define DATA3 3
#define REFRESH 200
int lcdHandle;
mpdConn mc("localhost", 6600);
/*
* Initialize the wiring pi stuff, write welcom message to the screen
*/
bool initWiringPi()
{
if (wiringPiSetup() < 0)
{
return false;
}
lcdHandle = lcdInit(ROWS, COLS, BITS, RS, STRB, DATA0, DATA1, DATA2, DATA3, 0, 0, 0, 0);
if (lcdHandle < 0)
{
return false;
}
lcdClear(lcdHandle);
lcdPuts(lcdHandle, " ");
lcdPuts(lcdHandle, " Ramo's Jukebox ");
return true;
}
void print(const char** data)
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
lcdPosition(lcdHandle, j, i);
lcdPutchar(lcdHandle, data[i][j]);
// cout << data[i][j];
}
// cout << endl;
}
}
int main()
{
if (!initWiringPi())
{
fprintf(stderr, "Failed to intialize wiringPi and lcdInit");
return false;
}
screenLayout screen;
initInterrupts();
char ** data = new char*[ROWS];
for (int i = 0; i < ROWS; i++)
{
data[i] = new char[COLS];
}
char title[MPD_CONN_STRING_LEN] = "";
char artist[MPD_CONN_STRING_LEN] = "";
char album[MPD_CONN_STRING_LEN] = "";
char year[MPD_CONN_STRING_LEN] = "";
char tmp[TIMELEN] = "";
char elapsed[TIMELEN] = "";
char duration[TIMELEN] = "";
int songID = 0;
int oldSongID = -1;
bool state = true;
mpd_state oldState = MPD_STATE_UNKNOWN;
if (!mc.isConnected())
{
fprintf(stderr, "Connection to MPD failed");
return false;
}
// First delay to show "Ramo's Jukebox"
delay(2000);
while (state)
{
delay(REFRESH);
if (!mc.updateTags())
{
fprintf(stderr, "Failed to update tags");
return false;
}
songID = mc.getID();
if (mc.getState() == MPD_STATE_PLAY || mc.getState() == MPD_STATE_PAUSE)
{
mc.getTitle(title, MPD_CONN_STRING_LEN);
mc.getAlbum(album, MPD_CONN_STRING_LEN);
mc.getArtist(artist, MPD_CONN_STRING_LEN);
mc.getYear(year, MPD_CONN_STRING_LEN);
}
if (mc.getState() == MPD_STATE_STOP || songID == -1)
{
strcpy(title, "Stopped");
strcpy(album, "");
strcpy(artist, "");
strcpy(year, "");
} else if (mc.getState() == MPD_STATE_UNKNOWN)
{
strcpy(title, "Error - MPD STATE UNKNOWN");
strcpy(album, "");
strcpy(artist, "");
strcpy(year, "");
state = false;
}
elapsed[0] = 0;
duration[0] = 0;
sprintf(tmp, "%0d", mc.getElapsedTime() / 60);
strcat(elapsed, tmp);
strcat(elapsed, ":");
sprintf(tmp, "%02d", mc.getElapsedTime() % 60);
strcat(elapsed, tmp);
sprintf(tmp, "%0d", mc.getDuration() / 60);
strcat(duration, tmp);
strcat(duration, ":");
sprintf(tmp, "%02d", mc.getDuration() % 60);
strcat(duration, tmp);
if (songID != oldSongID || mc.getState() != oldState)
{
screen.setContents(title, artist, album, year, elapsed, duration);
oldSongID = songID;
} else
{
screen.setTime(elapsed, duration);
}
if (mc.getState() == MPD_STATE_STOP || mc.getState() == MPD_STATE_UNKNOWN)
{
screen.setTime("0:00", "0:00");
}
oldState = mc.getState();
screen.scrollScreen();
screen.getContents(data);
print((const char**) data);
}
}