xrocusOS_ADXL355 version

Dependencies:   mbed SDFileSystem

Committer:
APS_Lab
Date:
Fri Jul 05 02:09:06 2019 +0000
Revision:
20:2f2687580ecb
Parent:
17:c2709a9c0a68
Ver0.0

Who changed what in which revision?

UserRevisionLine numberNew 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 10:db2be22bc2f9 12 DeviceRepeater::DeviceRepeater(int setDeviceID, DeviceDriver *setDriver)
Inscape_ao 7:9ab8809f9693 13 {
Inscape_ao 10:db2be22bc2f9 14 deviceID = setDeviceID;
Inscape_ao 7:9ab8809f9693 15 pClock = new Ticker();
Inscape_ao 7:9ab8809f9693 16 selected_id = NOT_ID_SELECTED;
Inscape_ao 7:9ab8809f9693 17 pDriver = setDriver;
Inscape_ao 7:9ab8809f9693 18 stat = DEV_NOT_INIT;
Inscape_ao 7:9ab8809f9693 19 repeat_max_count = DEFAULT_REPEAT_MAX_COUNT;
Inscape_ao 7:9ab8809f9693 20 repeat_stride_sec = DEFAULT_REPEAT_STRIDE;
Inscape_ao 17:c2709a9c0a68 21 repeat_singleshot = DEFAULT_REPEAT_SINGLESHOT;
Inscape_ao 7:9ab8809f9693 22 }
Inscape_ao 7:9ab8809f9693 23
Inscape_ao 7:9ab8809f9693 24 bool DeviceRepeater::readyToStart(void)
Inscape_ao 7:9ab8809f9693 25 {
Inscape_ao 7:9ab8809f9693 26 bool ret;
Inscape_ao 7:9ab8809f9693 27 switch(stat) {
Inscape_ao 7:9ab8809f9693 28 case DEV_READY:
Inscape_ao 7:9ab8809f9693 29 ret = true;
Inscape_ao 7:9ab8809f9693 30 break;
Inscape_ao 7:9ab8809f9693 31 case DEV_NOT_INIT:
Inscape_ao 7:9ab8809f9693 32 ret = true;
Inscape_ao 7:9ab8809f9693 33 break;
Inscape_ao 7:9ab8809f9693 34 case DEV_RUNNING:
Inscape_ao 7:9ab8809f9693 35 ret = false;
Inscape_ao 7:9ab8809f9693 36 break;
Inscape_ao 7:9ab8809f9693 37 default:
Inscape_ao 7:9ab8809f9693 38 ret = false;
Inscape_ao 7:9ab8809f9693 39 break;
Inscape_ao 7:9ab8809f9693 40 }
Inscape_ao 7:9ab8809f9693 41 return ret;
Inscape_ao 7:9ab8809f9693 42 }
Inscape_ao 7:9ab8809f9693 43
Inscape_ao 7:9ab8809f9693 44 bool DeviceRepeater::setRepeatCount(int maxcount)
Inscape_ao 7:9ab8809f9693 45 {
Inscape_ao 7:9ab8809f9693 46 /* never accept in Not Ready To Run */
Inscape_ao 7:9ab8809f9693 47 if (!this->readyToStart()) {
Inscape_ao 7:9ab8809f9693 48 return false;
Inscape_ao 7:9ab8809f9693 49 }
Inscape_ao 7:9ab8809f9693 50 /* invalid arguments */
Inscape_ao 7:9ab8809f9693 51 if (maxcount < 0) {
Inscape_ao 7:9ab8809f9693 52 return false;
Inscape_ao 7:9ab8809f9693 53 }
Inscape_ao 7:9ab8809f9693 54 repeat_max_count = maxcount;
Inscape_ao 7:9ab8809f9693 55 return true;
Inscape_ao 7:9ab8809f9693 56 }
Inscape_ao 7:9ab8809f9693 57
Inscape_ao 7:9ab8809f9693 58 bool DeviceRepeater::setRepeatStride(int sec)
Inscape_ao 7:9ab8809f9693 59 {
Inscape_ao 7:9ab8809f9693 60 /* never accept in Not Ready To Run */
Inscape_ao 7:9ab8809f9693 61 if (!this->readyToStart()) {
Inscape_ao 7:9ab8809f9693 62 return false;
Inscape_ao 7:9ab8809f9693 63 }
Inscape_ao 7:9ab8809f9693 64 /* invalid arguments */
Inscape_ao 7:9ab8809f9693 65 if (sec < 0) {
Inscape_ao 7:9ab8809f9693 66 return false;
Inscape_ao 7:9ab8809f9693 67 } else if (sec == 0) {
Inscape_ao 7:9ab8809f9693 68 return false;
Inscape_ao 7:9ab8809f9693 69 }
Inscape_ao 7:9ab8809f9693 70 repeat_stride_sec = sec;
Inscape_ao 7:9ab8809f9693 71 return true;
Inscape_ao 7:9ab8809f9693 72 }
Inscape_ao 7:9ab8809f9693 73
Inscape_ao 17:c2709a9c0a68 74 bool DeviceRepeater::setRepeatSingleShot(bool setMode)
Inscape_ao 17:c2709a9c0a68 75 {
Inscape_ao 17:c2709a9c0a68 76 /* never accept in Not Ready To Run */
Inscape_ao 17:c2709a9c0a68 77 if (!this->readyToStart()) {
Inscape_ao 17:c2709a9c0a68 78 return false;
Inscape_ao 17:c2709a9c0a68 79 }
Inscape_ao 17:c2709a9c0a68 80 /* TODO */
Inscape_ao 17:c2709a9c0a68 81 repeat_singleshot = setMode;
Inscape_ao 17:c2709a9c0a68 82 return true;
Inscape_ao 17:c2709a9c0a68 83 }
Inscape_ao 17:c2709a9c0a68 84
Inscape_ao 7:9ab8809f9693 85 bool DeviceRepeater::start(void)
Inscape_ao 7:9ab8809f9693 86 {
Inscape_ao 7:9ab8809f9693 87 bool ret;
Inscape_ao 7:9ab8809f9693 88 switch(stat) {
Inscape_ao 7:9ab8809f9693 89 case DEV_READY:
Inscape_ao 7:9ab8809f9693 90 break;
Inscape_ao 7:9ab8809f9693 91 case DEV_NOT_INIT:
Inscape_ao 7:9ab8809f9693 92 ret = this->init();
Inscape_ao 7:9ab8809f9693 93 if (ret != true) {
Inscape_ao 7:9ab8809f9693 94 return false;
Inscape_ao 7:9ab8809f9693 95 }
Inscape_ao 7:9ab8809f9693 96 break;
Inscape_ao 7:9ab8809f9693 97 case DEV_RUNNING:
Inscape_ao 7:9ab8809f9693 98 return false;
Inscape_ao 7:9ab8809f9693 99 case DEV_FATAL:
Inscape_ao 7:9ab8809f9693 100 default:
Inscape_ao 7:9ab8809f9693 101 return false;
Inscape_ao 7:9ab8809f9693 102 }
Inscape_ao 7:9ab8809f9693 103 // Device is in DEV_READY
Inscape_ao 7:9ab8809f9693 104
Inscape_ao 8:b18a8764ecae 105 /* TODO - add callback for changing READY to RUNNING */
Inscape_ao 9:c81d0df866f5 106 pDriver->ready2run();
Inscape_ao 16:602bc04e3cb5 107 pSds->syncFile();
Inscape_ao 8:b18a8764ecae 108
Inscape_ao 7:9ab8809f9693 109 // start Runnig
Inscape_ao 7:9ab8809f9693 110 // Device is in DEV_RUNNING
Inscape_ao 7:9ab8809f9693 111 stat = DEV_RUNNING;
Inscape_ao 7:9ab8809f9693 112 repeat_remain_cnt = repeat_max_count;
Inscape_ao 7:9ab8809f9693 113 repeat_remain_sec = repeat_stride_sec;
Inscape_ao 7:9ab8809f9693 114 pClock->attach(repeater_basetick, 1.0);
Inscape_ao 7:9ab8809f9693 115 return true;
Inscape_ao 7:9ab8809f9693 116 }
Inscape_ao 7:9ab8809f9693 117
Inscape_ao 7:9ab8809f9693 118 bool DeviceRepeater::stop(void)
Inscape_ao 7:9ab8809f9693 119 {
Inscape_ao 7:9ab8809f9693 120 if (stat != DEV_RUNNING) {
Inscape_ao 7:9ab8809f9693 121 return false;
Inscape_ao 7:9ab8809f9693 122 }
Inscape_ao 7:9ab8809f9693 123
Inscape_ao 7:9ab8809f9693 124 /* Delaied Stop */
Inscape_ao 7:9ab8809f9693 125 stat = DEV_REQ_STOP;
Inscape_ao 7:9ab8809f9693 126 repeat_remain_sec = 0;
Inscape_ao 7:9ab8809f9693 127 stat = DEV_READY;
Inscape_ao 7:9ab8809f9693 128 return true;
Inscape_ao 7:9ab8809f9693 129 }
Inscape_ao 7:9ab8809f9693 130
Inscape_ao 7:9ab8809f9693 131 void DeviceRepeater::tick(void)
Inscape_ao 7:9ab8809f9693 132 {
Inscape_ao 7:9ab8809f9693 133 if (stat != DEV_RUNNING) {
Inscape_ao 7:9ab8809f9693 134 /* Delaied Stop */
Inscape_ao 9:c81d0df866f5 135 if (stat == DEV_REQ_STOP) {
Inscape_ao 8:b18a8764ecae 136 /*****************************************************/
Inscape_ao 8:b18a8764ecae 137 /* TODO - add callback for changing RUNNING to READY */
Inscape_ao 8:b18a8764ecae 138 /*****************************************************/
Inscape_ao 9:c81d0df866f5 139 pDriver->run2ready();
Inscape_ao 16:602bc04e3cb5 140 pSds->syncFile();
Inscape_ao 7:9ab8809f9693 141 repeat_remain_sec = 0;
Inscape_ao 7:9ab8809f9693 142 stat = DEV_READY;
Inscape_ao 7:9ab8809f9693 143 return;
Inscape_ao 7:9ab8809f9693 144 }
Inscape_ao 7:9ab8809f9693 145 return;
Inscape_ao 7:9ab8809f9693 146 }
Inscape_ao 7:9ab8809f9693 147 if (repeat_remain_sec > 0) {
Inscape_ao 9:c81d0df866f5 148 repeat_remain_sec--;
Inscape_ao 9:c81d0df866f5 149 }
Inscape_ao 9:c81d0df866f5 150 if (repeat_remain_sec == 0) {
Inscape_ao 17:c2709a9c0a68 151 bool procRun;
Inscape_ao 12:a45a9c65dc03 152 FILE *pSDFile = pSds->getFilePointer();
Inscape_ao 8:b18a8764ecae 153 /*****************************************************/
Inscape_ao 8:b18a8764ecae 154 /* TODO - kick Senser and send XFD/XDS Data */
Inscape_ao 8:b18a8764ecae 155 /*****************************************************/
Inscape_ao 7:9ab8809f9693 156 /** Sensing and Sending Data **/
Inscape_ao 17:c2709a9c0a68 157 procRun = pDriver->exec(deviceID, pUR->getCurrentUart(), pSDFile);
Inscape_ao 16:602bc04e3cb5 158 pSds->syncFile();
Inscape_ao 17:c2709a9c0a68 159 if (repeat_singleshot) {
Inscape_ao 17:c2709a9c0a68 160 /* Check Exec Sigle On */
Inscape_ao 17:c2709a9c0a68 161 if (procRun == true) {
Inscape_ao 17:c2709a9c0a68 162 /* Already - Executed Mesurement in exec */
Inscape_ao 17:c2709a9c0a68 163 /** GOTO Shutdown */
Inscape_ao 17:c2709a9c0a68 164 /* STOP ticking (0->1) */
Inscape_ao 17:c2709a9c0a68 165 /*****************************************************/
Inscape_ao 17:c2709a9c0a68 166 /* TODO - add callback for changing RUNNING to READY */
Inscape_ao 17:c2709a9c0a68 167 /*****************************************************/
Inscape_ao 17:c2709a9c0a68 168 repeat_remain_cnt = 0;
Inscape_ao 17:c2709a9c0a68 169 pDriver->run2ready();
Inscape_ao 17:c2709a9c0a68 170 pSds->syncFile();
Inscape_ao 17:c2709a9c0a68 171 repeat_remain_sec = 0;
Inscape_ao 17:c2709a9c0a68 172 stat = DEV_READY;
Inscape_ao 17:c2709a9c0a68 173 pClock->detach();
Inscape_ao 17:c2709a9c0a68 174 return;
Inscape_ao 17:c2709a9c0a68 175 } else {
Inscape_ao 17:c2709a9c0a68 176 /* Not Yet - NO Execution in exec */
Inscape_ao 17:c2709a9c0a68 177 }
Inscape_ao 17:c2709a9c0a68 178 }
Inscape_ao 7:9ab8809f9693 179 }
Inscape_ao 7:9ab8809f9693 180 if (repeat_remain_sec <= 0) {
Inscape_ao 7:9ab8809f9693 181 if (repeat_remain_cnt > 0) {
Inscape_ao 7:9ab8809f9693 182 repeat_remain_cnt--;
Inscape_ao 7:9ab8809f9693 183 if (repeat_remain_cnt == 0) {
Inscape_ao 7:9ab8809f9693 184 /* STOP ticking (0->1) */
Inscape_ao 8:b18a8764ecae 185 /*****************************************************/
Inscape_ao 8:b18a8764ecae 186 /* TODO - add callback for changing RUNNING to READY */
Inscape_ao 8:b18a8764ecae 187 /*****************************************************/
Inscape_ao 9:c81d0df866f5 188 pDriver->run2ready();
Inscape_ao 16:602bc04e3cb5 189 pSds->syncFile();
Inscape_ao 7:9ab8809f9693 190 repeat_remain_sec = 0;
Inscape_ao 7:9ab8809f9693 191 stat = DEV_READY;
Inscape_ao 7:9ab8809f9693 192 pClock->detach();
Inscape_ao 7:9ab8809f9693 193 return;
Inscape_ao 7:9ab8809f9693 194 }
Inscape_ao 7:9ab8809f9693 195 /* REFILL */
Inscape_ao 7:9ab8809f9693 196 } else if (repeat_remain_cnt == 0) {
Inscape_ao 7:9ab8809f9693 197 /* REFILL */
Inscape_ao 7:9ab8809f9693 198 } else {
Inscape_ao 8:b18a8764ecae 199 /*****************************************************/
Inscape_ao 7:9ab8809f9693 200 /* TODO : FATAL */
Inscape_ao 8:b18a8764ecae 201 /*****************************************************/
Inscape_ao 7:9ab8809f9693 202 while(1);
Inscape_ao 7:9ab8809f9693 203 }
Inscape_ao 7:9ab8809f9693 204 repeat_remain_sec = repeat_stride_sec;
Inscape_ao 7:9ab8809f9693 205 }
Inscape_ao 7:9ab8809f9693 206 }
Inscape_ao 7:9ab8809f9693 207
Inscape_ao 7:9ab8809f9693 208 bool DeviceRepeater::init(void)
Inscape_ao 7:9ab8809f9693 209 {
Inscape_ao 7:9ab8809f9693 210 int ret;
Inscape_ao 7:9ab8809f9693 211 selected_id = NOT_ID_SELECTED;
Inscape_ao 7:9ab8809f9693 212 ret = pDriver->init();
Inscape_ao 7:9ab8809f9693 213 if (ret == 0) {
Inscape_ao 7:9ab8809f9693 214 stat = DEV_READY;
Inscape_ao 7:9ab8809f9693 215 } else {
Inscape_ao 7:9ab8809f9693 216 stat = DEV_FATAL;
Inscape_ao 7:9ab8809f9693 217 }
Inscape_ao 7:9ab8809f9693 218 return (stat == DEV_READY)? true : false;
Inscape_ao 7:9ab8809f9693 219 }
Inscape_ao 7:9ab8809f9693 220
Inscape_ao 7:9ab8809f9693 221 bool DeviceRepeater::resetAllStatus(void)
Inscape_ao 7:9ab8809f9693 222 {
Inscape_ao 7:9ab8809f9693 223 /* TODO */
Inscape_ao 7:9ab8809f9693 224 int ret;
Inscape_ao 7:9ab8809f9693 225 ret = pDriver->reset();
Inscape_ao 7:9ab8809f9693 226 if (ret == 0) {
Inscape_ao 7:9ab8809f9693 227 /* SUCCESS */
Inscape_ao 7:9ab8809f9693 228 /* TODO */
Inscape_ao 7:9ab8809f9693 229 } else {
Inscape_ao 7:9ab8809f9693 230 /* ERROR */
Inscape_ao 7:9ab8809f9693 231 /* TODO */
Inscape_ao 7:9ab8809f9693 232 }
Inscape_ao 7:9ab8809f9693 233 return false;
Inscape_ao 7:9ab8809f9693 234 }
Inscape_ao 7:9ab8809f9693 235
Inscape_ao 7:9ab8809f9693 236 bool DeviceRepeater::setConfigId(int id)
Inscape_ao 7:9ab8809f9693 237 {
Inscape_ao 7:9ab8809f9693 238 /* never accept in Not Ready To Run */
Inscape_ao 7:9ab8809f9693 239 if (!this->readyToStart()) {
Inscape_ao 7:9ab8809f9693 240 return false;
Inscape_ao 7:9ab8809f9693 241 }
Inscape_ao 7:9ab8809f9693 242 if (selected_id != NOT_ID_SELECTED) {
Inscape_ao 7:9ab8809f9693 243 return false;
Inscape_ao 7:9ab8809f9693 244 }
Inscape_ao 7:9ab8809f9693 245 selected_id = id;
Inscape_ao 7:9ab8809f9693 246 return true;
Inscape_ao 7:9ab8809f9693 247 }
Inscape_ao 7:9ab8809f9693 248
Inscape_ao 7:9ab8809f9693 249 bool DeviceRepeater::setConfigValue(int setValue)
Inscape_ao 7:9ab8809f9693 250 {
Inscape_ao 7:9ab8809f9693 251 bool ret;
Inscape_ao 7:9ab8809f9693 252 /* never accept in Not Ready To Run */
Inscape_ao 7:9ab8809f9693 253 if (!this->readyToStart()) {
Inscape_ao 7:9ab8809f9693 254 return false;
Inscape_ao 7:9ab8809f9693 255 }
Inscape_ao 7:9ab8809f9693 256 /* not set config ID before Set */
Inscape_ao 7:9ab8809f9693 257 if (selected_id == NOT_ID_SELECTED) {
Inscape_ao 7:9ab8809f9693 258 return false;
Inscape_ao 7:9ab8809f9693 259 }
Inscape_ao 7:9ab8809f9693 260 ret = pDriver->set_config(selected_id, setValue);
Inscape_ao 7:9ab8809f9693 261 selected_id = NOT_ID_SELECTED;
Inscape_ao 7:9ab8809f9693 262 return ret;
Inscape_ao 7:9ab8809f9693 263 }
Inscape_ao 7:9ab8809f9693 264
Inscape_ao 7:9ab8809f9693 265 bool DeviceRepeater::getConfigValue(int *retValue)
Inscape_ao 7:9ab8809f9693 266 {
Inscape_ao 7:9ab8809f9693 267 bool ret;
Inscape_ao 7:9ab8809f9693 268 /* never accept in Not Ready To Run */
Inscape_ao 7:9ab8809f9693 269 if (!this->readyToStart()) {
Inscape_ao 7:9ab8809f9693 270 return false;
Inscape_ao 7:9ab8809f9693 271 }
Inscape_ao 7:9ab8809f9693 272 /* not set config ID before Get */
Inscape_ao 7:9ab8809f9693 273 if (selected_id == NOT_ID_SELECTED) {
Inscape_ao 7:9ab8809f9693 274 return false;
Inscape_ao 7:9ab8809f9693 275 }
Inscape_ao 7:9ab8809f9693 276 ret = pDriver->get_config(selected_id, retValue);
Inscape_ao 7:9ab8809f9693 277 selected_id = NOT_ID_SELECTED;
Inscape_ao 7:9ab8809f9693 278 return ret;
Inscape_ao 7:9ab8809f9693 279 }