Skip to content

Commit 8aeaceb

Browse files
committed
Merge branch 'master' into development
Conflicts: libraries/MySensors/MyGateway.cpp libraries/MySensors/MySensor.cpp libraries/MySensors/examples/EthernetGateway/EthernetGateway.ino
2 parents d9ffd61 + e1fa3dc commit 8aeaceb

File tree

14 files changed

+555
-54
lines changed

14 files changed

+555
-54
lines changed

libraries/MAX6675/MAX6675.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
MAX6675.cpp - Library for reading temperature from a MAX6675.
3+
4+
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
5+
http://creativecommons.org/licenses/by-sa/3.0/
6+
*/
7+
8+
#include <MAX6675.h>
9+
10+
MAX6675::MAX6675(uint8_t CS_pin, uint8_t SO_pin, uint8_t SCK_pin, uint8_t units)
11+
{
12+
pinMode(CS_pin, OUTPUT);
13+
pinMode(SO_pin, INPUT);
14+
pinMode(SCK_pin, OUTPUT);
15+
16+
digitalWrite(CS_pin, HIGH);
17+
18+
_CS_pin = CS_pin;
19+
_SO_pin = SO_pin;
20+
_SCK_pin = SCK_pin;
21+
_units = units;
22+
}
23+
24+
float MAX6675::read_temp()
25+
{
26+
uint16_t value = 0;
27+
uint8_t error_tc = 0;
28+
float temp = 0.0;
29+
30+
/*
31+
Initiate a temperature conversion. According to MAX's tech notes FAQ's
32+
for the chip, Line going high initiates a conversion, which means, we
33+
need to clock the chip low to high to initiate the conversion, then wait
34+
for the conversion to be complete before trying to read the data from
35+
the chip.
36+
*/
37+
digitalWrite(_CS_pin,LOW);
38+
delay(2);
39+
digitalWrite(_CS_pin,HIGH);
40+
delay(220);
41+
42+
/* Read the chip and return the raw temperature value */
43+
44+
/*
45+
Bring CS pin low to allow us to read the data from
46+
the conversion process
47+
*/
48+
digitalWrite(_CS_pin,LOW);
49+
50+
/* Cycle the clock for dummy bit 15 */
51+
digitalWrite(_SCK_pin,HIGH);
52+
delay(1);
53+
digitalWrite(_SCK_pin,LOW);
54+
55+
/*
56+
Read bits 14-3 from MAX6675 for the Temp. Loop for each bit reading
57+
the value and storing the final value in 'temp'
58+
*/
59+
for (int i=11; i>=0; i--) {
60+
digitalWrite(_SCK_pin,HIGH);
61+
value += digitalRead(_SO_pin) << i;
62+
digitalWrite(_SCK_pin,LOW);
63+
}
64+
65+
/* Read the TC Input inp to check for TC Errors */
66+
digitalWrite(_SCK_pin,HIGH);
67+
error_tc = digitalRead(_SO_pin);
68+
digitalWrite(_SCK_pin,LOW);
69+
70+
/*
71+
Read the last two bits from the chip, faliure to do so will result
72+
in erratic readings from the chip.
73+
*/
74+
for (int i=1; i>=0; i--) {
75+
digitalWrite(_SCK_pin,HIGH);
76+
delay(1);
77+
digitalWrite(_SCK_pin,LOW);
78+
}
79+
80+
// Disable Device
81+
digitalWrite(_CS_pin, HIGH);
82+
83+
/*
84+
Keep in mind that the temp that was just read is on the digital scale
85+
from 0˚C to 1023.75˚C at a resolution of 2^12. We now need to convert
86+
to an actual readable temperature (this drove me nuts until I figured
87+
this out!). Now multiply by 0.25. I tried to avoid float math but
88+
it is tough to do a good conversion to ˚F. THe final value is converted
89+
to an int and returned at x10 power.
90+
91+
2 = temp in deg F
92+
1 = temp in deg C
93+
0 = raw chip value 0-4095
94+
*/
95+
if(_units == 2) {
96+
temp = (value*0.25) * 9.0/5.0 + 32.0;
97+
} else if(_units == 1) {
98+
temp = (value*0.25);
99+
} else {
100+
temp = value;
101+
}
102+
103+
/* Output negative of CS_pin if there is a TC error, otherwise return 'temp' */
104+
if(error_tc != 0) {
105+
return -_CS_pin;
106+
} else {
107+
return temp;
108+
}
109+
}

