-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pioneer1120Engine.cs
84 lines (72 loc) · 2.24 KB
/
Pioneer1120Engine.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.ComponentModel;
using System.Text.RegularExpressions;
namespace ChiefMarleyController
{
public class Pioneer1120Engine
{
/// <summary>
/// last known volume state
/// </summary>
int Volume = 0;
bool Listening = false;
private string splitSequence = "\r\n";
comms Comms;
BackgroundWorker bw = new BackgroundWorker();
// going to use this to put received strings in, will figure out what to do with them later
Queue<string> Received;
public Pioneer1120Engine(comms comms)
{
Received = new Queue<string>();
Comms = comms;
SendData("VU");
// create thread to listen and receive responses
bw.DoWork += RX;
bw.RunWorkerAsync();
}
public void SendData(string s)
{
Comms.Write(s + "\r");
}
/// <summary>
/// get received messages
/// </summary>
private void RX(object sender, DoWorkEventArgs e)
{
string rxBuffer = string.Empty;
Listening = true;
while (Comms.IsConneted)
{
rxBuffer += Comms.Read();
if (rxBuffer.Contains(splitSequence))
{
ProcessRawRX(rxBuffer);
rxBuffer = string.Empty;
}
}
Listening = false;
}
/// <summary>
/// processes the raw rx and puts them on the queue
/// </summary>
private void ProcessRawRX(string rx)
{
string[] lines = Regex.Split(rx, splitSequence);
foreach (string s in lines)
{
if (s.StartsWith("FL") || s.Length == 0)
{
// don't need blank lines or FL stuff now, throw it away
}
else
{
Received.Enqueue(s);
}
}
}
}
}