]> rtime.felk.cvut.cz Git - hydro.git/blob - app-stefic/control/board.c
365fbf02d4902cc7d7c2452d5e09f1037899dbc3
[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 uint16_t status_val;
10 uint16_t oi_fan;
11 uint16_t oi_cid_fan;
12 uint16_t oi_humidifier;
13 uint16_t oi_cid_humidifier;
14 uint16_t oi_light;
15 uint16_t oi_cid_light;
16 uint16_t oi_period;
17
18 // OBJECT INTERFACE FUNCTIONS
19 int oi_cid_fan_wrfnc(ULOI_PARAM_coninfo void *context){
20     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
21     return 1;
22 }
23
24 int oi_cid_humidifier_wrfnc(ULOI_PARAM_coninfo void *context){
25     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
26     return 1;
27 }
28
29 int oi_cid_light_wrfnc(ULOI_PARAM_coninfo void *context){
30     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
31     return 1;
32 }
33
34 int oi_period_wrfnc(ULOI_PARAM_coninfo void *context){
35     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
36     return 1;
37 }
38
39 //*********************************************************
40 int oi_fan_wrfnc(ULOI_PARAM_coninfo void *context){
41     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
42     if (oi_fan == 1) {
43         SET_OUT_PIN(OUT_PORT,P1_27_FAN);
44         printf("FAN PIN UP\n");
45     } else {
46         CLR_OUT_PIN(OUT_PORT,P1_27_FAN);
47         printf("FAN PIN DOWN\n");
48     }
49     return 1;
50 }
51
52 int oi_humidifier_wrfnc(ULOI_PARAM_coninfo void *context){
53     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
54     if (oi_humidifier == 1) {
55         SET_OUT_PIN(OUT_PORT,P1_28_HUMIDIFIER);
56         printf("HUM PIN UP\n");
57     } else {
58         CLR_OUT_PIN(OUT_PORT,P1_28_HUMIDIFIER);
59         printf("HUM PIN DOWN\n");
60     }
61     return 1;
62 }
63
64 int oi_light_wrfnc(ULOI_PARAM_coninfo void *context){
65     uloi_uint_wrfnc(ULOI_ARG_coninfo context);
66     if (oi_light == 1) {
67         SET_OUT_PIN(OUT_PORT,P1_29_LIGHT);
68         printf("LIGHT PIN UP\n");
69     } else {
70         CLR_OUT_PIN(OUT_PORT,P1_29_LIGHT);
71         printf("LIGHT PIN DOWN\n");
72     }
73     return 1;
74 }
75 //*********************************************************
76
77 void send_data() {
78     int msgsend;
79     uchar *buf;
80     int i, len;
81
82     len = DATA_BUF_LEN + 6;
83 //     if (oi_period == 1) len = len + 6;
84
85     // preparing of msg
86     buf = (uchar *) malloc(len*sizeof(uchar));  // NULL NULL NULL TEMPERATURE_CID len_data oi_temperature CID_HUM len_data oi_humidity
87     for (i=0; i<=2; i++) buf[i] = 0;
88
89     if (oi_period == 1){
90         int2buf(&buf[3],HEART_CID);
91         int2buf(&buf[5],2);
92         int2buf(&buf[7],status_val);
93     }
94
95     msgsend = ul_send_query(ul_fd, ul_dyac->ul_dysa, UL_CMD_PDO, UL_BFL_NORE, (void*) buf, len);
96     printf("[I] DATA\n");
97     free(buf);
98     ul_freemsg(ul_fd);
99 }
100
101 void accept_SDO(void) {
102         // setting lan configuration
103         if ((ul_inepoll(ul_fd)>0) && (ul_acceptmsg(ul_fd, &msginfo)>=0)) {
104             if ((msginfo.cmd != UL_CMD_PDO) && (msginfo.sadr != 98)) {
105                 if (!(msginfo.flg&(UL_BFL_PROC | UL_BFL_FAIL))) {
106                     //waiting for msg from bus
107                     if (uloi_process_msg(ULOI_ARG_coninfo (uloi_objdes_array_t*)&uloi_objdes_main, &msginfo)<0) {
108                         //checking dynamic adresation
109                         if (uldy_process_msg(ULDY_ARG_ul_dyac &msginfo)>=0){
110                             printf("message recieved - UL_CMD_NCS, sadr: %i, dadr: %i, cmd: %i\n",msginfo.sadr, msginfo.dadr, msginfo.cmd);
111                             printf("server addr :%i\n",ul_dyac->ul_dysa);
112                         } else {
113                             ul_freemsg(ul_fd);
114                             printf("problem in uldy_process_msg\n");
115                         }
116                     } else {
117                         printf("ULOI message processed\n");
118                     }
119                 } else {
120                     ul_freemsg(ul_fd);
121                 }
122             }
123         }
124 }
125
126 void setmyaddr(void){
127     ul_setmyadr(ul_fd,98);
128 }
129
130 void oiinit(void){
131     oi_cid_fan=243;
132     oi_cid_humidifier=244;
133     oi_cid_light=245;
134     oi_period=1;
135     status_val = 5;
136 }
137
138 void work_with(void){
139
140     PINSEL1 &= 0xFFFFB3FF;
141
142     // u vetsi karbicky je SET z 0 do 3.3
143     // u mensi je SET z -3.3 do 0
144     // varpins  yy
145
146 //     IO0SET = P0_21_BIT;
147 //     IO0SET = P0_22_BIT;
148
149     SET_OUT_PIN(OUT_PORT, P1_27_FAN);
150     CLR_OUT_PIN(OUT_PORT, P1_28_HUMIDIFIER);
151     SET_OUT_PIN(OUT_PORT, P1_29_LIGHT);
152
153     IO0SET = LED1_BIT; //vypinani LED
154     IO0CLR = LED2_BIT; //zapinani LED
155
156 //     IO0CLR = LED1_BIT;
157 //     IO0SET = LED2_BIT;
158 }