Improve error handling and reporting in the LED pattern parser
[led-pattern-ed] / src / led-pattern.vala
1 /* This file is part of LED Pattern Editor.
2  *
3  * Copyright (C) 2010 Philipp Zabel
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 errordomain LedPatternError {
20         INVALID_PATTERN;
21 }
22
23 abstract class LedPattern : Object {
24         enum ScreenOn {
25                 DISPLAY_OFF = 0,
26                 DISPLAY_ON = 1,
27                 DISPLAY_OFF_ACT_DEAD = 2,
28                 DISPLAY_ON_ACT_DEAD = 3,
29                 DISPLAY_OFF_OR_ACT_DEAD = 4,
30                 DISABLED = 5
31         }
32
33         public string name;
34         public int priority;
35         public int screen_on;
36         public int timeout;
37
38         public double duration;
39
40         public abstract string dump ();
41         public abstract void parse (string line) throws LedPatternError;
42
43         public signal void changed ();
44 }
45
46 enum CommandType {
47         UNKNOWN,
48         RESET_MUX,
49         SET_PWM,
50         RAMP_WAIT,
51         GO_TO_START,
52         BRANCH,
53         END,
54         TRIGGER
55 }
56
57 class LedCommand : Object {
58         public CommandType type;
59         public double time;
60         public double step_time;
61         public double duration;
62         public int level;
63         public int steps;
64
65         public virtual void set_pwm (int _level) {
66                 type = CommandType.SET_PWM;
67                 level = _level;
68                 changed ();
69         }
70
71         public virtual void ramp_wait (double _step_time, int _steps) {
72                 type = CommandType.RAMP_WAIT;
73                 step_time = _step_time;
74                 steps = _steps;
75                 if (steps < 0)
76                         duration = step_time * (1 - steps);
77                 else
78                         duration = step_time * (steps + 1);
79                 changed ();
80         }
81
82         public virtual void go_to_start () {
83                 type = CommandType.GO_TO_START;
84                 changed ();
85         }
86
87         public virtual void end (bool reset) {
88                 type = CommandType.END;
89                 steps = reset ? -255 : 0;
90                 changed ();
91         }
92
93         public signal void changed ();
94 }
95