-
Notifications
You must be signed in to change notification settings - Fork 8
/
GasBreakout.cpp
147 lines (119 loc) · 4.24 KB
/
GasBreakout.cpp
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
145
146
147
#include "GasBreakout.h"
////////////////////////////////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS/DESTRUCTOR
////////////////////////////////////////////////////////////////////////////////////////////////////
GasBreakout::GasBreakout(TwoWire& wire, uint8_t address, float brightness, uint32_t timeout, int8_t interruptPin, bool debug)
: _ioe(wire, address, timeout, interruptPin, debug)
, _brightness(brightness)
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// METHODS
////////////////////////////////////////////////////////////////////////////////////////////////////
bool GasBreakout::initialise(bool skipChipIdCheck)
{
bool bSucceeded = false;
if(_ioe.initialise(skipChipIdCheck))
{
_ioe.setMode(MICS6814_RED, IOExpander::PIN_ADC);
_ioe.setMode(MICS6814_NH3, IOExpander::PIN_ADC);
_ioe.setMode(MICS6814_OX, IOExpander::PIN_ADC);
_ioe.setMode(PIN_HEATER_EN, IOExpander::PIN_OUT);
_ioe.output(PIN_HEATER_EN, LOW);
setBrightness(_brightness);
_ioe.setPwmControl(2); //PWM as fast as we can to avoid LED flicker
_ioe.setMode(PIN_RED, IOExpander::PIN_PWM, false, INVERT_OUTPUT);
_ioe.setMode(PIN_GREEN, IOExpander::PIN_PWM, false, INVERT_OUTPUT);
_ioe.setMode(PIN_BLUE, IOExpander::PIN_PWM, false, INVERT_OUTPUT);
bSucceeded = true;
}
return bSucceeded;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void GasBreakout::setAddr(uint8_t i2cAddr)
{
_ioe.setAddr(i2cAddr);
}
void GasBreakout::setPwmPeriod(uint16_t value, bool load)
{
_pwm_period = value;
_ioe.setPwmPeriod(_pwm_period, load);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void GasBreakout::setBrightness(float brightness)
{
_brightness = constrain(brightness, 0.01f, 1.0f);
//Calculate a period large enough to get 0-255 steps at the desired brightness
unsigned int period = (unsigned int)(255.0f / _brightness);
setPwmPeriod(period);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void GasBreakout::setRGB(uint8_t r, uint8_t g, uint8_t b)
{
_ioe.output(PIN_RED, r, false); //Hold off pwm load until the last
_ioe.output(PIN_GREEN, g, false); //Hold off pwm load until the last
_ioe.output(PIN_BLUE, b); //Loads all 3 pwms
}
void GasBreakout::setSingle(uint8_t pin, uint8_t v)
{
uint16_t value = (v * _pwm_period / 255.0f );
_ioe.output(pin, value);
}
void GasBreakout::setR(uint8_t r)
{
setSingle(PIN_RED, r);
}
void GasBreakout::setG(uint8_t g)
{
setSingle(PIN_GREEN, g);
}
void GasBreakout::setB(uint8_t b)
{
setSingle(PIN_BLUE, b);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void GasBreakout::setHeater(bool value)
{
uint16_t state = (value)?LOW:HIGH;
_ioe.output(PIN_HEATER_EN, state);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
float GasBreakout::readGas(byte channel, uint32_t adcTimeout)
{
float vref = _ioe.getAdcVref();
float vgas = _ioe.inputAsVoltage(channel, adcTimeout);
float res = 0;
if (vref != vgas)
res = (vgas * 56000) / (vref - vgas);
return res;
}
GasBreakout::Reading GasBreakout::readAll(uint32_t adcTimeout)
{
GasBreakout::Reading reading;
reading.reducing = readReducing(adcTimeout);
reading.nh3 = readNH3(adcTimeout);
reading.oxidising = readOxidising(adcTimeout);
reading.ref = readRef(adcTimeout);
return reading;
}
float GasBreakout::readReducing(uint32_t adcTimeout)
{
return readGas(MICS6814_RED, adcTimeout);
}
float GasBreakout::readNH3(uint32_t adcTimeout)
{
return readGas(MICS6814_NH3, adcTimeout);
}
float GasBreakout::readOxidising(uint32_t adcTimeout)
{
return readGas(MICS6814_OX, adcTimeout);
}
float GasBreakout::readRef(uint32_t adcTimeout)
{
float ref = _ioe.inputAsVoltage(MICS6814_VREF, adcTimeout);
if (ref == -1)
ref = 0;
return ref;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////