libraries/MAX6675/MAX6675.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
MAX6675.h - Library for reading temperature from a MAX6675.
3+
4+
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
5+
http://creativecommons.org/licenses/by-sa/3.0/
6+
*/
7+
8+
#ifndef MAX6675_h
9+
#define MAX6675_h
10+
11+
#if defined(ARDUINO) && ARDUINO >= 100
12+
#include "Arduino.h"
13+
#else
14+
#include "WProgram.h"
15+
#endif
16+
17+
class MAX6675
18+
{
19+
public:
20+
MAX6675(uint8_t CS_pin, uint8_t SO_pin, uint8_t SCK_pin, uint8_t units);
21+
float read_temp();
22+
private:
23+
uint8_t _CS_pin;
24+
uint8_t _SO_pin;
25+
uint8_t _SCK_pin;
26+
uint8_t _units;
27+
uint8_t chip_read(uint8_t CS_pin, uint8_t &error_tc);
28+
};
29+
30+
#endif

libraries/MAX6675/README.markdown

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
MAX6675 Arduino Library
2+
=======================
3+
Version: **2.0.1**
4+
5+
This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>.
6+
7+
8+
Summary
9+
-------
10+
11+
The primary use of the library is to easily interface with a MAX6675 chip via it's SPI interface. Use the following code to initialize the library. V2 also greatly improves performance and timing of the chip for more accurate reads. Thanks Ed!
12+
13+
MAX6675 temp0(CS,SO,SCK,units);
14+
15+
Variables:
16+
17+
* CS is chip select pin of chip
18+
* SO is data out of the chip
19+
* SCK is the clock pin of the chip
20+
* units is one of the following:
21+
* 0 = raw 0-4095 value
22+
* 1 = temp in ˚C
23+
* 2 = temp in ˚F
24+
25+
Following this you can use the _read_temp()_ function to return the temperature as a float.
26+
27+
temperature = temp0.read_temp();
28+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Single_Temp.pde - Example using the MAX6675 Library.
3+
Created by Ryan McLaughlin <ryanjmclaughlin@gmail.com>
4+
5+
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
6+
http://creativecommons.org/licenses/by-sa/3.0/
7+
*/
8+
9+
#include <MAX6675.h>
10+
11+
int LED1 = 9; // Status LED Pin
12+
int CS = 10; // CS pin on MAX6675
13+
int SO = 12; // SO pin of MAX6675
14+
int SCK = 13; // SCK pin of MAX6675
15+
int units = 2; // Units to readout temp (0 = raw, 1 = ˚C, 2 = ˚F)
16+
float temperature = 0.0; // Temperature output variable
17+
18+
19+
// Initialize the MAX6675 Library for our chip
20+
MAX6675 temp(CS,SO,SCK,units);
21+
22+
23+
// Setup Serial output and LED Pin
24+
// MAX6675 Library already sets pin modes for MAX6675 chip!
25+
void setup() {
26+
Serial.begin(9600);
27+
pinMode(LED1, OUTPUT);
28+
}
29+
30+
void loop() {
31+
// Read the temp from the MAX6675
32+
temperature = temp.read_temp();
33+
34+
if(temperature < 0) {
35+
// If there is an error with the TC, temperature will be < 0
36+
Serial.print("Thermocouple Error on CS");
37+
Serial.println( temperature );
38+
digitalWrite(LED1, HIGH);
39+
} else {
40+
Serial.print("Current Temperature: ");
41+
Serial.println( temperature );
42+
digitalWrite(LED1, LOW);
43+
}
44+
45+
// Wait one second before reading again
46+
delay(1000);
47+
}

