Added a GPIO to power on/off for external I2C sensor(s) (with LEDs)
Dependencies: UniGraphic mbed vt100
18-Jun-2018 外部センサの電源オン・オフ機能は下位互換の為に無効になっていました。 この版で再度有効にしました。
edge_sensor/edge_accel.cpp@0:846e2321c637, 2018-04-13 (annotated)
- Committer:
- Rhyme
- Date:
- Fri Apr 13 04:19:23 2018 +0000
- Revision:
- 0:846e2321c637
power to color sensor on/off test OK. Currently the function is disabled.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:846e2321c637 | 1 | #include "mbed.h" |
Rhyme | 0:846e2321c637 | 2 | #include "afLib.h" |
Rhyme | 0:846e2321c637 | 3 | #include "edge_reset_mgr.h" |
Rhyme | 0:846e2321c637 | 4 | #include "edge_sensor.h" |
Rhyme | 0:846e2321c637 | 5 | #include "edge_accel.h" |
Rhyme | 0:846e2321c637 | 6 | #include "MMA8451Q.h" |
Rhyme | 0:846e2321c637 | 7 | |
Rhyme | 0:846e2321c637 | 8 | edge_accel::edge_accel(MMA8451Q *accel) : edge_sensor() |
Rhyme | 0:846e2321c637 | 9 | { |
Rhyme | 0:846e2321c637 | 10 | _accel = accel ; |
Rhyme | 0:846e2321c637 | 11 | _sample_count = 0 ; |
Rhyme | 0:846e2321c637 | 12 | _accumulation = 0 ; |
Rhyme | 0:846e2321c637 | 13 | _prev_x = 0 ; |
Rhyme | 0:846e2321c637 | 14 | _prev_y = 0 ; |
Rhyme | 0:846e2321c637 | 15 | _prev_z = 0 ; |
Rhyme | 0:846e2321c637 | 16 | |
Rhyme | 0:846e2321c637 | 17 | _interval = 30 ; |
Rhyme | 0:846e2321c637 | 18 | } |
Rhyme | 0:846e2321c637 | 19 | |
Rhyme | 0:846e2321c637 | 20 | edge_accel::~edge_accel(void) |
Rhyme | 0:846e2321c637 | 21 | { |
Rhyme | 0:846e2321c637 | 22 | delete _accel ; |
Rhyme | 0:846e2321c637 | 23 | } |
Rhyme | 0:846e2321c637 | 24 | |
Rhyme | 0:846e2321c637 | 25 | void edge_accel::reset(void) |
Rhyme | 0:846e2321c637 | 26 | { |
Rhyme | 0:846e2321c637 | 27 | clear_value() ; |
Rhyme | 0:846e2321c637 | 28 | edge_sensor::reset() ; |
Rhyme | 0:846e2321c637 | 29 | } |
Rhyme | 0:846e2321c637 | 30 | |
Rhyme | 0:846e2321c637 | 31 | #if 0 |
Rhyme | 0:846e2321c637 | 32 | void edge_accel::prepare(void) |
Rhyme | 0:846e2321c637 | 33 | { |
Rhyme | 0:846e2321c637 | 34 | // printf("accel prepare\n") ; |
Rhyme | 0:846e2321c637 | 35 | } |
Rhyme | 0:846e2321c637 | 36 | #endif |
Rhyme | 0:846e2321c637 | 37 | |
Rhyme | 0:846e2321c637 | 38 | int edge_accel::sample(void) |
Rhyme | 0:846e2321c637 | 39 | { |
Rhyme | 0:846e2321c637 | 40 | int result ; |
Rhyme | 0:846e2321c637 | 41 | float theValue = 0.0 ; |
Rhyme | 0:846e2321c637 | 42 | if (_sample_count > 1) { /* if sample is 1 or less, no data */ |
Rhyme | 0:846e2321c637 | 43 | _num_sampled = _sample_count - 1 ; |
Rhyme | 0:846e2321c637 | 44 | theValue = (float)_accumulation / (float)(_num_sampled) ; |
Rhyme | 0:846e2321c637 | 45 | result = 0 ; /* success */ |
Rhyme | 0:846e2321c637 | 46 | } else { |
Rhyme | 0:846e2321c637 | 47 | result = 1 ; /* fail! */ |
Rhyme | 0:846e2321c637 | 48 | } |
Rhyme | 0:846e2321c637 | 49 | _value = theValue / 4096.0 ; |
Rhyme | 0:846e2321c637 | 50 | _sampled_time = edge_time ; |
Rhyme | 0:846e2321c637 | 51 | _sample_count = 0 ; |
Rhyme | 0:846e2321c637 | 52 | _accumulation = 0 ; |
Rhyme | 0:846e2321c637 | 53 | return( result ) ; |
Rhyme | 0:846e2321c637 | 54 | } |
Rhyme | 0:846e2321c637 | 55 | |
Rhyme | 0:846e2321c637 | 56 | int edge_accel::deliver(void) |
Rhyme | 0:846e2321c637 | 57 | { |
Rhyme | 0:846e2321c637 | 58 | int result ; |
Rhyme | 0:846e2321c637 | 59 | char timestr[16] ; |
Rhyme | 0:846e2321c637 | 60 | |
Rhyme | 0:846e2321c637 | 61 | print_time(_sampled_time) ; |
Rhyme | 0:846e2321c637 | 62 | // _value = get_value() ; |
Rhyme | 0:846e2321c637 | 63 | printf(" accel: %.3f [%d samples]\n", _value, _num_sampled) ; |
Rhyme | 0:846e2321c637 | 64 | time2seq(_sampled_time, timestr) ; |
Rhyme | 0:846e2321c637 | 65 | sprintf(_str_buf, |
Rhyme | 0:846e2321c637 | 66 | "{\"DEVICE\":\"ACCEL\",\"PN\":\"MMA8451Q\",\"VAL_X\":\"%.3f\",\"VAL_Y\":\"0\",\"VAL_Z\":\"0\",\"UNIT\":\"g\",\"T\":\"%s\",\"E\":\"%d\"}", |
Rhyme | 0:846e2321c637 | 67 | _value, timestr, _error_count) ; |
Rhyme | 0:846e2321c637 | 68 | result = afero->setAttribute(1, _str_buf) ; |
Rhyme | 0:846e2321c637 | 69 | |
Rhyme | 0:846e2321c637 | 70 | return( result == afSUCCESS ) ; |
Rhyme | 0:846e2321c637 | 71 | } |
Rhyme | 0:846e2321c637 | 72 | |
Rhyme | 0:846e2321c637 | 73 | int accel_v2y(float value, edge_chart_type *p) |
Rhyme | 0:846e2321c637 | 74 | { |
Rhyme | 0:846e2321c637 | 75 | int y ; |
Rhyme | 0:846e2321c637 | 76 | if (value < p->min) { |
Rhyme | 0:846e2321c637 | 77 | value = p->min ; |
Rhyme | 0:846e2321c637 | 78 | } else if (value > p->max) { |
Rhyme | 0:846e2321c637 | 79 | value = p->max ; |
Rhyme | 0:846e2321c637 | 80 | } |
Rhyme | 0:846e2321c637 | 81 | y = p->top + p->height/2 - 1 |
Rhyme | 0:846e2321c637 | 82 | - (int)((p->height - 2) * value /(p->max - p->min)) ; |
Rhyme | 0:846e2321c637 | 83 | return( y ) ; |
Rhyme | 0:846e2321c637 | 84 | } |
Rhyme | 0:846e2321c637 | 85 | |
Rhyme | 0:846e2321c637 | 86 | void edge_accel::show(void) |
Rhyme | 0:846e2321c637 | 87 | { |
Rhyme | 0:846e2321c637 | 88 | int x, y ; |
Rhyme | 0:846e2321c637 | 89 | edge_chart_type *p = &edge_chart[0] ; /* edge_chart for accel */ |
Rhyme | 0:846e2321c637 | 90 | reset_watch_dog() ; |
Rhyme | 0:846e2321c637 | 91 | if (display) { |
Rhyme | 0:846e2321c637 | 92 | switch(display_mode) { |
Rhyme | 0:846e2321c637 | 93 | case DISPLAY_MODE_SUMMARY: |
Rhyme | 0:846e2321c637 | 94 | display->BusEnable(true) ; |
Rhyme | 0:846e2321c637 | 95 | display->set_font((unsigned char*) Arial12x12); |
Rhyme | 0:846e2321c637 | 96 | display->set_font_zoom(2, 2) ; |
Rhyme | 0:846e2321c637 | 97 | display->foreground(White) ; |
Rhyme | 0:846e2321c637 | 98 | display->locate(EDGE_SUMMARY_X, EDGE_SUMMARY_TIME_Y) ; |
Rhyme | 0:846e2321c637 | 99 | displayTime(_sampled_time) ; |
Rhyme | 0:846e2321c637 | 100 | // display->locate(10, 5) ; |
Rhyme | 0:846e2321c637 | 101 | // display->printf(timestr) ; |
Rhyme | 0:846e2321c637 | 102 | display->locate(EDGE_SUMMARY_X, EDGE_SUMMARY_ACCEL_Y) ; |
Rhyme | 0:846e2321c637 | 103 | display->printf("Accel: %.3f [%4d]", _value, _num_sampled) ; |
Rhyme | 0:846e2321c637 | 104 | display->BusEnable(false) ; |
Rhyme | 0:846e2321c637 | 105 | reset_watch_dog() ; |
Rhyme | 0:846e2321c637 | 106 | break ; |
Rhyme | 0:846e2321c637 | 107 | case DISPLAY_MODE_CHART: |
Rhyme | 0:846e2321c637 | 108 | x = p->left + p->index + 1; |
Rhyme | 0:846e2321c637 | 109 | y = accel_v2y(_value, p) ; |
Rhyme | 0:846e2321c637 | 110 | display->BusEnable(true) ; |
Rhyme | 0:846e2321c637 | 111 | if (p->index == 0) { |
Rhyme | 0:846e2321c637 | 112 | draw_chart_frame(p) ; |
Rhyme | 0:846e2321c637 | 113 | } |
Rhyme | 0:846e2321c637 | 114 | display->pixel(x, y, White) ; |
Rhyme | 0:846e2321c637 | 115 | display->BusEnable(false) ; |
Rhyme | 0:846e2321c637 | 116 | p->index = (p->index + 1) % (p->width - 2) ; |
Rhyme | 0:846e2321c637 | 117 | break ; |
Rhyme | 0:846e2321c637 | 118 | default: |
Rhyme | 0:846e2321c637 | 119 | break ; |
Rhyme | 0:846e2321c637 | 120 | } |
Rhyme | 0:846e2321c637 | 121 | } |
Rhyme | 0:846e2321c637 | 122 | clear_value() ; |
Rhyme | 0:846e2321c637 | 123 | reset_watch_dog() ; |
Rhyme | 0:846e2321c637 | 124 | } |
Rhyme | 0:846e2321c637 | 125 | |
Rhyme | 0:846e2321c637 | 126 | int edge_accel::accum(void) |
Rhyme | 0:846e2321c637 | 127 | { |
Rhyme | 0:846e2321c637 | 128 | int result ; |
Rhyme | 0:846e2321c637 | 129 | int16_t value[3] ; |
Rhyme | 0:846e2321c637 | 130 | |
Rhyme | 0:846e2321c637 | 131 | if (_enable) { |
Rhyme | 0:846e2321c637 | 132 | result = _accel->getAllRawData(value) ; |
Rhyme | 0:846e2321c637 | 133 | |
Rhyme | 0:846e2321c637 | 134 | if (result == 0) { /* success */ |
Rhyme | 0:846e2321c637 | 135 | if (_sample_count != 0) { /* first data does not have prev_data */ |
Rhyme | 0:846e2321c637 | 136 | _accumulation += |
Rhyme | 0:846e2321c637 | 137 | abs(_prev_x - value[0]) |
Rhyme | 0:846e2321c637 | 138 | + abs(_prev_y - value[1]) |
Rhyme | 0:846e2321c637 | 139 | + abs(_prev_z - value[2]) ; |
Rhyme | 0:846e2321c637 | 140 | } |
Rhyme | 0:846e2321c637 | 141 | |
Rhyme | 0:846e2321c637 | 142 | _sample_count++ ; |
Rhyme | 0:846e2321c637 | 143 | |
Rhyme | 0:846e2321c637 | 144 | _prev_x = value[0] ; |
Rhyme | 0:846e2321c637 | 145 | _prev_y = value[1] ; |
Rhyme | 0:846e2321c637 | 146 | _prev_z = value[2] ; |
Rhyme | 0:846e2321c637 | 147 | } |
Rhyme | 0:846e2321c637 | 148 | } |
Rhyme | 0:846e2321c637 | 149 | |
Rhyme | 0:846e2321c637 | 150 | return( result ) ; |
Rhyme | 0:846e2321c637 | 151 | } |
Rhyme | 0:846e2321c637 | 152 | |
Rhyme | 0:846e2321c637 | 153 | void edge_accel::clear_value(void) |
Rhyme | 0:846e2321c637 | 154 | { |
Rhyme | 0:846e2321c637 | 155 | _sample_count = 0 ; |
Rhyme | 0:846e2321c637 | 156 | _accumulation = 0 ; |
Rhyme | 0:846e2321c637 | 157 | _prev_x = 0 ; |
Rhyme | 0:846e2321c637 | 158 | _prev_y = 0 ; |
Rhyme | 0:846e2321c637 | 159 | _prev_z = 0 ; |
Rhyme | 0:846e2321c637 | 160 | } |
Rhyme | 0:846e2321c637 | 161 | |
Rhyme | 0:846e2321c637 | 162 | |
Rhyme | 0:846e2321c637 | 163 |