IGOR / Mbed 2 deprecated PONY_Ph0-uAXIS

Dependencies:   C027 C027_Support M2XStreamClient PowerControl jsonlite mbed-rtos mbed

Fork of PONY_Ph0-uAXIS by Sean McBeath

PONY_sense.cpp

Committer:
sgmcb
Date:
2015-12-29
Revision:
41:f603d76dc6fe
Child:
43:80aa0c933e1a

File content as of revision 41:f603d76dc6fe:

/*

PONY_sense.cpp
(C) 2015 Igor Institute

*/

#include "PONY_sense.h"

#define THROWAWAY

#ifdef THROWAWAY
// Codes for a throwaway M2X device
#define M2XAPIKEY "54c6bbf11012f34830bd23cc091ca250"
#define DEVID "b9c57667ee0495dc0b3cddd890f8d2df"
#endif

#ifndef THROWAWAY
// v1.2 codes
#define M2XAPIKEY "bbc483492238dc76f7d12f0cd6e13a4b"
#define DEVID "3764db38b6c9ec4045a38e0125b14b4c"
#endif












// Convert voltage across thermistor to temperature
// Right now, this math assumes we're using a Vishay NTCLG100E2 thermistor

// Thermistor values vs. temperature (-20C to 100C x 5C)
// NOTE: This setup assumes that we're measuring a thermistor where one end is connected directly to ground
float thermistorToTemp(float thermVoltage) {
    float nomCurrent = 0.0000825;   // This is the set current of the current configuration (on breadboard, 2015-12-17)
    float nomResistance = thermVoltage / nomCurrent;
    
    float nomTemp = -25.87 * log(nomResistance) + 266.53; // Best fit for Vishay NTCLG100E2 between -20C and 100C; R = 0.99228
    
    return nomTemp;
    
};

float getTemp(AnalogIn* pTempPin) {
    float tempV = *pTempPin;
    float temp = thermistorToTemp(AINTOV(tempV));
    
    return temp;
}

// Records and sends the temperature up to the M2X cloud
int logTemp(AnalogIn* pTempPin, float* pTemp, M2XStreamClient* m2x) {
    *pTemp = getTemp(pTempPin);
    printf("Temp=%f\r\n", *pTemp);
    
    int ret = m2x->updateStreamValue(DEVID, "kegtemp", *pTemp);
    printf("m2x ret=%i\r\n",ret);
    
    return ret;
    
}





//-----------------------------------------------
//-----------------------------------------------

#define AX_WRITE_REG    0x3A
#define AX_READ_REG     0x3B

#define AX_WHOAMI_REG   0x0F

#define AX_CONFIG1_REG  0x20 
#define AX_CONFIG1_VAL  0xC7


#define AX_X_REG        0x29
#define AX_Y_REG        0x2B
#define AX_Z_REG        0x2D

#define AX_NEWFILTER    0b00000111





void LIS331write(I2C* axis, int targetReg, int targetVal) {
    axis->start();
    
    axis->write(AX_WRITE_REG);
    axis->write(targetReg);
    axis->write(targetVal);
    
    axis->stop();

    return;    
}

int LIS331read(I2C* axis, const int readReg, int* readVal) {
    axis->start();
    
    axis->write(AX_WRITE_REG);
    axis->write(readReg);
    
    axis->start();
    axis->write(AX_READ_REG);
    *readVal = axis->read(1);
    
    axis->stop();
    return *readVal;    
}

int LIS331read(I2C* axis, const int readReg) {
    axis->start();
    
    axis->write(AX_WRITE_REG);
    axis->write(readReg);
    
    axis->start();
    axis->write(AX_READ_REG);
    int readVal = axis->read(1);
    
    axis->stop();
    return readVal;    
}


int configureAccel(I2C* axis) {
    axis->frequency(400); 

    LIS331write(axis, AX_CONFIG1_REG, AX_CONFIG1_VAL);
    int configVal = 0;
    LIS331read(axis, AX_CONFIG1_REG, &configVal);

    return configVal;
}

int accelWhoAmI (I2C* axis) {
    int axID = 0;
    
    LIS331read(axis, AX_WHOAMI_REG, &axID);
    
    return axID;   
}

int accelX (I2C* axis) {
    int xAccel = 0;
    
    LIS331read(axis,AX_X_REG,&xAccel);

    return xAccel;
}

int accelY (I2C* axis) {
    int yAccel = 0;
    
    LIS331read(axis,AX_Y_REG,&yAccel);

    return yAccel;
}


int accelZ (I2C* axis) {
    int zAccel = 0;
    
    LIS331read(axis,AX_Z_REG,&zAccel);

    return zAccel;
}