Last active
September 11, 2016 03:25
-
-
Save erinxocon/16a47c1b929215e4c1ce804da4bfce1e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.IO.Ports; | |
using SpotifyAPI.Local; | |
using System.Collections.Generic; | |
using System.Collections.Concurrent; | |
using System.Threading; | |
class PortDataReceived | |
{ | |
public static SpotifyLocalAPI _spotify; | |
public static bool isPlaying1 = false; | |
public static bool isPlaying2 = false; | |
public static FixedSizedQueue<int> runningTotal = new FixedSizedQueue<int>(61); | |
public static void Main() | |
{ | |
_spotify = new SpotifyLocalAPI(); | |
_spotify.Connect(); | |
SerialPort mySerialPort = new SerialPort("COM1"); | |
mySerialPort.BaudRate = 115200; | |
mySerialPort.Parity = Parity.None; | |
mySerialPort.StopBits = StopBits.One; | |
mySerialPort.DataBits = 8; | |
mySerialPort.Handshake = Handshake.None; | |
mySerialPort.RtsEnable = true; | |
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); | |
mySerialPort.Open(); | |
var timer = new Timer(e => ChangePlaylist(), null, TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(30)); | |
Console.WriteLine("Press any key to continue..."); | |
Console.WriteLine(); | |
Console.ReadKey(); | |
mySerialPort.Close(); | |
} | |
public static void ChangePlaylist() | |
{ | |
double average = runningTotal.Average(); | |
Console.WriteLine(average.ToString()); | |
Debug.Print(average.ToString()); | |
if (average < 95 && isPlaying1 == false) | |
{ | |
_spotify.PlayURL("spotify:album:6qb9MDR0lfsN9a2pw77uJy"); | |
Console.WriteLine("average bpm is below 95"); | |
isPlaying1 = true; | |
isPlaying2 = false; | |
} | |
else if (average > 95 && isPlaying2 == false) | |
{ | |
_spotify.PlayURL("spotify:album:7ppypgQppMf3mkRbZxYIFM"); | |
Console.WriteLine("average bpm is above 95"); | |
isPlaying1 = false; | |
isPlaying2 = true; | |
} | |
} | |
public static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) | |
{ | |
SerialPort sp = (SerialPort)sender; | |
string indata = sp.ReadExisting(); | |
if (indata.Contains("B")) | |
{ | |
Debug.WriteLine("Data Received:"); | |
Debug.WriteLine(indata); | |
List<string> sub = new List<string>(indata.Split("SBQ".ToCharArray())); | |
Debug.WriteLine(sub[2]); | |
try | |
{ | |
int bpm = Int32.Parse(sub[2]); | |
if (bpm < 240) | |
{ | |
runningTotal.Enqueue(bpm); | |
} | |
} | |
catch (System.FormatException exp) | |
{ | |
Debug.Print(exp.ToString()); | |
} | |
} | |
} | |
public class FixedSizedQueue<T> : ConcurrentQueue<T> | |
{ | |
private readonly object syncObject = new object(); | |
public int Size { get; private set; } | |
public FixedSizedQueue(int size) | |
{ | |
Size = size; | |
} | |
public new void Enqueue(T obj) | |
{ | |
base.Enqueue(obj); | |
lock (syncObject) | |
{ | |
while (base.Count > Size) | |
{ | |
T outObj; | |
base.TryDequeue(out outObj); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment