Possible command line uses

Hi !,

I am already in the testing phase of my light data logger and I am trying to use the cmd utility to modify a parameter of the light sensor in real time.
Specifically, it would try to modify the high and low thresholds, so that the interruption is triggered
Do you think it would be possible? With the "cmd" library, is it possible to modify in real time any variable found in the loop function?

Thanks !
PS: I'm using this library https://bitbucket.org/christandlg/tsl2591mi/src/master/




Hi JAL.

Sure, the command line library was designed for changing parameters dynamically so you can see the results. Here's some example code for how you would do it.

Here's a bit of explanation of the code below. In the BYOD course, we've tried to stick with using only the integer data type (int) as much as possible. We would start discussing using other data types in the next course. But in this case, you would need to match the data types to the functions you are trying to access. For example, the setALSInterruptThreshold() function is expecting the threshold argument as an unsigned 8-bit integer or uint8_t type.

The same is true for the 2nd argument of setALSInterruptThreshold() which is the value. It's expected in the form of an unsigned 16-bit integer or uint16_t. In case you're wondering, custom datatypes are usually followed by '_t'. So with that, here's how you could set the thresholds dynamically. You can interchange other functions as well or add as many as you want. 


void setup()
{
    cmd.begin(57600);

    cmd.add("setthreshold", cmdSetThreshold);
    cmd.add("getthreshold", cmdGetThreshold);

    // other setup functions like initializing the chip go here
}

void loop()
{
    cmd.poll();

    // other stuff go here
}

void cmdSetTheshold(int argc, char **args)
{
    uint8_t threshold = cmd.conv(args[1]);
    uint16_t val = cmd.conv(args[2]);

    setALSInterruptThreshold(threshold, val);
}

void cmdGetThreshold(int argc, char **args)
{
    uint8_t threshold = cmd.conv(args[1]);
    uint16_t val = getALSInterruptThreshold(threshold);

    Serial.print("Threshold for ");
    Serial.print(threshold);
    Serial.print("is ");
    Serial.println(val);
}

 

Hope this helps :)

Akiba

Just for reference, I pulled the functions from the source code of the link you provided here:
https://bitbucket.org/christandlg/tsl2591mi/src/master/src/TSL2591MI.h

 You can set and get any of these parameters or all of them. You will have to match the data types to what they are expecting though.

/*

 @param threshold one of the thresholds defined in threshold_t.

    @return threshold value in counts. returns 0 if threshold is invalid.

*/

    uint16_t getALSInterruptThreshold(uint8_t threshold);

 

    /*

    @param threshold one of the thresholds defined in threshold_t.

    @param value threshold value in counts.

    @return true on success, false otherwise. */

    bool setALSInterruptThreshold(uint8_t threshold, uint16_t value);

    

    /*

    @param threshold one of the thresholds defined in threshold_t.

    @return threshold value in counts. returns 0 if threshold is invalid. */

    double getALSInterruptThresholdIrradiance(uint8_t threshold);

 

    /*

    @param threshold one of the thresholds defined in threshold_t. fails if the threshold value is

    too small or too large for the current gain and integration time settings. must be called again

    with new values if gain or integration time has changed.

    @param value threshold value in counts.

    @return true on success, false otherwise. */

    bool setALSInterruptThresholdIrradiance(uint8_t threshold, double value);

 

    /*

    @param threshold one of the thresholds defined in threshold_t.

    @return threshold value in counts. returns 0 if threshold is invalid. */

    double getALSInterruptThresholdBrightness(uint8_t threshold);

 

    /*

    @param threshold one of the thresholds defined in threshold_t. fails if the threshold value is

    too small or too large for the current gain and integration time settings. must be called again

    with new values if gain or integration time has changed.

    @param value threshold value in counts.

    @return true on success, false otherwise. */

    bool setALSInterruptThresholdBrightness(uint8_t threshold, double value);

 

/*

@return ALS interrupt persistence filter value as persistence_t */

uint8_t getALSInterruptPersistence();

 

/*

@param ALS interrupt persistence filter value as persistence_t

@return true on success, false otherwise. */

bool setALSInterruptPersistence(uint8_t value);

Fantastic Akiba, the truth is that I did not expect so many explanations!, You make my work much easier
I made a mistake with the library that I indicated, because in reality the one that I am using is the TSL2591I2C, but I think that with the indications that you have given me I will be able to solve the problem
I understand that only those variables that are in the loop can be modified, right?
I also have to finish setting the code to avoid activating the SD in the event that a low battery is detected. I was thinking of doing it with a BOOL, what do you think?
Thank you very much for your valuable help!

My interest in disabling SD writing in case of low battery comes because I had problems with lowcost cards in a device that I realize. On a schedule programmed by a timer, data was recorded on a card, and it could happen that the cut occurred at the time of recording. In a short time I had several corrupt cards, but I never knew if the problem came from the poor quality of the cards, or that there was a power cut at the time of writing
I will consider your advice
Greetings,