Skip to content

Commit 4cbb81f

Browse files
authored
Fix #7 refactor constructor (#8)
- refactor constructor/begin interface - breaking changes. - minimize conditional code. -- create SPI_CLASS macro to solve it. - update readme.md - update examples
1 parent b618654 commit 4cbb81f

File tree

12 files changed

+122
-147
lines changed

12 files changed

+122
-147
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@ 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.2.0] - 2023-11-28
10+
- refactor constructor/begin interface - breaking changes.
11+
- minimize conditional code. -- create SPI_CLASS macro to solve it.
12+
- update readme.md
13+
- update examples
14+
15+
----
16+
917
## [0.1.3] - 2023-11-11
1018
- update readme.md
1119
- fix keywords.txt
1220
- minor edits
1321

14-
1522
## [0.1.2] - 2022-11-16
1623
- add RP2040 in build-CI
1724
- add changelog.md

MAX6675.cpp

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: MAX6675.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.3
4+
// VERSION: 0.2.0
55
// PURPOSE: Arduino library for MAX6675 chip for K type thermocouple
66
// DATE: 2022-01-11
77
// URL: https://github.com/RobTillaart/MAX6675
@@ -10,54 +10,45 @@
1010
#include "MAX6675.h"
1111

1212

13-
MAX6675::MAX6675()
13+
// HW SPI
14+
MAX6675::MAX6675(uint8_t select, __SPI_CLASS__ * mySPI)
1415
{
16+
_select = select;
17+
_miso = 255;
18+
_clock = 255;
19+
_mySPI = mySPI;
20+
_hwSPI = true;
1521
}
1622

1723

18-
void MAX6675::begin(const uint8_t select)
24+
// SW SPI
25+
MAX6675::MAX6675(uint8_t select, uint8_t miso, uint8_t clock)
1926
{
20-
begin(255, select, 255);
27+
_select = select;
28+
_miso = miso;
29+
_clock = clock;
30+
_mySPI = NULL;
31+
_hwSPI = false;
2132
}
2233

2334

24-
void MAX6675::begin(const uint8_t clock, const uint8_t select, const uint8_t miso)
35+
void MAX6675::begin()
2536
{
26-
_clock = clock;
27-
_miso = miso;
28-
_select = select;
29-
_hwSPI = (_clock == 255);
30-
3137
_lastTimeRead = 0;
3238
_offset = 0;
3339
_status = STATUS_NOREAD;
3440
_temperature = MAX6675_NO_TEMPERATURE;
3541
_rawData = 0;
42+
3643
setSPIspeed(1000000);
3744

3845
pinMode(_select, OUTPUT);
3946
digitalWrite(_select, HIGH);
4047

4148
if (_hwSPI)
4249
{
43-
#if defined(ESP32)
44-
if (_useHSPI) // HSPI
45-
{
46-
mySPI = new SPIClass(HSPI);
47-
mySPI->end();
48-
mySPI->begin(14, 12, 13, _select); // CLK=14 MISO=12 MOSI=13
49-
}
50-
else // VSPI
51-
{
52-
mySPI = new SPIClass(VSPI);
53-
mySPI->end();
54-
mySPI->begin(18, 19, 23, _select); // CLK=18 MISO=19 MOSI=23
55-
}
56-
#else // generic hardware SPI
57-
mySPI = &SPI;
58-
mySPI->end();
59-
mySPI->begin();
60-
#endif
50+
_mySPI->end();
51+
_mySPI->begin();
6152
delay(1);
6253
}
6354
else
@@ -76,22 +67,6 @@ void MAX6675::setSPIspeed(uint32_t speed)
7667
};
7768

7869

79-
#if defined(ESP32)
80-
void MAX6675::setGPIOpins(uint8_t clock, uint8_t miso, uint8_t mosi, uint8_t select)
81-
{
82-
_clock = clock;
83-
_miso = miso;
84-
_select = select;
85-
pinMode(_select, OUTPUT);
86-
digitalWrite(_select, HIGH);
87-
88-
// disable SPI and enable again
89-
mySPI->end();
90-
mySPI->begin(clock, miso, mosi, select);
91-
}
92-
#endif
93-
94-
9570
uint8_t MAX6675::read()
9671
{
9772
// return value of _read() page 5 datasheet
@@ -126,26 +101,31 @@ uint8_t MAX6675::read()
126101
}
127102

128103

