Added a GPIO to power on/off for external I2C sensor(s) (with LEDs)

Dependencies:   UniGraphic mbed vt100

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers edge_pressure.cpp Source File

edge_pressure.cpp

00001 #include "mbed.h"
00002 #include "edge_sensor.h"
00003 #include "edge_time.h"
00004 #include "edge_reset_mgr.h"
00005 #include "PSE530.h"
00006 #include "edge_pressure.h"
00007 #include <ILI9341.h>
00008 #include "edge_chart.h"
00009 // #include "SO1602A.h"
00010 
00011 // extern SO1602A *display ; /* OLED display on I2C */
00012 extern ILI9341 *display ;
00013 
00014 #define LOW_THR   0.2
00015 #define HIGH_THR  0.3 
00016 #define MIN_TEMP 12.0
00017 #define MAX_TEMP 30.0
00018 
00019 /**
00020  * SMC PSE530 pressure sensor
00021  * analog output 1.0V - 5.0V
00022  * 1.0V : 0
00023  * 5.0V : 1MPa
00024  * (at 0.6V : -0.1MPa)
00025  * Our sensor I/F converts 0-5V to 0-1
00026  * So we suppose V = Analog Float Value : Pressure
00027  * 0.2 =  0
00028  * 1.0 = 1MPa
00029  */
00030  
00031 float temp2expected(float temp)
00032 {
00033     const float coef_A = 0.089 ;
00034     const float coef_B = 0.831 ;
00035     float pressure ;
00036 
00037     pressure = temp * coef_A + coef_B ;
00038     return( pressure ) ;
00039 }
00040 
00041 edge_pressure::edge_pressure(PSE530 *pse, DigitalOut *en)
00042 {
00043     _pse = pse ;
00044     _en = en ;
00045     _value = 0.0 ;
00046     _interval = 30 ;
00047     _thr_mode = 0 ;
00048     _thr_high = HIGH_THR ;
00049     _thr_low = LOW_THR ;
00050 }
00051 
00052 edge_pressure::~edge_pressure(void)
00053 {
00054     if (_pse) {
00055         delete _pse ;
00056     }
00057     _value = 0.0 ;
00058 }
00059 
00060 float edge_pressure::get_value(void)
00061 {
00062     float value = 0.0 ;
00063     value = _pse->getPressure() ;
00064     return( value ) ;
00065 }
00066 
00067 void edge_pressure::reset(void) 
00068 {
00069     _value = 0.0 ;
00070     _sampled_time = 0 ;
00071 }
00072 
00073 void edge_pressure::prepare(void) 
00074 {
00075 }
00076 
00077 int edge_pressure::sample(void) 
00078 {
00079     int result = 0 ;
00080     *_en = 1 ; /* enable pressure sensor */
00081     wait_ms(30) ;
00082     _value = get_value() ;
00083     _sampled_time = edge_time ;
00084     *_en = 0 ; /* disable pressure sensor */
00085     wait_ms(10) ; /* to avoid power transition effect remaining */
00086     return( result ) ; /* this always success */
00087 }
00088 
00089 void edge_pressure::set_thr_high(int16_t thr_high)
00090 {
00091     switch(_thr_mode) {
00092     case 0: /* absolute value */
00093         _thr_high = (float)thr_high/100.0 ;
00094         break ;
00095     case 1: /* persentage */
00096         _thr_high = (float)(thr_high/100.0) ;
00097         break ;
00098     default:
00099         printf("Unknown Threshold mode %d\n", _thr_mode) ;
00100         _thr_high = (float)thr_high/100.0 ;
00101         break ;
00102     }
00103 // printf("thr_high = %.3f\n", _thr_high) ;
00104 }
00105 
00106 float edge_pressure::get_thr_high(float expected)
00107 {
00108     float thr_high ;
00109     
00110     switch(_thr_mode) {
00111     case 0: /* absolute value */
00112         thr_high = expected + _thr_high ;
00113         break ;
00114     case 1: /* persentage */
00115         thr_high = expected * (1.0 + _thr_high) ;
00116         break ;
00117     default:
00118         printf("Unknown Threshold mode %d\n", _thr_mode) ;
00119         thr_high = expected + _thr_high ; /* use this as default */
00120         break ;
00121     }
00122     return (thr_high) ;
00123 }
00124 
00125 void edge_pressure::set_thr_low(int16_t thr_low)
00126 {   
00127     switch(_thr_mode) {
00128     case 0: /* absolute value */
00129         _thr_low = (float)thr_low/100.0 ;
00130         break ;
00131     case 1: /* persentage */
00132         _thr_low = (float)(thr_low/100.0) ;
00133         break ;
00134     default:
00135         printf("Unknown Threshold mode %d\n", _thr_mode) ;
00136         _thr_low = (float)thr_low/100.0 ;
00137         break ;
00138     }
00139 //printf("thr_low = %.3f\n", _thr_low) ;
00140 }
00141 
00142 float edge_pressure::get_thr_low(float expected)
00143 {
00144     float thr_low ;
00145     
00146     switch(_thr_mode) {
00147     case 0: /* absolute value */
00148         thr_low = expected - _thr_low ;
00149         break ;
00150     case 1: /* persentage */
00151         thr_low = expected * (1.0 - _thr_low) ;
00152         break ;
00153     default:
00154         printf("Unknown Threshold mode %d\n", _thr_mode) ;
00155         thr_low = expected + _thr_low ; /* use this as default */
00156         break ;
00157     }
00158     return (thr_low) ;
00159 }
00160 
00161 int edge_pressure::deliver(void) 
00162 {
00163     char str_buf[32] ;
00164     char timestr[16] ;
00165     int result ;
00166 
00167 reset_watch_dog() ;
00168     print_time(_sampled_time) ;
00169     if (current_temp != 0) {
00170         sprintf(str_buf, "GAS: %.3f kgf/cm2 @ %.1fC", _value, *current_temp ) ;
00171     } else {
00172         sprintf(str_buf, "GAS: %.3f kgf/cm2", _value  ) ;
00173     }
00174     printf(" ") ;
00175     printf(str_buf) ;
00176 
00177     if (current_temp != 0) {
00178 reset_watch_dog() ;
00179         _expected = temp2expected(*current_temp) ;
00180         _higher = get_thr_high(_expected) ;
00181         _lower = get_thr_low(_expected) ;
00182         printf(" (%.3f, %.3f) ", _higher, _lower) ;
00183     }
00184 
00185 reset_watch_dog() ;
00186     printf(" %s\n", str_buf) ;
00187     time2seq(_sampled_time, timestr) ;
00188 //    printf(str_buf) ;
00189 //    printf("\n") ;
00190     sprintf(_str_buf,
00191        "{\"DEVICE\":\"PRESS\",\"PN\":\"PSE530\",\"VAL\":\"%.3f\",\"UNIT\":\"kgf/cm2\",\"T\":\"%s\",\"E\":\"%d\"}",
00192        _value, timestr, _error_count) ;
00193 reset_watch_dog() ;
00194     result = afero->setAttribute(1, _str_buf) ;
00195     return( result == afSUCCESS ) ;
00196 }
00197 
00198 int v2x(float value)
00199 {
00200     int result ;
00201     if (value < 0) {
00202         result = 20 ;
00203     } else if (value > 4) {
00204         result = 300 ;
00205     } else {
00206         result = 20 + (int)(70 * value + 0.5) ;
00207     }
00208     return( result ) ;
00209 }
00210 
00211 int press_v2y(float value, edge_chart_type *p)
00212 {
00213     int y ;
00214     if (value < p->min) {
00215         value = p->min ;
00216     } else if (value > p->max) {
00217         value = p->max ;
00218     }
00219     y = p->top + p->height - 2
00220         - (int)((p->height - 2) * ((value - p->min) /(p->max - p->min))) ;
00221     return( y ) ;
00222 }
00223 
00224 /**
00225  * drawPointer
00226  *
00227  * draw a triangle pointer at value place
00228  * in GAS pressure display mode 
00229  */
00230 void edge_pressure::drawPointer(int c)
00231 {
00232     float delta_x ;
00233     int x[2], y, i ;
00234     const int top = 75 ;
00235     const int pointer_height = 15 ;
00236     for (i = 0 ; i < pointer_height ; i++ ) {
00237         y = top + i ;
00238         delta_x = i * 5.0 / 8.0 ;
00239         x[0] = c - delta_x ;
00240         x[1] = c + delta_x ;
00241         display->line(x[0], y, x[1], y, White) ;
00242     }
00243 }
00244 
00245 void edge_pressure::show(void)
00246 {
00247     edge_chart_type *p = &edge_chart[ _id ] ;
00248     uint16_t color = White ;
00249     char str_buf[32] ;
00250     int i, x, y, l, r, c, str_x ;
00251     if (display) {
00252         reset_watch_dog() ;
00253 /* for debug */
00254 //  _value = _lower - 0.5 ; /* LOW */
00255 // _value = (_higher + _lower) / 2 ; /* GOOD */
00256 // _value = _higher + 0.2 ; /* HIGH */
00257  
00258         if (_value > _higher) {
00259             sprintf(str_buf, "HIGH") ;
00260             color = Red ;
00261             str_x = 60 ;
00262         } else if (_value < _lower) {
00263             sprintf(str_buf, "LOW") ;
00264             color = Yellow ;
00265             str_x = 60 ;
00266         } else {
00267             sprintf(str_buf, "GOOD") ;
00268             color = Green ;
00269             str_x = 35 ;
00270         }
00271         switch(display_mode) {
00272         case DISPLAY_MODE_GAS:
00273             display->BusEnable(true) ;
00274             display->cls() ;
00275             /* printf frame */
00276             display->foreground(White) ;
00277             display->line(20, 75, 300, 75, White) ;
00278             for (i = 0 ; i <= 8 ; i++ ) {
00279                 x = 20 + i * 35 ;
00280                 if (i & 0x01) { /* odd */
00281                     display->line(x, 55, x, 95, White) ;
00282                 } else { /* even */
00283                     display->line(x, 45, x, 105, White) ;
00284                 }
00285             }
00286             display->set_font((unsigned char*) Arial28x28);
00287             for (i = 0 ; i <= 4 ; i++ ) {
00288                 x = 12 + i * 70 ;
00289                 display->locate(x, 10) ;
00290                 display->printf("%d", i) ;
00291             }
00292             /* print expected area and current pressure */
00293             l = v2x(_lower) ;
00294             r = v2x(_higher) ;
00295             c = v2x(_value) ;
00296 //            display->fillrect(l, 70, r, 80, Red) ;
00297             display->fillrect(l, 65, r, 74, Red) ;
00298 //            display->fillcircle(c, 75, 10, White) ;
00299             drawPointer(c) ;
00300                     
00301             /* print status */
00302             display->locate(str_x, 140) ;
00303             display->set_font_zoom(3, 3) ;
00304             display->foreground(color) ;
00305             display->printf(str_buf) ;
00306             display->set_font_zoom(1, 1) ;
00307             display->BusEnable(false) ;
00308             break ;
00309         case DISPLAY_MODE_SUMMARY:
00310             display->BusEnable(true) ;
00311             display->set_font((unsigned char*) Arial12x12);
00312             display->set_font_zoom(2, 2) ;
00313             display->foreground(White) ;
00314             display->locate(10, EDGE_SUMMARY_TIME_Y) ;
00315             displayTime(_sampled_time) ;
00316 //          display->locate(10,50) ;
00317 //          display->printf(timestr) ;
00318             display->locate(10, EDGE_SUMMARY_PRESS_Y) ;
00319             display->printf("Press: ") ;
00320             display->foreground(color) ;
00321             display->locate(90, EDGE_SUMMARY_PRESS_Y) ;
00322             display->printf("%.3f ", _value) ;
00323             display->foreground(White) ;
00324             display->printf("kgf/cm2") ;
00325             display->BusEnable(false) ;
00326             break ;
00327         case DISPLAY_MODE_CHART:
00328             x = p->left + p->index + 1;
00329             y = press_v2y(_value, p) ;
00330             display->BusEnable(true) ;
00331             if (p->index == 0) {
00332                 draw_chart_frame(p) ;
00333             }
00334             display->foreground(color) ;
00335             display->pixel(x, y, color) ;
00336             display->set_font((unsigned char*) Arial12x12);
00337             display->locate(p->left + 40, p->top + 5) ;
00338             display->printf("%5s", str_buf) ;
00339             display->foreground(White) ;
00340             display->BusEnable(false) ;
00341             p->index = (p->index + 1) % (p->width - 2) ;
00342             break ;
00343         }
00344     }
00345     reset_watch_dog() ;
00346 }