summaryrefslogtreecommitdiff
path: root/src/scanout.c
blob: 1b0229c101e45e299e5c18216e2a053d454c0b54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * scanout.c
 *
 * Copyright © 2019 Thomas White <taw@bitwiz.me.uk>
 *
 * This file is part of NanoLight.
 *
 * NanoLight is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <libsoup/soup.h>

#include "nanolight.h"

static void set_val(int *dmx, int base_addr, int attr_offset, float value, int sixteenbit)
{
	/* Minus one to convert DMX address to indexing in 'dmx' array */
	int pos = base_addr + attr_offset - 1;

	if ( sixteenbit ) {
		int val = value * 65535;
		dmx[pos] = (val & 0xff00) >> 8;
		dmx[pos+1] = val & 0xff;
	} else {
		dmx[pos] = value * 255;
	}
}


static void set_prism(int *dmx, struct fixture *fix, int prism)
{
	int i;

	if ( prism > fix->cls->n_prisms ) return;

	for ( i=0; i<fix->cls->n_prism_chans; i++ ) {
		int idx = fix->cls->n_prism_chans * prism + i;
		int chan = fix->cls->prism_chans[i];
		dmx[fix->base_addr+chan-1] = fix->cls->prism_vals[idx] & 0xff;
	}

	if ( fix->cls->prism_flags[prism] & PRISM_ROTATE ) {
		float rotate_val;
		rotate_val = (fix->prism_rotate + 1.0)/2.0;
		set_val(dmx, fix->base_addr, fix->cls->prism_rotate_offset, rotate_val, 0);
	}

	if ( fix->cls->prism_flags[prism] & PRISM_SPIN ) {
		float rotate_val;
		rotate_val = (fix->prism_spin + 1.0)/2.0;
		set_val(dmx, fix->base_addr, fix->cls->prism_spin_offset, rotate_val, 0);
	}
}


static void set_gobo(int *dmx, struct fixture *fix, int gobo)
{
	int i;

	if ( gobo > fix->cls->n_gobos ) return;

	for ( i=0; i<fix->cls->n_gobo_chans; i++ ) {
		int idx = fix->cls->n_gobo_chans * gobo + i;
		int chan = fix->cls->gobo_chans[i];
		dmx[fix->base_addr+chan-1] = fix->cls->gobo_vals[idx] & 0xff;
	}

	if ( fix->cls->gobo_flags[gobo] & GOBO_ROTATE ) {
		float rotate_val;
		rotate_val = (fix->gobo_rotate + 1.0)/2.0;
		set_val(dmx, fix->base_addr, fix->cls->gobo_rotate_offset, rotate_val, 0);
	}

	if ( fix->cls->gobo_flags[gobo] & GOBO_SPIN ) {
		float rotate_val;
		rotate_val = (fix->gobo_spin + 1.0)/2.0;
		set_val(dmx, fix->base_addr, fix->cls->gobo_spin_offset, rotate_val, 0);
	}
}


int scanout_all(struct nanolight *nl)
{
	SoupSession *sess;
	SoupMessage *msg;
	int i;
	int dmx[512];
	char str[8200];
	signed int universe = -1;

	/* Start from zero */
	for ( i=0; i<512; i++ ) dmx[i] = 0;

	/* Loop over fixtures and set values */
	for ( i=0; i<nl->n_fixtures; i++ ) {

		int j;
		struct fixture *fix = &nl->fixtures[i];
		struct fixture_class *cls = fix->cls;

		if ( universe < 0 ) universe = fix->universe;
		if ( fix->universe != universe ) {
			fprintf(stderr, "Sorry, only one universe for now!\n");
			abort();
		}

		for ( j=0; j<cls->n_magic; j++ ) {
			dmx[fix->base_addr + cls->magic_chans[j] - 1] = cls->magic_vals[j];
		}

		if ( cls->attributes & INTENSITY ) {
			set_val(dmx, fix->base_addr, cls->intensity_offset, fix->intensity,
			        cls->attributes16 & INTENSITY);
		}

		if ( cls->attributes & PANTILT ) {
			float pan_val, tilt_val;
			pan_val = (fix->pan + 1.0)/2.0;
			tilt_val = (fix->tilt + 1.0)/2.0;
			set_val(dmx, fix->base_addr, cls->pan_offset, pan_val,
			        cls->attributes16 & PANTILT);
			set_val(dmx, fix->base_addr, cls->tilt_offset, tilt_val,
			        cls->attributes16 & PANTILT);
		}

		if ( (cls->attributes & COLOUR) && (cls->properties & COL_CMY) ) {
			set_val(dmx, fix->base_addr, cls->cyan_offset, fix->cyan,
			        cls->attributes16 & COL_CMY);
			set_val(dmx, fix->base_addr, cls->magenta_offset, fix->magenta,
			        cls->attributes16 & COL_CMY);
			set_val(dmx, fix->base_addr, cls->yellow_offset, fix->yellow,
			        cls->attributes16 & COL_CMY);
		}

		if ( (cls->attributes & COLOUR) && (cls->properties & COL_RGB) ) {
			set_val(dmx, fix->base_addr, cls->red_offset, fix->red,
			        cls->attributes16 & COL_RGB);
			set_val(dmx, fix->base_addr, cls->green_offset, fix->green,
			        cls->attributes16 & COL_RGB);
			set_val(dmx, fix->base_addr, cls->blue_offset, fix->blue,
			        cls->attributes16 & COL_RGB);
		}

		if ( cls->attributes & FOCUS ) {
			set_val(dmx, fix->base_addr, cls->focus_offset, fix->focus,
			        cls->attributes16 & FOCUS);
		}

		if ( cls->attributes & ZOOM ) {
			set_val(dmx, fix->base_addr, cls->zoom_offset, fix->zoom,
			        cls->attributes16 & ZOOM);
		}

		if ( cls->attributes & FROST ) {
			set_val(dmx, fix->base_addr, cls->frost_offset, fix->frost,
			        cls->attributes16 & FROST);
		}

		if ( cls->attributes & IRIS ) {
			set_val(dmx, fix->base_addr, cls->iris_offset, fix->iris,
			        cls->attributes16 & IRIS);
		}

		if ( cls->attributes & GOBO ) {
			set_gobo(dmx, fix, fix->gobo);
		}

		if ( cls->attributes & PRISM ) {
			set_prism(dmx, fix, fix->prism);
		}

	}

	if ( universe == -1 ) return 0;  /* Nothing to do! */

	/* Loop over DMX channels and prepare request */
	snprintf(str, 16, "u=%i&d=", universe);
	for ( i=0; i<512; i++ ) {
		char tmp[6];
		snprintf(tmp, 5, "%i,", dmx[i]);
		strcat(str, tmp);
	}

	/* Send request to OLA */
	sess = soup_session_new();
	msg = soup_message_new("POST", "http://127.0.0.1:9090/set_dmx");
	soup_message_set_request(msg, "application/x-www-form-urlencoded",
	                         SOUP_MEMORY_TEMPORARY, str, strlen(str));
	soup_session_send_message(sess, msg);

	g_object_unref(msg);
	g_object_unref(sess);
	return 0;
}