Skip to content

Commit 584380b

Browse files
committed
0.3.2 MTP40C
1 parent e55d876 commit 584380b

File tree

9 files changed

+163
-59
lines changed

9 files changed

+163
-59
lines changed

libraries/MTP40C/CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.3.2] - 2025-06-13
10+
- fix #11, sync MTP40F (PR 6)
11+
- add constructor
12+
- add setStream(Stream stream)
13+
- set default airPressureReference = 1013 hPa.
14+
- set default setSelfCalibrationHours = 168 hours.
15+
- update/sync readme.md
16+
- minor edits
17+
918
## [0.3.1] - 2023-11-14
1019
- update readme.md
1120

12-
1321
## [0.3.0] - 2023-07-30
1422
- replace CRC with other algorithm for AVR
1523
- less RAM, less code, faster (AVR only)

libraries/MTP40C/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021-2024 Rob Tillaart
3+
Copyright (c) 2021-2025 Rob Tillaart
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

libraries/MTP40C/MTP40C.cpp

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// FILE: MTP40C.cpp
33
// AUTHOR: Rob Tillaart
44
// DATE: 2021-08-20
5-
// VERSION: 0.3.1
5+
// VERSION: 0.3.2
66
// PURPOSE: Arduino library for MTP40C MTP40D CO2 sensor
77
// URL: https://github.com/RobTillaart/MTP40C
88

@@ -14,15 +14,33 @@
1414
// #define MTP40_DEBUG 1
1515

1616

17-
1817
MTP40::MTP40(Stream * stream)
1918
{
19+
init();
2020
_ser = stream;
21+
}
22+
23+
24+
MTP40::MTP40()
25+
{
26+
init();
27+
}
28+
29+
30+
void MTP40::init()
31+
{
2132
_buffer[0] = '\0';
2233
_type = 0xFF;
2334
}
2435

2536

