Каркасик. Поток слушающий соединения, апдейтилка температуры, регистры, конфиг, логирование.
This commit is contained in:
+93
-61
@@ -1,28 +1,23 @@
|
||||
|
||||
#include "configparser.h"
|
||||
#include "grove.h"
|
||||
#include "jhd1313m1.h"
|
||||
#include "mraa/common.hpp"
|
||||
#include "mraa/gpio.hpp"
|
||||
#include "globals.h"
|
||||
#include "ModbusServiceListenThread.h"
|
||||
#include "libdiff.h"
|
||||
|
||||
#include <climits>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <unistd.h>
|
||||
#include <sys/file.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define GPIO_PIN_1 2
|
||||
|
||||
void temperature_update(upm::GroveTemp* temperature_sensor, upm::Jhd1313m1 *lcd)
|
||||
{
|
||||
// minimum and maximum temperatures registered, the initial values will be
|
||||
// replaced after the first read
|
||||
static int min_temperature = INT_MAX;
|
||||
static int max_temperature = INT_MIN;
|
||||
|
||||
// the temperature range in degrees Celsius,
|
||||
// adapt to your room temperature for a nicer effect!
|
||||
const int TEMPERATURE_RANGE_MIN_VAL = 18;
|
||||
const int TEMPERATURE_RANGE_MAX_VAL = 31;
|
||||
|
||||
// other helper variables
|
||||
int temperature; // temperature sensor value in degrees Celsius
|
||||
float fade; // fade value [0.0 .. 1.0]
|
||||
@@ -30,31 +25,29 @@ void temperature_update(upm::GroveTemp* temperature_sensor, upm::Jhd1313m1 *lcd)
|
||||
std::stringstream row_1, row_2; // LCD rows
|
||||
|
||||
// update the min and max temperature values
|
||||
temperature = temperature_sensor->value();
|
||||
|
||||
if (temperature < min_temperature) {
|
||||
min_temperature = temperature;
|
||||
}
|
||||
if (temperature > max_temperature) {
|
||||
max_temperature = temperature;
|
||||
}
|
||||
temperature = temperature_sensor->value() * 100;
|
||||
registers[REGISTER_CUR_TEMP] = temperature;
|
||||
|
||||
// display the temperature values on the LCD
|
||||
row_1 << "Temp " << temperature << " ";
|
||||
row_2 << "Min " << min_temperature << " Max " << max_temperature << " ";
|
||||
row_1 << "T " << temperature / 100 << " Mode: ";
|
||||
int mode = registers[REGISTER_MODE];
|
||||
if (mode == 0) row_1 << "auto";
|
||||
else if (mode == 1) row_1 << "on";
|
||||
else if (mode == 2) row_1 << "off";
|
||||
row_2 << "On: " << registers[REGISTER_ON_TEMP] / 100 << " Off " << registers[REGISTER_OFF_TEMP] / 100 << " ";
|
||||
lcd->setCursor(0,0);
|
||||
lcd->write(row_1.str());
|
||||
lcd->setCursor(1,0);
|
||||
lcd->write(row_2.str());
|
||||
|
||||
// set the fade value depending on where we are in the temperature range
|
||||
if (temperature <= TEMPERATURE_RANGE_MIN_VAL) {
|
||||
if (temperature <= registers[REGISTER_ON_TEMP]) {
|
||||
fade = 0.0;
|
||||
} else if (temperature >= TEMPERATURE_RANGE_MAX_VAL) {
|
||||
} else if (temperature >= registers[REGISTER_OFF_TEMP]) {
|
||||
fade = 1.0;
|
||||
} else {
|
||||
fade = (float)(temperature - TEMPERATURE_RANGE_MIN_VAL) /
|
||||
(TEMPERATURE_RANGE_MAX_VAL - TEMPERATURE_RANGE_MIN_VAL);
|
||||
fade = (float)(temperature - registers[REGISTER_ON_TEMP]) /
|
||||
(registers[REGISTER_OFF_TEMP] - registers[REGISTER_ON_TEMP]);
|
||||
}
|
||||
|
||||
// fade the color components separately
|
||||
@@ -68,45 +61,84 @@ void temperature_update(upm::GroveTemp* temperature_sensor, upm::Jhd1313m1 *lcd)
|
||||
|
||||
int main()
|
||||
{
|
||||
// check that we are running on Galileo or Edison
|
||||
mraa_platform_t platform = mraa_get_platform_type();
|
||||
if ((platform != MRAA_INTEL_GALILEO_GEN1) &&
|
||||
(platform != MRAA_INTEL_GALILEO_GEN2) &&
|
||||
(platform != MRAA_INTEL_EDISON_FAB_C))
|
||||
{
|
||||
std::cerr << "Unsupported platform, exiting" << std::endl;
|
||||
return MRAA_ERROR_INVALID_PLATFORM;
|
||||
}
|
||||
try {
|
||||
// ����� lock-file, ����� ���������� ������ ���� ��������� �������
|
||||
int lockfile = open("defrost.lock", O_RDWR | O_CREAT, 0777);
|
||||
if (lockfile == -1)
|
||||
{
|
||||
cout << "Unable to open file for locking, errno = " << errno << "\n";
|
||||
return 1;
|
||||
}
|
||||
if (flock(lockfile, LOCK_EX | LOCK_NB) == -1)
|
||||
{
|
||||
cout << "Unable to lock file, errno = " << errno << "\n";
|
||||
return 1;
|
||||
}
|
||||
// check that we are running on Galileo or Edison
|
||||
mraa_platform_t platform = mraa_get_platform_type();
|
||||
if ((platform != MRAA_INTEL_GALILEO_GEN1) &&
|
||||
(platform != MRAA_INTEL_GALILEO_GEN2) &&
|
||||
(platform != MRAA_INTEL_EDISON_FAB_C))
|
||||
{
|
||||
std::cerr << "Unsupported platform, exiting" << std::endl;
|
||||
return MRAA_ERROR_INVALID_PLATFORM;
|
||||
}
|
||||
|
||||
mraa::Gpio gpio(GPIO_PIN_1);
|
||||
mraa_result_t status = gpio.dir(mraa::DIR_OUT);
|
||||
if (status != MRAA_SUCCESS)
|
||||
mraa::Gpio gpio(GPIO_PIN_1);
|
||||
mraa_result_t status = gpio.dir(mraa::DIR_OUT);
|
||||
if (status != MRAA_SUCCESS)
|
||||
{
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// temperature sensor connected to A0 (analog in)
|
||||
upm::GroveTemp* temp_sensor = new upm::GroveTemp(0);
|
||||
|
||||
// LCD connected to the default I2C bus
|
||||
upm::Jhd1313m1* lcd = new upm::Jhd1313m1(0);
|
||||
|
||||
if ((temp_sensor == NULL) || (lcd == NULL))
|
||||
|
||||
{
|
||||
std::cerr << "Can't create all objects, exiting" << std::endl;
|
||||
return MRAA_ERROR_UNSPECIFIED;
|
||||
}
|
||||
// load config
|
||||
ConfigParser cfg;
|
||||
cfg.Load("defrost.cfg");
|
||||
mainlog.InitLogging(cfg.GetParam("LogPath"), LIBLOGSINGLEFILE);
|
||||
mainlog.LogString("Defrost service started");
|
||||
|
||||
if (pthread_mutex_init(®ister_mutex, NULL) != 0)
|
||||
{
|
||||
mainlog.LogString("Unable to create registers_mutex");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
registers[REGISTER_ON_TEMP] = 100;
|
||||
registers[REGISTER_OFF_TEMP] = 500;
|
||||
registers[REGISTER_MODE] = 0;
|
||||
|
||||
// run modbus service listen thread
|
||||
ModbusServiceListenThread *modbus_service = new ModbusServiceListenThread(&mainlog, atoi(cfg.GetParam("ListenPort").c_str()));
|
||||
modbus_service->Start();
|
||||
|
||||
int io_value = 0;
|
||||
// loop forever updating the temperature values every second
|
||||
while (true)
|
||||
{
|
||||
LockMutex(register_mutex);
|
||||
temperature_update(temp_sensor, lcd);
|
||||
if (registers[REGISTER_CUR_TEMP] < registers[REGISTER_ON_TEMP]) io_value = 1;
|
||||
else if (registers[REGISTER_CUR_TEMP] > registers[REGISTER_OFF_TEMP]) io_value = 0;
|
||||
gpio.write(io_value);
|
||||
ReleaseMutex(register_mutex);
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
catch (string s)
|
||||
{
|
||||
cerr << "Exception: " << s << "\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// temperature sensor connected to A0 (analog in)
|
||||
upm::GroveTemp* temp_sensor = new upm::GroveTemp(0);
|
||||
|
||||
// LCD connected to the default I2C bus
|
||||
upm::Jhd1313m1* lcd = new upm::Jhd1313m1(0);
|
||||
|
||||
if ((temp_sensor == NULL) || (lcd == NULL))
|
||||
|
||||
{
|
||||
std::cerr << "Can't create all objects, exiting" << std::endl;
|
||||
return MRAA_ERROR_UNSPECIFIED;
|
||||
}
|
||||
|
||||
int io_value = 1;
|
||||
// loop forever updating the temperature values every second
|
||||
for (;;) {
|
||||
temperature_update(temp_sensor, lcd);
|
||||
gpio.write(io_value);
|
||||
io_value = 1 - io_value;
|
||||
std::cerr << io_value << "\n";
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
return MRAA_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user