Skip to content

Commit

Permalink
Disable autorestart
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Sapundzhiev committed Jul 9, 2018
1 parent a4d4290 commit ab3734f
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 25 deletions.
6 changes: 3 additions & 3 deletions PiPiano/ItemState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public class ItemState
/// <summary>
/// Min value for pressed state
/// </summary>
private const int MinPressedValue = 75;
public int MinPressedValue { get; set; } = 100;

/// <summary>
/// Number of values to consider valid state
/// </summary>
private const int ValueStackSize = 5;
private const int ValueStackSize = 3;

/// <summary>
/// Is pressed state
Expand Down Expand Up @@ -65,7 +65,7 @@ private void UpdateStack(int value)
/// <returns></returns>
private bool CalculatePressed()
{
if(IsPressed)
if (IsPressed)
{
foreach (var value in LastValues)
if (value > MinPressedValue)
Expand Down
118 changes: 97 additions & 21 deletions PiPiano/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
//#define DEBUGMEM
//#define RESTARTTIMER

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -8,6 +11,7 @@
using Windows.Devices.Spi;
using Windows.Media.Core;
using Windows.Media.Playback;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

Expand All @@ -26,13 +30,18 @@ public sealed partial class MainPage : Page
/// <summary>
/// Measure timer interval
/// </summary>
private const int interval = 20;
private const int interval = 40;

/// <summary>
/// Values read timer
/// </summary>
private DispatcherTimer timer;

/// <summary>
/// Restart timer
/// </summary>
private DispatcherTimer restartTimer;

/// <summary>
/// Piano states
/// </summary>
Expand All @@ -43,6 +52,11 @@ public sealed partial class MainPage : Page
/// </summary>
private MediaPlayer[] players = new MediaPlayer[NUM_OF_SOUNDS];

/// <summary>
/// Is first run
/// </summary>
private bool firstTime = true;

/// <summary>
/// Line 1 reading
/// </summary>
Expand All @@ -67,6 +81,26 @@ public MainPage()
timer.Tick += Timer_Tick;
timer.Start();
}

#if RESTARTTIMER
if (gpio != null)
{
restartTimer = new DispatcherTimer();
restartTimer.Interval = TimeSpan.FromMinutes(15);
restartTimer.Tick += Restart_Timer_Tick;
restartTimer.Start();
}
#endif
}

/// <summary>
/// Restart timer handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Restart_Timer_Tick(object sender, object e)
{
ShutdownManager.BeginShutdown(ShutdownKind.Restart, TimeSpan.FromSeconds(5));
}

/// <summary>
Expand All @@ -78,7 +112,18 @@ private void InitPlayers()
{
players[i] = new MediaPlayer();
players[i].Source = MediaSource.CreateFromUri(new Uri($"ms-appx:///Assets/sound{i}.mp3"));
players[i].AutoPlay = false;
downStates[i] = new ItemState();

switch (i)
{
case 5:
downStates[i].MinPressedValue = 110;
break;
default:
downStates[i].MinPressedValue = 100;
break;
}
}
}

Expand All @@ -88,22 +133,28 @@ private void InitPlayers()
/// <returns></returns>
private async Task InitSpi()
{
if (ADC == null)
if(firstTime)
{
ADC = await InitSPI(0);
}

if (ADC2 == null)
{
ADC2 = await InitSPI(1);
firstTime = false;

timer.Interval = TimeSpan.FromMilliseconds(interval);

for(int i=0; i<NUM_OF_SOUNDS; i++)
for (int i = 0; i < NUM_OF_SOUNDS; i++)
{
PlayNote(i);
await Task.Delay(250);
}
if (ADC == null)
{
ADC = await InitSPI(0);
}

if (ADC2 == null)
{
ADC2 = await InitSPI(1);
}

timer.Start();
}
}

Expand Down Expand Up @@ -133,11 +184,19 @@ private async Task<SpiDevice> InitSPI(int busNumber)
/// <param name="e"></param>
private async void Timer_Tick(object sender, object e)
{
timer.Stop();

await InitSpi();

ReadSpiData();

timer.Start();
}

#if DEBUGMEM
static int counter = 0;
#endif

/// <summary>
/// Read values
/// </summary>
Expand All @@ -146,10 +205,10 @@ private void ReadSpiData()
try
{
// Reading 8 values from SPI0 and play note if state is pressed
for(int i = 0; i < 8 && ADC != null; i++)
for (int i = 0; i < 8 && ADC != null; i++)
{
int value = ReadAdc(i, ADC);
Debug.WriteLine(i + " = [" + value + " ], ");
//Trace.WriteLine(i + " = [" + value + " ], ");

ItemState state = downStates[i];
bool wasPressed = state.IsPressed;
Expand All @@ -164,7 +223,7 @@ private void ReadSpiData()
for (int i = 0; i < 4 && ADC2 != null; i++)
{
int value = ReadAdc(i, ADC2);
Debug.WriteLine(i + 8 + " = [" + value + " ], ");
//Trace.WriteLine(i + 8 + " = [" + value + " ], ");

ItemState state = downStates[i + 8];
bool wasPressed = state.IsPressed;
Expand All @@ -175,13 +234,13 @@ private void ReadSpiData()
}
}
}
catch(Exception ex)
catch (Exception ex)
{
Debug.WriteLine(ex);
//Trace.WriteLine(ex);
}
finally
{
Debug.WriteLine("");
//Trace.WriteLine("");
}
}

Expand All @@ -202,6 +261,23 @@ private static int ReadAdc(int adc_number, SpiDevice spiADC)
spiADC.TransferFullDuplex(request, response);

int value = ConverToInt(response);

#if DEBUGMEM
counter++;
if (counter > 5)
counter = -5;

if (counter > 0)
{
value = 120;
}
else
{
value = 90;
}

#endif

return value;
}

Expand Down Expand Up @@ -267,14 +343,14 @@ private static int ConverToInt(byte[] data)
/// <param name="index"></param>
private void PlayNote(int index)
{
if(players[index].PlaybackSession.CanPause)
MediaPlayer mp = players[index];
if (mp.PlaybackSession.CanPause)
{
players[index].Dispose();
players[index] = new MediaPlayer();
players[index].Source = MediaSource.CreateFromUri(new Uri($"ms-appx:///Assets/sound{index}.mp3"));
mp.Pause();
mp.PlaybackSession.Position = TimeSpan.FromMilliseconds(0);
}

players[index].Play();
mp.Play();
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion PiPiano/Package.appxmanifest
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" IgnorableNamespaces="uap mp">
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10" IgnorableNamespaces="uap mp iot">
<Identity Name="6fc4e9b4-320f-4bba-a48f-cf8bce9a95b7" Publisher="CN=julia" Version="1.0.1.0" />
<mp:PhoneIdentity PhoneProductId="6fc4e9b4-320f-4bba-a48f-cf8bce9a95b7" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
<Properties>
Expand All @@ -24,5 +24,6 @@
</Applications>
<Capabilities>
<Capability Name="internetClient" />
<iot:Capability Name="systemManagement" />
</Capabilities>
</Package>

0 comments on commit ab3734f

Please sign in to comment.