37+
void MTP40::setStream(Stream * stream)
38+
{
39+
init();
40+
_ser = stream;
41+
}
42+
43+
2644
bool MTP40::begin(uint8_t address)
2745
{
2846
if (address > 247) return false;
@@ -273,8 +291,12 @@ int MTP40::lastError()
273291
//
274292
// PROTECTED
275293
//
276-
bool MTP40::request(uint8_t *data, uint8_t commandLength, uint8_t answerLength)
294+
bool MTP40::request(uint8_t *data, uint8_t commandLength, uint8_t responseLength)
277295
{
296+
if (_ser == NULL) {
297+
_lastError = MTP40_NO_STREAM;
298+
return false;
299+
}
278300
// generic or specific address
279301
if (_useAddress)
280302
{
@@ -302,17 +324,18 @@ bool MTP40::request(uint8_t *data, uint8_t commandLength, uint8_t answerLength)
302324

303325
uint32_t start = millis();
304326
uint8_t i = 0;
305-
while (answerLength)
327+
while (responseLength)
306328
{
307329
if (millis() - start > _timeout) return false;
308330
if (_ser->available())
309331
{
310332
_buffer[i] = _ser->read();
311333
i++;
312-
answerLength--;
334+
responseLength--;
313335
}
314336
yield(); // because baud rate is low!
315337
}
338+
// Add MTP40_INVALID_CRC response check here?
316339
return true;
317340
}
318341

@@ -452,14 +475,28 @@ const uint8_t auchCRCLo[] = {
452475

453476
/////////////////////////////////////////////////////////////
454477
//
455-
// DERIVED CLASSES
478+
// MTP40C DERIVED CLASS
456479
//
480+
MTP40C::MTP40C() : MTP40()
481+
{
482+
_type = 2;
483+
};
484+
457485
MTP40C::MTP40C(Stream * str) : MTP40(str)
458486
{
459487
_type = 2;
460488
};
461489

462490

491+
/////////////////////////////////////////////////////////////
492+
//
493+
// MTP40D DERIVED CLASS
494+
//
495+
MTP40D::MTP40D() : MTP40()
496+
{
497+
_type = 3;
498+
};
499+
463500
MTP40D::MTP40D(Stream * str) : MTP40(str)
464501
{
465502
_type = 3;

libraries/MTP40C/MTP40C.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// FILE: MTP40C.h
44
// AUTHOR: Rob Tillaart
55
// DATE: 2021-08-20
6-
// VERSION: 0.3.1
6+
// VERSION: 0.3.2
77
// PURPOSE: Arduino library for MTP40C + MTP40D CO2 sensor
88
// URL: https://github.com/RobTillaart/MTP40C
99
//
@@ -13,36 +13,41 @@
1313
#include "Arduino.h"
1414

1515

16-
#define MTP40_LIB_VERSION (F("0.3.1"))
16+
#define MTP40_LIB_VERSION (F("0.3.2"))
1717

1818

1919
#define MTP40_DEFAULT_ADDRESS 0x64
2020

2121
#define MTP40_OK 0x00
2222
#define MTP40_INVALID_AIR_PRESSURE 0x01
2323
#define MTP40_INVALID_GAS_LEVEL 0x02
24+
#define MTP40_INVALID_CRC 0x10
25+
#define MTP40_NO_STREAM 0x20
2426
#define MTP40_INVALID_ADDRESS 0xFF
2527
#define MTP40_REQUEST_FAILED 0xFFFF
2628

2729

2830
class MTP40
2931
{
3032
public:
33+
MTP40();
3134
MTP40(Stream * stream);
3235

36+
void setStream(Stream * stream);
37+
3338
bool begin(uint8_t address = MTP40_DEFAULT_ADDRESS);
3439
bool isConnected();
3540

3641
uint8_t getAddress();
3742
bool setAddress(uint8_t address = MTP40_DEFAULT_ADDRESS);
3843

3944
float getAirPressureReference();
40-
bool setAirPressureReference(float apr);
45+
bool setAirPressureReference(float apr = 1013); // 1 Atm default
4146

4247
uint16_t getGasConcentration(); // returns PPM
4348

44-
void suppressError(bool se) { _suppressError = se; };
45-
bool getSuppressError() { return _suppressError; };
49+
void suppressError(bool suppress) { _suppressError = suppress; };
50+
bool getSuppressError() { return _suppressError; };
4651

4752
// CALIBRATION FUNCTIONS
4853
// READ DATASHEET !!
@@ -52,7 +57,7 @@ class MTP40
5257
bool openSelfCalibration();
5358
bool closeSelfCalibration();
5459
uint16_t getSelfCalibrationStatus();
55-
bool setSelfCalibrationHours(uint16_t hours);
60+
bool setSelfCalibrationHours(uint16_t hours = 168);
5661
uint16_t getSelfCalibrationHours();
5762

5863
void setGenericAddress() { _useAddress = false; };
@@ -77,7 +82,7 @@ class MTP40
7782
// PROTECTED
7883
//
7984
protected:
80-
Stream * _ser;
85+
Stream * _ser = NULL;
8186
uint8_t _buffer[24]; // should be big enough.
8287
uint8_t _address = MTP40_DEFAULT_ADDRESS;
8388

@@ -92,7 +97,8 @@ class MTP40
9297
bool _suppressError = false;
9398
int _lastError = MTP40_OK;
9499

95-
bool request(uint8_t *data, uint8_t cmdlen, uint8_t anslen);
100+
void init();
101+
bool request(uint8_t *data, uint8_t commandLength, uint8_t responseLength);
96102

97103
uint16_t CRC(uint8_t *data, uint16_t len);
98104

@@ -111,18 +117,20 @@ class MTP40
111117
class MTP40C : public MTP40
112118
{
113119
public:
120+
MTP40C();
114121
MTP40C(Stream * str);
115122
};
116123

117124

118125
class MTP40D : public MTP40
119126
{
120127
public:
128+
MTP40D();
121129
MTP40D(Stream * str);
122130

123131
// TODO
124132
// I2C interface
125-
// PWM interface
133+
// PWM interface
126134
};
127135

128136

0 commit comments

Comments
 (0)