Maxim MAX11210 24-bit ADC on Spark

This library has been written to support the Maxim MAX11210 24-bit A/D converter as peripheral of the Spark module. Spark libraries can be used in the Spark IDE.
breadboard_dual-tmp36
The code snippet below shows how to add the high resolution A/D converter to the example for measuring the temperature.

#include "maxim-max11210.h"
int sensor1 = 0;
double temperature1 = 0.0;
double sensor2 = 0;
double temperature2 = 0.0;

// Declare the peripheral A/D converter as type Max11210 from the Maxim namespace
Maxim::Max11210 adcMax11210;
void setup() {
   Spark.variable("Temp1", &temperature1, DOUBLE);
   Spark.variable("Temp2", &temperature2, DOUBLE);
   pinMode(A7, INPUT);
   adcMax11210.begin();
}

void loop() {
   sensor1 = analogRead(A7);
   sensor2 = adcMax11210.read();
   // TMP36: 0.5 V offset voltage, 10 mV/degC, 750 mV at 25 degC
   // 12-bit A/D for sensor 1: (2^12 - 1) = 4095 steps
   // 24-bit A/D for sensor 2: (2^24 - 1) = 1677215 steps
   temperature1 = (3.3 * sensor1 / 4095.0 - 0.5) * 100.0;
   temperature2 = (3.3 * sensor2 / 16777215.0 - 0.5) * 100.0;
}

In a second example the configuration settings of the MAX11210 are changed by sending commands through a function of the Spark module.

#include "maxim-max11210.h"

double sensor2 = 0;
double temperature2 = 0.0;

// Declare the peripheral A/D converter as type Max11210 from the Maxim namespace
Maxim::Max11210 adcMax11210;

void setup() {
   Spark.variable("Temp2", &temperature2, DOUBLE);
   Spark.function("Command", cmdFunction);
   adcMax11210.begin();
}

void loop() {
   sensor2 = adcMax11210.read();
   temperature2 = (3.3 * sensor2 / 16777215.0 - 0.5) * 100.0;
}

int cmdFunction(String command) {
   if (command == "SELFCAL") {
      adcMax11210.setDisableSelfCalGain(false);
      adcMax11210.setDisableSelfCalOffset(false);
      adcMax11210.selfCal();
      selfcalgain = adcMax11210.getSelfCalGain();
      selfcaloffset = adcMax11210.getSelfCalOffset();
      return 1;
   }
   if (command == "SYSCALOFFSET") {
      // System level calibration should only be performed when a zero-scale signal can be presented to the ADC.
      // Give a rate command afterwards to reinitiate conversion.
      adcMax11210.setDisableSysOffset(false);
      adcMax11210.sysOffsetCal();
      syscaloffset = adcMax11210.getSysOffsetCal();
      return 1;
   }
   if (command == "SYSCALGAIN") {
      // System level calibration should only be performed when a full-scale signal can be presented to the ADC.
      // Give a rate command afterwards to reinitiate conversion.
      adcMax11210.setDisableSysGain(false);
      adcMax11210.sysGainCal();
      syscalgain = adcMax11210.getSysGainCal();
      return 1;
   }
   if (command == "RATE1") {
      adcMax11210.setRate(MAX11210_RATE1);
      return 1;
   }
   if (command == "RATE2_5") {
      adcMax11210.setRate(MAX11210_RATE2_5);
      return 1;
   }
   if (command == "GAIN1") {
      adcMax11210.setGain(MAX11210_GAIN1);
      return 1;
   }
   if (command == "GAIN2") {
      adcMax11210.setGain(MAX11210_GAIN2);
      return 1;
   }
   // Simply add other commands here ...
   return -1;
}

Both examples can be found in the example project dual-tmp36.ino provided with this library. See my Github repository for the source code and additional information: github.com/bankrasrg/maxim-max11210.