gbmanager/src/gbmanager.cpp

113 lines
3.2 KiB
C++

#include <string>
#include "../modules/fmt/include/fmt/format.h"
#include "../modules/pico-onewire/api/one_wire.h"
#include "hardware/i2c.h"
#include "pico/stdlib.h"
#include "config.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;
constexpr uint BUTTON_1_PIN = 17;
constexpr uint BUTTON_2_PIN = 16;
constexpr uint BUTTON_3_PIN = 15;
// Custom chars for the LCD
constexpr char CUSTOM_CHAR_DEG = 0xDF;
constexpr char CUSTOM_CHAR_AE = 0xE1;
// Global variables which have to be accessed by callback function
bool isSystemOn = false;
float temp_tgt{28.0};
absolute_time_t lastPressed = get_absolute_time();
using std::string;
void buttonPressedCallback(uint gpio, [[maybe_unused]] uint32_t events) {
absolute_time_t now = get_absolute_time();
if (absolute_time_diff_us(lastPressed, now) < 750000) {
return;
} else {
lastPressed = now;
}
switch (gpio) {
case BUTTON_1_PIN:
temp_tgt -= 0.5;
break;
case BUTTON_2_PIN:
temp_tgt += 0.5;
break;
case BUTTON_3_PIN:
isSystemOn = !isSystemOn;
break;
}
}
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);
// Initialize the Buttons
gpio_set_irq_enabled_with_callback(BUTTON_1_PIN, GPIO_IRQ_EDGE_FALL, true,
&buttonPressedCallback);
gpio_set_irq_enabled_with_callback(BUTTON_2_PIN, GPIO_IRQ_EDGE_FALL, true,
&buttonPressedCallback);
gpio_set_irq_enabled_with_callback(BUTTON_3_PIN, GPIO_IRQ_EDGE_FALL, true,
&buttonPressedCallback);
float temp_act{0};
float temp_diff{0.5};
string lcdText{};
bool isHeating = false;
string heatInfo{""};
string systemInfo{""};
lcdText = fmt::format(" G{}rbox Manager\n (Ver. {})", CUSTOM_CHAR_AE,
PROJECT_VERSION);
myLCD.sendString(lcdText);
sleep_ms(3000);
while (true) {
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 = " ";
isSystemOn ? systemInfo = "ON " : systemInfo = "OFF";
relais.activate(isHeating);
lcdText = fmt::format("ACT: {:05.2f}{}C {}\nACT: {:05.2f}{}C {}",
temp_act, CUSTOM_CHAR_DEG, heatInfo, temp_tgt,
CUSTOM_CHAR_DEG, systemInfo);
myLCD.setCursor(0, 0);
myLCD.sendString(lcdText);
}
}