add thermal module

This commit is contained in:
randomuser 2022-09-21 12:50:10 -05:00
parent 0c7873753d
commit e76bafa482
8 changed files with 98 additions and 29 deletions

View File

@ -3,36 +3,11 @@
#include <unistd.h>
#include "module.h"
#include "file.c"
#include "battery.h"
void populate_buffer(char *buffer, char pattern) {
/* XXX: we assume the buffer is at least four bytes long */
int i;
for(i = 0; i < 3; i++) *(buffer + i) = pattern;
*(buffer + i + 1) = '\0';
}
int battery_update(struct module *module) {
char *filepath = BATTERY_PRE BATTERY_DIR BATTERY_CAP;
int i;
if (!access(filepath, F_OK) == 0) {
populate_buffer((char *)&module->buffer, '?');
return 1;
}
FILE *file = fopen(filepath, "r");
i = fread(&module->buffer, 1, MODULE_BUFFER_LEN, file);
module->buffer[i - 1] = '\0';
if(ferror(file)) {
populate_buffer((char *)&module->buffer, '!');
return 1;
}
module->buffer[i] = '\0';
return 0;
return read_file_into_buffer(filepath, (char *)&module->buffer, MODULE_BUFFER_LEN);
}

View File

@ -12,7 +12,6 @@
#define BATTERY_PRE "/sys/class/power_supply/"
#define BATTERY_CAP "/capacity"
void populate_buffer(char *buffer, char pattern);
int battery_module(struct module *module);
#endif

37
file.c Normal file
View File

@ -0,0 +1,37 @@
/* see LICENSE file for details on license */
#ifndef TSTATUS_FILE_C
#include "file.h"
void populate_buffer(char *buffer, char pattern) {
/* XXX: we assume the buffer is at least four bytes long */
int i;
for(i = 0; i < 3; i++) *(buffer + i) = pattern;
*(buffer + i + 1) = '\0';
}
int read_file_into_buffer(char *filename, char *buffer, int length) {
int i;
if (!access(filename, F_OK) == 0) {
populate_buffer(buffer, '?');
return -1;
}
FILE *file = fopen(filename, "r");
i = fread(buffer, 1, length, file);
*(buffer + i - 1) = '\0';
if(ferror(file)) {
populate_buffer(buffer, '!');
return -1;
}
fclose(file);
*(buffer + i) = '\0';
return 0;
}
#endif
#define TSTATUS_FILE_C

8
file.h Normal file
View File

@ -0,0 +1,8 @@
/* see LICENSE file for details on license */
#ifndef TSTATUS_FILE_H
void populate_buffer(char *buffer, char pattern);
int read_file_into_buffer(char *filename, char *buffer, ssize_t length);
#endif
#define TSTATUS_FILE_H

View File

@ -1,6 +1,6 @@
/* see LICENSE file for details on license */
#ifndef TSTATUS_MODULE_H
#define MODULE_BUFFER_LEN 64
#define MODULE_BUFFER_LEN 128
struct module {
int update; /* update interval in seconds */
int termcode; /* update termcode */

27
thermal.c Normal file
View File

@ -0,0 +1,27 @@
/* see LICENSE file for details on license */
#include <stdio.h>
#include <unistd.h>
#include "module.h"
#include "file.c"
#include "thermal.h"
int convert_milli_to_reg(int millidegree) {
return millidegree / 1000;
}
int thermal_update(struct module *module) {
char *filepath = THERMAL_PRE THERMAL_DIR THERMAL_TMP;
int i;
i = read_file_into_buffer(filepath, (char *)&module->buffer, MODULE_BUFFER_LEN);
if(i == -1) return i; /* indicate an error */
i = atoi((char *)&module->buffer);
if(!i) return -1;
i = convert_milli_to_reg(i);
snprintf((char *)&module->buffer, MODULE_BUFFER_LEN, "%i", i);
return 0;
}

18
thermal.h Normal file
View File

@ -0,0 +1,18 @@
/* see LICENSE file for details on license */
#ifndef TSTATUS_THERMAL_H
#ifdef FISH
#define THERMAL_DIR "thermal_zone1"
/* you can add other computer hostnames here */
#else
#define THERMAL_DIR "thermal_zone1"
#endif
#define THERMAL_PRE "/sys/class/thermal/"
#define THERMAL_TMP "/temp"
int convert_milli_to_reg(int millidegree);
int thermal_update(struct module *module);
#endif
#define TSTATUS_THERMAL_H

View File

@ -4,10 +4,12 @@
#include "module.h"
#include "bspwm.c"
#include "battery.c"
#include "thermal.c"
struct module table[] = {
{0, 10, bspwm_update, {'\0'}},
{0, 10, battery_update, {'\0'}},
{0, 10, thermal_update, {'\0'}},
};
int main(void) {
@ -17,5 +19,8 @@ int main(void) {
table[1].updatecallback(&table[1]);
printf("%s\n", table[1].buffer);
table[2].updatecallback(&table[2]);
printf("%s\n", table[2].buffer);
return 0;
}