Skip to content

Commit 010598f

Browse files
authored
Merge pull request #34 from caternuson/busio
Move to BusIO
2 parents a354192 + 5612d17 commit 010598f

File tree

3 files changed

+32
-71
lines changed

3 files changed

+32
-71
lines changed

Adafruit_MAX31855.cpp

Lines changed: 23 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
/***************************************************
1+
/***************************************************
22
This is a library for the Adafruit Thermocouple Sensor w/MAX31855K
33
44
Designed specifically to work with the Adafruit Thermocouple Sensor
55
----> https://www.adafruit.com/products/269
66
7-
These displays use SPI to communicate, 3 pins are required to
7+
These displays use SPI to communicate, 3 pins are required to
88
interface
9-
Adafruit invests time and resources providing this open source code,
10-
please support Adafruit and open-source hardware by purchasing
9+
Adafruit invests time and resources providing this open source code,
10+
please support Adafruit and open-source hardware by purchasing
1111
products from Adafruit!
1212
13-
Written by Limor Fried/Ladyada for Adafruit Industries.
13+
Written by Limor Fried/Ladyada for Adafruit Industries.
1414
BSD license, all text above must be included in any redistribution
1515
****************************************************/
1616

@@ -22,38 +22,22 @@
2222
#endif
2323

2424
#include <stdlib.h>
25-
#include <SPI.h>
2625

2726

2827
Adafruit_MAX31855::Adafruit_MAX31855(int8_t _sclk, int8_t _cs, int8_t _miso) {
29-
sclk = _sclk;
30-
cs = _cs;
31-
miso = _miso;
28+
spi_dev = Adafruit_SPIDevice(_cs, _sclk, _miso, -1, 4000000);
3229

3330
initialized = false;
3431
}
3532

3633
Adafruit_MAX31855::Adafruit_MAX31855(int8_t _cs) {
37-
cs = _cs;
38-
sclk = miso = -1;
34+
spi_dev = Adafruit_SPIDevice(_cs, 4000000);
3935

4036
initialized = false;
4137
}
4238

4339
void Adafruit_MAX31855::begin(void) {
44-
//define pin modes
45-
pinMode(cs, OUTPUT);
46-
digitalWrite(cs, HIGH);
47-
48-
if (sclk == -1) {
49-
// hardware SPI
50-
//start and configure hardware SPI
51-
SPI.begin();
52-
} else {
53-
pinMode(sclk, OUTPUT);
54-
pinMode(miso, INPUT);
55-
}
56-
initialized = true;
40+
initialized = spi_dev.begin();
5741
}
5842

5943
double Adafruit_MAX31855::readInternal(void) {
@@ -88,14 +72,14 @@ double Adafruit_MAX31855::readCelsius(void) {
8872
/*
8973
float internal = (v >> 4) & 0x7FF;
9074
internal *= 0.0625;
91-
if ((v >> 4) & 0x800)
75+
if ((v >> 4) & 0x800)
9276
internal *= -1;
9377
Serial.print("\tInternal Temp: "); Serial.println(internal);
9478
*/
9579

9680
if (v & 0x7) {
9781
// uh oh, a serious problem!
98-
return NAN;
82+
return NAN;
9983
}
10084

10185
if (v & 0x80000000) {
@@ -107,7 +91,7 @@ double Adafruit_MAX31855::readCelsius(void) {
10791
v >>= 18;
10892
}
10993
//Serial.println(v, HEX);
110-
94+
11195
double centigrade = v;
11296

11397
// LSB = 0.25 degrees C
@@ -127,52 +111,27 @@ double Adafruit_MAX31855::readFarenheit(void) {
127111
return f;
128112
}
129113

130-
uint32_t Adafruit_MAX31855::spiread32(void) {
114+
uint32_t Adafruit_MAX31855::spiread32(void) {
131115
int i;
132116
uint32_t d = 0;
117+
uint8_t buf[4];
133118

134119
// backcompatibility!
135120
if (! initialized) {
136121
begin();
137122
}
138123

139-
digitalWrite(cs, LOW);
140-
delay(1);
141-
142-
if(sclk == -1) {
143-
// hardware SPI
144-
145-
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
146-
147-
d = SPI.transfer(0);
148-
d <<= 8;
149-
d |= SPI.transfer(0);
150-
d <<= 8;
151-
d |= SPI.transfer(0);
152-
d <<= 8;
153-
d |= SPI.transfer(0);
154-
155-
SPI.endTransaction();
156-
} else {
157-
// software SPI
158-
159-
digitalWrite(sclk, LOW);
160-
delay(1);
161-
162-
for (i=31; i>=0; i--) {
163-
digitalWrite(sclk, LOW);
164-
delay(1);
165-
d <<= 1;
166-
if (digitalRead(miso)) {
167-
d |= 1;
168-
}
169-
170-
digitalWrite(sclk, HIGH);
171-
delay(1);
172-
}
173-
}
124+
spi_dev.read(buf, 4);
125+
126+
d = buf[0];
127+
d <<= 8;
128+
d |= buf[1];
129+
d <<= 8;
130+
d |= buf[2];
131+
d <<=8;
132+
d |= buf[3];
174133

175-
digitalWrite(cs, HIGH);
176134
//Serial.println(d, HEX);
135+
177136
return d;
178137
}

Adafruit_MAX31855.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
/***************************************************
1+
/***************************************************
22
This is a library for the Adafruit Thermocouple Sensor w/MAX31855K
33
44
Designed specifically to work with the Adafruit Thermocouple Sensor
55
----> https://www.adafruit.com/products/269
66
7-
These displays use SPI to communicate, 3 pins are required to
7+
These displays use SPI to communicate, 3 pins are required to
88
interface
9-
Adafruit invests time and resources providing this open source code,
10-
please support Adafruit and open-source hardware by purchasing
9+
Adafruit invests time and resources providing this open source code,
10+
please support Adafruit and open-source hardware by purchasing
1111
products from Adafruit!
1212
13-
Written by Limor Fried/Ladyada for Adafruit Industries.
13+
Written by Limor Fried/Ladyada for Adafruit Industries.
1414
BSD license, all text above must be included in any redistribution
1515
****************************************************/
1616

@@ -23,6 +23,8 @@
2323
#include "WProgram.h"
2424
#endif
2525

26+
#include <Adafruit_SPIDevice.h>
27+
2628
class Adafruit_MAX31855 {
2729
public:
2830
Adafruit_MAX31855(int8_t _sclk, int8_t _cs, int8_t _miso);
@@ -35,9 +37,9 @@ class Adafruit_MAX31855 {
3537
uint8_t readError();
3638

3739
private:
40+
Adafruit_SPIDevice spi_dev = NULL;
3841
boolean initialized;
3942

40-
int8_t sclk, miso, cs;
4143
uint32_t spiread32(void);
4244
};
4345

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Adafruit MAX31855 library
2-
version=1.0.4
2+
version=1.0.5
33
author=Adafruit
44
maintainer=Adafruit <info@adafruit.com>
55
sentence=Library for the Adafruit Thermocouple breakout with MAX31855K

0 commit comments

Comments
 (0)