SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer. svghmi
authorEdouard Tisserant
Fri, 23 Aug 2019 13:01:37 +0200
branchsvghmi
changeset 2765 887aba5ef178
parent 2764 b75cc2cf4e50
child 2766 3f3b1b8ccba4
SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
svghmi/svghmi.c
svghmi/svghmi.py
--- a/svghmi/svghmi.c	Thu Aug 22 14:58:53 2019 +0200
+++ b/svghmi/svghmi.c	Fri Aug 23 13:01:37 2019 +0200
@@ -1,23 +1,57 @@
+#include <pthread.h>
 #include "iec_types_all.h"
 #include "POUS.h"
 #include "config.h"
 #include "beremiz.h"
 
+#define HMI_BUFFER_SIZE %(buffer_size)d
+
+/* PLC reads from that buffer */
+static char rbuf[HMI_BUFFER_SIZE];
+
+/* PLC writes to that buffer */
+static char wbuf[HMI_BUFFER_SIZE];
+
+static pthread_mutex_t wbuf_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t rbuf_mutex = PTHREAD_MUTEX_INITIALIZER;
+
 %(extern_variables_declarations)s
 
 typedef const struct {
     void *ptr;
     __IEC_types_enum type;
-    /* TODO : w/r buffer, flags, locks */
+    uint32_t buf_index;
+    uint32_t flags;
 } hmi_tree_item_t;
 
 static hmi_tree_item_t hmi_tree_item[] = {
 %(variable_decl_array)s
 };
 
+typedef void(*hmi_tree_iterator)(hmi_tree_item_t*);
+void traverse_hmi_tree(hmi_tree_iterator fp)
+{
+    unsigned int i;
+    for(i = 0; i < sizeof(hmi_tree_item)/sizeof(hmi_tree_item_t); i++){
+        hmi_tree_item_t *dsc = &hmi_tree_item[i];
+        if(dsc->type != UNKNOWN_ENUM) 
+            (*fp)(dsc);
+    }
+}
+
+void read_iterator(hmi_tree_item_t *dsc){
+    /* todo */
+}
+
+void write_iterator(hmi_tree_item_t *dsc){
+    /* todo */
+}
+
 int __init_svghmi()
 {
-    %(varinit)s
+    bzero(rbuf,sizeof(rbuf));
+    bzero(wbuf,sizeof(wbuf));
+
     return 0;
 }
 
@@ -27,11 +61,15 @@
 
 void __retrieve_svghmi()
 {
-%(varret)s
+    if(!pthread_mutex_lock(&rbuf_mutex)){
+        pthread_mutex_unlock(&rbuf_mutex);
+    }
 }
 
 void __publish_svghmi()
 {
-%(varpub)s
+    if(!pthread_mutex_lock(&wbuf_mutex)){
+        pthread_mutex_unlock(&wbuf_mutex);
+    }
 }
 
--- a/svghmi/svghmi.py	Thu Aug 22 14:58:53 2019 +0200
+++ b/svghmi/svghmi.py	Fri Aug 23 13:01:37 2019 +0200
@@ -168,7 +168,7 @@
 
         variable_decl_array = []
         extern_variables_declarations = []
-        bofs = 0
+        buf_index = 0
         for node in hmi_tree_root.traverse():
             if hasattr(node, "iectype"):
                 sz = DebugTypesSize.get(node.iectype, 0)
@@ -179,8 +179,9 @@
                         "MEM": "_O_ENUM",
                         "OUT": "_O_ENUM",
                         "VAR": "_ENUM"
-                    }[node.vartype] + "}"]
-                bofs += sz
+                    }[node.vartype] + ", " +
+                    str(buf_index) + ", 0}"]
+                buf_index += sz
                 if len(node.path) == 1:
                     extern_variables_declarations += [
                         "extern __IEC_" + node.iectype + "_" +
@@ -205,9 +206,7 @@
         svghmi_c_code = svghmi_c_code % { 
             "variable_decl_array": ",\n".join(variable_decl_array),
             "extern_variables_declarations": "\n".join(extern_variables_declarations),
-            "varinit":"",
-            "varret":"",
-            "varpub":""
+            "buffer_size": buf_index
             }
 
         gen_svghmi_c_path = os.path.join(buildpath, "svghmi.c")