Add support for second pattern engine to helper program
authorPhilipp Zabel <philipp.zabel@gmail.com>
Sun, 28 Feb 2010 14:32:37 +0000 (15:32 +0100)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Sun, 28 Feb 2010 15:48:11 +0000 (16:48 +0100)
src/led-pattern-helper.vala

index cda11da..7fc5ad5 100644 (file)
@@ -39,44 +39,72 @@ public bool test_pattern (string pattern, bool apply) {
        // screen_on = p[1].to_int ();
        // timeout = p[2].to_int ();
 
+       int led_map1 = 0x000;
+       int led_map2 = 0x000;
+       if ("r" in p[3])
+               led_map1 |= 0x001;
+       if ("R" in p[3])
+               led_map2 |= 0x001;
+       if ("g" in p[3])
+               led_map1 |= 0x010;
+       if ("G" in p[3])
+               led_map2 |= 0x010;
+       if ("b" in p[3])
+               led_map1 |= 0x100;
+       if ("B" in p[3])
+               led_map2 |= 0x100;
+
+       if ((led_map1 & led_map2) != 0) {
+               stderr.printf ("pattern assigns a led to both channels: '%s'\n", p[3]);
+               return false;
+       }
+
+       string led_map = "";
+       if (0x001 in led_map1)
+               led_map += "r";
+       if (0x001 in led_map2)
+               led_map += "R";
+       if (0x010 in led_map1)
+               led_map += "g";
+       if (0x010 in led_map2)
+               led_map += "G";
+       if (0x100 in led_map1)
+               led_map += "b";
+       if (0x100 in led_map2)
+               led_map += "B";
+
+       if (led_map != p[3]) {
+               stderr.printf ("pattern contains invalid led map: '%s\n", p[3]);
+               return false;
+       }
+
        int[] led_currents = { 2, 2, 2 };
-       string led_map1 = "000";
-       switch (p[3]) {
-       case "r":
-               led_map1 = "001";
+       switch (led_map1 | led_map2) {
+       case 0x001:
                led_currents = { 8, 0, 2 };
                break;
-       case "g":
-               led_map1 = "010";
+       case 0x010:
                led_currents = { 2, 2, 2 };
                break;
-       case "b":
-               led_map1 = "100";
+       case 0x100:
                led_currents = { 2, 2, 2 };
                break;
-       case "rg":
-               led_map1 = "011";
+       case 0x011:
                led_currents = { 20, 2, 0 };
                break;
-       case "rb":
-               led_map1 = "101";
+       case 0x101:
                // TODO: led_currents?
                break;
-       case "gb":
-               led_map1 = "110";
+       case 0x110:
                // TODO: led_currents?
                break;
-       case "rgb":
-               led_map1 = "111";
+       case 0x111:
                led_currents = { 8, 2, 2 };
                break;
-       default:
-               stderr.printf ("only single-engine patterns supported for now\n");
-               return false;
        }
 
        if (p[4].length > 16*4 || p[5].length > 16*4) {
-               stderr.printf ("pattern too long!\n");
+               stderr.printf ("engine1 pattern too long!\n");
                return false;
        }
 
@@ -90,9 +118,18 @@ public bool test_pattern (string pattern, bool apply) {
                return false;
        }
 
-       if (p[5] != "9d800000") {
-               // FIXME
-               stderr.printf ("only single-engine patterns supported for now\n");
+       if (p[5].length > 16*4 || p[5].length > 16*4) {
+               stderr.printf ("engine2 pattern too long!\n");
+               return false;
+       }
+
+       if (!(p[5].has_prefix ("9d80"))) {
+               stderr.printf ("engine2 pattern doesn't start with reset mux command\n");
+               return false;
+       }
+
+       if (!(p[5].has_suffix ("0000")) && !(p[5].has_suffix ("c000"))) {
+               stderr.printf ("engine2 pattern doesn't end with repeat or stop command\n");
                return false;
        }
 
@@ -139,7 +176,7 @@ public bool test_pattern (string pattern, bool apply) {
                stderr.printf ("failed to set engine1 mux\n");
                return false;
        }
-       f.printf ("0000%s00\n", led_map1);
+       f.printf ("0000%03x00\n", led_map1);
 
        f = FileStream.open (Path.build_filename (i2c_dev, "engine1_load"), "w");
        if (f == null) {
@@ -148,6 +185,40 @@ public bool test_pattern (string pattern, bool apply) {
        }
        f.printf ("%s\n", p[4]);
 
+       if (led_map2 != 0x000) {
+               f = FileStream.open (Path.build_filename (i2c_dev, "engine2_mode"), "w");
+               if (f == null) {
+                       stderr.printf ("failed to set engine2 to load mode\n");
+                       return false;
+               }
+               f.printf ("load\n");
+
+               for (int i = 0; i < retries; i++) {
+                       f = FileStream.open (Path.build_filename (i2c_dev, "engine2_leds"), "w");
+                       if (f != null)
+                               break;
+               }
+               if (f == null) {
+                       stderr.printf ("failed to set engine2 mux\n");
+                       return false;
+               }
+               f.printf ("0000%03x00\n", led_map2);
+
+               f = FileStream.open (Path.build_filename (i2c_dev, "engine2_load"), "w");
+               if (f == null) {
+                       stderr.printf ("failed to load engine1 pattern\n");
+                       return false;
+               }
+               f.printf ("%s\n", p[5]);
+
+               f = FileStream.open (Path.build_filename (i2c_dev, "engine2_mode"), "w");
+               if (f == null) {
+                       stderr.printf ("failed to set engine2 to run mode\n");
+                       return false;
+               }
+               f.printf ("run\n");
+       }
+
        f = FileStream.open (Path.build_filename (i2c_dev, "engine1_mode"), "w");
        if (f == null) {
                stderr.printf ("failed to set engine1 to run mode\n");