cueOS  2.4
cueOS - Universal Show Control OS for ARM
DMX512_scene.c
1
2/***============================================================================================================================
3 * Dependencies inclusion
4 * Necessary dependencies should be declared here. Header file should contain as little dependecies declarations as possible
5 *=============================================================================================================================*/
6
7#include "cueos_config.h"
8#if cueOS_CONFIG_NODETYPE == cueOS_NODETYPE_SLAVE_DMX
9#include <stdlib.h>
10#include "math.h"
11#include "cmsis_os.h"
12#include "DMX512_driver.h"
13#include "DMX512_scene.h"
14
15
16/***===========================================================================================================================
17 * Private functions definitions
18 * These functions are only accessible from within the file's scope
19 *=============================================================================================================================*/
20
29static int16_t _DMX512_scene_search(DMX512_scene_s *scene, uint16_t fixture_id){
30 for(uint16_t i=0; i<scene->fixture_preset_count; i++){
31 if(fixture_id == scene->fixture_presets[i].fixture->id){
32 return i;
33 }
34 }
35 return -1;
36}
37
43static void _DMX512_scene_fadein(DMX512_scene_s *scene){
44 for(uint16_t i=0; i<scene->fixture_preset_count; i++){
45 for(uint16_t j=0; j<scene->fixture_presets[i].ch_count; j++){
46 uint16_t addr = scene->fixture_presets[i].fixture->addr + scene->fixture_presets[i].channels[j];
47 uint16_t value = scene->fixture_presets[i].values[j];
48 uint32_t time = ms_chronometer_get_elapsed_ms(&scene->ms_chronometer);
49 if(time < scene->fadein_time){
50 value = floor(((float)value/(float)scene->fadein_time)*((float)(time)));
51 }else{
52 scene->state = DMX512_SCENE_IDLE;
53 }
54
55 DMX512_driver_set_single(addr, value);
56 }
57 }
58}
59
64static void _DMX512_scene_fadeout(DMX512_scene_s *scene){
65 for(uint16_t i=0; i<scene->fixture_preset_count; i++){
66 for(uint16_t j=0; j<scene->fixture_presets[i].ch_count; j++){
67 uint16_t addr = scene->fixture_presets[i].fixture->addr + scene->fixture_presets[i].channels[j];
68 uint16_t value = scene->fixture_presets[i].values[j];
69 uint32_t time = ms_chronometer_get_elapsed_ms(&scene->ms_chronometer);
70 if(time < scene->fadeout_time){
71 value = floor(((float)value/(float)scene->fadein_time)*((float)(time + value)));
72 }else{
73 scene->state = DMX512_SCENE_IDLE;
74 }
75
76 DMX512_driver_set_single(addr, value);
77 }
78 }
79}
80
81
82/***============================================================================================================================
83 * Public functions definitions
84 * These functions can be accessed outside of the file's scope
85 * see DMX512_scene.h for declarations
86 *=============================================================================================================================*/
87
96DMX512_scene_s DMX512_scene_new(uint16_t id, uint16_t fadein_time, uint16_t fadeout_time){
98 scene.id = id;
99 scene.fadein_time = fadein_time;
100 scene.fadeout_time = fadeout_time;
102 return scene;
103}
104
113
115
116 if(fixture_preset.status == DMX512_FIXTURE_PRESET_UNINITIALISED){
118 }else if(_DMX512_scene_search(scene, fixture_preset.fixture->id) >= 0){
120 }else{
121 scene->fixture_presets = (DMX512_fixture_preset_s*) pvPortRealloc(scene->fixture_presets, sizeof(DMX512_fixture_preset_s) * (scene->fixture_preset_count + 1));
122 scene->fixture_presets[scene->fixture_preset_count] = fixture_preset;
123 scene->fixture_preset_count++;
124 }
125
126 return err;
127
128}
129
138
140 int16_t index = _DMX512_scene_search(scene, id);
141
142 if(index >= 0){
143 for(uint16_t i=index+1; i< scene->fixture_preset_count; i++){
144 scene->fixture_presets[i-1] = scene->fixture_presets[i];
145 }
146 scene->fixture_preset_count--;
148 scene->fixture_presets = pvPortRealloc(scene->fixture_presets, sizeof(DMX512_fixture_s) * (scene->fixture_preset_count));
149 err = DMX512_ENGINE_OK;
150 }
151
152 return err;
153
154}
155
164 int16_t index = _DMX512_scene_search(scene, id);
165 if(index >= 0){
166 return &scene->fixture_presets[index];
167 }else{
168 return NULL;
169 }
170}
171
179 scene->state = DMX512_SCENE_FADEIN;
180}
181
190}
191
199 switch(scene->state){
200 case DMX512_SCENE_FADEIN: _DMX512_scene_fadein(scene); break;
201 case DMX512_SCENE_FADEOUT: _DMX512_scene_fadeout(scene); break;
202 case DMX512_SCENE_IDLE: /*TODO: handle IDLE state ?*/ break;
203 }
204}
205
213 if(scene != NULL){
214 for(uint16_t i=0; i<scene->fixture_preset_count; i++){
216 }
217 scene->fadein_time = 0;
218 scene->fadeout_time = 0;
220 scene->status = DMX512_SCENE_IDLE;
221 vPortFree(scene->fixture_presets);
222 }
223}
224
225#endif
DMX512_engine_err_e
DMX512 engine error index constants.
Definition: DMX512_defs.h:24
@ DMX512_ENGINE_INSTANCE_UNDEFINED
Definition: DMX512_defs.h:28
@ DMX512_ENGINE_INSTANCE_DUPLICATE
Definition: DMX512_defs.h:26
@ DMX512_ENGINE_OK
Definition: DMX512_defs.h:25
@ DMX512_ENGINE_INSTANCE_INVALID
Definition: DMX512_defs.h:27
DMX512_driver_err_e DMX512_driver_set_single(uint16_t address, uint8_t value)
Sets a buffer's address to a given value.
void DMX512_fixture_preset_free(DMX512_fixture_preset_s *fixture_preset)
Frees a fixture preset's dynamically allocated ressources and resets its values parameters to default...
@ DMX512_FIXTURE_PRESET_UNINITIALISED
void DMX512_scene_manage(DMX512_scene_s *scene)
State machine to manage scene at a given state.
Definition: DMX512_scene.c:198
void DMX512_scene_stop(DMX512_scene_s *scene)
Starts a scene fade-out process.
Definition: DMX512_scene.c:187
DMX512_fixture_preset_s * DMX512_scene_get_fixture_preset(DMX512_scene_s *scene, uint16_t id)
Gets a fixture instance from the pool.
Definition: DMX512_scene.c:163
DMX512_scene_s DMX512_scene_new(uint16_t id, uint16_t fadein_time, uint16_t fadeout_time)
Creates and initialises a new scene instance.
Definition: DMX512_scene.c:96
#define DMX512_SCENE_DEFAULT
scene instance default values
Definition: DMX512_scene.h:25
DMX512_engine_err_e DMX512_scene_add_fixture_preset(DMX512_scene_s *scene, DMX512_fixture_preset_s fixture_preset)
Adds a fixture preset instance into the scene.
Definition: DMX512_scene.c:112
DMX512_engine_err_e DMX512_scene_del_fixture_preset(DMX512_scene_s *scene, uint16_t id)
Deletes a fixture preset instance from the scene.
Definition: DMX512_scene.c:137
void DMX512_scene_start(DMX512_scene_s *scene)
Starts a scene fade-in process.
Definition: DMX512_scene.c:177
void DMX512_scene_free(DMX512_scene_s *scene)
Resets a scene to its uninitialised state. frees dynamically allocated scene Data.
Definition: DMX512_scene.c:212
@ DMX512_SCENE_UNINITIALISED
Definition: DMX512_scene.h:34
@ DMX512_SCENE_INITIALISED
Definition: DMX512_scene.h:35
@ DMX512_SCENE_FADEIN
Definition: DMX512_scene.h:46
@ DMX512_SCENE_FADEOUT
Definition: DMX512_scene.h:47
@ DMX512_SCENE_IDLE
Definition: DMX512_scene.h:45
void ms_chronometer_reset(ms_chronometer_s *ms_chronometer)
Resets an mschronometer instance time delta to zero.
uint32_t ms_chronometer_get_elapsed_ms(ms_chronometer_s *ms_chronometer)
Returns elapsed time in milliseconds since last reset.
DMX512 fixture preset structure object.
DMX512_fixture_preset_status status
DMX512 fixture structure object.
DMX512 scene structure object.
Definition: DMX512_scene.h:54
ms_chronometer_s ms_chronometer
Definition: DMX512_scene.h:60
DMX512_fixture_preset_s * fixture_presets
Definition: DMX512_scene.h:59
uint16_t fadein_time
Definition: DMX512_scene.h:56
DMX512_scene_state_e state
Definition: DMX512_scene.h:61
DMX512_scene_status_e status
Definition: DMX512_scene.h:62
uint16_t fixture_preset_count
Definition: DMX512_scene.h:58
uint16_t fadeout_time
Definition: DMX512_scene.h:57