first commit
This commit is contained in:
commit
a5a0434432
1126 changed files with 439481 additions and 0 deletions
41
Software/CSharp/GrovePi/Sensors/AccelerometerSensor.cs
Normal file
41
Software/CSharp/GrovePi/Sensors/AccelerometerSensor.cs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
using System;
|
||||
|
||||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface IAccelerometerSensor
|
||||
{
|
||||
byte[] Read();
|
||||
}
|
||||
|
||||
internal class AccelerometerSensor : IAccelerometerSensor
|
||||
{
|
||||
private const byte CommandAddress = 20;
|
||||
private readonly GrovePi _device;
|
||||
private readonly Pin _pin;
|
||||
|
||||
internal AccelerometerSensor(GrovePi device, Pin pin)
|
||||
{
|
||||
if (device == null) throw new ArgumentNullException(nameof(device));
|
||||
_device = device;
|
||||
_pin = pin;
|
||||
}
|
||||
|
||||
public byte[] Read()
|
||||
{
|
||||
var buffer = new [] {CommandAddress, (byte) _pin, Constants.Unused, Constants.Unused};
|
||||
_device.DirectAccess.Write(buffer);
|
||||
|
||||
var readBuffer = new byte[1];
|
||||
_device.DirectAccess.Read(readBuffer);
|
||||
|
||||
if (readBuffer[1] > 32)
|
||||
readBuffer[1] = (byte) -(readBuffer[1] - 224);
|
||||
if (readBuffer[2] > 32)
|
||||
readBuffer[2] = (byte) -(readBuffer[1] - 224);
|
||||
if (readBuffer[3] > 32)
|
||||
readBuffer[3] = (byte) -(readBuffer[1] - 224);
|
||||
|
||||
return readBuffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
27
Software/CSharp/GrovePi/Sensors/AirQualitySensor.cs
Normal file
27
Software/CSharp/GrovePi/Sensors/AirQualitySensor.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
using System;
|
||||
|
||||
|
||||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface IAirQualitySensor
|
||||
{
|
||||
int AirQuality();
|
||||
}
|
||||
internal class AirQualitySensor : IAirQualitySensor
|
||||
{
|
||||
private readonly GrovePi _device;
|
||||
private readonly Pin _pin;
|
||||
|
||||
internal AirQualitySensor(GrovePi device, Pin pin)
|
||||
{
|
||||
if (device == null) throw new ArgumentNullException(nameof(device));
|
||||
device.PinMode(_pin, PinMode.Input);
|
||||
_device = device;
|
||||
_pin = pin;
|
||||
}
|
||||
public int AirQuality()
|
||||
{
|
||||
return _device.AnalogRead(_pin);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Software/CSharp/GrovePi/Sensors/ButtonSensor.cs
Normal file
14
Software/CSharp/GrovePi/Sensors/ButtonSensor.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface IButtonSensor
|
||||
{
|
||||
SensorStatus CurrentState { get; }
|
||||
}
|
||||
|
||||
internal class ButtonSensor : Sensor<IButtonSensor>, IButtonSensor
|
||||
{
|
||||
internal ButtonSensor(IGrovePi device, Pin pin) : base(device, pin, PinMode.Input)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
15
Software/CSharp/GrovePi/Sensors/Buzzer.cs
Normal file
15
Software/CSharp/GrovePi/Sensors/Buzzer.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface IBuzzer
|
||||
{
|
||||
SensorStatus CurrentState { get; }
|
||||
IBuzzer ChangeState(SensorStatus newState);
|
||||
}
|
||||
|
||||
internal class Buzzer : Sensor<IBuzzer>, IBuzzer
|
||||
{
|
||||
internal Buzzer(IGrovePi device, Pin pin) : base(device, pin, PinMode.Output)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
75
Software/CSharp/GrovePi/Sensors/ChainableRgbLed.cs
Normal file
75
Software/CSharp/GrovePi/Sensors/ChainableRgbLed.cs
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
using System;
|
||||
|
||||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface IChainableRgbLed
|
||||
{
|
||||
IChainableRgbLed Initialise(byte numberOfLeds);
|
||||
IChainableRgbLed StoreColor(byte red, byte green, byte blue);
|
||||
IChainableRgbLed Test(byte numberOfLeds, byte testColor);
|
||||
IChainableRgbLed SetPattern(byte pattern, byte led);
|
||||
IChainableRgbLed Mudulo(byte offset, byte divisor);
|
||||
IChainableRgbLed SetLevel(byte level, bool reverse);
|
||||
}
|
||||
|
||||
internal class ChainableRgbLed : IChainableRgbLed
|
||||
{
|
||||
public const byte StoreColorCommandAddress = 90;
|
||||
public const byte InitialiseCommandAddress = 91;
|
||||
public const byte TestCommandAddress = 92;
|
||||
public const byte SetPatternCommandAddress = 93;
|
||||
public const byte SetModuloCommandAddress = 94;
|
||||
public const byte SetLevelCommmandAddress = 95;
|
||||
private readonly GrovePi _device;
|
||||
private readonly Pin _pin;
|
||||
|
||||
internal ChainableRgbLed(GrovePi device, Pin pin)
|
||||
{
|
||||
if (device == null) throw new ArgumentNullException(nameof(device));
|
||||
_device = device;
|
||||
_pin = pin;
|
||||
}
|
||||
|
||||
public IChainableRgbLed SetLevel(byte level, bool reverse)
|
||||
{
|
||||
var buffer = new[] {level, (byte) _pin, level, reverse ? (byte) 1 : (byte) 0};
|
||||
_device.DirectAccess.Write(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IChainableRgbLed Initialise(byte numberOfLeds)
|
||||
{
|
||||
var buffer = new[] {InitialiseCommandAddress, (byte) _pin, numberOfLeds, Constants.Unused};
|
||||
_device.DirectAccess.Write(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IChainableRgbLed StoreColor(byte red, byte green, byte blue)
|
||||
{
|
||||
var buffer = new[] {StoreColorCommandAddress, red, green, blue};
|
||||
_device.DirectAccess.Write(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IChainableRgbLed Test(byte numberOfLeds, byte testColor)
|
||||
{
|
||||
var buffer = new[] {TestCommandAddress, (byte) _pin, numberOfLeds, testColor};
|
||||
_device.DirectAccess.Write(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IChainableRgbLed SetPattern(byte pattern, byte led)
|
||||
{
|
||||
var buffer = new[] {SetPatternCommandAddress, (byte) _pin, pattern, led};
|
||||
_device.DirectAccess.Write(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IChainableRgbLed Mudulo(byte offset, byte divisor)
|
||||
{
|
||||
var buffer = new[] {SetModuloCommandAddress, (byte) _pin, offset, divisor};
|
||||
_device.DirectAccess.Write(buffer);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
using System;
|
||||
using GrovePi.Common;
|
||||
|
||||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface IDHTTemperatureAndHumiditySensor
|
||||
{
|
||||
double TemperatureInCelsius { get; }
|
||||
double TemperatureInFahrenheit { get; }
|
||||
double Humidity { get; }
|
||||
void Measure();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the model of sensor.
|
||||
/// DHT11 - blue one - comes with the GrovePi+ Starter Kit.
|
||||
/// DHT22 - white one, aka DHT Pro or AM2302.
|
||||
/// DHT21 - black one, aka AM2301.
|
||||
/// </summary>
|
||||
public enum DHTModel
|
||||
{
|
||||
/*
|
||||
*/
|
||||
Dht11 = 0,
|
||||
Dht21 = 1,
|
||||
Dht22 = 2
|
||||
}
|
||||
|
||||
internal class DHTTemperatureAndHumiditySensor : IDHTTemperatureAndHumiditySensor
|
||||
{
|
||||
private readonly GrovePi _device;
|
||||
private readonly DHTModel _model;
|
||||
private readonly Pin _pin;
|
||||
|
||||
private const byte DHTCmd = 40;
|
||||
|
||||
private double t = 0;
|
||||
private double h = 0;
|
||||
|
||||
internal DHTTemperatureAndHumiditySensor(GrovePi device, Pin pin, DHTModel model)
|
||||
{
|
||||
if (device == null) throw new ArgumentNullException(nameof(device));
|
||||
_device = device;
|
||||
_pin = pin;
|
||||
_model = model;
|
||||
}
|
||||
|
||||
public void Measure()
|
||||
{
|
||||
_device.DirectAccess.WritePartial(new byte[4] { DHTCmd, (byte)_pin, (byte)_model, Constants.Unused });
|
||||
Delay.Milliseconds(600);
|
||||
|
||||
var readBuffer = new byte[9];
|
||||
_device.DirectAccess.ReadPartial(readBuffer);
|
||||
|
||||
float t0 = BitConverter.ToSingle(readBuffer, 1);
|
||||
float h0 = BitConverter.ToSingle(readBuffer, 5);
|
||||
|
||||
t = (double)t0;
|
||||
h = (double)h0;
|
||||
}
|
||||
|
||||
private double CtoF(double c)
|
||||
{
|
||||
return c * 9 / 5 + 32;
|
||||
}
|
||||
|
||||
public double TemperatureInCelsius
|
||||
{
|
||||
get
|
||||
{
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
public double TemperatureInFahrenheit
|
||||
{
|
||||
get
|
||||
{
|
||||
return CtoF(t);
|
||||
}
|
||||
}
|
||||
|
||||
public double Humidity
|
||||
{
|
||||
get
|
||||
{
|
||||
return h;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
88
Software/CSharp/GrovePi/Sensors/FourDigitDisplay.cs
Normal file
88
Software/CSharp/GrovePi/Sensors/FourDigitDisplay.cs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
using System;
|
||||
|
||||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface IFourDigitDisplay
|
||||
{
|
||||
IFourDigitDisplay Initialise();
|
||||
IFourDigitDisplay SetBrightness(byte brightness);
|
||||
IFourDigitDisplay SetIndividualSegment(byte segment, byte value);
|
||||
IFourDigitDisplay SetLedsOfSegment(byte segment, byte leds);
|
||||
IFourDigitDisplay SetScore(byte left, byte right);
|
||||
IFourDigitDisplay AllOn();
|
||||
IFourDigitDisplay AllOff();
|
||||
}
|
||||
|
||||
internal class FourDigitDisplay : IFourDigitDisplay
|
||||
{
|
||||
private const byte InitialiseCommandAddress = 70;
|
||||
private const byte BrightnessCommandAddress = 71;
|
||||
private const byte ValueCommandAddress = 72;
|
||||
private const byte ValueZerosCommandAddress = 73;
|
||||
private const byte IndividualDigitCommandAddress = 74;
|
||||
private const byte IndividualLedsCommandAddress = 75;
|
||||
private const byte ScoreCommandAddress = 76;
|
||||
private const byte AnalogReadCommandAddress = 77;
|
||||
private const byte AllOnCommandAddress = 78;
|
||||
private const byte AllOffCommandAddress = 79;
|
||||
private readonly GrovePi _device;
|
||||
private readonly Pin _pin;
|
||||
|
||||
internal FourDigitDisplay(GrovePi device, Pin pin)
|
||||
{
|
||||
if (device == null) throw new ArgumentNullException(nameof(device));
|
||||
_device = device;
|
||||
_pin = pin;
|
||||
}
|
||||
|
||||
public IFourDigitDisplay Initialise()
|
||||
{
|
||||
var buffer = new[] {InitialiseCommandAddress, (byte) _pin, Constants.Unused, Constants.Unused};
|
||||
_device.DirectAccess.Write(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IFourDigitDisplay SetBrightness(byte brightness)
|
||||
{
|
||||
brightness = Math.Min(brightness, (byte) 7);
|
||||
var buffer = new[] { BrightnessCommandAddress, (byte) _pin, brightness, Constants.Unused};
|
||||
_device.DirectAccess.Write(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IFourDigitDisplay SetIndividualSegment(byte segment, byte value)
|
||||
{
|
||||
var buffer = new[] {IndividualDigitCommandAddress, (byte) _pin, segment, value};
|
||||
_device.DirectAccess.Write(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IFourDigitDisplay SetLedsOfSegment(byte segment, byte leds)
|
||||
{
|
||||
var buffer = new[] {IndividualLedsCommandAddress, (byte) _pin, segment, leds};
|
||||
_device.DirectAccess.Write(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IFourDigitDisplay SetScore(byte left, byte right)
|
||||
{
|
||||
var buffer = new[] {ScoreCommandAddress, (byte) _pin, left, right};
|
||||
_device.DirectAccess.Write(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IFourDigitDisplay AllOn()
|
||||
{
|
||||
var buffer = new[] {AllOnCommandAddress, (byte) _pin, Constants.Unused, Constants.Unused};
|
||||
_device.DirectAccess.Write(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IFourDigitDisplay AllOff()
|
||||
{
|
||||
var buffer = new[] {AllOffCommandAddress, (byte) _pin, Constants.Unused, Constants.Unused};
|
||||
_device.DirectAccess.Write(buffer);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
22
Software/CSharp/GrovePi/Sensors/GasSensorMQ2.cs
Normal file
22
Software/CSharp/GrovePi/Sensors/GasSensorMQ2.cs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
|
||||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface IGasSensorMQ2
|
||||
{
|
||||
int SensorValue();
|
||||
}
|
||||
|
||||
internal class GasSensorMQ2 : Sensor<IGasSensorMQ2>, IGasSensorMQ2
|
||||
{
|
||||
public GasSensorMQ2(GrovePi device, Pin pin) : base(device,pin,PinMode.Input)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int SensorValue()
|
||||
{
|
||||
return Device.AnalogRead(Pin);
|
||||
}
|
||||
}
|
||||
}
|
||||
16
Software/CSharp/GrovePi/Sensors/Led.cs
Normal file
16
Software/CSharp/GrovePi/Sensors/Led.cs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface ILed
|
||||
{
|
||||
SensorStatus CurrentState { get; }
|
||||
ILed ChangeState(SensorStatus newState);
|
||||
void AnalogWrite(byte value);
|
||||
}
|
||||
|
||||
internal class Led : Sensor<ILed>, ILed
|
||||
{
|
||||
internal Led(IGrovePi device, Pin pin) : base(device, pin, PinMode.Output)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
76
Software/CSharp/GrovePi/Sensors/LedBar.cs
Normal file
76
Software/CSharp/GrovePi/Sensors/LedBar.cs
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
using System;
|
||||
|
||||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface ILedBar
|
||||
{
|
||||
ILedBar Initialize(Orientation orientation);
|
||||
ILedBar SetOrientation(Orientation orientation);
|
||||
ILedBar SetLevel(byte level);
|
||||
ILedBar SetLed(byte level, byte led, SensorStatus state);
|
||||
ILedBar ToggleLed(byte led);
|
||||
}
|
||||
|
||||
internal class LedBar : ILedBar
|
||||
{
|
||||
private const byte InitialiseCommandAddress = 50;
|
||||
private const byte OrientationCommandAddress = 51;
|
||||
private const byte LevelCommandAddress = 52;
|
||||
private const byte SetOneCommandAddress = 53;
|
||||
private const byte ToggleOneCommandAddress = 54;
|
||||
//private const byte SetCommandAddress = 55;
|
||||
//private const byte GetCommandAddress = 56;
|
||||
|
||||
private readonly GrovePi _device;
|
||||
private readonly Pin _pin;
|
||||
|
||||
internal LedBar(GrovePi device, Pin pin)
|
||||
{
|
||||
if (device == null) throw new ArgumentNullException(nameof(device));
|
||||
_device = device;
|
||||
_pin = pin;
|
||||
}
|
||||
|
||||
public ILedBar Initialize(Orientation orientation)
|
||||
{
|
||||
var buffer = new[] {InitialiseCommandAddress, (byte) _pin, (byte) orientation, Constants.Unused};
|
||||
_device.DirectAccess.WritePartial(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ILedBar SetOrientation(Orientation orientation)
|
||||
{
|
||||
var buffer = new[] {OrientationCommandAddress, (byte) _pin, (byte) orientation, Constants.Unused};
|
||||
_device.DirectAccess.WritePartial(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ILedBar SetLevel(byte level)
|
||||
{
|
||||
level = Math.Min(level, (byte) 10);
|
||||
var buffer = new[] {LevelCommandAddress, (byte) _pin, level, Constants.Unused};
|
||||
_device.DirectAccess.WritePartial(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ILedBar SetLed(byte level, byte led, SensorStatus state)
|
||||
{
|
||||
var buffer = new[] {SetOneCommandAddress, (byte) _pin, led, (byte) state};
|
||||
_device.DirectAccess.WritePartial(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ILedBar ToggleLed(byte led)
|
||||
{
|
||||
var buffer = new[] {ToggleOneCommandAddress, (byte) _pin, led, Constants.Unused};
|
||||
_device.DirectAccess.WritePartial(buffer);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public enum Orientation
|
||||
{
|
||||
RedToGreen = 0,
|
||||
GreenToRed = 1
|
||||
}
|
||||
}
|
||||
35
Software/CSharp/GrovePi/Sensors/LightSensor.cs
Normal file
35
Software/CSharp/GrovePi/Sensors/LightSensor.cs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
using System;
|
||||
|
||||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface ILightSensor
|
||||
{
|
||||
int SensorValue();
|
||||
double Resistance();
|
||||
}
|
||||
|
||||
internal class LightSensor : ILightSensor
|
||||
{
|
||||
private readonly GrovePi _device;
|
||||
private readonly Pin _pin;
|
||||
|
||||
internal LightSensor(GrovePi device, Pin pin)
|
||||
{
|
||||
if (device == null) throw new ArgumentNullException(nameof(device));
|
||||
device.PinMode(_pin, PinMode.Input);
|
||||
_device = device;
|
||||
_pin = pin;
|
||||
}
|
||||
|
||||
public int SensorValue()
|
||||
{
|
||||
return _device.AnalogRead(_pin);
|
||||
}
|
||||
|
||||
public double Resistance()
|
||||
{
|
||||
var sensorValue = SensorValue();
|
||||
return (double) (1023 - sensorValue)*10/sensorValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
28
Software/CSharp/GrovePi/Sensors/PIRMotionSensor.cs
Normal file
28
Software/CSharp/GrovePi/Sensors/PIRMotionSensor.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface IPIRMotionSensor
|
||||
{
|
||||
bool IsPeopleDetected();
|
||||
}
|
||||
|
||||
internal class PIRMotionSensor:Sensor<IPIRMotionSensor>, IPIRMotionSensor
|
||||
{
|
||||
public PIRMotionSensor(GrovePi device, Pin pin):base(device, pin, PinMode.Input)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public bool IsPeopleDetected()
|
||||
{
|
||||
int sensorValue = Device.DigitalRead(Pin);
|
||||
if (sensorValue == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
31
Software/CSharp/GrovePi/Sensors/RealTimeClock.cs
Normal file
31
Software/CSharp/GrovePi/Sensors/RealTimeClock.cs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
using System;
|
||||
|
||||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface IRealTimeClock
|
||||
{
|
||||
byte[] Read();
|
||||
}
|
||||
|
||||
internal class RealTimeClock : IRealTimeClock
|
||||
{
|
||||
private const byte CommandAddress = 30;
|
||||
private readonly GrovePi _device;
|
||||
private readonly Pin _pin;
|
||||
|
||||
internal RealTimeClock(GrovePi device, Pin pin)
|
||||
{
|
||||
if (device == null) throw new ArgumentNullException(nameof(device));
|
||||
_device = device;
|
||||
_pin = pin;
|
||||
}
|
||||
|
||||
public byte[] Read()
|
||||
{
|
||||
var buffer = new[] {CommandAddress, (byte) _pin, Constants.Unused, Constants.Unused};
|
||||
_device.DirectAccess.Write(buffer);
|
||||
_device.DirectAccess.Read(buffer);
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Software/CSharp/GrovePi/Sensors/Relay.cs
Normal file
14
Software/CSharp/GrovePi/Sensors/Relay.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface IRelay
|
||||
{
|
||||
SensorStatus CurrentState { get; }
|
||||
IRelay ChangeState(SensorStatus newState);
|
||||
}
|
||||
internal class Relay : Sensor<IRelay>, IRelay
|
||||
{
|
||||
public Relay(IGrovePi device, Pin pin) : base(device, pin, PinMode.Output)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
46
Software/CSharp/GrovePi/Sensors/RotaryAngleSensor.cs
Normal file
46
Software/CSharp/GrovePi/Sensors/RotaryAngleSensor.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
using System;
|
||||
|
||||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface IRotaryAngleSensor
|
||||
{
|
||||
int SensorValue();
|
||||
double Voltage();
|
||||
double Degrees();
|
||||
}
|
||||
|
||||
public class RotaryAngleSensor : IRotaryAngleSensor
|
||||
{
|
||||
private const int FullAngle = 300;
|
||||
private readonly GrovePi _device;
|
||||
private readonly Pin _pin;
|
||||
|
||||
internal RotaryAngleSensor(GrovePi device, Pin pin)
|
||||
{
|
||||
if (device == null) throw new ArgumentNullException(nameof(device));
|
||||
device.PinMode(_pin, PinMode.Input);
|
||||
_device = device;
|
||||
_pin = pin;
|
||||
}
|
||||
|
||||
public int SensorValue()
|
||||
{
|
||||
return _device.AnalogRead(_pin);
|
||||
}
|
||||
|
||||
public double Voltage()
|
||||
{
|
||||
return Math.Round(((float) SensorValue()*Constants.AdcVoltage/1023), 2);
|
||||
}
|
||||
|
||||
public double Degrees()
|
||||
{
|
||||
return Math.Round((Voltage()*FullAngle)/Constants.GroveVcc, 2);
|
||||
}
|
||||
|
||||
public int Brightness()
|
||||
{
|
||||
return (int) (Degrees()/FullAngle*255);
|
||||
}
|
||||
}
|
||||
}
|
||||
38
Software/CSharp/GrovePi/Sensors/Sensor.cs
Normal file
38
Software/CSharp/GrovePi/Sensors/Sensor.cs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
using System;
|
||||
|
||||
namespace GrovePi.Sensors
|
||||
{
|
||||
public abstract class Sensor<TSensorType> where TSensorType : class
|
||||
{
|
||||
protected readonly IGrovePi Device;
|
||||
protected readonly Pin Pin;
|
||||
|
||||
internal Sensor(IGrovePi device, Pin pin, PinMode pinMode)
|
||||
{
|
||||
if (device == null) throw new ArgumentNullException(nameof(device));
|
||||
Device = device;
|
||||
Pin = pin;
|
||||
device.PinMode(Pin, pinMode);
|
||||
}
|
||||
|
||||
internal Sensor(IGrovePi device, Pin pin)
|
||||
{
|
||||
if (device == null) throw new ArgumentNullException(nameof(device));
|
||||
Device = device;
|
||||
Pin = pin;
|
||||
}
|
||||
|
||||
public SensorStatus CurrentState => (SensorStatus) Device.DigitalRead(Pin);
|
||||
|
||||
public TSensorType ChangeState(SensorStatus newState)
|
||||
{
|
||||
Device.DigitalWrite(Pin, (byte) newState);
|
||||
return this as TSensorType;
|
||||
}
|
||||
|
||||
public void AnalogWrite(byte value)
|
||||
{
|
||||
Device.AnalogWrite(Pin,value);
|
||||
}
|
||||
}
|
||||
}
|
||||
8
Software/CSharp/GrovePi/Sensors/SensorStatus.cs
Normal file
8
Software/CSharp/GrovePi/Sensors/SensorStatus.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
namespace GrovePi.Sensors
|
||||
{
|
||||
public enum SensorStatus
|
||||
{
|
||||
Off = 0,
|
||||
On = 1
|
||||
}
|
||||
}
|
||||
26
Software/CSharp/GrovePi/Sensors/SoundSensor.cs
Normal file
26
Software/CSharp/GrovePi/Sensors/SoundSensor.cs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface ISoundSensor
|
||||
{
|
||||
int SensorValue();
|
||||
}
|
||||
|
||||
internal class SoundSensor : Sensor<ISoundSensor>, ISoundSensor
|
||||
{
|
||||
|
||||
public SoundSensor(IGrovePi device, Pin pin) : base(device, pin, PinMode.Input)
|
||||
{
|
||||
}
|
||||
|
||||
public int SensorValue()
|
||||
{
|
||||
return Device.AnalogRead(Pin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
using System;
|
||||
|
||||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface ITemperatureAndHumiditySensor
|
||||
{
|
||||
double TemperatureInCelsius();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the model of sensor.
|
||||
/// DHT11 - blue one - comes with the GrovePi+ Starter Kit.
|
||||
/// DHT22 - white one, aka DHT Pro or AM2302.
|
||||
/// DHT21 - black one, aka AM2301.
|
||||
/// </summary>
|
||||
public enum Model
|
||||
{
|
||||
/*
|
||||
*/
|
||||
Dht11 = 3975,
|
||||
Dht21 = 4250,
|
||||
Dht22 = 4250
|
||||
}
|
||||
|
||||
internal class TemperatureAndHumiditySensor : ITemperatureAndHumiditySensor
|
||||
{
|
||||
private readonly IGrovePi _device;
|
||||
private readonly Model _model;
|
||||
private readonly Pin _pin;
|
||||
|
||||
internal TemperatureAndHumiditySensor(IGrovePi device, Pin pin, Model model)
|
||||
{
|
||||
if (device == null) throw new ArgumentNullException(nameof(device));
|
||||
_device = device;
|
||||
_pin = pin;
|
||||
_model = model;
|
||||
}
|
||||
|
||||
public double TemperatureInCelsius()
|
||||
{
|
||||
var result = (double) _device.AnalogRead(_pin);
|
||||
var resistance = (1023 - result)*10000/result;
|
||||
return 1/(Math.Log(resistance/10000)/(int) _model + 1/298.15) - 273.15;
|
||||
}
|
||||
}
|
||||
}
|
||||
33
Software/CSharp/GrovePi/Sensors/TemperatureSensor.cs
Normal file
33
Software/CSharp/GrovePi/Sensors/TemperatureSensor.cs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
using System;
|
||||
|
||||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface ITemperatureSensor
|
||||
{
|
||||
double TemperatureInCelsius();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Temperature Sensor V1.1 & 1.2
|
||||
/// ref: <http://wiki.seeed.cc/Grove-Temperature_Sensor_V1.2/>
|
||||
/// </summary>
|
||||
internal class TemperatureSensor : ITemperatureSensor
|
||||
{
|
||||
private readonly IGrovePi _device;
|
||||
private readonly Pin _pin;
|
||||
|
||||
internal TemperatureSensor(IGrovePi device, Pin pin)
|
||||
{
|
||||
if (device == null) throw new ArgumentNullException(nameof(device));
|
||||
_device = device;
|
||||
_pin = pin;
|
||||
}
|
||||
|
||||
public double TemperatureInCelsius()
|
||||
{
|
||||
var result = (double)_device.AnalogRead(_pin);
|
||||
var resistance = (1023 - result) * 10000 / result;
|
||||
return 1 / (Math.Log(resistance / 10000) / 4275 + 1 / 298.15) - 273.15;
|
||||
}
|
||||
}
|
||||
}
|
||||
40
Software/CSharp/GrovePi/Sensors/UltrasonicRangerSensor.cs
Normal file
40
Software/CSharp/GrovePi/Sensors/UltrasonicRangerSensor.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
using GrovePi.Common;
|
||||
|
||||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface IUltrasonicRangerSensor
|
||||
{
|
||||
int MeasureInCentimeters();
|
||||
}
|
||||
|
||||
internal class UltrasonicRangerSensor : IUltrasonicRangerSensor
|
||||
{
|
||||
private const byte CommandAddress = 7;
|
||||
private readonly GrovePi _device;
|
||||
private readonly Pin _pin;
|
||||
|
||||
internal UltrasonicRangerSensor(GrovePi device, Pin pin)
|
||||
{
|
||||
_device = device;
|
||||
_pin = pin;
|
||||
}
|
||||
|
||||
public int MeasureInCentimeters()
|
||||
{
|
||||
var buffer = new byte[4] {CommandAddress, (byte) _pin, Constants.Unused, Constants.Unused};
|
||||
var result = _device.DirectAccess.WritePartial(buffer);
|
||||
if (result.Status != Windows.Devices.I2c.I2cTransferStatus.FullTransfer)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
Delay.Milliseconds(50);
|
||||
buffer = new byte[3];
|
||||
result = _device.DirectAccess.ReadPartial(buffer);
|
||||
if (result.Status != Windows.Devices.I2c.I2cTransferStatus.FullTransfer)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return buffer[1]*256 + buffer[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Software/CSharp/GrovePi/Sensors/WaterAtomizer.cs
Normal file
14
Software/CSharp/GrovePi/Sensors/WaterAtomizer.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
namespace GrovePi.Sensors
|
||||
{
|
||||
public interface IWaterAtomizer
|
||||
{
|
||||
SensorStatus CurrentState { get; }
|
||||
IWaterAtomizer ChangeState(SensorStatus newState);
|
||||
}
|
||||
|
||||
internal class WaterAtomizer : Sensor<IWaterAtomizer>, IWaterAtomizer
|
||||
{
|
||||
public WaterAtomizer(IGrovePi device, Pin pin) : base(device, pin, PinMode.Output)
|
||||
{ }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue