diff --git a/RotaxMonitorTester/platformio.ini b/RotaxMonitorTester/platformio.ini index 2653613..51de170 100644 --- a/RotaxMonitorTester/platformio.ini +++ b/RotaxMonitorTester/platformio.ini @@ -22,7 +22,7 @@ build_type = release [env:esp32-devtest-debug] board = esp32dev platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip - +framework = arduino lib_deps = hideakitai/DebugLog@^0.8.4 board_build.flash_size = 4MB diff --git a/RotaxMonitorTester/src/colors.h b/RotaxMonitorTester/src/colors.h new file mode 100644 index 0000000..2e313ca --- /dev/null +++ b/RotaxMonitorTester/src/colors.h @@ -0,0 +1,12 @@ +#pragma once + +// ANSI colors +#define COLOR_RESET "\033[0m" +#define COLOR_RED "\033[31m" +#define COLOR_GREEN "\033[32m" +#define COLOR_BLUE "\033[34m" +#define COLOR_MAGENTA "\033[35m" +#define COLOR_CYAN "\033[36m" +#define COLOR_YELLOW "\033[33m" +#define COLOR_WHITE "\033[37m" +#define COLOR_LBLUE "\033[94m" diff --git a/RotaxMonitorTester/src/main.cpp b/RotaxMonitorTester/src/main.cpp index e2fafd5..defe9a1 100644 --- a/RotaxMonitorTester/src/main.cpp +++ b/RotaxMonitorTester/src/main.cpp @@ -4,6 +4,8 @@ #include #include "timer.h" +#include "colors.h" + #include static hw_timer_t *timerA = NULL; @@ -17,6 +19,12 @@ static uint32_t count = 0; #define SPARK_DLY_MIN 10 #define SPARK_DLY_MAX 490 +#define COIL_PULSE_MIN 100 +#define COIL_PULSE_MAX 1000 + +#define SPARK_PULSE_MIN 10 +#define SPARK_PULSE_MAX 500 + #define PAUSE_LONG_MIN 5000 #define PAUSE_LONG_MAX PAUSE_LONG_MIN * 100 @@ -30,7 +38,8 @@ void clearScreen() Serial.flush(); } -static double filtered_rpm = 0; +static uint32_t set_rpm = 500; +static uint32_t set_delay = 100; static const std::map pin2Name = { {PIN_TRIG_A12P, "HIGH_PIN_TRIG_A12P"}, @@ -68,7 +77,7 @@ static timerStatus stsB = { .clock_period_us = (uint32_t)PERIOD_US, .pause_long_us = 10000, .pause_short_us = 1000, - .coil_pulse_us = 1000, + .coil_pulse_us = 500, .spark_pulse_us = 100, .spark_delay_us = 50, .pins = { @@ -83,11 +92,14 @@ static timerStatus stsB = { static bool isEnabled_A = false; static bool isEnabled_B = false; +static String last_command; + void setup() { Serial.begin(115200); delay(1000); + Serial.setTimeout(100); LOG_ATTACH_SERIAL(Serial); pinMode(PIN_TRIG_A12P, OUTPUT); @@ -133,63 +145,124 @@ void setup() void loop() { - LOG_INFO("Loop: ", count++); - uint32_t spark_delay = (uint32_t)(map(analogRead(SPARK_DELAY_POT), 0, 4096, SPARK_DLY_MIN, SPARK_DLY_MAX) / PERIOD_US); - stsA.spark_delay_us = spark_delay * PERIOD_US; - if (stsA.spark_delay_us > (SPARK_DLY_MIN + SPARK_DLY_MAX) / 2) - { - stsA.soft_start = true; - stsA.spark_delay_us -= (SPARK_DLY_MIN + SPARK_DLY_MAX) / 2; - } - else - { - stsA.soft_start = false; - } - stsB.soft_start = stsA.soft_start; - stsB.spark_delay_us = stsA.spark_delay_us; + clearScreen(); - double new_rpm = (double)(map(analogRead(FREQ_POT), 0, 4096, RPM_MIN, RPM_MAX)); - filtered_rpm = filtered_rpm + 0.1 * (new_rpm - filtered_rpm); - stsA.pause_long_us = (uint32_t)(60000000.0f / filtered_rpm / 2.0f); - stsB.pause_long_us = stsA.pause_long_us; + Serial.printf("\t++++ Loop: %u ++++\n", count++); if (isEnabled_A) - LOG_INFO("==== System A is ENABLED ===="); + Serial.println("==== System A is" COLOR_GREEN " ENABLED" COLOR_RESET " ===="); else - LOG_INFO("==== System A is DISABLED ===="); + Serial.println("==== System A is" COLOR_RED " DISABLED" COLOR_RESET " ===="); if (isEnabled_B) - LOG_INFO("==== System B is ENABLED ===="); + Serial.println("==== System B is" COLOR_GREEN " ENABLED" COLOR_RESET " ===="); else - LOG_INFO("==== System B is DISABLED ===="); + Serial.println("==== System B is" COLOR_RED " DISABLED" COLOR_RESET " ===="); - LOG_INFO("Spark Delay uS: ", stsA.spark_delay_us, "\tSoft Start: ", stsA.soft_start ? "TRUE" : "FALSE"); - LOG_INFO("Engine Rpm: ", (uint32_t)(filtered_rpm)); - LOG_INFO("Coil Pulse: ", stsA.coil_pulse_us, "us"); - LOG_INFO("Spark Pulse: ", stsA.spark_pulse_us, "us"); + Serial.printf("Spark Delay uS: %u\n", stsA.spark_delay_us); + Serial.printf("Soft Start: %s\n", stsA.soft_start ? "ENABLED" : "DISABLED"); + Serial.printf("Engine Rpm: %u\n", (uint32_t)(set_rpm)); + Serial.printf("Coil Pulse: %u uS\n", stsA.coil_pulse_us); + Serial.printf("Spark Pulse: %u uS\n", stsA.spark_pulse_us); + Serial.println(COLOR_CYAN "-------------------------------------"); + Serial.println("E[a/b] > Enable Box a/b | D[a/b] > Disable a/b"); + Serial.println("S[ddd] > Spark Delay | R[dddd] > Engine RPM"); + Serial.println("C[ddd] > Spark Pulse | P[ddd] > Coil Pulse"); + Serial.println("-------------------------------------" COLOR_RESET); + Serial.printf("Last Command: %s\n", last_command.c_str()); - if (digitalRead(ENABLE_PIN_A) == LOW && !isEnabled_A) + auto str = Serial.readStringUntil('\n'); + if (!str.isEmpty()) { - timerStart(timerA); - isEnabled_A = true; - } - else if (digitalRead(ENABLE_PIN_A) == HIGH && isEnabled_A) - { - timerStop(timerA); - isEnabled_A = false; + last_command = str; + const auto cmd = str.charAt(0); + char c; + switch (cmd) + { + case 'E': + { + char box; + sscanf(str.c_str(), "%c%c\n", &c, &box); + if (box == 'a' && !isEnabled_A) + { + timerStart(timerA); + isEnabled_A = true; + } + else if (box == 'b' && !isEnabled_B) + { + timerStart(timerB); + isEnabled_B = true; + } + break; + } + case 'D': + { + char c; + char box; + sscanf(str.c_str(), "%c%c\n", &c, &box); + if (box == 'a' && isEnabled_A) + { + timerStop(timerA); + isEnabled_A = false; + } + else if (box == 'b' && isEnabled_B) + { + timerStop(timerB); + isEnabled_B = false; + } + break; + } + case 'R': + { + int new_rpm; + sscanf(str.c_str(), "%c%d\n", &c, &new_rpm); + new_rpm = min(RPM_MAX, max(RPM_MIN, new_rpm)); + stsA.pause_long_us = (uint32_t)(60000000.0f / (float)new_rpm / 2.0f); + stsB.pause_long_us = stsA.pause_long_us; + set_rpm = (uint32_t)new_rpm; + break; + } + case 'S': + { + int new_delay; + sscanf(str.c_str(), "%c%d\n", &c, &new_delay); + new_delay = min(SPARK_DLY_MAX, max(SPARK_DLY_MIN, new_delay)); + stsA.spark_delay_us = (uint32_t)(new_delay); + if (stsA.spark_delay_us > (SPARK_DLY_MIN + SPARK_DLY_MAX) / 2) + { + stsA.soft_start = true; + stsA.spark_delay_us -= (SPARK_DLY_MIN + SPARK_DLY_MAX) / 2; + } + else + { + stsA.soft_start = false; + } + stsB.soft_start = stsA.soft_start; + stsB.spark_delay_us = stsA.spark_delay_us; + break; + } + case 'P': + { + int new_pulse; + sscanf(str.c_str(), "%c%d\n", &c, &new_pulse); + new_pulse = min(COIL_PULSE_MAX, max(COIL_PULSE_MIN, new_pulse)); + stsA.coil_pulse_us = stsB.coil_pulse_us = (uint32_t)new_pulse; + break; + } + case 'C': + { + int new_pulse; + sscanf(str.c_str(), "%c%d\n", &c, &new_pulse); + new_pulse = min(SPARK_PULSE_MAX, max(SPARK_PULSE_MIN, new_pulse)); + stsA.spark_pulse_us = stsB.spark_pulse_us = (uint32_t)new_pulse; + break; + } + default: + break; + } + Serial.read(); } - if (digitalRead(ENABLE_PIN_B) == LOW && !isEnabled_B) - { - timerStart(timerB); - isEnabled_B = true; - } - else if (digitalRead(ENABLE_PIN_B) == HIGH && isEnabled_B) - { - timerStop(timerB); - isEnabled_B = false; - } - - delay(100); - clearScreen(); + str.clear(); + delay(1000); }