UART Command Parser Time Manager Data Store for SD Card for stm32l476 [it's not Licensed as BSD/GPLx]
Dependencies: mbed SDFileSystem
common/DeviceRepeater.cpp@8:b18a8764ecae, 2019-05-11 (annotated)
- Committer:
- Inscape_ao
- Date:
- Sat May 11 04:20:42 2019 +0000
- Revision:
- 8:b18a8764ecae
- Parent:
- 7:9ab8809f9693
- Child:
- 9:c81d0df866f5
add TODO comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Inscape_ao | 7:9ab8809f9693 | 1 | /** --- Includes --- */ |
Inscape_ao | 7:9ab8809f9693 | 2 | #include "mbed.h" |
Inscape_ao | 7:9ab8809f9693 | 3 | #include "global.h" |
Inscape_ao | 7:9ab8809f9693 | 4 | #include "string.h" |
Inscape_ao | 7:9ab8809f9693 | 5 | #include "DeviceRepeater.h" |
Inscape_ao | 7:9ab8809f9693 | 6 | |
Inscape_ao | 7:9ab8809f9693 | 7 | void repeater_basetick(void) |
Inscape_ao | 7:9ab8809f9693 | 8 | { |
Inscape_ao | 7:9ab8809f9693 | 9 | pDevRept->tick(); |
Inscape_ao | 7:9ab8809f9693 | 10 | } |
Inscape_ao | 7:9ab8809f9693 | 11 | |
Inscape_ao | 7:9ab8809f9693 | 12 | DeviceRepeater::DeviceRepeater(DeviceDriver *setDriver) |
Inscape_ao | 7:9ab8809f9693 | 13 | { |
Inscape_ao | 7:9ab8809f9693 | 14 | pClock = new Ticker(); |
Inscape_ao | 7:9ab8809f9693 | 15 | selected_id = NOT_ID_SELECTED; |
Inscape_ao | 7:9ab8809f9693 | 16 | pDriver = setDriver; |
Inscape_ao | 7:9ab8809f9693 | 17 | stat = DEV_NOT_INIT; |
Inscape_ao | 7:9ab8809f9693 | 18 | repeat_max_count = DEFAULT_REPEAT_MAX_COUNT; |
Inscape_ao | 7:9ab8809f9693 | 19 | repeat_stride_sec = DEFAULT_REPEAT_STRIDE; |
Inscape_ao | 7:9ab8809f9693 | 20 | } |
Inscape_ao | 7:9ab8809f9693 | 21 | |
Inscape_ao | 7:9ab8809f9693 | 22 | bool DeviceRepeater::readyToStart(void) |
Inscape_ao | 7:9ab8809f9693 | 23 | { |
Inscape_ao | 7:9ab8809f9693 | 24 | bool ret; |
Inscape_ao | 7:9ab8809f9693 | 25 | switch(stat) { |
Inscape_ao | 7:9ab8809f9693 | 26 | case DEV_READY: |
Inscape_ao | 7:9ab8809f9693 | 27 | ret = true; |
Inscape_ao | 7:9ab8809f9693 | 28 | break; |
Inscape_ao | 7:9ab8809f9693 | 29 | case DEV_NOT_INIT: |
Inscape_ao | 7:9ab8809f9693 | 30 | ret = true; |
Inscape_ao | 7:9ab8809f9693 | 31 | break; |
Inscape_ao | 7:9ab8809f9693 | 32 | case DEV_RUNNING: |
Inscape_ao | 7:9ab8809f9693 | 33 | ret = false; |
Inscape_ao | 7:9ab8809f9693 | 34 | break; |
Inscape_ao | 7:9ab8809f9693 | 35 | default: |
Inscape_ao | 7:9ab8809f9693 | 36 | ret = false; |
Inscape_ao | 7:9ab8809f9693 | 37 | break; |
Inscape_ao | 7:9ab8809f9693 | 38 | } |
Inscape_ao | 7:9ab8809f9693 | 39 | return ret; |
Inscape_ao | 7:9ab8809f9693 | 40 | } |
Inscape_ao | 7:9ab8809f9693 | 41 | |
Inscape_ao | 7:9ab8809f9693 | 42 | bool DeviceRepeater::setRepeatCount(int maxcount) |
Inscape_ao | 7:9ab8809f9693 | 43 | { |
Inscape_ao | 7:9ab8809f9693 | 44 | /* never accept in Not Ready To Run */ |
Inscape_ao | 7:9ab8809f9693 | 45 | if (!this->readyToStart()) { |
Inscape_ao | 7:9ab8809f9693 | 46 | return false; |
Inscape_ao | 7:9ab8809f9693 | 47 | } |
Inscape_ao | 7:9ab8809f9693 | 48 | /* invalid arguments */ |
Inscape_ao | 7:9ab8809f9693 | 49 | if (maxcount < 0) { |
Inscape_ao | 7:9ab8809f9693 | 50 | return false; |
Inscape_ao | 7:9ab8809f9693 | 51 | } |
Inscape_ao | 7:9ab8809f9693 | 52 | repeat_max_count = maxcount; |
Inscape_ao | 7:9ab8809f9693 | 53 | return true; |
Inscape_ao | 7:9ab8809f9693 | 54 | } |
Inscape_ao | 7:9ab8809f9693 | 55 | |
Inscape_ao | 7:9ab8809f9693 | 56 | bool DeviceRepeater::setRepeatStride(int sec) |
Inscape_ao | 7:9ab8809f9693 | 57 | { |
Inscape_ao | 7:9ab8809f9693 | 58 | /* never accept in Not Ready To Run */ |
Inscape_ao | 7:9ab8809f9693 | 59 | if (!this->readyToStart()) { |
Inscape_ao | 7:9ab8809f9693 | 60 | return false; |
Inscape_ao | 7:9ab8809f9693 | 61 | } |
Inscape_ao | 7:9ab8809f9693 | 62 | /* invalid arguments */ |
Inscape_ao | 7:9ab8809f9693 | 63 | if (sec < 0) { |
Inscape_ao | 7:9ab8809f9693 | 64 | return false; |
Inscape_ao | 7:9ab8809f9693 | 65 | } else if (sec == 0) { |
Inscape_ao | 7:9ab8809f9693 | 66 | return false; |
Inscape_ao | 7:9ab8809f9693 | 67 | } |
Inscape_ao | 7:9ab8809f9693 | 68 | repeat_stride_sec = sec; |
Inscape_ao | 7:9ab8809f9693 | 69 | return true; |
Inscape_ao | 7:9ab8809f9693 | 70 | } |
Inscape_ao | 7:9ab8809f9693 | 71 | |
Inscape_ao | 7:9ab8809f9693 | 72 | bool DeviceRepeater::start(void) |
Inscape_ao | 7:9ab8809f9693 | 73 | { |
Inscape_ao | 7:9ab8809f9693 | 74 | bool ret; |
Inscape_ao | 7:9ab8809f9693 | 75 | switch(stat) { |
Inscape_ao | 7:9ab8809f9693 | 76 | case DEV_READY: |
Inscape_ao | 7:9ab8809f9693 | 77 | break; |
Inscape_ao | 7:9ab8809f9693 | 78 | case DEV_NOT_INIT: |
Inscape_ao | 7:9ab8809f9693 | 79 | ret = this->init(); |
Inscape_ao | 7:9ab8809f9693 | 80 | if (ret != true) { |
Inscape_ao | 7:9ab8809f9693 | 81 | return false; |
Inscape_ao | 7:9ab8809f9693 | 82 | } |
Inscape_ao | 7:9ab8809f9693 | 83 | break; |
Inscape_ao | 7:9ab8809f9693 | 84 | case DEV_RUNNING: |
Inscape_ao | 7:9ab8809f9693 | 85 | return false; |
Inscape_ao | 7:9ab8809f9693 | 86 | case DEV_FATAL: |
Inscape_ao | 7:9ab8809f9693 | 87 | default: |
Inscape_ao | 7:9ab8809f9693 | 88 | return false; |
Inscape_ao | 7:9ab8809f9693 | 89 | } |
Inscape_ao | 7:9ab8809f9693 | 90 | // Device is in DEV_READY |
Inscape_ao | 7:9ab8809f9693 | 91 | |
Inscape_ao | 8:b18a8764ecae | 92 | /* TODO - add callback for changing READY to RUNNING */ |
Inscape_ao | 8:b18a8764ecae | 93 | |
Inscape_ao | 7:9ab8809f9693 | 94 | // start Runnig |
Inscape_ao | 7:9ab8809f9693 | 95 | // Device is in DEV_RUNNING |
Inscape_ao | 7:9ab8809f9693 | 96 | stat = DEV_RUNNING; |
Inscape_ao | 7:9ab8809f9693 | 97 | repeat_remain_cnt = repeat_max_count; |
Inscape_ao | 7:9ab8809f9693 | 98 | repeat_remain_sec = repeat_stride_sec; |
Inscape_ao | 7:9ab8809f9693 | 99 | pClock->attach(repeater_basetick, 1.0); |
Inscape_ao | 7:9ab8809f9693 | 100 | return true; |
Inscape_ao | 7:9ab8809f9693 | 101 | } |
Inscape_ao | 7:9ab8809f9693 | 102 | |
Inscape_ao | 7:9ab8809f9693 | 103 | bool DeviceRepeater::stop(void) |
Inscape_ao | 7:9ab8809f9693 | 104 | { |
Inscape_ao | 7:9ab8809f9693 | 105 | if (stat != DEV_RUNNING) { |
Inscape_ao | 7:9ab8809f9693 | 106 | return false; |
Inscape_ao | 7:9ab8809f9693 | 107 | } |
Inscape_ao | 7:9ab8809f9693 | 108 | |
Inscape_ao | 7:9ab8809f9693 | 109 | /* Delaied Stop */ |
Inscape_ao | 7:9ab8809f9693 | 110 | stat = DEV_REQ_STOP; |
Inscape_ao | 7:9ab8809f9693 | 111 | repeat_remain_sec = 0; |
Inscape_ao | 7:9ab8809f9693 | 112 | stat = DEV_READY; |
Inscape_ao | 7:9ab8809f9693 | 113 | return true; |
Inscape_ao | 7:9ab8809f9693 | 114 | } |
Inscape_ao | 7:9ab8809f9693 | 115 | |
Inscape_ao | 7:9ab8809f9693 | 116 | void DeviceRepeater::tick(void) |
Inscape_ao | 7:9ab8809f9693 | 117 | { |
Inscape_ao | 7:9ab8809f9693 | 118 | if (stat != DEV_RUNNING) { |
Inscape_ao | 7:9ab8809f9693 | 119 | /* Delaied Stop */ |
Inscape_ao | 7:9ab8809f9693 | 120 | if (stat != DEV_REQ_STOP) { |
Inscape_ao | 8:b18a8764ecae | 121 | /*****************************************************/ |
Inscape_ao | 8:b18a8764ecae | 122 | /* TODO - add callback for changing RUNNING to READY */ |
Inscape_ao | 8:b18a8764ecae | 123 | /*****************************************************/ |
Inscape_ao | 7:9ab8809f9693 | 124 | repeat_remain_sec = 0; |
Inscape_ao | 7:9ab8809f9693 | 125 | stat = DEV_READY; |
Inscape_ao | 7:9ab8809f9693 | 126 | return; |
Inscape_ao | 7:9ab8809f9693 | 127 | } |
Inscape_ao | 7:9ab8809f9693 | 128 | return; |
Inscape_ao | 7:9ab8809f9693 | 129 | } |
Inscape_ao | 7:9ab8809f9693 | 130 | if (repeat_remain_sec > 0) { |
Inscape_ao | 8:b18a8764ecae | 131 | /*****************************************************/ |
Inscape_ao | 8:b18a8764ecae | 132 | /* TODO - kick Senser and send XFD/XDS Data */ |
Inscape_ao | 8:b18a8764ecae | 133 | /*****************************************************/ |
Inscape_ao | 7:9ab8809f9693 | 134 | /** Sensing and Sending Data **/ |
Inscape_ao | 7:9ab8809f9693 | 135 | repeat_remain_sec--; |
Inscape_ao | 7:9ab8809f9693 | 136 | } |
Inscape_ao | 7:9ab8809f9693 | 137 | if (repeat_remain_sec <= 0) { |
Inscape_ao | 7:9ab8809f9693 | 138 | if (repeat_remain_cnt > 0) { |
Inscape_ao | 7:9ab8809f9693 | 139 | repeat_remain_cnt--; |
Inscape_ao | 7:9ab8809f9693 | 140 | if (repeat_remain_cnt == 0) { |
Inscape_ao | 7:9ab8809f9693 | 141 | /* STOP ticking (0->1) */ |
Inscape_ao | 8:b18a8764ecae | 142 | /*****************************************************/ |
Inscape_ao | 8:b18a8764ecae | 143 | /* TODO - add callback for changing RUNNING to READY */ |
Inscape_ao | 8:b18a8764ecae | 144 | /*****************************************************/ |
Inscape_ao | 7:9ab8809f9693 | 145 | repeat_remain_sec = 0; |
Inscape_ao | 7:9ab8809f9693 | 146 | stat = DEV_READY; |
Inscape_ao | 7:9ab8809f9693 | 147 | pClock->detach(); |
Inscape_ao | 7:9ab8809f9693 | 148 | return; |
Inscape_ao | 7:9ab8809f9693 | 149 | } |
Inscape_ao | 7:9ab8809f9693 | 150 | /* REFILL */ |
Inscape_ao | 7:9ab8809f9693 | 151 | } else if (repeat_remain_cnt == 0) { |
Inscape_ao | 7:9ab8809f9693 | 152 | /* REFILL */ |
Inscape_ao | 7:9ab8809f9693 | 153 | } else { |
Inscape_ao | 8:b18a8764ecae | 154 | /*****************************************************/ |
Inscape_ao | 7:9ab8809f9693 | 155 | /* TODO : FATAL */ |
Inscape_ao | 8:b18a8764ecae | 156 | /*****************************************************/ |
Inscape_ao | 7:9ab8809f9693 | 157 | while(1); |
Inscape_ao | 7:9ab8809f9693 | 158 | } |
Inscape_ao | 7:9ab8809f9693 | 159 | repeat_remain_sec = repeat_stride_sec; |
Inscape_ao | 7:9ab8809f9693 | 160 | } |
Inscape_ao | 7:9ab8809f9693 | 161 | } |
Inscape_ao | 7:9ab8809f9693 | 162 | |
Inscape_ao | 7:9ab8809f9693 | 163 | bool DeviceRepeater::init(void) |
Inscape_ao | 7:9ab8809f9693 | 164 | { |
Inscape_ao | 7:9ab8809f9693 | 165 | int ret; |
Inscape_ao | 7:9ab8809f9693 | 166 | selected_id = NOT_ID_SELECTED; |
Inscape_ao | 7:9ab8809f9693 | 167 | ret = pDriver->init(); |
Inscape_ao | 7:9ab8809f9693 | 168 | if (ret == 0) { |
Inscape_ao | 7:9ab8809f9693 | 169 | stat = DEV_READY; |
Inscape_ao | 7:9ab8809f9693 | 170 | } else { |
Inscape_ao | 7:9ab8809f9693 | 171 | stat = DEV_FATAL; |
Inscape_ao | 7:9ab8809f9693 | 172 | } |
Inscape_ao | 7:9ab8809f9693 | 173 | return (stat == DEV_READY)? true : false; |
Inscape_ao | 7:9ab8809f9693 | 174 | } |
Inscape_ao | 7:9ab8809f9693 | 175 | |
Inscape_ao | 7:9ab8809f9693 | 176 | bool DeviceRepeater::resetAllStatus(void) |
Inscape_ao | 7:9ab8809f9693 | 177 | { |
Inscape_ao | 7:9ab8809f9693 | 178 | /* TODO */ |
Inscape_ao | 7:9ab8809f9693 | 179 | int ret; |
Inscape_ao | 7:9ab8809f9693 | 180 | ret = pDriver->reset(); |
Inscape_ao | 7:9ab8809f9693 | 181 | if (ret == 0) { |
Inscape_ao | 7:9ab8809f9693 | 182 | /* SUCCESS */ |
Inscape_ao | 7:9ab8809f9693 | 183 | /* TODO */ |
Inscape_ao | 7:9ab8809f9693 | 184 | } else { |
Inscape_ao | 7:9ab8809f9693 | 185 | /* ERROR */ |
Inscape_ao | 7:9ab8809f9693 | 186 | /* TODO */ |
Inscape_ao | 7:9ab8809f9693 | 187 | } |
Inscape_ao | 7:9ab8809f9693 | 188 | return false; |
Inscape_ao | 7:9ab8809f9693 | 189 | } |
Inscape_ao | 7:9ab8809f9693 | 190 | |
Inscape_ao | 7:9ab8809f9693 | 191 | bool DeviceRepeater::setConfigId(int id) |
Inscape_ao | 7:9ab8809f9693 | 192 | { |
Inscape_ao | 7:9ab8809f9693 | 193 | /* never accept in Not Ready To Run */ |
Inscape_ao | 7:9ab8809f9693 | 194 | if (!this->readyToStart()) { |
Inscape_ao | 7:9ab8809f9693 | 195 | return false; |
Inscape_ao | 7:9ab8809f9693 | 196 | } |
Inscape_ao | 7:9ab8809f9693 | 197 | if (selected_id != NOT_ID_SELECTED) { |
Inscape_ao | 7:9ab8809f9693 | 198 | return false; |
Inscape_ao | 7:9ab8809f9693 | 199 | } |
Inscape_ao | 7:9ab8809f9693 | 200 | selected_id = id; |
Inscape_ao | 7:9ab8809f9693 | 201 | return true; |
Inscape_ao | 7:9ab8809f9693 | 202 | } |
Inscape_ao | 7:9ab8809f9693 | 203 | |
Inscape_ao | 7:9ab8809f9693 | 204 | bool DeviceRepeater::setConfigValue(int setValue) |
Inscape_ao | 7:9ab8809f9693 | 205 | { |
Inscape_ao | 7:9ab8809f9693 | 206 | bool ret; |
Inscape_ao | 7:9ab8809f9693 | 207 | /* never accept in Not Ready To Run */ |
Inscape_ao | 7:9ab8809f9693 | 208 | if (!this->readyToStart()) { |
Inscape_ao | 7:9ab8809f9693 | 209 | return false; |
Inscape_ao | 7:9ab8809f9693 | 210 | } |
Inscape_ao | 7:9ab8809f9693 | 211 | /* not set config ID before Set */ |
Inscape_ao | 7:9ab8809f9693 | 212 | if (selected_id == NOT_ID_SELECTED) { |
Inscape_ao | 7:9ab8809f9693 | 213 | return false; |
Inscape_ao | 7:9ab8809f9693 | 214 | } |
Inscape_ao | 7:9ab8809f9693 | 215 | ret = pDriver->set_config(selected_id, setValue); |
Inscape_ao | 7:9ab8809f9693 | 216 | selected_id = NOT_ID_SELECTED; |
Inscape_ao | 7:9ab8809f9693 | 217 | return ret; |
Inscape_ao | 7:9ab8809f9693 | 218 | } |
Inscape_ao | 7:9ab8809f9693 | 219 | |
Inscape_ao | 7:9ab8809f9693 | 220 | bool DeviceRepeater::getConfigValue(int *retValue) |
Inscape_ao | 7:9ab8809f9693 | 221 | { |
Inscape_ao | 7:9ab8809f9693 | 222 | bool ret; |
Inscape_ao | 7:9ab8809f9693 | 223 | /* never accept in Not Ready To Run */ |
Inscape_ao | 7:9ab8809f9693 | 224 | if (!this->readyToStart()) { |
Inscape_ao | 7:9ab8809f9693 | 225 | return false; |
Inscape_ao | 7:9ab8809f9693 | 226 | } |
Inscape_ao | 7:9ab8809f9693 | 227 | /* not set config ID before Get */ |
Inscape_ao | 7:9ab8809f9693 | 228 | if (selected_id == NOT_ID_SELECTED) { |
Inscape_ao | 7:9ab8809f9693 | 229 | return false; |
Inscape_ao | 7:9ab8809f9693 | 230 | } |
Inscape_ao | 7:9ab8809f9693 | 231 | ret = pDriver->get_config(selected_id, retValue); |
Inscape_ao | 7:9ab8809f9693 | 232 | selected_id = NOT_ID_SELECTED; |
Inscape_ao | 7:9ab8809f9693 | 233 | return ret; |
Inscape_ao | 7:9ab8809f9693 | 234 | } |