Skip to content

Comments

Moving battery calculation for lipo discharge into utility.c#227

Merged
Sapd merged 3 commits intoSapd:masterfrom
jaypikay:master
Aug 17, 2022
Merged

Moving battery calculation for lipo discharge into utility.c#227
Sapd merged 3 commits intoSapd:masterfrom
jaypikay:master

Conversation

@jaypikay
Copy link
Contributor

I have moved the calculation of the battery level over to utility.c and updated the code for the Logitech Pro X to use it. I did not touch the logitech_g633_g933_935.c as I cannot verify the correctness, when porting it.

@Sapd
Copy link
Owner

Sapd commented Aug 17, 2022

Thank you, its a good PR for cleaning up.

However we should also provide a function for simple splines. As polynomials are a bit of overkill (and more complicated) for most cases, i.e.:

static float estimate_battery_level(uint16_t voltage) {
    int p[] = { 100, 60, 10, 0 };
    int v[] = { 4200, 3870, 3690, 3270 };
    int size = 4;
    
    float percent = 0;
    for(int i = 0; i < size; ++i) {
        // if >= then 100%
        if(voltage >= v[i]) {
            percent = p[i];
            break;
        } 
        
        // if not last
        if(i < size-1 && voltage >= v[i+1]) {
            percent = p[i+1] + (voltage-v[i+1])/(v[i]-v[i+1])*(p[i]-p[i+1]);
            break;
        }
       return percent;
}

We maybe should also start writing tests for utility functions.

@Sapd Sapd merged commit c7fd2ce into Sapd:master Aug 17, 2022
rpbaptist pushed a commit to rpbaptist/HeadsetControl that referenced this pull request May 31, 2024
* Move calculation of battery level for discharging battery to utility function

* Refactored battery calculation to use the 'utility function'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants