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

serialParser.cpp

Committer:
chrissidach
Date:
2015-07-13
Revision:
7:22b5cbcece06

File content as of revision 7:22b5cbcece06:

#include "serialParser.h"


SerialParser::SerialParser(PinName tx, PinName rx, EIF *eif) {
    serial = new Serial(tx, rx);
    this->eif = eif;
    recBuffCurrent = recBuff;
    
    serial->attach(this, &SerialParser::creceive);
    
    parser = new Thread(&SerialParser::parserThreadStarter,this);
    //status = new Thread(&SerialParser::statusThreadStarter,this);
}

void SerialParser::creceive(void) {
    char c = serial->getc();
    
    *recBuffCurrent = c;
    
    if(c == '\r') {
        *recBuffCurrent = 0;
        strcpy(Line, recBuff);
        recBuffCurrent = recBuff;
        parser->signal_set(SERIAL_SigRX);
    } else {
        recBuffCurrent++;
        if((recBuffCurrent - recBuff) == SERIAL_RXBUFFLEN) {
            recBuffCurrent = recBuff;
        }
    }   
}

void SerialParser::parserThreadStarter(void const *p) {
    SerialParser *instance = (SerialParser*)p;
    instance->parserFunc();
}

void SerialParser::parserFunc() {
    while(true) {
        Thread::signal_wait(SERIAL_SigRX);
        //printf("Received >%s< on serial.\r\n",Line);
        if(strlen(Line) <= 8) {
            printf("!Line malformed: to short\r\n");
            continue;
        }
        if(Line[6] != ' ') {
            printf("!Line malformed: no space after signature\r\n");
            continue;
        }
        
        char ref[7];
        memcpy(ref, Line, 6);
        ref[6] = 0;
        //printf("-> Extracted Ref: %s\r\n", ref);
        
        char cmd[32];
        strcpy(cmd, "");
        uint8_t i=0;
        while(i < 32 && Line[7+i] != 0 && Line[7+i] != ' ') {
            cmd[i] = Line[7+i];
            i++;
        }
        cmd[i] = 0;
        //printf("-> Extracted Command: %s\r\n", cmd);
        
        uint8_t ext = 0;
        uint8_t pod = 0;
        bool podinfo = false;
        
        if(strlen(Line) > 7 + i + 3) {
            if(Line[7+i+2] != '-') {
                printf("!Line malformed: no hypen in pod address\r\n");
                continue;
            }
            if(!isdigit(Line[7+i+1])) {
                printf("!Line malformed: extension is no digit\r\n");
                continue;
            }
            if(!isdigit(Line[7+i+3])) {
                printf("!Line malformed: pod is no digit\r\n");
                continue;
            }
            
            podinfo = true;
            ext = (uint8_t) Line[7+i+1] - '0';
            pod = (uint8_t) Line[7+i+3] - '0';
            
            if(ext >= EIF_maxExt) {
                printf("!Line malformed: extension index out of range\r\n");
                continue;
            }
            if(pod >= EIF_maxPods) {
                printf("!Line malformed: pod index out of range\r\n");
                continue;
            }
            
            //printf("-> Extracted extension: %i\r\n-> Extracted pod: %i\r\n", ext, pod);
        }
        
        char val[32];
        int vali=0;
        strcpy(val, "");
        if(strlen(Line) > 7 + i + 5) {
            //printf("-> Line contains value %s\r\n", (Line + 7+i+5));
            strcpy(val, (Line + 7+i+5));
            //printf("-> Extracted value: %s\r\n", val);
            vali = strtol(val, NULL, 10);
        } 
        
        // Commands with no parameters
        if(strcmp(cmd, "listextensions") == 0) {
            serial->printf("%s %s ", ref, cmd);
            eif->mutex.lock();
            for(uint8_t i = 0; i < EIF_maxExt; i++) {
                if(eif->extensions[i].present == true) {
                    for(uint8_t j = 0; j < EIF_maxPods; j++) {
                        if(eif->extensions[i].pods[j].present == true) {
                            serial->printf("%1i-%1i,", i, j);
                        }
                    }
                }
            }
            eif->mutex.unlock();
            printf("\r\n");
            continue;
        }
        
        // Commands which address a pod
        if(podinfo) {
            if(strcmp(cmd, "gettargettemp") == 0) {
                eif->mutex.lock();
                serial->printf("%s %s %1i-%1i %i\r\n", ref, cmd, ext, pod, eif->extensions[ext].pods[pod].setpoint);
                eif->mutex.unlock();
                continue;
            }
            
            if(strcmp(cmd, "getpower") == 0) {
                eif->mutex.lock();
                serial->printf("%s %s %1i-%1i %i\r\n", ref, cmd, ext, pod, eif->extensions[ext].pods[pod].powered);
                eif->mutex.unlock();
                continue;
            }
        }
        
        //Commands which adddres a pod and need a value
        if(podinfo && strlen(val) > 0) {
            if(strcmp(cmd, "settargettemp") == 0) {
                eif->mutex.lock();
                eif->extensions[ext].pods[pod].setpoint = vali;
                serial->printf("%s %s %1i-%1i %i\r\n", ref, cmd, ext, pod, eif->extensions[ext].pods[pod].setpoint);
                eif->mutex.unlock();
                continue;
            }
            if(strcmp(cmd, "setpower") == 0) {
                eif->mutex.lock();
                if(vali) {
                    eif->extensions[ext].pods[pod].powered = true;
                } else {
                    eif->extensions[ext].pods[pod].powered = false;
                }
                serial->printf("%s %s %1i-%1i %i\r\n", ref, cmd, ext, pod, eif->extensions[ext].pods[pod].powered);
                eif->mutex.unlock();
                continue;
            }
        }
        
        printf("!Could not find matching command\r\n");
        
    }
}    



void SerialParser::statusThreadStarter(void const *p) {
    SerialParser *instance = (SerialParser*)p;
    instance->statusFunc();
}

void SerialParser::statusFunc() {
    while(true) {
        Thread::wait(1000);
        eif->mutex.lock();
        for(uint8_t i = 0; i < EIF_maxExt; i++) {
            if(eif->extensions[i].present == true) {
                for(uint8_t j = 0; j < EIF_maxPods; j++) {
                    if(eif->extensions[i].pods[j].present == true) {
                        if(eif->extensions[i].pods[j].sensorfail == true) {
                            serial->printf("###### podstatus %1i-%1i sensorfail\r\n", i, j);
                        } else {
                            int t = (eif->extensions[i].pods[j].temp1 + eif->extensions[i].pods[j].temp2)/2;
                            serial->printf("###### podstatus %1i-%1i %i\r\n", i, j, t);
                        }
                    }
                }
            }
        }
        eif->mutex.unlock();
    }    
}