]> rtime.felk.cvut.cz Git - hydro.git/blobdiff - app-stefic/regulator/board.c
Added regulator. Added accepting of PDO messages.
[hydro.git] / app-stefic / regulator / board.c
diff --git a/app-stefic/regulator/board.c b/app-stefic/regulator/board.c
new file mode 100644 (file)
index 0000000..6c2ce49
--- /dev/null
@@ -0,0 +1,152 @@
+
+#include <stdio.h>
+#include <system_def.h>
+#include <cpu_def.h>
+
+#include "board.h"
+
+// OBJECT INTERFACE VARIABLES
+unsigned int oi_cid_fan;
+unsigned int oi_cid_hum;
+unsigned int oi_cid_light;
+unsigned int oi_slot_temp;
+unsigned int oi_slot_hum;
+unsigned int oi_period;
+
+unsigned int oi_want_temp;
+unsigned int oi_want_hum;
+unsigned int oi_want_light;
+unsigned int oi_want_dark;
+
+int hyst_temp = 1;
+int hyst_hum = 10;
+unsigned int fan = 0;
+unsigned int hum = 0;
+unsigned int light = 0;
+int temperature;
+int humidity;
+
+// OBJECT INTERFACE FUNCTIONS
+int oi_cid_fan_wrfnc(ULOI_PARAM_coninfo void *context){
+    uloi_uint_wrfnc(ULOI_ARG_coninfo context);
+    return 1;
+}
+
+int oi_cid_hum_wrfnc(ULOI_PARAM_coninfo void *context){
+    uloi_uint_wrfnc(ULOI_ARG_coninfo context);
+    return 1;
+}
+
+int oi_cid_light_wrfnc(ULOI_PARAM_coninfo void *context){
+    uloi_uint_wrfnc(ULOI_ARG_coninfo context);
+    return 1;
+}
+
+int oi_slot_temp_wrfnc(ULOI_PARAM_coninfo void *context){
+    uloi_uint_wrfnc(ULOI_ARG_coninfo context);
+    return 1;
+}
+
+int oi_slot_hum_wrfnc(ULOI_PARAM_coninfo void *context){
+    uloi_uint_wrfnc(ULOI_ARG_coninfo context);
+    return 1;
+}
+
+int oi_period_wrfnc(ULOI_PARAM_coninfo void *context){
+    uloi_uint_wrfnc(ULOI_ARG_coninfo context);
+    return 1;
+}
+
+void send_data() {
+    int msgsend;
+
+    int cid_cnt = 0;
+    static const int MAX_CID_CNT = 4;
+    cid_data_t cids[MAX_CID_CNT];
+
+    if (oi_period == 1){
+        cids[cid_cnt].cid = HEART_CID;
+        cids[cid_cnt].value = status_val;
+        cid_cnt++;
+    }
+    if (oi_cid_fan > 0){
+        cids[cid_cnt].cid = oi_cid_fan;
+        cids[cid_cnt].value = fan;
+        cid_cnt++;
+    }
+    if (oi_cid_hum > 0) {
+        cids[cid_cnt].cid = oi_cid_hum;
+        cids[cid_cnt].value = hum;
+        cid_cnt++;
+    }
+    if (oi_cid_light > 0) {
+        cids[cid_cnt].cid = oi_cid_light;
+        cids[cid_cnt].value = light;
+        cid_cnt++;
+    }
+
+    if (cid_cnt == 0){
+        heartbeat();
+    } else {
+
+        int i;
+        int buff_len;
+
+        uchar buf[cid_cnt * 6 + 3];
+        for (buff_len = 0; buff_len < 3; buff_len++) buf[buff_len] = 0;
+
+        for(i=0; i<cid_cnt; i++) {
+            int2buf(buf + buff_len, cids[i].cid);
+            buff_len += 2;
+            int2buf(buf + buff_len, 2);
+            buff_len += 2;
+            int2buf(buf + buff_len, cids[i].value);
+            buff_len += 2;
+        }
+
+        msgsend = ul_send_query(ul_fd, 0, UL_CMD_PDO, UL_BFL_NORE, (void*) buf, buff_len);
+        printf("[I] DATA\n");
+        free(buf);
+        ul_freemsg(ul_fd);
+    }
+}
+
+void oiinit(void){
+    oi_cid_fan = 0;
+    oi_cid_hum = 0;
+    oi_cid_light = 0;
+    oi_slot_temp = 0;
+    oi_slot_hum = 0;
+    oi_period = 1;
+}
+
+void check_PDO(int cid, int data){
+    if (cid == oi_slot_temp)
+        temperature = data;
+    if (cid == oi_slot_temp)
+        humidity = data;
+}
+
+void regulate(void){
+
+    if (((oi_want_temp + hyst_temp) > temperature) && fan != 1) fan = 1; 
+    if (((oi_want_temp - hyst_temp) < temperature) && fan != 0) fan = 0;
+
+    if (((oi_want_hum + hyst_hum) > humidity) && hum != 1) hum = 1; 
+    if (((oi_want_hum - hyst_hum) < humidity) && hum != 0) hum = 0;
+
+    if(((current_time()-l3time) < oi_want_light*3600*1000) && light == 0) {
+        light = 1;
+        l3time = current_time();
+    }
+    if(((current_time()-l3time) < oi_want_dark*3600*1000) && light == 1) {
+        light = 0;
+        l3time = current_time();
+    }
+}
+
+void work_with(void){
+
+}
+
+