Skip to content

Commit

Permalink
Fix #29 refactor constructor (#30)
Browse files Browse the repository at this point in the history
- refactor constructor/begin interface - breaking changes.
  - minimize conditional code. -- create SPI_CLASS macro to solve it.
- update readme.md
- update examples
  • Loading branch information
RobTillaart authored Nov 29, 2023
1 parent ce75bad commit a35c109
Show file tree
Hide file tree
Showing 22 changed files with 315 additions and 343 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.4.4] - 2023-11-11
## [0.5.0] - 2023-11-29
- refactor constructor/begin interface - breaking changes.
- minimize conditional code. -- create SPI_CLASS macro to solve it.
- update readme.md
- update examples

----

## [0.4.4] - 2023-11-11
- update readme.md

## [0.4.3] - 2023-02-26
- update readme.md
Expand Down
71 changes: 23 additions & 48 deletions MAX31855.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: MAX31855.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.4.4
// VERSION: 0.5.0
// PURPOSE: Arduino library for MAX31855 chip for K type thermocouple
// DATE: 2014-01-01
// URL: https://github.com/RobTillaart/MAX31855_RT
Expand All @@ -10,24 +10,30 @@
#include "MAX31855.h"


MAX31855::MAX31855()
// HW SPI
MAX31855::MAX31855(uint8_t select, __SPI_CLASS__ * mySPI)
{
_select = select;
_miso = 255;
_clock = 255;
_mySPI = mySPI;
_hwSPI = true;
}


void MAX31855::begin(const uint8_t select)
// SW SPI
MAX31855::MAX31855(uint8_t select, uint8_t miso, uint8_t clock)
{
begin(255, select, 255);
_select = select;
_miso = miso;
_clock = clock;
_mySPI = NULL;
_hwSPI = false;
}


