diff --git a/libraries/includes/defines.h b/libraries/includes/defines.h index b51601c..961af31 100644 --- a/libraries/includes/defines.h +++ b/libraries/includes/defines.h @@ -10,5 +10,7 @@ #define SEND_PERIOD 15000 // ms #define LABEL_LENGTH 5 +#define NUM_OF_SENSORS 4 + #endif /* _DEFINES_H_ */ \ No newline at end of file diff --git a/sketch/production.cpp b/sketch/production.cpp index 9ccca32..082734b 100644 --- a/sketch/production.cpp +++ b/sketch/production.cpp @@ -59,58 +59,79 @@ DallasTemperature DS18B20(&oneWire); bool firstrun = true; -char *labels; -uint8_t cnt; + +typedef struct { + uint64_t addr; + uint8_t index; + char label[LABEL_LENGTH+1]; +} sensor_t; + +sensor_t sensors[NUM_OF_SENSORS]; + +typedef enum { + ERR_OK = 0x0000, + ERR_SENSOR_LOST = 0x0001, + ERR_ILLEGAL_DOWNLINK_MESSAGE_RECEIVED = 0x0002, + ERR_START_UP = 0x0004, + ERR_INVALID_NUM_OF_SENSORS = 0x8008, +} errorCode_t; +#define FATAL_MASK 0x8000 +uint16_t errorCode = ERR_OK; + /* Prepares the payload of the frame */ static void prepareTxFrame( uint8_t port ) { - display.clear(); + if ((errorCode & FATAL_MASK) == 0) { + display.clear(); - DS18B20.begin(); - cnt = DS18B20.getDS18Count(); - Serial.printf("cnt: %d\n\r", cnt); - const uint8_t SLOT_WIDTH = 1 + 8 + 4; // 1 byte statue, 8 bytes address, 4 bytes value - // status: 00: regular sending - // 01: very first sending, please send label mapping - appDataSize = 1 + (cnt * SLOT_WIDTH); - uint8_t *buf = appData; - char *tmpLabels; - if (firstrun) { - *buf = 0x01; - firstrun = false; - labels = (char*) malloc(cnt * (LABEL_LENGTH + 1)); - memset(labels, 0, cnt * (LABEL_LENGTH + 1)); - tmpLabels = labels; - for (uint8_t i = 0; i < cnt; i++) { - snprintf(tmpLabels, LABEL_LENGTH + 1, "Sens%d", i); - tmpLabels += (LABEL_LENGTH + 1); + if (firstrun) { + struct { + uint16_t status; + uint64_t addrs[NUM_OF_SENSORS]; + } msg; + msg.status = ERR_START_UP; + for (uint8_t i = 0; i < NUM_OF_SENSORS; i++) { + msg.addrs[i] = sensors[i].addr; + } + appDataSize = sizeof(msg); + memcpy(&appData, (void*)&msg, appDataSize); + display.drawString(1, 0, "Start up"); + Serial.println("Start up message send"); + firstrun = false; + } else { + DS18B20.begin(); + struct { + uint16_t status; + struct { + uint64_t addr; + int32_t value; + } sensors[NUM_OF_SENSORS]; + } msg; + msg.status = (DS18B20.getDS18Count() == NUM_OF_SENSORS) ? errorCode : errorCode | ERR_SENSOR_LOST; + DS18B20.requestTemperatures(); + for (uint8_t i = 0; i < NUM_OF_SENSORS; i++) { + msg.sensors[i].addr = sensors[i].addr; + msg.sensors[i].value = DS18B20.getTemp((const uint8_t*)&(sensors[i].addr)); + + float tempC = ((float)msg.sensors[i].value) / 128; + char dispbuf[128]; + sprintf(dispbuf, "%s: %.2f °C", sensors[i].label, tempC); + display.drawString(1, i * 17, dispbuf); + Serial.printf("%d, %08x, %s, %.2f\n\r", sensors[i].index, sensors[i].addr, sensors[i].label, tempC); + } + appDataSize = sizeof(msg); + memcpy(&appData, (void*)&msg, appDataSize); + Serial.println("Send"); } + + display.display(); + + errorCode = ERR_OK; } else { - *buf = 0x00; + appDataSize = 1; + appData[0] = errorCode; } - - buf++; - tmpLabels = labels; - uint8_t yline = 0; - for (uint8_t i = 0; i < cnt; i++) { - uint8_t *addr = (buf + (i * SLOT_WIDTH)); - uint32_t *value = (uint32_t*) (buf + (i * SLOT_WIDTH) + 8); - - DS18B20.getAddress(addr, 0); - Serial.printf("%02x %02x %02x %02x %02x %02x %02x %02x\n\r", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]); - DS18B20.requestTemperatures(); // send the command to get temperatures - *value = DS18B20.getTemp(addr); - Serial.printf("tempC: %08x\n\r", *value); - float tempC = ((float)*value) / 128; - char dispbuf[128]; - sprintf(dispbuf, "%s: %.2f °C", tmpLabels, tempC); - tmpLabels += (LABEL_LENGTH + 1); - display.drawString(1, yline, dispbuf); - yline += 17; - } - - display.display(); } void downLinkDataHandle(McpsIndication_t *mcpsIndication) @@ -118,13 +139,29 @@ void downLinkDataHandle(McpsIndication_t *mcpsIndication) Serial.printf("+REV DATA:%s,RXSIZE %d,PORT %d\r\n",mcpsIndication->RxSlot?"RXWIN2":"RXWIN1",mcpsIndication->BufferSize,mcpsIndication->Port); Serial.print("+REV DATA:"); - if (mcpsIndication->BufferSize != cnt * (LABEL_LENGTH + 1)) { + sensor_t downlinkSensors[NUM_OF_SENSORS]; + if (mcpsIndication->BufferSize != sizeof(downlinkSensors)) { Serial.println("illegal number of octets in downlink message"); } else { for(uint8_t i=0;iBufferSize;i++) { Serial.printf("%02X",mcpsIndication->Buffer[i]); } - memcpy(labels, mcpsIndication->Buffer, mcpsIndication->BufferSize); + memcpy(&downlinkSensors, mcpsIndication->Buffer, sizeof(downlinkSensors)); + for (uint8_t i = 0; i < NUM_OF_SENSORS; i++) { + bool found = false; + for (uint8_t j = 0; j < NUM_OF_SENSORS; j++) { + if (sensors[i].addr == downlinkSensors[i].addr) { + found = true; + sensors[i].index = downlinkSensors[i].index; + memcpy(sensors[i].label, downlinkSensors[i].label, LABEL_LENGTH); + sensors[i].label[LABEL_LENGTH] = 0; + } + } + if (! found) { + Serial.printf("Illegal label received for not existing %08x\n\r", downlinkSensors[i].addr); + errorCode |= ERR_ILLEGAL_DOWNLINK_MESSAGE_RECEIVED; + } + } } Serial.println(); uint32_t color=mcpsIndication->Buffer[0]<<16|mcpsIndication->Buffer[1]<<8|mcpsIndication->Buffer[2]; @@ -146,6 +183,22 @@ void productionSetup() { display.display(); deviceState = DEVICE_STATE_INIT; + + DS18B20.begin(); + uint8_t cnt = DS18B20.getDS18Count(); + if (cnt != 4) { + display.drawString(1, 0, "invalid number of sensors"); + errorCode = ERR_INVALID_NUM_OF_SENSORS; + } else { + for (uint8_t i = 0; i < NUM_OF_SENSORS; i++) { + sprintf(sensors[i].label, "Sens%d", i); + DS18B20.getAddress((uint8_t*)&(sensors[i].addr), i); + char buf[128]; + sprintf(buf, "%s: %08x", sensors[i].label, sensors[i].addr); + display.drawString(1, i*16, buf); + sensors[i].index = 0; + } + } } void productionLoop()