91 lines
2.4 KiB
C++
91 lines
2.4 KiB
C++
#include <iomanip>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
|
|
#include "../modules/pico-onewire/api/one_wire.h"
|
|
#include "hardware/i2c.h"
|
|
#include "pico/stdlib.h"
|
|
|
|
#include "config.h"
|
|
#include "ds18b20.h"
|
|
#include "lcd.h"
|
|
#include "relais.h"
|
|
|
|
// GPIOs used
|
|
constexpr uint I2C_SDA_PIN = 26;
|
|
constexpr uint I2C_SCL_PIN = 27;
|
|
constexpr uint DS18B20_PIN = 28;
|
|
constexpr uint RELAIS_PIN = 18;
|
|
|
|
// Custom chars for the LCD
|
|
constexpr char CUSTOM_CHAR_DEG = 0xDF;
|
|
constexpr char CUSTOM_CHAR_AE = 0xE1;
|
|
|
|
using std::string;
|
|
|
|
int main() {
|
|
// Enable UART so we can print status output
|
|
stdio_init_all();
|
|
|
|
// Initialize the LCD
|
|
auto myLCD = LCD(i2c1, I2C_SDA_PIN, I2C_SCL_PIN);
|
|
myLCD.clear();
|
|
|
|
// Initialize the temp sensor
|
|
One_wire oneWire(DS18B20_PIN);
|
|
oneWire.init();
|
|
rom_address_t address{};
|
|
oneWire.single_device_read_rom(address);
|
|
|
|
// Initialize the relais
|
|
Relais relais(RELAIS_PIN);
|
|
|
|
float temp_act{0};
|
|
float temp_tgt{28.0};
|
|
float temp_diff{0.5};
|
|
std::stringstream lcdText{};
|
|
bool isHeating = false;
|
|
bool isSystemOn = false;
|
|
string heatInfo{" "};
|
|
string systemInfo{"OFF"};
|
|
|
|
lcdText << " G" << CUSTOM_CHAR_AE << "rbox Manager\n (Ver. "
|
|
<< PROJECT_VERSION << ")";
|
|
myLCD.sendString(lcdText.str());
|
|
sleep_ms(3000);
|
|
|
|
while (true) {
|
|
absolute_time_t start = get_absolute_time();
|
|
oneWire.convert_temperature(address, true, false);
|
|
temp_act = oneWire.temperature(address);
|
|
|
|
if (isSystemOn && temp_act < temp_tgt - temp_diff) {
|
|
isHeating = true;
|
|
} else if (isSystemOn && temp_act > temp_tgt + temp_diff) {
|
|
isHeating = false;
|
|
} else if (!isSystemOn) {
|
|
isHeating = false;
|
|
}
|
|
isHeating ? heatInfo = ">H<" : heatInfo = " ";
|
|
|
|
relais.activate(isHeating);
|
|
|
|
lcdText.str("");
|
|
lcdText.clear();
|
|
lcdText.precision(4);
|
|
lcdText << "ACT: " << temp_act << CUSTOM_CHAR_DEG << "C " << heatInfo
|
|
<< "\n"
|
|
<< "TGT: " << temp_tgt << CUSTOM_CHAR_DEG << "C " << systemInfo;
|
|
|
|
myLCD.setCursor(0, 0);
|
|
myLCD.sendString(lcdText.str());
|
|
|
|
absolute_time_t stop = get_absolute_time();
|
|
|
|
int64_t duration_ms = absolute_time_diff_us(start, stop) / 1000;
|
|
int64_t timeToSleep = 1000 - duration_ms;
|
|
|
|
if (timeToSleep > 0)
|
|
sleep_ms(timeToSleep);
|
|
}
|
|
} |