Programm to control a huge setup of sous vide cookers. See https://stratum0.org/wiki/S0us-vide for more information on this project.

Dependencies:   mbed-rtos mbed

Fork of rtos_basic by mbed official

Committer:
chrissidach
Date:
Mon Jul 13 19:50:02 2015 +0000
Revision:
7:22b5cbcece06
+initial infrastructure; +extensionIF; +serial parser

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chrissidach 7:22b5cbcece06 1 #include "serialParser.h"
chrissidach 7:22b5cbcece06 2
chrissidach 7:22b5cbcece06 3
chrissidach 7:22b5cbcece06 4 SerialParser::SerialParser(PinName tx, PinName rx, EIF *eif) {
chrissidach 7:22b5cbcece06 5 serial = new Serial(tx, rx);
chrissidach 7:22b5cbcece06 6 this->eif = eif;
chrissidach 7:22b5cbcece06 7 recBuffCurrent = recBuff;
chrissidach 7:22b5cbcece06 8
chrissidach 7:22b5cbcece06 9 serial->attach(this, &SerialParser::creceive);
chrissidach 7:22b5cbcece06 10
chrissidach 7:22b5cbcece06 11 parser = new Thread(&SerialParser::parserThreadStarter,this);
chrissidach 7:22b5cbcece06 12 //status = new Thread(&SerialParser::statusThreadStarter,this);
chrissidach 7:22b5cbcece06 13 }
chrissidach 7:22b5cbcece06 14
chrissidach 7:22b5cbcece06 15 void SerialParser::creceive(void) {
chrissidach 7:22b5cbcece06 16 char c = serial->getc();
chrissidach 7:22b5cbcece06 17
chrissidach 7:22b5cbcece06 18 *recBuffCurrent = c;
chrissidach 7:22b5cbcece06 19
chrissidach 7:22b5cbcece06 20 if(c == '\r') {
chrissidach 7:22b5cbcece06 21 *recBuffCurrent = 0;
chrissidach 7:22b5cbcece06 22 strcpy(Line, recBuff);
chrissidach 7:22b5cbcece06 23 recBuffCurrent = recBuff;
chrissidach 7:22b5cbcece06 24 parser->signal_set(SERIAL_SigRX);
chrissidach 7:22b5cbcece06 25 } else {
chrissidach 7:22b5cbcece06 26 recBuffCurrent++;
chrissidach 7:22b5cbcece06 27 if((recBuffCurrent - recBuff) == SERIAL_RXBUFFLEN) {
chrissidach 7:22b5cbcece06 28 recBuffCurrent = recBuff;
chrissidach 7:22b5cbcece06 29 }
chrissidach 7:22b5cbcece06 30 }
chrissidach 7:22b5cbcece06 31 }
chrissidach 7:22b5cbcece06 32
chrissidach 7:22b5cbcece06 33 void SerialParser::parserThreadStarter(void const *p) {
chrissidach 7:22b5cbcece06 34 SerialParser *instance = (SerialParser*)p;
chrissidach 7:22b5cbcece06 35 instance->parserFunc();
chrissidach 7:22b5cbcece06 36 }
chrissidach 7:22b5cbcece06 37
chrissidach 7:22b5cbcece06 38 void SerialParser::parserFunc() {
chrissidach 7:22b5cbcece06 39 while(true) {
chrissidach 7:22b5cbcece06 40 Thread::signal_wait(SERIAL_SigRX);
chrissidach 7:22b5cbcece06 41 //printf("Received >%s< on serial.\r\n",Line);
chrissidach 7:22b5cbcece06 42 if(strlen(Line) <= 8) {
chrissidach 7:22b5cbcece06 43 printf("!Line malformed: to short\r\n");
chrissidach 7:22b5cbcece06 44 continue;
chrissidach 7:22b5cbcece06 45 }
chrissidach 7:22b5cbcece06 46 if(Line[6] != ' ') {
chrissidach 7:22b5cbcece06 47 printf("!Line malformed: no space after signature\r\n");
chrissidach 7:22b5cbcece06 48 continue;
chrissidach 7:22b5cbcece06 49 }
chrissidach 7:22b5cbcece06 50
chrissidach 7:22b5cbcece06 51 char ref[7];
chrissidach 7:22b5cbcece06 52 memcpy(ref, Line, 6);
chrissidach 7:22b5cbcece06 53 ref[6] = 0;
chrissidach 7:22b5cbcece06 54 //printf("-> Extracted Ref: %s\r\n", ref);
chrissidach 7:22b5cbcece06 55
chrissidach 7:22b5cbcece06 56 char cmd[32];
chrissidach 7:22b5cbcece06 57 strcpy(cmd, "");
chrissidach 7:22b5cbcece06 58 uint8_t i=0;
chrissidach 7:22b5cbcece06 59 while(i < 32 && Line[7+i] != 0 && Line[7+i] != ' ') {
chrissidach 7:22b5cbcece06 60 cmd[i] = Line[7+i];
chrissidach 7:22b5cbcece06 61 i++;
chrissidach 7:22b5cbcece06 62 }
chrissidach 7:22b5cbcece06 63 cmd[i] = 0;
chrissidach 7:22b5cbcece06 64 //printf("-> Extracted Command: %s\r\n", cmd);
chrissidach 7:22b5cbcece06 65
chrissidach 7:22b5cbcece06 66 uint8_t ext = 0;
chrissidach 7:22b5cbcece06 67 uint8_t pod = 0;
chrissidach 7:22b5cbcece06 68 bool podinfo = false;
chrissidach 7:22b5cbcece06 69
chrissidach 7:22b5cbcece06 70 if(strlen(Line) > 7 + i + 3) {
chrissidach 7:22b5cbcece06 71 if(Line[7+i+2] != '-') {
chrissidach 7:22b5cbcece06 72 printf("!Line malformed: no hypen in pod address\r\n");
chrissidach 7:22b5cbcece06 73 continue;
chrissidach 7:22b5cbcece06 74 }
chrissidach 7:22b5cbcece06 75 if(!isdigit(Line[7+i+1])) {
chrissidach 7:22b5cbcece06 76 printf("!Line malformed: extension is no digit\r\n");
chrissidach 7:22b5cbcece06 77 continue;
chrissidach 7:22b5cbcece06 78 }
chrissidach 7:22b5cbcece06 79 if(!isdigit(Line[7+i+3])) {
chrissidach 7:22b5cbcece06 80 printf("!Line malformed: pod is no digit\r\n");
chrissidach 7:22b5cbcece06 81 continue;
chrissidach 7:22b5cbcece06 82 }
chrissidach 7:22b5cbcece06 83
chrissidach 7:22b5cbcece06 84 podinfo = true;
chrissidach 7:22b5cbcece06 85 ext = (uint8_t) Line[7+i+1] - '0';
chrissidach 7:22b5cbcece06 86 pod = (uint8_t) Line[7+i+3] - '0';
chrissidach 7:22b5cbcece06 87
chrissidach 7:22b5cbcece06 88 if(ext >= EIF_maxExt) {
chrissidach 7:22b5cbcece06 89 printf("!Line malformed: extension index out of range\r\n");
chrissidach 7:22b5cbcece06 90 continue;
chrissidach 7:22b5cbcece06 91 }
chrissidach 7:22b5cbcece06 92 if(pod >= EIF_maxPods) {
chrissidach 7:22b5cbcece06 93 printf("!Line malformed: pod index out of range\r\n");
chrissidach 7:22b5cbcece06 94 continue;
chrissidach 7:22b5cbcece06 95 }
chrissidach 7:22b5cbcece06 96
chrissidach 7:22b5cbcece06 97 //printf("-> Extracted extension: %i\r\n-> Extracted pod: %i\r\n", ext, pod);
chrissidach 7:22b5cbcece06 98 }
chrissidach 7:22b5cbcece06 99
chrissidach 7:22b5cbcece06 100 char val[32];
chrissidach 7:22b5cbcece06 101 int vali=0;
chrissidach 7:22b5cbcece06 102 strcpy(val, "");
chrissidach 7:22b5cbcece06 103 if(strlen(Line) > 7 + i + 5) {
chrissidach 7:22b5cbcece06 104 //printf("-> Line contains value %s\r\n", (Line + 7+i+5));
chrissidach 7:22b5cbcece06 105 strcpy(val, (Line + 7+i+5));
chrissidach 7:22b5cbcece06 106 //printf("-> Extracted value: %s\r\n", val);
chrissidach 7:22b5cbcece06 107 vali = strtol(val, NULL, 10);
chrissidach 7:22b5cbcece06 108 }
chrissidach 7:22b5cbcece06 109
chrissidach 7:22b5cbcece06 110 // Commands with no parameters
chrissidach 7:22b5cbcece06 111 if(strcmp(cmd, "listextensions") == 0) {
chrissidach 7:22b5cbcece06 112 serial->printf("%s %s ", ref, cmd);
chrissidach 7:22b5cbcece06 113 eif->mutex.lock();
chrissidach 7:22b5cbcece06 114 for(uint8_t i = 0; i < EIF_maxExt; i++) {
chrissidach 7:22b5cbcece06 115 if(eif->extensions[i].present == true) {
chrissidach 7:22b5cbcece06 116 for(uint8_t j = 0; j < EIF_maxPods; j++) {
chrissidach 7:22b5cbcece06 117 if(eif->extensions[i].pods[j].present == true) {
chrissidach 7:22b5cbcece06 118 serial->printf("%1i-%1i,", i, j);
chrissidach 7:22b5cbcece06 119 }
chrissidach 7:22b5cbcece06 120 }
chrissidach 7:22b5cbcece06 121 }
chrissidach 7:22b5cbcece06 122 }
chrissidach 7:22b5cbcece06 123 eif->mutex.unlock();
chrissidach 7:22b5cbcece06 124 printf("\r\n");
chrissidach 7:22b5cbcece06 125 continue;
chrissidach 7:22b5cbcece06 126 }
chrissidach 7:22b5cbcece06 127
chrissidach 7:22b5cbcece06 128 // Commands which address a pod
chrissidach 7:22b5cbcece06 129 if(podinfo) {
chrissidach 7:22b5cbcece06 130 if(strcmp(cmd, "gettargettemp") == 0) {
chrissidach 7:22b5cbcece06 131 eif->mutex.lock();
chrissidach 7:22b5cbcece06 132 serial->printf("%s %s %1i-%1i %i\r\n", ref, cmd, ext, pod, eif->extensions[ext].pods[pod].setpoint);
chrissidach 7:22b5cbcece06 133 eif->mutex.unlock();
chrissidach 7:22b5cbcece06 134 continue;
chrissidach 7:22b5cbcece06 135 }
chrissidach 7:22b5cbcece06 136
chrissidach 7:22b5cbcece06 137 if(strcmp(cmd, "getpower") == 0) {
chrissidach 7:22b5cbcece06 138 eif->mutex.lock();
chrissidach 7:22b5cbcece06 139 serial->printf("%s %s %1i-%1i %i\r\n", ref, cmd, ext, pod, eif->extensions[ext].pods[pod].powered);
chrissidach 7:22b5cbcece06 140 eif->mutex.unlock();
chrissidach 7:22b5cbcece06 141 continue;
chrissidach 7:22b5cbcece06 142 }
chrissidach 7:22b5cbcece06 143 }
chrissidach 7:22b5cbcece06 144
chrissidach 7:22b5cbcece06 145 //Commands which adddres a pod and need a value
chrissidach 7:22b5cbcece06 146 if(podinfo && strlen(val) > 0) {
chrissidach 7:22b5cbcece06 147 if(strcmp(cmd, "settargettemp") == 0) {
chrissidach 7:22b5cbcece06 148 eif->mutex.lock();
chrissidach 7:22b5cbcece06 149 eif->extensions[ext].pods[pod].setpoint = vali;
chrissidach 7:22b5cbcece06 150 serial->printf("%s %s %1i-%1i %i\r\n", ref, cmd, ext, pod, eif->extensions[ext].pods[pod].setpoint);
chrissidach 7:22b5cbcece06 151 eif->mutex.unlock();
chrissidach 7:22b5cbcece06 152 continue;
chrissidach 7:22b5cbcece06 153 }
chrissidach 7:22b5cbcece06 154 if(strcmp(cmd, "setpower") == 0) {
chrissidach 7:22b5cbcece06 155 eif->mutex.lock();
chrissidach 7:22b5cbcece06 156 if(vali) {
chrissidach 7:22b5cbcece06 157 eif->extensions[ext].pods[pod].powered = true;
chrissidach 7:22b5cbcece06 158 } else {
chrissidach 7:22b5cbcece06 159 eif->extensions[ext].pods[pod].powered = false;
chrissidach 7:22b5cbcece06 160 }
chrissidach 7:22b5cbcece06 161 serial->printf("%s %s %1i-%1i %i\r\n", ref, cmd, ext, pod, eif->extensions[ext].pods[pod].powered);
chrissidach 7:22b5cbcece06 162 eif->mutex.unlock();
chrissidach 7:22b5cbcece06 163 continue;
chrissidach 7:22b5cbcece06 164 }
chrissidach 7:22b5cbcece06 165 }
chrissidach 7:22b5cbcece06 166
chrissidach 7:22b5cbcece06 167 printf("!Could not find matching command\r\n");
chrissidach 7:22b5cbcece06 168
chrissidach 7:22b5cbcece06 169 }
chrissidach 7:22b5cbcece06 170 }
chrissidach 7:22b5cbcece06 171
chrissidach 7:22b5cbcece06 172
chrissidach 7:22b5cbcece06 173
chrissidach 7:22b5cbcece06 174 void SerialParser::statusThreadStarter(void const *p) {
chrissidach 7:22b5cbcece06 175 SerialParser *instance = (SerialParser*)p;
chrissidach 7:22b5cbcece06 176 instance->statusFunc();
chrissidach 7:22b5cbcece06 177 }
chrissidach 7:22b5cbcece06 178
chrissidach 7:22b5cbcece06 179 void SerialParser::statusFunc() {
chrissidach 7:22b5cbcece06 180 while(true) {
chrissidach 7:22b5cbcece06 181 Thread::wait(1000);
chrissidach 7:22b5cbcece06 182 eif->mutex.lock();
chrissidach 7:22b5cbcece06 183 for(uint8_t i = 0; i < EIF_maxExt; i++) {
chrissidach 7:22b5cbcece06 184 if(eif->extensions[i].present == true) {
chrissidach 7:22b5cbcece06 185 for(uint8_t j = 0; j < EIF_maxPods; j++) {
chrissidach 7:22b5cbcece06 186 if(eif->extensions[i].pods[j].present == true) {
chrissidach 7:22b5cbcece06 187 if(eif->extensions[i].pods[j].sensorfail == true) {
chrissidach 7:22b5cbcece06 188 serial->printf("###### podstatus %1i-%1i sensorfail\r\n", i, j);
chrissidach 7:22b5cbcece06 189 } else {
chrissidach 7:22b5cbcece06 190 int t = (eif->extensions[i].pods[j].temp1 + eif->extensions[i].pods[j].temp2)/2;
chrissidach 7:22b5cbcece06 191 serial->printf("###### podstatus %1i-%1i %i\r\n", i, j, t);
chrissidach 7:22b5cbcece06 192 }
chrissidach 7:22b5cbcece06 193 }
chrissidach 7:22b5cbcece06 194 }
chrissidach 7:22b5cbcece06 195 }
chrissidach 7:22b5cbcece06 196 }
chrissidach 7:22b5cbcece06 197 eif->mutex.unlock();
chrissidach 7:22b5cbcece06 198 }
chrissidach 7:22b5cbcece06 199 }