This repository has been archived by the owner on Jul 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSpoti15.cs
269 lines (222 loc) · 7.84 KB
/
Spoti15.cs
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using JariZ;
namespace Spoti15
{
class Spoti15
{
private SpotifyAPI api;
private Responses.CFID cfid;
Responses.Status currentStatus;
private Exception initExcpt;
private LogiLcd lcd;
private Timer spotTimer;
private Timer lcdTimer;
private Timer refreshTimer;
private uint scrollStep = 0;
public Spoti15()
{
initExcpt = null;
InitSpot();
lcd = new LogiLcd("Spoti15");
spotTimer = new Timer();
spotTimer.Interval = 1000;
spotTimer.Enabled = true;
spotTimer.Tick += OnSpotTimer;
lcdTimer = new Timer();
lcdTimer.Interval = 100;
lcdTimer.Enabled = true;
lcdTimer.Tick += OnLcdTimer;
refreshTimer = new Timer();
refreshTimer.Interval = 300000;
refreshTimer.Enabled = true;
refreshTimer.Tick += OnRefreshTimer;
UpdateSpot();
UpdateLcd();
}
private void OnSpotTimer(object source, EventArgs e)
{
UpdateSpot();
}
private bool btnBefore = false;
private void OnLcdTimer(object source, EventArgs e)
{
bool btnNow = lcd.IsButtonPressed(LogiLcd.LcdButton.Mono0);
if (btnNow && !btnBefore)
InitSpot();
btnBefore = btnNow;
UpdateLcd();
scrollStep += 1;
}
private void OnRefreshTimer(object source, EventArgs e)
{
InitSpot();
}
public void Dispose()
{
lcd.Dispose();
cfid = null;
api = null;
spotTimer.Enabled = false;
spotTimer.Dispose();
spotTimer = null;
lcdTimer.Enabled = false;
lcdTimer.Dispose();
lcdTimer = null;
refreshTimer.Enabled = false;
refreshTimer.Dispose();
refreshTimer = null;
initExcpt = null;
}
private void InitSpot()
{
try
{
api = new SpotifyAPI(SpotifyAPI.GetOAuth());
cfid = api.CFID;
initExcpt = null;
}
catch (Exception e)
{
initExcpt = e;
}
}
public void UpdateSpot()
{
if(initExcpt != null)
return;
try
{
currentStatus = api.Status;
}
catch (Exception e)
{
initExcpt = e;
}
}
private Bitmap bgBitmap = new Bitmap(LogiLcd.MonoWidth, LogiLcd.MonoHeight);
private Font mainFont = new Font(Program.GetFontFamily("8pxbus"), 10, GraphicsUnit.Pixel);
private Color bgColor = Color.Black;
private Color fgColor = Color.White;
private Brush bgBrush = Brushes.Black;
private Brush fgBrush = Brushes.White;
private void SetupGraphics(Graphics g)
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.None;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
g.PageUnit = GraphicsUnit.Pixel;
g.TextContrast = 0;
g.Clear(bgColor);
}
private void DrawText(Graphics g, int line, string text, Font fnt, int offset = 0)
{
int x = offset;
int y = line * 10;
TextRenderer.DrawText(g, text, fnt, new Point(x, y), fgColor);
}
private void DrawTextScroll(Graphics g, int line, string text, Font fnt, bool center = true)
{
Size textSize = TextRenderer.MeasureText(text, fnt);
if (textSize.Width <= LogiLcd.MonoWidth + 2)
{
if (center)
{
int offset = (LogiLcd.MonoWidth - textSize.Width) / 2;
DrawText(g, line, text, fnt, offset);
}
else
{
DrawText(g, line, text, fnt);
}
return;
}
int pxstep = 4;
int speed = 5;
int prewait = 5;
int postwait = 5;
int olen = textSize.Width - LogiLcd.MonoWidth;
int len = pxstep * (int)((scrollStep / speed) % ((olen / pxstep) + prewait + postwait) - prewait);
if (len < 0)
len = 0;
if (len > olen)
len = olen;
DrawText(g, line, text, fnt, -len);
}
private void DrawTextScroll(Graphics g, int line, string text, bool center = true)
{
DrawTextScroll(g, line, text, mainFont, center);
}
private void DrawText(Graphics g, int line, string text, int offset = 0)
{
DrawText(g, line, text, mainFont, offset);
}
private void DoRender()
{
lcd.MonoSetBackground(bgBitmap);
lcd.Update();
}
private Byte[] emptyBg = new Byte[LogiLcd.MonoWidth * LogiLcd.MonoHeight];
public void UpdateLcd()
{
if (initExcpt != null)
{
using (Graphics g = Graphics.FromImage(bgBitmap))
{
SetupGraphics(g);
DrawText(g, 0, "Exception:");
DrawText(g, 1, initExcpt.GetType().ToString());
DrawTextScroll(g, 2, initExcpt.Message, false);
}
DoRender();
return;
}
if (cfid.error != null)
{
using (Graphics g = Graphics.FromImage(bgBitmap))
{
SetupGraphics(g);
DrawText(g, 0, "SpotifyError:");
DrawTextScroll(g, 1, cfid.error.message, false);
DrawText(g, 2, String.Format("Type: 0x{0}", cfid.error.type));
}
DoRender();
return;
}
using (Graphics g = Graphics.FromImage(bgBitmap))
{
SetupGraphics(g);
try
{
int len = currentStatus.track.length;
int pos = (int)currentStatus.playing_position;
double perc = currentStatus.playing_position / currentStatus.track.length;
DrawTextScroll(g, 0, currentStatus.track.artist_resource.name + " - " + currentStatus.track.album_resource.name);
DrawTextScroll(g, 1, currentStatus.track.track_resource.name);
DrawTextScroll(g, 3, String.Format("{0}:{1:D2}/{2}:{3:D2}", pos / 60, pos % 60, len / 60, len % 60));
g.DrawRectangle(Pens.White, 3, 24, LogiLcd.MonoWidth - 6, 4);
g.FillRectangle(Brushes.White, 3, 24, (int)((LogiLcd.MonoWidth - 6) * perc), 4);
if (currentStatus.playing)
{
g.FillPolygon(Brushes.White, new Point[] { new Point(3, 40), new Point(3, 30), new Point(8, 35) });
}
else
{
g.FillRectangle(Brushes.White, new Rectangle(3, 32, 2, 7));
g.FillRectangle(Brushes.White, new Rectangle(6, 32, 2, 7));
}
}
catch (NullReferenceException)
{
g.Clear(bgColor);
DrawTextScroll(g, 1, "No track information available", false);
}
}
DoRender();
}
}
}