Front
|
Back
|
|
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Net.Http;
using
Windows.ApplicationModel.Background;
using
Windows.Devices.Gpio;
using
Windows.System.Threading;
using
System.Diagnostics;
using
System.Threading.Tasks;
using
AlexBackgroundApplication.ServiceReference1;
using
Windows.Devices.Enumeration;
using
Windows.Devices.I2c;
namespace
AlexBackgroundApplication
{
public
sealed
class
StartupTask
:
IBackgroundTask
{
BackgroundTaskDeferral
deferral;
private
GpioPin
pinR, pinG, pinB;
private
I2cDevice
i2cTemp;
private
const
byte
i2cTempAddress = 0x18;
byte
[] ReadBuf;
byte
[] WriteBuf;
double
tempc = 0;
double
tempf = 0;
public
void
Run(
IBackgroundTaskInstance
taskInstance)
{
deferral = taskInstance.GetDeferral();
Task
t = InitI2C();
t.Wait();
DateTime
startTime =
DateTime
.Now, endTime;
double
elapsedMillisecs;
pinR =
GpioController
.GetDefault().OpenPin(13);
pinR.SetDriveMode(
GpioPinDriveMode
.Output);
pinG =
GpioController
.GetDefault().OpenPin(26);
pinG.SetDriveMode(
GpioPinDriveMode
.Output);
pinB =
GpioController
.GetDefault().OpenPin(16);
pinB.SetDriveMode(
GpioPinDriveMode
.Output);
pinR.Write(
GpioPinValue
.Low);
pinG.Write(
GpioPinValue
.High);
pinB.Write(
GpioPinValue
.Low);
while
(
true
)
{
endTime =
DateTime
.Now;
elapsedMillisecs = ((
TimeSpan
)(endTime - startTime)).TotalMilliseconds;
if
(elapsedMillisecs > 5000)
{
if
(OWReset() ==
true
)
{
OWWriteByte(0xCC);
OWWriteByte(0x44);
delay();
if
(OWReadByte() != 0)
{
OWReset();
OWWriteByte(0xCC);
OWWriteByte(0xBE);
byte
test = OWReadByte();
int
testresult = (OWReadByte() << 8);
int
result = test | testresult;
tempc = (result / 16.00);
tempf = ((1.80 * (result / 16.00)) + 32.00);
if
(tempc > 23)
{
pinR.Write(
GpioPinValue
.High);
pinG.Write(
GpioPinValue
.Low);
pinB.Write(
GpioPinValue
.Low);
}
else
{
pinR.Write(
GpioPinValue
.Low);
pinG.Write(
GpioPinValue
.High);
pinB.Write(
GpioPinValue
.Low);
}
//Debug.WriteLine("C: " + tempc.ToString() + " ;F: " + tempf.ToString());
}
}
startTime =
DateTime
.Now;
}
}
}
private
async
Task
InitI2C()
{
try
{
var
settings =
new
I2cConnectionSettings
(i2cTempAddress);
settings.BusSpeed =
I2cBusSpeed
.FastMode;
string
aqs =
I2cDevice
.GetDeviceSelector();
var
dis =
await
DeviceInformation
.FindAllAsync(aqs);
i2cTemp =
await
I2cDevice
.FromIdAsync(dis[0].Id, settings);
if
(i2cTemp ==
null
)
{
return
;
}
}
catch
(
Exception
ex)
{
throw
new
Exception
(
"I2C Initialization Failed"
, ex);
}
}
private
bool
OWReset()
{
bool
ret =
true
;
byte
test;
ReadBuf =
new
byte
[1];
WriteBuf =
new
byte
[] { 0xB4 };
i2cTemp.WriteRead(WriteBuf, ReadBuf);
if
(ReadBuf[0] == 0)
return
false
;
while
(
true
)
{
i2cTemp.Read(ReadBuf);
test =
Convert
.ToByte(ReadBuf[0] | 0xFE);
if
(test == 0xFE)
break
;
}
i2cTemp.Read(ReadBuf);
test =
Convert
.ToByte(ReadBuf[0] | 0xFC);
if
(test != 0xFE)
ret =
false
;
return
ret;
}
private
bool
OWWriteByte(
byte
value)
{
bool
ret =
true
;
byte
[] buff =
new
byte
[2];
byte
test;
buff[0] = 0xE1;
//setReadPointerCommand
buff[1] = 0xF0;
//statusRegister
ReadBuf =
new
byte
[1];
i2cTemp.WriteRead(buff, ReadBuf);
if
(ReadBuf[0] == 0)
{
return
false
;
}
while
(
true
)
{
i2cTemp.Read(ReadBuf);
test =
Convert
.ToByte(ReadBuf[0] | 0xFE);
if
(test == 0xFE)
break
;
}
buff[0] = 0xA5;
//writeByteCommand
buff[1] = value;
ReadBuf =
new
byte
[1];
i2cTemp.WriteRead(buff, ReadBuf);
if
(ReadBuf[0] == 0)
{
return
false
;
}
while
(
true
)
{
i2cTemp.Read(ReadBuf);
test =
Convert
.ToByte(ReadBuf[0] | 0xFE);
if
(test == 0xFE)
break
;
}
return
ret;
}
private
byte
OWReadByte()
{
byte
result = 0x00;
byte
[] buff =
new
byte
[2];
byte
test;
bool
error =
false
;
buff[0] = 0xE1;
//setReadPointerCommand
buff[1] = 0xF0;
//statusRegister
ReadBuf =
new
byte
[1];
i2cTemp.WriteRead(buff, ReadBuf);
if
(ReadBuf[0] == 0)
{
error =
true
;
}
if
(error ==
false
)
{
while
(
true
)
{
i2cTemp.Read(ReadBuf);
test =
Convert
.ToByte(ReadBuf[0] | 0xFE);
if
(test == 0xFE)
break
;
}
ReadBuf =
new
byte
[1];
WriteBuf =
new
byte
[] { 0x96 };
i2cTemp.WriteRead(WriteBuf, ReadBuf);
if
(ReadBuf[0] == 0)
{
error =
true
;
}
if
(error ==
false
)
{
buff[0] = 0xE1;
//setReadPointerCommand
buff[1] = 0xE1;
//readDataRegister
ReadBuf =
new
byte
[1];
i2cTemp.WriteRead(buff, ReadBuf);
if
(ReadBuf[0] == 0)
{
error =
true
;
}
if
(error ==
false
)
{
i2cTemp.Read(ReadBuf);
result = ReadBuf[0];
}
}
}
return
result;
}
private
void
delay()
{
DateTime
startTime =
DateTime
.Now, endTime;
double
elapsedMillisecs;
while
(
true
)
{
endTime =
DateTime
.Now;
elapsedMillisecs = ((
TimeSpan
)(endTime - startTime)).TotalMilliseconds;
if
(elapsedMillisecs > 1000)
break
;
}
}
}
}
|
SKIP ROM [0xCC]
The master can use this command to address all devices on the bus simultaneously without sending out any ROM code information. For example, the master can make all DS18B20s on the bus perform simultaneous temperature conversions by issuing a Skip ROM command followed by a
Convert T [0x44] command.
Note that the
Read Scratchpad [0xBE] command can follow the Skip ROM command only if there is a single slave device on the bus. In this case, time is saved by allowing the master to read from the slave without sending the device's 64-bit ROM code. A Skip ROM command followed by a Read Scratchpad command will cause a data collision on the bus if there is more than one slave since multiple devices will attempt to transmit data simultaneously.
|
CONVERT T [0x44]
This command initiates a single temperature conversion. Following the conversion, the resulting thermal data is stored in the 2-byte temperature register in the scratchpad memory and the DS18B20 returns to its low-power idle state. If the device is being used in parasite power mode, within 10μs (max) after this command is issued the master must enable a strong pullup on the 1-Wire bus for the duration of the conversion (tCONV) as described in the Powering the DS18B20 section. If the DS18B20 is powered by an external supply, the master can issue read time slots after the Convert T command and the DS18B20 will respond by transmitting a 0 while the temperature conversion is in progress and a 1 when the conversion is done. In parasite power mode this notification technique cannot be used since the bus is pulled high by the strong pullup during the conversion.
|
READ SCRATCHPAD [0xBE]
This command allows the master to read the contents of the scratchpad. The data transfer starts with the least significant bit of byte 0 and continues through the scratchpad until the 9th byte (byte 8 – CRC) is read. The master may issue a reset to terminate reading at any time if only part of the scratchpad data is needed.
|
DS18B20
1W <-> DS2482-100 1W
PWR <-> +
GND <-> -
LED
GPIO(R) <-> GPIO13
GPIO(G) <-> GPIO26
GPIO(B) <-> GPIO16
GND <-> -
DS2482-100
GND <-> -
NC (Not connected)
VAUX (Not connected)
VCC <-> +
SDA <-> I2C1 SDA
SDL <-> I2C1 SDL
0V (5) (Not connected)
0V (4) (Not connected)
1W (3) <-> DS18B20 1W
V+ (2) (Not connected)
VX (1) (Not connected)
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Net.Http;
using
Windows.ApplicationModel.Background;
using
Windows.Devices.Gpio;
using
Windows.System.Threading;
using
System.Diagnostics;
using
System.Threading.Tasks;
using
AlexBackgroundApplication.ServiceReference1;
namespace
AlexBackgroundApplication
{
public
sealed
class
StartupTask
:
IBackgroundTask
{
BackgroundTaskDeferral
deferral;
private
GpioPin
pinR, pinG, pinB;
private
GpioPin
pinPI;
bool
found =
false
;
public
void
Run(
IBackgroundTaskInstance
taskInstance)
{
deferral = taskInstance.GetDeferral();
InitGPIO();
}
private
void
InitGPIO()
{
DateTime
startTimeDelay =
DateTime
.Now, endTimeDelay;
double
elapsedMillisecsDelay;
pinPI =
GpioController
.GetDefault().OpenPin(18);
pinPI.SetDriveMode(
GpioPinDriveMode
.Input);
pinR =
GpioController
.GetDefault().OpenPin(13);
pinR.SetDriveMode(
GpioPinDriveMode
.Output);
pinG =
GpioController
.GetDefault().OpenPin(26);
pinG.SetDriveMode(
GpioPinDriveMode
.Output);
pinB =
GpioController
.GetDefault().OpenPin(16);
pinB.SetDriveMode(
GpioPinDriveMode
.Output);
pinR.Write(
GpioPinValue
.High);
pinG.Write(
GpioPinValue
.Low);
pinB.Write(
GpioPinValue
.Low);
startTimeDelay =
DateTime
.Now;
while
(
true
)
{
endTimeDelay =
DateTime
.Now;
elapsedMillisecsDelay = ((
TimeSpan
)(endTimeDelay - startTimeDelay)).TotalMilliseconds;
if
(pinPI.Read() ==
GpioPinValue
.Low)
{
found =
true
;
}
if
(elapsedMillisecsDelay > 500)
{
if
(found ==
true
)
{
pinR.Write(
GpioPinValue
.Low);
pinG.Write(
GpioPinValue
.High);
pinB.Write(
GpioPinValue
.Low);
}
else
{
pinR.Write(
GpioPinValue
.High);
pinG.Write(
GpioPinValue
.Low);
pinB.Write(
GpioPinValue
.Low);
}
found =
false
;
startTimeDelay =
DateTime
.Now;
}
}
}
}
}
|
Shock switch
GND <-> -
PWR <-> +
GPIO <-> GPIO18
LED
GPIO(R) <-> GPIO13
GPIO(G) <-> GPIO26
GPIO(B) <-> GPIO16
GND <-> -
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Net.Http;
using
Windows.ApplicationModel.Background;
using
Windows.Devices.Gpio;
using
Windows.System.Threading;
using
System.Diagnostics;
using
System.Threading.Tasks;
using
AlexBackgroundApplication.ServiceReference1;
namespace
AlexBackgroundApplication
{
public
sealed
class
StartupTask
:
IBackgroundTask
{
BackgroundTaskDeferral
deferral;
private
GpioPin
pinR, pinG, pinB;
private
GpioPin
pinPI;
public
void
Run(
IBackgroundTaskInstance
taskInstance)
{
deferral = taskInstance.GetDeferral();
InitGPIO();
}
private
void
InitGPIO()
{
DateTime
startTimeDelay =
DateTime
.Now, endTimeDelay;
double
elapsedMillisecsDelay;
bool
found =
false
;
pinPI =
GpioController
.GetDefault().OpenPin(18);
pinPI.SetDriveMode(
GpioPinDriveMode
.Input);
pinR =
GpioController
.GetDefault().OpenPin(13);
pinR.SetDriveMode(
GpioPinDriveMode
.Output);
pinG =
GpioController
.GetDefault().OpenPin(26);
pinG.SetDriveMode(
GpioPinDriveMode
.Output);
pinB =
GpioController
.GetDefault().OpenPin(16);
pinB.SetDriveMode(
GpioPinDriveMode
.Output);
pinR.Write(
GpioPinValue
.High);
pinG.Write(
GpioPinValue
.Low);
pinB.Write(
GpioPinValue
.Low);
startTimeDelay =
DateTime
.Now;
while
(
true
)
{
endTimeDelay =
DateTime
.Now;
elapsedMillisecsDelay = ((
TimeSpan
)(endTimeDelay - startTimeDelay)).TotalMilliseconds;
//if (elapsedMillisecsDelay > 1000)
{
if
(pinPI.Read() ==
GpioPinValue
.High)
{
pinR.Write(
GpioPinValue
.Low);
pinG.Write(
GpioPinValue
.High);
pinB.Write(
GpioPinValue
.Low);
}
else
{
pinR.Write(
GpioPinValue
.High);
pinG.Write(
GpioPinValue
.Low);
pinB.Write(
GpioPinValue
.Low);
}
startTimeDelay =
DateTime
.Now;
}
}
}
}
}
|
Photo interrupter
GPIO <-> GPIO18
PWR <-> +
GND <-> -
LED
GPIO(R) <-> GPIO13
GPIO(G) <-> GPIO26
GPIO(B) <-> GPIO16
GND <-> -
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Net.Http;
using
Windows.ApplicationModel.Background;
using
Windows.Devices.Gpio;
using
Windows.System.Threading;
using
System.Diagnostics;
using
System.Threading.Tasks;
using
AlexBackgroundApplication.ServiceReference1;
namespace
AlexBackgroundApplication
{
public
sealed
class
StartupTask
:
IBackgroundTask
{
BackgroundTaskDeferral
deferral;
private
GpioPin
pinR, pinG, pinB;
private
GpioPin
pinPI;
public
void
Run(
IBackgroundTaskInstance
taskInstance)
{
deferral = taskInstance.GetDeferral();
InitGPIO();
}
private
void
InitGPIO()
{
pinPI =
GpioController
.GetDefault().OpenPin(18);
pinPI.SetDriveMode(
GpioPinDriveMode
.Input);
pinR =
GpioController
.GetDefault().OpenPin(13);
pinR.SetDriveMode(
GpioPinDriveMode
.Output);
pinG =
GpioController
.GetDefault().OpenPin(26);
pinG.SetDriveMode(
GpioPinDriveMode
.Output);
pinB =
GpioController
.GetDefault().OpenPin(16);
pinB.SetDriveMode(
GpioPinDriveMode
.Output);
pinR.Write(
GpioPinValue
.High);
pinG.Write(
GpioPinValue
.Low);
pinB.Write(
GpioPinValue
.Low);
while
(
true
)
{
if
(pinPI.Read() ==
GpioPinValue
.Low)
{
pinR.Write(
GpioPinValue
.Low);
pinG.Write(
GpioPinValue
.High);
pinB.Write(
GpioPinValue
.Low);
}
else
{
pinR.Write(
GpioPinValue
.High);
pinG.Write(
GpioPinValue
.Low);
pinB.Write(
GpioPinValue
.Low);
}
}
}
}
}
|
Button
GND <-> -
PWR <-> +
GPIO <-> GPIO18
LED
GPIO(R) <-> GPIO13
GPIO(G) <-> GPIO26
GPIO(B) <-> GPIO16
GND <-> -
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Net.Http;
using
Windows.ApplicationModel.Background;
using
Windows.Devices.Gpio;
using
Windows.System.Threading;
using
System.Diagnostics;
using
System.Threading.Tasks;
using
AlexBackgroundApplication.ServiceReference1;
namespace
AlexBackgroundApplication
{
public
sealed
class
StartupTask
:
IBackgroundTask
{
BackgroundTaskDeferral
deferral;
private
GpioPin
pinR, pinG, pinB;
private
GpioPin
pinIRTIRR;
bool
found =
false
;
public
void
Run(
IBackgroundTaskInstance
taskInstance)
{
deferral = taskInstance.GetDeferral();
InitGPIO();
}
private
void
InitGPIO()
{
DateTime
startTime =
DateTime
.Now, endTime;
double
elapsedMillisecs;
pinIRTIRR =
GpioController
.GetDefault().OpenPin(12);
pinIRTIRR.SetDriveMode(
GpioPinDriveMode
.Input);
pinR =
GpioController
.GetDefault().OpenPin(13);
pinR.SetDriveMode(
GpioPinDriveMode
.Output);
pinG =
GpioController
.GetDefault().OpenPin(26);
pinG.SetDriveMode(
GpioPinDriveMode
.Output);
pinB =
GpioController
.GetDefault().OpenPin(16);
pinB.SetDriveMode(
GpioPinDriveMode
.Output);
pinR.Write(
GpioPinValue
.High);
pinG.Write(
GpioPinValue
.Low);
pinB.Write(
GpioPinValue
.Low);
while
(
true
)
{
endTime =
DateTime
.Now;
elapsedMillisecs = ((
TimeSpan
)(endTime - startTime)).TotalMilliseconds;
//if (elapsedMillisecs > 500)
{
if
(pinIRTIRR.Read() ==
GpioPinValue
.Low)
{
pinR.Write(
GpioPinValue
.Low);
pinG.Write(
GpioPinValue
.High);
pinB.Write(
GpioPinValue
.Low);
}
else
{
pinR.Write(
GpioPinValue
.High);
pinG.Write(
GpioPinValue
.Low);
pinB.Write(
GpioPinValue
.Low);
}
startTime =
DateTime
.Now;
}
}
}
}
}
|
Obstacle detection sensor
NULL (Not connected)
GPIO <-> GPIO12
PWR <-> +
GND <-> -
LED
GPIO(R) <-> GPIO13
GPIO(G) <-> GPIO26
GPIO(B) <-> GPIO16
GND <-> -
|
using
System;
using
Windows.UI.Core;
using
Windows.UI.Popups;
using
Windows.UI.Xaml;
using
Windows.UI.Xaml.Controls;
using
Lego.Ev3.Core;
using
Lego.Ev3.WinRT;
using
AlexApp.Controls;
namespace
AlexApp
{
public
sealed
partial
class
MainPage
:
Page
{
private
Brick
_brick;
private
MotorControl
_selectedMotorControl;
private
SensorDataControl
_selectedSensorControl;
private
DateTime
time1 =
DateTime
.Now, time2;
private
TimeSpan
diff;
public
MainPage()
{
this
.InitializeComponent();
ConnControl.Visibility =
Visibility
.Visible;
}
private
async
void
TryToConnect(
object
sender,
RoutedEventArgs
e)
{
Overlay.Show(
"Connecting"
);
//ApplicationBar.IsVisible = false;
ConnControl.Visibility =
Visibility
.Visible;
var
conType = CreateConnection();
await
Dispatcher.RunAsync(
CoreDispatcherPriority
.Normal,
async
() =>
{
if
(conType !=
null
)
{
_brick =
new
Brick
(conType,
true
);
_brick.BrickChanged += _brick_BrickChanged;
try
{
await
_brick.ConnectAsync();
ConnControl.Visibility =
Visibility
.Collapsed;
ConnTypeRun.Text = ConnControl.GetConnectionType().ToString();
//ApplicationBar.IsVisible = true;
}
catch
(
Exception
)
{
new
MessageDialog
(
"Could not connect"
,
"Error"
).ShowAsync();
}
}
else
{
MessageDialog
dialog =
new
MessageDialog
(
"Invalid connection type for this device"
,
"Error"
);
await
dialog.ShowAsync();
}
Overlay.Hide();
});
}
private
async
void
_brick_BrickChanged(
object
sender,
BrickChangedEventArgs
e)
{
MotorA.Update(_brick);
this
.reportAsFinishedCycle();
}
private
async
void
reportAsFinishedCycle()
{
time2 =
DateTime
.Now;
diff = time2 - time1;
if
(diff.TotalSeconds > 1)
{
await
_brick.DirectCommand.TurnMotorAtPowerForTimeAsync(
OutputPort
.A, 25, 0, 100, 0,
false
);
time1 =
DateTime
.Now;
}
}
private
void
MotorSettingClicked(
object
sender,
RoutedEventArgs
routedEventArgs)
{
var
control = sender
as
MotorControl
;
if
(control !=
null
)
{
MotorSettings.SaveSettings += MotorSettings_SaveSettings;
_selectedMotorControl = control;
MotorSettings.Show(
control.BrickInputPort,
_brick.Ports[control.BrickInputPort].Type,
control.MotorMovementType,
control.DegreeMovement,
control.PowerRatingMovement,
control.TimeToMoveInSeconds);
}
}
private
void
MotorSettings_SaveSettings(
object
sender,
MotorSettingsEventArgs
e)
{
if
(_selectedMotorControl ==
null
)
return
;
MotorSettings.SaveSettings -= MotorSettings_SaveSettings;
_selectedMotorControl.MotorMovementType = e.MotorMovementType;
_selectedMotorControl.DegreeMovement = e.DegreeMovement;
_selectedMotorControl.PowerRatingMovement = e.PowerRatingMovement;
_selectedMotorControl.TimeToMoveInSeconds = e.TimeToMoveInSeconds;
_selectedMotorControl.UpdateUx();
_selectedMotorControl =
null
;
}
private
async
void
MotorPlayClicked(
object
sender,
RoutedEventArgs
e)
{
var
control = sender
as
MotorControl
;
if
(control !=
null
)
{
var
output = control.BrickOutputPort;
if
(control.MotorMovementType ==
MotorMovementTypes
.Degrees)
{
await
_brick.DirectCommand.StepMotorAtPowerAsync(output, control.PowerRatingMovement, 0, (
uint
)control.DegreeMovement, 0,
false
);
}
else
{
if
(control.TimeToMoveInSeconds == 0)
{
await
_brick.DirectCommand.TurnMotorAtPowerAsync(output, control.PowerRatingMovement);
}
else
{
await
_brick.DirectCommand.TurnMotorAtPowerForTimeAsync(output, control.PowerRatingMovement, 0, (
uint
)control.TimeToMoveInSeconds * 1000, 0,
false
);
}
}
}
}
private
void
SensorSettingClicked(
object
sender,
RoutedEventArgs
routedEventArgs)
{
var
control = sender
as
SensorDataControl
;
if
(control !=
null
)
{
SensorSettings.SaveSettings += SensorSettings_SaveSettings;
_selectedSensorControl = control;
SensorSettings.Show(
control.BrickInputPort,
_brick.Ports[control.BrickInputPort].Type,
_brick.Ports[control.BrickInputPort].Mode);
}
}
private
void
SensorSettings_SaveSettings(
object
sender,
SensorSettingsEventArgs
e)
{
if
(_selectedSensorControl ==
null
)
return
;
SensorSettings.SaveSettings -= SensorSettings_SaveSettings;
_brick.Ports[_selectedSensorControl.BrickInputPort].SetMode(e.SensorMode);
_selectedSensorControl.UpdateUx();
_selectedSensorControl =
null
;
}
private
ICommunication
CreateConnection()
{
ICommunication
returnType =
null
;
switch
(ConnControl.GetConnectionType())
{
case
ConnectionType
.Usb:
returnType =
new
UsbCommunication
();
break
;
case
ConnectionType
.Bluetooth:
returnType =
new
BluetoothCommunication
();
break
;
case
ConnectionType
.WiFi:
returnType =
new
NetworkCommunication
(ConnControl.GetIpAddress());
break
;
}
return
returnType;
}
}
}
|
Port A <-> Large Motor
Port B (Not connected)
Port C (Not connected)
Port D (Not connected)
Port 1 (Not connected)
Port 2 (Not connected)
Port 3 (Not connected)
Port 4 (Not connected)
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Net.Http;
using
Windows.ApplicationModel.Background;
using
Windows.Devices.Gpio;
using
Windows.System.Threading;
using
System.Diagnostics;
using
System.Threading.Tasks;
using
AlexBackgroundApplication.ServiceReference1;
namespace
AlexBackgroundApplication
{
public
sealed
class
StartupTask
:
IBackgroundTask
{
BackgroundTaskDeferral
deferral;
private
GpioPin
pinR, pinG, pinB;
private
GpioPin
pinOut;
private
GpioPin
pinS0, pinS1, pinS2, pinS3;
int
colorNone, colorRed, colorGreen, colorBlue;
int
colorNoneTotal, colorRedTotal, colorGreenTotal, colorBlueTotal;
string
color =
"None"
;
public
void
Run(
IBackgroundTaskInstance
taskInstance)
{
deferral = taskInstance.GetDeferral();
InitGPIO();
}
private
int
calcFrequency()
{
int
result = 0;
bool
found =
false
;
DateTime
startTime =
DateTime
.Now, endTime;
double
elapsedMillisecs = 0;
while
(
true
)
{
if
(pinOut.Read() ==
GpioPinValue
.Low)
{
found =
true
;
}
else
{
if
(found ==
true
)
{
result++;
found =
false
;
}
}
if
(elapsedMillisecs > 100)
break
;
endTime =
DateTime
.Now;
elapsedMillisecs = ((
TimeSpan
)(endTime - startTime)).TotalMilliseconds;
}
return
result;
}
private
void
InitGPIO()
{
DateTime
startTime =
DateTime
.Now, endTime;
double
elapsedMillisecs;
int
count = 0;
pinOut =
GpioController
.GetDefault().OpenPin(27);
pinOut.SetDriveMode(
GpioPinDriveMode
.Input);
pinS0 =
GpioController
.GetDefault().OpenPin(18);
pinS0.SetDriveMode(
GpioPinDriveMode
.Output);
pinS1 =
GpioController
.GetDefault().OpenPin(23);
pinS1.SetDriveMode(
GpioPinDriveMode
.Output);
pinS2 =
GpioController
.GetDefault().OpenPin(24);
pinS2.SetDriveMode(
GpioPinDriveMode
.Output);
pinS3 =
GpioController
.GetDefault().OpenPin(25);
pinS3.SetDriveMode(
GpioPinDriveMode
.Output);
pinR =
GpioController
.GetDefault().OpenPin(13);
pinR.SetDriveMode(
GpioPinDriveMode
.Output);
pinG =
GpioController
.GetDefault().OpenPin(26);
pinG.SetDriveMode(
GpioPinDriveMode
.Output);
pinB =
GpioController
.GetDefault().OpenPin(16);
pinB.SetDriveMode(
GpioPinDriveMode
.Output);
pinS0.Write(
GpioPinValue
.High);
pinS1.Write(
GpioPinValue
.Low);
pinR.Write(
GpioPinValue
.Low);
pinG.Write(
GpioPinValue
.Low);
pinB.Write(
GpioPinValue
.Low);
while
(
true
)
{
endTime =
DateTime
.Now;
elapsedMillisecs = ((
TimeSpan
)(endTime - startTime)).TotalMilliseconds;
if
(elapsedMillisecs > 5000)
{
count = 0;
while
(count < 10)
{
//Clear
pinS2.Write(
GpioPinValue
.High);
pinS3.Write(
GpioPinValue
.Low);
colorNone = calcFrequency();
//Red
pinS2.Write(
GpioPinValue
.Low);
pinS3.Write(
GpioPinValue
.Low);
colorRed = calcFrequency();
//Green
pinS2.Write(
GpioPinValue
.High);
pinS3.Write(
GpioPinValue
.High);
colorGreen = calcFrequency();
//Blue
pinS2.Write(
GpioPinValue
.Low);
pinS3.Write(
GpioPinValue
.High);
colorBlue = calcFrequency();
if
(colorRed > colorGreen && colorRed > colorBlue)
{
colorRedTotal++;
}
else
if
(colorGreen > colorRed && colorGreen > colorBlue)
{
colorGreenTotal++;
}
else
if
(colorBlue > colorRed && colorBlue > colorGreen)
{
colorBlueTotal++;
}
else
{
colorNoneTotal++;
}
count++;
}
if
(colorRedTotal > colorGreenTotal && colorRedTotal > colorBlueTotal)
{
color =
"Red"
;
pinR.Write(
GpioPinValue
.High);
pinG.Write(
GpioPinValue
.Low);
pinB.Write(
GpioPinValue
.Low);
}
else
if
(colorGreenTotal > colorRedTotal && colorGreenTotal > colorBlueTotal)
{
color =
"Green"
;
pinR.Write(
GpioPinValue
.Low);
pinG.Write(
GpioPinValue
.High);
pinB.Write(
GpioPinValue
.Low);
}
else
if
(colorBlueTotal > colorRedTotal && colorBlueTotal > colorGreenTotal)
{
color =
"Blue"
;
pinR.Write(
GpioPinValue
.Low);
pinG.Write(
GpioPinValue
.Low);
pinB.Write(
GpioPinValue
.High);
}
else
{
color =
"None"
;
pinR.Write(
GpioPinValue
.Low);
pinG.Write(
GpioPinValue
.Low);
pinB.Write(
GpioPinValue
.Low);
}
//Debug.WriteLine("R: " + colorRedTotal + " | G: " + colorGreenTotal + " | B: " + colorBlueTotal);
//Debug.WriteLine("Color: " + color);
count = 0;
colorNoneTotal = 0;
colorRedTotal = 0;
colorGreenTotal = 0;
colorBlueTotal = 0;
startTime =
DateTime
.Now;
}
}
}
}
}
|
TCS3200
S0 <-> GPIO18
S1 <-> GPIO23
S2 <-> GPIO24
S3 <-> GPIO25
LED (Not connected)
OUT <-> GPIO27
VCC <-> +
GND <-> -
LED
GPIO(R) <-> GPIO13
GPIO(G) <-> GPIO26
GPIO(B) <-> GPIO16
GND <-> -
|