UART Command Parser Time Manager Data Store for SD Card for stm32l476 [it's not Licensed as BSD/GPLx]

Dependencies:   mbed SDFileSystem

Committer:
Inscape_ao
Date:
Thu May 30 03:00:15 2019 +0000
Revision:
16:602bc04e3cb5
Parent:
12:a45a9c65dc03
Child:
17:c2709a9c0a68
bugfix fflush->fclose-AND-fopen(a+)

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