Created
February 1, 2020 22:07
-
-
Save nikp123/258ecf7182640a06794a69b49c9d96d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Copyright (c) 2020 Nikola Pavlica | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
**/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#if !defined(__linux__) | |
#error "Commit suicide pls!" | |
#endif | |
#define false 0 | |
#define true 1 | |
#define POWER_DIR "/sys/class/power_supply/" | |
#define CURR_STAT "/current_now" | |
#define VOLT_STAT "/voltage_now" | |
int main(int argc, char *argv[]) { | |
// battery device | |
char *battery = "BAT0"; | |
_Bool showWatts = false; | |
unsigned int voltage, current; | |
// process options | |
for(int i = 1; i<argc; i++) { | |
if(!strcmp("--battery", argv[i])) { | |
battery = argv[i+1]; | |
i++; | |
} else if(!strcmp("--help", argv[i])) { | |
printf("Usage:\n" | |
"\t--help - shows this\n" | |
"\t--watts - shows the 'W' behind the value\n" | |
"\t--battery [BAT0] - specify battery to measure from\n"); | |
return 0; | |
} else if(!strcmp("--watts", argv[i])) { | |
showWatts = true; | |
} else { | |
fprintf(stderr, "Invalid parameter %s\n" | |
"For usage information try --help\n", | |
argv[i]); | |
return -1; | |
} | |
} | |
// obligatory C memory managment | |
const size_t strLen = sizeof(POWER_DIR) + sizeof(CURR_STAT) + sizeof(battery); | |
// read voltage | |
char *vS = (char*)malloc(strLen); | |
strcpy(vS, POWER_DIR); | |
strcat(vS, battery); | |
strcat(vS, VOLT_STAT); | |
FILE *vF = fopen(vS, "r"); | |
if(vF == NULL) { | |
fprintf(stderr, "Unable to read battery voltage!\n"); | |
return -1; | |
} | |
fscanf(vF, "%u", &voltage); | |
// read current | |
char *cS = (char*)malloc(strLen); | |
strcpy(cS, POWER_DIR); | |
strcat(cS, battery); | |
strcat(cS, CURR_STAT); | |
FILE *cF = fopen(cS, "r"); | |
if(cF == NULL) { | |
fprintf(stderr, "Unable to read battery current!\n"); | |
return -1; | |
} | |
fscanf(cF, "%u", ¤t); | |
float power = current/1000000.0 * voltage/1000000.0; | |
// show value | |
printf("%.2f", power); | |
if(showWatts) printf(" W"); | |
putchar('\n'); | |
// safely close everything | |
fclose(vF); | |
fclose(cF); | |
free(vS); | |
free(cS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment