chiark / gitweb /
Fixed the requirement for -lm
[wiringPi.git] / wiringPi / max31855.c
index 21858391b406e73107565695a5b29bbb13c1909c..ea184d8fd770ab788b9a53d4f5274a6aa2254d08 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * max31855.c:
  *     Extend wiringPi with the max31855 SPI Analog to Digital convertor
- *     Copyright (c) 2012-2013 Gordon Henderson
+ *     Copyright (c) 2012-2015 Gordon Henderson
  ***********************************************************************
  * This file is part of wiringPi:
  *     https://projects.drogon.net/raspberry-pi/wiringpi/
  ***********************************************************************
  */
 
+#include <byteswap.h>
+#include <stdint.h>
+
 #include <wiringPi.h>
 #include <wiringPiSPI.h>
 
 #include "max31855.h"
 
-/*
- * myAnalogRead:
- *     Return the analog value of the given pin
- *     Note: The chip really only has one read "channel", but we're faking it
- *     here so we can read the error registers. Channel 0 will be the data
- *     channel, and 1 is the error register code.
- *     Note: Temperature returned is temp in C * 4, so divide result by 4
- *********************************************************************************
- */
-
 static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
 {
-  unsigned int spiData ;
+  uint32_t spiData ;
   int temp ;
   int chan = pin - node->pinBase ;
 
   wiringPiSPIDataRW (node->fd, (unsigned char *)&spiData, 4) ;
 
-  if (chan == 0)                       // Read temp in C
+  spiData = __bswap_32(spiData) ;
+
+  switch (chan)
   {
-    spiData >>= 18 ;
-    temp = spiData & 0x3FFF ;          // Bottom 13 bits
-    if ((spiData & 0x2000) != 0)       // Negative
-      temp = -temp ;
-    return temp ;
+    case 0:                            // Existing read - return raw value * 4
+      spiData >>= 18 ;
+      temp = spiData & 0x1FFF ;                // Bottom 13 bits
+      if ((spiData & 0x2000) != 0)     // Negative
+        temp = -temp ;
+
+      return temp ;
+
+    case 1:                            // Return error bits
+      return spiData & 0x7 ;
+
+    case 2:                            // Return temp in C * 10
+      spiData >>= 18 ;
+      temp = spiData & 0x1FFF ;                // Bottom 13 bits
+      if ((spiData & 0x2000) != 0)     // Negative
+        temp = -temp ;
+
+      return (int)((((double)temp * 25) + 0.5) / 10.0) ;
+
+    case 3:                            // Return temp in F * 10
+      spiData >>= 18 ;
+      temp = spiData & 0x1FFF ;                // Bottom 13 bits
+      if ((spiData & 0x2000) != 0)     // Negative
+        temp = -temp ;
+
+      return (int)((((((double)temp * 0.25 * 9.0 / 5.0) + 32.0) * 100.0) + 0.5) / 10.0) ;
+
+    default:                           // Who knows...
+      return 0 ;
+
   }
-  else                                 // Return error bits
-    return spiData & 0x7 ;
 }
 
 
@@ -72,7 +90,7 @@ int max31855Setup (const int pinBase, int spiChannel)
   if (wiringPiSPISetup (spiChannel, 5000000) < 0)      // 5MHz - prob 4 on the Pi
     return -1 ;
 
-  node = wiringPiNewNode (pinBase, 2) ;
+  node = wiringPiNewNode (pinBase, 4) ;
 
   node->fd         = spiChannel ;
   node->analogRead = myAnalogRead ;