POC1.5 prototype 2 x color sensor 2 x LM75B 3 x AnalogIn 1 x accel

Dependencies:   mbed vt100

edge_sensor/edge_pressure.cpp

Committer:
Rhyme
Date:
2017-12-07
Revision:
8:5590f55bdf41
Parent:
7:aa858d789025
Child:
9:f958fa2cdc74

File content as of revision 8:5590f55bdf41:

#include "mbed.h"
#include "edge_sensor.h"
#include "edge_time.h"
#include "PSE530.h"
#include "edge_pressure.h"
#include "SO1602A.h"

extern SO1602A *display ; /* OLED display on I2C */

#define LOW_THR   0.2
#define HIGH_THR  0.3 
#define MIN_TEMP 12.0
#define MAX_TEMP 30.0

/**
 * SMC PSE530 pressure sensor
 * analog output 1.0V - 5.0V
 * 1.0V : 0
 * 5.0V : 1MPa
 * (at 0.6V : -0.1MPa)
 * Our sensor I/F converts 0-5V to 0-3V
 * So we suppose V = Analog Float Value : Pressure
 * 0.6V = 0.2 : 0
 * 3.0V = 1.0 : 1MPa
 */
 
float temp2expected(float temp)
{
    const float coef_A = 0.089 ;
    const float coef_B = 0.831 ;
    float pressure ;

    pressure = temp * coef_A + coef_B ;
    return( pressure ) ;
}

edge_pressure::edge_pressure(PSE530 *pse)
{
    _pse = pse ;
    _value = 0.0 ;
    _interval = 30 ;
}

edge_pressure::~edge_pressure(void)
{
    if (_pse) {
        delete _pse ;
    }
    _value = 0.0 ;
}

float edge_pressure::get_value(void)
{
    float value = 0.0 ;
    value = _pse->getPressure() ;
    return( value ) ;
}

void edge_pressure::reset(void) 
{
    _value = 0.0 ;
    _sampled_time = 0 ;
}

void edge_pressure::prepare(void) 
{
}

void edge_pressure::sample(void) 
{
    _value = get_value() ;
    _sampled_time = edge_time ;
}

int edge_pressure::deliver(void) 
{
    char str_buf[32] ;
    char timestr[16] ;
    int result ;
    float expected ;
    print_time(_sampled_time) ;
    if (current_temp != 0) {
        sprintf(str_buf, "GAS:%.2f @ %.1fC", _value, *current_temp ) ;
    } else {
        sprintf(str_buf, "GAS:%.2f ", _value  ) ;
    }
    printf(" ") ;
    printf(str_buf) ;
    if (display != 0) {
        display->clearDisplay() ;
        display->locate(0, 0) ;
        display->putStr(str_buf) ;
    }
    if (current_temp != 0) {
        expected = temp2expected(*current_temp) ;
        if (_value > (expected + HIGH_THR)) {
            sprintf(str_buf, "High [exp %.2f]", expected) ;
        } else if (_value < (expected - LOW_THR)) {
            sprintf(str_buf, "Low [exp %.2f]", expected) ;
        } else {
            sprintf(str_buf, "Normal") ;
        }
    }
    if (display != 0) {
        display->locate(0, 1) ;
        display->putStr(str_buf) ;
    }
    printf(" %s\n", str_buf) ;
    time2seq(_sampled_time, timestr) ;
//    printf(str_buf) ;
//    printf("\n") ;
    sprintf(_str_buf,
       "{\"DEVICE\":\"PRESS\",\"PN\":\"PSE530\",\"VAL\":\"%.3f\",\"UNIT\":\"kgf/cm2\",\"S\":\"%s\",\"E\":\"%d\"}",
       _value, timestr, _error_count) ;
    result = afero->setAttribute(1, _str_buf) ;
    return( result == afSUCCESS ) ;
}

void edge_pressure::recv_config(void)
{
}

void edge_pressure::send_config(void)
{
}