-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TinyCodeReader driver + general cleanup
- Loading branch information
1 parent
fe1654d
commit a4efc7b
Showing
10 changed files
with
370 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
67 changes: 67 additions & 0 deletions
67
Source/Sensors.UsefulSensors.TinyCodeReader/Driver/Readme.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Meadow.Foundation.Sensors.PersonSensor | ||
|
||
**Useful Sensor's I2C optical person sensor** | ||
|
||
The **UsefulSensorsPersonSensor** library is designed for the [Wilderness Labs](www.wildernesslabs.co) Meadow .NET IoT platform and is part of [Meadow.Foundation](https://developer.wildernesslabs.co/Meadow/Meadow.Foundation/). | ||
|
||
The **Meadow.Foundation** peripherals library is an open-source repository of drivers and libraries that streamline and simplify adding hardware to your C# .NET Meadow IoT application. | ||
|
||
For more information on developing for Meadow, visit [developer.wildernesslabs.co](http://developer.wildernesslabs.co/). | ||
|
||
To view all Wilderness Labs open-source projects, including samples, visit [github.com/wildernesslabs](https://github.com/wildernesslabs/). | ||
|
||
## Usage | ||
|
||
```csharp | ||
PersonSensor personSensor; | ||
|
||
public override Task Initialize() | ||
{ | ||
Console.WriteLine("Initialize..."); | ||
|
||
personSensor = new PersonSensor(Device.CreateI2cBus()); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public override Task Run() | ||
{ | ||
while (true) | ||
{ | ||
var sensorData = personSensor.GetSensorData(); | ||
DisplaySensorData(sensorData); | ||
|
||
Thread.Sleep(1500); | ||
} | ||
} | ||
|
||
private void DisplaySensorData(PersonSensorResults sensorData) | ||
{ | ||
if (sensorData.NumberOfFaces == 0) | ||
{ | ||
Console.WriteLine("No faces found"); | ||
return; | ||
} | ||
|
||
for (int i = 0; i < sensorData.NumberOfFaces; ++i) | ||
{ | ||
var face = sensorData.FaceData[i]; | ||
Console.Write($"Face #{i}: "); | ||
Console.Write($"{face.BoxConfidence} confidence, "); | ||
Console.Write($"({face.BoxLeft}, {face.BoxTop}), "); | ||
Console.Write($"({face.BoxRight}, {face.BoxBottom}), "); | ||
Console.WriteLine(face.IsFacing == 1 ? "facing" : "not facing"); | ||
} | ||
} | ||
|
||
``` | ||
## How to Contribute | ||
|
||
- **Found a bug?** [Report an issue](https://github.com/WildernessLabs/Meadow_Issues/issues) | ||
- Have a **feature idea or driver request?** [Open a new feature request](https://github.com/WildernessLabs/Meadow_Issues/issues) | ||
- Want to **contribute code?** Fork the [Meadow.Foundation.CompositeDevices](https://github.com/WildernessLabs/Meadow.Foundation.CompositeDevices) repository and submit a pull request against the `develop` branch | ||
|
||
|
||
## Need Help? | ||
|
||
If you have questions or need assistance, please join the Wilderness Labs [community on Slack](http://slackinvite.wildernesslabs.co/). |
29 changes: 29 additions & 0 deletions
29
...e/Sensors.UsefulSensors.TinyCodeReader/Driver/Sensors.UsefulSensors.TinyCodeReader.csproj
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<Project Sdk="Meadow.Sdk/1.1.0"> | ||
<PropertyGroup> | ||
<PackageReadmeFile>Readme.md</PackageReadmeFile> | ||
<LangVersion>10.0</LangVersion> | ||
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<PackageIcon>icon.png</PackageIcon> | ||
<Authors>Wilderness Labs, Inc</Authors> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
<OutputType>Library</OutputType> | ||
<AssemblyName>UsefulSensorsPersonSensor</AssemblyName> | ||
<Company>Wilderness Labs, Inc</Company> | ||
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.Foundation/</PackageProjectUrl> | ||
<PackageId>Meadow.Foundation.Sensors.PersonSensor</PackageId> | ||
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Foundation</RepositoryUrl> | ||
<PackageTags>Meadow.Foundation,Meadow,camera,person,sensor,AI</PackageTags> | ||
<Version>1.7.0</Version> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<Description>Useful Sensor's I2C optical person sensor</Description> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<None Include=".\Readme.md" Pack="true" PackagePath="" /> | ||
<None Include="..\..\icon.png" Pack="true" PackagePath="" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\..\Meadow.Foundation\Source\Meadow.Foundation.Core\Meadow.Foundation.Core.csproj" /> | ||
</ItemGroup> | ||
</Project> |
113 changes: 113 additions & 0 deletions
113
Source/Sensors.UsefulSensors.TinyCodeReader/Driver/TinyCodeReader.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using Meadow.Hardware; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Meadow.Foundation.Sensors; | ||
|
||
/// <summary> | ||
/// Represents a Useful Sensor's Tiny Code Reader | ||
/// </summary> | ||
public class TinyCodeReader : II2cPeripheral | ||
{ | ||
/// <summary> | ||
/// Event raised when a QR code is read | ||
/// </summary> | ||
public event EventHandler<string> CodeRead = default!; | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether the sensor is sampling/running | ||
/// </summary> | ||
public bool IsRunning { get; private set; } | ||
|
||
/// <summary> | ||
/// The sample period of the sensor (default 200ms) | ||
/// </summary> | ||
public TimeSpan SamplePeriod { get; set; } = TimeSpan.FromMilliseconds(200); | ||
|
||
/// <inheritdoc/> | ||
public byte DefaultI2cAddress => 0x0C; | ||
|
||
private readonly int CONTENT_BYTE_COUNT = 254; | ||
private readonly int CONTENT_BYTE_LENGTH_COUNT = 2; | ||
private readonly int LED_REGISTER = 0x01; | ||
|
||
private readonly byte[] readBuffer; | ||
private readonly II2cCommunications i2cComms; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the ElectroMagneticRelayModule device | ||
/// </summary> | ||
/// <param name="i2cBus">The I2C bus the peripheral is connected to</param> | ||
public TinyCodeReader(II2cBus i2cBus) | ||
{ | ||
i2cComms = new I2cCommunications(i2cBus, DefaultI2cAddress, CONTENT_BYTE_COUNT + CONTENT_BYTE_LENGTH_COUNT); | ||
readBuffer = new byte[CONTENT_BYTE_COUNT + CONTENT_BYTE_LENGTH_COUNT]; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the LED on the Tiny Code Reader | ||
/// </summary> | ||
/// <param name="enable">enable if true, disable if false</param> | ||
public void SetLed(bool enable) | ||
{ | ||
i2cComms.WriteRegister((byte)LED_REGISTER, (byte)(enable ? 0x01 : 0x00)); | ||
} | ||
|
||
/// <summary> | ||
/// Reads the string value of the QR code from the Tiny Code Reader | ||
/// </summary> | ||
/// <returns>the code as a string if avaliable, null if no code found</returns> | ||
public string? ReadCode() | ||
{ | ||
i2cComms.ReadRegister(0x00, readBuffer); | ||
|
||
if (readBuffer[0] == 0) | ||
{ | ||
return null; | ||
} | ||
else | ||
{ | ||
return System.Text.Encoding.UTF8.GetString(readBuffer, 2, readBuffer[0]); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Start sampling the sensor | ||
/// </summary> | ||
public void StartUpdating(TimeSpan? samplePeriod = null) | ||
{ | ||
if (IsRunning) | ||
{ | ||
return; | ||
} | ||
|
||
IsRunning = true; | ||
|
||
if (samplePeriod != null) | ||
{ | ||
SamplePeriod = samplePeriod.Value; | ||
} | ||
|
||
Task.Run(async () => | ||
{ | ||
while (IsRunning) | ||
{ | ||
var code = ReadCode(); | ||
if (code != null) | ||
{ | ||
CodeRead?.Invoke(this, code); | ||
} | ||
await Task.Delay(SamplePeriod); | ||
} | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// Stop sampling the sensor | ||
/// </summary> | ||
public void StopUpdating() | ||
{ | ||
IsRunning = false; | ||
} | ||
} |
Oops, something went wrong.