Logging function moved to separate file.
[fpga/rpi-motor-control.git] / pmsm-control / test_sw / logs.c
1
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include "logs.h"
6 #include "pmsm_state.h"
7 #include "rp_spi.h"
8
9 /*
10  * \brief
11  * Free logs memory.
12  */
13 void freeLogs(struct rpi_state* this){
14         int r;
15         if (this->log_col_count){
16                 for (r=0;r<LOG_ROWS;r++){
17                         free(this->logs[r]);
18                 }
19         }
20         this->log_col_count=0;
21         this->doLogs=0;
22 }
23
24 /*
25  * \brief
26  * Makes log.
27  */
28 void makeLog(struct rpi_state* this){
29         int r;
30         if (this->log_col==MAX_LOGS-1){
31                 this->doLogs=0;
32                 return;
33         }
34         this->logs[0][this->log_col]=(int)this->tf_count;
35         this->logs[1][this->log_col]=(int)this->spi_dat->pozice;
36
37         this->logs[2][this->log_col]=(int)this->pwm1;
38         this->logs[3][this->log_col]=(int)this->pwm2;
39         this->logs[4][this->log_col]=(int)this->pwm3;
40         this->logs[5][this->log_col]=this->duty;
41
42         this->logs[6][this->log_col]=this->desired_spd;
43         this->logs[7][this->log_col]=this->speed;
44
45         this->logs[8][this->log_col]=(int)(this->spi_dat->ch1/this->spi_dat->adc_m_count);
46         this->logs[9][this->log_col]=(int)(this->spi_dat->ch2/this->spi_dat->adc_m_count);
47         this->logs[10][this->log_col]=(int)(this->spi_dat->ch0/this->spi_dat->adc_m_count);
48
49         this->log_col++;
50      /*
51         if (rps.log_col==rps.log_col_count-1){
52                 rps.log_col_count*=2;
53                 rps.log_col_count%=MAX_LOGS;
54                 for (r=0;r<LOG_ROWS;r++){
55                         rps.logs[r]=realloc(rps.logs[r],rps.log_col_count*sizeof(int));
56                         if (rps.logs[r]==NULL){
57                                 rps.doLogs=0;
58                                 rps.error=1;
59                         }
60                 }
61         }
62      */
63 }
64
65 /*
66  * \brief
67  * Save logs
68  */
69 void saveLogs(struct rpi_state* this){
70         FILE *f;
71         int r,s;
72
73         f = fopen("logs.log", "w");
74         if (f == NULL){
75                 printf("Error opening file!\n");
76                 return;
77         }
78
79         for (r=0;r<LOG_ROWS;r++){
80                 for(s=0;s<this->log_col-1;s++){ /*posledni sloupec je vevyplneny*/
81                         if (s==this->log_col-2){
82                                 fprintf(f,"%d ",this->logs[r][s]);
83                         }else{
84                                 fprintf(f,"%d, ",this->logs[r][s]);
85                         }
86                 }
87                 fprintf(f,"\r");
88         }
89         fclose(f);
90         freeLogs(this);
91 }