OSDN Git Service

shared memory improved.
authorastoria-d <astoria-d@mail.goo.ne.jp>
Fri, 8 Mar 2013 01:30:23 +0000 (10:30 +0900)
committerastoria-d <astoria-d@mail.goo.ne.jp>
Fri, 8 Mar 2013 01:30:23 +0000 (10:30 +0900)
display/Makefile
display/communication.c [deleted file]
display/display.c
display/dummy-driver.c
display/vga.h
display/vgashm.c [new file with mode: 0644]
display/window.c

index 5994763..1513685 100644 (file)
@@ -3,8 +3,9 @@ BIN=vgadisp
 
 DRIVER=dummy-driver
 
-OBJS=display.o window.o communication.o
+OBJS=display.o window.o vgashm.o
         
+DRIVER_OBJS=vgashm.o dummy-driver.o
 
 
 LIBS=-L../libs -lmotones -pthread
@@ -20,8 +21,8 @@ all:  $(BIN) $(DRIVER)
 $(BIN): $(OBJS) ../libs
        gcc -o $(BIN) $(OBJS) $(LFLAGS)
 
-$(DRIVER): $(DRIVER).o
-       gcc -o $(DRIVER) $(CFLAGS) $(DRIVER).c
+$(DRIVER): $(DRIVER_OBJS)
+       gcc -o $(DRIVER) $(DRIVER_OBJS)
 
 .c.o: 
        gcc $(CFLAGS) -c $<
diff --git a/display/communication.c b/display/communication.c
deleted file mode 100644 (file)
index 46fcc47..0000000
+++ /dev/null
@@ -1,114 +0,0 @@
-#include <stdio.h>
-#include <time.h>
-#include <pthread.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <sys/ipc.h>
-#include <sys/shm.h>
-
-#include "tools.h"
-#include "vga.h"
-
-void draw_point(int x, int y, char r, char g, char b);
-void set_pixel_color(int x, int y, int r, int g, int b);
-static struct rgb15 *disp_data;
-
-static pthread_t com_thread_id;
-
-static void *com_loop(void* arg) {
-    int posx, posy;
-
-    while (1) {
-        struct timespec ts = {0, 100};
-        for (posy = 0; posy < VGA_HEIGHT; posy++) {
-            for (posx = 0; posx < VGA_WIDTH; posx++) {
-                int pos = posx + VGA_WIDTH * posy;
-                set_pixel_color(posx, posy, 
-                        to16bit(disp_data[pos].r), 
-                        to16bit(disp_data[pos].g), 
-                        to16bit(disp_data[pos].b));
-            }
-        }
-        nanosleep(&ts, NULL);
-    }
-    /*
-    */
-
-    /*
-    while (1) {
-        int x, y;
-        int r,g,b;
-        struct timespec ts = {0, 10};
-
-        for (y = 0; y < VGA_HEIGHT; y++) {
-            for (x = 0; x < VGA_WIDTH; x++) {
-                set_pixel_color(x, y, r, g, b);
-            }
-        }
-        r = rand();
-        g = rand();
-        b = rand();
-        //dprint("sleep while...\n");
-        nanosleep(&ts, NULL);
-    }
-    */
-
-    return NULL;
-}
-
-static int shm_init(void) {
-    key_t key;
-    int   shmid;
-
-    //create shared memory
-    key = ftok(VGA_SHM, VGA_SHM_PRJ_ID);
-    if (key == -1) {
-        fprintf(stderr, "error preparing shared memory.\n");
-        return FALSE;
-    }
-
-    if((shmid = shmget(key, VGA_SHM_SIZE, IPC_CREAT|IPC_EXCL|0666)) == -1) 
-    {
-        printf("Shared memory segment exists - opening as client\n");
-
-        /* Segment probably already exists - try as a client */
-        if((shmid = shmget(key, VGA_SHM_SIZE, 0)) == -1) 
-        {
-            fprintf(stderr, "error opening shared memory.\n");
-            return FALSE;
-        }
-    }
-
-    /* Attach (map) the shared memory segment into the current process */
-    if((disp_data = (struct rgb15 *)shmat(shmid, 0, 0)) == (struct rgb15*)-1)
-    {
-        fprintf(stderr, "error attaching shared memory.\n");
-        return FALSE;
-    }
-    return TRUE;
-}
-
-int comm_init(void) {
-    int ret;
-    pthread_attr_t attr;
-
-    ret = shm_init();
-    if (!ret)
-        return FALSE;
-
-    ret = pthread_attr_init(&attr);
-    if (ret != RT_OK)
-        return FALSE;
-
-    com_thread_id = 0;
-    ret = pthread_create(&com_thread_id, &attr, com_loop, NULL);
-    if (ret != RT_OK)
-        return FALSE;
-
-    return TRUE;
-}
-
index 0e9d3c8..32b6cec 100644 (file)
@@ -3,8 +3,6 @@
 #include "tools.h"
 
 int window_start(int argc, char** argv);
