11#include " EBF_STTS22H_TemperatureSensor.h"
2- #include " wiring_private.h " // pinPeripheral() function
3-
2+ #include " ../Core/EBF_Logic.h "
3+ # include " ../Core/EBF_DigitalInput.h "
44
55// Part of the code is based on the SparkFun_STTS22H_Arduino_Library
66// https://github.com/sparkfun/SparkFun_STTS22H_Arduino_Library/tree/main
@@ -14,8 +14,11 @@ extern void EBF_EmptyCallback();
1414uint8_t EBF_STTS22H_TemperatureSensor::Init (uint8_t i2cAddress, OperationMode mode)
1515{
1616 uint8_t rc;
17+ StatusRegister_t status;
1718
1819 this ->state = InstanceState::STATE_IDLE;
20+ this ->interruptAttached = 0 ;
21+ this ->postProcessingFlags = 0 ;
1922 this ->highThresholdSet = 0 ;
2023 this ->lowThresholdSet = 0 ;
2124 this ->i2cAddress = i2cAddress;
@@ -30,6 +33,12 @@ uint8_t EBF_STTS22H_TemperatureSensor::Init(uint8_t i2cAddress, OperationMode mo
3033 return rc;
3134 }
3235
36+ // Reading status register to reset the interrupt line
37+ rc = GetStatusRegister (status);
38+ if (rc != EBF_OK) {
39+ return rc;
40+ }
41+
3342 rc = SetOperationMode (mode);
3443 if (rc != EBF_OK) {
3544 return rc;
@@ -38,6 +47,78 @@ uint8_t EBF_STTS22H_TemperatureSensor::Init(uint8_t i2cAddress, OperationMode mo
3847 return EBF_OK;
3948}
4049
50+ uint8_t EBF_STTS22H_TemperatureSensor::AttachInterrupt (uint8_t interruptPin)
51+ {
52+ uint8_t rc;
53+ EBF_Logic *pLogic = EBF_Logic::GetInstance ();
54+
55+ rc = pLogic->AttachInterrupt (interruptPin, this , EBF_DigitalInput::InterruptMode::MODE_LOW);
56+ if (rc != EBF_OK) {
57+ return rc;
58+ }
59+
60+ interruptAttached = 1 ;
61+
62+ UpdatePollInterval ();
63+
64+ return EBF_OK;
65+ }
66+
67+ void EBF_STTS22H_TemperatureSensor::UpdatePollInterval ()
68+ {
69+ uint8_t needPolling = 0 ;
70+
71+ // No need to poll, unless some callbacks are needed and there is no interrupt attached
72+ pollIntervalMs = EBF_NO_POLLING;
73+
74+ if (onChangeCallback != NULL && onChangeCallback != EBF_EmptyCallback) {
75+ needPolling = 1 ;
76+ }
77+
78+ if (!interruptAttached) {
79+ if (onThresholdHigh != NULL && onThresholdHigh != EBF_EmptyCallback) {
80+ needPolling = 1 ;
81+ }
82+
83+ if (onThresholdLow != NULL && onThresholdLow != EBF_EmptyCallback) {
84+ needPolling = 1 ;
85+ }
86+ }
87+
88+ if (needPolling) {
89+ switch (operationMode)
90+ {
91+ case OperationMode::POWER_DOWN:
92+ pollIntervalMs = EBF_NO_POLLING;
93+ break ;
94+
95+ case OperationMode::MODE_ONE_SHOT:
96+ pollIntervalMs = 0 ;
97+ break ;
98+
99+ case OperationMode::MODE_1HZ:
100+ pollIntervalMs = 1000 ;
101+ break ;
102+
103+ case OperationMode::MODE_25HZ:
104+ pollIntervalMs = 1000 / 25 ;
105+ break ;
106+
107+ case OperationMode::MODE_50HZ:
108+ pollIntervalMs = 1000 / 50 ;
109+ break ;
110+
111+ case OperationMode::MODE_100HZ:
112+ pollIntervalMs = 1000 / 100 ;
113+ break ;
114+
115+ case OperationMode::MODE_200HZ:
116+ pollIntervalMs = 1000 / 200 ;
117+ break ;
118+ }
119+ }
120+ }
121+
41122uint8_t EBF_STTS22H_TemperatureSensor::GetControlRegister (ControlRegister_t &ctrl)
42123{
43124 uint8_t rc;
@@ -102,6 +183,8 @@ uint8_t EBF_STTS22H_TemperatureSensor::SetOperationMode(OperationMode mode)
102183 uint8_t rc;
103184 ControlRegister_t ctrl = {};
104185
186+ operationMode = mode;
187+
105188 // Any change should be done after the device is moved to power down mode
106189 ctrl.fields .freeRun = 0 ;
107190 ctrl.fields .mode_1Hz = 0 ;
@@ -110,31 +193,27 @@ uint8_t EBF_STTS22H_TemperatureSensor::SetOperationMode(OperationMode mode)
110193 return rc;
111194 }
112195
113- switch (mode )
196+ switch (operationMode )
114197 {
115198 case OperationMode::POWER_DOWN:
116199 state = InstanceState::STATE_IDLE;
117- pollIntervalMs = EBF_NO_POLLING;
118200 break ;
119201
120202 case OperationMode::MODE_ONE_SHOT:
121203 state = InstanceState::STATE_ONE_SHOT;
122- pollIntervalMs = 0 ;
123204
124205 ctrl.fields .oneShot = 1 ;
125206 break ;
126207
127208 case OperationMode::MODE_1HZ:
128209 state = InstanceState::STATE_MEASURING;
129- pollIntervalMs = 1000 ;
130210
131211 ctrl.fields .mode_1Hz = 1 ;
132212 ctrl.fields .freeRun = 0 ;
133213 break ;
134214
135215 case OperationMode::MODE_25HZ:
136216 state = InstanceState::STATE_MEASURING;
137- pollIntervalMs = 1000 / 25 ;
138217
139218 ctrl.fields .mode_1Hz = 0 ;
140219 ctrl.fields .freeRun = 1 ;
@@ -143,7 +222,6 @@ uint8_t EBF_STTS22H_TemperatureSensor::SetOperationMode(OperationMode mode)
143222
144223 case OperationMode::MODE_50HZ:
145224 state = InstanceState::STATE_MEASURING;
146- pollIntervalMs = 1000 / 50 ;
147225
148226 ctrl.fields .mode_1Hz = 0 ;
149227 ctrl.fields .freeRun = 1 ;
@@ -152,7 +230,6 @@ uint8_t EBF_STTS22H_TemperatureSensor::SetOperationMode(OperationMode mode)
152230
153231 case OperationMode::MODE_100HZ:
154232 state = InstanceState::STATE_MEASURING;
155- pollIntervalMs = 1000 / 100 ;
156233
157234 ctrl.fields .mode_1Hz = 0 ;
158235 ctrl.fields .freeRun = 1 ;
@@ -161,7 +238,6 @@ uint8_t EBF_STTS22H_TemperatureSensor::SetOperationMode(OperationMode mode)
161238
162239 case OperationMode::MODE_200HZ:
163240 state = InstanceState::STATE_MEASURING;
164- pollIntervalMs = 1000 / 200 ;
165241
166242 ctrl.fields .mode_1Hz = 0 ;
167243 ctrl.fields .freeRun = 1 ;
@@ -174,6 +250,8 @@ uint8_t EBF_STTS22H_TemperatureSensor::SetOperationMode(OperationMode mode)
174250 SetControlRegister (ctrl);
175251 }
176252
253+ UpdatePollInterval ();
254+
177255 return EBF_OK;
178256}
179257
@@ -237,6 +315,19 @@ uint8_t EBF_STTS22H_TemperatureSensor::Process()
237315 float change;
238316 StatusRegister_t status;
239317
318+ // Process interrupt detected logic
319+ if (postProcessingFlags) {
320+ if (postProcessingFlags & postProcessHighThreshold) {
321+ onThresholdHigh ();
322+ }
323+
324+ if (postProcessingFlags & postProcessLowThreshold) {
325+ onThresholdLow ();
326+ }
327+
328+ postProcessingFlags = 0 ;
329+ }
330+
240331 switch (state)
241332 {
242333 case InstanceState::STATE_IDLE:
@@ -282,6 +373,30 @@ uint8_t EBF_STTS22H_TemperatureSensor::Process()
282373 return EBF_OK;
283374}
284375
376+ void EBF_STTS22H_TemperatureSensor::ProcessInterrupt ()
377+ {
378+ StatusRegister_t status;
379+
380+ // Status register is cleared on read
381+ GetStatusRegister (status);
382+
383+ // Set the relevant post-processing flags
384+ if (status.fields .overThreshold ) {
385+ postProcessingFlags |= postProcessHighThreshold;
386+ }
387+
388+ if (status.fields .underThreshold ) {
389+ postProcessingFlags |= postProcessLowThreshold;
390+ }
391+
392+ if (postProcessingFlags) {
393+ // Pass the control back to EBF, so it will call the Process() function from normal run
394+ EBF_Logic *pLogic = EBF_Logic::GetInstance ();
395+
396+ pLogic->ProcessInterrupt (this );
397+ }
398+ }
399+
285400// Sets high threshold value
286401uint8_t EBF_STTS22H_TemperatureSensor::SetThresholdHigh (float temp)
287402{
0 commit comments