1 /* This file is part of LED Pattern Editor.
3 * Copyright (C) 2010 Philipp Zabel
5 * LED Pattern Editor is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * LED Pattern Editor is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with LED Pattern Editor. If not, see <http://www.gnu.org/licenses/>.
19 public bool test_pattern (string pattern, bool apply) {
20 string[] key_value = pattern.split ("=");
22 if (key_value.length != 2) {
23 stderr.printf ("pattern does not contain '=': %s\n", pattern);
27 string[] p = key_value[1].split (";");
29 stderr.printf ("pattern does not contain 6 components: %d\n", p.length);
33 if (p[0].has_prefix ("Pattern")) {
34 stderr.printf ("pattern name doesn't start with 'Pattern': '%s'\n", p[0]);
38 // priority = p[0].to_int ();
39 // screen_on = p[1].to_int ();
40 // timeout = p[2].to_int ();
57 if ((led_map1 & led_map2) != 0) {
58 stderr.printf ("pattern assigns a led to both channels: '%s'\n", p[3]);
63 if (0x001 in led_map1)
65 if (0x001 in led_map2)
67 if (0x010 in led_map1)
69 if (0x010 in led_map2)
71 if (0x100 in led_map1)
73 if (0x100 in led_map2)
76 if (led_map != p[3]) {
77 stderr.printf ("pattern contains invalid led map: '%s\n", p[3]);
81 int[] led_currents = { 2, 2, 2 };
82 switch (led_map1 | led_map2) {
84 led_currents = { 8, 0, 2 };
87 led_currents = { 2, 2, 2 };
90 led_currents = { 2, 2, 2 };
93 led_currents = { 20, 2, 0 };
96 // TODO: led_currents?
99 // TODO: led_currents?
102 led_currents = { 8, 2, 2 };
106 if (p[4].length > 16*4 || p[5].length > 16*4) {
107 stderr.printf ("engine1 pattern too long!\n");
111 if (!(p[4].has_prefix ("9d80"))) {
112 stderr.printf ("engine1 pattern doesn't start with reset mux command\n");
116 if (!(p[4].has_suffix ("0000")) && !(p[4].has_suffix ("c000"))) {
117 stderr.printf ("engine1 pattern doesn't end with repeat or stop command\n");
121 if (p[5].length > 16*4 || p[5].length > 16*4) {
122 stderr.printf ("engine2 pattern too long!\n");
126 if (!(p[5].has_prefix ("9d80"))) {
127 stderr.printf ("engine2 pattern doesn't start with reset mux command\n");
131 if (!(p[5].has_suffix ("0000")) && !(p[5].has_suffix ("c000"))) {
132 stderr.printf ("engine2 pattern doesn't end with repeat or stop command\n");
139 string i2c_dev = "/sys/bus/i2c/devices/2-0032";
141 var f = FileStream.open ("/sys/class/leds/lp5523:r/led_current", "w");
143 stderr.printf ("failed to set red led current\n");
146 f.printf ("%d\n", led_currents[0]);
148 f = FileStream.open ("/sys/class/leds/lp5523:g/led_current", "w");
150 stderr.printf ("failed to set green led current\n");
153 f.printf ("%d\n", led_currents[1]);
155 f = FileStream.open ("/sys/class/leds/lp5523:b/led_current", "w");
157 stderr.printf ("failed to set blue led current\n");
160 f.printf ("%d\n", led_currents[2]);
162 f = FileStream.open (Path.build_filename (i2c_dev, "engine1_mode"), "w");
164 stderr.printf ("failed to set engine1 to load mode\n");
170 for (int i = 0; i < retries; i++) {
171 f = FileStream.open (Path.build_filename (i2c_dev, "engine1_leds"), "w");
176 stderr.printf ("failed to set engine1 mux\n");
179 f.printf ("0000%03x00\n", led_map1);
181 f = FileStream.open (Path.build_filename (i2c_dev, "engine1_load"), "w");
183 stderr.printf ("failed to load engine1 pattern\n");
186 f.printf ("%s\n", p[4]);
188 if (led_map2 != 0x000) {
189 f = FileStream.open (Path.build_filename (i2c_dev, "engine2_mode"), "w");
191 stderr.printf ("failed to set engine2 to load mode\n");
196 for (int i = 0; i < retries; i++) {
197 f = FileStream.open (Path.build_filename (i2c_dev, "engine2_leds"), "w");
202 stderr.printf ("failed to set engine2 mux\n");
205 f.printf ("0000%03x00\n", led_map2);
207 f = FileStream.open (Path.build_filename (i2c_dev, "engine2_load"), "w");
209 stderr.printf ("failed to load engine1 pattern\n");
212 f.printf ("%s\n", p[5]);
214 f = FileStream.open (Path.build_filename (i2c_dev, "engine2_mode"), "w");
216 stderr.printf ("failed to set engine2 to run mode\n");
222 f = FileStream.open (Path.build_filename (i2c_dev, "engine1_mode"), "w");
224 stderr.printf ("failed to set engine1 to run mode\n");
232 bool mce_ini_check (string new_mce_ini) {
233 var f = FileStream.open ("/etc/mce/mce.ini", "r");
234 var g = FileStream.open (new_mce_ini, "r");
236 if (f == null || g == null) {
237 stderr.printf ("failed to open /etc/mce/mce.ini and %s\n", new_mce_ini);
241 var line1 = f.read_line ();
242 var line2 = g.read_line ();
243 while (line1 != null && line2 != null) {
244 if (line1 == line2) {
245 line1 = f.read_line ();
246 line2 = g.read_line ();
250 string[] key_value1 = line1.split ("=");
251 string[] key_value2 = line2.split ("=");
253 if (key_value1.length != 2 || key_value2.length != 2 || !line1.has_prefix ("Pattern")) {
254 stderr.printf ("not allowed to change anything but led patterns:\n-%s\n+%s\n",
259 if (key_value1[0] != key_value2[0]) {
260 stderr.printf ("not allowed to change pattern names\n-%s\n+%s\n",
265 if (!test_pattern (line2, false))
268 line1 = f.read_line ();
269 line2 = g.read_line ();
272 if (line1 != line2) {
273 stderr.printf ("different number of lines\n");
280 int mce_copy (string source, string destination) {
281 var f = FileStream.open (source, "r");
282 var g = FileStream.open (destination, "w");
284 if (f == null || g == null) {
288 var line = f.read_line ();
289 while (line != null) {
290 g.printf ("%s\n", line);
291 line = f.read_line ();
297 public static int main (string[] args) {
298 string usage = "usage: led-pattern-helper test <Pattern>\n";
300 if (args.length != 3) {
301 stderr.printf (usage);
305 if (args[1] == "test") {
306 if (!test_pattern (args[2], true))
312 if (args[1] == "save") {
313 if (!FileUtils.test (args[2], FileTest.IS_REGULAR)) {
314 stderr.printf ("not a regular file: %s\n", args[2]);
318 if (!mce_ini_check (args[2]))
321 // Ok, we're good to go
322 int result = mce_copy ("/etc/mce/mce.ini", "/etc/mce/mce.ini.led-pattern-old");
324 stderr.printf ("failed to copy old mce.ini to mce.ini.led-pattern-old\n");
327 result = mce_copy (args[2], "/etc/mce/mce.ini.led-pattern-new");
329 stderr.printf ("failed to copy new mce.ini to mce.ini.led-pattern-new\n");
332 result = FileUtils.rename ("/etc/mce/mce.ini.led-pattern-new", "/etc/mce/mce.ini");
334 stderr.printf ("failed to replace MCE configuration: %d\n", result);
338 // Moment of truth, restart MCE
342 var command = "initctl stop mce";
343 Process.spawn_command_line_sync (command, null, out error, out exit_status);
344 if (exit_status != 0) {
345 stderr.printf ("stopping mce failed: %d\n%s", exit_status, error);
349 Process.spawn_command_line_sync (command, null, out error, out exit_status);
350 command = "initctl start mce";
351 Process.spawn_command_line_sync (command, null, out error, out exit_status);
352 if (exit_status != 0) {
353 stderr.printf ("starting mce failed: %d\n%s", exit_status, error);
356 } catch (SpawnError e) {
357 stderr.printf ("restarting mce failed: %s", e.message);
364 stderr.printf (usage);