Class: Max31855

Inherits:
Object
  • Object
show all
Defined in:
lib/max31855.rb,
lib/max31855/version.rb,
ext/max31855/max31855.c

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Instance Method Details

#fetchObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'ext/max31855/max31855.c', line 86

VALUE wMax31855_fetch(VALUE self)
{
	struct spidev *device;
	unsigned char* recv;
	unsigned int thermocouple_raw;
	unsigned int internal_raw;
	double thermocouple, internal;

	Data_Get_Struct(self, struct spidev, device);

	recv = receiveData(device, 4);

	thermocouple_raw = recv[0] << 8;
	thermocouple_raw |= recv[1];
	internal_raw = recv[2] << 8;
	internal_raw |= recv[3];

	if (thermocouple_raw & 0x0001) { // On error
		char message[128];

		memset(message, 0, 128);

		if (internal_raw & 0x0004)
			strcat(message, "Short to Vcc,");

		if (internal_raw & 0x0002)
			strcat(message, "Short to GND,");

		if (internal_raw & 0x0001)
			strcat(message, "Open Circuit");

		rb_raise(rb_eStandardError, "Sensor error: %s", message);

		return Qnil;
	} else {
		if ((thermocouple_raw & 0x8000) == 0) // above 0 Degrees Celsius
			thermocouple = (thermocouple_raw >> 2) * 0.25;
		else // below zero
			thermocouple = (((~thermocouple_raw & 0xffff) >> 2) + 1)  * -0.25;

		if ((internal_raw & 0x8000) == 0) // above 0 Degrees Celsius
			internal = (internal_raw >> 4) * 0.0625;
		else // below zero
			internal = (((~internal_raw & 0xffff) >> 4) + 1) * -0.0625;

		return rb_ary_new3(2, rb_float_new(thermocouple), rb_float_new(internal));
	}
}