Skip to content

Commit cd5c0d2

Browse files
committed
Changed Class structure
Changed class structure and SPI Management
1 parent 66382c1 commit cd5c0d2

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

README.md renamed to README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Allows you to read the temperature sensors conencted to your MKR Therm shield.
44

55
== License ==
66

7-
Copyright (c) 2018 Arduino SA. All rights reserved.
7+
Copyright (c) 2019 Arduino SA. All rights reserved.
88

99
This library is free software; you can redistribute it and/or
1010
modify it under the terms of the GNU Lesser General Public

src/MKRTherm.cpp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
This file is part of the MKRTherm library.
3-
Copyright (c) 2018 Arduino SA. All rights reserved.
3+
Copyright (c) 2019 Arduino SA. All rights reserved.
44
55
This library is free software; you can redistribute it and/or
66
modify it under the terms of the GNU Lesser General Public
@@ -19,55 +19,56 @@
1919

2020
#include "MKRTherm.h"
2121

22-
#include "SPI.h"
23-
24-
THERMClass::THERMClass(int cs) : _cs(cs)
22+
THERMClass::THERMClass(int cs, SPIClass& spi) :
23+
_cs(cs),
24+
_spi(&spi),
25+
_spiSettings(4000000, MSBFIRST, SPI_MODE0)
2526
{
2627
}
2728

2829
int THERMClass::begin()
2930
{
3031
pinMode(_cs, OUTPUT);
3132
digitalWrite(_cs, HIGH);
32-
SPI.begin();
33+
_spi->begin();
3334

3435
return 1;
3536
}
3637

3738
void THERMClass::end()
3839
{
39-
SPI.end();
40+
pinMode(_cs, INPUT);
41+
digitalWrite(_cs, LOW);
42+
_spi->end();
4043
}
4144

4245
uint32_t THERMClass::readSensor()
4346
{
44-
uint32_t read;
47+
uint32_t read=0x00;
4548

4649
digitalWrite(_cs, LOW);
47-
delay(1);
50+
delayMicroseconds(1);
4851

49-
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
52+
_spi->beginTransaction(_spiSettings);
5053

51-
read = SPI.transfer(0);
52-
read <<= 8;
53-
read |= SPI.transfer(0);
54-
read <<= 8;
55-
read |= SPI.transfer(0);
56-
read <<= 8;
57-
read |= SPI.transfer(0);
5854

59-
SPI.endTransaction();
55+
for (int i = 0; i < 4; i++) {
56+
read <<= 8;
57+
read |= _spi->transfer(0);
58+
}
59+
60+
_spi->endTransaction();
6061

6162
digitalWrite(_cs, HIGH);
62-
delay(1);
63+
6364
return read;
6465
}
6566

6667

67-
double THERMClass::readCelsiusTemperature()
68+
float THERMClass::readTemperature()
6869
{
6970
uint32_t rawword;
70-
double celsius;
71+
float celsius;
7172

7273
rawword = readSensor();
7374

@@ -92,10 +93,10 @@ double THERMClass::readCelsiusTemperature()
9293

9394

9495

95-
double THERMClass::readRefTemperature()
96+
float THERMClass::readInternalTemperature()
9697
{
9798
uint32_t rawword;
98-
double ref;
99+
float ref;
99100

100101
rawword = readSensor();
101102

src/MKRTherm.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
This file is part of the MKRTherm library.
3-
Copyright (c) 2018 Arduino SA. All rights reserved.
3+
Copyright (c) 2019 Arduino SA. All rights reserved.
44
55
This library is free software; you can redistribute it and/or
66
modify it under the terms of the GNU Lesser General Public
@@ -28,16 +28,21 @@
2828

2929
class THERMClass {
3030
private:
31+
uint32_t readSensor();
32+
3133
int _cs;
34+
SPIClass* _spi;
35+
SPISettings _spiSettings;
36+
3237
public:
33-
THERMClass(int cs = A4);
38+
THERMClass(int cs = A4, SPIClass& spi = SPI);
3439

3540
int begin();
3641
void end();
3742

38-
uint32_t readSensor();
39-
double readCelsiusTemperature();
40-
double readRefTemperature();
43+
44+
float readTemperature();
45+
float readInternalTemperature();
4146
};
4247

4348
extern THERMClass THERM;

0 commit comments

Comments
 (0)