]> rtime.felk.cvut.cz Git - hydro.git/blob - app-stefic/sensor/board.c
Added monitoring web system. Minor changes in regulator and in control.
[hydro.git] / app-stefic / sensor / board.c
1
2 #include <stdio.h>
3 #include <system_def.h>
4 #include <cpu_def.h>
5
6 #include <math.h>
7
8 #include "board.h"
9
10 adc_stat_t adcst;
11
12 // OBJECT INTERFACE VARIABLES
13 int oi_temperature;
14 unsigned int oi_cid_temp;
15 int oi_humidity;
16 unsigned int oi_cid_hum;
17 unsigned int oi_period;
18
19 // OBJECT INTERFACE FUNCTIONS
20 int oi_cid_temp_wrfnc(ULOI_PARAM_coninfo void *context){
21     uloi_uint_wrfnc(ULOI_ARG_coninfo &oi_cid_temp);
22     return 1;
23 }
24
25 int oi_cid_hum_wrfnc(ULOI_PARAM_coninfo void *context){
26     uloi_uint_wrfnc(ULOI_ARG_coninfo &oi_cid_hum);
27     return 1;
28 }
29
30 int oi_period_wrfnc(ULOI_PARAM_coninfo void *context){
31     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
32     return 1;
33 }
34
35 void send_data() {
36     int msgsend;
37
38     int cid_cnt = 0;
39     static const int MAX_CID_CNT = 3;
40     cid_data_t cids[MAX_CID_CNT];
41
42     if (oi_period == HEART_PERIOD){
43         cids[cid_cnt].cid = HEART_CID; 
44         cids[cid_cnt].value = status_val; 
45         cid_cnt++;
46     }
47     if (oi_cid_temp > 0){
48         cids[cid_cnt].cid = oi_cid_temp; 
49         cids[cid_cnt].value = oi_temperature; 
50         cid_cnt++;
51     }
52     if (oi_cid_hum > 0){
53         cids[cid_cnt].cid = oi_cid_hum; 
54         cids[cid_cnt].value = oi_humidity; 
55         cid_cnt++;
56     }
57
58     if (cid_cnt != 0){
59         int i;
60         int buff_len;
61
62         for (i=0; i<=CNT; i++) read_ADC(&adcst);
63         adc2oi();
64
65         uchar buf[cid_cnt * 6 + 3];
66         for (buff_len = 0; buff_len < 3; buff_len++) buf[buff_len] = 0;
67
68         for(i=0; i<cid_cnt; i++) {
69             int2buf(buf + buff_len, cids[i].cid);
70             buff_len += 2;
71             int2buf(buf + buff_len, 2);
72             buff_len += 2;
73             int2buf(buf + buff_len, cids[i].value);
74             buff_len += 2;
75         }
76
77         msgsend = ul_send_command(ul_fd, 0, UL_CMD_PDO, UL_BFL_NORE, (void*) buf, buff_len);
78         printf("[I] DATA\n");
79         ul_freemsg(ul_fd);
80     }
81 }
82
83 // void adc2oi(void){
84 //     oi_temperature = 25;
85 // //     printf("temperature: %i\n",adcst.temp);
86 //     oi_humidity = 100;
87 // //     printf("humidity: %i\n",adcst.hum);
88 // }
89
90 void adc2oi(void){
91     double tmp_temp, tmp_hum;
92
93     tmp_temp = (adcst.temp*ADres*100)/Aoz;
94     tmp_temp = ((int)(tmp_temp*10))/10.0;
95
96     tmp_hum = (adcst.hum*ADres)*20;
97
98     oi_temperature = tmp_temp * 10;
99     printf("temperature: %4.1f\n",tmp_temp);
100     oi_humidity = tmp_hum * 10;
101     printf("humidity: %4.1f\n",tmp_hum);
102 }
103
104 void oiinit(void){
105     oi_cid_temp = 0;
106     oi_cid_hum = 0;
107     oi_period = 2;
108     status_val = 5;
109 }
110
111 void check_PDO(int cid, int data){}
112 void regulate(void){}
113
114 void work_with(void){
115     mstime_t time;
116     led1_time = current_time();
117     led2_time = current_time();
118 //     adcst.read = 0;
119     adcst.temp = 498;
120 //     adcst.temp_tmp = 0;
121 //     adcst.hum = 0;
122 //     adcst.hum_tmp = 0;
123 //     adcst.cnt = 0;
124
125     time = current_time();
126
127     double tmp_temp;
128     tmp_temp = (adcst.temp*ADres*100)/Aoz;
129     printf("temperature: %f\n",tmp_temp);
130
131
132 //     while(1){
133 // //         blink();
134 // //         read_ADC(&adcst);
135 // 
136 //         if((current_time()-time) > 1000){
137 //             printf("AD0.1 %i\n", adcst.temp);
138 // //             printf("AD0.2 %i\n", adcst.hum);
139 //             time = current_time();
140 //             }
141 //     }
142 }
143
144 void init_ADC (int selected){
145     #ifndef OS_POSIX
146
147     PINSEL1|= 0x05000000; // P0.28 and P0.29 set for AD0.1 and AD0.2 inputs
148     AD0CR &= 0x00000000; // Clear All Bit Control 
149     switch (selected){
150         case 1: AD0CR |= 0x00000002; // Select ADC = AIN1, CLKDive => Pclk/(x+1)< 4.5Mhz 
151                 break;
152         case 2: AD0CR |= 0x00000004; // Select ADC = AIN2
153                 break;
154     }
155     AD0CR |= 0x0000FF00; // ADC Clock = VBP(PCLK) / 7 1.55us 
156     AD0CR &= 0xFFF1FFFF; // CLKS 11 ciclos de reloj Res Máx 
157     AD0CR |= 0x00200000; // PDN = 1 = Active ADC Module 
158 //     AD0CR |= 0x00010000; // Burst = 1 = Continuing conversion 
159 //     AD0CR &= 0xFF3FFFFF; // TEST[1:0] = 00 = Modo normal 
160 //     AD0CR &= 0xF7FFFFFF; // EDGE = 0 = Flanco de bajada 
161 //     AD0STAT = 0x00000110; 
162     #endif
163 }
164
165 int read_ADC (adc_stat_t *adcst){
166     #ifndef OS_POSIX
167
168     init_ADC(1);
169     AD0CR |= 0x01000000; // start conversion
170     while (!(AD0GDR & (ADGDR_DONE))); // wait for DONE to go high
171
172     adcst->read = AD0DR1; // get ADC data
173     adcst->read = ((adcst->read >>6) & 0x03FF); // extraction of result
174     adcst->temp_tmp += adcst->read;
175
176     init_ADC(2);
177     AD0CR |= 0x01000000; // start conversion
178     while (!(AD0GDR & (ADGDR_DONE))); // wait for DONE to go high
179
180     adcst->read = AD0DR2;
181     adcst->read = ((adcst->read >>6) & 0x03FF); // extraction of result
182     adcst->hum_tmp += adcst->read;
183
184     adcst->cnt++;
185     if (adcst->cnt == CNT){
186         adcst->cnt = 0;
187         adcst->temp = adcst->temp_tmp/CNT;
188         adcst->hum = adcst->hum_tmp/CNT;
189         adcst->temp_tmp = 0;
190         adcst->hum_tmp = 0;
191         return 1;
192     }
193
194     #endif
195     return 0;
196 }