void MAX31855::begin(const uint8_t clock, const uint8_t select, const uint8_t miso)
void MAX31855::begin()
{
_clock = clock;
_miso = miso;
_select = select;
_hwSPI = (_clock == 255);

_lastTimeRead = 0;
_offset = 0;
_SeebeckC = K_TC;
Expand All @@ -42,24 +48,8 @@ void MAX31855::begin(const uint8_t clock, const uint8_t select, const uint8_t mi

if (_hwSPI)
{
#if defined(ESP32)
if (_useHSPI) // HSPI
{
mySPI = new SPIClass(HSPI);
mySPI->end();
mySPI->begin(14, 12, 13, _select); // CLK=14 MISO=12 MOSI=13
}
else // VSPI
{
mySPI = new SPIClass(VSPI);
mySPI->end();
mySPI->begin(18, 19, 23, _select); // CLK=18 MISO=19 MOSI=23
}
#else // generic hardware SPI
mySPI = &SPI;
mySPI->end();
mySPI->begin();
#endif
_mySPI->end();
_mySPI->begin();
delay(1);
}
else
Expand All @@ -78,22 +68,6 @@ void MAX31855::setSPIspeed(uint32_t speed)
};


#if defined(ESP32)
void MAX31855::setGPIOpins(uint8_t clock, uint8_t miso, uint8_t mosi, uint8_t select)
{
_clock = clock;
_miso = miso;
_select = select;
pinMode(_select, OUTPUT);
digitalWrite(_select, HIGH);

// disable SPI and enable again
mySPI->end();
mySPI->begin(clock, miso, mosi, select);
}
#endif


uint8_t MAX31855::read()
{
// return value of _read()
Expand Down Expand Up @@ -162,15 +136,16 @@ uint32_t MAX31855::_read(void)
// DATA TRANSFER
if (_hwSPI)
{
mySPI->beginTransaction(_spi_settings);
digitalWrite(_select, LOW); // must be after mySPI->beginTransaction() - see #14 STM32
_mySPI->beginTransaction(_spi_settings);
// must be after mySPI->beginTransaction() - see #14 STM32
digitalWrite(_select, LOW);
for (uint8_t i = 0; i < 4; i++)
{
_rawData <<= 8;
_rawData += mySPI->transfer(0);
_rawData += _mySPI->transfer(0);
}
digitalWrite(_select, HIGH);
mySPI->endTransaction();
_mySPI->endTransaction();
}
else // Software SPI
{
Expand Down
45 changes: 18 additions & 27 deletions MAX31855.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: MAX31855.h
// AUTHOR: Rob Tillaart
// VERSION: 0.4.4
// VERSION: 0.5.0
// PURPOSE: Arduino library for MAX31855 chip for K type thermocouple
// DATE: 2014-01-01
// URL: https://github.com/RobTillaart/MAX31855_RT
Expand All @@ -25,7 +25,16 @@
#include "SPI.h"


#define MAX31855_VERSION (F("0.4.4"))
#define MAX31855_VERSION (F("0.5.0"))

#ifndef __SPI_CLASS__
#if defined(ARDUINO_ARCH_RP2040)
#define __SPI_CLASS__ SPIClassRP2040
#else
#define __SPI_CLASS__ SPIClass
#endif
#endif


#define MAX31855_NO_TEMPERATURE -999

Expand Down Expand Up @@ -63,13 +72,12 @@
class MAX31855
{
public:
// HW SPI
MAX31855(uint8_t select, __SPI_CLASS__ * mySPI);
// SW SPI
MAX31855(uint8_t select, uint8_t miso, uint8_t clock);

MAX31855();

// HW SPI
void begin(uint8_t select);
// SW SPI
void begin(uint8_t clock, uint8_t select, uint8_t miso);
void begin();

// returns state - bit field: 0 = STATUS_OK
uint8_t read();
Expand Down Expand Up @@ -104,19 +112,6 @@ class MAX31855
uint16_t getSWSPIdelay() { return _swSPIdelay; };



// ESP32 specific
#if defined(ESP32)
void selectHSPI() { _useHSPI = true; };
void selectVSPI() { _useHSPI = false; };
bool usesHSPI() { return _useHSPI; };
bool usesVSPI() { return !_useHSPI; };

// to overrule ESP32 default hardware pins
void setGPIOpins(uint8_t clock, uint8_t miso, uint8_t mosi, uint8_t select);
#endif


private:
uint32_t _read();

Expand All @@ -135,12 +130,8 @@ class MAX31855

uint16_t _swSPIdelay = 0;
uint32_t _SPIspeed;
SPIClass * mySPI;
SPISettings _spi_settings;

#if defined(ESP32)
bool _useHSPI = true;
#endif
__SPI_CLASS__ * _mySPI;
SPISettings _spi_settings;
};


Expand Down
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ Library tested with breakout board.
```


#### 0.5.0 Breaking change

The version 0.5.0 has breaking changes in the interface.
The essence is removal of ESP32 specific code from the library.
This makes it possible to support the ESP32-S3 and other processors in the future.
Also it makes the library a bit simpler to maintain.

Note the order of the parameters of the software SPI constructor has changed in 0.5.0.


#### Related

- https://github.com/RobTillaart/MAX6675
Expand Down Expand Up @@ -81,9 +91,10 @@ Performance read() function, timing in us. (ESP32 @240MHz)

#### Constructor

- **MAX31855()** create object.
- **void begin(const uint8_t select)** set select pin => hardware SPI
- **void begin(const uint8_t sclk, const uint8_t select, const uint8_t miso)** set clock, select and miso pin => software SPI
- **MAX31855(uint8_t select, SPIClassRP2040 \* mySPI)** hardware SPI R2040
- **MAX31855(uint8_t select, SPIClass \* mySPI)** hardware SPI other
- **MAX31855(uint8_t select, uint8_t miso, uint8_t clock)** software SPI
- **void begin()** initialize internals


#### Hardware SPI
Expand All @@ -96,15 +107,6 @@ To be used only if one needs a specific speed.
- **uint16_t getSWSPIdelay()** get set value in micros.


#### ESP32 specific

- **void selectHSPI()** must be called before **begin()**
- **void selectVSPI()** must be called before **begin()**
- **bool usesHSPI()**
- **bool usesVSPI()**
- **void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select)** to overrule ESP32 default hardware pins


#### Reading

To make a temperature reading call **read()**.
Expand Down Expand Up @@ -149,7 +151,10 @@ The library supports a fixed offset to calibrate the thermocouple.
For this the functions **float getOffset()** and **void setOffset(float offset)** are available.
This offset is "added" in the **getTemperature()** function.

Note the offset used is a float, so decimals can be used.
Notes
- the offset can be positive or negative.
- the offset used is a float, so decimals can be used.
A typical usage is to call **setOffset(273.15)** to get ° Kelvin.


#### Delta analysis
Expand Down Expand Up @@ -245,11 +250,6 @@ or

See examples

#### breaking change 0.4.0

In issue #21 it became clear that the code in the constructor is not always executed correctly.
Therefore this code + parameters is moved to the **begin()** function.


## Experimental part (to be tested)

Expand Down
28 changes: 15 additions & 13 deletions examples/Demo_getRawData/Demo_getRawData.ino
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
//
// FILE: Demo_getRawData.ino
// AUTHOR: FabioBrondo
// VERSION: 0.4.0
// PURPOSE: thermocouple lib demo application
// DATE: 2020-08-24
// URL: https://github.com/RobTillaart/MAX31855_RT
//


#include <SPI.h>
#include <MAX31855.h>
#include "MAX31855.h"


#define MAXDO 7 // Defining the MISO pin
#define MAXCS 6 // Defining the CS pin
#define MAXCLK 5 // Defining the SCK pin
const int selectPin = 7;
const int dataPin = 6;
const int clockPin = 5;


MAX31855 thermocouple;
MAX31855 thermoCouple(selectPin, dataPin, clockPin);


void setup ()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MAX31855_VERSION : ");
Serial.println(MAX31855_VERSION);
Serial.println();
delay(250);
thermocouple.begin(MAXCLK, MAXCS, MAXDO);

thermoCouple.begin();
}


void loop ()
{
int status = thermocouple.read();
int status = thermoCouple.read();
if (status == STATUS_NO_COMMUNICATION)
{
Serial.println("NO COMMUNICATION");
}

uint32_t value = thermocouple.getRawData(); // Read the raw Data value from the module
uint32_t value = thermoCouple.getRawData(); // Read the raw Data value from the module
Serial.print("RAW:\t");

// Display the raw data value in BIN format
Expand All @@ -50,11 +52,11 @@ void loop ()
Serial.println();

Serial.print("TMP:\t");
Serial.println(thermocouple.getTemperature(), 3);
Serial.println(thermoCouple.getTemperature(), 3);

delay(100);
}


// -- END OF FILE --
// -- END OF FILE --

Loading

0 comments on commit a35c109

Please sign in to comment.