-
Notifications
You must be signed in to change notification settings - Fork 2
/
logging.c
executable file
·128 lines (105 loc) · 5.03 KB
/
logging.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include "main.h"
#include "logging.h"
#include "thermistor.h"
/* *** Constants *** */
/* *** Types *** */
/* *** Global Variables *** */
static FILE *write_ptr; // File pointer for writing data
/* *** Function Declarations *** */
static void Logging_Signal_Handler( int signal );
/***************************************************************************************************
Open a file for logging data
***************************************************************************************************/
void Logging_Init( void )
{
char filename[50];
time_t t = time(NULL);
struct tm tm;
tm = *localtime(&t);
sprintf(filename, "logs/Log-%d-%d-%d.csv", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
write_ptr = fopen(filename,"a"); // Open the log file for appending
if (write_ptr == NULL)
printf("Error, can't open log file: %s\n", filename);
else
printf("Log file: %s\n", filename);
}
/***************************************************************************************************
The sleep() function stalls the thread execution for the specified number of seconds; however, it
can be interrupted. In the case that the function is interrupted, it returns the remaining number
of seconds of sleep that did not occur. Continue sleeping until sleep() returns 0, which indicates
that the full sleep has occurred.
***************************************************************************************************/
int Logging_Full_Sleep( int seconds )
{
while (seconds > 0)
seconds = sleep(seconds);
return seconds;
}
/***************************************************************************************************
This service routine logs data to a comma separated file suitable for reading by spreadsheet
programs. A file is opened for appending, data is written to the file, then the file is closed.
Log Contents include temperature data from half of the thermistors, ADC measurements from the
remaining half of the analog channels, and the propane valve's servo position.
***************************************************************************************************/
void Logging_Service( void *shared_data_address )
{
float local_temperature_deg_f[NBR_OF_THERMISTORS]; // Local copy of the temperature data
float local_fire_temp_deg_f; // Local copy of the fire temperature
uint16_t local_adc_results[NBR_ADC_CHANNELS]; // Local copy of the shared ADC data
uint16_t local_servo_position; // Local copy of the servo position
time_t t = time(NULL); // Used for obtaining current time
struct tm tm; // Used for obtaining current time
uint8_t i;
// Pointer for accessing shared data
shared_data_type* p_shared_data = (shared_data_type*)shared_data_address;
signal(SIGINT, Logging_Signal_Handler);
sleep(5); // Sleep 5 seconds before logging any data
while (write_ptr != NULL)
{
// Obtain a lock to the shared data, create a local
// copy of the data, then release the lock
pthread_mutex_lock(&mutex);
memcpy( (uint8_t*)local_temperature_deg_f, (uint8_t*)p_shared_data->temp_deg_f, sizeof(local_temperature_deg_f));
memcpy( (uint8_t*)local_adc_results, (uint8_t*)p_shared_data->adc_results, sizeof(local_adc_results));
local_servo_position = p_shared_data->servo_position;
local_fire_temp_deg_f = p_shared_data->temp_deg_f_fire;
pthread_mutex_unlock(&mutex);
// Get the current time, build the timestamp and write it to the file. Might
// as well write the servo position as well. Good a time as any.
t = time(NULL);
tm = *localtime(&t);
fprintf(write_ptr, "%d-%d-%d %2d:%02d:%02d,%u", tm.tm_year + 1900, tm.tm_mon + 1,
tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, local_servo_position);
// Write the temperature for each of the probes
for (i = 0; i < NBR_OF_THERMISTORS; i++)
fprintf(write_ptr, ",%4.2f", local_temperature_deg_f[i]);
// Write the temperature and ADC value for the fire detection
fprintf( write_ptr, ",%4.1f", local_fire_temp_deg_f);
fprintf( write_ptr, ",%u", local_adc_results[NBR_ADC_CHANNELS-1]);
fwrite("\n", 1, 1, write_ptr); // Append a new line to the file
fflush(write_ptr); // Force the write to disk
Logging_Full_Sleep(15); // Delay 15 seconds before writing the next log entry
}
}
/***************************************************************************************************
When Ctrl+C is pressed to end the process, this function will catch the signal and close the file
stream before exiting the thread
***************************************************************************************************/
static void Logging_Signal_Handler( int signalnum )
{
switch (signalnum)
{
case SIGINT:
fclose(write_ptr);
exit(signalnum);
break;
}
}
/* *** End of File *** */