GDP group 24 node core

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue

sensorinterface.cpp

Committer:
Trumple
Date:
2014-11-11
Revision:
0:fcab3b154e49
Child:
1:27b35752c5d0

File content as of revision 0:fcab3b154e49:

#include "sensorinterface.h"
 
sensorinterface::sensorinterface() : i2c(p28,p27), readyLine(p30)
{
    readyLine.fall(this, &sensorinterface::readyTrigger);
    
    this->ready = false;
    
    char buffer[255];
    
    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.type = findType(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()
{
    currentSensor = sensors.begin();
    
    #ifdef DEBUG
        printf("[SIF] Beginning device query chain from 0x%.2X\r\n", currentSensor->first);
    #endif
    
    sendRequest(currentSensor->first);
}

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.type = findType(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;
}
 
 
char sensorinterface::findType(int address)
{
    char temp[3];
    char cmd[1] = {0x54};
 
    this->i2c.write(address, cmd, 1);
    int type_timeout = 10;
    i2c.stop();
    i2c.start();
    while( (this->i2c.read(address, temp, 3)) && (type_timeout) )
    {
        --type_timeout;
        if (type_timeout == 2)
        {
            #ifdef DEBUG
                error(3);
            #endif
            return 1;
        }
    }

    if (temp[0] != 'T')
    {
        #ifdef DEBUG
            error(1);
        #endif
    }
    else
    {
        //seperate the incoming bits
        if ((temp[1] != 0x00) && (temp[1] != 0x01) && (temp[1] != 0x02))
        {
            //if its not a recognised type
            #ifdef DEBUG
                error(2);
            #endif
        }
    }
    return temp[1];
}
 
int sensorinterface::sendRequest(char address)
{
    char cmd[1] = {0x52};
 
    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] = {0x44};
    char buffer [18] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    this->i2c.write(address, cmd, 1);
    wait(0.01);
    this->i2c.read(address, buffer, 18);
    
    d_reply reply;
    
    reply.type = currentSensor->second.type;
    
    for (int i = 2; i <= 17; i += 2)
    {
        uint16_t reading = buffer[i];
        reading = reading << 8;
        reading |= buffer[i+1];
        reply.readings.push_back(reading);
    }
 
    if (buffer[0] != 'D')
    {
        //if it doesn't send the correct header bit, something has gone wrong
        #ifdef DEBUG
            error(0);
        #endif
    }
    else
    {
        
    }
    
    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;
}