Code for sensor nodes connected by radio receiver with gateway

Dependencies:   mbed WakeUp coapRadioClient DHT11

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers msg.cpp Source File

msg.cpp

00001 #include "msg.h"
00002 
00003 #define UINT8_MAX 256
00004 
00005 void Msg::construct_data_msg(uint8_t sensor_id, int data, uint8_t *buffer, int buffer_len) {
00006     if (buffer_len < 5)
00007         return;
00008 
00009     buffer[0] = sensor_id;
00010     for (int i = 4; i > 0; i--) {
00011         buffer[i] = data % UINT8_MAX;
00012         data /= UINT8_MAX;
00013     }
00014 }
00015 
00016 void Msg::deconstruct_data_msg(uint8_t *sensor_id, int *data, uint8_t *buffer, int buffer_len) {
00017     if (buffer_len < 5)
00018         return;
00019 
00020     *sensor_id = buffer[0];
00021     *data = 0;
00022     for (int i = 1; i < 5; i++) {
00023         *data = *data * UINT8_MAX + buffer[i];
00024     }
00025 }