-int comm_init(void);
-int receiver_init(void);
 int window_init(void);
 
 
@@ -18,12 +16,6 @@ int main(int argc, char** argv) {
         return -1;
     }
 
-    ret = comm_init();
-    if (!ret) {
-        fprintf(stderr, "comm init error.\n");
-        return -1;
-    }
-    //return main_file();
     return window_start(argc, argv);
 }
 
index 07b65eb..422e537 100644 (file)
@@ -4,9 +4,6 @@
 #include <time.h>
 #include <stdlib.h>
 #include <stdio.h>
-#include <sys/types.h>
-#include <sys/ipc.h>
-#include <sys/shm.h>
 #include <ctype.h>
 #include <string.h>
 
@@ -14,6 +11,7 @@
 #include "vga.h"
 
 static struct rgb15 *disp_data;
+void *vga_shm_get(void);
 
 //struct timespec sleep_inteval = {0, 1};
 
@@ -70,43 +68,17 @@ static void init_color1(void) {
                 disp_data[pos].b = to5bit(0xffff);
             }
 
-            /*
-            disp_data[pos].r = to5bit(0xffff);
-            disp_data[pos].g = to5bit(0);
-            disp_data[pos].b = to5bit(0xffff);
-             */
         }
     }
 }
 
 int main(int argc, char** argv) {
-    key_t key;
-    int   shmid;
-
+    //register signal handler
     //signal(SIGPIPE, SIG_IGN);
     signal(SIGPIPE, pipe_sig_handler);
-
-    //create shared memory
-    key = ftok(VGA_SHM, VGA_SHM_PRJ_ID);
-    if (key == -1) {
-        fprintf(stderr, "error preparing shared memory.\n");
-        return -1;
-    }
-
-    if((shmid = shmget(key, VGA_SHM_SIZE, IPC_CREAT|IPC_EXCL|0666)) == -1) 
-    {
-        printf("Shared memory segment exists - opening as client\n");
-
-        /* Segment probably already exists - try as a client */
-        if((shmid = shmget(key, VGA_SHM_SIZE, 0)) == -1) 
-        {
-            fprintf(stderr, "error opening shared memory.\n");
-            return -1;
-        }
-    }
-
-    /* Attach (map) the shared memory segment into the current process */
-    if((disp_data = (struct rgb15 *)shmat(shmid, 0, 0)) == (struct rgb15*)-1)
+    
+    /* get vga shared memory */
+    if((disp_data = (struct rgb15 *)vga_shm_get()) == NULL)
     {
         fprintf(stderr, "error attaching shared memory.\n");
         return -1;
@@ -115,10 +87,6 @@ int main(int argc, char** argv) {
     memset(disp_data, 0, sizeof(VGA_SHM_SIZE));
     init_color1();
     
-    while (1) {
-        sleep(1);
-    }
-
     return 0;
 }
 
index 513e940..be7f206 100644 (file)
@@ -2,6 +2,7 @@
 #define VGA_WIDTH   640
 #define VGA_HEIGHT  480
 
+/*
 struct vga_pulse {
     //vertical sync bit
     unsigned int v_sync  :1;
@@ -15,7 +16,8 @@ struct vga_pulse {
     unsigned int b       :5;
 
 };
-
+#define DISPLAY_PORT    9999
+*/
 struct rgb15 {
     unsigned int r   :5;
     unsigned int g   :5;
@@ -26,9 +28,9 @@ struct rgb15 {
 #define to5bit(col16) col16 * 0x1F / 0xFFFF
 #define to16bit(col5) col5 * 0xFFFF / 0x1F
 
-#define DISPLAY_PORT    9999
 
 #define VGA_SHM             "vgadisp"
 #define VGA_SHM_PRJ_ID      'm'
 #define VGA_SHM_SIZE        VGA_WIDTH * VGA_HEIGHT * sizeof (struct rgb15)
 
+#define VGA_REFRESH_RATE    60
diff --git a/display/vgashm.c b/display/vgashm.c
new file mode 100644 (file)
index 0000000..cebceb2
--- /dev/null
@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/shm.h>
+
+#include "tools.h"
+#include "vga.h"
+
+void *vga_shm_get(void) {
+    void* ret;
+    key_t key;
+    int   shmid;
+
+    //create shared memory
+    key = ftok(VGA_SHM, VGA_SHM_PRJ_ID);
+    if (key == -1) {
+        fprintf(stderr, "error preparing shared memory.\n");
+        return NULL;
+    }
+
+    if((shmid = shmget(key, VGA_SHM_SIZE, IPC_CREAT|IPC_EXCL|0666)) == -1) 
+    {
+        printf("Shared memory segment exists - opening as client\n");
+
+        /* Segment probably already exists - try as a client */
+        if((shmid = shmget(key, VGA_SHM_SIZE, 0)) == -1) 
+        {
+            fprintf(stderr, "error opening shared memory.\n");
+            return NULL;
+        }
+    }
+
+    /* Attach (map) the shared memory segment into the current process */
+    if((ret = shmat(shmid, 0, 0)) == (void*)-1)
+    {
+        fprintf(stderr, "error attaching shared memory.\n");
+        return NULL;
+    }
+
+    return ret;
+}
+
index b007525..1112389 100644 (file)
@@ -5,16 +5,11 @@
 #include "tools.h"
 #include "vga.h"
 
-int pix_buf[VGA_WIDTH][VGA_HEIGHT][3];
-GdkPixmap *pixmap = NULL;
-GdkGC *gc = NULL;
-
-void set_pixel_color(int x, int y, int r, int g, int b) {
-    //g_print ("set pix...\n");
-    pix_buf[x][y][0] = r;
-    pix_buf[x][y][1] = g;
-    pix_buf[x][y][2] = b;
-}
+void *vga_shm_get(void);
+
+static struct rgb15 *disp_data;
+static GdkPixmap *pixmap = NULL;
+static GdkGC *gc = NULL;
 
 GdkGC *set_color(gushort r, gushort g, gushort b)
 {
@@ -38,12 +33,14 @@ gint repaint(gpointer data){
     for (y = 0; y < VGA_HEIGHT; y++) {
         for (x = 0; x < VGA_WIDTH; x++) {
 
-            if (x%2 || y%2)
-                continue;
+            int pos = x + VGA_WIDTH * y;
 
-            set_color(pix_buf[x][y][0],
-                    pix_buf[x][y][1],
-                    pix_buf[x][y][2]); 
+            //if (x%2 || y%2)
+             //   continue;
+
+            set_color( to16bit(disp_data[pos].r), 
+                    to16bit(disp_data[pos].g),
+                    to16bit(disp_data[pos].b)); 
             gdk_draw_point (pixmap, gc, x, y);
         }
     }
@@ -64,6 +61,7 @@ void configure_event(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
 }
 
 void expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data){
+    //copy pixmap to the window
     gdk_draw_pixmap(widget->window,
             widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
             pixmap,
@@ -105,7 +103,7 @@ int window_start(int argc, char** argv)
 
     gc = gdk_gc_new(window->window);
 
-    gtk_timeout_add(1, repaint, (gpointer) drawing_area);
+    gtk_timeout_add(1000 / VGA_REFRESH_RATE, repaint, (gpointer) drawing_area);
 
     gtk_main();
     gdk_threads_leave();
@@ -113,8 +111,24 @@ int window_start(int argc, char** argv)
     return 0;
 }
 
+static int shm_init(void) {
+
+    /* get vga shared memory */
+    if((disp_data = (struct rgb15 *)vga_shm_get()) == NULL)
+    {
+        fprintf(stderr, "error attaching shared memory.\n");
+        return FALSE;
+    }
+    return TRUE;
+}
+
 int window_init(void) {
-    memset(pix_buf, 0, sizeof(pix_buf));
+    int ret;
+    disp_data = NULL;
+    ret = shm_init();
+    if (!ret)
+        return FALSE;
+   
     return TRUE;
 }