]> rtime.felk.cvut.cz Git - hydro.git/blob - app-stefic/control/board.c
Repairs in code
[hydro.git] / app-stefic / control / board.c
1
2 #include <system_def.h>
3 #include <cpu_def.h>
4 #include <stdio.h>
5
6 #include "board.h"
7
8 // OBJECT INTERFACE VARIABLES
9 unsigned int oi_fan;
10 unsigned int oi_cid_fan;
11 unsigned int oi_slot_fan;
12 unsigned int oi_humidifier;
13 unsigned int oi_cid_humidifier;
14 unsigned int oi_slot_humidifier;
15 unsigned int oi_light;
16 unsigned int oi_cid_light;
17 unsigned int oi_slot_light;
18 unsigned int oi_period;
19
20 // OBJECT INTERFACE FUNCTIONS
21 int oi_cid_fan_wrfnc(ULOI_PARAM_coninfo void *context){
22     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
23     return 1;
24 }
25
26 int oi_cid_humidifier_wrfnc(ULOI_PARAM_coninfo void *context){
27     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
28     return 1;
29 }
30
31 int oi_cid_light_wrfnc(ULOI_PARAM_coninfo void *context){
32     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
33     return 1;
34 }
35
36 int oi_slot_fan_wrfnc(ULOI_PARAM_coninfo void *context){
37     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
38     return 1;
39 }
40
41 int oi_slot_humidifier_wrfnc(ULOI_PARAM_coninfo void *context){
42     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
43     return 1;
44 }
45
46 int oi_slot_light_wrfnc(ULOI_PARAM_coninfo void *context){
47     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
48     return 1;
49 }
50
51 int oi_period_wrfnc(ULOI_PARAM_coninfo void *context){
52     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
53     return 1;
54 }
55
56 void set_fan_PIN(){
57     if (oi_fan == 1) {
58         SET_OUT_PIN(OUT_PORT,P1_29_FAN);
59         printf("FAN PIN UP\n");
60     } else {
61         CLR_OUT_PIN(OUT_PORT,P1_29_FAN);
62         printf("FAN PIN DOWN\n");
63     }
64 }
65
66 void set_humidifier_PIN(){
67     if (oi_humidifier == 1) {
68         SET_OUT_PIN(OUT_PORT,P1_30_HUMIDIFIER);
69         printf("HUM PIN UP\n");
70     } else {
71         CLR_OUT_PIN(OUT_PORT,P1_30_HUMIDIFIER);
72         printf("HUM PIN DOWN\n");
73     }
74 }
75
76 void set_light_PIN(){
77     if (oi_light == 1) {
78         SET_OUT_PIN(OUT_PORT,P1_31_LIGHT);
79         printf("LIGHT PIN UP\n");
80     } else {
81         CLR_OUT_PIN(OUT_PORT,P1_31_LIGHT);
82         printf("LIGHT PIN DOWN\n");
83     }
84 }
85
86 //*********************************************************
87 int oi_fan_wrfnc(ULOI_PARAM_coninfo void *context){
88     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
89     set_fan_PIN();
90     return 1;
91 }
92
93 int oi_humidifier_wrfnc(ULOI_PARAM_coninfo void *context){
94     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
95     set_humidifier_PIN();
96     return 1;
97 }
98
99 int oi_light_wrfnc(ULOI_PARAM_coninfo void *context){
100     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
101     set_light_PIN();
102     return 1;
103 }
104 //*********************************************************
105
106 void send_data() {
107     int msgsend;
108
109     int cid_cnt = 0;
110     static const int MAX_CID_CNT = 4;
111     cid_data_t cids[MAX_CID_CNT];
112
113     if (oi_period == 1){
114         cids[cid_cnt].cid = HEART_CID;
115         cids[cid_cnt].value = status_val;
116         cid_cnt++;
117     }
118     if (oi_cid_fan > 0){
119         cids[cid_cnt].cid = oi_cid_fan;
120         cids[cid_cnt].value = oi_fan;
121         cid_cnt++;
122     }
123     if (oi_cid_humidifier > 0) {
124         cids[cid_cnt].cid = oi_cid_humidifier;
125         cids[cid_cnt].value = oi_humidifier;
126         cid_cnt++;
127     }
128     if (oi_cid_light > 0) {
129         cids[cid_cnt].cid = oi_cid_light;
130         cids[cid_cnt].value = oi_light;
131         cid_cnt++;
132     }
133
134     if (cid_cnt != 0){
135         int i;
136         int buff_len;
137
138         uchar buf[cid_cnt * 6 + 3];
139         for (buff_len = 0; buff_len < 3; buff_len++) buf[buff_len] = 0;
140
141         for(i=0; i<cid_cnt; i++) {
142             int2buf(buf + buff_len, cids[i].cid);
143             buff_len += 2;
144             int2buf(buf + buff_len, 2);
145             buff_len += 2;
146             int2buf(buf + buff_len, cids[i].value);
147             buff_len += 2;
148         }
149
150         msgsend = ul_send_query(ul_fd, 0, UL_CMD_PDO, UL_BFL_NORE, (void*) buf, buff_len);
151         printf("[I] DATA\n");
152         ul_freemsg(ul_fd);
153     }
154 }
155
156 void oiinit(void){
157     oi_fan = 0;
158     oi_cid_fan = 0;
159     oi_slot_fan = 0;
160     oi_humidifier = 0;
161     oi_cid_humidifier = 0;
162     oi_slot_humidifier = 0;
163     oi_light = 0;
164     oi_cid_light = 0;
165     oi_slot_light = 0;
166     oi_period = 1;
167     status_val = 5;
168 }
169
170 void check_PDO(int cid, int data){
171     printf("CID: %i\n", cid);
172     if (cid == oi_slot_fan){
173         oi_fan = data;
174         set_fan_PIN();
175     }
176     if (cid == oi_slot_humidifier){
177         oi_humidifier = data;
178         set_humidifier_PIN();
179     }
180     if (cid == oi_slot_light){
181         oi_light = data;
182         set_light_PIN();
183     }
184 }
185
186 void regulate(void){}
187
188 void work_with(void){
189
190     PINSEL1 &= 0xFFFFB3FF;
191
192     // u vetsi karbicky je SET z 0 do 3.3
193     // u mensi je SET z -3.3 do 0
194     // varpins  yy
195
196 //     IO0SET = P0_21_BIT;
197 //     IO0SET = P0_22_BIT;
198
199     SET_OUT_PIN(OUT_PORT, P1_29_FAN);
200     SET_OUT_PIN(OUT_PORT, P1_31_LIGHT);
201     SET_OUT_PIN(OUT_PORT, P1_30_HUMIDIFIER);
202
203
204     IO0SET = LED1_BIT; //vypinani LED
205     IO0CLR = LED2_BIT; //zapinani LED
206
207 //     IO0CLR = LED1_BIT;
208 //     IO0SET = LED2_BIT;
209 }