Skip to content

Commit

Permalink
Update samples to .NET 5.0 (dotnet#1224)
Browse files Browse the repository at this point in the history
* Update samples to .NET 5.0

* Update per feedback

* Resolve conflicts

* Remove duplicate project file

* Update NRT errors
  • Loading branch information
richlander authored Oct 28, 2020
1 parent 2323b01 commit 94c715f
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 326 deletions.
3 changes: 1 addition & 2 deletions samples/force-sensitive-resistor/FsrWithCapacitorSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ namespace force_sensitive_resistor
{
class FsrWithCapacitorSample
{
private GpioController _controller;
private readonly GpioController _controller = new();
private int _pinNumber = 18; // set the reading pin number

public FsrWithCapacitorSample()
{
_controller = new GpioController();
_controller.OpenPin(_pinNumber);
}

Expand Down
74 changes: 33 additions & 41 deletions samples/force-sensitive-resistor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,46 @@
using System;
using System.Threading;
using Iot.Device.Adc;
using force_sensitive_resistor;

namespace force_sensitive_resistor
Console.WriteLine("Hello Fsr408 capacitor Sample!");
// Use this sample when using ADC for reading
StartReadingWithADC();

// Use this sample if using capacitor for reading
// StartReadingWithCapacitor();

void StartReadingWithADC()
{
class Program
FsrWithAdcSample fsrWithAdc = new();

while (true)
{
static void Main(string[] args)
{
Console.WriteLine("Hello Fsr408 capacitor Sample!");
// Use this sample when using ADC for reading
StartReadingWithADC();
int value = fsrWithAdc.Read(0);
double voltage = fsrWithAdc.CalculateVoltage(value);
double resistance = fsrWithAdc.CalculateFsrResistance(voltage);
double force = fsrWithAdc.CalculateForce(resistance);
Console.WriteLine($"Read value: {value}, milli voltage: {voltage.ToString("f2")}, resistance: {resistance.ToString("f2")}, approximate force in Newtons: {force.ToString("f2")}");
Thread.Sleep(500);
}
}

// Use this sample if using capacitor for reading
// StartReadingWithCapacitor();
}
void StartReadingWithCapacitor()
{
FsrWithCapacitorSample fsrWithCapacitor = new();

private static void StartReadingWithADC()
{
FsrWithAdcSample fsrWithAdc = new FsrWithAdcSample();

while (true)
{
int value = fsrWithAdc.Read(0);
double voltage = fsrWithAdc.CalculateVoltage(value);
double resistance = fsrWithAdc.CalculateFsrResistance(voltage);
double force = fsrWithAdc.CalculateForce(resistance);
Console.WriteLine($"Read value: {value}, milli voltage: {voltage.ToString("f2")}, resistance: {resistance.ToString("f2")}, approximate force in Newtons: {force.ToString("f2")}");
Thread.Sleep(500);
}
}
while (true)
{
int value = fsrWithCapacitor.ReadCapacitorChargingDuration();

private static void StartReadingWithCapacitor()
if (value == 30000)
{ // 30000 is count limit, if we got this count it means Fsr has its highest resistance, so it is not pressed
Console.WriteLine("Not pressed");
}
else
{
FsrWithCapacitorSample fsrWithCapacitor = new FsrWithCapacitorSample();

while (true)
{
int value = fsrWithCapacitor.ReadCapacitorChargingDuration();

if (value == 30000)
{ // 30000 is count limit, if we got this count it means Fsr has its highest resistance, so it is not pressed
Console.WriteLine("Not pressed");
}
else
{
Console.WriteLine($"Pressed {value}");
}
Thread.Sleep(500);
}
Console.WriteLine($"Pressed {value}");
}
Thread.Sleep(500);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>force_sensitive_resistor</RootNamespace>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
43 changes: 17 additions & 26 deletions samples/led-blink/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,23 @@
using System.Device.Gpio;
using System.Threading;

namespace led_blink
{
class Program
{
static void Main(string[] args)
{
var pin = 18;
var lightTime = 1000;
var dimTime = 200;

Console.WriteLine($"Let's blink an LED!");
using GpioController controller = new GpioController();
controller.OpenPin(pin, PinMode.Output);
Console.WriteLine($"GPIO pin enabled for use: {pin}");
var pin = 18;
var lightTime = 1000;
var dimTime = 200;

Console.WriteLine($"Let's blink an LED!");
using GpioController controller = new();
controller.OpenPin(pin, PinMode.Output);
Console.WriteLine($"GPIO pin enabled for use: {pin}");

// turn LED on and off
while (true)
{
Console.WriteLine($"Light for {lightTime}ms");
controller.Write(pin, PinValue.High);
Thread.Sleep(lightTime);
// turn LED on and off
while (true)
{
Console.WriteLine($"Light for {lightTime}ms");
controller.Write(pin, PinValue.High);
Thread.Sleep(lightTime);

Console.WriteLine($"Dim for {dimTime}ms");
controller.Write(pin, PinValue.Low);
Thread.Sleep(dimTime);
}
}
}
Console.WriteLine($"Dim for {dimTime}ms");
controller.Write(pin, PinValue.Low);
Thread.Sleep(dimTime);
}
6 changes: 5 additions & 1 deletion samples/led-blink/led-blink.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<!--
To target Windows IoT, use the following TargetFramework property instead, or multi-target
<TargetFramework>net5.0-windows10.0.17763.0</TargetFramework>
-->
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion samples/led-matrix-weather/led-matrix-weather.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
189 changes: 89 additions & 100 deletions samples/led-more-blinking-lights/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,116 +6,105 @@
using System.Device.Gpio;
using System.Threading;

namespace led_more_blinking_lights
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.WriteLine("Hello World!");

// pins
var ledOne = 16;
var ledTwo = 21;
var ledThree = 20;
var buttonOne = 26;
var buttonSleep = 50;
var volumeSleep = 50;
// pins
var ledOne = 16;
var ledTwo = 21;
var ledThree = 20;
var buttonOne = 26;
var buttonSleep = 50;
var volumeSleep = 50;

// volume support
var initialSleep = 100;
var sleep = initialSleep;
Volume? volume = null;
// this line should only be enabled if a trimpot is connected
volume = Volume.EnableVolume();
// volume support
var initialSleep = 100;
var sleep = initialSleep;
Volume? volume = null;
// this line should only be enabled if a trimpot is connected
volume = Volume.EnableVolume();

Console.WriteLine($"Let's blink some LEDs!");
using (GpioController controller = new GpioController(PinNumberingScheme.Logical))
{
controller.OpenPin(ledOne, PinMode.Output);
controller.OpenPin(ledTwo, PinMode.Output);
controller.OpenPin(ledThree, PinMode.Output);
controller.OpenPin(buttonOne, PinMode.Input);
Console.WriteLine($"Let's blink some LEDs!");
using GpioController controller = new GpioController(PinNumberingScheme.Logical);
controller.OpenPin(ledOne, PinMode.Output);
controller.OpenPin(ledTwo, PinMode.Output);
controller.OpenPin(ledThree, PinMode.Output);
controller.OpenPin(buttonOne, PinMode.Input);

Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs eventArgs) =>
{
controller.Dispose();
Console.WriteLine("Pin cleanup complete!");
};

var timer1 = new TimeEnvelope(1000);
var timer2 = new TimeEnvelope(1000);
var timer3 = new TimeEnvelope(4000);
var timers = new TimeEnvelope[] { timer1, timer2, timer3 };

while (true)
{
// behavior for ledOne
if (timer1.Time == 0)
{
Console.WriteLine($"Light LED one for 800ms");
controller.Write(ledOne, PinValue.High);
}
else if (timer1.IsLastMultiple(200))
{
Console.WriteLine($"Dim LED one for 200ms");
controller.Write(ledOne, PinValue.Low);
}
Console.CancelKeyPress += (s, e) =>
{
controller.Dispose();
Console.WriteLine("Pin cleanup complete!");
};

// behavior for ledTwo
if (timer2.IsMultiple(200))
{
Console.WriteLine($"Light LED two for 100ms");
controller.Write(ledTwo, PinValue.High);
}
else if (timer2.IsMultiple(100))
{
Console.WriteLine($"Dim LED two for 100ms");
controller.Write(ledTwo, PinValue.Low);
}
var timer1 = new TimeEnvelope(1000);
var timer2 = new TimeEnvelope(1000);
var timer3 = new TimeEnvelope(4000);
var timers = new TimeEnvelope[] { timer1, timer2, timer3 };

// behavior for ledThree
if (timer3.Time == 0)
{
Console.WriteLine("Light LED two for 2000 ms");
controller.Write(ledThree, PinValue.High);
}
else if (timer3.IsFirstMultiple(2000))
{
Console.WriteLine("Dim LED two for 2000 ms");
controller.Write(ledThree, PinValue.Low);
}
while (true)
{
// behavior for ledOne
if (timer1.Time == 0)
{
Console.WriteLine($"Light LED one for 800ms");
controller.Write(ledOne, PinValue.High);
}
else if (timer1.IsLastMultiple(200))
{
Console.WriteLine($"Dim LED one for 200ms");
controller.Write(ledOne, PinValue.Low);
}

// behavior for buttonOne
if (volume is object)
{
var update = true;
var value = 0;
while (update)
{
(update, value) = volume.GetSleepforVolume(initialSleep);
if (update)
{
sleep = value;
Thread.Sleep(volumeSleep);
}
}
}
// behavior for ledTwo
if (timer2.IsMultiple(200))
{
Console.WriteLine($"Light LED two for 100ms");
controller.Write(ledTwo, PinValue.High);
}
else if (timer2.IsMultiple(100))
{
Console.WriteLine($"Dim LED two for 100ms");
controller.Write(ledTwo, PinValue.Low);
}

while (controller.Read(buttonOne) == PinValue.High)
{
Console.WriteLine("Button one pin value high!");
controller.Write(ledOne, PinValue.High);
controller.Write(ledTwo, PinValue.High);
controller.Write(ledThree, PinValue.High);
Thread.Sleep(buttonSleep);
}
// behavior for ledThree
if (timer3.Time == 0)
{
Console.WriteLine("Light LED two for 2000 ms");
controller.Write(ledThree, PinValue.High);
}
else if (timer3.IsFirstMultiple(2000))
{
Console.WriteLine("Dim LED two for 2000 ms");
controller.Write(ledThree, PinValue.Low);
}

Console.WriteLine($"Sleep: {sleep}");
Thread.Sleep(sleep); // starts at 100ms
TimeEnvelope.AddTime(timers, 100); // always stays at 100
}
// behavior for buttonOne
if (volume != null)
{
var update = true;
var value = 0;
while (update)
{
(update, value) = volume.GetSleepforVolume(initialSleep);
if (update)
{
sleep = value;
Thread.Sleep(volumeSleep);
}
}
}

while (controller.Read(buttonOne) == PinValue.High)
{
Console.WriteLine("Button one pin value high!");
controller.Write(ledOne, PinValue.High);
controller.Write(ledTwo, PinValue.High);
controller.Write(ledThree, PinValue.High);
Thread.Sleep(buttonSleep);
}

Console.WriteLine($"Sleep: {sleep}");
Thread.Sleep(sleep); // starts at 100ms
TimeEnvelope.AddTime(timers, 100); // always stays at 100
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>led_more_blinking_lights</RootNamespace>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 94c715f

Please sign in to comment.