Text config file support
authorCameron Ball <c.ball1729@gmail.com>
Fri, 20 Mar 2015 03:52:03 +0000 (03:52 +0000)
committerCameron Ball <c.ball1729@gmail.com>
Fri, 20 Mar 2015 03:52:03 +0000 (03:52 +0000)
Makefile
config.c [new file with mode: 0644]
config.h [new file with mode: 0644]
config.ini [new file with mode: 0644]
lib/inih [new submodule]
main.c

index 620e783..9c6d836 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,9 +1,21 @@
 CC = gcc -Wall
+lib_inih := lib/inih
+libs := $(lib_inih)
 
 all: hdmi-switcher
 
-hdmi-switcher: main.o
-       $(CC) main.o -o hdmi-switcher -lwiringPi
+hdmi-switcher: main.o config.o
+       $(CC) $(lib_inih)/ini.o config.o main.o -o hdmi-switcher -lwiringPi
 
 main.o: main.c
        $(CC) -O -c main.c
+
+config.o: inih config.c config.h
+       $(CC) -O -c config.c
+
+inih:
+       $(MAKE) -C $(lib_inih)
+
+clean:
+       rm -f *.o *.bak *.out ex
+       for d in $(libs); do make --directory=$$d clean; done
diff --git a/config.c b/config.c
new file mode 100644 (file)
index 0000000..a752d13
--- /dev/null
+++ b/config.c
@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "config.h"
+
+static int handler(void* user, const char* section, const char* name, const char* value)
+{
+    configuration* pconfig = (configuration*)user;
+
+    #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
+    if (MATCH("global", "credit_value")) {
+        pconfig->credit_value = atoi(value);
+    //}else if (MATCH("user", "name")) {
+      //  pconfig->name = strdup(value);
+    //} else if (MATCH("user", "email")) {
+    //    pconfig->email = strdup(value);
+    } else {
+        return 0;  /* unknown section/name, error */
+    }
+    return 1;
+}
+
+int init_config(configuration *config)
+{
+  return ini_parse("config.ini", handler, config) >= 0;
+}
diff --git a/config.h b/config.h
new file mode 100644 (file)
index 0000000..625ff63
--- /dev/null
+++ b/config.h
@@ -0,0 +1,10 @@
+#include "lib/inih/ini.h"
+
+typedef struct
+{
+    int credit_value;
+//    const char* name;
+//    const char* email;
+} configuration;
+
+int init_config(configuration *config);
diff --git a/config.ini b/config.ini
new file mode 100644 (file)
index 0000000..c46ee0f
--- /dev/null
@@ -0,0 +1,3 @@
+[global]
+credit_value=10              ; How many  seconds inserting a credit adds to the timer
+
diff --git a/lib/inih b/lib/inih
new file mode 160000 (submodule)
index 0000000..d694557
--- /dev/null
+++ b/lib/inih
@@ -0,0 +1 @@
+Subproject commit d6945571ad745e12952e4b824f591864f190934e
diff --git a/main.c b/main.c
index 06d1713..9227a5a 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,9 +1,10 @@
 #include <stdio.h>
 #include <wiringPi.h>
 #include <time.h>
+#include "config.h"
 #define COIN_BUTTON 7
-#define CREDIT_EXTENSION 10
 
+configuration config;
 int expires = -1;
 int one_second_ago = 0;
 
@@ -41,14 +42,14 @@ int read_button()
 {
   if(digitalRead(COIN_BUTTON) == LOW)
   {
-    printf("Nice! I just added %d to your time.\n", CREDIT_EXTENSION);
+    printf("Nice! I just added %d to your time.\n", config.credit_value);
 
     //Increment timer
     if(expires == -1) //Initial credit, set the expiry to now plus time
     {
-        expires = now() + CREDIT_EXTENSION;
+        expires = now() + config.credit_value;
     } else { //expiry already set, simply extend by time
-        expires += CREDIT_EXTENSION;
+        expires += config.credit_value;
     }
 
     //wait for button to be released
@@ -71,7 +72,9 @@ int read_button()
 
 int main (void)
 {
+  init_config(&config);
   init_gpio();
+
   printf ("Short pin 7 and 9 to increment timer.\n") ;
 
   while(1)