gbmanager/src/gbmanager.cpp

120 lines
3.4 KiB
C++
Raw Permalink Normal View History

2022-06-03 14:33:42 +02:00
#include <string>
2022-05-30 14:10:48 +02:00
2022-06-03 14:33:42 +02:00
#include "../modules/fmt/include/fmt/format.h"
2022-06-01 13:38:22 +02:00
#include "../modules/pico-onewire/api/one_wire.h"
2022-05-30 16:09:42 +02:00
#include "hardware/i2c.h"
2022-05-30 14:10:48 +02:00
#include "pico/stdlib.h"
2022-05-31 13:01:10 +02:00
#include "config.h"
2022-05-31 12:31:29 +02:00
#include "lcd.h"
2022-05-31 13:53:19 +02:00
#include "relais.h"
2022-05-30 16:09:42 +02:00
2022-05-31 13:53:19 +02:00
// GPIOs used
2022-05-30 16:09:42 +02:00
constexpr uint I2C_SDA_PIN = 26;
constexpr uint I2C_SCL_PIN = 27;
2022-05-31 12:31:29 +02:00
constexpr uint DS18B20_PIN = 28;
2022-05-31 13:53:19 +02:00
constexpr uint RELAIS_PIN = 18;
2022-06-01 14:31:17 +02:00
constexpr uint BUTTON_1_PIN = 17;
constexpr uint BUTTON_2_PIN = 16;
constexpr uint BUTTON_3_PIN = 15;
2022-05-31 12:31:29 +02:00
2022-05-31 13:53:19 +02:00
// Custom chars for the LCD
2022-05-31 12:31:29 +02:00
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();
2022-05-31 12:31:29 +02:00
using std::string;
2022-05-30 16:09:42 +02:00
2022-06-03 14:33:42 +02:00
void buttonPressedCallback(uint gpio, [[maybe_unused]] uint32_t events) {
2022-06-02 09:56:45 +02:00
absolute_time_t now = get_absolute_time();
if (absolute_time_diff_us(lastPressed, now) < 500000) {
return;
} else {
2022-06-02 09:56:45 +02:00
lastPressed = now;
}
2022-06-01 21:14:22 +02:00
2022-06-01 14:31:17 +02:00
switch (gpio) {
case BUTTON_1_PIN:
temp_tgt -= 0.5;
2022-06-01 14:31:17 +02:00
break;
case BUTTON_2_PIN:
temp_tgt += 0.5;
2022-06-01 14:31:17 +02:00
break;
case BUTTON_3_PIN:
isSystemOn = !isSystemOn;
2022-06-01 14:31:17 +02:00
break;
}
}
2022-05-30 16:09:42 +02:00
int main() {
2022-05-31 12:31:29 +02:00
// Enable UART so we can print status output
stdio_init_all();
2022-05-31 13:53:19 +02:00
// Initialize the LCD
2022-05-31 12:31:29 +02:00
auto myLCD = LCD(i2c1, I2C_SDA_PIN, I2C_SCL_PIN);
myLCD.clear();
2022-05-31 13:53:19 +02:00
// Initialize the temp sensor
2022-06-01 13:10:01 +02:00
One_wire oneWire(DS18B20_PIN);
oneWire.init();
rom_address_t address{};
oneWire.single_device_read_rom(address);
2022-05-31 12:31:29 +02:00
2022-05-31 13:53:19 +02:00
// Initialize the relais
// It need the button callback function, since it has to disable the on/off
// button for a short amount of time
Relais relais(RELAIS_PIN, &buttonPressedCallback);
2022-05-31 13:53:19 +02:00
2022-06-01 14:31:17 +02:00
// 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);
2022-06-01 14:31:17 +02:00
2022-05-31 12:31:29 +02:00
float temp_act{0};
2022-05-31 13:01:10 +02:00
float temp_diff{0.5};
2022-06-03 14:33:42 +02:00
string lcdText{};
2022-05-31 12:31:29 +02:00
bool isHeating = false;
string heatInfo{""};
string systemInfo{""};
2022-05-31 12:31:29 +02:00
2022-06-03 14:33:42 +02:00
lcdText = fmt::format(" G{}rbox Manager\n (Ver. {})", CUSTOM_CHAR_AE,
PROJECT_VERSION);
myLCD.sendString(lcdText);
2022-05-31 13:01:10 +02:00
sleep_ms(3000);
2022-05-31 12:31:29 +02:00
while (true) {
uint64_t destTime = time_us_64() + 1000000;
2022-06-01 13:38:22 +02:00
oneWire.convert_temperature(address, true, false);
2022-06-01 13:10:01 +02:00
temp_act = oneWire.temperature(address);
2022-05-30 14:10:48 +02:00
2022-05-31 13:01:10 +02:00
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;
}
2022-06-02 09:56:45 +02:00
2022-05-31 13:01:10 +02:00
isHeating ? heatInfo = ">H<" : heatInfo = " ";
isSystemOn ? systemInfo = "ON " : systemInfo = "OFF";
2022-05-31 13:01:10 +02:00
2022-05-31 13:53:19 +02:00
relais.activate(isHeating);
2022-12-23 13:29:10 +01:00
lcdText = fmt::format("ACT: {:05.2f}{}C {}\nTGT: {:05.2f}{}C {}",
2022-06-03 14:33:42 +02:00
temp_act, CUSTOM_CHAR_DEG, heatInfo, temp_tgt,
CUSTOM_CHAR_DEG, systemInfo);
2022-05-31 12:31:29 +02:00
myLCD.setCursor(0, 0);
2022-06-03 14:33:42 +02:00
myLCD.sendString(lcdText);
sleep_until((absolute_time_t){destTime});
2022-05-31 12:31:29 +02:00
}
2022-12-23 13:29:10 +01:00
}