ground-dashboard/receiver/receiver.ino

158 lines
3.6 KiB
Arduino
Raw Normal View History

#include <SPI.h>
#include <stdbool.h>
#include <string.h>
#define TRIGGER_FLIGHT_TIME '#'
#define TRIGGER_ALTITUDE_100 '{'
#define TRIGGER_ALTITUDE '<'
#define TRIGGER_VELOCITY '('
#define TRIGGER_ACCELERATION_10 '\\'
#define TRIGGER_FLIGHT_PHASE '@'
#define TRIGGER_CHANNEL '~'
#define TRIGGER_TEMPERATURE_10 '!'
#define TRIGGER_NAME '='
#define TRIGGER_BATERY_10 '?'
#define TRIGGER_APOGEE '%'
#define TRIGGER_MAX_VELOCITY '^'
#define TRIGGER_MAX_ACCELERATION '['
#define TRIGGER_END_PACKET '>'
typedef union EggtimerData
{
int fight_time;
int altitude_100;
int altitude;
int velocity;
int acceleration_10;
int fight_phase;
char channel[6];
int temperature_10;
char name[9];
int battery_voltage_10;
int apogee;
int max_velocity;
int max_acceleration;
}EggtimerData;
typedef struct EggtimerElementPacket{
char type;
EggtimerData data;
} EggtimerElementPacket;
static char _last_state = 0;
static size_t counter = 0;
/**
* @brief this function stores the data in the element packet
*/
void _save_data(char byte_received, EggtimerElementPacket *element){
switch (_last_state)
{
case TRIGGER_FLIGHT_TIME:
case TRIGGER_ALTITUDE_100:
case TRIGGER_ALTITUDE:
case TRIGGER_VELOCITY:
case TRIGGER_ACCELERATION_10:
case TRIGGER_FLIGHT_PHASE:
case TRIGGER_TEMPERATURE_10:
case TRIGGER_BATERY_10:
case TRIGGER_APOGEE:
case TRIGGER_MAX_VELOCITY:
case TRIGGER_MAX_ACCELERATION:
element->data.altitude *= 10;
element->data.altitude += byte_received - '0';
break;
case TRIGGER_CHANNEL:
case TRIGGER_NAME:
element->data.name[counter++] = byte_received;
break;
default:
break;
}
}
/**
* @brief This function is used to parse the data received from the Eggtimer
* @param byte_received byte received
* @param packet package to store the data
*
* @return true if receive new data packet, false otherwise
*/
bool decode_eggtimer_data(char byte_received, EggtimerElementPacket * packet){
switch (byte_received)
{
case TRIGGER_FLIGHT_TIME:
case TRIGGER_ALTITUDE_100:
case TRIGGER_ALTITUDE:
case TRIGGER_VELOCITY:
case TRIGGER_ACCELERATION_10:
case TRIGGER_FLIGHT_PHASE:
case TRIGGER_CHANNEL:
case TRIGGER_TEMPERATURE_10:
case TRIGGER_NAME:
case TRIGGER_BATERY_10:
case TRIGGER_APOGEE:
case TRIGGER_MAX_VELOCITY:
case TRIGGER_MAX_ACCELERATION:
_last_state = byte_received;
packet->type = byte_received;
memset(&packet->data, 0, sizeof packet->data);
Serial.println( byte_received);
counter = 0;
break;
case TRIGGER_END_PACKET:
if (_last_state){
packet->type = _last_state;
_last_state = 0;
return true;
}
_last_state = 0;
break;
default:
_save_data(byte_received, packet);
break;
}
return false;
}
bool test(){
char str[] = "{004>@5>#0132>~1B---->(00023>\\000>%04679>^0660>[025>?079>!201>=KM6ZFL>";
char *p = str;
EggtimerElementPacket packet;
for (; *p; p++)
{
if(decode_eggtimer_data(*p, &packet)){
Serial.print("Data packet: ");
if(packet.type == TRIGGER_NAME || packet.type == TRIGGER_CHANNEL){
Serial.println(packet.data.name);
}
else{
Serial.println(packet.data.altitude);
}
}
}
return false;
}
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Ready!");
test();
}
void loop() {
}