Programm to control a huge setup of sous vide cookers. See https://stratum0.org/wiki/S0us-vide for more information on this project.
Fork of rtos_basic by
Revision 7:22b5cbcece06, committed 2015-07-13
- Comitter:
- chrissidach
- Date:
- Mon Jul 13 19:50:02 2015 +0000
- Parent:
- 6:209f4db62daf
- Commit message:
- +initial infrastructure; +extensionIF; +serial parser
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ds2482.h Mon Jul 13 19:50:02 2015 +0000 @@ -0,0 +1,32 @@ +#define DS2482_cmd_read 0xE1 +#define DS2482_register_status 0xF0 +#define DS2482_register_readData 0xE1 +#define DS2482_register_channelSel 0xD2 +#define DS2482_register_config 0xC3 + +#define DS2482_cmd_reset 0xF0 + +#define DS2482_cmd_write 0xD2 +#define DS2482_cmd_channelSel 0xC3 +#define DS2482_channel_write_0 0xF0 +#define DS2482_channel_write_1 0xE1 +#define DS2482_channel_write_2 0xD2 +#define DS2482_channel_write_3 0xC3 +#define DS2482_channel_write_4 0xB4 +#define DS2482_channel_write_5 0xA5 +#define DS2482_channel_write_6 0x96 +#define DS2482_channel_write_7 0x87 +#define DS2482_channel_read_0 0xB8 +#define DS2482_channel_read_1 0xB1 +#define DS2482_channel_read_2 0xAA +#define DS2482_channel_read_3 0xA3 +#define DS2482_channel_read_4 0x9C +#define DS2482_channel_read_5 0x95 +#define DS2482_channel_read_6 0x8E +#define DS2482_channel_read_7 0x87 + +#define DS2482_cmd_1w_reset 0xB4 +#define DS2482_cmd_singleBit 0x87 +#define DS2482_cmd_writeByte 0xA5 +#define DS2482_cmd_readByte 0x96 +#define DS2482_cmd_triplet 0x78
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extensionIF.cpp Mon Jul 13 19:50:02 2015 +0000 @@ -0,0 +1,22 @@ +#include "extensionIF.h" + + + + +EIF::EIF() { + this->mutex.lock(); + for(uint8_t i = 0; i < EIF_maxExt; i++) { + this->extensions[i].present = false; + for(uint8_t j = 0; j < EIF_maxPods; j++) { + this->extensions[i].pods[j].present = false; + this->extensions[i].pods[j].powered = false; + this->extensions[i].pods[j].sensorfail = false; + this->extensions[i].pods[j].temp1 = 0; + this->extensions[i].pods[j].temp2 = 0; + this->extensions[i].pods[j].setpoint = 0; + this->extensions[i].pods[j].heaterDC = 0; + } + } + + this->mutex.unlock(); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extensionIF.h Mon Jul 13 19:50:02 2015 +0000 @@ -0,0 +1,35 @@ +#include "mbed.h" +#include "rtos.h" + +#ifndef __extensionIF__ +#define __extensionIF__ + +#define EIF_maxPods 4 +#define EIF_maxExt 8 + +class EIF_pod { + public: + int16_t temp1; + int16_t temp2; + int16_t setpoint; + int8_t heaterDC; + bool present; + bool powered; + bool sensorfail; +}; + +class EIF_extension { + public: + bool present; + EIF_pod pods[EIF_maxPods]; +}; + +class EIF { + public: + EIF(); + EIF_extension extensions[EIF_maxExt]; + Mutex mutex; +}; + + +#endif //__extensionIF__ \ No newline at end of file
--- a/main.cpp Tue Jun 04 16:01:32 2013 +0100 +++ b/main.cpp Mon Jul 13 19:50:02 2015 +0000 @@ -1,21 +1,69 @@ #include "mbed.h" #include "rtos.h" + +#include "extensionIF.h" +#include "serialParser.h" DigitalOut led1(LED1); -DigitalOut led2(LED2); - -void led2_thread(void const *args) { - while (true) { - led2 = !led2; + +SerialParser *pc; +EIF *eif; + +I2C i2c(PB_9, PB_8); + +#define I2CslaveAddr 0x40 + + +void i2c_1w(void const *args) { + char data[10]; + char data2[10]; + int rc; + + data[0] = 0; + + i2c.frequency(100000); + + while(true) + { + printf("Write... "); + rc = i2c.write(0x40, data, 1); + printf("%i\r\n", rc); + data[0] = ~data[0]; + + printf("Read... "); + rc = i2c.read(0x40, data2, 1); + printf("%i, %x\r\n", rc, data2[0]); Thread::wait(1000); } } -int main() { - Thread thread(led2_thread); +int main() { + printf("\r\n!****\r\n!System Init...\r\n"); + + eif = new EIF(); + printf("!* EIF init complete\r\n"); + //TODO: Remove + //Lets pretend this extension is present: + eif->mutex.lock(); + eif->extensions[0].present = true; + eif->extensions[0].pods[0].present = true; + eif->extensions[0].pods[0].temp1 = 280; + eif->extensions[0].pods[0].temp2 = 270; + eif->mutex.unlock(); + + + pc = new SerialParser(USBTX, USBRX, eif); + printf("!* SerialParser init complete\r\n"); + + //Thread thread(i2c_1w); + + + printf("!Init complete\r\n"); + + //Pretend we are still there... while (true) { led1 = !led1; - Thread::wait(500); + Thread::wait(200); } }
--- a/mbed-rtos.lib Tue Jun 04 16:01:32 2013 +0100 +++ b/mbed-rtos.lib Mon Jul 13 19:50:02 2015 +0000 @@ -1,1 +1,1 @@ -https://mbed.org/users/mbed_official/code/mbed-rtos/ \ No newline at end of file +https://mbed.org/users/mbed_official/code/mbed-rtos/#3516160e016b
--- a/mbed.bld Tue Jun 04 16:01:32 2013 +0100 +++ b/mbed.bld Mon Jul 13 19:50:02 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/ \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/dbbf35b96557 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/serialParser.cpp Mon Jul 13 19:50:02 2015 +0000 @@ -0,0 +1,199 @@ +#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(); + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/serialParser.h Mon Jul 13 19:50:02 2015 +0000 @@ -0,0 +1,37 @@ +#include "mbed.h" +#include "rtos.h" +#include "extensionIF.h" +#include <ctype.h> + +#ifndef __serialParser__ +#define __serialParser__ + +#define SERIAL_RXBUFFLEN 255 +#define SERIAL_SigRX 0x01 + +class SerialParser { + private: + void creceive(void); + //void parserFunc(void const* args); + + char recBuff[SERIAL_RXBUFFLEN]; + char* recBuffCurrent; + char Line[SERIAL_RXBUFFLEN]; + + Thread* parser; + static void parserThreadStarter(void const *p); + void parserFunc(); + + Thread* status; + static void statusThreadStarter(void const *p); + void statusFunc(); + + EIF *eif; + + public: + SerialParser(PinName tx, PinName rx, EIF *eif); + + Serial* serial; +}; + +#endif //__serialParser__ \ No newline at end of file