Skip to content

Commit

Permalink
clean up and simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinabrokwa committed Oct 26, 2017
1 parent 088b6d9 commit 758b352
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 63 deletions.
4 changes: 0 additions & 4 deletions src/Comms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
#include "Comms.h"
#include "Packet.h"

uint8_t PACKET_HEADER[3] = { 0x50, 0x50, 0x50 };
uint8_t ACK[3] = { 0xAA, 0x05, 0x00 };
uint8_t NAK[3] = { 0xAA, 0x05, 0xFF };

//int sendPacket(Packet* packet) {}

bool isACK(uint8_t* p) {
Expand Down
4 changes: 0 additions & 4 deletions src/Comms.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@

#define SEND_NUM_RETRIES 5

extern uint8_t PACKET_HEADER[3];
extern uint8_t ACK[3];
extern uint8_t NAK[3];

/**
*
*/
Expand Down
48 changes: 11 additions & 37 deletions src/Packet.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
/**
*
* Packet structure:
* Temperature: 0 - 1
*/
#define DEBUG // TODO: REMOVE ME

#include <Arduino.h>
#include "Packet.h"

/**
* Constructor with no packet
* Constructor
*/
Packet::Packet() {
this->header = this->packet; // header points to the top of the packet
this->data = this->packet + PACKET_HEADER_SIZE; // data is offset PACKET_HEADER_SIZE bytes

// set header 0x50, 0x50 0x50
this->header[0] = this->header[1] = this->header[2] = 0x50;
}

/**
Expand All @@ -38,50 +42,20 @@ void Packet::print() {

Serial.println("]");

// Parse packet data and output individual fields
// temperature data
int temp = 0;
temp |= this->data[1];
temp <<= 8;
temp |= this->data[0];
Serial.print("Temperature: ");
Serial.println(temp);
Serial.println(*(int*)this->data);
}
#endif /* DEBUG */

/**
*
*/
void Packet::setHeader(uint8_t* header) {
for (int i = 0; i < PACKET_HEADER_SIZE; i++)
this->header[i] = header[i];
}

/**
* Write to a specific byte in the packet data
*/
void Packet::setData(uint8_t byt, int index) {
this->data[index] = byt;
}

/**
* Copies the data
*/
void Packet::setData(uint8_t* data) {
for (int i = 0; i < PACKET_DATA_SIZE; i++)
this->data[i] = data[i];
}

/**
* Encode sensor data into the packet
*/
void Packet::setField(PacketField field, void* data) {
int temp;
int *d;
switch (field) {
case Temperature:
temp = *((int*)data);
this->data[0] = temp & 0xff;
this->data[1] = (temp >> 8) & 0xff;
d = (int*)this->data;
*d = *((int*)data);
break;
#ifdef DEBUG
default:
Expand All @@ -91,7 +65,7 @@ void Packet::setField(PacketField field, void* data) {
}

/**
* Returns a reference to the underlying array that is the packet
* Returns a pointer to the underlying array that is the packet
*/
uint8_t* Packet::toArray() {
return this->packet;
Expand Down
3 changes: 0 additions & 3 deletions src/Packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ class Packet {
public:
Packet();
~Packet();
void setHeader(uint8_t* header);
void setData(uint8_t byt, int index);
void setData(uint8_t* data);
void setField(PacketField field, void* data);
uint8_t* toArray();
#ifdef DEBUG
Expand Down
25 changes: 10 additions & 15 deletions src/Payload/Payload.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

SoftwareSerial ModemSerial(10, 11); // RX, TX

uint8_t *ackOrNak = new uint8_t[ACK_SIZE];
uint8_t ackOrNak[ACK_SIZE];
Packet *packet = new Packet();

// For Adafruit TMP102
Expand All @@ -31,17 +31,16 @@ int readTempSensor() {
/**
*
*/
int sendPacket(Packet* packet) {
bool sendPacket(Packet* packet) {
int retryCount = SEND_NUM_RETRIES;

#ifdef DEBUG
Serial.print("sending packet: ");
packet->print();
Serial.print("sending packet: "); packet->print();
#endif /* DEBUG */

ModemSerial.write(packet->toArray(), PACKET_SIZE);

// read ACK or NAK - wait for 3 bytes to be written
// read ACK or NAK - wait for 3 bytes to be available
while (ModemSerial.available() < ACK_SIZE)
delay(100);

Expand All @@ -61,14 +60,14 @@ int sendPacket(Packet* packet) {
retryCount--;
}

if (isACK(ackOrNak)) {
return 1;
} else {
bool success = isACK(ackOrNak);

#ifdef DEBUG
if (!success)
Serial.print("Failed to send packet after "); Serial.print(SEND_NUM_RETRIES); Serial.println(" attemps");
#endif /* DEBUG */
return 0;
}

return success;
}

/**
Expand Down Expand Up @@ -122,9 +121,6 @@ void setup() {
// set the data rate for the SoftwareSerial port
ModemSerial.begin(38400);

// all outgoing packets have the same header - write this once during setup
packet->setHeader(PACKET_HEADER);

// set up temperature sensor
initTempSensor();
}
Expand All @@ -133,10 +129,9 @@ void setup() {
*
*/
void loop() {
// temperature
int temp = readTempSensor();
packet->setField(Temperature, &temp);
int success = sendPacket(packet);
bool success = sendPacket(packet);

#ifdef DEBUG
if (success) {
Expand Down

0 comments on commit 758b352

Please sign in to comment.