Skip to content

Commit

Permalink
Use Raspberry.System 2.0
Browse files Browse the repository at this point in the history
Changed model for connector layout / processor identification
Use TimeSpan instead of int
  • Loading branch information
ebezine committed Apr 1, 2015
1 parent 341bf9d commit 739666a
Show file tree
Hide file tree
Showing 31 changed files with 224 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ public class Pca9685Connection : IPwmDevice
#region Fields

private readonly I2cDeviceConnection connection;

private static readonly ILog log = LogManager.GetLogger<Pca9685Connection>();

private static readonly TimeSpan delay = TimeSpan.FromMilliseconds(5);

#endregion

#region Instance Management
Expand Down Expand Up @@ -72,7 +74,7 @@ public void SetPwmUpdateRate(Frequency frequency)
WriteRegister(Register.PRESCALE, (byte) Math.Floor(prescale));
WriteRegister(Register.MODE1, oldmode);

Timer.Sleep(5);
Timer.Sleep(delay);

WriteRegister(Register.MODE1, oldmode | 0x80);
}
Expand Down
29 changes: 13 additions & 16 deletions Raspberry.IO.Components/Displays/Hd44780/Hd44780LcdConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class Hd44780LcdConnection : IDisposable

private bool backlightEnabled;

private static readonly TimeSpan syncDelay = TimeSpanUtility.FromMicroseconds(1);

#endregion

#region Instance Management
Expand Down Expand Up @@ -236,7 +238,7 @@ public void Home()
currentRow = 0;
currentColumn = 0;

Sleep(3);
Timer.Sleep(TimeSpan.FromMilliseconds(3));
}

/// <summary>
Expand All @@ -248,7 +250,7 @@ public void Clear()
currentRow = 0;
currentColumn = 0;

Sleep(3); // Clearing the display takes a long time
Timer.Sleep(TimeSpan.FromMilliseconds(3)); // Clearing the display takes a long time
}

