commit d111705216ed3edd035a6a31063aee40e87060a4
parent 17041b973906f56465d5a990b5c1cf21252c55f1
Author: Tonton <tonterface@proton.me>
Date: Fri, 3 Jul 2026 15:21:18 +0200
refactoring
Diffstat:
4 files changed, 114 insertions(+), 27 deletions(-)
diff --git a/gas_sensor.ino b/gas_sensor.ino
@@ -1,35 +1,34 @@
/*
* Gas sensor
*/
+#include <Arduino.h>
-#include "include/menu.h"
#include "include/display.h"
+#include "include/menu.h"
#include "include/sensor.h"
/* function declarations */
-void setup(void);
-void loop(void);
-
-/* objects */
+extern void setup(void);
+extern void loop(void);
/* variables */
/* function implementations */
-void setup(void)
+extern void setup(void)
{
Serial.begin(9600);
while (!Serial) {;}
- delay(500);
+ delay(3000);
pinMode(BUILTIN_LED, OUTPUT);
dsp_init();
+ Serial.print("Screen init done\n\r");
sensor_init();
+ menu_main();
}
-void loop(void)
+extern void loop(void)
{
- menu_main();
- delay(100);
}
diff --git a/src/display.cpp b/src/display.cpp
@@ -1,11 +1,12 @@
-/* display utilities */
+/* Display Utilities
+ * Handles all interactions with the external display.
+ */
#include <Adafruit_SSD1306.h>
#include <Arduino.h>
#include <Wire.h>
#include "../include/display.h"
-#include "../include/sensor.h"
/* macros */
@@ -73,9 +74,6 @@ void dsp_init(void)
Serial.println("Allocation failed");
for(;;);
}
- display.display();
- delay(2000);
- Serial.println("Screen init done");
delay(2000);
display.clearDisplay();
diff --git a/src/menu.cpp b/src/menu.cpp
@@ -9,8 +9,8 @@
#include "../include/sensor.h"
/* function declarations */
-static int get_user_input(void);
-static int goto_main();
+static int get_input(void);
+static int goto_main(void);
static void print_main(void);
static void select_menu_item(int input);
static void menu_item1(void);
@@ -19,7 +19,7 @@ static void menu_item3(void);
static void print_live(void);
/* function implementations */
-static int get_user_input(void)
+static int get_input(void)
{
/* implement handling of physical buttons */
int input;
@@ -33,7 +33,7 @@ static int get_user_input(void)
return input;
}
-static int goto_main()
+static int goto_main(void)
{
char input;
@@ -46,7 +46,7 @@ static int goto_main()
menu_main();
}
-static void print_main()
+static void print_main(void)
{
dsp_print_main();
Serial.print("\e[1;1H\e[2J");
@@ -82,7 +82,7 @@ static void menu_item1(void)
Serial.print("\e[1;1H\e[2J");
dsp_print_menu1();
Serial.print("Wait for analysis\n\r");
- res = sensor_read(0);
+ res = sensor_analyze();
dsp_print_result(res);
goto_main();
}
@@ -128,7 +128,7 @@ void menu_main(void)
{
int input;
print_main();
- input = get_user_input();
+ input = get_input();
select_menu_item(input);
Serial.println("You're not supposed to be here!");
}
diff --git a/src/sensor.cpp b/src/sensor.cpp
@@ -4,7 +4,11 @@
#include "../include/sensor.h"
-/* enums */
+/* macros */
+#define SAMPLE_NUMBER 10
+#define SENSOR_NUMBER 1
+#define INTERVAL_NUMBER 3
+
enum {
SENSOR_1 = 26,
SENSOR_2 = 25,
@@ -12,15 +16,85 @@ enum {
SENSOR_4 = 39
};
+struct interval {
+ int lower_limit;
+ int upper_limit;
+};
+
/* variables */
-const int sens_arr[] = {
+struct interval sensor1_intervals[INTERVAL_NUMBER];
+struct interval sensor2_intervals[INTERVAL_NUMBER];
+struct interval sensor3_intervals[INTERVAL_NUMBER];
+struct interval sensor4_intervals[INTERVAL_NUMBER];
+
+static struct interval* all_intervals[] = {
+ sensor1_intervals,
+ sensor2_intervals,
+ sensor3_intervals,
+ sensor4_intervals
+};
+
+static int sens_arr[] = {
SENSOR_1,
SENSOR_2,
SENSOR_3,
SENSOR_4
};
-/* function definitions */
+
+/* function definitions*/
+static int *read_series(int sensor);
+static int find_interval(int *series, int sensor);
+
+/* function implementations */
+static int *read_series(int sensor)
+{
+ int *ser;
+ ser = (int*)malloc(SAMPLE_NUMBER * sizeof(int));
+
+ if (ser == NULL) {
+ Serial.print("ERROR ALLOCATING MEMORY\n\r");
+ return NULL;
+ }
+
+ for (int i = 0; i < SAMPLE_NUMBER; ++i) {
+ ser[i] = sensor_read(sensor);
+ Serial.print(ser[i]);
+ Serial.print("\n\r");
+ delay(500);
+ }
+ return ser;
+}
+
+static int in_range(int num, int u_lim, int l_lim)
+{
+ if (l_lim < num && num < u_lim) {
+ return 1;
+ }
+ return 0;
+}
+
+static int find_interval(int *series, int sensor)
+{
+ /* only checks a single int currently */
+ struct interval *sen;
+ int *ser;
+ int u_lim, l_lim;
+
+ ser = series;
+ sen = all_intervals[sensor];
+ for (int i = 0; i < INTERVAL_NUMBER; ++i) {
+ l_lim = sen[i].lower_limit;
+ u_lim = sen[i].upper_limit;
+ if (in_range(ser[0], u_lim, l_lim)) {
+ Serial.print("In interval 1\n\r");
+ } else {
+ Serial.print("Not in interval 1\n\r");
+ }
+ }
+ return 0;
+}
+
int sensor_read(int sensor)
{
int rx;
@@ -31,8 +105,23 @@ int sensor_read(int sensor)
int sensor_analyze()
{
- delay(1000);
- return 1;
+ int *ser;
+ int res;
+
+ for (int i = 0; i < SENSOR_NUMBER; ++i) {
+ ser = read_series(i);
+ if (ser == NULL) {
+ Serial.print("ERROR READING SERIES\n\r");
+ res = -1;
+ goto out_free_ser;
+ }
+ find_interval(ser, i);
+ }
+ res = 1;
+
+out_free_ser:
+ free(ser);
+ return res;
}
void sensor_init(void)
@@ -41,4 +130,5 @@ void sensor_init(void)
pinMode(SENSOR_2, INPUT);
pinMode(SENSOR_3, INPUT);
pinMode(SENSOR_4, INPUT);
+
}