refactoring sensor and label handling

This commit is contained in:
2023-01-24 16:09:53 +01:00
parent 8043bb8208
commit 4b119a7666
2 changed files with 101 additions and 46 deletions

View File

@ -10,5 +10,7 @@
#define SEND_PERIOD 15000 // ms #define SEND_PERIOD 15000 // ms
#define LABEL_LENGTH 5 #define LABEL_LENGTH 5
#define NUM_OF_SENSORS 4
#endif /* _DEFINES_H_ */ #endif /* _DEFINES_H_ */

View File

@ -59,58 +59,79 @@ DallasTemperature DS18B20(&oneWire);
bool firstrun = true; 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 */ /* Prepares the payload of the frame */
static void prepareTxFrame( uint8_t port ) static void prepareTxFrame( uint8_t port )
{ {
display.clear(); if ((errorCode & FATAL_MASK) == 0) {
display.clear();
DS18B20.begin(); if (firstrun) {
cnt = DS18B20.getDS18Count(); struct {
Serial.printf("cnt: %d\n\r", cnt); uint16_t status;
const uint8_t SLOT_WIDTH = 1 + 8 + 4; // 1 byte statue, 8 bytes address, 4 bytes value uint64_t addrs[NUM_OF_SENSORS];
// status: 00: regular sending } msg;
// 01: very first sending, please send label mapping msg.status = ERR_START_UP;
appDataSize = 1 + (cnt * SLOT_WIDTH); for (uint8_t i = 0; i < NUM_OF_SENSORS; i++) {
uint8_t *buf = appData; msg.addrs[i] = sensors[i].addr;
char *tmpLabels; }
if (firstrun) { appDataSize = sizeof(msg);
*buf = 0x01; memcpy(&appData, (void*)&msg, appDataSize);
firstrun = false; display.drawString(1, 0, "Start up");
labels = (char*) malloc(cnt * (LABEL_LENGTH + 1)); Serial.println("Start up message send");
memset(labels, 0, cnt * (LABEL_LENGTH + 1)); firstrun = false;
tmpLabels = labels; } else {
for (uint8_t i = 0; i < cnt; i++) { DS18B20.begin();
snprintf(tmpLabels, LABEL_LENGTH + 1, "Sens%d", i); struct {
tmpLabels += (LABEL_LENGTH + 1); 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 { } 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) 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.printf("+REV DATA:%s,RXSIZE %d,PORT %d\r\n",mcpsIndication->RxSlot?"RXWIN2":"RXWIN1",mcpsIndication->BufferSize,mcpsIndication->Port);
Serial.print("+REV DATA:"); 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"); Serial.println("illegal number of octets in downlink message");
} else { } else {
for(uint8_t i=0;i<mcpsIndication->BufferSize;i++) { for(uint8_t i=0;i<mcpsIndication->BufferSize;i++) {
Serial.printf("%02X",mcpsIndication->Buffer[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(); Serial.println();
uint32_t color=mcpsIndication->Buffer[0]<<16|mcpsIndication->Buffer[1]<<8|mcpsIndication->Buffer[2]; uint32_t color=mcpsIndication->Buffer[0]<<16|mcpsIndication->Buffer[1]<<8|mcpsIndication->Buffer[2];
@ -146,6 +183,22 @@ void productionSetup() {
display.display(); display.display();
deviceState = DEVICE_STATE_INIT; 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() void productionLoop()