/// <summary>
Expand Down Expand Up @@ -280,7 +282,7 @@ public void SetCustomCharacter(byte character, byte[] pattern)
/// </summary>
/// <param name="value">The value.</param>
/// <param name="animationDelay">The animation delay.</param>
public void WriteLine(object value, decimal animationDelay = 0m)
public void WriteLine(object value, TimeSpan animationDelay = new TimeSpan())
{
WriteLine("{0}", value, animationDelay);
}
Expand All @@ -290,7 +292,7 @@ public void WriteLine(object value, decimal animationDelay = 0m)
/// </summary>
/// <param name="text">The text.</param>
/// <param name="animationDelay">The animation delay.</param>
public void WriteLine(string text, decimal animationDelay = 0m)
public void WriteLine(string text, TimeSpan animationDelay = new TimeSpan())
{
Write(text + Environment.NewLine, animationDelay);
}
Expand All @@ -300,7 +302,7 @@ public void WriteLine(string text, decimal animationDelay = 0m)
/// </summary>
/// <param name="value">The value.</param>
/// <param name="animationDelay">The animation delay.</param>
public void Write(object value, decimal animationDelay = 0m)
public void Write(object value, TimeSpan animationDelay = new TimeSpan())
{
Write("{0}", value, animationDelay);
}
Expand Down Expand Up @@ -331,7 +333,7 @@ public void Write(string format, params object[] values)
/// <param name="format">The format.</param>
/// <param name="animationDelay">The animation delay.</param>
/// <param name="values">The values.</param>
public void WriteLine(string format, decimal animationDelay, params object[] values)
public void WriteLine(string format, TimeSpan animationDelay, params object[] values)
{
WriteLine(string.Format(format, values), animationDelay);
}
Expand All @@ -342,7 +344,7 @@ public void WriteLine(string format, decimal animationDelay, params object[] val
/// <param name="format">The format.</param>
/// <param name="animationDelay">The animation delay.</param>
/// <param name="values">The values.</param>
public void Write(string format, decimal animationDelay, params object[] values)
public void Write(string format, TimeSpan animationDelay, params object[] values)
{
Write(string.Format(format, values), animationDelay);
}
Expand All @@ -352,7 +354,7 @@ public void Write(string format, decimal animationDelay, params object[] values)
/// </summary>
/// <param name="text">The text.</param>
/// <param name="animationDelay">The animation delay.</param>
public void Write(string text, decimal animationDelay = 0m)
public void Write(string text, TimeSpan animationDelay = new TimeSpan())
{
var lines = text.Split(new[] {Environment.NewLine}, StringSplitOptions.None);

Expand All @@ -368,7 +370,7 @@ public void Write(string text, decimal animationDelay = 0m)
if (currentColumn < width)
WriteByte(b, true);

if (animationDelay > 0m)
if (animationDelay > TimeSpan.Zero)
Timer.Sleep(animationDelay);

currentColumn++;
Expand All @@ -389,11 +391,6 @@ public void Write(string text, decimal animationDelay = 0m)

#region Private Helpers

private void Sleep(decimal delay)
{
Timer.Sleep(delay);
}

private void WriteCommand(Command command, int parameter = 0)
{
var bits = (int) command | parameter;
Expand Down Expand Up @@ -456,10 +453,10 @@ private void WriteByte4Pins(int bits, bool charMode)
private void Synchronize()
{
pins.Clock.Write(true);
Sleep(0.001m); // 1 microsecond pause - enable pulse must be > 450ns
Timer.Sleep(syncDelay); // 1 microsecond pause - enable pulse must be > 450ns

pins.Clock.Write(false);
Sleep(0.001m); // commands need > 37us to settle
Timer.Sleep(syncDelay); // commands need > 37us to settle
}

#endregion
Expand Down
22 changes: 11 additions & 11 deletions Raspberry.IO.Components/Leds/GroveBar/GroveBarConnection.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#region References

using System;
using Raspberry.IO;
using Raspberry.Timers;
using Raspberry.IO.GeneralPurpose;
using System.Text;

#endregion
Expand All @@ -18,9 +16,11 @@ public class GroveBarConnection : IDisposable
{
#region Fields

private IOutputBinaryPin dataPin;
private IInputOutputBinaryPin clockPin;
private const uint CommandMode = 0x0000;
private static readonly TimeSpan delay = TimeSpan.FromTicks(1);

private readonly IOutputBinaryPin dataPin;
private readonly IInputOutputBinaryPin clockPin;
private string currentLedsStatus = "0000000000";

#endregion
Expand Down Expand Up @@ -54,10 +54,10 @@ public void SetFromString(string ledsString)
{
currentLedsStatus = ledsString;
SendData(CommandMode);
uint indexBits = (uint)Convert.ToInt32(ledsString, 2);
var indexBits = (uint)Convert.ToInt32(ledsString, 2);
for (int i = 0; i < 12; i++)
{
uint state = (uint)((indexBits & 0x0001) > 0 ? 0x00FF : 0x0000);
var state = (uint)((indexBits & 0x0001) > 0 ? 0x00FF : 0x0000);
SendData(state);
indexBits = indexBits >> 1;
}
Expand All @@ -79,7 +79,7 @@ public void SetLevel(int level)
SendData(CommandMode);
for (int i = 0; i < 12; i++)
{
uint state = (uint)((i < level) ? 0x00FF : 0x0000);
var state = (uint)((i < level) ? 0x00FF : 0x0000);
SendData(state);
}
LatchData();
Expand Down Expand Up @@ -143,7 +143,7 @@ public void Close()
private void Initialize()
{
dataPin.Write(false);
Raspberry.Timers.HighResolutionTimer.Sleep(0.000001m);
HighResolutionTimer.Sleep(delay);
for(int i = 0; i < 4; i++)
{
dataPin.Write(true);
Expand All @@ -157,9 +157,9 @@ private void SendData(uint data)
// Send 16 bit data
for(int i = 0; i < 16; i++)
{
bool state = ((data & 0x8000) > 0 ? true : false);
bool state = ((data & 0x8000) > 0);
dataPin.Write(state);
state = clockPin.Read() ? false : true;
state = !clockPin.Read();
clockPin.Write(state);
data <<= 1;
}
Expand All @@ -168,7 +168,7 @@ private void SendData(uint data)
private void LatchData()
{
dataPin.Write(false);
Raspberry.Timers.HighResolutionTimer.Sleep(0.000001m);
HighResolutionTimer.Sleep(delay);
for(int i = 0; i < 4; i++)
{
dataPin.Write(true);
Expand Down
25 changes: 10 additions & 15 deletions Raspberry.IO.Components/Leds/GroveRgb/GroveRgbConnection.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#region References

using System;
using Raspberry.IO;
using Raspberry.Timers;
using Raspberry.IO.GeneralPurpose;
using System.Collections.Generic;

#endregion
Expand All @@ -12,15 +10,17 @@ namespace Raspberry.IO.Components.Leds.GroveRgb
{
/// <summary>
/// Represents a connection with Grove Chainable RGB Led modules.
/// @see http://www.seeedstudio.com/wiki/Grove_-_Chainable_RGB_LED
/// <see cref="http://www.seeedstudio.com/wiki/Grove_-_Chainable_RGB_LED" />
/// </summary>
public class GroveRgbConnection: IDisposable
{
#region Fields

IOutputBinaryPin dataPin;
IOutputBinaryPin clockPin;
List<RgbColor> ledColors;
private static readonly TimeSpan delay = TimeSpanUtility.FromMicroseconds(20);

readonly IOutputBinaryPin dataPin;
readonly IOutputBinaryPin clockPin;
readonly List<RgbColor> ledColors;

#endregion

Expand Down Expand Up @@ -54,9 +54,7 @@ void IDisposable.Dispose()
/// Sets the color of a led.
/// </summary>
/// <param name="ledNumber">Led number (zero based index).</param>
/// <param name="red">Red.</param>
/// <param name="green">Green.</param>
/// <param name="blue">Blue.</param>
/// <param name="color">The color.</param>
public void SetColor (int ledNumber, RgbColor color)
{
// Send data frame prefix (32x "0")
Expand Down Expand Up @@ -124,16 +122,13 @@ private void SendByte(byte data)
for (byte i = 0; i < 8; i++)
{
// If MSB is 1, write one and clock it, else write 0 and clock
if ((data & 0x80) != 0)
dataPin.Write(true);
else
dataPin.Write(false);
dataPin.Write((data & 0x80) != 0);

// clk():
clockPin.Write(false);
HighResolutionTimer.Sleep(0.02m);
HighResolutionTimer.Sleep(delay);
clockPin.Write(true);
HighResolutionTimer.Sleep(0.02m);
HighResolutionTimer.Sleep(delay);

// Advance to the next bit to send
data <<= 1;
Expand Down
11 changes: 11 additions & 0 deletions Raspberry.IO.Components/Raspberry.IO.Components.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@
<Reference Include="Raspberry.System">
<HintPath>..\packages\Raspberry.System.1.4.0\lib\net40\Raspberry.System.dll</HintPath>
</Reference>
<Reference Include="Raspberry.System, Version=1.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Raspberry.System.2.0.0\lib\net40\Raspberry.System.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="UnitsNet">
<HintPath>..\packages\UnitsNet.3.4.0\lib\net35\UnitsNet.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\SharedAssemblyInfo.cs">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class HcSr04Connection : IDisposable
{
#region Fields

private const decimal triggerTime = 0.01m; // Waits at least 10µs = 0.01ms
private static readonly TimeSpan triggerTime = TimeSpanUtility.FromMicroseconds(10);
private static readonly TimeSpan echoUpTimeout = TimeSpan.FromMilliseconds(500);

private readonly IOutputBinaryPin triggerPin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public class Bmp085I2cConnection
private short mc;
private short md;

private static readonly TimeSpan lowDelay = TimeSpan.FromMilliseconds(5);
private static readonly TimeSpan highDelay = TimeSpan.FromMilliseconds(14);
private static readonly TimeSpan highestDelay = TimeSpan.FromMilliseconds(26);
private static readonly TimeSpan defaultDelay = TimeSpan.FromMilliseconds(8);

#endregion

#region Instance Management
Expand Down Expand Up @@ -178,7 +183,7 @@ private void Initialize()
private ushort GetRawTemperature()
{
WriteByte(Interop.CONTROL, Interop.READTEMPCMD);
HighResolutionTimer.Sleep(5);
HighResolutionTimer.Sleep(lowDelay);

return ReadUInt16(Interop.TEMPDATA);
}
Expand All @@ -190,19 +195,19 @@ private uint GetRawPressure()
switch (precision)
{
case Bmp085Precision.Low:
HighResolutionTimer.Sleep(5);
HighResolutionTimer.Sleep(lowDelay);
break;

case Bmp085Precision.High:
HighResolutionTimer.Sleep(14);
HighResolutionTimer.Sleep(highDelay);
break;

case Bmp085Precision.Highest:
HighResolutionTimer.Sleep(26);
HighResolutionTimer.Sleep(highestDelay);
break;

default:
HighResolutionTimer.Sleep(8);
HighResolutionTimer.Sleep(defaultDelay);
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ private DhtData TryGetData()

var remainingSamplingInterval = SamplingInterval - (DateTime.UtcNow - previousRead);
if (remainingSamplingInterval > TimeSpan.Zero)
HighResolutionTimer.Sleep((int)remainingSamplingInterval.TotalMilliseconds);
HighResolutionTimer.Sleep(remainingSamplingInterval);

// Prepare for reading
try
{
// Measure required by host : pull down then pull up
pin.Write(false);
HighResolutionTimer.Sleep((decimal)WakeupInterval.TotalMilliseconds);
HighResolutionTimer.Sleep(WakeupInterval);
pin.Write(true);

// Read acknowledgement from DHT
Expand Down
2 changes: 1 addition & 1 deletion Raspberry.IO.Components/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<packages>
<package id="Common.Logging" version="3.0.0" targetFramework="net40" />
<package id="Common.Logging.Core" version="3.0.0" targetFramework="net40" />
<package id="Raspberry.System" version="1.4.0" targetFramework="net40" />
<package id="Raspberry.System" version="2.0.0" targetFramework="net40" />
<package id="UnitsNet" version="3.4.0" targetFramework="net40" />
</packages>
Loading

0 comments on commit 739666a

Please sign in to comment.