[uC] Added a header file for packets

This commit is contained in:
giuliof 2019-06-26 22:45:37 +02:00
parent 71d721057b
commit 77dd3293f2
2 changed files with 86 additions and 81 deletions

85
dg-arduino/dg-arduino.h Normal file
View File

@ -0,0 +1,85 @@
#ifndef DG_ARDUINO_H_
#define DG_ARDUINO_H_
enum {CMD_CLIMA, CMD_TEMP, CMD_ERR, CMD_DEBUG};
struct Packet_t {
uint8_t size;
uint8_t cmd;
// Note: at the end a CHK is append and size is automatically updated by SendPacket method
Packet_t() {
}
Packet_t(uint8_t size, uint8_t cmd) {
this->size = size;
this->cmd = cmd;
}
};
struct PackClimaCmd_t : Packet_t {
uint8_t cmd_clima;
PackClimaCmd_t() {
this->size = sizeof(PackClimaCmd_t);
this->cmd = CMD_CLIMA;
//this->cmd_clima = cmd_clima;
}
};
struct PackTempCmd_t : Packet_t {
uint8_t cmd_temp;
PackTempCmd_t() {
this->size = sizeof(PackClimaCmd_t);
this->cmd = CMD_TEMP;
//this->cmd_temp = cmd_temp;
}
};
struct PackReceived_t : Packet_t {
uint8_t buffer[BUF_SIZE];
PackReceived_t() {
// Uninitalised, will be populated by reading function
}
};
struct PackSendTemp_t : Packet_t {
int temp;
PackSendTemp_t(int temp) {
this->size = sizeof(PackSendTemp_t);
this->cmd = CMD_TEMP;
this->temp = temp;
}
};
struct PackError_t : Packet_t {
uint8_t err_code;
PackError_t(uint8_t err_code) {
this->size = sizeof(PackError_t);
this->cmd = CMD_CLIMA;
this->err_code = err_code;
}
};
struct PackMsg_t : Packet_t {
uint8_t msg[10];
PackMsg_t(uint8_t size) {
this->size = size + sizeof(Packet_t);
this->cmd = CMD_DEBUG;
}
};
void SendPacket(void *_p) {
uint8_t * p = (uint8_t*)_p;
uint8_t chk = 0;
// increase by one to keep CHK in account
uint8_t size = *p;
*p = *p + 1;
for (uint8_t c = 0; c < size; c++) {
Serial.write(*p);
chk += *p;
p++;
}
Serial.write(chk);
}
#endif

View File

@ -4,6 +4,7 @@
*/
#include <Servo.h>
#include "dg-arduino.h"
#define SERVO_ON 55
#define SERVO_OFF 80
#define SERVO_PIN 9
@ -16,87 +17,6 @@ Servo myServo;
#define BUF_SIZE 10
enum {CMD_CLIMA, CMD_TEMP, CMD_ERR, CMD_DEBUG};
struct Packet_t {
uint8_t size;
uint8_t cmd;
// Note: at the end a CHK is append and size is automatically updated by SendPacket method
Packet_t() {
}
Packet_t(uint8_t size, uint8_t cmd) {
this->size = size;
this->cmd = cmd;
}
};
struct PackClimaCmd_t : Packet_t {
uint8_t cmd_clima;
PackClimaCmd_t(/*uint8_t cmd_clima*/) {
this->size = sizeof(*this);
this->cmd = CMD_CLIMA;
//this->cmd_clima = cmd_clima;
}
};
struct PackTempCmd_t : Packet_t {
uint8_t cmd_temp;
PackTempCmd_t(/*uint8_t cmd_temp*/) {
this->size = sizeof(*this);
this->cmd = CMD_TEMP;
//this->cmd_temp = cmd_temp;
}
};
struct PackReceived_t : Packet_t {
uint8_t buffer[BUF_SIZE];
PackReceived_t() {
// Uninitalised, will be populated by reading function
}
};
struct PackSendTemp_t : Packet_t {
int temp;
PackSendTemp_t(int temp) {
this->size = sizeof(*this);
this->cmd = CMD_TEMP;
this->temp = temp;
}
};
struct PackError_t : Packet_t {
uint8_t err_code;
PackError_t(uint8_t err_code) {
this->size = sizeof(PackError_t);
this->cmd = CMD_CLIMA;
this->err_code = err_code;
}
};
struct PackMsg_t : Packet_t {
uint8_t msg[10];
PackMsg_t(uint8_t size) {
this->size = size + sizeof(Packet_t);
this->cmd = CMD_DEBUG;
}
};
void SendPacket(void *_p) {
uint8_t * p = _p;
uint8_t chk = 0;
// increase by one to keep CHK in account
uint8_t size = *p;
*p = *p + 1;
for (uint8_t c = 0; c < size; c++) {
Serial.write(*p);
chk += *p;
p++;
}
Serial.write(chk);
}
PackReceived_t PackReceived;
int temperatures[10];
unsigned long int interval = 30000; // 30 seconds