Skip to content

Commit d3e5876

Browse files
committed
0.4.2 - corrections for ESP32
set better impuls treshold 70 -> 100 replace float in IRQ handler by int add IRAM_ATTR for irqhandler rename BIT0 and 1 to TBIT0 and 1
1 parent 7ae7e31 commit d3e5876

File tree

2 files changed

+59
-42
lines changed

2 files changed

+59
-42
lines changed

SensorT25.cpp

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,47 @@ v0.2.0 - copy from 0.1.9 and remove coments and debug data
77
v0.2.1 - add new funcion - enable, disable and remove some variables
88
v0.2.2 - rename data array to buffer, and use data array for data per chanel
99
v0.2.3 - save data directly to data array
10-
v0.2.4 - rename get functions to decode and create new getfunctions and some isFFF functions
10+
v0.2.4 - rename get functions to decode and create new getfunctions and some isFFF functions
1111
v0.2.5 - correct some daty types
1212
v0.2.6 - set prefix _ for global private objects
1313
v0.3.0 - do it like class
1414
v0.3.1 - change all to static
1515
v0.3.2 - rename class to SensorT25
1616
v0.4.0 - save it like library
17+
v0.4.1 - rename BIT0 and 1 to TBIT0 and 1, user IRAM_ATTR for IRQ handler
18+
v0.4.2 - ESP32 is not supporting float in IRQ handler, use int instead
1719
*/
1820

1921
#include <SensorT25.h>
2022

