From 5c0d38f3b234e0fbb7f448322f41a771b00b43ac Mon Sep 17 00:00:00 2001 From: Filip Ayazi Date: Wed, 8 Mar 2023 01:22:22 +0000 Subject: [PATCH] Illumination module fixes, report as module --- src/main.cpp | 11 +- src/modules/illumination/illumination.cpp | 125 +++++++++++++++++----- src/modules/illumination/illumination.h | 1 + test/test_argument_parsing.cpp | 2 +- 4 files changed, 110 insertions(+), 29 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6524b19..95c1c53 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,16 +2,16 @@ * Sangaboard firmware * * Refactored from bath_open_instrumentation_group/sangaboard/arduino_code - * + * * This firmware was written by * Richard Bowman * Julian Stirling * Boyko Vodenicharski * Filip Ayazi - * + * * Much of the code is based on older code written by * James Sharkey and Fergus Riche - * + * * Released under GPL v3, 2021 */ @@ -137,6 +137,9 @@ void get_modules(String) comPort->print(" soft"); #endif comPort->println(); +#endif +#if defined(ILLUMINATION) + comPort->println("Illumination:default"); #endif comPort->println("--END--"); } @@ -173,7 +176,7 @@ void setup() { #if defined(SUPPORT_EEPROM) && defined(MCU_PICO) // On the Pico, EEPROM is emulated. - // This means we need to explicitly enable it, and also + // This means we need to explicitly enable it, and also // call EEPROM.commit() periodically to persist it to flash. EEPROM.begin(256); eeprom_last_commit = millis(); diff --git a/src/modules/illumination/illumination.cpp b/src/modules/illumination/illumination.cpp index 83014f8..e35c1bc 100644 --- a/src/modules/illumination/illumination.cpp +++ b/src/modules/illumination/illumination.cpp @@ -8,23 +8,53 @@ #include "dummyEEPROM.h" #endif +const uint16_t eeprom_offset = 100; + const uint8_t pwm_led_pins[] = WIRING_PWM_LEDS; +const uint16_t cc_value_eeprom = eeprom_offset; +const uint16_t pwm_frequency_eeprom = cc_value_eeprom + sizeof(uint8_t); +const uint16_t pwm_values_eeprom = pwm_frequency_eeprom + sizeof(uint16_t); + +uint32_t pwm_frequency; +uint8_t cc_value; + +uint16_t pwm_values[PWM_NUM]; + + void illumination_pwm_freq(String command) { char * args[1]; - parse_arguments(args, command, 1); + uint8_t parsed = parse_arguments(args, command, 1); #ifdef MCU_PICO - analogWriteFreq(atol(args[0]));//32khz seems sensible - analogWriteRange(65535); + if (parsed > 0 && !(args[0] == "" || args[0][0] == '?')) + { + pwm_frequency = atol(args[0]); + EEPROM.put(pwm_frequency_eeprom, pwm_frequency); + analogWriteFreq(pwm_frequency); + analogWriteRange(65535); + } - comPort->print("Frequency set: "); - comPort->print(atol(args[0])); + comPort->print("PWM Frequency:"); + comPort->print(pwm_frequency); comPort->println("Hz"); #else comPort->println("Feature not supported"); #endif - free(args[0]); + if (parsed > 0) + free(args[0]); +} + +void illumination_channels(String command) +{ + comPort->print("CC:"); + #ifdef WIRING_CC_LED + comPort->print(1); + #else + comPort->print(0); + #endif + comPort->print(" PWM:"); + comPort->println(PWM_NUM); } void illumination_pwm(String command) @@ -32,7 +62,26 @@ void illumination_pwm(String command) #ifdef MCU_PICO //PWM frequency is shared between both PWM outputs char * args[2]; - parse_arguments(args, command, 2); + uint8_t parsed = parse_arguments(args, command, 2); + if (parsed > 0 && args[0][0] == '?') + { + comPort->print("PWM:"); + for(int i=0; iprint(pwm_values[i]/65535.0); + #else + comPort->print(pwm_values[i]/255.0); + #endif + comPort->print(";"); + } + comPort->println(""); + free(args[0]); + if (parsed == 2) + free(args[1]); + + return; + } uint8_t index = atoi(args[0]); if (index >= PWM_NUM) { @@ -45,26 +94,28 @@ void illumination_pwm(String command) digitalWrite(pwm_led_pins[index], LOW); } #ifdef MCU_PICO - uint32_t pwm_val = val * 65535; + uint16_t pwm_val = val * 65535; #else - uint8_t pwm_val = val*255; + uint8_t pwm_val = val * 255; #endif analogWrite(pwm_led_pins[index], pwm_val); - comPort->print("Illumination set"); + pwm_values[index] = pwm_val; + EEPROM.put(pwm_values_eeprom+index*sizeof(uint16_t), pwm_val); + comPort->print("PWM:"); + comPort->print(index); + comPort->print(":"); comPort->println(pwm_val); - free(args[0]); - free(args[1]); + if (parsed > 0) + free(args[0]); + if (parsed > 1) + free(args[1]); #endif } void illumination_setup() { - #ifdef MCU_PICO - analogWriteRange(65535); - analogWriteFreq(32000);//32khz seems sensible - #endif for (uint8_t i = 0; i < PWM_NUM; i++) { pinMode(pwm_led_pins[i], OUTPUT); @@ -72,15 +123,31 @@ void illumination_setup() } #ifdef WIRING_CC_LED + EEPROM.get(cc_value_eeprom, cc_value); pinMode(WIRING_CC_LED, OUTPUT); + if (cc_value > 0) + cc_set_value(cc_value/32.0); #endif + #ifdef MCU_PICO + EEPROM.get(pwm_frequency_eeprom, pwm_frequency); + if (pwm_frequency < 1) + pwm_frequency = 64000; + analogWriteRange(65535); + analogWriteFreq(pwm_frequency);//32khz seems sensible + #endif + + for(int i = 0; i < PWM_NUM; i++) + { + EEPROM.get(pwm_values_eeprom+i*sizeof(uint16_t), pwm_values[i]); + analogWrite(pwm_led_pins[i], pwm_values[i]); + } + register_module(illumination_commands, NULL); } #ifdef WIRING_CC_LED - -//these functions are useful testing +//these functions are useful for testing float multi_read(uint8_t pin, uint16_t reads) { float total = 0; @@ -114,11 +181,17 @@ void cc_test() void cc_set(String command) { char * args[1]; - parse_arguments(args, command, 1); - float val = atof(args[0]); + uint8_t parsed = parse_arguments(args, command, 1); + if (parsed > 0 && !(args[0] == "" || args[0][0] == '?')) + { + float val = atof(args[0]); + cc_set_value(val); + } - free(args[0]); - cc_set_value(val); + comPort->print("CC LED:"); + comPort->println(cc_value/32.0); + if (parsed > 0) + free(args[0]); } void cc_set_value(float val) @@ -130,6 +203,9 @@ void cc_set_value(float val) return; } + cc_value = 32 * val; + EEPROM.put(cc_value_eeprom, cc_value); + const uint8_t td = 3; // >= 1.5 us const uint8_t tup = 2; // 1-75 us const uint8_t tdown = 180; //180-300us @@ -173,7 +249,8 @@ void cc_set_value(float val) extern const Command illumination_commands[] = { {"led_pwm", illumination_pwm}, - {"led_freq", illumination_pwm_freq}, - {"led_cc_set", cc_set}, + {"led_channels", illumination_channels}, + {"led_frequency", illumination_pwm_freq}, + {"led_cc", cc_set}, END_COMMAND}; #endif \ No newline at end of file diff --git a/src/modules/illumination/illumination.h b/src/modules/illumination/illumination.h index 2e4ae70..47044ce 100644 --- a/src/modules/illumination/illumination.h +++ b/src/modules/illumination/illumination.h @@ -7,6 +7,7 @@ void illumination_pwm(String command); void illumination_pwm_freq(String command); +void illumination_channels(String command); void cc_set(String command); void cc_set_value(float val); diff --git a/test/test_argument_parsing.cpp b/test/test_argument_parsing.cpp index e923cf5..adee2ba 100644 --- a/test/test_argument_parsing.cpp +++ b/test/test_argument_parsing.cpp @@ -74,7 +74,7 @@ void setup() { Serial.begin(115200); // NOTE!!! Wait for >2 secs // if board doesn't support software reset via comPort->DTR/RTS - delay(2000); + delay(5000); UNITY_BEGIN(); // IMPORTANT LINE! //bizzare string issues caused this to fail in specific sequences, so we run it multiple times RUN_TEST(test_multiple_argument_parsing);