104+
///////////////////////////////////////////////////
105+
//
106+
// PRIVATE
107+
//
129108
uint32_t MAX6675::_read(void)
130109
{
131110
_rawData = 0;
132111
// DATA TRANSFER
133112
if (_hwSPI)
134113
{
135-
mySPI->beginTransaction(_spi_settings);
114+
_mySPI->beginTransaction(_spi_settings);
115+
// must be after mySPI->beginTransaction() - STM32 (#14 MAX31855_RT)
136116
digitalWrite(_select, LOW);
137-
_rawData = mySPI->transfer(0);
117+
_rawData = _mySPI->transfer(0);
138118
_rawData <<= 8;
139-
_rawData += mySPI->transfer(0);
119+
_rawData += _mySPI->transfer(0);
140120
digitalWrite(_select, HIGH);
141-
mySPI->endTransaction();
121+
_mySPI->endTransaction();
142122
}
143123
else // Software SPI
144124
{
145125
// split _swSPIdelay in equal dLow and dHigh
146126
// dLow should be longer one when _swSPIdelay = odd.
147127
uint16_t dHigh = _swSPIdelay / 2;
148-
uint16_t dLow = _swSPIdelay - dHigh;
128+
uint16_t dLow = _swSPIdelay - dHigh;
149129
digitalWrite(_select, LOW);
150130
for (int8_t i = 15; i >= 0; i--)
151131
{

MAX6675.h

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: MAX6675.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.3
5+
// VERSION: 0.2.0
66
// PURPOSE: Arduino library for MAX6675 chip for K type thermocouple
77
// DATE: 2022-01-12
88
// URL: https://github.com/RobTillaart/MAX6675
@@ -23,7 +23,16 @@
2323
#include "SPI.h"
2424

2525

26-
#define MAX6675_LIB_VERSION (F("0.1.3"))
26+
#define MAX6675_LIB_VERSION (F("0.2.0"))
27+
28+
#ifndef __SPI_CLASS__
29+
#if defined(ARDUINO_ARCH_RP2040)
30+
#define __SPI_CLASS__ SPIClassRP2040
31+
#else
32+
#define __SPI_CLASS__ SPIClass
33+
#endif
34+
#endif
35+
2736

2837
#define MAX6675_NO_TEMPERATURE -999
2938

@@ -45,13 +54,12 @@
4554
class MAX6675
4655
{
4756
public:
57+
// HW SPI
58+
MAX6675(uint8_t select, __SPI_CLASS__ * mySPI);
59+
// SW SPI
60+
MAX6675(uint8_t select, uint8_t miso, uint8_t clock);
4861

49-
MAX6675();
50-
51-
// HW SPI
52-
void begin(uint8_t select);
53-
// SW SPI
54-
void begin(uint8_t clock, uint8_t select, uint8_t miso);
62+
void begin();
5563

5664
// returns state - bit field: 0 = STATUS_OK
5765
uint8_t read();
@@ -73,18 +81,6 @@ class MAX6675
7381
uint16_t getSWSPIdelay() { return _swSPIdelay; };
7482

7583

76-
// ESP32 specific
77-
#if defined(ESP32)
78-
void selectHSPI() { _useHSPI = true; };
79-
void selectVSPI() { _useHSPI = false; };
80-
bool usesHSPI() { return _useHSPI; };
81-
bool usesVSPI() { return !_useHSPI; };
82-
83-
// to overrule ESP32 default hardware pins
84-
void setGPIOpins(uint8_t clock, uint8_t miso, uint8_t mosi, uint8_t select);
85-
#endif
86-
87-
8884
private:
8985
uint32_t _read();
9086

@@ -101,11 +97,8 @@ class MAX6675
10197

10298
uint16_t _swSPIdelay = 0;
10399
uint32_t _SPIspeed;
104-
SPIClass * mySPI;
105-
SPISettings _spi_settings;
106-
#if defined(ESP32)
107-
bool _useHSPI = true;
108-
#endif
100+
__SPI_CLASS__ * _mySPI;
101+
SPISettings _spi_settings;
109102
};
110103

111104

README.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ The library is tested with a breakout board with following pins:
4848
+---------------------+
4949
```
5050

51+
#### 0.2.0 Breaking change
52+
53+
The version 0.2.0 has breaking changes in the interface.
54+
The essence is removal of ESP32 specific code from the library.
55+
This makes it possible to support the ESP32-S3 and other processors in the future.
56+
Also it makes the library a bit simpler to maintain.
57+
58+
Note the order of the parameters of the software SPI constructor has changed in 0.2.0.
59+
60+
5161
#### Related
5262

5363
- https://github.com/RobTillaart/MAX6675
@@ -60,7 +70,7 @@ The library is tested with a breakout board with following pins:
6070

6171
#### Pins
6272

63-
Default pin connections. ESP32 can overrule with **setGPIOpins()**.
73+
Default pin connections.
6474

6575
| HW SPI | UNO | ESP32 VSPI | ESP32 HSPI | Notes
6676
|:---------|:-----:|:-----------:|:-----------:|:----------|
@@ -101,10 +111,10 @@ Tested with **MAX6675_test_HWSPI.ino**
101111

102112
#### Constructor
103113

104-
- **MAX6675()** create object.
105-
- **void begin(const uint8_t select)** set select pin => hardware SPI
106-
- **void begin(const uint8_t sclk, const uint8_t select, const uint8_t miso)**
107-
set CLOCK, SELECT and MISO pin => software SPI
114+
- **MAX6675(uint8_t select, SPIClassRP2040 \* mySPI)** hardware SPI R2040
115+
- **MAX6675(uint8_t select, SPIClass \* mySPI)** hardware SPI other
116+
- **MAX6675(uint8_t select, uint8_t miso, uint8_t clock)** software SPI
117+
- **void begin()** initialize internals
108118

109119

110120
#### Hardware SPI
@@ -118,15 +128,6 @@ Del is the time in micros added per bit. Even numbers keep the duty cycle of the
118128
- **uint16_t getSWSPIdelay()** get set value in micros.
119129

120130

121-
#### ESP32 specific
122-
123-
- **void selectHSPI()** must be called before **begin()**
124-
- **void selectVSPI()** must be called before **begin()**
125-
- **bool usesHSPI()**
126-
- **bool usesVSPI()**
127-
- **void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select)** to overrule ESP32 default hardware pins.
128-
129-
130131
#### Reading
131132

132133
To make a temperature reading call **read()**.
@@ -159,7 +160,8 @@ The library supports a fixed offset to calibrate the thermocouple.
159160
For this the functions **float getOffset()** and **void setOffset(float offset)** are available.
160161
This offset is "added" in the **getTemperature()** function.
161162

162-
Notes
163+
Notes
164+
- the offset can be positive or negative.
163165
- the offset used is a float, so decimals can be used.
164166
A typical usage is to call **setOffset(273.15)** to get ° Kelvin.
165167
- the offset can cause negative temperatures.
@@ -263,9 +265,10 @@ See examples
263265

264266
#### Must
265267

268+
- update and verify documentation (as it is copied from MAX31855 lib)
269+
266270
#### Should
267271

268-
- update and verify documentation (as it is copied from MAX31855 lib)
269272
- keep interface in sync with MAX31855 if possible.
270273

271274
#### Could

examples/Demo_getRawData/Demo_getRawData.ino

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,43 @@
55
// URL: https://github.com/RobTillaart/MAX6675
66

77

8-
#include "SPI.h"
98
#include "MAX6675.h"
109

1110

12-
#define MAXDO 7 // Defining the MISO pin
13-
#define MAXCS 6 // Defining the CS pin
14-
#define MAXCLK 5 // Defining the SCK pin
11+
const int dataPin = 7;
12+
const int clockPin = 6;
13+
const int selectPin = 5;
1514

1615

17-
MAX6675 thermocouple;
16+
MAX6675 thermoCouple(selectPin, dataPin, clockPin);
1817

1918

2019
void setup ()
2120
{
2221
Serial.begin(115200);
2322
Serial.println(__FILE__);
23+
Serial.print("MAX6675_LIB_VERSION: ");
24+
Serial.println(MAX6675_LIB_VERSION);
2425
Serial.println();
2526
delay(250);
2627

27-
thermocouple.begin(MAXCLK, MAXCS, MAXDO);
28+
thermoCouple.begin();
2829
}
2930

3031

3132
void loop ()
3233
{
33-
int status = thermocouple.read();
34+
int status = thermoCouple.read();
3435
if (status != STATUS_OK)
3536
{
3637
Serial.println("ERROR!");
3738
}
3839

39-
uint32_t value = thermocouple.getRawData(); // Read the raw Data value from the module
40+
// Read the raw Data value from the module
41+
uint32_t value = thermoCouple.getRawData();
4042
Serial.print("RAW:\t");
4143

42-
// Display the raw data value in BIN format
44+
// Display the raw data value in BIN format
4345
uint32_t mask = 0x80000000;
4446
for (int i = 0; i < 32; i++)
4547
{
@@ -50,11 +52,11 @@ void loop ()
5052
Serial.println();
5153

5254
Serial.print("TMP:\t");
53-
Serial.println(thermocouple.getTemperature(), 3);
55+
Serial.println(thermoCouple.getTemperature(), 3);
5456

5557
delay(100);
5658
}
5759

5860

59-
// -- END OF FILE --
61+
// -- END OF FILE --
6062

0 commit comments

Comments
 (0)