#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 #include #include #include #include #include #define GPIO_PIN_1 2 void temperature_update(upm::GroveTemp* temperature_sensor, upm::Jhd1313m1 *lcd) { // other helper variables int temperature; // temperature sensor value in degrees Celsius float fade; // fade value [0.0 .. 1.0] uint8_t r, g, b; // resulting LCD backlight color components [0 .. 255] std::stringstream row_1, row_2; // LCD rows // update the min and max temperature values temperature = temperature_sensor->value() * 100; registers[REGISTER_CUR_TEMP] = temperature; // display the temperature values on the LCD 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 <= registers[REGISTER_ON_TEMP]) { fade = 0.0; } else if (temperature >= registers[REGISTER_OFF_TEMP]) { fade = 1.0; } else { fade = (float)(temperature - registers[REGISTER_ON_TEMP]) / (registers[REGISTER_OFF_TEMP] - registers[REGISTER_ON_TEMP]); } // fade the color components separately r = (int)(255 * fade); g = (int)(64 * fade); b = (int)(255 * (1 - fade)); // apply the calculated result lcd->setColor(r, g, b); } int main() { 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) { 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; } return MRAA_SUCCESS; }