GDP group 24 node core

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue

sensorinterface.cpp

Committer:
jakehodges
Date:
2015-01-11
Revision:
11:190d07f37ec0
Parent:
3:71b090ec5c69
Child:
14:bc0e2fa25f94

File content as of revision 11:190d07f37ec0:

#include "sensorinterface.h"
 
sensorinterface::sensorinterface() : i2c(p28,p27), readyLine(p29)
{
    readyLine.fall(this, &sensorinterface::readyTrigger);
    
    this->ready = false;
    
    char buffer[255];
    
    #ifdef DEBUG
        printf("[SIF] Scanning for devices...\r\n");
    #endif
    
    for (char i=1; i<=254; i=i+2)
    {
        //last bit determines read/write not actual address
        if (this->i2c.read(i, &buffer[0], 1) ==0)
        {
            //returns 0 if ack i.e. a device is found
            sensor newSensor;
            newSensor = findType(newSensor, i);
            this->sensors[i] = newSensor;
            #ifdef DEBUG
                printf("[SIF] Device found at 0x%.2X, device count: %i\r\n", i, this->sensors.size());
            #endif
        }
    }
}

bool sensorinterface::isDataWaiting()
{
    return this->ready;
}

void sensorinterface::requestData()
{
    if (sensors.size() > 0)
    {
        currentSensor = sensors.begin();
        
        #ifdef DEBUG
            printf("[SIF] Beginning device query chain from 0x%.2X\r\n", currentSensor->first);
        #endif
        
        sendRequest(currentSensor->first);
    }
    else
    {
        #ifdef DEBUG
            printf("[SIF] Data requested, but no devices present\r\n");
        #endif
    }
}

void sensorinterface::readyTrigger()
{
    this->ready = true;
}

#ifdef DEBUG
void sensorinterface::error(int e_num)
{
    printf("[SIF] Error %i\r",e_num);
}
#endif
 
int sensorinterface::update()
{
    char buffer[255];
    
    for (char i=1; i<=254; i=i+2)
    {
        //last bit determines read/write not actual address
        
        bool connected = (this->i2c.read(i, &buffer[0], 1) == 0);
        bool registered = (this->sensors.count(i) > 0);
        
        if (connected)
        {
            if (!registered)
            {
                //device added
                #ifdef DEBUG
                    printf("[SIF] Device added at %i\r\n", i);
                #endif
                sensor newSensor;
                newSensor = findType(newSensor, i);
                this->sensors[i] = newSensor;
            }
        }
        else if (registered)
        {
            if (!connected)
            {
                //device removed
                #ifdef DEBUG
                    printf("[SIF] Device removed at %i\r\n", i);
                #endif
                this->sensors.erase(i);
            }
        }
    }
    
    return 1;
}
 
 
sensor& sensorinterface::findType(sensor& s, int address)
{
    char response[3];
    char cmd[1] = {'T'};
 
    this->i2c.write(address, cmd, 1);
    int type_timeout = 10;
    i2c.stop();
    i2c.start();
    while( (this->i2c.read(address, response, 3)) && (type_timeout) )
    {
        --type_timeout;
        if (type_timeout == 2)
        {
            #ifdef DEBUG
                error(3);
            #endif
            return s;
        }
    }

    if (response[0] != 'T')
    {
        #ifdef DEBUG
            error(1);
        #endif
    }
    else
    {
        //seperate the incoming bits
        if ((response[1] != 0x00) && (response[1] != 0x01) && (response[1] != 0x02))
        {
            //if its not a recognised type
            #ifdef DEBUG
                error(2);
            #endif
        }
    }
    s.type = response[1];
    s.readingSize = response[2]; 
    return s;
}
 
int sensorinterface::sendRequest(char address)
{
    char cmd[1] = {'R'};
 
    this->i2c.write(address, cmd, 1);
    
    return 1;
}
 
d_reply sensorinterface::readData()
{
    #ifdef DEBUG
        printf("[SIF] Reading data from current device (0x%.2X)\r\n", currentSensor->first);
    #endif
    char address = currentSensor->first;
    char cmd[1] = {'D'};
    char bufSize = currentSensor->second.readingSize + 2;
    char buffer[bufSize];
    this->i2c.write(address, cmd, 1);
    wait(0.01);
    this->i2c.read(address, buffer, bufSize);
    
    d_reply reply;
    
    reply.type = currentSensor->second.type;
    
    for (int i = 2; i < bufSize; i++)
    {
        reply.readings.push_back(buffer[i]);
    }
 
    if (buffer[0] != 'D')
    {
        //if it doesn't send the correct header bit, something has gone wrong
        #ifdef DEBUG
            error(0);
        #endif
    }
    
    // TODO sync ready property to ready line - don't manually
    this->ready = false;
    
    if (currentSensor != sensors.end() && sensors.size() > 1 )
    {
        #ifdef DEBUG
            printf("[SIF] Continuing chain of devices, just read from device: 0x%.2X\r\n", currentSensor->first);
        #endif
        currentSensor++;
        sendRequest(currentSensor->first);
    }
    #ifdef DEBUG
    else
    {
        printf("[SIF] All devices have been read from, end of chain\r\n");
    }
    #endif
    
    return reply;
}