2123
unsigned long SensorT25::_buffer[BUFF_ROW] = {0L,0L,0L}; // raw data buffer
2224
boolean SensorT25::_valid[SENSCOUNT] = {false,false,false}; // valid dta forsensor
2325
byte SensorT25::_sid[SENSCOUNT]; // sensor ID
24-
float SensorT25::_temperature[SENSCOUNT]; // temperature
26+
//float SensorT25::_temperature[SENSCOUNT]; // temperature
27+
int SensorT25::_itemperature[SENSCOUNT]; // temperature
2528
unsigned long SensorT25::_time[SENSCOUNT]; // last set time
2629
boolean SensorT25::_start = LOW; // start data reading flag
27-
boolean SensorT25::_space = LOW; // space is starting
30+
boolean SensorT25::_space = LOW; // space is starting
2831
boolean SensorT25::_bit0 = LOW;
2932
boolean SensorT25::_bit1 = LOW;
3033
byte SensorT25::_bits = 0; // bits counter in data word
3134
byte SensorT25::_repeat = 0; // counter for repeated reading
3235
unsigned long SensorT25::_lastTime = 0; // variable for last time point
3336
byte SensorT25::_irq_pin; // number of used IRQ PIN
34-
// methods
35-
void SensorT25::enable(byte irq_pin) //enable data reading - INT0,INT1 name, number 2 or 3 for UNO
36-
{
37-
_irq_pin = irq_pin;
37+
// methods
38+
void SensorT25::enable(byte irq_pin) //enable data reading - INT0,INT1 name, number 2 or 3 for UNO
39+
{
40+
_irq_pin = irq_pin;
3841
pinMode(irq_pin, INPUT_PULLUP); // input for RF receiver
3942
attachInterrupt(digitalPinToInterrupt(irq_pin), _irqHandler, CHANGE); // IRQ handler for RF receiver
4043
}
41-
44+
4245
void SensorT25::disable(byte irq_pin) //disable daata reading
4346
{
4447
detachInterrupt(digitalPinToInterrupt(irq_pin)); // stop IRQ
4548
}
4649

47-
// are data valid for channel
50+
// are data valid for channel
4851
boolean SensorT25::isValid(byte channel)
4952
{
5053
return (isValidChannel(channel) ? _valid[channel] : false);
@@ -62,19 +65,20 @@ void SensorT25::setInValid(byte channel)
6265
//get sensor ID for channel
6366
byte SensorT25::getSID(byte channel)
6467
{
65-
return (isValidChannel(channel) ? _sid[channel] : 0);
68+
return (isValidChannel(channel) ? _sid[channel] : 0);
6669
}
6770

6871
//get temperature for channel
6972
float SensorT25::getTemperature(byte channel)
7073
{
71-
return (isValidChannel(channel) ? _temperature[channel] : 0);
74+
//return (isValidChannel(channel) ? _temperature[channel] : 0);
75+
return (isValidChannel(channel) ? float(_itemperature[channel])/10 : 0);
7276
}
7377

7478
//get temperature value age in s
7579
long SensorT25::getValueAge(byte channel)
7680
{
77-
return (isValidChannel(channel) ? (millis() - _time[channel])/1000 : 0);
81+
return (isValidChannel(channel) ? (millis() - _time[channel])/1000 : 0);
7882
}
7983

8084
// channel number validation
@@ -83,7 +87,7 @@ boolean SensorT25::isValidChannel(byte channel)
8387
return ( channel >= 0 && channel < SENSCOUNT);
8488
}
8589

86-
// return specified bits from sensor word
90+
// return specified bits from sensor word
8791
unsigned int SensorT25::_decodeBits(unsigned long buffer, unsigned long mask, byte shift )
8892
{
8993
return ((buffer & mask) >> shift);
@@ -102,69 +106,77 @@ byte SensorT25::_decodeChanel(unsigned long buffer)
102106
}
103107

104108
// decode temperature from data word
105-
float SensorT25::_decodeTemp(unsigned long buffer)
109+
// float SensorT25::_decodeTemp(unsigned long buffer)
110+
// {
111+
// int t = _decodeBits(buffer, TEM_MASK, TEM_SHIFT);
112+
// if (t > 2047) {t = t - 4095;}
113+
// return float(t)/10;
114+
// }
115+
116+
// decode temperature from data word
117+
int SensorT25::_idecodeTemp(unsigned long buffer)
106118
{
107119
int t = _decodeBits(buffer, TEM_MASK, TEM_SHIFT);
108120
if (t > 2047) {t = t - 4095;}
109-
return float(t)/10;
121+
return t;
110122
}
111123

112-
// IRQ handler for decode bits
124+
// IRQ handler for decode bits ...
113125
void SensorT25::_irqHandler()
114126
{
115127
unsigned long currentTime = micros(); // shot time
116128
int duration = currentTime - _lastTime; // calculate duration
117129
_lastTime = currentTime; // memory curent time
118130
boolean state = digitalRead(_irq_pin); // read input status
119-
if (!state) // goes from HIGH to LOW
120-
{
131+
if (!state) // goes from HIGH to LOW
132+
{
121133
_space = _isImpuls(duration - IMPULSE, ITRESHOLD); // if impuls time is valid
122-
}
134+
}
123135
else if (_space) // goes from LOW to HIGH
124-
{
136+
{
125137
if (!_start) // start flag is false
126-
{
138+
{
127139
_start = _isImpuls(duration - PAUSE, STRESHOLD); // start temperature data it was pause signal
128-
}
140+
}
129141
else // start flag is true
130142
{
131-
_bit0 = _isImpuls(duration - BIT0, STRESHOLD); // is it bit 0
132-
_bit1 = _isImpuls(duration - BIT1, STRESHOLD); // is it bit 1
143+
_bit0 = _isImpuls(duration - TBIT0, STRESHOLD); // is it bit 0
144+
_bit1 = _isImpuls(duration - TBIT1, STRESHOLD); // is it bit 1
133145
if(_bit0 ^ _bit1) // XOR / only one is true
134146
{
135147
_bit1 && bitSet(_buffer[_repeat],DATA_LONG - _bits - 1); // write bit 1
136148
_bit0 && bitClear(_buffer[_repeat],DATA_LONG - _bits - 1); // write bit 0
137149
_bits++; // increment bit counter
138-
}
139-
else // both or nothing bit
150+
}
151+
else // both or nothing bit
140152
{
141153
_resetTWord (); // reset reading
142154
}
143155
if(_bits >= DATA_LONG) // last bit was reading
144156
{
145157
if(_repeat > 0) // is it temp sentence hienr then 0
146158
{
147-
if (_buffer[_repeat] != _buffer[_repeat-1]) // current temp word is differnt than previous
159+
if (_buffer[_repeat] != _buffer[_repeat-1]) // current temp word is differnt than previous
148160
{
149161
_resetTWord (); // reset reading
150162
}
151163
}
152164
_repeat++; // increment temp word reading
153165
_newTWord(); // reset counters and flags
154166
}
155-
}
167+
}
156168
}
157-
169+
158170
// write valid data to data array
159171
if (_repeat >= BUFF_ROW) // buffer is full
160172
{
161173
byte ch = (_decodeChanel(_buffer[0])); // set channel
162174
_valid[ch] = true; // set data valid
163175
_sid[ch] = (_decodeSID(_buffer[0])); //sensor ID // save sensor ID
164-
_temperature[ch] = (_decodeTemp(_buffer[0])); // save temperature
176+
_itemperature[ch] = (_idecodeTemp(_buffer[0])); // save temperature
165177
_time[ch] = millis(); //save data timestamp
166178
_resetTWord (); // reset reading
167-
}
179+
}
168180
}
169181

