-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application.cs
144 lines (118 loc) · 4.31 KB
/
Application.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using HumidityControl.LcdSerialBackpack;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System;
using System.IO.Ports;
using System.Threading;
namespace HumidityControl
{
public sealed class Application : IDisposable
{
// Half screen
private const int half = 11;
// Devices:
private readonly AnalogInput trimmer = new AnalogInput(AnalogChannels.ANALOG_PIN_A0);
private readonly Fan fan = new Fan(PWMChannels.PWM_PIN_D9, Pins.GPIO_PIN_D8);
private readonly SerialPort screenPort = new SerialPort(SerialPorts.COM2, (int)BaudRates.Baud115200, Parity.None, 8, StopBits.One);
private readonly LcdScreen screen;
private readonly I2CDevice i2c = new I2CDevice(new I2CDevice.Configuration(Bme680.Bme680.SecondaryI2CAddress, 100 /* kHz */));
private readonly Bme680.Bme680 bme680;
// User interface:
private readonly Label temperatureLabel, humidityLabel, pressureLabel, fanPowerLabel, fanSpeedLabel;
public Application()
{
screen = new LcdScreen(screenPort);
bme680 = new Bme680.Bme680(i2c);
temperatureLabel = new Label(screen, LcdScreen.LastColumn - 1, 6, Label.TextAlign.Right);
humidityLabel = new Label(screen, LcdScreen.LastColumn - 1, 4, Label.TextAlign.Right);
pressureLabel = new Label(screen, LcdScreen.LastColumn - 3, 2, Label.TextAlign.Right);
fanPowerLabel = new Label(screen, LcdScreen.LastColumn - 1, 1, Label.TextAlign.Right);
fanSpeedLabel = new Label(screen, LcdScreen.LastColumn - 3, 0, Label.TextAlign.Right);
}
public void Run()
{
// Screen setup
screenPort.Open();
// Fan setup
fan.Start();
// BME680 setup
bme680.HumidityOversampling = Bme680.Oversampling.x2;
bme680.TemperatureOversampling = Bme680.Oversampling.x8;
bme680.PressureOversampling = Bme680.Oversampling.x4;
bme680.IirFilterSize = Bme680.IirFilterSize.C3;
// Draw screen:
screen.SetBacklightDutyCycle(100);
screen.ClearScreen();
screen.TextAt(half, 7).Draw("Temp.");
screen.TextAt(LcdScreen.LastColumn, 6).Draw("C");
screen.TextAt(half, 5).Draw("Rel. hum.");
screen.TextAt(LcdScreen.LastColumn, 4).Draw("%");
screen.TextAt(half, 3).Draw("Pressure");
screen.TextAt(LcdScreen.LastColumn - 2, 2).Draw("hPa");
screen.TextAt(half, 1).Draw("Fan:");
screen.TextAt(LcdScreen.LastColumn, 1).Draw("%");
screen.TextAt(LcdScreen.LastColumn - 2, 0).Draw("RPM");
// Await screen start up
Thread.Sleep(500);
// Read sensor and print data to screen
while (true)
{
try
{
loop();
}
catch (Exception e)
{
Debug.Print("Exception caught: " + e);
Thread.Sleep(1000);
}
}
}
private void loop()
{
bme680.PowerMode = Bme680.PowerMode.Forced;
Thread.Sleep(Program.Clamp(bme680.MeasurementDuration.Milliseconds, 400, 1000));
if (bme680.HasNewData)
{
Debug.Print("Righteous update.");
temperatureLabel.Text = bme680.Temperature.ToString("F3");
humidityLabel.Text = bme680.Humidity.ToString("F3");
pressureLabel.Text = (bme680.Pressure / 100.0).ToString("F1");
}
else
{
Debug.Print("Sterile update.");
}
double fanControl = FanControl;
// Sets fan speed through PWM
fan.SpeedControl = fanControl;
fanPowerLabel.Text = (fanControl * 100.0).ToString("F0");
fanSpeedLabel.Text = fan.Speed.ToString("F1");
temperatureLabel.Draw();
humidityLabel.Draw();
pressureLabel.Draw();
temperatureLabel.Draw();
fanPowerLabel.Draw();
fanSpeedLabel.Draw();
}
/// <summary>
/// Reads the fan speed indicated by the user through a trimmer. Values range between zero and one.
/// </summary>
private double FanControl
{
get
{
double fanControl = trimmer.Read();
return double.IsNaN(fanControl) ? 0.0 : Program.Clamp(fanControl, 0.0, 1.0);
}
}
public void Dispose()
{
trimmer.Dispose();
fan.Dispose();
screenPort.Dispose();
i2c.Dispose();
}
}
}