[uC] New clima management

Removed servo 'finger';
Added IR LED
This commit is contained in:
giuliof 2019-06-26 23:07:19 +02:00
parent 77dd3293f2
commit d7fa1335fb
4 changed files with 89 additions and 32 deletions

View File

@ -1,6 +1,8 @@
#ifndef DG_ARDUINO_H_
#define DG_ARDUINO_H_
#define BUF_SIZE 10
enum {CMD_CLIMA, CMD_TEMP, CMD_ERR, CMD_DEBUG};
struct Packet_t {
@ -18,11 +20,10 @@ struct Packet_t {
};
struct PackClimaCmd_t : Packet_t {
uint8_t cmd_clima;
uint32_t cmd_clima;
PackClimaCmd_t() {
this->size = sizeof(PackClimaCmd_t);
this->cmd = CMD_CLIMA;
//this->cmd_clima = cmd_clima;
}
};

View File

@ -2,32 +2,23 @@
Domotic Gateway
by giuliof @ golem.linux.it
*/
#include <Servo.h>
#include "dg-arduino.h"
#define SERVO_ON 55
#define SERVO_OFF 80
#define SERVO_PIN 9
#define TMP_PIN A0
#define BUTTON_PIN 7
Servo myServo;
#define MEAN_SIZE 10
#define BUF_SIZE 10
// 30 seconds, interval between temperature samples
unsigned long int interval = 30000;
PackReceived_t PackReceived;
int temperatures[10];
unsigned long int interval = 30000; // 30 seconds
void setup() {
myServo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
myServo.write(SERVO_OFF);
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(115200); // open a serial connection to your computer
initRemote();
for (uint8_t c = 0; c < 10; c++)
temperatures[c] = analogRead(TMP_PIN);
}
@ -55,26 +46,24 @@ void loop() {
if (ch == PackCHK) {
switch (PackReceived.cmd) {
case CMD_CLIMA:
// At the moment ignore the subcommand
myServo.write(SERVO_ON);
delay(500);
myServo.write(SERVO_OFF);
break;
case CMD_CLIMA: {
PackClimaCmd_t *cp = (PackClimaCmd_t*)&PackReceived;
sendCommand(cp->cmd_clima);
} break;
case CMD_TEMP: {
float mean_tmp = 0;
for (uint8_t c = 0; c < MEAN_SIZE; c++) {
mean_tmp += temperatures[c] / 10.0;
}
PackSendTemp_t p(mean_tmp);
SendPacket(&p);
break;
float mean_tmp = 0;
for (uint8_t c = 0; c < MEAN_SIZE; c++) {
mean_tmp += temperatures[c] / 10.0;
}
PackSendTemp_t p(mean_tmp);
SendPacket(&p);
} break;
default: {
PackError_t err(0x42);
SendPacket(&err);
}
break;
PackError_t err(0x42);
SendPacket(&err);
} break;
}
}

7
dg-arduino/remote.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef DG_ARDUINO_H_
#define DG_ARDUINO_H_
void initRemote();
void sendCommand(uint32_t data);
#endif

60
dg-arduino/remote.ino Normal file
View File

@ -0,0 +1,60 @@
#include <TimerOne.h>
#include "remote.h"
// Function calls to make mark (PWM pulses) and space (LOW)
#define MARK() Timer1.setPwmDuty(9, 472)
#define SPACE() Timer1.setPwmDuty(9, 0)
const uint8_t PWM_PIN = 9;
// Timings
const int PWM_PERIOD = 26;
const int START_PULSE_LENGTH = 4455;
const int MARK_LENGTH = 560;
const int ZERO_SPACE_LENGTH = 1635;
const int ONE_SPACE_LENGTH = 537;
void initRemote() {
Timer1.initialize(PWM_PERIOD);
Timer1.pwm(PWM_PIN, 0);
pinMode(PWM_PIN, OUTPUT);
}
void sendBit(boolean b) {
MARK();
delayMicroseconds(MARK_LENGTH);
SPACE();
delayMicroseconds(b ? ONE_SPACE_LENGTH : ZERO_SPACE_LENGTH);
}
void sendChunk(uint8_t chunk) {
for (uint8_t c = 0; c < 8; c++) {
sendBit(chunk & _BV(7));
chunk <<= 1;
}
}
void sendData(uint32_t data) {
// Header
MARK();
delayMicroseconds(START_PULSE_LENGTH);
SPACE();
delayMicroseconds(START_PULSE_LENGTH);
// Data
sendChunk((data & 0xff0000) >> 16);
sendChunk((~data & 0xff0000) >> 16);
sendChunk((data & 0x00ff00) >> 8);
sendChunk((~data & 0x00ff00) >> 8);
sendChunk(data & 0x0000ff);
sendChunk(~data & 0x0000ff);
sendBit(0);
}
void sendCommand(uint32_t data) {
// Send it twice
sendData(data);
delayMicroseconds(START_PULSE_LENGTH);
sendData(data);
}