]> git.zerfleddert.de Git - fnordlicht-mini/blame - firmware/fnordlicht-firmware/script.c
fix comment
[fnordlicht-mini] / firmware / fnordlicht-firmware / script.c
CommitLineData
ec1bef8e 1/* vim:ts=4 sts=4 et tw=80
2 *
3 * fnordlicht firmware
4 *
5 * for additional information please
6 * see http://lochraster.org/fnordlichtmini
7 *
8 * (c) by Alexander Neumann <alexander@bumpern.de>
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 3 as published by
12 * the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include <avr/pgmspace.h>
24#include <string.h>
25#include "globals.h"
26#include "script.h"
27#include "static_programs.h"
28#include "pwm.h"
29
30#if CONFIG_SCRIPT
31
32/* global variables */
33struct global_script_t global_script;
34
35void script_init(void)
36{
37 /* initialize global structures */
38#ifdef INIT_ZERO
39 for (uint8_t i = 0; i < CONFIG_SCRIPT_TASKS; i++) {
40 global_script.tasks[i].enable = 0;
41 PT_INIT(&global_script.tasks[i].pt);
42 }
43#endif
44
45 /* initialize timer, delay before start is 200ms */
46 timer_set(&global_script.timer, 20);
47}
48
49void script_start_default(void)
50{
51#ifdef CONFIG_SCRIPT_DEFAULT
52#if CONFIG_SCRIPT_DEFAULT == 0
53 /* enable colorwheel program */
54 union program_params_t params;
55 params.colorwheel.hue_start = 0;
56 params.colorwheel.hue_step = 60;
57 params.colorwheel.add_addr = 0;
58 params.colorwheel.saturation = 255;
59 params.colorwheel.value = 255;
60
61 params.colorwheel.fade_step = 1;
62 params.colorwheel.fade_delay = 2;
63 params.colorwheel.fade_sleep = 0;
64
65 script_start(0, 0, &params);
66#elif CONFIG_SCRIPT_DEFAULT == 1
7e0e8f7c 67 /* enable random program */
ec1bef8e 68 union program_params_t params;
69 params.random.seed = 23;
70 params.random.use_address = 0;
71 params.random.wait_for_fade = 1;
72 params.random.min_distance = 60;
73
74 params.random.saturation = 255;
75 params.random.value = 255;
76
77 params.random.fade_step = 1;
78 params.random.fade_delay = 3;
79 params.random.fade_sleep = 100;
80
81 script_start(0, 1, &params);
82#else
83#warning "CONFIG_SCRIPT_DEFAULT has unknown value! No default program is started."
84#endif
85#endif
86}
87
88void script_poll(void)
89{
90 if (global_script.disable)
91 return;
92
93 if (timer_expired(&global_script.timer)) {
94 for (uint8_t i = 0; i < CONFIG_SCRIPT_TASKS; i++) {
95 struct process_t *task = &global_script.tasks[i];
96 if (task->enable) {
97 /* run task, disable if exited */
98 if (PT_SCHEDULE(task->execute(task)) == 0)
99 task->enable = false;
100 }
101 }
102
103 /* recall after 100ms */
104 timer_set(&global_script.timer, 10);
105 }
106}
107
108void script_stop(void)
109{
110 /* stop all tasks */
111 for (uint8_t i = 0; i < CONFIG_SCRIPT_TASKS; i++) {
112 global_script.tasks[i].enable = false;
113 PT_INIT(&global_script.tasks[i].pt);
114 }
115
116 /* disable global */
117 global_script.disable = true;
118}
119
120void script_start(uint8_t task, uint8_t index, union program_params_t *params)
121{
122 /* check for valid index */
123 if (index >= STATIC_PROGRAMS_LEN)
124 return;
125
126 /* enable global */
127 global_script.disable = false;
128
129 /* copy params from pointer to task structure */
130 memcpy(&global_script.tasks[task].params, params, sizeof(union program_params_t));
131
132 /* load program handler */
133 global_script.tasks[task].execute = (program_handler)pgm_read_word(&static_programs[index]);
134
135 /* enable script */
136 global_script.tasks[task].enable = true;
137
138 /* reset timer */
139 timer_set(&global_script.timer, 10);
140}
141
142#endif
Impressum, Datenschutz