source: roaraudio/roard/driver_pwmled.c @ 1981:62fd1e3d0daf

Last change on this file since 1981:62fd1e3d0daf was 1981:62fd1e3d0daf, checked in by phi, 15 years ago

optimize a bit

File size: 2.8 KB
Line 
1//driver_pwmled.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include "roard.h"
26
27int driver_pwmled_open_vio  (struct roar_vio_calls * inst, char * device, struct roar_audio_info * info, int fh) {
28 struct roar_vio_defaults def;
29 struct driver_pwmled * self = malloc(sizeof(struct driver_pwmled));
30
31 if ( self == NULL )
32  return -1;
33
34 if ( fh == -1 ) {
35  if ( device == NULL )
36   device = "/dev/ttyS0";
37
38  if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_WRONLY, 0644) == -1 ) {
39   free(self);
40   return -1;
41  }
42
43  if ( roar_vio_open_dstr(&(self->vio), device, &def, 1) == -1 ) {
44   free(self);
45   return -1;
46  }
47
48  inst->inst = self;
49 } else {
50/*
51  if ( roar_vio_open_fh(inst, fh) == -1 )
52   return -1;
53*/
54  free(self);
55  return -1;
56 }
57
58 inst->read     = NULL;
59 inst->write    = driver_pwmled_write;
60 inst->lseek    = NULL;
61 inst->nonblock = NULL;
62 inst->sync     = NULL;
63 inst->ctl      = driver_pwmled_ctl;
64 inst->close    = driver_pwmled_close;
65
66 info->codec = ROAR_CODEC_DMX512;
67
68 if ( roar_light_pwm_new(&(self->state), 16) == -1 ) {
69  free(self);
70  return -1;
71 }
72
73 return 0;
74}
75
76int     driver_pwmled_close (struct roar_vio_calls * vio) {
77 int ret = roar_vio_close(&(((struct driver_pwmled*)(vio->inst))->vio));
78
79 if ( vio->inst != NULL )
80  free(vio->inst);
81
82 return ret;
83}
84
85// TODO: this function should be optimized.
86ssize_t driver_pwmled_write (struct roar_vio_calls * vio,  void *buf, size_t count) {
87 struct driver_pwmled * self = vio->inst;
88
89 if ( vio == NULL || buf == NULL )
90  return -1;
91
92 if ( count != 512 )
93  return -1;
94
95 if ( roar_light_pwm_set(&(self->state), ((unsigned char*)buf)[0] / 16) == -1 )
96  return -1;
97
98 return roar_light_pwm_send(&(self->state), &(self->vio), 1) == 0 ? count : -1;
99}
100
101int driver_pwmled_ctl(struct roar_vio_calls * vio, int cmd, void * data) {
102
103 if ( vio == NULL )
104  return -1;
105
106 switch (cmd) {
107  case ROAR_VIO_CTL_SET_SSTREAM:
108    ROAR_STREAM(data)->dir = ROAR_DIR_LIGHT_OUT;
109    ROAR_STREAM_SERVER(data)->codec_orgi = ROAR_CODEC_DMX512;
110   break;
111  default:
112   return -1;
113 }
114
115 return 0;
116}
117
118//ll
Note: See TracBrowser for help on using the repository browser.