I2C hang recover function added

Dependencies:   UniGraphic mbed vt100

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers edge_accel.cpp Source File

edge_accel.cpp

00001 #include "mbed.h"
00002 #include "afLib.h"
00003 #include "edge_reset_mgr.h"
00004 #include "edge_sensor.h"
00005 #include "edge_accel.h"
00006 #include "MMA8451Q.h"
00007 
00008 edge_accel::edge_accel(MMA8451Q *accel) : edge_sensor() 
00009 {
00010     _accel = accel ;
00011     _sample_count = 0 ;
00012     _accumulation = 0 ;
00013     _prev_x = 0 ;
00014     _prev_y = 0 ;
00015     _prev_z = 0 ;
00016     
00017     _interval = 30 ;
00018 }
00019 
00020 edge_accel::~edge_accel(void)
00021 {
00022     delete _accel ;
00023 }
00024 
00025 void    edge_accel::reset(void) 
00026 {
00027     clear_value() ;
00028     edge_sensor::reset() ;
00029 }
00030 
00031 #if 0
00032 void    edge_accel::prepare(void) 
00033 {
00034 //    printf("accel prepare\n") ;
00035 }
00036 #endif
00037 
00038 int    edge_accel::sample(void) 
00039 {
00040     int result ;
00041     float theValue = 0.0 ;
00042     if (_sample_count > 1) { /* if sample is 1 or less, no data */
00043         _num_sampled = _sample_count - 1 ;
00044         theValue = (float)_accumulation / (float)(_num_sampled) ;
00045         result = 0 ; /* success */
00046     } else {
00047         result = 1 ; /* fail! */
00048     }
00049     _value = theValue / 4096.0 ;
00050     _sampled_time = edge_time ;
00051     _sample_count = 0 ;
00052     _accumulation = 0 ;
00053     return( result ) ;
00054 }
00055 
00056 int    edge_accel::deliver(void) 
00057 {
00058     int result ;
00059     char timestr[16] ;
00060     
00061     print_time(_sampled_time) ;
00062 //    _value = get_value() ;
00063     printf(" accel: %.3f [%d samples]\n", _value, _num_sampled) ;
00064     time2seq(_sampled_time, timestr) ;
00065     sprintf(_str_buf,
00066         "{\"DEVICE\":\"ACCEL\",\"PN\":\"MMA8451Q\",\"VAL_X\":\"%.3f\",\"VAL_Y\":\"0\",\"VAL_Z\":\"0\",\"UNIT\":\"g\",\"T\":\"%s\",\"E\":\"%d\"}",
00067         _value, timestr, _error_count) ;
00068     result = afero->setAttribute(1, _str_buf) ;
00069 
00070     return( result == afSUCCESS ) ;
00071 }
00072 
00073 int accel_v2y(float value, edge_chart_type *p)
00074 {
00075     int y ;
00076     if (value < p->min) {
00077         value = p->min ;
00078     } else if (value > p->max) {
00079         value = p->max ;
00080     }
00081     y = p->top + p->height/2 - 1
00082         - (int)((p->height - 2) * value /(p->max - p->min)) ;
00083     return( y ) ;
00084 }
00085 
00086 void edge_accel::show(void)
00087 {
00088     int x, y ;
00089     edge_chart_type *p = &edge_chart[0] ; /* edge_chart for accel */
00090     reset_watch_dog() ;
00091     if (display) {
00092         switch(display_mode) {
00093         case DISPLAY_MODE_SUMMARY:
00094             display->BusEnable(true) ;
00095             display->set_font((unsigned char*) Arial12x12);
00096             display->set_font_zoom(2, 2) ;
00097             display->foreground(White) ;
00098             display->locate(EDGE_SUMMARY_X, EDGE_SUMMARY_TIME_Y) ;
00099             displayTime(_sampled_time) ;
00100 //          display->locate(10, 5) ;
00101 //          display->printf(timestr) ;
00102             display->locate(EDGE_SUMMARY_X, EDGE_SUMMARY_ACCEL_Y) ;
00103             display->printf("Accel: %.3f [%4d]", _value, _num_sampled) ;
00104             display->BusEnable(false) ;
00105             reset_watch_dog() ;
00106             break ;
00107         case DISPLAY_MODE_CHART:
00108             x = p->left + p->index + 1;
00109             y = accel_v2y(_value, p) ;
00110             display->BusEnable(true) ;
00111             if (p->index == 0) {
00112                 draw_chart_frame(p) ;
00113             }
00114             display->pixel(x, y, White) ;
00115             display->BusEnable(false) ;
00116             p->index = (p->index + 1) % (p->width - 2) ;
00117             break ;
00118         default:
00119             break ;
00120         }
00121     }
00122     clear_value() ;
00123     reset_watch_dog() ;
00124 }
00125 
00126 int edge_accel::accum(void)
00127 {
00128     int result ;
00129     int16_t value[3] ;
00130 
00131     if (_enable) {
00132        result = _accel->getAllRawData(value) ;
00133        
00134         if (result == 0) { /* success */
00135             if (_sample_count != 0) { /* first data does not have prev_data */
00136                     _accumulation +=
00137                     abs(_prev_x - value[0])
00138                     + abs(_prev_y - value[1])
00139                     + abs(_prev_z - value[2]) ; 
00140             }
00141             
00142             _sample_count++ ;
00143     
00144             _prev_x = value[0] ;
00145             _prev_y = value[1] ;
00146             _prev_z = value[2] ;
00147         }
00148     }
00149         
00150     return( result ) ;
00151 }
00152 
00153 void edge_accel::clear_value(void)
00154 {
00155     _sample_count = 0 ;
00156     _accumulation = 0 ;
00157     _prev_x = 0 ;
00158     _prev_y = 0 ;
00159     _prev_z = 0 ;
00160 }
00161 
00162 
00163