source: roaraudio/roard/driver_pwmled.c @ 5961:06e7fd9e4c25

Last change on this file since 5961:06e7fd9e4c25 was 5961:06e7fd9e4c25, checked in by phi, 10 years ago

Updates of copyright and license headers

File size: 3.6 KB
Line 
1//driver_pwmled.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2014
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "roard.h"
27
28#if !defined(ROAR_WITHOUT_DCOMP_PWMLED) && !defined(ROAR_WITHOUT_VIO_DSTR)
29
30int driver_pwmled_open_vio  (struct roar_vio_calls * inst, char * device, struct roar_audio_info * info, int fh, struct roar_stream_server * sstream) {
31 struct roar_vio_defaults def;
32 struct driver_pwmled * self = roar_mm_malloc(sizeof(struct driver_pwmled));
33
34 if ( self == NULL )
35  return -1;
36
37 if ( fh == -1 ) {
38  if ( device == NULL )
39   device = "/dev/ttyS0";
40
41  if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_WRONLY, 0644) == -1 ) {
42   roar_mm_free(self);
43   return -1;
44  }
45
46  if ( roar_vio_open_dstr(&(self->vio), device, &def, 1) == -1 ) {
47   roar_mm_free(self);
48   return -1;
49  }
50
51  inst->inst = self;
52 } else {
53/*
54  if ( roar_vio_open_fh(inst, fh) == -1 )
55   return -1;
56*/
57  roar_mm_free(self);
58  return -1;
59 }
60
61 inst->read     = NULL;
62 inst->write    = driver_pwmled_write;
63 inst->lseek    = NULL;
64 inst->sync     = NULL;
65 inst->ctl      = driver_pwmled_ctl;
66 inst->close    = driver_pwmled_close;
67
68 info->codec = ROAR_CODEC_DMX512;
69
70 if ( info->rate == g_sa->rate ) {
71  self->rate = 9600;
72 } else {
73  self->rate = info->rate;
74 }
75
76 self->channel = 0;
77
78 if ( roar_light_pwm_new(&(self->state), 16) == -1 ) {
79  roar_mm_free(self);
80  return -1;
81 }
82
83 if ( sstream != NULL )
84  driver_pwmled_ctl(inst, ROAR_VIO_CTL_SET_SSTREAM, sstream);
85
86 return 0;
87}
88
89int     driver_pwmled_close (struct roar_vio_calls * vio) {
90 int ret = roar_vio_close(&(((struct driver_pwmled*)(vio->inst))->vio));
91
92 if ( vio->inst != NULL )
93  roar_mm_free(vio->inst);
94
95 return ret;
96}
97
98// TODO: this function should be optimized.
99ssize_t driver_pwmled_write (struct roar_vio_calls * vio,  void *buf, size_t count) {
100 struct driver_pwmled * self;
101 int value;
102
103 if ( vio == NULL || buf == NULL )
104  return -1;
105
106 if ( count != 512 )
107  return -1;
108
109 self = vio->inst;
110
111 value = ((unsigned char*)buf)[self->channel] / 15;
112
113 if ( roar_light_pwm_set(&(self->state), value) == -1 )
114  return -1;
115
116// ROAR_WARN("driver_pwmled_write(*): value=%i", value);
117
118 if ( value ) {
119                                                                     // bit per word, bit per byte
120  return roar_light_pwm_send(&(self->state), &(self->vio), self->rate/11/8/100) == 0 ? count : -1;
121 }
122
123 return count;
124}
125
126int driver_pwmled_ctl(struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
127 struct driver_pwmled * self;
128
129 if ( vio == NULL )
130  return -1;
131
132 self = vio->inst;
133
134 switch (cmd) {
135  case ROAR_VIO_CTL_SET_SSTREAM:
136    ROAR_STREAM(data)->dir = ROAR_DIR_LIGHT_OUT;
137    ROAR_STREAM_SERVER(data)->codec_orgi = ROAR_CODEC_DMX512;
138   break;
139  case ROAR_VIO_CTL_SET_DMXSCHAN:
140    if ( *(uint16_t*)data > 511 )
141     return -1;
142
143    self->channel = *(uint16_t*)data;
144   break;
145  default:
146   return -1;
147 }
148
149 return 0;
150}
151
152#endif
153
154//ll
Note: See TracBrowser for help on using the repository browser.