]> rtime.felk.cvut.cz Git - hydro.git/blob - app-stefic/regulator/board.c
Repairs in code
[hydro.git] / app-stefic / regulator / board.c
1
2 #include <stdio.h>
3 #include <system_def.h>
4 #include <cpu_def.h>
5
6 #include "board.h"
7
8 // OBJECT INTERFACE VARIABLES
9 unsigned int oi_cid_fan;
10 unsigned int oi_cid_hum;
11 unsigned int oi_cid_light;
12 unsigned int oi_slot_temp;
13 unsigned int oi_slot_hum;
14 unsigned int oi_period;
15
16 unsigned int oi_want_temp;
17 unsigned int oi_want_hum;
18 unsigned int oi_want_light;
19 unsigned int oi_want_dark;
20
21 int hyst_temp = 1;
22 int hyst_hum = 10;
23
24 unsigned int fan = 0;
25 unsigned int hum = 0;
26 unsigned int light = 0;
27 int temperature;
28 int humidity;
29
30 // OBJECT INTERFACE FUNCTIONS
31 int oi_cid_fan_wrfnc(ULOI_PARAM_coninfo void *context){
32     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
33     return 1;
34 }
35
36 int oi_cid_hum_wrfnc(ULOI_PARAM_coninfo void *context){
37     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
38     return 1;
39 }
40
41 int oi_cid_light_wrfnc(ULOI_PARAM_coninfo void *context){
42     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
43     return 1;
44 }
45
46 int oi_slot_temp_wrfnc(ULOI_PARAM_coninfo void *context){
47     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
48     return 1;
49 }
50
51 int oi_slot_hum_wrfnc(ULOI_PARAM_coninfo void *context){
52     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
53     return 1;
54 }
55
56 int oi_period_wrfnc(ULOI_PARAM_coninfo void *context){
57     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
58     return 1;
59 }
60
61 int oi_want_temp_wrfnc(ULOI_PARAM_coninfo void *context){
62     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
63     return 1;
64 }
65
66 int oi_want_hum_wrfnc(ULOI_PARAM_coninfo void *context){
67     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
68     return 1;
69 }
70
71 void send_data() {
72     int msgsend;
73
74     int cid_cnt = 0;
75     static const int MAX_CID_CNT = 4;
76     cid_data_t cids[MAX_CID_CNT];
77
78     if (oi_period == 1){
79         cids[cid_cnt].cid = HEART_CID;
80         cids[cid_cnt].value = status_val;
81         cid_cnt++;
82     }
83     if (oi_cid_fan > 0){
84         cids[cid_cnt].cid = oi_cid_fan;
85         cids[cid_cnt].value = fan;
86         cid_cnt++;
87     }
88     if (oi_cid_hum > 0) {
89         cids[cid_cnt].cid = oi_cid_hum;
90         cids[cid_cnt].value = hum;
91         cid_cnt++;
92     }
93     if (oi_cid_light > 0) {
94         cids[cid_cnt].cid = oi_cid_light;
95         cids[cid_cnt].value = light;
96         cid_cnt++;
97     }
98
99     if (cid_cnt != 0){
100         int i;
101         int buff_len;
102
103         uchar buf[cid_cnt * 6 + 3];
104         for (buff_len = 0; buff_len < 3; buff_len++) buf[buff_len] = 0;
105
106         for(i=0; i<cid_cnt; i++) {
107             int2buf(buf + buff_len, cids[i].cid);
108             buff_len += 2;
109             int2buf(buf + buff_len, 2);
110             buff_len += 2;
111             int2buf(buf + buff_len, cids[i].value);
112             buff_len += 2;
113         }
114
115         msgsend = ul_send_query(ul_fd, 0, UL_CMD_PDO, UL_BFL_NORE, (void*) buf, buff_len);
116         printf("[I] DATA\n");
117         ul_freemsg(ul_fd);
118     }
119 }
120
121 void oiinit(void){
122     oi_cid_fan = 0;
123     oi_cid_hum = 0;
124     oi_cid_light = 0;
125     oi_slot_temp = 0;
126     oi_slot_hum = 0;
127     oi_period = 1;
128 }
129
130 void check_PDO(int cid, int data){
131     printf("CID: %i\n", cid);
132     if (cid == oi_slot_temp){
133         temperature = data;
134         printf("temperature: %i\n", temperature);
135     }
136     if (cid == oi_slot_hum){
137         humidity = data;
138         printf("humidity: %i\n", humidity);
139     }
140 }
141
142 void regulate(void){
143
144     if (((oi_want_temp + hyst_temp) > temperature) && fan != 1) fan = 1; 
145     if (((oi_want_temp - hyst_temp) < temperature) && fan != 0) fan = 0;
146
147     if (((oi_want_hum + hyst_hum) > humidity) && hum != 1) hum = 1; 
148     if (((oi_want_hum - hyst_hum) < humidity) && hum != 0) hum = 0;
149
150     if(((current_time()-l3time) < oi_want_light*3600*1000) && light == 0) {
151         light = 1;
152         l3time = current_time();
153     }
154     if(((current_time()-l3time) < oi_want_dark*3600*1000) && light == 1) {
155         light = 0;
156         l3time = current_time();
157     }
158 }
159
160 void work_with(void){
161
162 }
163
164