aboutsummaryrefslogtreecommitdiff
path: root/dmx512/src/dmxout.c
blob: 08d70b245a1971ab126ab99929c96db209f949a4 (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
/******************************************************
 *
 * dmxout - implementation file
 *
 * copyleft (c) IOhannes m zmölnig
 *
 *   0603:forum::für::umläute:2008
 *
 *   institute of electronic music and acoustics (iem)
 *
 ******************************************************
 *
 * license: GNU General Public License v.2
 *
 ******************************************************/


#include "dmx4pd.h"

#include <unistd.h>
#include <string.h>

static t_class *dmxout_class;

typedef struct _dmxout
{
  t_object x_obj;

  t_inlet *x_portinlet;

  int      x_device;
  t_float  x_port;

} t_dmxout;

static void dmxout_close(t_dmxout*x)
{
  if(x->x_device>=0) {
    close(x->x_device);
  }
  x->x_device=-1;
}


static void dmxout_open(t_dmxout*x, t_symbol*s_devname)
{
  int argc=2;
  const char *args[2] = {"--dmx", s_devname->s_name};
  const char**argv=args;
  char*devname="";
  int fd;

  if(s_devname && s_devname->s_name)
    devname=s_devname->s_name;

  //  strncpy(args[0], "--dmx", MAXPDSTRING);
  //  strncpy(args[1], devname, MAXPDSTRING);

  fd = open (DMXdev(&argc, argv), O_WRONLY);

  if(fd!=-1) {
    dmxout_close(x);
    x->x_device=fd;
  }
}

static void dmxout_doout(t_dmxout*x, short port, unsigned char value)
{
  dmx_t buffer[1] = {value};
  if(x->x_device<=0) {
    pd_error(x, "no DMX universe found");
    return;
  }

  lseek (x->x_device, sizeof(buffer)*port, SEEK_SET);  /* set to the current channel */
  write (x->x_device, buffer, sizeof(buffer)); /* write the channel */
}


static void dmxout_float(t_dmxout*x, t_float f)
{
  unsigned char val=(unsigned char)f;
  short port = (short)x->x_port;
  if(f<0. || f>255.) {
    pd_error(x, "value %f out of bounds [0..255]", f);
    return;
  }
  if(x->x_port<0. || x->x_port>512.) {
    pd_error(x, "port %f out of bounds [0..512]", x->x_port);
    return;
  }

  dmxout_doout(x, port, val);
}

static void *dmxout_new(t_symbol*s, int argc, t_atom*argv)
{
  t_dmxout *x = (t_dmxout *)pd_new(dmxout_class);

  t_symbol*devname=gensym("");

  x->x_portinlet=floatinlet_new(&x->x_obj, &x->x_port);

  x->x_device=-1;
  x->x_port  =0;

  switch(argc) {
  case 0: break;
  case 1:
    if(A_FLOAT==argv->a_type) {
      x->x_port=atom_getfloat(argv);
    } else {
      devname=atom_getsymbol(argv);
    }
    break;
  default:
    if((A_FLOAT==(argv+0)->a_type) && (A_SYMBOL==(argv+1)->a_type)) {
      x->x_port=atom_getfloat(argv+0);
      devname=atom_getsymbol(argv+1);
    } else if(A_FLOAT==(argv+1)->a_type && A_SYMBOL==(argv+0)->a_type) {
      x->x_port=atom_getfloat(argv+1);
      devname=atom_getsymbol (argv+0);
    }
    break;
  }


  dmxout_open(x, devname);
  return (x);
}

static void *dmxout_free(t_dmxout*x)
{
  dmxout_close(x);
}


void dmxout_setup(void)
{
  dmxout_class = class_new(gensym("dmxout"), (t_newmethod)dmxout_new, (t_method)dmxout_free,
                           sizeof(t_dmxout), 
                           0,
                           A_GIMME, A_NULL);

  class_addfloat(dmxout_class, dmxout_float);

#ifdef DMX4PD_POSTBANNER
  DMX4PD_POSTBANNER
#endif
}