Illumination module fixes, report as module

This commit is contained in:
Filip Ayazi 2023-03-08 01:22:22 +00:00
parent 225e208eae
commit 5c0d38f3b2
4 changed files with 110 additions and 29 deletions

View file

@ -2,16 +2,16 @@
* Sangaboard firmware * Sangaboard firmware
* *
* Refactored from bath_open_instrumentation_group/sangaboard/arduino_code * Refactored from bath_open_instrumentation_group/sangaboard/arduino_code
* *
* This firmware was written by * This firmware was written by
* Richard Bowman * Richard Bowman
* Julian Stirling * Julian Stirling
* Boyko Vodenicharski * Boyko Vodenicharski
* Filip Ayazi * Filip Ayazi
* *
* Much of the code is based on older code written by * Much of the code is based on older code written by
* James Sharkey and Fergus Riche * James Sharkey and Fergus Riche
* *
* Released under GPL v3, 2021 * Released under GPL v3, 2021
*/ */
@ -137,6 +137,9 @@ void get_modules(String)
comPort->print(" soft"); comPort->print(" soft");
#endif #endif
comPort->println(); comPort->println();
#endif
#if defined(ILLUMINATION)
comPort->println("Illumination:default");
#endif #endif
comPort->println("--END--"); comPort->println("--END--");
} }
@ -173,7 +176,7 @@ void setup()
{ {
#if defined(SUPPORT_EEPROM) && defined(MCU_PICO) #if defined(SUPPORT_EEPROM) && defined(MCU_PICO)
// On the Pico, EEPROM is emulated. // 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. // call EEPROM.commit() periodically to persist it to flash.
EEPROM.begin(256); EEPROM.begin(256);
eeprom_last_commit = millis(); eeprom_last_commit = millis();

View file

@ -8,23 +8,53 @@
#include "dummyEEPROM.h" #include "dummyEEPROM.h"
#endif #endif
const uint16_t eeprom_offset = 100;
const uint8_t pwm_led_pins[] = WIRING_PWM_LEDS; 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) void illumination_pwm_freq(String command)
{ {
char * args[1]; char * args[1];
parse_arguments(args, command, 1); uint8_t parsed = parse_arguments(args, command, 1);
#ifdef MCU_PICO #ifdef MCU_PICO
analogWriteFreq(atol(args[0]));//32khz seems sensible if (parsed > 0 && !(args[0] == "" || args[0][0] == '?'))
analogWriteRange(65535); {
pwm_frequency = atol(args[0]);
EEPROM.put(pwm_frequency_eeprom, pwm_frequency);
analogWriteFreq(pwm_frequency);
analogWriteRange(65535);
}
comPort->print("Frequency set: "); comPort->print("PWM Frequency:");
comPort->print(atol(args[0])); comPort->print(pwm_frequency);
comPort->println("Hz"); comPort->println("Hz");
#else #else
comPort->println("Feature not supported"); comPort->println("Feature not supported");
#endif #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) void illumination_pwm(String command)
@ -32,7 +62,26 @@ void illumination_pwm(String command)
#ifdef MCU_PICO #ifdef MCU_PICO
//PWM frequency is shared between both PWM outputs //PWM frequency is shared between both PWM outputs
char * args[2]; 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; i<PWM_NUM; i++)
{
#ifdef MCU_PICO
comPort->print(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]); uint8_t index = atoi(args[0]);
if (index >= PWM_NUM) if (index >= PWM_NUM)
{ {
@ -45,26 +94,28 @@ void illumination_pwm(String command)
digitalWrite(pwm_led_pins[index], LOW); digitalWrite(pwm_led_pins[index], LOW);
} }
#ifdef MCU_PICO #ifdef MCU_PICO
uint32_t pwm_val = val * 65535; uint16_t pwm_val = val * 65535;
#else #else
uint8_t pwm_val = val*255; uint8_t pwm_val = val * 255;
#endif #endif
analogWrite(pwm_led_pins[index], pwm_val); 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); comPort->println(pwm_val);
free(args[0]); if (parsed > 0)
free(args[1]); free(args[0]);
if (parsed > 1)
free(args[1]);
#endif #endif
} }
void illumination_setup() void illumination_setup()
{ {
#ifdef MCU_PICO
analogWriteRange(65535);
analogWriteFreq(32000);//32khz seems sensible
#endif
for (uint8_t i = 0; i < PWM_NUM; i++) for (uint8_t i = 0; i < PWM_NUM; i++)
{ {
pinMode(pwm_led_pins[i], OUTPUT); pinMode(pwm_led_pins[i], OUTPUT);
@ -72,15 +123,31 @@ void illumination_setup()
} }
#ifdef WIRING_CC_LED #ifdef WIRING_CC_LED
EEPROM.get(cc_value_eeprom, cc_value);
pinMode(WIRING_CC_LED, OUTPUT); pinMode(WIRING_CC_LED, OUTPUT);
if (cc_value > 0)
cc_set_value(cc_value/32.0);
#endif #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); register_module(illumination_commands, NULL);
} }
#ifdef WIRING_CC_LED #ifdef WIRING_CC_LED
//these functions are useful for testing
//these functions are useful testing
float multi_read(uint8_t pin, uint16_t reads) float multi_read(uint8_t pin, uint16_t reads)
{ {
float total = 0; float total = 0;
@ -114,11 +181,17 @@ void cc_test()
void cc_set(String command) void cc_set(String command)
{ {
char * args[1]; char * args[1];
parse_arguments(args, command, 1); uint8_t parsed = parse_arguments(args, command, 1);
float val = atof(args[0]); if (parsed > 0 && !(args[0] == "" || args[0][0] == '?'))
{
float val = atof(args[0]);
cc_set_value(val);
}
free(args[0]); comPort->print("CC LED:");
cc_set_value(val); comPort->println(cc_value/32.0);
if (parsed > 0)
free(args[0]);
} }
void cc_set_value(float val) void cc_set_value(float val)
@ -130,6 +203,9 @@ void cc_set_value(float val)
return; return;
} }
cc_value = 32 * val;
EEPROM.put(cc_value_eeprom, cc_value);
const uint8_t td = 3; // >= 1.5 us const uint8_t td = 3; // >= 1.5 us
const uint8_t tup = 2; // 1-75 us const uint8_t tup = 2; // 1-75 us
const uint8_t tdown = 180; //180-300us const uint8_t tdown = 180; //180-300us
@ -173,7 +249,8 @@ void cc_set_value(float val)
extern const Command illumination_commands[] = { extern const Command illumination_commands[] = {
{"led_pwm", illumination_pwm}, {"led_pwm", illumination_pwm},
{"led_freq", illumination_pwm_freq}, {"led_channels", illumination_channels},
{"led_cc_set", cc_set}, {"led_frequency", illumination_pwm_freq},
{"led_cc", cc_set},
END_COMMAND}; END_COMMAND};
#endif #endif

View file

@ -7,6 +7,7 @@
void illumination_pwm(String command); void illumination_pwm(String command);
void illumination_pwm_freq(String command); void illumination_pwm_freq(String command);
void illumination_channels(String command);
void cc_set(String command); void cc_set(String command);
void cc_set_value(float val); void cc_set_value(float val);

View file

@ -74,7 +74,7 @@ void setup() {
Serial.begin(115200); Serial.begin(115200);
// NOTE!!! Wait for >2 secs // NOTE!!! Wait for >2 secs
// if board doesn't support software reset via comPort->DTR/RTS // if board doesn't support software reset via comPort->DTR/RTS
delay(2000); delay(5000);
UNITY_BEGIN(); // IMPORTANT LINE! UNITY_BEGIN(); // IMPORTANT LINE!
//bizzare string issues caused this to fail in specific sequences, so we run it multiple times //bizzare string issues caused this to fail in specific sequences, so we run it multiple times
RUN_TEST(test_multiple_argument_parsing); RUN_TEST(test_multiple_argument_parsing);