-
Notifications
You must be signed in to change notification settings - Fork 586
/
Volume.cs
89 lines (72 loc) · 1.89 KB
/
Volume.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Device.Spi;
using Iot.Device.Adc;
public class Volume : IDisposable
{
public static Volume EnableVolume()
{
var connection = new SpiConnectionSettings(0,0);
connection.ClockFrequency = 1000000;
connection.Mode = SpiMode.Mode0;
var spi = SpiDevice.Create(connection);
var mcp3008 = new Mcp3008(spi);
var volume = new Volume(mcp3008);
volume.Init();
return volume;
}
private Mcp3008 _mcp3008;
private int _lastValue = 0;
public Volume(Mcp3008 mcp3008)
{
_mcp3008 = mcp3008;
}
public int GetVolumeValue()
{
double value = _mcp3008.Read(0);
value = value / 10.24;
value = Math.Round(value);
return (int)value;
}
public void Dispose()
{
if (_mcp3008 != null)
{
_mcp3008.Dispose();
}
}
private void Init()
{
_lastValue = GetVolumeValue();
}
public (bool update, int value) GetSleepforVolume(int sleep)
{
var value = GetVolumeValue();
if (value > _lastValue - 2 && value < _lastValue +2)
{
return (false,0);
}
_lastValue = value;
Console.WriteLine($"Volume: {value}");
var tenth = value / 10;
if (tenth == 5)
{
return (true, sleep);
}
double factor = 5 - tenth;
factor = factor * 2;
factor = Math.Abs(factor);
var newValue = 0;
if (tenth < 5)
{
factor = 1 / factor;
}
newValue = (int)(sleep / factor);
if (newValue >=10 && newValue <=1000)
{
return (true,newValue);
}
return (true, sleep);
}
}