Assignment 3: Code after testing localy added

This commit is contained in:
2020-06-28 16:59:18 +03:00
parent c459375677
commit 8033e2dc5d
21 changed files with 4734 additions and 0 deletions
+130
View File
@@ -0,0 +1,130 @@
/*
* console.c
*
* Created on: Jun 28, 2020
* Author: hoo2
*/
#include "console.h"
static char _buffer[COM_BUFFER_SIZE];
static int _cursor;
static char _ans[512];
void parse (char* buffer) {
int i;
float_t s;
if ((strncmp((const void*)buffer, "help", 4) == 0) ||
(strncmp((const void*)buffer, "?", 1) == 0) ) {
i = sprintf (_ans, "\r\nThermostat commands:\r\n");
i += sprintf (&_ans[i], "info : Get information\r\n");
i += sprintf (&_ans[i], "set Tset <val> : Set Thermostat\'s temperature\r\n");
i += sprintf (&_ans[i], "set Thyst <val> : Set Thermostat\'s hysteresis\r\n");
i += sprintf (&_ans[i], "set Pset <val> : Set Proximity\r\n");
i += sprintf (&_ans[i], "set Physt <val> : Set Proximity\'s hysteresis\r\n");
i += sprintf (&_ans[i], "set mode heating : Set Thermostat to heating mode\r\n");
i += sprintf (&_ans[i], "set mode cooling : Set Thermostat to cooling mode\r\n");
i += sprintf (&_ans[i], "\r\n");
_ans[i] =0;
}
else if (strncmp((const void*)buffer, "info", 4) == 0) {
i = sprintf (_ans, "\r\nThermostat data:\r\n");
i += sprintf (&_ans[i], "Tcur = %4.1f\r\n", temp_get_current ());
i += sprintf (&_ans[i], "Tav = %4.1f\r\n", app_data.Tav);
i += sprintf (&_ans[i], "Mode = %s\r\n", (settings.mode == HEATING) ? "Heating" : "Cooling");
i += sprintf (&_ans[i], "Output = %s\r\n", (app_data.flag_output) ? "On" : "Off");
i += sprintf (&_ans[i], "Tset = %4.1f, Hyst = %4.1f\r\n", settings.Tset, settings.Thyst);
i += sprintf (&_ans[i], "Pset = %4.1f, Hyst = %4.1f\r\n", settings.Pset, settings.Physt);
i += sprintf (&_ans[i], "\r\n");
_ans[i] =0;
}
else if (strncmp((const void*)buffer, "set ", 4) == 0) {
if (strncmp((const void*)&buffer[4], "Tset ", 5) == 0) {
sscanf((const void*)&buffer[9], "%f", &s);
if (s > -20 && s < 80) {
settings.Tset = s;
i = sprintf (_ans, "OK\r\n");
}
else
i = sprintf (_ans, "Error\r\n");
_ans[i] =0;
}
else if (strncmp((const void*)&buffer[4], "Thyst ", 6) == 0) {
sscanf((const void*)&buffer[10], "%f", &s);
if (s > 0 && s < 20) {
settings.Thyst = s;
i = sprintf (_ans, "OK\r\n");
}
else
i = sprintf (_ans, "Error\r\n");
_ans[i] =0;
}
else if (strncmp((const void*)&buffer[4], "Pset ", 5) == 0) {
sscanf((const void*)&buffer[9], "%f", &s);
if (s > 0 && s < 100)
settings.Pset = s;
i = sprintf (_ans, "OK\r\n");
_ans[i] =0;
}
else if (strncmp((const void*)&buffer[4], "Physt ", 6) == 0) {
sscanf((const void*)&buffer[10], "%f", &s);
if (s > 0 && s < 50) {
settings.Physt = s;
i = sprintf (_ans, "OK\r\n");
}
else
i = sprintf (_ans, "Error\r\n");
_ans[i] =0;
}
else if (strncmp((const void*)&buffer[4], "mode ", 5) == 0) {
if (strncmp((const void*)&buffer[9], "heating", 7) == 0) {
settings.mode = HEATING;
i = sprintf (_ans, "OK\r\n");
_ans[i] =0;
}
else if (strncmp((const void*)&buffer[9], "cooling", 7) == 0) {
settings.mode = COOLING;
i = sprintf (_ans, "OK\r\n");
_ans[i] =0;
}
}
else {
i = sprintf (_ans, "Error\r\n");
_ans[i] =0;
}
}
else {
i = sprintf (_ans, "Error\r\n");
_ans[i] =0;
}
COM_Transmit((uint8_t*)_ans, i);
}
void console (void) {
int ch;
if (COM_CheckReceive()) {
ch = COM_getchar(); // get character
COM_putchar((char)ch); // Echo back
_buffer[_cursor++] = ch; // store
// Backspace, delete
if (ch == 0x08 || ch == 0x7F)
--_cursor;
if (ch == 0 || ch == '\n' || ch == '\r') {
COM_putchar('\n'); // Echo an extra '\n'
_buffer[_cursor -1] = 0;
_cursor =0;
parse(_buffer);
}
if (_cursor >= COM_BUFFER_SIZE) {
_buffer[_cursor -1] = 0;
_cursor =0;
parse(_buffer);
}
}
}
+19
View File
@@ -0,0 +1,19 @@
/*
* console.h
*
* Created on: Jun 28, 2020
* Author: hoo2
*/
#ifndef CONSOLE_H_
#define CONSOLE_H_
#include <hal.h>
#include "thermostat.h"
#include <string.h>
extern float_t temp_get_current (void);
void console (void);
#endif /* CONSOLE_H_ */
+187
View File
@@ -0,0 +1,187 @@
/*!
* \file
* main.c
* \brief
* Main application file
*
* Author: Christos Choutouridis AEM: 8997
* email : <cchoutou@ece.auth.gr>
*/
#include <hal.h>
#include "thermostat.h"
#include "console.h"
/*
* Zero initialized globals
*/
app_data_t app_data;
state_en state;
// Value initialized globals
settings_t _Init_settings(settings);
/*
* helper api
*/
float_t temp_get_current (void) {
return (app_data.cur) ? app_data.T[app_data.cur-1] : app_data.T[MEASUREMENTS-1];
}
void control (void) {
static time_t sample =0;
time_t now = time(0); // get time
// Proximity
if (proximity(&prox) < settings.Pset)
app_data.flag_proximity = 1;
else if (proximity(&prox) < settings.Pset + settings.Physt)
;
else
app_data.flag_proximity = 0;
// Temperature calculation
if (now - sample >= MEAS_INTERVAL) {
sample = now;
app_data.T[app_data.cur] = temp_read();
if (++app_data.cur >= MEASUREMENTS) {
app_data.cur =0;
float_t f =0;
for (int i=0 ; i<MEASUREMENTS ; ++i) {
f += app_data.T[i];
}
app_data.Tav = f / MEASUREMENTS;
app_data.signal_cycle = 1;
app_data.flag_init = 0;
}
}
// Output handling
if (!app_data.flag_init) {
switch (settings.mode) {
case HEATING: {
if (app_data.Tav <= settings.Tset)
app_data.flag_output = 1;
else if (app_data.Tav < settings.Tset + settings.Thyst)
;
else
app_data.flag_output = 0;
}
break;
case COOLING: {
if (app_data.Tav <= settings.Tset - settings.Thyst)
app_data.flag_output = 0;
else if (app_data.Tav < settings.Tset)
;
else
app_data.flag_output = 1;
}
break;
}
}
}
void display (void) {
static time_t mark =0;
char line1[18];
char line2[18];
time_t now = time(0);
switch (state) {
case ST_INIT:
sprintf(line1, "\fInitializing %c ", (time(0)%2) ? '*' : ' ');
sprintf(line2, "\n ");
// escapes
if (app_data.flag_proximity)
state = ST_USER;
else if (app_data.signal_cycle) {
app_data.signal_cycle =0;
mark = now;
state = ST_AVERAGE;
}
break;
case ST_COUNT:
sprintf(line1, "\fMeasuring %c ", (time(0)%2) ? '*' : ' ');
sprintf(line2, "\n%s: %s ",
(settings.mode == HEATING) ? "Heating" : "Cooling",
(app_data.flag_output) ? "On " : "Off"
);
// escapes
if (app_data.flag_proximity)
state = ST_USER;
else if (app_data.signal_cycle) {
app_data.signal_cycle =0;
mark = now;
state = ST_AVERAGE;
}
break;
case ST_AVERAGE:
sprintf(line1, "\fTav=%4.1f ", app_data.Tav);
sprintf(line2, "\n%s: %s ",
(settings.mode == HEATING) ? "Heating" : "Cooling",
(app_data.flag_output) ? "On " : "Off"
);
// escapes
if (app_data.flag_proximity)
state = ST_USER;
else if (now - mark >= UI_AVERAGE_TIME)
state = ST_COUNT;
break;
case ST_USER:
sprintf(line1, "\fTav=%4.1f T=%4.1f", app_data.Tav, temp_get_current());
sprintf(line2, "\n%s: %s ",
(settings.mode == HEATING) ? "Heating" : "Cooling",
(app_data.flag_output) ? "On " : "Off"
);
// escapes
if (!app_data.flag_proximity)
state = ST_COUNT;
break;
}
lcd_puts(line1);
lcd_puts(line2);
}
void outputs (void) {
static uint8_t pr_output =0;
if (app_data.flag_init)
return;
led_green(app_data.flag_output && settings.mode == COOLING);
led_red (app_data.flag_output && settings.mode == HEATING);
if (app_data.flag_output != pr_output) {
relay(app_data.flag_output);
pr_output = app_data.flag_output;
}
}
int main(void) {
hal_hw_init ();
lcd_init ();
proximity_init(&prox);
temp_init (TEMP_11_BIT);
lcd_enable(1);
app_data.flag_init = 1;
while (1) {
control ();
display ();
outputs();
console ();
HAL_Delay(10);
}
}
+65
View File
@@ -0,0 +1,65 @@
/*
* thermostat.h
*
* Created on: Jun 27, 2020
* Author: hoo2
*/
#ifndef THERMOSTAT_H_
#define THERMOSTAT_H_
#define MEAS_WINDOW (120) // [sec]
#define MEAS_INTERVAL (5) // [sec]
#define MEASUREMENTS (MEAS_WINDOW / MEAS_INTERVAL) // keep this integer
#define UI_AVERAGE_TIME (10) // [sec]
//#define UI_USER_TIME (10)
typedef float float_t;
typedef int int_t;
typedef enum {
HEATING =0,
COOLING
} mode_en;
typedef struct {
float_t T[MEASUREMENTS];
float_t Tav;
uint32_t cur;
uint8_t flag_output;
uint8_t flag_init;
uint8_t flag_proximity;
uint8_t signal_cycle;
} app_data_t;
typedef struct {
mode_en mode;
float_t Tset;
float_t Thyst;
float_t Pset;
float_t Physt;
} settings_t;
typedef enum {
ST_INIT =0,
ST_COUNT,
ST_AVERAGE,
ST_USER
} state_en;
/*
* globals
*/
extern app_data_t app_data;
extern state_en state;
extern settings_t settings;
#define _Init_settings(s) s = { \
.mode = HEATING, \
.Tset = 21.0, \
.Thyst = 2.0, \
.Pset = 75.0, \
.Physt = 10.0 \
}
#endif /* THERMOSTAT_H_ */