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 | |
---|
27 | #ifndef ROAR_WITHOUT_DCOMP_PWMLED |
---|
28 | |
---|
29 | int driver_pwmled_open_vio (struct roar_vio_calls * inst, char * device, struct roar_audio_info * info, int fh, struct roar_stream_server * sstream) { |
---|
30 | struct roar_vio_defaults def; |
---|
31 | struct driver_pwmled * self = malloc(sizeof(struct driver_pwmled)); |
---|
32 | |
---|
33 | if ( self == NULL ) |
---|
34 | return -1; |
---|
35 | |
---|
36 | if ( fh == -1 ) { |
---|
37 | if ( device == NULL ) |
---|
38 | device = "/dev/ttyS0"; |
---|
39 | |
---|
40 | if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_WRONLY, 0644) == -1 ) { |
---|
41 | free(self); |
---|
42 | return -1; |
---|
43 | } |
---|
44 | |
---|
45 | if ( roar_vio_open_dstr(&(self->vio), device, &def, 1) == -1 ) { |
---|
46 | free(self); |
---|
47 | return -1; |
---|
48 | } |
---|
49 | |
---|
50 | inst->inst = self; |
---|
51 | } else { |
---|
52 | /* |
---|
53 | if ( roar_vio_open_fh(inst, fh) == -1 ) |
---|
54 | return -1; |
---|
55 | */ |
---|
56 | free(self); |
---|
57 | return -1; |
---|
58 | } |
---|
59 | |
---|
60 | inst->read = NULL; |
---|
61 | inst->write = driver_pwmled_write; |
---|
62 | inst->lseek = NULL; |
---|
63 | inst->nonblock = 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 | 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 | |
---|
89 | int 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 | free(vio->inst); |
---|
94 | |
---|
95 | return ret; |
---|
96 | } |
---|
97 | |
---|
98 | // TODO: this function should be optimized. |
---|
99 | ssize_t driver_pwmled_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
100 | struct driver_pwmled * self = vio->inst; |
---|
101 | int value; |
---|
102 | |
---|
103 | if ( vio == NULL || buf == NULL ) |
---|
104 | return -1; |
---|
105 | |
---|
106 | if ( count != 512 ) |
---|
107 | return -1; |
---|
108 | |
---|
109 | value = ((unsigned char*)buf)[self->channel] / 15; |
---|
110 | |
---|
111 | if ( roar_light_pwm_set(&(self->state), value) == -1 ) |
---|
112 | return -1; |
---|
113 | |
---|
114 | // ROAR_WARN("driver_pwmled_write(*): value=%i", value); |
---|
115 | |
---|
116 | if ( value ) { |
---|
117 | // bit per word, bit per byte |
---|
118 | return roar_light_pwm_send(&(self->state), &(self->vio), self->rate/11/8/100) == 0 ? count : -1; |
---|
119 | } |
---|
120 | |
---|
121 | return count; |
---|
122 | } |
---|
123 | |
---|
124 | int driver_pwmled_ctl(struct roar_vio_calls * vio, int cmd, void * data) { |
---|
125 | struct driver_pwmled * self = vio->inst; |
---|
126 | |
---|
127 | if ( vio == NULL ) |
---|
128 | return -1; |
---|
129 | |
---|
130 | switch (cmd) { |
---|
131 | case ROAR_VIO_CTL_SET_SSTREAM: |
---|
132 | ROAR_STREAM(data)->dir = ROAR_DIR_LIGHT_OUT; |
---|
133 | ROAR_STREAM_SERVER(data)->codec_orgi = ROAR_CODEC_DMX512; |
---|
134 | break; |
---|
135 | case ROAR_VIO_CTL_SET_DMXSCHAN: |
---|
136 | if ( *(uint16_t*)data > 511 ) |
---|
137 | return -1; |
---|
138 | |
---|
139 | self->channel = *(uint16_t*)data; |
---|
140 | break; |
---|
141 | default: |
---|
142 | return -1; |
---|
143 | } |
---|
144 | |
---|
145 | return 0; |
---|
146 | } |
---|
147 | |
---|
148 | #endif |
---|
149 | |
---|
150 | //ll |
---|