170182
void SensorT25::_resetTWord () // reset reading
@@ -183,4 +195,4 @@ void SensorT25::_newTWord () // reset temp wo
183195
boolean SensorT25::_isImpuls(int duration, byte TRESHOLD) // is it valid impulse
184196
{
185197
return (abs(duration) <= TRESHOLD);
186-
}
198+
}

SensorT25.h

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ v0.2.0 - copy from 0.1.9 and remove coments and debug data
77
v0.2.1 - add new funcion - enable, disable and remove some variables
88
v0.2.2 - rename data array to buffer, and use data array for data per chanel
99
v0.2.3 - save data directly to data array
10-
v0.2.4 - rename get functions to decode and create new getfunctions and some isFFF functions
10+
v0.2.4 - rename get functions to decode and create new getfunctions and some isFFF functions
1111
v0.2.5 - correct some daty types
1212
v0.2.6 - set prefix _ for global private objects
1313
v0.3.0 - do it like class
1414
v0.3.1 - change all to static
1515
v0.3.2 - rename class to SensorT25
1616
v0.4.0 - save it like library
17+
v0.4.1 - rename BIT0 and 1 to TBIT0 and 1, user IRAM_ATTR for IRQ handler
18+
v0.4.2 - ESP32 is not supporting float in IRQ handler, use int for array
19+
v0.4.2 - treshold 100 for impuls
1720
*/
1821

1922
#ifndef SensorT25_h
@@ -24,10 +27,10 @@ v0.4.0 - save it like library
2427
#define SENSCOUNT 3 // Sensors count
2528
#define START 9000 // start space time
2629
#define PAUSE 4020 // space between sentences
27-
#define BIT1 1980 // 1
28-
#define BIT0 1000 // 0
30+
#define TBIT1 1980 // 1
31+
#define TBIT0 1000 // 0
2932
#define IMPULSE 410 // impulse
30-
#define ITRESHOLD 70 // impulse treshold +- for duration
33+
#define ITRESHOLD 100 // impulse treshold +- for duration
3134
#define STRESHOLD 50 // space treshold +- for duration
3235
#define BUFF_ROW 3 // row in buffer for data
3336
#define DATA_LONG 32 // buffer long 32bits, last 4 bits are droped
@@ -48,28 +51,30 @@ class SensorT25 {
4851
// sensors data - index 0 for channel 1,...
4952
static boolean _valid[SENSCOUNT]; // valid dta forsensor
5053
static byte _sid[SENSCOUNT]; // sensor ID
51-
static float _temperature[SENSCOUNT]; // temperature
54+
//static float _temperature[SENSCOUNT]; // temperature
55+
static int _itemperature[SENSCOUNT]; // temperature in integer (float * 10)
5256
static unsigned long _time[SENSCOUNT]; // last set time
5357
static boolean _start; // start data reading flag
54-
static boolean _space; // space is starting
58+
static boolean _space; // space is starting
5559
static boolean _bit0;
5660
static boolean _bit1;
5761
static byte _bits; // bits counter in data word
5862
static byte _repeat; // counter for repeated reading
5963
static unsigned long _lastTime; // variable for last time point
6064
static byte _irq_pin;
61-
65+
6266
static unsigned int _decodeBits(unsigned long, unsigned long, byte );
6367
static byte _decodeSID(unsigned long);
6468
static byte _decodeChanel(unsigned long);
65-
static float _decodeTemp(unsigned long);
69+
//static float _decodeTemp(unsigned long);
70+
static int _idecodeTemp(unsigned long);
6671
static void _irqHandler();
6772
static void _resetTWord (); // reset reading
6873
static void _newTWord (); // reset temp word counter
6974
static boolean _isImpuls(int, byte); // is it valid impulse
70-
75+
7176
public:
72-
static void enable(byte); //enable data reading - INT0,INT1 name, number 2 or 3 for UNO
77+
static void enable(byte); //enable data reading - INT0,INT1 name, number 2 or 3 for UNO
7378
static void disable(byte); //disable daata reading
7479
static boolean isValid(byte);
7580
static void setInValid(byte);

0 commit comments

Comments
 (0)