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 class LedPatternRX51 : LedPattern {
20 public string led_map;
21 public List<LedCommandRX51> engine1;
22 public List<LedCommandRX51> engine2;
24 public void parse (string line) {
25 string[] key_value = line.split ("=");
27 if (key_value.length != 2) {
28 print ("pattern line does not contain '=': %s\n", line);
32 string[] p = key_value[1].split (";");
34 print ("pattern does not contain 6 components: %d\n", p.length);
38 if (p[4].length > 16*4 || p[5].length > 16*4) {
39 print ("pattern too long!\n");
44 priority = p[0].to_int ();
45 screen_on = p[1].to_int ();
46 timeout = p[2].to_int ();
48 engine1 = parse_pattern (p[4]);
49 engine2 = parse_pattern (p[5]);
51 if (engine1.first ().data.code != 0x9d80) {
52 print ("engine1 pattern doesn't start with refresh mux command\n");
54 if (engine2.first ().data.code != 0x9d80) {
55 print ("engine2 pattern doesn't start with refresh mux command\n");
61 private List<LedCommandRX51> parse_pattern (string pattern) {
62 var list = new List<LedCommandRX51> ();
63 var length = pattern.length;
68 char *p = ((char*) pattern) + length - 4;
69 while (p >= (char*) pattern) {
70 var command = new LedCommandRX51.with_code ((uint16) ((string) p).to_ulong (null, 16));
71 command.changed.connect (on_changed);
72 list.prepend (command);
80 public string dump () {
81 return "%s=%d;%d;%d;%s;%s;%s".printf (name, priority, screen_on, timeout, led_map,
82 dump_pattern (engine1), dump_pattern (engine2));
85 private string dump_pattern (List<LedCommandRX51> list) {
87 foreach (LedCommandRX51 command in list) {
88 result += "%04x".printf (command.code);
93 public LedPatternRX51 copy () {
94 var pattern = new LedPatternRX51 ();
96 pattern.name = name.dup ();
97 pattern.priority = priority;
98 pattern.screen_on = screen_on;
99 pattern.timeout = timeout;
101 pattern.duration = duration;
103 pattern.led_map = led_map;
104 pattern.engine1 = deep_copy (pattern, engine1);
105 pattern.engine2 = deep_copy (pattern, engine2);
110 public List<LedCommandRX51> deep_copy (LedPatternRX51 pattern, List<LedCommandRX51> list) {
111 var list2 = new List<LedCommandRX51> ();
113 foreach (LedCommandRX51 command in list) {
114 var command2 = command.copy ();
115 command2.changed.connect (pattern.on_changed);
116 list2.append (command2);
122 public void replace_with (LedPatternRX51 pattern) {
124 priority = pattern.priority;
125 screen_on = pattern.screen_on;
126 timeout = pattern.timeout;
128 duration = pattern.duration;
130 led_map = pattern.led_map;
131 engine1 = deep_copy (this, pattern.engine1);
132 engine2 = deep_copy (this, pattern.engine2);
138 // calculate timing and level info
141 foreach (LedCommandRX51 command in engine1) {
143 time += command.duration;
144 if (command.type == CommandType.SET_PWM) {
145 level = command.level;
147 command.level = level;
148 level += command.steps;
160 class LedCommandRX51 : LedCommand {
163 public LedCommandRX51 () {
166 public LedCommandRX51.with_code (uint16 _code) {
168 if ((code & 0x8000) == 0) {
169 if (code == 0x0000) {
170 type = CommandType.REPEAT;
171 } else if ((code & 0x3e00) != 0) {
172 type = CommandType.RAMP_WAIT;
174 step_time = code >> 9;
175 if ((code & 0x4000) == 0)
176 step_time = (code >> 9) * 0.49;
178 step_time = ((code & 0x3e00) >> 9) * 15.6;
180 duration = step_time * (steps + 1);
181 if ((code & 0x100) != 0)
184 type = CommandType.SET_PWM;
189 type = CommandType.RESET_MUX;
191 type = CommandType.STOP;
192 //if (code == 0xe0??)
193 // type = CommandType.TRIGGER_WAIT;
197 public override void set_pwm (int _level) {
198 code = 0x4000 | _level;
199 base.set_pwm (_level);
202 public override void ramp_wait (double _step_time, int _steps) requires (_step_time >= 0.49) {
204 if (_step_time <= 31*0.49) {
205 step_time = (int) ((_step_time + 0.001) / 0.49);
206 code = (uint16) step_time << 9;
207 _step_time = step_time * 0.49;
208 } else if (_step_time <= 31*15.6) {
209 step_time = (int) ((_step_time + 0.01) / 15.6);
210 code = 0x4000 | (step_time << 9);
211 _step_time = step_time * 15.6;
216 code |= 0x100 | (-_steps);
220 base.ramp_wait (_step_time, _steps);
223 public LedCommandRX51 copy () {
224 var command = new LedCommandRX51 ();
228 command.step_time = step_time;
229 command.duration = duration;
230 command.level = level;
231 command.steps = steps;