libraries/MAX6675/keywords.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#######################################
2+
# Syntax Coloring Map For MAX6675
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
MAX6675 KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
read_temp KEYWORD2
16+

libraries/MySensors/MyMessage.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ MyMessage::MyMessage(uint8_t _sensor, uint8_t _type) {
1313
type = _type;
1414
}
1515

16-
1716
bool MyMessage::isAck() const {
1817
return miGetAck();
1918
}
2019

21-
2220
/* Getters for payload converted to desired form */
2321
void* MyMessage::getCustom() const {
2422
return (void *)data;
@@ -42,16 +40,20 @@ char MyMessage::i2h(uint8_t i) const {
4240
return 'A' + k - 10;
4341
}
4442

43+
char* MyMessage::getCustomString(char *buffer) const {
44+
for (uint8_t i = 0; i < miGetLength(); i++)
45+
{
46+
buffer[i * 2] = i2h(data[i] >> 4);
47+
buffer[(i * 2) + 1] = i2h(data[i]);
48+
}
49+
buffer[miGetLength() * 2] = '\0';
50+
return buffer;
51+
}
52+
4553
char* MyMessage::getStream(char *buffer) const {
4654
uint8_t cmd = miGetCommand();
4755
if ((cmd == C_STREAM) && (buffer != NULL)) {
48-
for (uint8_t i = 0; i < miGetLength(); i++)
49-
{
50-
buffer[i * 2] = i2h(data[i] >> 4);
51-
buffer[(i * 2) + 1] = i2h(data[i]);
52-
}
53-
buffer[miGetLength() * 2] = '\0';
54-
return buffer;
56+
return getCustomString(buffer);
5557
} else {
5658
return NULL;
5759
}
@@ -73,12 +75,11 @@ char* MyMessage::getString(char *buffer) const {
7375
} else if (payloadType == P_LONG32) {
7476
ltoa(lValue, buffer, 10);
7577
} else if (payloadType == P_ULONG32) {
76-
7778
ultoa(ulValue, buffer, 10);
7879
} else if (payloadType == P_FLOAT32) {
7980
dtostrf(fValue,2,fPrecision,buffer);
8081
} else if (payloadType == P_CUSTOM) {
81-
return getStream(buffer);
82+
return getCustomString(buffer);
8283
}
8384
return buffer;
8485
} else {
@@ -151,7 +152,6 @@ unsigned int MyMessage::getUInt() const {
151152

152153
}
153154

154-
155155
MyMessage& MyMessage::setType(uint8_t _type) {
156156
type = _type;
157157
return *this;
@@ -175,12 +175,11 @@ MyMessage& MyMessage::set(void* value, uint8_t length) {
175175
return *this;
176176
}
177177

178-
179178
MyMessage& MyMessage::set(const char* value) {
180-
uint8_t length = strlen(value);
179+
uint8_t length = min(strlen(value), MAX_PAYLOAD);
181180
miSetLength(length);
182181
miSetPayloadType(P_STRING);
183-
strncpy(data, value, min(length, MAX_PAYLOAD));
182+
strncpy(data, value, length);
184183
return *this;
185184
}
186185

@@ -191,7 +190,6 @@ MyMessage& MyMessage::set(uint8_t value) {
191190
return *this;
192191
}
193192

194-
195193
MyMessage& MyMessage::set(float value, uint8_t decimals) {
196194
miSetLength(5); // 32 bit float + persi
197195
miSetPayloadType(P_FLOAT32);
@@ -227,5 +225,3 @@ MyMessage& MyMessage::set(int value) {
227225
iValue = value;
228226
return *this;
229227
}
230-
231-

libraries/MySensors/MyMessage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ typedef enum {
122122
#ifdef __cplusplus
123123
class MyMessage
124124
{
125+
private:
126+
char* getCustomString(char *buffer) const;
127+
125128
public:
126129
// Constructors
127130
MyMessage();

0 commit comments

Comments
 (0)