From f14df2bc9d34df70401911116dadca1f3e22a6ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= <17674105+martinjaeger@users.noreply.github.com> Date: Fri, 25 Oct 2019 11:36:01 +0200 Subject: [PATCH] Global print functions based on debug level flags added (closes #58) --- platformio.ini | 6 ++++-- src/dcdc.cpp | 15 +++++++-------- src/debug.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 src/debug.h diff --git a/platformio.ini b/platformio.ini index 51d2813..e7c7a42 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,9 +13,9 @@ default_envs = # mppt-1210-hus-v0.2 # mppt-1210-hus-v0.4 # mppt-1210-hus-v0.6 - mppt-1210-hus-v0.7 +# mppt-1210-hus-v0.7 # pwm-2420-lus-v0.2 - pwm-2420-lus-v0.3 +# pwm-2420-lus-v0.3 # unit-test-native [env] @@ -43,6 +43,7 @@ build_flags = -fsingle-precision-constant -Wl,-Map,memory.map -D TURN_OFF_MBED_DEPRECATED_WARNING + -D DEBUG_PRINT_FLAGS=0x7 -D MBED_BUILD_PROFILE_RELEASE # -D MBED_BUILD_PROFILE_DEBUG @@ -109,6 +110,7 @@ build_flags = -D LITTLE_ENDIAN -D UNIT_TEST -D __STDC_FORMAT_MACROS + -D DEBUG_PRINT_FLAGS=0xF # include src directory (otherwise unit-tests will only include lib directory) test_build_project_src = true lib_ignore = USB, mbed-USBDevice, mbed-mbedtls, USBSerial, ESP32, Adafruit_GFX, SX1276GenericLib diff --git a/src/dcdc.cpp b/src/dcdc.cpp index 957b68a..092425f 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -17,6 +17,7 @@ #include "dcdc.h" #include "pcb.h" #include "log.h" +#include "debug.h" #include "half_bridge.h" @@ -80,15 +81,13 @@ int Dcdc::duty_cycle_delta() int pwr_inc_goal = 0; // stores if we want to increase (+1) or decrease (-1) power -#if 0 - printf("P: %.2f, P_prev: %.2f, v_in: %.2f, v_out: %.2f, i_in: %.2f, i_out: %.2f, " + print_test("P: %.2f, P_prev: %.2f, v_in: %.2f, v_out: %.2f, i_in: %.2f, i_out: %.2f, " "v_chg_target: %.2f, i_chg_max: %.2f, PWM: %.1f, chg_state: %d\n", out->power, power_prev, in->voltage, out->voltage, in->current, out->current, out->sink_voltage_max, out->pos_current_limit, half_bridge_get_duty_cycle() * 100.0, state); -#endif - if (time(NULL) - power_good_timestamp > 10 || // more than 10s low power + if (time(NULL) - power_good_timestamp > 10 || // more than 10s low power out->power < -1.0) // or already generating negative power { pwr_inc_goal = 0; // switch off @@ -188,7 +187,7 @@ void Dcdc::control() half_bridge_stop(); state = DCDC_STATE_OFF; off_timestamp = time(NULL); - printf("DC/DC Stop: %s.\n",stop_reason); + print_info("DC/DC Stop: %s.\n",stop_reason); } } else { // half bridge is off @@ -234,7 +233,7 @@ void Dcdc::control() half_bridge_start(lvs->voltage / (hvs->voltage + 1)); } power_good_timestamp = time(NULL); - printf("DC/DC %s mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", mode_name, + print_info("DC/DC %s mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", mode_name, hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100); startup_delay_counter = 0; // ensure we will have a delay before next start } @@ -260,7 +259,7 @@ void Dcdc::test() if (startup_delay_counter > num_wait_calls) { if (check_start_conditions() != 0) { half_bridge_start(lvs->voltage / hvs->voltage); - printf("DC/DC test mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", + print_info("DC/DC test mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100); } } @@ -272,7 +271,7 @@ void Dcdc::test() void Dcdc::self_destruction() { - printf("Charge controller self-destruction called!\n"); + print_error("Charge controller self-destruction called!\n"); //half_bridge_stop(); //half_bridge_init(50, 0, 0, 0.98); // reset safety limits to allow 0% duty cycle //half_bridge_start(0); diff --git a/src/debug.h b/src/debug.h new file mode 100644 index 0000000..43c9748 --- /dev/null +++ b/src/debug.h @@ -0,0 +1,47 @@ +/* LibreSolar charge controller firmware + * Copyright (c) 2016-2019 Martin Jäger (www.libre.solar) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DEBUG_H +#define DEBUG_H + +#include + +// Debug printing macros +// see https://stackoverflow.com/questions/1644868/define-macro-for-debug-printing-in-c + +// Set the bits in DEBUG_PRINT_FLAGS via platformio.ini to enable specific debug print levels + +// Bit 0: Serious errors +#define print_error(fmt, ...) \ + do { if (DEBUG_PRINT_FLAGS & (1 << 0)) printf(fmt, ##__VA_ARGS__); } while (0) + +// Bit 1: Warnings +#define print_warning(fmt, ...) \ + do { if (DEBUG_PRINT_FLAGS & (1 << 1)) printf(fmt, ##__VA_ARGS__); } while (0) + +// Bit 2: Information only +#define print_info(fmt, ...) \ + do { if (DEBUG_PRINT_FLAGS & (1 << 2)) printf(fmt, ##__VA_ARGS__); } while (0) + +// Bit 3: Unit test information +#define print_test(fmt, ...) \ + do { if (DEBUG_PRINT_FLAGS & (1 << 3)) printf(fmt, ##__VA_ARGS__); } while (0) + +// Bit 4: Communication module special flag +#define print_comms(fmt, ...) \ + do { if (DEBUG_PRINT_FLAGS & (1 << 4)) printf(fmt, ##__VA_ARGS__); } while (0) + +#endif /* DEBUG_H */