diff --git a/.clang-tidy b/.clang-tidy index e6f8d5b..196f4fb 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,13 +1,14 @@ Checks: ' -*, bugprone-*, +-bugprone-easily-swappable-parameters cert-*, -cert-err58-cpp, -cert-dcl16-c, cppcoreguidelines-*, -cppcoreguidelines-avoid-magic-numbers, -modernize-*, -cppcoreguidelines-avoid-non-const-global-variables, +modernize-*, -modernize-use-nodiscard, -modernize-avoid-c-arrays, -modernize-use-trailing-return-type, @@ -24,5 +25,4 @@ hicpp-*,-hicpp-special-member-functions, -hicpp-no-array-decay, -hicpp-noexcept-move, -hicpp-uppercase-literal-suffix, --bugprone-easily-swappable-parameters ' \ No newline at end of file diff --git a/include/utils.hpp b/include/utils.hpp index b0d4566..4485033 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -1,18 +1,28 @@ #ifndef OBC_UTILS_HPP #define OBC_UTILS_HPP +#include + #include namespace utl { enum class Logger { Debug, Flight }; +enum class Modes { Idle, Measurements, Experiment, Uplink }; + template constexpr auto to_underlying(Enum e) { return static_cast>(e); } +void handle_mode(Modes mode); +void handle_idle(); +void handle_measurements(); +void handle_experiment(); +void handle_uplink(); + } // namespace utl #endif \ No newline at end of file diff --git a/src/buzzer.cpp b/src/buzzer.cpp index c6d4aca..35419cf 100644 --- a/src/buzzer.cpp +++ b/src/buzzer.cpp @@ -2,14 +2,16 @@ #include +constexpr auto buzzer_pin = LED_BUILTIN; // PC13 + namespace obc { void buzzer(std::size_t quantity) { for (std::size_t i = 0; i < quantity; ++i) { - digitalWrite(D6, 255); + digitalWrite(buzzer_pin, HIGH); delay(100); - digitalWrite(D6, LOW); + digitalWrite(buzzer_pin, LOW); delay(200); } } diff --git a/src/devices.cpp b/src/devices.cpp index a512fd8..174f265 100644 --- a/src/devices.cpp +++ b/src/devices.cpp @@ -2,15 +2,23 @@ #include -extern MMA8452Q accelerometer; -extern BMP280 bmp; -extern Adafruit_GPS gps; +HardwareSerial Serial2{PA3, PA2}; +Adafruit_GPS gps{&Serial2}; +MMA8452Q accelerometer; +BMP280 bmp; + +constexpr auto custom_sda = PB9; +constexpr auto custom_scl = PB8; +constexpr auto custom_buzzer_pin = LED_BUILTIN; // PC13 namespace obc { void init() { - pinMode(D6, OUTPUT); + Wire.setSDA(custom_sda); + Wire.setSCL(custom_scl); + + pinMode(custom_buzzer_pin, OUTPUT); sd_init().expect("SD init failure"); if (auto result = init(accelerometer); result.is_err()) { @@ -38,6 +46,7 @@ void init() } log_boot("Devices initialized properly."); + obc::buzzer(5); } } // namespace obc \ No newline at end of file diff --git a/src/lora.cpp b/src/lora.cpp index 3510479..aead319 100644 --- a/src/lora.cpp +++ b/src/lora.cpp @@ -71,7 +71,7 @@ void send_packet(const String &packet) { auto encoded_logs = encode(packet); String encoded = String("AT+MSG=") + "\"" + encoded_logs + "\""; - Serial5.print(encoded); + Serial5.println(encoded); } } // namespace obc \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 73aa548..3b1e921 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,20 +1,23 @@ #include +#include -#include "accelerometer.hpp" -#include "barometer.hpp" -#include "gps.hpp" -#include "logger.hpp" +#include "devices.hpp" +#include "utils.hpp" -HardwareSerial Serial2{PA3, PA2}; +constexpr auto baud_rate = 9600l; +constexpr auto watchdog_interval = 10000000; -Adafruit_GPS gps{&Serial2}; +auto current_mode = utl::Modes::Measurements; void setup() { - // put your setup code here, to run once: + Serial.begin(baud_rate); + IWatchdog.begin(watchdog_interval); + obc::init(); } void loop() { - // put your main code here, to run repeatedly: + utl::handle_mode(current_mode); + IWatchdog.reload(); } \ No newline at end of file diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..edc8785 --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,83 @@ +#include "utils.hpp" + +#include "accelerometer.hpp" +#include "barometer.hpp" +#include "buzzer.hpp" +#include "devices.hpp" +#include "gps.hpp" +#include "logger.hpp" +#include "lora.hpp" + +extern Adafruit_GPS gps; +extern MMA8452Q accelerometer; +extern BMP280 bmp; + +namespace utl { + +namespace { +constexpr auto logs_interval = 2000; +constexpr auto lora_interval = 60000; +constexpr std::size_t buzzer_ind_fix_not_fetched = 1; + +uint32_t timer = millis(); +uint32_t lora_timer = millis(); + +bool is_date_appended = false; + +} // namespace + +void handle_mode(Modes mode) +{ + switch (mode) { + case Modes::Idle: + return handle_idle(); + case Modes::Measurements: + return handle_measurements(); + case Modes::Experiment: + return handle_experiment(); + case Modes::Uplink: + return handle_uplink(); + default: + break; + } +} + +void handle_idle() {} + +void handle_measurements() +{ + if (obc::measure(gps).is_ok() and millis() - timer > logs_interval) { + obc::Packet logs = {{}, {}, {}, {}, {}}; + const auto acclr = obc::measure(accelerometer); + const auto bmp_measurements = obc::measure(bmp); + logs.time = obc::read_time(gps); + logs.position = obc::read_position(gps); + + if (acclr.is_ok()) { logs.acclr_measurements = acclr.unwrap(); } + if (bmp_measurements.is_ok()) { + logs.bmp_measurements = bmp_measurements.unwrap(); + } + + if (not is_date_appended) { + obc::log_boot(obc::serialize(obc::read_date(gps))); + is_date_appended = true; + } + + obc::log_data(obc::serialize(logs)); + + if (millis() - lora_timer > lora_interval) { + obc::send_packet(obc::lora_packet(logs)); + lora_timer = millis(); + } + + if (not logs.position.fix) { obc::buzzer(buzzer_ind_fix_not_fetched); } + + timer = millis(); + } +} + +void handle_experiment() {} + +void handle_uplink() {} + +} // namespace utl \ No newline at end of file