feat(MP/daemon): add logic file and sysfs
This commit is contained in:
1
src/06-mini-project/daemon/logic/app.c
Normal file
1
src/06-mini-project/daemon/logic/app.c
Normal file
@@ -0,0 +1 @@
|
||||
#include "app.h"
|
||||
25
src/06-mini-project/daemon/logic/app.h
Normal file
25
src/06-mini-project/daemon/logic/app.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef APP_H
|
||||
#define APP_H
|
||||
|
||||
#include "../gpio/led.h"
|
||||
|
||||
#define DEFAULT_PERIOD_MS 1000
|
||||
|
||||
void set_led(
|
||||
|
||||
void increase_period();
|
||||
void decrease_period();
|
||||
void set_period(int period_ms);
|
||||
|
||||
void mode_toggle();
|
||||
void set_mode(int mode);
|
||||
|
||||
float get_temperature();
|
||||
|
||||
|
||||
/* Button specific function */
|
||||
|
||||
void btn_increase_period();
|
||||
void btn_decrease_period();
|
||||
|
||||
#endif /* APP_H */
|
||||
66
src/06-mini-project/daemon/logic/sysfs.c
Normal file
66
src/06-mini-project/daemon/logic/sysfs.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "sysfs.h"
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
|
||||
float get_temperature() {
|
||||
FILE *f = fopen(PATH_TEMPERATURE, "r");
|
||||
if (f == NULL) {
|
||||
return 0.0f; /* Return 0.0 if the kernel module is not loaded */
|
||||
}
|
||||
|
||||
float temp = 0.0f;
|
||||
|
||||
/*
|
||||
* The kernel formats the temperature as "XX.YYY" (e.g., "41.730").
|
||||
* Since we are in user space, we can simply read it as a float.
|
||||
*/
|
||||
if (fscanf(f, "%f", &temp) == 1) {
|
||||
fclose(f);
|
||||
return temp;
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
uint32_t get_mode() {
|
||||
FILE *f = fopen(PATH_MODE, "r");
|
||||
if (f == NULL) return 0;
|
||||
|
||||
uint32_t mode = 0;
|
||||
fscanf(f, "%u", &mode);
|
||||
fclose(f);
|
||||
|
||||
return mode;
|
||||
}
|
||||
|
||||
void set_mode(uint32_t mode) {
|
||||
FILE *f = fopen(PATH_MODE, "w");
|
||||
if (f == NULL) return;
|
||||
|
||||
/* Write the integer as an ASCII string */
|
||||
fprintf(f, "%u\n", mode);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
uint32_t get_period() {
|
||||
FILE *f = fopen(PATH_PERIOD_ST, "r");
|
||||
if (f == NULL) return 0;
|
||||
|
||||
uint32_t period = 0;
|
||||
fscanf(f, "%u", &period);
|
||||
fclose(f);
|
||||
|
||||
return period;
|
||||
}
|
||||
|
||||
void set_period(uint32_t period) {
|
||||
FILE *f = fopen(PATH_PERIOD_SET, "w");
|
||||
if (f == NULL) return;
|
||||
|
||||
fprintf(f, "%u\n", period);
|
||||
fclose(f);
|
||||
}
|
||||
20
src/06-mini-project/daemon/logic/sysfs.h
Normal file
20
src/06-mini-project/daemon/logic/sysfs.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef SYSFS_H
|
||||
#define SYSFS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* Define the absolute paths to the sysfs files created by the kernel */
|
||||
#define SYSFS_BASE_PATH "/sys/class/temp_regulator/regulator"
|
||||
#define PATH_TEMPERATURE SYSFS_BASE_PATH "/temperature"
|
||||
#define PATH_MODE SYSFS_BASE_PATH "/mode"
|
||||
#define PATH_PERIOD_ST SYSFS_BASE_PATH "/period_status"
|
||||
#define PATH_PERIOD_SET SYSFS_BASE_PATH "/period_set"
|
||||
|
||||
float get_temperature();
|
||||
uint32_t get_mode();
|
||||
void set_mode(uint32_t mode);
|
||||
uint32_t get_period();
|
||||
void set_period(uint32_t period);
|
||||
|
||||
|
||||
#endif /* SYSFS_H */
|
||||
Reference in New Issue
Block a user